我创建了一个模型,使用的是预训练模型 xception,
数据集是Oxford-IIIT Pet Dataset
该模型已经可以预测出宠物的头部框,我根据需要又增加了一个输出,输出宠物类别,但是训练报错
NotImplementedError: Cannot convert a symbolic Tensor (IteratorGetNext:1) to a numpy array
https://wwlw.lanzouy.com/i5T4f0kxe60j (ipynb文件链接)
[Python] 纯文本查看 复制代码 # 模型构造
xception = tf.keras.applications.Xception(weights= 'imagenet',
include_top=False,
input_shape=(224,224,3))
inputs = tf.keras.layers.Input(shape=(224,224,3))
x = xception(inputs)
x = tf.keras.layers.GlobalAveragePooling2D()(x)
x1 = tf.keras.layers.Dense(2048, activation='relu')(x)
x1 = tf.keras.layers.Dense(256, activation='relu')(x)
out1 = tf.keras.layers.Dense(1,name='out1')(x1)
out2 = tf.keras.layers.Dense(1,name='out2')(x1)
out3 = tf.keras.layers.Dense(1,name='out3')(x1)
out4 = tf.keras.layers.Dense(1,name='out4')(x1)
prediction = [out1,out2,out3,out4]
x2 = tf.keras.layers.Dense(2048,activation='relu')(x)
x2 = tf.keras.layers.Dense(1024,activation='relu')(x2)
out_class = tf.keras.layers.Dense(35,activation='softmax',name='out_class')(x2)
model = tf.keras.models.Model(inputs=inputs,
outputs=[prediction,out_class])
model.compile(
tf.keras.optimizers.Adam(learning_rate=0.0001),
loss="mse",
metrics=["mae"]
)
Epochs = 3
history = model.fit(train_dataset,
epochs = Epochs,
steps_per_epoch=STEP_PER_EPOCH,
validation_steps=VALIDATION_STEP,
validation_data=test_dataset
)
|