keras - 带有 VGG16 模型拟合误差的迁移学习
问题描述
我是迁移学习的新手,无法理解导致以下错误的原因: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
)
解决方案
您的数据框的对象 dtypes 可能格式不正确。用于training_dataframe.info()
了解 dtypes。还要检查您的数据框中是否有任何 NaN 值
training_dataframe['Weight'] = training_dataframe['Weight'].astype(int)
也尝试使用编码器来编码分类特征
推荐阅读
- ide - WebStorm 2020.1.3 Backspace 自动删除空对象/字符串
- javascript - HttpClient 调用有时不会触发
- typescript - 你能定义一个复杂的 Typescript 返回类型内联(没有显式接口)吗?
- python - 由于多个版本导致的 Python 路径问题
- r - 地理加权逻辑回归不同维度
- ruby - 在 ruby-alpine docker 镜像中添加特定的节点版本
- r - 如何为 r 中的每一行找到两个表的百分比变化?
- javascript - 如何在 VueJs 中添加/推送深层分层数据
- pandas - 熊猫中的#DataFrame
- python - 使用 Python/C# 进行动态 Google 搜索