-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
First changes to structure and AlexNet integration #3
Conversation
Modified model.py Reshape output file structure. Added main and argpars for eas of use Updated .gitignore to ignore .h5 files
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the PR, its a good starter and especially the amount of changes is nice as it is not too large to review :)
I added some comments which we should discuss before merging. You can obviously already start more work on another branch which can be a follow-up PR.
Additionally, there are a bunch of binary files which I think should not be committed, those are:
feature_vis_inceptionV3_mixed6_1.npy
outputs/feature_vis_inceptionV3_mixed6_1.png
pretrained_models/data/meta_clsloc.mat
test.npy
test.png
I think in general, you want to have the main.py
outside of this project, and all the output as well.
featurevis/featurevis.py
Outdated
@@ -131,7 +132,7 @@ def visualize_filter(image, model, layer, filter_index, iterations, | |||
print('>> 100 %') | |||
# Decode the resulting input image | |||
image = imgs.deprocess_image(image[0].numpy()) | |||
return loss, image | |||
return loss, image, layer, filter_index |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why would we return layer and filter_index? We already provide this to the function as a parameter, so the other end should know about this, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right. I will implement the variables in the main function and use them for the file name generation there.
main.py
Outdated
from pretrained_models import models | ||
from featurevis import featurevis, images, image_reader | ||
import argparse | ||
|
||
parser = argparse.ArgumentParser() | ||
|
||
parser.add_argument("-a", "--architecture", type = str, default = "inceptionV3", help = "The model architecture") | ||
args = parser.parse_args() | ||
|
||
arch = args.architecture | ||
#architecture = "inceptionV3" | ||
|
||
model = models.get_model(arch) | ||
# model.summary() | ||
image = images.initialize_image(224, 224) | ||
loss, image, layer_name, channel_num = featurevis.visualize_filter(image, model, "mixed6", 1, 500, 2, 0, 0, 0) | ||
|
||
name = "feature_vis_{}_{}_{}".format(arch, layer_name, channel_num) | ||
print(loss) | ||
|
||
images.save_image(image, name= name) | ||
image_reader.save_npy_as_png("{}.npy".format(name)) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should not have a main.py
like this. If people are supposed to use this as a library (as is probably intended), i.e., to write their own code and import this as a package (import luna
), then such a file is not needed. What we could think of is two options:
- Currently, we have a how-to demo in the README.md. It could stay this way and we could change that readme if we think this code is a better showcase.
- We could do a
luna_demo.py
, which does what you have here. The downside of this is that when the project is used as a library, this does not really make sense, as there is no method to call.
These things considered, I would move this code to the README.md, if we think this is a better showcase. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would agree more to put it inside README.md. Maybe we can talk more about it in our meeting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, lets talk tomorrow.
pretrained_models/models.py
Outdated
if model_name == "VGG16": | ||
return keras.applications.vgg16(weights= "imagenet", include_top=False) | ||
if model_name == "alexnet": | ||
return alexnet.AlexNet() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This does not use a weights_path
, is that intended? For other models, we load the pretrained weights by default.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wrote the initialization of the weight_path inside the main of alexnet.py. But if we want to get rid of it, I will give the pretrained weight as default in the model.py file.
pretrained_models/models.py
Outdated
else: | ||
print("Model is not specified") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem about this is, that we do have one path now that does not return anything. I put a default return here that returns VGG16 if nothing else matches. Maybe we should go back to that and print a warning? Otherwise the code will fail if the wrong model is specified. In the long run, we might want to have individual functions for the models to get rid of this if/else
, which is not very pretty.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can talk about it. I just don't like the VGG as fallback but we can decide about this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, lets talk about this tomorrow.
featurevis/featurevis.py
Outdated
import tensorflow as tf | ||
from tensorflow import keras | ||
|
||
from luna.featurevis import images as imgs | ||
from featurevis import images as imgs |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Out of curiosity, why did you change that? I think the previous version was correct, no? At least if you use this as a library.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did this because I wrote my main.py file inside the library and this luna was redundant at that point.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In that case, we should probably change it back.
featurevis/featurevis.py
Outdated
import tensorflow as tf | ||
from tensorflow import keras | ||
|
||
from luna.featurevis import images as imgs | ||
from featurevis import images as imgs |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In that case, we should probably change it back.
featurevis/image_reader.py
Outdated
from pathlib import Path | ||
import numpy as np | ||
from tensorflow import keras | ||
|
||
|
||
def save_npy_as_png(path): | ||
def save_npy_as_png(input_path, output_path = "lune/outputs"): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general, I think this change is good. But I don't think we should have an outputs folder inside the project. If this is to be used as a library, this does not really make sense. So I would say this should probably not have a default value.
pretrained_models/models.py
Outdated
else: | ||
print("Model is not specified") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, lets talk about this tomorrow.
featurevis/featurevis.py
Outdated
@@ -132,6 +134,9 @@ def visualize_filter(image, model, layer, filter_index, iterations, | |||
print('>> 100 %') | |||
# Decode the resulting input image | |||
image = imgs.deprocess_image(image[0].numpy()) | |||
print(image.shape) | |||
print(image) | |||
# conver the proceed image to a valid rgb color |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
conver -> convert
featurevis/featurevis.py
Outdated
print(image.shape) | ||
print(image) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is that for?
featurevis/featurevis.py
Outdated
@@ -175,10 +180,11 @@ def gradient_ascent_step(img, model, filter_index, learning_rate, channels_first | |||
with tf.GradientTape() as tape: | |||
tape.watch(img) | |||
loss = compute_loss(img, model, filter_index, channels_first) | |||
#loss_prop = tf.reduce_sum(img, axis=-1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uncommented lines should be removed.
featurevis/featurevis.py
Outdated
# Compute gradients. | ||
grads = tape.gradient(loss, img) | ||
# Normalize gradients. | ||
grads = tf.math.l2_normalize(grads) | ||
#grads = tf.math.l2_normalize(grads) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uncommented lines should be removed.
featurevis/image_reader.py
Outdated
@@ -8,7 +8,7 @@ | |||
from tensorflow import keras | |||
|
|||
|
|||
def save_npy_as_png(input_path, output_path = "lune/outputs"): | |||
def save_npy_as_png(input_path, output_path = "luna/outputs"): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still think per default, this should not be in luna
.
Describe your improvements
Added AlexNet model with Keras.
Modified model.py
Reshape output file structure.
Added main and argpars for ease of use
Updated .gitignore to ignore .h5 files