带有 VGG16 模型拟合误差的迁移学习
Posted
技术标签:
【中文标题】带有 VGG16 模型拟合误差的迁移学习【英文标题】:Transfer Learning with VGG16 Model Fit Error 【发布时间】:2021-12-02 05:16:23 【问题描述】:我是迁移学习的新手,无法理解导致以下错误的原因:ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type int)
运行 model.fit
时。什么可能导致这个问题?
#created dataframes for training, validation, and testing
#Example of what dataframe looks like:
dataframe.head(1)
Sex Weight File
0 female 124 1_124_3_20161220221743058.jpg
weight_label = df.columns[1]
sex_label = df.columns[0]
labels = [classlabel for classlabel in df.columns[:2]]
train_datagen = ImageDataGenerator(
rescale=1./255,
rotation_range = 40,
width_shift_range = 0.4,
height_shift_range = 0.4
)
test_datagen = ImageDataGenerator(rescale=1./255)
subfolder = "./training/"
#Dataframe is simply partitioned as you would splitting by dataset
training_dataframe, validation_dataframe, testing_dataframe
train_generator=train_datagen.flow_from_dataframe(
dataframe=training_dataframe,
directory=directory_dataset_path,
x_col="file",
y_col=labels,
batch_size=32,
seed=42,
shuffle=True,
class_mode="raw"
)
valid_generator=test_datagen.flow_from_dataframe(
dataframe=validation_dataframe,
directory=directory_dataset_path,
x_col="file",
y_col=labels,
batch_size=32,
seed=42,
shuffle=True,
class_mode="raw"
)
Base_VGG16 = VGG16(weights = 'imagenet',include_top = False)
for layer in Base_VGG16[:12]:
layer.trainable = False
sex_model = Base_VGG16.output
sex_model = GlobalAveragePooling2D()(sex_model)
sex_model = Dropout(0.5)(sex_model)
predict_sex = Dense(2, activation='sigmoid')(sex_model)
weight_model = Base_VGG16.output
weight_model = GlobalAveragePooling2D()(weight_model)
weight_model = Dropout(0.5)(weight_model)
predict_weight = Dense(1, activation='relu')(weight_model)
model = Model(inputs=Base_VGG16.input, outputs=[predict_sex, predict_weight])
model.compile(loss =['binary_crossentropy','mae'],
optimizer=SGD(lr=1e-4, momentum=0.9),
metrics=['accuracy','mae'])
history=model.fit(
train_generator,
steps_per_epoch=5000 // 32,
epochs=10,
validation_data=valid_generator,
validation_steps=1500 // 32
)
【问题讨论】:
我认为您需要发布更多代码,train_generator
和 valid_generator
定义似乎不在您给定的 sn-p 中。
感谢您的建议。我为两者都添加了代码。
【参考方案1】:
您的数据框的对象 dtypes 可能格式不正确。
使用training_dataframe.info()
了解数据类型。还要检查您的数据框中是否有任何 NaN 值
training_dataframe['Weight'] = training_dataframe['Weight'].astype(int)
也尝试使用 Encoders 对分类特征进行编码
【讨论】:
谢谢。这非常有帮助。类型是:dtypes: int64(1), object(2)
所以我需要将这两个对象类型转换为 int64 正确吗?
是的,不正确的 dtypes 会导致此类问题。
只是为了确认,我的理解是否正确,在 dtypes: object
上运行 model.fit
会导致错误?对象无法处理,必须先转换?
是的,ML 模型不理解分类数据。所以它应该被编码。以上是关于带有 VGG16 模型拟合误差的迁移学习的主要内容,如果未能解决你的问题,请参考以下文章