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

Remove dependency of architectures #8

Merged
merged 13 commits into from
Apr 28, 2021
Merged

Conversation

fuzhanrahmanian
Copy link
Collaborator

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

to the existence both .h5 files. If files are not in the standard
download directory, donwload the arichtectures
@fuzhanrahmanian fuzhanrahmanian linked an issue Feb 21, 2021 that may be closed by this pull request
Deprecated the use of a channles_first variable in order to rely on the golbal keras backend image format varialbe.
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 these changes. With minor adjustments, they should be mergeable.

luna/featurevis/images.py Outdated Show resolved Hide resolved
ZeroPadding2D)
from keras.utils.data_utils import get_file
from tensorflow.keras.layers import Conv2D
Copy link
Owner

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.

Copy link
Owner

Choose a reason for hiding this comment

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

Still valid.

Copy link
Collaborator Author

@fuzhanrahmanian fuzhanrahmanian Apr 13, 2021

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

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

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.

Copy link
Owner

Choose a reason for hiding this comment

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

Still valid.

Copy link
Owner

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/alexnet.py Outdated Show resolved Hide resolved
@@ -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")
Copy link
Owner

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.

Copy link
Collaborator Author

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.

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. Most of my comments should be easy fixes.

padding='SAME', data_format='NHWC')


#pylint: disable=R0914
Copy link
Owner

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.

Comment on lines 34 to 37
# Temporary method for random choice of
# transformation combination
choice_num = [0, 1, 2, 3]
augmentation = ['noise', 'blur', 'scale']
Copy link
Owner

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?

Comment on lines 41 to 58
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)

Copy link
Owner

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?

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 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.

Comment on lines 43 to 49
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")
Copy link
Owner

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.

Comment on lines 1 to 3
"""
Copyright [2017] [https://github.com/tensorflow/lucid]
"""
Copy link
Owner

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.

Copy link
Owner

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.

Comment on lines 142 to 152
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)
Copy link
Owner

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.

Copy link
Owner

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?

ZeroPadding2D)
from keras.utils.data_utils import get_file
from tensorflow.keras.layers import Conv2D
Copy link
Owner

Choose a reason for hiding this comment

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

Still valid.

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

Choose a reason for hiding this comment

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

Still valid.

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

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

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.

Copy link
Owner

Choose a reason for hiding this comment

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

Still open.

Copy link
Owner

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
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.

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

Choose a reason for hiding this comment

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

What does that mean?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

added

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)
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? Can you add a comment?

Copy link
Collaborator Author

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.

luna/featurevis/featurevis.py Show resolved Hide resolved

return tf.where(return_relu_grad, relu_grad, result_grad)

#pylint: disable=R0914
Copy link
Owner

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

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

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?

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 actually need all of them. I can't think of a way of how to get rid of them. Any idea?


#pylint: disable=too-many-locals
#pylint: disable=too-many-arguments
Copy link
Owner

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

Copy link
Collaborator Author

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.

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.

Lets get this in now. We can fix the minor issues later.

@Sparkier Sparkier merged commit 3d6a72d into Sparkier:master Apr 28, 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.

Referring to static weight files
2 participants