我正处于训练模型的最后一步,我得到了进一步描述的错误。我该怎么解决这个问题?(这是一个图像分类模型(
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
)
它给我这个警告
/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)` '
然后是这个错误:
TypeError: The added layer must be an instance of class Layer. Found: <keras.engine.training.Model object at 0x7fcf283b1990>
我该怎么解决这个问题?
非常感谢你的帮助。
我可以通过更改导入来执行代码,如下所示
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()
输出:
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
_________________________________________________________________