安装程序
我在Azure DSVM STANDARD_NC6 (6核,56 GB RAM,380 GB磁盘)上使用1 GPU,使用Python3.6,TF 2.4.0
参数/模型
我有训练数据:形状为print(xtrain.shape)的(4599, 124, 124, 3)和作为分类的ytrain。
我用的是一个经典的发电机
datagen = ImageDataGenerator(
zoom_range=0.1,
rotation_range=25,
width_shift_range=0.1,
height_shift_range=0.1,
shear_range=0.15,
horizontal_flip=True,
fill_mode="nearest"
)
datagen.fit(xtrain)我的模型是有自己脑袋的基本mobilenetv2:
baseModel = MobileNetV2(weights="imagenet",
include_top=False,
input_tensor=Input(shape=(224, 224,3)),
#input_shape=(224, 224, 3),
)
headModel = baseModel.output
headModel = AveragePooling2D(pool_size=(7, 7))(headModel)
headModel = Flatten(name="flatten")(headModel)
headModel = Dense(64, activation="relu")(headModel)
headModel = Dropout(0.5)(headModel)
headModel = Dense(2, activation="softmax")(headModel)
model = Model(inputs=baseModel.input, outputs=headModel)
for layer in baseModel.layers:
layer.trainable = False
model.compile(loss="mse", optimizer='adam', metrics=["accuracy"])当我现在符合模型的时候
Batch_Size=1
h = model.fit(
datagen.flow(xtrain, ytrain, batch_size=Batch_Size),
steps_per_epoch=len(xtrain) // Batch_Size,
validation_data=(xval, yval),
validation_steps=len(xval) // Batch_Size,
epochs=EPOCHS,
callbacks=[model_checkpoint_callback, Board])错误
我得到错误(全部相同,但随批处理大小和损失函数而变化)。
当我将batch_size=1与loss=mse、categorical_crossentropy或其他工具一起使用时,模型会对模型进行训练,但会在时代结束时抛出以下错误
ValueError:输入0与图层model_2不兼容:预期的shape=(无,224,224,3),找到shape=(1,124,124,3)
如果我使用高于1的batch_size (例如,在loss=categorical_crossentropy中使用32 ),则会在培训之前抛出错误:
InvalidArgumentError:不兼容的形状:32 vs. [节点相等(定义为:12) ]
用loss=mse
gradient_tape/mean_squared_error/BroadcastGradientArgs:
InvalidArgumentError:不兼容的形状:0,2 vs. 32,2 [节点InvalidArgumentError(定义为:12) ]
如果我更改最后一个密集层的隐藏单位,则错误将更改为该值。例如:
...
headModel = Dense(5, activation="softmax")(headModel)结果:
InvalidArgumentError:不兼容形状:0,5 vs. 32,2
显然正确的输入形状会在某些地方丢失。特别是批次的大小(二维是基于密集的隐藏单元)。有谁有主意吗?谢谢
我在git上查看了这个旧线程的许多答案:https://github.com/kuza55/keras-extras/issues/7,但在那里找不到解决方案。
发布于 2021-01-22 13:58:24
您要输入到网络的数据必须具有与网络输入相同的形状。您正在尝试向接受维度224x224x3图像的网络提供具有维度124x124x3的数据。
你可以:
baseModel = MobileNetV2(weights=None,include_top=False,input_tensor=Input(shape=(124,124,3))
这种方法的缺点是不能使用预先训练的权重。
124xx124图像调整为224x224。做这件事有很多方法,但如果你愿意保留ImageDataGenerator,我建议你先动手。https://stackoverflow.com/questions/65843923
复制相似问题