使用 VGG16 模型拟合误差进行迁移学习

Transfer Learning with VGG16 Model Fit Error

我是迁移学习的新手,无法理解导致以下错误的原因:ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type int) when 运行 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
    )

您的数据框对象数据类型可能格式不正确。 使用 training_dataframe.info() 了解数据类型。还要检查您的数据框中是否有任何 NaN 值

training_dataframe['Weight'] = training_dataframe['Weight'].astype(int)

也尝试使用编码器对分类特征进行编码