简体   繁体   中英

getting class layer error in Tensorflow model

I am on the last step of training my model, and I am getting the further described error. How can I fix this? (this is an image classification model)

from tensorflow.python.keras.models import Sequential 
from tensorflow.python.keras.layers import GlobalMaxPooling2D, Dense, Flatten, GlobalAveragePooling2D

#Model definition

my_model = Sequential() 
my_model.add(ResNet50(input_shape=(image_size, image_size, 3), include_top=False, weights='imagenet')) 
my_model.add(GlobalMaxPooling2D())
my_model.add(Flatten()) 
my_model.add(Dense(128, activation='relu')) 
my_model.add(Dense(1, activation='linear'))

#The first layer (ResNet) of the model is already trained, so we don't need to train it
my_model.layers[0].trainable = False

#Model compilation 
my_model.compile(loss ='mse', optimizer= 'adam', metrics = ['mean_absolute_error']) 
my_model.summary()

#Model fitting 
my_model.fit_generator(train_generator, 
                           steps_per_epoch = 180, 
                           validation_data = val_generator, 
                           validation_steps = 18, 
                           epochs = 30
                      )

it gives me this warning

/Users/folder/opt/anaconda3/envs/ML2/lib/python3.7/site-packages/keras_applications/resnet50.py:265: UserWarning: The output shape of `ResNet50(include_top=False)` has been changed since Keras 2.2.0.
  warnings.warn('The output shape of `ResNet50(include_top=False)` '

followed by this error:

TypeError: The added layer must be an instance of class Layer. Found: <keras.engine.training.Model object at 0x7fcf283b1990>

How can I fix this?

Thank you very much for any help.

I am able to execute code by changing imports as shown below

import tensorflow as tf
print(tf.__version__)
import numpy as np
from tensorflow.keras.models import Sequential 
from tensorflow.keras.layers import GlobalMaxPooling2D, Dense, Flatten, GlobalAveragePooling2D

image_size = 224
#Model definition

my_model = Sequential() 
my_model.add(tf.keras.applications.resnet50.ResNet50(input_shape=(image_size, image_size, 3), include_top=False, weights='imagenet')) 
my_model.add(GlobalMaxPooling2D())
my_model.add(Flatten()) 
my_model.add(Dense(128, activation='relu')) 
my_model.add(Dense(1, activation='linear'))

#The first layer (ResNet) of the model is already trained, so we don't need to train it
my_model.layers[0].trainable = False

#Model compilation 
my_model.compile(loss ='mse', optimizer= 'adam', metrics = ['mean_absolute_error']) 

my_model.summary()

Output:

2.7.0
Downloading data from https://storage.googleapis.com/tensorflow/keras-applications/resnet/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5
94773248/94765736 [==============================] - 1s 0us/step
94781440/94765736 [==============================] - 1s 0us/step

Model: "sequential_1"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 resnet50 (Functional)       (None, 7, 7, 2048)        23587712  
                                                                 
 global_max_pooling2d (Globa  (None, 2048)             0         
 lMaxPooling2D)                                                  
                                                                 
 flatten (Flatten)           (None, 2048)              0         
                                                                 
 dense_4 (Dense)             (None, 128)               262272    
                                                                 
 dense_5 (Dense)             (None, 1)                 129       
                                                                 
=================================================================
Total params: 23,850,113
Trainable params: 262,401
Non-trainable params: 23,587,712
_________________________________________________________________

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