目录
1、网络训练、Acc和Loss可视化输出
# -*- coding: utf-8 -*-
"""
Created on Sat Apr 11 23:27:41 2020
@author: ASUS
"""
import glob
import os
import cv2
import numpy as np
from keras import layers
from keras import models
from keras.utils import to_categorical
import tensorflow as tf
from tensorflow import keras
import matplotlib.pyplot as plt
from keras.layers import Dropout
from keras import regularizers
%matplotlib inline
#解决出现问题:UnknownError: 2 root error(s) found. (0) Unknown: Failed to get convolution algorithm.
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
sess = tf.Session(config = config)
keras.backend.set_session(sess)
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = "0"#指定在第0块GPU上跑
#第一个00
picture_path_00 = 'E:/CNN_flow/test_flow/test_accuracy/picture_2_01'
#print(len(picture_path_00))
paths_00 = glob.glob(os.path.join(picture_path_00, '*.jpg'))
lenth_picture_path_00 = len(picture_path_00) + 1
#读取本地图片并按照文件名排序
paths_00.sort(key=lambda x:int(x[ lenth_picture_path_00 :-4]))
#按照地址中间的数字排序,例:‘E:\CNN测流量\仿真实验\转化时频图0.01\1.jpg’
train = []
for path in paths_00:
img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
img = cv2.resize(img, (775,556))
train.append(img)
#图片大小,img_a 高度,img_b 宽度
img_a, img_b = img.shape
print('img_a:%s img_b:%s' %(img_a, img_b))
##第二个01
#picture_path_01 = 'E:/CNN_flow/test_flow/test_accuracy/picture_5_6_0.005_2'
##print(len(picture_path_01))
#paths_01 = glob.glob(os.path.join(picture_path_01, '*.jpg'))
#
##读取本地图片并按照文件名排序
#paths_01.sort(key=lambda x:int(x[ (len(picture_path_01)+1) :-4]))
##按照地址中间的数字排序,例:‘E:/personal_file/tensorflow_file/all_images\1.jpg’
#for path in paths_01:
# img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
# img = cv2.resize(img, (875, 656))
# train.append(img)
##
len_train = len(train)
print('len(train):',len(train))
new_data = np.array(train[:len_train])
print(new_data.shape)
new_data = new_data.reshape((len_train, img_a, img_b, 1))
##标签获取
f = open(r"E:/CNN_flow/test_flow/test_accuracy/label_2_01.txt")
line = f.readline()
data_list = []
while line:
num = list(map(str,line.split()))
#print(num[0])
data_list.append(num[0])
line = f.readline()
f.close()
print('len(label):',len(data_list))
len_labels = len(data_list)
labels = np.array(data_list[:len_labels])
print('labels:',labels[0:100])
#lb = LabelBinarizer()
#labels = lb.fit_transform(labels) # transfer label to binary value
#print(labels)
print(labels.shape)
#以样本数量生成乱序的list
permutation = np.arange(len_labels)
np.random.shuffle(permutation)
#按照随机生成的顺序重新排列数据集
new_data = new_data[permutation]
labels = labels[permutation]
print('new_labels:',labels[0:100])
#print(new_data[0].shape)
tf.reset_default_graph()
keras.backend.clear_session()
class LossHistory(keras.callbacks.Callback):
def on_train_begin(self, logs={}):
self.losses = {'batch':[], 'epoch':[]}
self.accuracy = {'batch':[], 'epoch':[]}
self.val_loss = {'batch':[], 'epoch':[]}
self.val_acc = {'batch':[], 'epoch':[]}
def on_batch_end(self, batch, logs={}):
self.losses['batch'].append(logs.get('loss'))
self.accuracy['batch'].append(logs.get('acc'))
self.val_loss['batch'].append(logs.get('val_loss'))
self.val_acc['batch'].append(logs.get('val_acc'))
def on_epoch_end(self, batch, logs={}):
self.losses['epoch'].append(logs.get('loss'))
self.accuracy['epoch'].append(logs.get('acc'))
self.val_loss['epoch'].append(logs.get('val_loss'))
self.val_acc['epoch'].append(logs.get('val_acc'))
def loss_plot(self, loss_type):
iters = range(len(self.losses[loss_type]))
plt.figure()
# acc
plt.plot(iters, self.accuracy[loss_type], 'r', label='train acc')
# loss
plt.plot(iters, self.losses[loss_type], 'g', label='train loss')
if loss_type == 'epoch':
# val_acc
plt.plot(iters, self.val_acc[loss_type], 'b', label='val acc')
# val_loss
plt.plot(iters, self.val_loss[loss_type], 'k', label='val loss')
plt.grid(True)
plt.xlabel(loss_type)
plt.ylabel('acc-loss')
plt.legend(loc="upper right")
plt.show()
#分类类别
model = models.Sequential()
model.add(layers.Conv2D(96, (8, 11), strides=(4, 4), activation='relu', input_shape=(img_a, img_b, 1)))
model.add(layers.MaxPooling2D((2, 2), strides=(2, 2)))
model.add(layers.Conv2D(186, (5, 5), strides=(1, 1), activation='relu'))
model.add(layers.MaxPooling2D((3, 3), strides=(2, 2)))
model.add(layers.Conv2D(256, (2, 3), strides=(1, 1), activation='relu'))
model.add(layers.MaxPooling2D((3, 3), strides=(2, 2)))
model.add(Dropout(0.4))
model.add(layers.Flatten())
model.add(layers.Dense(256, activation='relu'))
model.add(Dropout(0.25))
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(6 , activation='softmax'))
print('模型加载')
len_train_images = int(len_labels / 10 * 6)
len_val_images = int(len_labels / 10 * 8)
print('len_train_images: %s len_val_images: %s' %(len_train_images,len_val_images))
#print('len_val_images:',labels[0:100])
train_images = new_data[:len_train_images]
val_images = new_data[len_train_images:len_val_images]
test_images = new_data[len_val_images:]
train_labels = labels[:len_train_images]
val_labels = labels[len_train_images:len_val_images]
test_labels = labels[len_val_images:]
train_images = train_images.astype('float32') / 255
val_images = val_images.astype('float32') / 255
test_images = test_images.astype('float32') / 255
train_labels = train_labels.reshape(train_labels.shape[0])
val_labels = val_labels.reshape(val_labels.shape[0])
test_labels = test_labels.reshape(test_labels.shape[0])
#print(train_labels.shape)
##print(val_labels.shape)
#print(test_labels.shape)
print("数据集")
train_labels = to_categorical(train_labels,sorts)
val_labels = to_categorical(val_labels,sorts)
test_labels = to_categorical(test_labels,sorts)
#train_labels = to_categorical(train_labels)
#val_labels = to_categorical(val_labels)
#test_labels = to_categorical(test_labels)
print('len(train_labels):',len(train_labels))
#print('len(val_labels):',len(val_labels))
print('len(test_labels):',len(test_labels))
print(train_labels.shape)
#print(val_labels.shape)
print(test_labels.shape)
print('变换')
print('模型')
model.compile(optimizer = 'rmsprop',
loss = 'categorical_crossentropy',
metrics = ['accuracy'])
history = LossHistory()
#acc和loss分开输出
model.fit(train_images, train_labels, batch_size=64, nb_epoch=50, validation_data=(val_images,val_labels),validation_steps=None,callbacks=[history])
#model.fit(train_images, train_labels, batch_size=64, nb_epoch=6, validation_steps=None,callbacks=[history])
history.loss_plot('epoch')
test_loss, test_acc = model.evaluate(test_images, test_labels)
print(test_acc, test_loss)
2、模型保存与导入
#保存模型
model.save('E:/LeNet/test_try.h5')
#加载保存的模型
model=load_model('E:/LeNet/test_try.h5')
3、模型结构可视化与具体内容显示
from keras.utils import plot_model
plot_model(model, to_file='model_1.png', show_shapes= True, show_layer_names= True, rankdir='TB')
model.summary()
4、中间层可视化
image_arr=np.reshape(train_images[100], (-1,556, 775,1))
#可视化第一个MaxPooling2D
layer_1 = K.function([model.layers[0].input], [model.layers[1].output])
# 只修改inpu_image
f1 = layer_1([image_arr])[0]
# 第一层卷积后的特征图展示,输出是(1,12,12,6),(样本个数,特征图尺寸长,特征图尺寸宽,特征图个数)
re = np.transpose(f1, (0,3,1,2))
for i in range(96):
plt.subplot(8,12,i+1)
plt.imshow(re[0][i]) #,cmap='gray'
plt.axis('off')
plt.show()
#可视化第二个MaxPooling2D
layer_2 = K.function([model.layers[0].input], [model.layers[3].output])
f2 = layer_2([image_arr])[0]
# 第一层卷积后的特征图展示,输出是(1,4,4,16),(样本个数,特征图尺寸长,特征图尺寸宽,特征图个数)
re = np.transpose(f2, (0,3,1,2))
for i in range(186):
plt.subplot(14,14,i+1)
plt.imshow(re[0][i]) #, cmap='gray'
plt.axis('off')
plt.show()
#可视化第三个MaxPooling2D
layer_3 = K.function([model.layers[0].input], [model.layers[5].output])
f3= layer_3([image_arr])[0]
# 第一层卷积后的特征图展示,输出是(1,4,4,16),(样本个数,特征图尺寸长,特征图尺寸宽,特征图个数)
re = np.transpose(f3, (0,3,1,2))
for i in range(256):
plt.subplot(16,16,i+1)
plt.imshow(re[0][i]) #, cmap='gray'
plt.axis('off')
plt.show()
5、混淆矩阵可视化
from keras import backend as K
from keras.models import load_model
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix
import numpy as np
import matplotlib.pyplot as plt
import numpy as np
import itertools
def plot_confusion_matrix(cm,
target_names,
title='Confusion matrix',
cmap=plt.cm.Greens,#这个地方设置混淆矩阵的颜色主题,这个主题看着就干净~
normalize=True):
accuracy = np.trace(cm) / float(np.sum(cm))
misclass = 1 - accuracy
if cmap is None:
cmap = plt.get_cmap('Blues')
plt.figure(figsize=(15, 12))
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title)
plt.colorbar()
if target_names is not None:
tick_marks = np.arange(len(target_names)+1)
plt.xticks(tick_marks-0.5, target_names, rotation=45)
plt.yticks(tick_marks-0.5, target_names)
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
thresh = cm.max() / 1.5 if normalize else cm.max() / 2
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
if normalize:
plt.text(j, i, "{:0.4f}".format(cm[i, j]),
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
else:
plt.text(j, i, "{:,}".format(cm[i, j]),
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
plt.tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label\naccuracy={:0.4f}; misclass={:0.4f}'.format(accuracy, misclass))
#这里这个savefig是保存图片,如果想把图存在什么地方就改一下下面的路径,然后dpi设一下分辨率即可。
#plt.savefig('/content/drive/My Drive/Colab Notebooks/confusionmatrix32.png',dpi=350)
plt.show()
# 显示混淆矩阵
def plot_confuse(model, x_val, y_val):
predictions = model.predict_classes(x_val)
truelabel = y_val.argmax(axis=-1) # 将one-hot转化为label
conf_mat = confusion_matrix(y_true=truelabel, y_pred=predictions)
plt.figure()
plot_confusion_matrix(conf_mat, normalize=False,target_names=labels,title='Confusion Matrix')
#=========================================================================================
#最后调用这个函数即可。 test_x是测试数据,test_y是测试标签(这里用的是One——hot向量)
#labels是一个列表,存储了你的各个类别的名字,最后会显示在横纵轴上。
#比如这里我的labels列表
labels = ['1','2','3']
plot_confuse(model, train_images, train_labels)
def plot_confusion_matrix(cm, classes, title='Confusion matrix',cmap=plt.cm.jet):
cmap = plt.get_cmap('Greens') #设置输出主题颜色
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
#plt.figure(figsize=(15, 12))
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title)
plt.colorbar()
tick_marks = np.arange(len(classes)+1)
plt.xticks(tick_marks-0.5, classes, rotation=45)
plt.yticks(tick_marks-0.5, classes)
thresh = cm.max() / 2.
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
plt.text(j, i, '{:.2f}'.format(cm[i, j]), horizontalalignment="center",color="white" if cm[i, j] > thresh else "black")
plt.tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.savefig('混淆矩阵.png',dpi=350)
plt.show()
# 显示混淆矩阵
def plot_confuse(model, x_val, y_val):
predictions = model.predict_classes(x_val)
truelabel = y_val.argmax(axis = -1) # 将one-hot转化为label
conf_mat = confusion_matrix(y_true=truelabel, y_pred=predictions)
plt.figure()
#plot_confusion_matrix(conf_mat,labels)
plot_confusion_matrix(conf_mat,range(np.max(truelabel)+1))
from sklearn.metrics import confusion_matrix
import matplotlib.pyplot as plt
import itertools
#x_val.shape # (25838, 48, 48, 1)
#y_val.shape # (25838, 7)
labels = ['1','2','3']
plot_confuse(model, test_images, test_labels)