-
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
Remove dependency of architectures #8
Conversation
to the existence both .h5 files. If files are not in the standard download directory, donwload the arichtectures
Deprecated the use of a channles_first variable in order to rely on the golbal keras backend image format varialbe.
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 these changes. With minor adjustments, they should be mergeable.
luna/pretrained_models/alexnet.py
Outdated
ZeroPadding2D) | ||
from keras.utils.data_utils import get_file | ||
from tensorflow.keras.layers import Conv2D |
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.
Could merge with the other layers import.
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 valid.
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.
tensorflow.keras.layers can be merged. I cannot find a merge for get_file
p.s. Alexnet does not work (properly) yet. I am still working on it
luna/pretrained_models/alexnet.py
Outdated
@@ -141,7 +146,15 @@ def alex_net(weights_path=None, heatmap=False): | |||
|
|||
model = Model(input=inputs, output=prediction) | |||
|
|||
if weights_path: | |||
model.load_weights(weights_path) | |||
weights_path = Path(str(Path.home()) + r"~\.keras\datasets\alexnet_weights.h5") |
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 don't know if we can make these assumptions. Should we rather make the weights path a mandatory variable? Then one would have to specify it, and this would not be needed.
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 valid.
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 don't know if that would work on every OS etc.
luna/pretrained_models/googlenet.py
Outdated
@@ -430,8 +431,17 @@ def create_googlenet(): | |||
loss2_classifier_act, | |||
loss3_classifier_act]) | |||
|
|||
googlenet.load_weights(os.path.join( | |||
os.path.dirname(__file__), 'googlenet_weights.h5')) | |||
weights_path = Path(str(Path.home()) + r"\.keras\datasets\googlenet_weights.h5") |
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.
Same as above, thee assumptions could be problematic, so I would advocate for removing them and have the user specify weights path.
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.
Weights path is still an optional variable but if it is not given, it will be downloaded. It is directory independent so should not give problem in different OS.
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. Most of my comments should be easy fixes.
luna/featurevis/featurevis.py
Outdated
padding='SAME', data_format='NHWC') | ||
|
||
|
||
#pylint: disable=R0914 |
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 should be removed. You can use up to 10 arguments per function. This function only takes 9, so removing should have no effect.
luna/featurevis/featurevis.py
Outdated
# Temporary method for random choice of | ||
# transformation combination | ||
choice_num = [0, 1, 2, 3] | ||
augmentation = ['noise', 'blur', 'scale'] |
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.
Don't we want this to be specified by the user? Can you give an intuition for why this is random?
luna/featurevis/featurevis.py
Outdated
image_aug = {'noise': trans.add_noise(image, noise, pctg), | ||
'blur': trans.blur_image(image, blur, pctg), | ||
'scale': trans.rescale_image(image, scale)} | ||
num = random.choice(choice_num) | ||
if num ==1: | ||
ind = random.sample(augmentation, 1) | ||
print(ind) | ||
image = image_aug[ind[0]] | ||
if num ==2: | ||
ind = random.sample(augmentation, 2) | ||
print(ind) | ||
image = image_aug[ind[0]] | ||
image = image_aug[ind[1]] | ||
else: | ||
image = trans.add_noise(image, noise, pctg) | ||
image = trans.blur_image(image, blur, pctg) | ||
image = trans.rescale_image(image, scale) | ||
|
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 has some problems I think. First, you are ignoring the case where random choice is 0. But more importantly, it is not clear to me why this should be random. Maybe you can explain that to me?
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 was thinking: if we always add some amount of (i.e.) noise, this will be done at every single iteration and (since it depends on the pctg), it will increase the noisiness at every iteration. My idea was the following: randomize the combination of different transformations in order to not over-transform any feature since the model might not learn good (correct me if my understanding is wrong please). The same goes for every other transformations that use the pctg.
So I have two ideas:
- either I can find a more efficient way of writing this randomization
- or if the user wants to have a particular transformation, its amount should be random at every iteration and not depended on pctg (similar to rescale image as described in the paper which I am actually using their reported values)
The transformations have a strong effect on the end result.
luna/featurevis/images.py
Outdated
img = ((img - img.mean()) / img.std()) + 1e-5 | ||
img *= 0.15 | ||
img += 0.5 | ||
img = np.clip(img, 0, 1) | ||
|
||
# Convert to RGB array | ||
img *= 255 | ||
img = img.astype("uint8") | ||
img = np.clip(img, 0, 255).astype("uint8") |
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.
Can you add comments on what happens here? The one in the function doctoring seems not to cover this anymore.
luna/featurevis/relu_grad.py
Outdated
""" | ||
Copyright [2017] [https://github.com/tensorflow/lucid] | ||
""" |
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.
Is that really a 1:1 copy? I would assume we'd have to change some things to make it work with tf2. If that is the case, we should probably mention that we adopted but modified the code from there.
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.
More importantly, I don't see any place where we are using these functions. If we are not using them, we should remove this file.
luna/featurevis/transformations.py
Outdated
def random_rotate(img, rotation): | ||
"""Randomly rotates the image | ||
|
||
Args: | ||
img (list): image | ||
rotation (int): degree of rotation | ||
|
||
Returns: | ||
img (list): rotated image | ||
""" | ||
return tfa.image.rotate(img, rotation) |
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 function is unused. Should be removed I 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.
@fuzhanrahmanian this is till valid I think, no? can you comment?
luna/pretrained_models/alexnet.py
Outdated
ZeroPadding2D) | ||
from keras.utils.data_utils import get_file | ||
from tensorflow.keras.layers import Conv2D |
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 valid.
luna/pretrained_models/alexnet.py
Outdated
@@ -141,7 +146,15 @@ def alex_net(weights_path=None, heatmap=False): | |||
|
|||
model = Model(input=inputs, output=prediction) | |||
|
|||
if weights_path: | |||
model.load_weights(weights_path) | |||
weights_path = Path(str(Path.home()) + r"~\.keras\datasets\alexnet_weights.h5") |
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 valid.
luna/pretrained_models/alexnet.py
Outdated
@@ -141,7 +146,15 @@ def alex_net(weights_path=None, heatmap=False): | |||
|
|||
model = Model(input=inputs, output=prediction) | |||
|
|||
if weights_path: | |||
model.load_weights(weights_path) | |||
weights_path = Path(str(Path.home()) + r"~\.keras\datasets\alexnet_weights.h5") |
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 don't know if that would work on every OS etc.
@@ -135,7 +66,7 @@ def visualize_filter(image, model, layer, filter_index, iterations, | |||
return loss, image | |||
|
|||
|
|||
def compute_loss(input_image, model, filter_index, channels_first=False): | |||
def compute_loss(input_image, model, 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.
Should be removed from the docstring as well.
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 open.
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.
@fuzhanrahmanian this is not fixed yet. In the function documentation, you are still referring to this variable.
Alexnet was deleted for the moment Pull request correction Adaptation for optimal Googlenet results
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.
Hey, thanks for updating. I have just minor comments, mainly on the pylint stuff and some code comments. Once that is fixed (should be easy), we can merge.
feature_extractor = get_feature_extractor(model, layer) | ||
|
||
# Temporary method for random choice of |
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 does that mean?
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.
added
luna/featurevis/featurevis.py
Outdated
return tf.reduce_mean(filter_activation) | ||
|
||
|
||
@tf.function | ||
def gradient_ascent_step(img, model, filter_index, learning_rate, channels_first=False): | ||
@tf.function(experimental_relax_shapes=True) |
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? Can you add a comment?
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.
Not necessary anymore. I used it to reduce memory usage, memory is not a problem now.
|
||
return tf.where(return_relu_grad, relu_grad, result_grad) | ||
|
||
#pylint: disable=R0914 |
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.
Can you use the more explicit version #pylint: disable=too-many-locals
@@ -135,7 +66,7 @@ def visualize_filter(image, model, layer, filter_index, iterations, | |||
return loss, image | |||
|
|||
|
|||
def compute_loss(input_image, model, filter_index, channels_first=False): | |||
def compute_loss(input_image, model, 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.
Still open.
|
||
#pylint: disable=too-many-locals |
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.
Are there really too many locals? If yes, can we change that?
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 actually need all of them. I can't think of a way of how to get rid of them. Any idea?
luna/featurevis/featurevis.py
Outdated
|
||
#pylint: disable=too-many-locals | ||
#pylint: disable=too-many-arguments |
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.
Can we change the method to use objects for the OptimizationParameters
and AugmentationParameters
?
Something like this: https://stackoverflow.com/a/816517/4620643
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.
Good idea. I did it but pylint complains that there are not enough method in the class. I have omitted this error message for PR.
clean the code for pylint
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.
Lets get this in now. We can fix the minor issues later.
Remove dependency of architectures to the existence both .h5 files. If files are not in the standard
download directory, download the architectures. Partially adapted alexnet code to comply with keras 2.4