Code Monkey home page Code Monkey logo

starnet's Introduction

Update 2

Updated documentation for using Tensorflow-directml on windows for broad support on any modern gpu with sufficient memory.

Update

Pushed a new implementation os starnet in TF2.x. The whole implementation is in one file starnet_v1_TF2.py.

I also created a few Jupyter notebooks for ease of use:

  1. starnet_v1_TF2_transform.ipynb - loads and transforms an image.
  2. starnet_v1_TF2.ipynb - more detailed example that loads a model and shows how to train it (really simple as well I think).

Weights for the new model can be found here.


StarNet is a neural network that can remove stars from images in one simple step leaving only background.

More technically it is a convolutional residual net with encoder-decoder architecture and with L1, Adversarial and Perceptual losses.

Small example:



Intro

Star removal using classical methods is a very tricky and painful multi-step procedure, which is hard to master and hard to get nice results from, especially in case of images busy with stars.

This neural net will remove most of stars from input image in one step, leaving only really huge ones, and leaving (well, hopefully) intact all other small bright things whose shape is significantly different from that of a typical star, like small spiral galaxies, fine details in nebulosity, HH objects, etc.

It is intended to be used by astrophotographers. Primary use is for background nebulosity enhancement in rich star fields, but it can also help in creation of nice starless image.

Literature

This code is partially based on pix2pix code and ideas from pix2pix paper.

pix2pix code: https://github.com/phillipi/pix2pix

pix2pix paper: Image-to-Image Translation with Conditional Adversarial Networks

Udea of using Perceptual Adversarial losses is from this paper as well as some other ideas:

Perceptual Adversarial Networks for Image-to-Image Transformation

Other papers I took ideas from or found useful during development:

Image De-raining Using a Conditional Generative Adversarial Network

Image Restoration Using Convolutional Auto-encoders with Symmetric Skip Connections

Removing rain from single images via a deep detail network

Usage

Its primary purpose is to partially replace initial steps of star removal in tutorials, like one by Gerald Wechselberger, aiming to enhance nebulosity without pushing stars up. The tutorial itself was available under this link, but not any more, for some reason. Haven't found any newer links to it. Anyway, you got the idea.

Suggested Work Flow

The transformation by this neural net can be part of PixInsight/Photoshop processing work flow. Something like this:

  1. Start from stretched LRGB image. Save as 8 bits/channel tif file.
  2. Feed to StarNet.
  3. Open output in Photoshop, correct some of the worst artifacts that will most likely appear in the image. If there are huge stars in the image left, you will have to take care of them in some other way.
  4. Perhaps use some noise reduction (we don't want to push noise up).
  5. Use resulting image to enhance nebulosity using some method (Screen-Mask-Invert for example) or enjoy the result.
  6. ?
  7. Profit!

Weights for the network

This repository contains only a code base needed to run the net, but does not contain all the weights (which are uploaded into LFS. Pre-trained weights are also available for now through my dropbox account because they weight too much (lol) - about 700 Mb. You need to download them and unpack into root folder of starnet (the one with all python scripts, not into some sub-folder) to begin using StarNet:

This will rewrite some files in this repo!

The weights are also uploaded into LFS, but depending how you clone the repo they might or might not download.

Some tips and tricks

  1. Do not use heavily processed images for input. If star shapes are unusual (if star reduction techniques were used, too much sharpening, deconvolution, image was heavily resized, etc) the performance might be much worse than it could be. I am almost sure. Or maybe it will be better in your case, but that's unlikely.

  2. This newral net was trained using data from refractor telescope (FSQ106 + QSI 683 wsg-8), so will work best for data from similar imaging systems. That's a bit of a bad news for many users of reflector telescopes. If you have long spikes in your images, then the net will not take care of these spikes very well, I tried and didn't like results too much. What you can do, however, is you can train the net a bit more using your own data. Just prepare one or two starless images from your data and run training for 20 epochs or so. This should significantly improve results and make the net much better for your data.

  3. The point above is valid for all images, for which you are not getting good results. You can prepare a starless version even of a small part of that image (but not smaller than 256x256 pixels) and run training for 20 epochs or so on this image. This should improve quality of transformation of the whole image. This will take a lot of time, of course, but in the end you not only getting a starless image, but also train a net so it will perform better next time on a similar image.

  4. Also it might help, for example, to make image brighter (darker) if it is unusually dark (bright), or things like that.

  5. Sometimes the net will leave small stars in the output, if you feed it very busy image. In this case it is helpful to feed output to the net again.

Training Dataset

This is one part I'd like to keep for myself for now, but you can create your own dataset creating starless versions of your images. One extremely important note: the only difference between two images (original and starless) should be stars, which are replaced by background in the starless version. The rest of the image should be perfectly intact. If you will throw in a starless image which is super nice looking, but in a process of creation of this image you altered much more than just stars, this will only degrade network performance. Also be aware that quality of star removal by the net will never be better than that in your training set, so if you want a top-notch quality starless images, be ready to provide training images of even higher quality.

I left one training image to show organization of folders my code expects. Inside a folder named 'train' there are two sub-folders named 'original' and starless', both should contain equal number of images with identical name pairs.

Some technical info

Throughout the code all input and output images I use are 8 bits per channel tif images. This code should read some other image formats (like jpeg, 16bit tiff, etc), but I did not check all of them.

Prerequisites and installation Guide

for all environments, using conda is strongly encouraged, installation instructions assume a conda install of either Anaconda python or miniconda:

Windows (New!)

On windows we can now run starnet on GPU on any modern graphics card! (yes AMD and Intel included)

Prerequisites

Windows 10 Version 1709, 64-bit (Build 16299 or higher) or Windows 11 Version 21H2, 64-bit (Build 22000 or higher)

Installation

Once anaconda is installed, you can open an "anaconda powershell prompt" to proceed.

We use the environment config file provided to configure and install all the dependencies:

With GPU support (Windows):

conda env create -f environment-windows.yml

With CUDA support (linux or windows):

conda env create -f environment-lnx-cuda.yml

CPU only(Mac, Linux, Windows):

conda env create -f environment-cpu.yml

Post installation

Initialize the environment with:

conda activate starnet

And you're ready to go!

Originally tested on:

  • Win 10 + Cygwin
  • NVidia GeForce 840M 2Gb, compute capability 5.0, CUDA version 9.1

Windows general GPU support tested on:

  • Win 10 12H1
  • AMD RX 6800-XT 16GB

Usage

  python.exe -u starnet.py transform <input_image> - The most probable use. This command will transform 
                                                     input image (namely will remove stars) and will 
                                                     create a mask showing changes regions. Output images
                                                     names will be <input_image>_starless.tif and
                                                     <input_image>_mask.tif
  
  python.exe -u starnet.py train                   - Use if you want to train the model some more using
                                                     your training data. This will also output logs and
                                                     showcases of training transformations in 
                                                     './logs' sub-folder.
                                                     
  python.exe -u starnet.py plot                    - Will plot graphs from log files inside './logs' 
                                                     sub-folder.
  
  python.exe -u starnet.py train new               - Use only if you want to train a completely new model,
                                                     erasing all older weights and other output, such as
                                                     logs. Use only if you know what you are doing!

  python.exe -u starnet.py test <input_image>      - This will create some test transformations of patches
                                                     of the input. Similar to transform, but instead of
                                                     transforming an entire image, will create showcases
                                                     of transformations. Fast option to take a look at 
                                                     possible result.
                                                     By default output will be in './test' sub-folder.

Couple more examples

More examples can be found here.



Original:



Starless:



FAQ

What is all this 'python-sudo-mumbo-jumbo'?

This whole thing works as command line program, which means that there is no graphical interface: you have to run it in a console using some text commands (like ones you see above) and it outputs text (and writes image files of course!).

  1. Where exactly do I put weights of the network?

All the files you download should be in one folder: all the files with extension .py (starnet.py, train.py, transform.py, etc.) should be in the same folder with weights for the network (model.ckpt.data-00000-of-00001, model.ckpt.index, model.ckpt.meta, etc.)

Some Troubleshooting

  1. Error: 'No package named tensorflow'. Should be pretty self-explanatory: your python can not find tensorflow. That means you did not run pip to install it (pip install tensorflow) or something went wrong during this step if you did.

  2. Error: 'ImportError: libcublas.so.9.0: cannot open shared object file: No such file or directory.' You are trying to use GPU version of tensorflow and you don't have CUDA properly installed.

  3. Error: 'ValueError: The passed save_path is not a valid checkpoint: ./model.ckpt.' You did not copy network weights into proper location. See above.

Let me know if you have any other issues with the code. (Preferably through Astrobin)

Licenses

Code is available under MIT License, please review LICENSE.md file inside repo. Its very permissive, but no liability or warranty of any kind.

Weights are available under Attribution-NonCommercial-ShareAlike 4.0 International Creative Commons license.

In short: You are free to use and redistribute them in any medium or format, but only under the same license terms. You can transform, and build your projects upon them. You can NOT use them for commercial purposes. You must give appropriate credit for usage of these weights.

The weights are distributed on an "AS IS" BASIS WITHOUT WARRANTIES OF ANY KIND, EXPRESS OR IMPLIED.

starnet's People

Contributors

neveslucas avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

starnet's Issues

Training the AI

Hi,
Love and appreciate the work you put into this. It's an awesome tool!

Recently I've been trying to retrain it to be able to remove the alterations I get with a reflector telescope. I've been trying to get others interested enough to give me some sample images so that I can do a nice long training run...

Do you have any advice on the types of image that will improve the training? I first trained it for 70 epochs with 5 whole images that I prepared, but the output images weren't getting any better at removing the diffractions spikes, which I figured was because it was seeing them too infrequently on the whole images. After that, I tried using smaller images where I had just had clipped out the stars with spikes, but the output I'm getting now is very blocky...

image

I'd appreciate any advice on the type and number I'd need for a good training set! I'm just starting again now from fresh wit the 5 original images... on Epoch 50...

Unsupported API version 162(expected <= 160)

Hello Nekitmm:

Thanks your work! But there was an error happened during my installation of StarNet on my Mac OS X 10.15:
Log:
Installing 1 module(s):
/Applications/PixInsight/bin/StarNet-pxm.dylib
<* failed *>
*** PixInsight API Error: Module: /Applications/PixInsight/bin/StarNet-pxm.dylib: Unsupported API version 162 (expected = 160).: Module load error

What this error means and how could I fix it?

Scipy error

On running starnet I get the following error:

adrian@mononoke:~/starnet$ python starnet.py transform ../Flat/Keeps/M42-and-M43.png
Traceback (most recent call last):
File "/home/adrian/starnet/starnet.py", line 105, in
import transform
File "/home/adrian/starnet/transform.py", line 19, in
from scipy.misc import toimage
ImportError: cannot import name 'toimage' from 'scipy.misc' (/usr/lib/python3/dist-packages/scipy/misc/init.py)

My python installation is python 3.9.7 and scipy 1.6.0.

It appears that the toimage function in scipy was deprecated and then removed, the official recommendaton is to use a function from the pillow library instead (Image.fromarray).

GIMP Plugin for StarNet - gimp-plugin-starnet

Hi @nekitmm

I just wanted to report that I've found a nice/simple plugin to integrate StarNet with GIMP (have the starless automatically imported as layer). I think it's totally worth the mention in your website.

You may find it here:
https://github.com/pigeond/gimp-plugin-starnet

PS: I didn't know about this repo... is this the source for the binaries in the website (I don't see releases here, hence my question)? If it's all Python, then the plugin could actually be importing the code - one thing that could definitely improved is the progress completion feedback to GIMP....

Enhancement: I created a Docker image for Starnet

Hi! I'm not sure how to reach you except by creating an issue. If this is not the correct way, I apologize.

I enjoy starnet++ and wanted to contribute,
I've created a repository that enables users to use a containerized version of the Starnet CLI to process (or batch process) their images using Starnet++.
I would love for you to try it out & let me know what you think:
GH Repo: https://github.com/thijsvanloef/starnet-docker
Docker Hub: https://hub.docker.com/repository/docker/thijsvanloef/starnet-docker

Looking to freeze trained data for PI

Hi Nikita,

actually working to train with new data using my instrument that generate stars with spikes. Slowly it seems going well. I was wondering how do you proceed to generate both .pb freezed files (L and RGB) model in order to use with PI?

thanks!

data clues and research usage

Hello,

I am an astrophysicist from team working with University of Waterloo (UW), Canada. We find this project very interesting and of potential usage for our big ongoing full-sky survey. We would like to experiment with improving starnet performance a little (because unfortunately it is not always produce adoptable results) and probably tune it for FITS we will use. Could you share your expertise about data you train on? Specifically:

  • Can you share the data used and if yes, on which terms?
  • How big the data size for training you use?
  • How do you remove stars from images in training data to train the whole net?

In any case we will surely acknowledge and referencing your work

C++ sources?

I've looked everywhere and I can't find sources for Starnet++ binaries published on SourceForge, only the Python files in this repo.

Is the code those binaries are built from not open-source? If it is, could it be added to this repo as well?

Crashes after epoch when training (number of columns error)

Something about the way I'm running this is causing an error in the reading of the log files. The later epochs that I have run seem to create a "Perceptual_losses.txt" with too many spaces in it, causing it to see too many columns in the later lines (epoch 500+)

Not sure what might be causing it to give more spaces in the log... what version of numpy did you use?

Error - Failed to load the native TensorFlow runtime.

Getting error below

Anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 64, in
from tensorflow.python._pywrap_tensorflow_internal import *
ImportError: DLL load failed while importing _pywrap_tensorflow_internal: The specified module could not be found.

Failed to load the native TensorFlow runtime.

See https://www.tensorflow.org/install/errors

for some common reasons and solutions. Include the entire stack trace
above this error message when asking for help.

Starnet weights

Hello,
First of all, that's a very cool project, for me it works great, it helps to remove stars easily from astrophotos for easier nebulosity processing. I wish the weights were stored in this GitHub repository (maybe on master branch, maybe on separate branch) for easier updating. If you would update the weights, users would have to download and update them manually, but if they were stored in this repository, updating them locally would be as easy as typing git pull! 😄

You'd have to use https://git-lfs.github.com/ because there's a 100MB/file limit on regular repos

Incompatible with latest version of tensor flow etc

Looks like a great package.
Unfortunately it does run on the latest version of tensor flow as various changes
have been implemented to the package.
I tried hard but could not get it to run.
Please comment here if anyone has been more successful.
Thanks, Des

OSX save issue

Hi, just passing by on what seems to be a very cool projet. I saw a picture on instagram of someone using your code to remove stars and the effect was amazing.

While trying to test if myself, I run into a save error:

Which is linked to that line: https://github.com/nekitmm/starnet/blob/master/transform.py#L122

Probably removing the "./" would be a good idea as i was trying to run your script with an absolute path and not a relative path and it couldn't save due to a weird path :)

PS: also didn't managed to have the plugin installed with pixinsight 1.8, including with recursive search - even though the files were there. Maybe something changed with the versions ? It's not related to your warning on AVX since i could run the script successfully.

Anyhow, great job and interesting project !

no support tif files from photoshop?

hi nekitmm, i saved a milky way core tif in photoshop in 16 bit, but when i go to starnet++, it always say only support 16 bit files, any idea?

Tensorflow issues

I edited the python files as you suggested in replying to the other issue I raised and got rid of the toImage errors, unfortunately I now get a variety of tensorflow errors instead that I can't make go away by importing tensorflow.v1.compat and disabling v2 features.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.