我在 中创建了一个混合模型Keras
,为元数据和图像数据创建权重,然后将它们组合起来进行分类。这是模型:
Model: "model_1"
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_5 (InputLayer) [(None, 80, 120, 3)] 0
__________________________________________________________________________________________________
xception (Functional) (None, 3, 4, 2048) 20861480 input_5[0][0]
__________________________________________________________________________________________________
input_4 (InputLayer) [(None, 10)] 0
__________________________________________________________________________________________________
conv2d_9 (Conv2D) (None, 3, 4, 8) 409608 xception[0][0]
__________________________________________________________________________________________________
dense_3 (Dense) (None, 4) 44 input_4[0][0]
__________________________________________________________________________________________________
global_average_pooling2d_1 (Glo (None, 8) 0 conv2d_9[0][0]
__________________________________________________________________________________________________
concatenate_1 (Concatenate) (None, 12) 0 dense_3[0][0]
global_average_pooling2d_1[0][0]
__________________________________________________________________________________________________
dense_4 (Dense) (None, 4) 52 concatenate_1[0][0]
__________________________________________________________________________________________________
dense_5 (Dense) (None, 1) 5 dense_4[0][0]
==================================================================================================
Total params: 21,271,189
Trainable params: 21,216,661
Non-trainable params: 54,528
__________________________________________________________________________________________________
由于不平衡,我决定增加图像。我使用了以下 ImageDataGenerator:
aug = ImageDataGenerator(rescale=1/255.,
rotation_range=180,
height_shift_range=0.2,
width_shift_range=0.2,
brightness_range=[0.5,1.5],
channel_shift_range=100.0,
horizontal_flip=True,
vertical_flip=True,
shear_range=45.0)
然后我编译并尝试使用以下方法训练模型ImageDataGenerator().flow()
:
epochs = 10
BATCH_SIZE = 20
flow = aug.flow(img_train, y_train, batch_size=BATCH_SIZE)
history = model.fit([meta_train, flow], y_train, epochs=epochs, batch_size=100, validation_data=([meta_test, img_test], y_test), class_weight=class_weight)
这给了我一个错误:
ValueError: Failed to find data adapter that can handle input: (<class 'list'> containing values of
types {"<class 'pandas.core.frame.DataFrame'>", "<class 'tensorflow.python.keras.preprocessing.image.NumpyArrayIterator'>"}),
<class 'numpy.ndarray'>
我已经尝试了多个版本的代码,但我只是对后端不够熟悉,无法正确诊断问题。谁能帮我这个?
型号代码和 MRE
型号代码
LEARNING_RATE = 0.001
# Define inputs
meta_inputs = Input(shape=(10,))
img_inputs = Input(shape=(80,120,3,))
# Model 1
meta_layer1 = Dense(4, activation='relu')(meta_inputs)
# Model 2
xception_layer = Xception(include_top=False, input_shape=(80,120,3,))(img_inputs)
img_conv_layer1 = Conv2D(8, kernel_size=(5,5), padding='same', activation='relu')(xception_layer)
img_gap_layer = GlobalAveragePooling2D()(img_conv_layer1)
# img_sdense_layer = Dense(4, activation='relu')(img_gap_layer)
# Merge models
merged_layer = Concatenate()([meta_layer1, img_gap_layer])
merged_dense_layer = Dense(4, activation='relu')(merged_layer)
merged_output = Dense(1, activation='sigmoid')(merged_dense_layer)
# Define functional model
model = Model(inputs=[meta_inputs, img_inputs], outputs=merged_output)
# Compile model
auc = AUC(name = 'auc')
model.compile(Adam(learning_rate=LEARNING_RATE), loss='binary_crossentropy', metrics=[auc])
model.summary()
元训练 MRE
age_approx Unknown female male head/neck lower extremity \
11655 45 0 0 1 0 0
24502 60 0 0 1 0 1
2524 50 0 1 0 0 1
13894 60 0 1 0 0 0
29325 45 0 1 0 0 1
oral/genital palms/soles torso upper extremity
11655 0 0 1 0
24502 0 0 0 0
2524 0 0 0 0
13894 0 0 1 0
29325 0 0 0 0
img_train MRE
数组太大,请参见此处的代码。
y_train.shape
(23188, 1)