实操是基于11种天气图像的数据集,选择了当下在图像分类输出结果较好的一些神经网络模型做迁移学习,
在输出单模型结果之后,想尝试做一个基于联合投票机制的复合模型,但是一直尝试失败,求问各位怎么操作比较好,询问一下调参的建议,下附部分建立模型时候的代码,从拆分数据集到两个单模型的训练。
train_df, test_df = train_test_split(data, test_size=0.2, random_state=2)
def gen(pre,train,test):
train_datagen = ImageDataGenerator(preprocessing_function=pre, validation_split=0.2)
test_datagen = ImageDataGenerator(preprocessing_function=pre)
train_gen = train_datagen.flow_from_dataframe(
dataframe=train,
x_col='File_Path',
y_col='Labels',
target_size=(100,100),
class_mode='categorical',
batch_size=32,
shuffle=True,
seed=0,
subset='training',
rotation_range=30,
zoom_range=0.15,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.15,
horizontal_flip=True,
fill_mode="nearest")
valid_gen = train_datagen.flow_from_dataframe(
dataframe=train,
x_col='File_Path',
y_col='Labels',
target_size=(100,100),
class_mode='categorical',
batch_size=32,
shuffle=False,
seed=0,
subset='validation',
rotation_range=30,
zoom_range=0.15,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.15,
horizontal_flip=True,
fill_mode="nearest")
test_gen = test_datagen.flow_from_dataframe(
dataframe=test,
x_col='File_Path',
y_col='Labels',
target_size=(100,100),
color_mode='rgb',
class_mode='categorical',
batch_size=32,
verbose=0,
shuffle=False)
return train_gen, valid_gen, test_gen
def func(name_model):
pre_model = name_model(input_shape=(100,100, 3),
include_top=False,
weights='imagenet',
pooling='avg')
pre_model.trainable = False
inputs = pre_model.input
x = Dense(100, activation='relu')(pre_model.output)
x = Dense(100, activation='relu')(x)
outputs = Dense(11, activation='softmax')(x)
model = Model(inputs=inputs, outputs=outputs)
model.compile(loss = 'categorical_crossentropy',optimizer='Adam',metrics=['accuracy'])
my_callbacks = [EarlyStopping(monitor='val_loss',
min_delta=0,
patience=2,
mode='auto')]
return model, my_callbacks
def plot(history,test_gen,train_gen,model):
# Plotting Accuracy, val_accuracy, loss, val_loss
fig, ax = plt.subplots(1, 2, figsize=(10, 3))
ax = ax.ravel()
for i, met in enumerate(['accuracy', 'loss']):
ax[i].plot(history.history[met])
ax[i].plot(history.history['val_' + met])
ax[i].set_title('Model {}'.format(met))
ax[i].set_xlabel('epochs')
ax[i].set_ylabel(met)
ax[i].legend(['Train', 'Validation'])
# Predict Data Test
pred = model.predict(test_gen )
pred = np.argmax(pred,axis=1)
labels = (train_gen.class_indices)
labels = dict((v,k) for k,v in labels.items())
pred = [labels[k] for k in pred]
# Classification report
cm=confusion_matrix(test_df.Labels,pred)
clr = classification_report(test_df.Labels, pred)
print(clr)
# Display 6 picture of the dataset with their labels
fig, axes = plt.subplots(nrows=4, ncols=3, figsize=(12, 8),
subplot_kw={'xticks': [], 'yticks': []})
for i, ax in enumerate(axes.flat):
ax.imshow(plt.imread(test_df.File_Path.iloc[i+1]))
ax.set_title(f"True: {test_df.Labels.iloc[i+1]}\nPredicted: {pred[i+1]}")
plt.tight_layout()
plt.show()
return history
def result_test(test,model_use):
results = model_use.evaluate(test, verbose=0)
print(" Test Loss: {:.5f}".format(results[0]))
print("Test Accuracy: {:.2f}%".format(results[1] * 100))
return results
from tensorflow.keras.applications import EfficientNetB7
from tensorflow.keras.applications.efficientnet import preprocess_input
ENet_pre=preprocess_input
train_gen_ENet, valid_gen_ENet, test_gen_ENet = gen(ENet_pre,train_df,test_df)
ENet_model, callback=func(EfficientNetB7)
history = ENet_model.fit(
train_gen_ENet,
validation_data=valid_gen_ENet,
epochs=100,
callbacks=callback,
verbose=0
)
history_ENet=plot(history,test_gen_ENet,train_gen_ENet, ENet_model)
result_ENet = result_test(test_gen_ENet,ENet_model)
from tensorflow.keras.applications import ResNet50
from tensorflow.keras.applications.resnet50 import preprocess_input
ResNet_pre=preprocess_input
train_gen_ResNet, valid_gen_ResNet, test_gen_ResNet = gen(ResNet_pre,train_df,test_df)
ResNet_model, callback=func(ResNet50)
history = ResNet_model.fit(
train_gen_ResNet,
validation_data=valid_gen_ResNet,
epochs=100,
callbacks=callback,
verbose=0
)
history_ResNet= plot(history,test_gen_ResNet,train_gen_ResNet, ResNet_model)
result_ResNet = result_test(test_gen_ResNet,ResNet_model)