简体   繁体   中英

How do I add a top dense layer to ResNet50 in Keras?

I read this very helpful Keras tutorial on transfer learning here:

https://blog.keras.io/building-powerful-image-classification-models-using-very-little-data.html

I am thinking that this is probably very applicable to the fish data here, and started going down that route. I tried to follow the tutorial as much as I could. The code is a mess as I was just tyring to figure out how everything works, but it can be found here:

https://github.com/MrChristophRivera/ClassifiyingFish/blob/master/notebooks/Anthony/Resnet50%2BTransfer%20Learning%20Attempt.ipynb

For brevity, here are the steps I did here:

model = ResNet50(top_layer = False, weights="imagenet"
# I would resize the image to that of the standard input size of ResNet50.
datagen=ImageDataGenerator(1./255)
generator = datagen.flow_from_directory(
    train_data_dir,
    target_size=(img_width, img_height),
    batch_size=32,
    class_mode=None,
    shuffle=False)
# predict on the training data
bottleneck_features_train = model.predict_generator(generator, 
nb_train_samples)
print(bottleneck_features_train)
file_name = join(save_directory, 'tbottleneck_features_train.npy')
np.save(open(file_name, 'wb'), bottleneck_features_train)
# Then I would use this output to feed my top layer and train it. Let's 
say I defined 
# it like so:
top_model = Sequential()
# Skipping some layers for brevity
top_model.add(Dense(8,  activation='relu')
top_model.fit(train_data, train_labels)
top_model.save_weights(top_model_weights_path).

At this time, I have the weights saved. The next step would be to add the top layer to ResNet50. The tutorial simply did it like so:

# VGG16 model defined via Sequential is called bottom_model.
bottom_model.add(top_model)

The problem is when I try to do that this fails because "model does not have property add". My guess is that ResNet50 was defined in a different way. At any rate, my question is: How can I add this top model with the loaded weights to the bottom model? Can anyone give helpful pointers?

Try:

input_to_model = Input(shape=shape_of_your_image)
base_model = model(input_to_model)
top_model = Flatten()(base_model)
top_model = Dense(8,  activation='relu')
...

Your problem comes from the fact that Resnet50 is defined in a so called functional API . I would also advise you to use different activation function because having relu as an output activation might cause problems. Moreover - your model is not compiled.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM