Python InvalidArgumentError:不兼容的形状:[32]与[8]-Keras迁移学习
我正在尝试使用Mobilenet进行迁移学习 这是我的密码Python InvalidArgumentError:不兼容的形状:[32]与[8]-Keras迁移学习,python,keras,deep-learning,transfer-learning,Python,Keras,Deep Learning,Transfer Learning,我正在尝试使用Mobilenet进行迁移学习 这是我的密码 from keras.applications.mobilenet import preprocess_input from keras.applications.mobilenet import MobileNet import keras.backend as K x_train_final=preprocess_input(x_train) x_test_final=preprocess_input(x_test) pretra
from keras.applications.mobilenet import preprocess_input
from keras.applications.mobilenet import MobileNet
import keras.backend as K
x_train_final=preprocess_input(x_train)
x_test_final=preprocess_input(x_test)
pretrained_weights='imagenet'
#2
mobile=MobileNet(weights=pretrained_weights,include_top=False,input_shape=(416,416,3))
x=mobile.output
x=keras.layers.Flatten()(x)
x=keras.layers.Dense(512)(x)
x=keras.layers.Activation("relu")(x)
x=keras.layers.Dense(256)(x)
x=keras.layers.Activation("sigmoid")(x)
x=keras.layers.Dense(8)(x)
output=x
model=keras.models.Model(inputs=mobile.input,outputs=output)
def custom_loss(y_true, y_pred):
loss = K.square(y_pred - y_true) # (batch_size, 8)
# summing both loss values along batch dimension
loss = K.sum(loss, axis=0) # (batch_size,)
return loss
model.compile(optimizer = keras.optimizers.Adam(learning_rate=0.002),
loss = custom_loss,
metrics = ['accuracy', 'mse'])
model.fit(x_train_final, y_train, epochs = 100)
但是我遇到了一个错误
Epoch 1/100
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
<ipython-input-37-55c4376c8a25> in <module>()
----> 1 model.fit(x_train_final, y_train, epochs = 100)
7 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
58 ctx.ensure_initialized()
59 tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
---> 60 inputs, attrs, num_outputs)
61 except core._NotOkStatusException as e:
62 if name is not None:
InvalidArgumentError: Incompatible shapes: [32] vs. [8]
[[node gradients_4/loss_4/dense_18_loss/custom_loss/weighted_loss/mul_grad/Mul_1 (defined at /usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py:3009) ]] [Op:__inference_keras_scratch_graph_111168]
Function call stack:
keras_scratch_graph
纪元1/100
---------------------------------------------------------------------------
InvalidArgumentError回溯(最后一次最近调用)
在()
---->1个模型。拟合(x_系列最终版,y_系列,历代=100)
7帧
/快速执行中的usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/execute.py(op_name、num_output、input、attrs、ctx、name)
58 ctx.确保_已初始化()
59张量=pywrap\u tfe.tfe\u Py\u Execute(ctx.\u句柄、设备名称、操作名称、,
--->60个输入、属性、数量输出)
61除堆芯外,其他状态除外,如e:
62如果名称不是无:
InvalidArgumentError:不兼容的形状:[32]与[8]
[[node gradients_4/loss_4/dense_18_loss/custom_loss/weighted_loss/mul_grad/mul_1(定义于/usr/local/lib/python3.6/dist packages/keras/backend/tensorflow_backend.py:3009)][Op:[Op:[u推断\u keras\u scratch\u graph\u 111168]
函数调用堆栈:
keras_划痕图
我的火车数据集形状是(3066416,416,3)
测试数据集形状为(100416416,3)
我无法找出错误。我也面临同样的问题,发现我的池层存在问题。它的输出不正确。将maxpool2d更改为GlobalAvarAgepool解决了此问题。如果您想坚持使用maxpool,请更改它的输出形状。我也遇到了同样的问题,发现我的池层存在问题。它的输出不正确。将maxpool2d更改为GlobalAvarAgepool解决了此问题。如果要坚持使用maxpool,请更改其输出形状。x_train_final和y_train的形状是什么?x_train_final-(3066、416、416、3)y_train-(3066、8)在自定义损耗函数中,在计算损耗之前打印张量y_pred和y_true,并检查它们是否具有与加载预训练权重相同的形状和数据类型。但对于416*416图像大小的图像,keras中不存在预训练形状。因此,要么训练模型生成权重,要么更改图像的大小。预训练权重用于128*128、160*160、192*192和224*224的图像大小。也许这会solve@devSpartan有助于更改损失函数,谢谢x_train_final和y_train的形状是什么?x_train_final-(3066、416、416、3)y_train-(3066、8)在自定义损耗函数中,在计算损耗之前打印张量y_pred和y_true,并检查它们是否具有与加载预训练权重相同的形状和数据类型。但对于416*416图像大小的图像,keras中不存在预训练形状。因此,要么训练模型生成权重,要么更改图像的大小。预训练权重用于128*128、160*160、192*192和224*224的图像大小。也许这会solve@devSpartan更改损失函数有帮助,谢谢