继上篇。
使用VGG16预训练模型。
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import TensorBoard
from keras.applications import VGG16
from keras import layers,models,optimizers
train_pic_gen=ImageDataGenerator(rescale=1./255,rotation_range=20,width_shift_range=0.2,height_shift_range=0.2,
shear_range=0.2,zoom_range=0.5,horizontal_flip=True,fill_mode='nearest')
test_pic_gen=ImageDataGenerator(rescale=1./255)
train_flow=train_pic_gen.flow_from_directory("data/1_train",(128,128),batch_size=128,class_mode='categorical')
test_flow=test_pic_gen.flow_from_directory("data/1_test",(128,128),batch_size=128,class_mode='categorical')
# print(test_flow.class_indices)
### {'169': 0, '26': 1, '6': 2}
conv_base=VGG16(weights='imagenet',include_top=False,input_shape=(128,128,3))
model = models.Sequential()
model.add(conv_base)
model.add(layers.Flatten())
model.add(layers.Dense(1024, activation='relu'))
model.add(layers.Dense(20, activation='softmax'))
trainable=False
for layer in conv_base.layers:
if layer.name=='block4_conv1':
trainable=True
layer.trainable=trainable
model.summary()
model.compile(loss='categorical_crossentropy',optimizer=optimizers.adam(lr=1e-5),metrics=['acc'])
model.fit_generator(train_flow,steps_per_epoch=60,epochs=100,verbose=1,
validation_data=test_flow,validation_steps=20,callbacks=[TensorBoard(log_dir='output/logs/t')])
model.save("output/classify3_vgg16_pro.h5")
查看结果(混淆矩阵、kappa、分类报告)。test_in_te.py
from keras.models import load_model
import numpy as np
import cv2
import os
model=load_model("output/classify3_vgg16_pro.h5")
vec={'bf': 0, 'br': 1, 'cc': 2, 'cx_b': 3, 'cx_gw': 4, 'cx_rw': 5, 'dp': 6, 'gf': 7, 'gh': 8, 'gr': 9, 'greenh': 10, 'lt': 11, 'of': 12, 'ptc': 13, 'rf': 14, 'st': 15, 'wm': 16, 'wr': 17, 'wt': 18, 'xkc': 19}
#使用测试集数据验证
res=[]
true_label=[]
def get_inputs(src=[]):
pre_x = []
for s in src:
input = cv2.imread(s)
input = cv2.resize(input, (128, 128))
input = cv2.cvtColor(input, cv2.COLOR_BGR2RGB)
pre_x.append(input) # input一张图片
pre_x = np.array(pre_x) / 255.0
return pre_x
org_dir="data/1_test/"
for dir in os.listdir(org_dir):
print("deal:%s" % dir)
label=dir
for d in os.listdir(org_dir + dir):
pre_x=get_inputs([org_dir+dir+"/"+d])
pred=np.argmax(model.predict(pre_x)[0])
pred=list(vec.keys())[pred]
res.append(pred)
true_label.append(label)
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.metrics import cohen_kappa_score
print(confusion_matrix(true_label,res))
print()
print(classification_report(true_label,res))
print("kappa: ",cohen_kappa_score(true_label,res))
mat = confusion_matrix(true_label,res)
sns.heatmap(mat,annot=True,square=True,fmt="d",xticklabels=list(vec.keys()),yticklabels=list(vec.keys()))
plt.show()