nox is a convolutional encoder-decoder residual neural network to remove stars from astrophotographs. It uses almost the same architecture as StarNet and was trained using the same technique with adversarial and perceptual losses. The essential difference between StarNet and nox (besides the use of a different normalization technique) is that nox was trained on a synthetic dataset with artificial but physically realistic stars superimposed onto background images to create training pairs. In contrast, the creator of StarNet created the dataset by manually removing stars from real astrophotographs. nox therefore produces different results to StarNet because it has seen different data during training. I hope that my approach might provide better generalization to diverse optical systems because the training data includes a wide range of star resolutions, FWHMs, noise, gain, midtones transfer stretches, diffraction spikes, atmospheric conditions (blurring), star colors, optical aberration (asymmetry, color fringing), etc. Whether I have succeeded, however, remains to be proved by the experience of users!
I am providing here the trained weights and the Python code to use them to remove stars from your own images. You can use these directly without having to do any dataset generation or training, although you can do this if you wish. I am also providing the full Python code here to generate your own training datasets, and to train a version of nox.
Most of the code was written in Python. Generating the dataset relies on the Astropy and Photutils Python libraries. I used Tensorflow for machine learning. Other Python libraries are also used, and so you will need to have installed all the relevant packages as per the imports before you can use my code.
I have created a standalone compiled inference tool, which I think is much easier to use than my Python scripts. You can download it here.
The tool is cross-compilable for Mac and Linux. I've provided the C++ source code, so if that interests you, by all means, go ahead and recompile it for your operating system of choice. You will need to download, compile and link the OpenCV library as part of this. You will also need the Tensorflow C API and the CPPFlow header library.
- Download
nox.exe, the accompanying.pbmodel files, andtensorflow.dllfrom here, and store them all in the same location. - Since
nox.exeis a command line tool, in Windows start a Command prompt. (Press the Start button and typecmdand press enter.) - Drag and drop the downloaded
nox.exeonto the Command prompt. nox.exereceives command line arguments. For help, add--help(with a space before the hyphens) and press enter. This displays information about the accepted arguments.- Use the up arrow key on the keyboard to retrieve and edit the last command.
- Basic usage is:
"C:\path\to\nox.exe" -f "C:\path\to\input_image.tiff" -r. This will read"C:\path\to\input_image.tiff", remove the stars from it, and create the output file"C:\path\to\input_image_nox.tiff", overwriting it if it already exists.-findicates that what follows ("C:\path\to\input_image.tiff") is the input file.-rindicates that the output file should be overwritten if it already exists. Many other settings are possible, including patch size, stride, and batch size. - To enable temporary automatic stretching of linear data, set the
-lflag (the letter 'ell'). The stretch parameters can be adjusted by tuning the-dparameter (shadows clipping) and the-tparameter (target mean).
More details are below, and by specifying '--help' in the command line arguments.
Despite the official TF builds with GPU support only being available for Linux, it has been reported that it is possible to run nox.exe on a GPU, where it runs extremely fast compared to StarNet or StarXTerminator. I have been unable to test this myself (my current personal laptop is very old and does not have a GPU!). The following guidance is thanks to naixx.
I used TensorFlow as well as other CUDA stuff from PixInsight
bin/. I believe you can also get them from StarNet, StarXTerminator, or other sources. Additional CUDA libs were also required which I downloaded from NVIDIA.
cublas64_11.dll
cublasLt64_11.dll
cudart64_110.dll
cudnn_adv_infer64_8.dll
cudnn_adv_train64_8.dll
cudnn_cnn_infer64_8.dll
cudnn_cnn_train64_8.dll
cudnn_ops_infer64_8.dll
cudnn_ops_train64_8.dll
cudnn64_8.dll
cufft64_10.dll
cusparse64_11.dll
libwinpthread-1.dll
tensorflow.dll
zlibwapi.dll
Temporary automatic stretching is enabled by setting the -l (letter 'ell') command line flag. This will automatically apply a temporary midtones transfer function (MTF) stretch before removing the stars, and then remove the stretch again:
- The shadows will be clipped at a number of standard deviations relative to the median pixel value. The default number of standard deviations is
-0.75, which can be adjusted by setting the-dparameter (shadows clipping). - The midtones value will be set to achieve a mean pixel value. The default mean pixel value is
0.1875, which can be adjusted by setting the-tparameter (target mean).
Any clipped pixels will be restored to their value in the original image before any processing. Since this only affects dark pixels, it is very unlikely to affect star removal results.
- Ensure you have installed all the relevant packages as per the imports in
nox.py. I recommend using avenvvirtual environment for this, but that is up to you. - Download the trained weights from here and move the
generator_color.h5andgenerator_grayscale.h5weights files into the same directory asnox.py. - Set the desired global parameters on lines 26 to 44. These variables cover both inference and training. The relevant ones for inference are:
- Set
n_channelsto1to process grayscale images, or to convert color images to grayscale before processing. Otherwise leaven_channelsas3. - The
patch_sizeandstridevariables adjust the size and overlap of each square tile. You can experiment with these if you like. BATCH_SIZEis relevant for inference as well as training. It sets the number of samples per batch of computation. If you set this too high, you will run into out-of-memory errors, in which case reduce it, even down to1.
- Set
- Run
python nox.py infer input_image.png
This will create two files, starry.png and starless.png: starry.png is a copy of the input image, and starless.png is the result of star removal. Note that the input and output formats do not necessarily need to be png.
The inference examples folder contains a selection of starry and starless examples. Here are a couple of thumbnail previews from that folder (click for full resolution or see the folder for more):
This is not necessary to use nox to remove stars from your images, but may be of interest to some nevertheless.
I added artificial stars to images from the SIDD and RENOIR datasets (which were originally intended for noise reduction).
- Download the sRGB images only from "SIDD-Small Dataset". The "Data" directory contains 160 noisy and ground truth image pairs. We are obviously not interested in these pairings, but the different noise levels are useful for generalization, and so all 320 images are included as background images.
- Download the "aligned" datasets for the three different cameras, which contains 120 noisy and ground truth image pairs. Again, all these images are included in the list of background images.
Clearly, these datasets are not related to astrophotography in any way. Nevertheless, they seem adequate to train the net to infer some believable background in place of removed stars.
- Ensure that the arguments to the
get_images_pathsfunction on lines 299 (or 300) point to the downloaded SIDD and RENOIR datasets respectively. - Set the desired global parameters on lines 16 - 21. In particular
cpusthe number of CPUs to use (I used 10), andimcountthe number of images to generate (I generated 5000). The performance of the resulting network is closely related to the number of training images. LeavecolorasTrue, even if you want to train a grayscale net, as the conversion from color to grayscale happens during training if necessary. This means you only need to generate one dataset. - Ensure you have installed all the relevant packages as per the imports in
GenerateStars.py. I recommend using avenvvirtual environment for this, but that is up to you. - Open a command prompt and
cdto the directory containingGenerateStars.py. Before running, consider opening ascreenwhich will allow you to detach and reattach your terminal window during the long execution time, particularly if you will run it remotely. - Run
python GenerateStars.py
The nox data folder contains some example training pairs. Here are a couple of thumbnail previews of training pairs from that folder (click for full resolution or see the folder for more):
-
Ensure you have installed all the relevant packages as per the imports in
nox.py. I recommend using avenvvirtual environment for this, but that is up to you. -
Set the desired global parameters on lines 26 to 44. These variables cover settings for both inference and training. For training the relevant ones are:
epochssets the maximum number of training epochs. I prefer to set this to a large number (1500) and kill training withCtrl-Cwhen ready.- Set
n_channelsto1to train grayscale weights, or to3to train color weights. Both use the same color dataset, and conversion to grayscale happens during training, if required. - The
patch_sizeandstridevariables adjust the size and overlap of each square tile. You can experiment with these. BATCH_SIZEsets the number of samples per batch of computation. If you set this too high, you will run into out-of-memory errors, in which case reduce it, even down to1.emasets the decay rate of the exponential moving average. Each epoch, the model's metrics keepema% of the existing state and(1 - ema)% of the new state. This has been tuned to produce smooth model metrics that decay fast enough to allow timely assessment of plateauing and learning rate reduction without overtraining.lrsets the learning ratepatiencesets the number of epochs with no improvement after which learning rate will be reduced.cooldownsets the number of epochs to wait before resuming normal operation after the learning rate has been reduced.validationsets whether the dataset should be split into training (80%) and validation (20%) sets. The model will not train on the validation data, and will evaluate the model metrics on this data at the end of each epoch.save_freqsets how many training epochs between saving the progress. Everysave_freqepochs, the model metrics are saved inhistory.csvand plotted intraining.png(for example, as shown below). A random image from the validation set is also processed and saved asinput.png,output.pngandgt.png.
-
Open a command prompt and
cdto the directory containingnox.py. Before running, consider opening ascreenwhich will allow you to detach and reattach your terminal window during the long execution time, particularly if you will run it remotely. -
Run
python nox.py train
Note that model training can be resumed (including from the same optimizer state) from the files saved every save_freq epochs by reissuing the command above.
My code and trained weights are Copyright (c) 2023 Christopher Harvey, and are made available under the MIT License, detailed in the LICENSE file inside the repository.
Sections of code in nox.py, as indicated by comments, are Copyright (c) 2018-2019 Nikita Misiura and are used under the same MIT License.
