Skip to content
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

Merged
merged 13 commits into from
Feb 15, 2021
Merged

First changes to structure and AlexNet integration #3

merged 13 commits into from
Feb 15, 2021

Conversation

fuzhanrahmanian
Copy link
Collaborator

@fuzhanrahmanian fuzhanrahmanian commented Jan 26, 2021

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

Modified model.py
Reshape output file structure.
Added main and argpars for eas of use
Updated .gitignore to ignore .h5 files
Copy link
Owner

@Sparkier Sparkier left a 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.

@@ -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
Copy link
Owner

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?

Copy link
Collaborator Author

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.

featurevis/image_reader.py Outdated Show resolved Hide resolved
main.py Outdated
Comment on lines 1 to 23
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))

Copy link
Owner

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:

  1. 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.
  2. 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?

Copy link
Collaborator Author

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.

Copy link
Owner

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/alexnet.py Outdated Show resolved Hide resolved
pretrained_models/alexnet.py Outdated Show resolved Hide resolved
if model_name == "VGG16":
return keras.applications.vgg16(weights= "imagenet", include_top=False)
if model_name == "alexnet":
return alexnet.AlexNet()
Copy link
Owner

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.

Copy link
Collaborator Author

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.

Comment on lines 34 to 35
else:
print("Model is not specified")
Copy link
Owner

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.

Copy link
Collaborator Author

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.

Copy link
Owner

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.

import tensorflow as tf
from tensorflow import keras

from luna.featurevis import images as imgs
from featurevis import images as imgs
Copy link
Owner

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.

Copy link
Collaborator Author

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.

Copy link
Owner

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.

import tensorflow as tf
from tensorflow import keras

from luna.featurevis import images as imgs
from featurevis import images as imgs
Copy link
Owner

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.

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"):
Copy link
Owner

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.

Comment on lines 34 to 35
else:
print("Model is not specified")
Copy link
Owner

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.

@@ -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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

conver -> convert

Comment on lines 137 to 138
print(image.shape)
print(image)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is that for?

@@ -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)
Copy link
Owner

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.

# Compute gradients.
grads = tape.gradient(loss, img)
# Normalize gradients.
grads = tf.math.l2_normalize(grads)
#grads = tf.math.l2_normalize(grads)
Copy link
Owner

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.

@@ -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"):
Copy link
Owner

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.

@Sparkier Sparkier merged commit 840064f into Sparkier:master Feb 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants