ash luck
ash luck

Reputation: 17

Issue in removing layer from a pretrained model

I have the following code, I need to remove some layers of the model and perform prediction. But currently I am retrieving error.

 from tensorflow.keras.applications.resnet50 import ResNet50
 from tensorflow.keras.preprocessing import image
 from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions
 import numpy as np
 from keras.models import Model
 from tensorflow.python.keras.optimizers import SGD


 base_model = ResNet50(include_top=False, weights='imagenet')
 model= Model(inputs=base_model.input, outputs=base_model .layers[-2].output)
 #model = Model(inputs=base_model.input, outputs=predictions)
 #Compiling the model
 model.compile(optimizer=SGD(lr=0.0001, momentum=0.9), loss='categorical_crossentropy', metrics = 
 ['accuracy'])
 img_path = 'elephant.jpg'
 img = image.load_img(img_path, target_size=(224, 224))
 x = image.img_to_array(img)
 x = np.expand_dims(x, axis=0)
 x = preprocess_input(x)
 preds = model.predict(x)
 #decode the results into a list of tuples (class, description, probability)
 #(one such list for each sample in the batch)
 print('Predicted:', decode_predictions(preds, top=3)[0])

error

File "C:/Users/learn/remove_layer.py", line 9, in <module>
model= Model(inputs=base_model.input, outputs=base_model .layers[-2].output)
AttributeError: 'Tensor' object has no attribute '_keras_shape'

Due to my beginner's knowledge in Keras what I understood is the shape issue. Since its a resnet model, if I remove a layer from one merge to another merge layer, because merge layer doesn't have dimension issues, how can I accomplish this?

Upvotes: 0

Views: 519

Answers (1)

Yefet
Yefet

Reputation: 2086

You actually need to visualize what you have done, so lets do little summary for last layers of ResNet50 Model:

base_model.summary()

conv5_block3_2_relu (Activation (None, None, None, 5 0           conv5_block3_2_bn[0][0]          
__________________________________________________________________________________________________
conv5_block3_3_conv (Conv2D)    (None, None, None, 2 1050624     conv5_block3_2_relu[0][0]        
__________________________________________________________________________________________________
conv5_block3_3_bn (BatchNormali (None, None, None, 2 8192        conv5_block3_3_conv[0][0]        
__________________________________________________________________________________________________
conv5_block3_add (Add)          (None, None, None, 2 0           conv5_block2_out[0][0]           
                                                                 conv5_block3_3_bn[0][0]          
__________________________________________________________________________________________________
conv5_block3_out (Activation)   (None, None, None, 2 0           conv5_block3_add[0][0]           
==================================================================================================
Total params: 23,587,712
Trainable params: 23,534,592
Non-trainable params: 53,120
_____________________________

And now your model after removing last layer

model.summary()

conv5_block3_2_relu (Activation (None, None, None, 5 0           conv5_block3_2_bn[0][0]          
__________________________________________________________________________________________________
conv5_block3_3_conv (Conv2D)    (None, None, None, 2 1050624     conv5_block3_2_relu[0][0]        
__________________________________________________________________________________________________
conv5_block3_3_bn (BatchNormali (None, None, None, 2 8192        conv5_block3_3_conv[0][0]        
__________________________________________________________________________________________________
conv5_block3_add (Add)          (None, None, None, 2 0           conv5_block2_out[0][0]           
                                                                 conv5_block3_3_bn[0][0]          
==================================================================================================
Total params: 23,587,712
Trainable params: 23,534,592
Non-trainable params: 53,120

Reset50 in keras output is all the feature map after the last Conv2D blocks it doesn't care about the classfication part of your model, what you actualy did is that you just removed the last activation layer after the last addition block

enter image description here

So you need check more which block layer you wanna remove and add flatten and fully connected layer for the classfication part

Also as mentioned by Dr.Snoopy, dont mix imports between keras and tensorflow.keras

# this part

from tensorflow.keras.models import Model

Upvotes: 1

Related Questions