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

How to change the activation function of VGG16 model in keras? #9370

Closed
rcasiodu opened this issue Feb 12, 2018 · 6 comments
Closed

How to change the activation function of VGG16 model in keras? #9370

rcasiodu opened this issue Feb 12, 2018 · 6 comments

Comments

@rcasiodu
Copy link

rcasiodu commented Feb 12, 2018

I want to change the activation of the last layer 'softmax' to 'relu', the following is the code:

from keras.applications.vgg16 import VGG16
from keras.activations import softmax, relu, sigmoid
import numpy as np
import cv2

model_vgg16 = VGG16(weights='imagenet', include_top= True)

img_path = './vision_project/filter-visualization/doberman.png'
img = cv2.imread(img_path)
img = cv2.resize(img, (224, 224))
img = img.astype('float32') - 127.5

output = model_vgg16.predict(img[np.newaxis, :])
print(output[0, np.argmax(output)],np.argmax(output))

OUT:0.822323 236

model_vgg16.layers[-1].activation = relu
output = model_vgg16.predict(img[np.newaxis, :])
print(output[0, np.argmax(output)],np.argmax(output))

OUT:0.822323 236

The result is the same, but when i check the activation layer in the model.layers[-1].activation, it has changed with success.

model_vgg16.layers[-1].activation

OUT: <function keras.activations.relu>

Could anyone tell me how to solve this problem?

@23pointsNorth
Copy link
Contributor

You can take the model up to the last layer (before the softmax) and attach a dense relu layer at the end. Of course you'll need to retrain the last layer of the model.

@rex-yue-wu
Copy link

rex-yue-wu commented Feb 19, 2018

Simply change activation to None
model_vgg16.layers[-1].activation=None

If you look at the source code of dense layer you will see

    def call(self, inputs):
        output = K.dot(inputs, self.kernel)
        if self.use_bias:
            output = K.bias_add(output, self.bias)
        if self.activation is not None:
            output = self.activation(output)
        return output

@rcasiodu
Copy link
Author

rcasiodu commented Mar 5, 2018

@23pointsNorth @rex-yue-wu Thanks, i found the problem. The model must be retrained to make it works.

@rcasiodu rcasiodu closed this as completed Mar 5, 2018
@don-tpanic
Copy link

@23pointsNorth @rex-yue-wu Thanks, i found the problem. The model must be retrained to make it works.

Hi. what do you mean by retrain here? I compile the model again after I reset the activation like you did it doesn't seem to work.

@mhsamavatian
Copy link

I have the same problem. What is retraining here?

@burhr2
Copy link

burhr2 commented Jun 29, 2020

I recompiled the model then it worked. For illustration, I changed all activation to sigmoid. see the example below

    from tensorflow.keras.activations import relu,sigmoid,elu
    from tensorflow.keras.applications.vgg16 import VGG16
    base_model = VGG16(weights='imagenet', include_top=False,pooling='avg',input_shape= (100, 100, 3))
    # before if you check 
    base_model.get_config() # you will see all activation are relu 
    for layer in base_model.layers:
        if (hasattr(layer,'activation'))==True:
             layer.activation = sigmoid
    # without compiling you should not see any changes
    # when calling base_model.get_config()
    # when compiling
    base_model.compile(loss="categorical_crossentropy") #it forced me to put the loss
    # now you will see the changes when calling
    base_model.get_config()

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

No branches or pull requests

6 participants