utkuozbulak / pytorch-cnn-visualizations

Pytorch implementation of convolutional neural network visualization techniques
MIT License
7.81k stars 1.49k forks source link

Add blur, gradient clipping, and regularization to ClassSpecificImageGenerator #72

Closed alexstoken closed 4 years ago

alexstoken commented 4 years ago

Thanks for a great visualization suite. I was having trouble getting a quality image from the class specific image generator so I implemented some of the techniques you suggested in the README and that are originally from Understanding Neural Networks Through Deep Visualization. These techniques greatly improved my visualizations.

Tested with torchvision 0.5.0 with model and image on cpu. To use GPU, will likely have to add logic to transfer image to cpu for PIL and then back to GPU for optimization, but I haven't looked in to this much.

utkuozbulak commented 4 years ago

Thanks for the PR @alexstoken! I'm currently on a very tight schedule due to some conference deadlines, I will try to have a look at these changes as soon as I can!

alexstoken commented 4 years ago

I'll make the adjustments in the next few days and I'll let you know when I push them. What would you like the new file and class called? Maybe something along the lines of RegularizedClassSpecificImageGenerator or AdvancedClassSpecificImageGenerator?

utkuozbulak commented 4 years ago

Both are fine by me. Can you just use snake case instead of camel? Also, please do include a header similar to what is below.

""" Created on Mon Dec 31 99:99:99 2999 @author: Alex Stoken - https://github.com/alexstoken """

utkuozbulak commented 4 years ago

Thanks for the changes! I will try to have a look at it as soon as I have free time at my hands!

alexstoken commented 4 years ago

Thanks! Is it possible on your end to just accept the changes to the new file, or should I undo my commits to generate_class_specific_samples.py so that that can remain unchanged?

utkuozbulak commented 4 years ago

I would appreciate if you could undo changes on other two files, if you are too busy I can fix it as well.

alexstoken commented 4 years ago

I've now undone the regularization changes in the previous files, but I've left a few others for your consideration. The primary remaining changes in generate_class_specific_samples.py is an iterations argument (with default set as you had it) and changing the save folder to include the class label.

The primary change in misc_functions.py is checking if the input is a PIL image, and casting it to a PIL image if not. This is added because I found a case where a np.ndarray was passed to preprocess instead of a PIL.Image.

Finally, the new generate_regularized_class_samples.py is now cuda enabled, and runs must faster on the GPU devices. It auto detects if cuda is available and uses it if so.

Let me know what you think!

utkuozbulak commented 4 years ago

Hey @alexstoken, thanks for the changes. I will accept the PR and go over the changes the moment I have some free time.

utkuozbulak commented 4 years ago

Huh, finally able to merge the PR. Thanks a lot for it!