简体   繁体   English

在 Tensorflow 模型中获取类层错误

[英]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
_________________________________________________________________

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 图层未构建错误,即使在 tensorflow 2.0.0 中的 model.build() 之后 - Layer not built error, even after model.build() in tensorflow 2.0.0 从检查点加载TensorFlow模型并更改最后一层错误 - Loading TensorFlow model from checkpoint and change last layer, Error 在 Tensorflow 2.0 中使用我的自定义嵌入层时出错 - Getting error while using my custom embedding layer in Tensorflow 2.0 训练 CNN 模型时出错(tensorflow) - Getting error when training the CNN model(tensorflow) 顺序 model tensorflow 中的自定义层 - Custom layer in sequential model tensorflow tensorflow model 中的随机 select 层 - Randomly select layer in tensorflow model 在使用Tensorflow服务提供Keras构建的Tensorflow模型时,连接层出现错误 - Error with Concatenation layer when serving Keras-built Tensorflow model with Tensorflow Serving Tensorflow Model 输入形状错误:层顺序_11的输入0与层不兼容:未定义等级,但层需要定义的等级 - Tensorflow Model Input Shape Error: Input 0 of layer sequential_11 incompatible with layer: rank undefined, but the layer requires a defined rank Tensorflow:在另一个 model 中使用 model 作为层 - Tensorflow: Use model inside another model as layer 在 Tensorflow 2.0 中使用层子类化在我的自定义层中出现此错误“必须始终传递 `Layer.call` 的第一个参数。” - Getting this error in my custom layer using layer subclassing in Tensorflow 2.0 “The first argument to `Layer.call` must always be passed.”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM