我正在尝试使用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)
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
“我的火车”数据集形状为(3066、416、416、3)
测试数据集的形状为(100,416,416,3)
我无法弄清错误。
答案 0 :(得分:0)
我也面临同样的问题,发现我的池层存在问题。它给出了错误的输出。将maxpool2d更改为globalaevaragepool可解决此问题。如果要坚持使用maxpool,请更改其输出形状。