深度学习|基于传统神经网络,使用Keras训练自己的数据集

以图像多分类为例
1. 准备数据集:
以图像分类为例,准备了五种花的图片数据(每种各600张图片), 依次存放在'./flower_photos/daisy'、 './flower_photos/dandelion'、'./flower_photos/roses'、'./flower_photos/sunflowers'、'./flower_photos/tulips' 文件夹中。(其中,数据集及模型以上传,可自行下载)
深度学习|基于传统神经网络,使用Keras训练自己的数据集
文章图片

深度学习|基于传统神经网络,使用Keras训练自己的数据集
文章图片

2. 训练代码:

from skimage import io, transform import glob import os import tensorflow as tf import matplotlib.pyplot as plt import numpy as np import time# 数据集地址 path = 'C:/Users/zhf/Desktop/flower/flower_photos/' # 模型保存地址 model_path = 'model/model.ckpt'# 将所有的图片resize成100*100 w = 100 h = 100 c = 3# 读取图片 def read_img(path): cate = [path+x for x in os.listdir(path) if os.path.isdir(path+x)] imgs = [] labels = [] for idx, folder in enumerate(cate):# idx代表的是图像的标签,随着依次遍历每个文件夹,在本程序中的数值为:0、1、2、3、4 for im in glob.glob(folder+'/*.jpg'):# glob.glob获取目录下的所有图片 print('reading the images:%s' % (im)) img=io.imread(im)# 图像读取 img=transform.resize(img,(w,h))# 图像裁剪 imgs.append(img) labels.append(idx)# 标签满足 return np.asarray(imgs, np.float32), np.asarray(labels, np.int32) data, label = read_img(path)# 打乱顺序 num_example = data.shape[0]# 统计总共有3670张图片 arr = np.arange(num_example)# [0 1 2...3669] np.random.shuffle(arr) data = https://www.it610.com/article/data[arr] label = label[arr]# 将所有数据分为训练集和验证集 ratio = 0.8 s = np.int(num_example * ratio) x_train = data[:s] y_train = label[:s] x_val = data[s:] y_val = label[s:]# -----------------构建网络---------------------- # tf.placeholder占位符,暂时储存变量.在会话中,运行模型的时候通过feed_dict()函数向占位符送入数据 x = tf.placeholder(tf.float32, shape=[None, w, h, c], name='x') y_ = tf.placeholder(tf.int32, shape=[None, ], name='y_')def inference(input_tensor, train, regularizer): with tf.variable_scope('layer1-conv1'):# tf.variable_scope实现变量命名空间,变量共享# tf.truncated_normal_initializer按照正态分布 conv1_weights = tf.get_variable("weight", [5, 5, 3, 32], initializer=tf.truncated_normal_initializer(stddev=0.1))# 卷积核大小为5*5*3,暂时理解成在第一层进行了32次卷积操作,第一层有32个神经元 conv1_biases = tf.get_variable("bias", [32], initializer=tf.constant_initializer(0.0)) conv1 = tf.nn.conv2d(input_tensor, conv1_weights, strides=[1, 1, 1, 1], padding='SAME')# 使用SAME时,发生不对应时,边界补0,不丢弃像素点 relu1 = tf.nn.relu(tf.nn.bias_add(conv1, conv1_biases))# 激活的过程with tf.name_scope("layer2-pool1"): pool1 = tf.nn.max_pool(relu1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="VALID")# 发生不对应时,边界丢弃with tf.variable_scope("layer3-conv2"): conv2_weights = tf.get_variable("weight", [5, 5, 32, 64],initializer=tf.truncated_normal_initializer(stddev=0.1)) conv2_biases = tf.get_variable("bias", [64], initializer=tf.constant_initializer(0.0)) conv2 = tf.nn.conv2d(pool1, conv2_weights, strides=[1, 1, 1, 1], padding='SAME') relu2 = tf.nn.relu(tf.nn.bias_add(conv2, conv2_biases))with tf.name_scope("layer4-pool2"): pool2 = tf.nn.max_pool(relu2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID')with tf.variable_scope("layer5-conv3"): conv3_weights = tf.get_variable("weight",[3,3,64,128],initializer=tf.truncated_normal_initializer(stddev=0.1)) conv3_biases = tf.get_variable("bias", [128], initializer=tf.constant_initializer(0.0)) conv3 = tf.nn.conv2d(pool2, conv3_weights, strides=[1, 1, 1, 1], padding='SAME') relu3 = tf.nn.relu(tf.nn.bias_add(conv3, conv3_biases))with tf.name_scope("layer6-pool3"): pool3 = tf.nn.max_pool(relu3, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID')with tf.variable_scope("layer7-conv4"): conv4_weights = tf.get_variable("weight", [3, 3, 128, 128],initializer=tf.truncated_normal_initializer(stddev=0.1)) conv4_biases = tf.get_variable("bias", [128], initializer=tf.constant_initializer(0.0)) conv4 = tf.nn.conv2d(pool3, conv4_weights, strides=[1, 1, 1, 1], padding='SAME') relu4 = tf.nn.relu(tf.nn.bias_add(conv4, conv4_biases))with tf.name_scope("layer8-pool4"): pool4 = tf.nn.max_pool(relu4, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID') nodes = 6*6*128 reshaped = tf.reshape(pool4,[-1,nodes])with tf.variable_scope('layer9-fc1'): fc1_weights = tf.get_variable("weight", [nodes, 1024], initializer=tf.truncated_normal_initializer(stddev=0.1)) if regularizer != None: tf.add_to_collection('losses', regularizer(fc1_weights)) # tf.add_to_collection是把多个变量放入一个自己用引号命名的集合里,也就是把多个变量统一放在一个列表中。 fc1_biases = tf.get_variable("bias", [1024], initializer=tf.constant_initializer(0.1))fc1 = tf.nn.relu(tf.matmul(reshaped, fc1_weights) + fc1_biases) if train: fc1 = tf.nn.dropout(fc1, 0.5)with tf.variable_scope('layer10-fc2'): fc2_weights = tf.get_variable("weight", [1024, 512], initializer=tf.truncated_normal_initializer(stddev=0.1)) if regularizer != None: tf.add_to_collection('losses', regularizer(fc2_weights)) fc2_biases = tf.get_variable("bias", [512], initializer=tf.constant_initializer(0.1))fc2 = tf.nn.relu(tf.matmul(fc1, fc2_weights) + fc2_biases) if train: fc2 = tf.nn.dropout(fc2, 0.5)with tf.variable_scope('layer11-fc3'): fc3_weights = tf.get_variable("weight", [512, 5], initializer=tf.truncated_normal_initializer(stddev=0.1)) if regularizer != None: tf.add_to_collection('losses', regularizer(fc3_weights)) fc3_biases = tf.get_variable("bias", [5], initializer=tf.constant_initializer(0.1)) logit = tf.matmul(fc2, fc3_weights) + fc3_biasesreturn logit#---------------------------网络结束--------------------------- regularizer = tf.contrib.layers.l2_regularizer(0.0001)# 正则化就是给网络加上一些规则,提高模型的适用性 logits = inference(x, False, regularizer)#(小处理)将logits乘以1赋值给logits_eval,定义name,方便在后续调用模型时通过tensor名字调用输出tensor b = tf.constant(value=https://www.it610.com/article/1, dtype=tf.float32) logits_eval = tf.multiply(logits, b, name='logits_eval')loss = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=y_)# 损失函数的交叉熵 train_op = tf.train.AdamOptimizer(learning_rate=0.001).minimize(loss) correct_prediction = tf.equal(tf.cast(tf.argmax(logits,1),tf.int32), y_) acc = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))# 定义一个函数,按批次取数据 def minibatches(inputs=None, targets=None, batch_size=None, shuffle=False): assert len(inputs) == len(targets) if shuffle: indices = np.arange(len(inputs)) np.random.shuffle(indices) for start_idx in range(0, len(inputs) - batch_size + 1, batch_size): if shuffle: excerpt = indices[start_idx:start_idx + batch_size] else: excerpt = slice(start_idx, start_idx + batch_size) yield inputs[excerpt], targets[excerpt]# 训练和测试数据,可将n_epoch设置更大一些n_epoch = 10 batch_size = 64 saver = tf.train.Saver() sess = tf.Session() tf.summary.FileWriter("logs/", sess.graph)sess.run(tf.global_variables_initializer())loss_train = [] acc_train = [] loss_validation = [] acc_validation = []for epoch in range(n_epoch): start_time = time.time()# training train_loss, train_acc, n_batch = 0.0, 0.0, 0.0 for x_train_a, y_train_a in minibatches(x_train, y_train, batch_size, shuffle=True): _, err, ac = sess.run([train_op, loss, acc], feed_dict={x: x_train_a, y_: y_train_a}) # feed_dict中初始神经元的个数 train_loss += err; train_acc += ac; n_batch += 1loss_train.append(np.sum(train_loss)/ n_batch)# 正确理解 batch_size,n_batch以及n_epoch acc_train.append(np.sum(train_acc)/ n_batch)print("train loss: %f" % float(np.sum(train_loss)/ n_batch)) print("train acc: %f" % float(np.sum(train_acc)/ n_batch))# validation val_loss, val_acc, n_batch = 0.0, 0.0, 0.0 for x_val_a, y_val_a in minibatches(x_val, y_val, batch_size, shuffle=False): err, ac = sess.run([loss,acc], feed_dict={x: x_val_a, y_: y_val_a}) val_loss += err; val_acc += ac; n_batch += 1loss_validation.append(np.sum(val_loss)/ n_batch) acc_validation.append(np.sum(val_acc)/ n_batch)print("validation loss: %f" % float(np.sum(val_loss)/ n_batch)) print("validation acc: %f" % float(np.sum(val_acc)/ n_batch)) saver.save(sess,model_path) sess.close()# 绘制结果曲线 N = np.arange(0, n_epoch) plt.style.use("ggplot") plt.figure() plt.plot(N, acc_train, label="train_acc",color="red") plt.plot(N, acc_validation, label="val_acc", color="green") plt.title("Training and Validation Accuracy (Simple NN)") plt.xlabel("Epoch 10") plt.ylabel("Accuracy") plt.legend() plt.savefig('simple_nn_plot_acc.png')plt.figure() plt.plot(N, loss_train, label="train_loss", color="red") plt.plot(N, loss_validation, label="val_loss", color="green") plt.title("Training and Validation Loss (Simple NN)") plt.xlabel("Epoch #") plt.ylabel("Loss") plt.legend() plt.savefig('simple_nn_plot_loss.png')

【深度学习|基于传统神经网络,使用Keras训练自己的数据集】运行输出:
深度学习|基于传统神经网络,使用Keras训练自己的数据集
文章图片

深度学习|基于传统神经网络,使用Keras训练自己的数据集
文章图片

深度学习|基于传统神经网络,使用Keras训练自己的数据集
文章图片

深度学习|基于传统神经网络,使用Keras训练自己的数据集
文章图片

3.测试代码
from skimage import io,transform import tensorflow as tf import numpy as nppath2 = "flower_photos/daisy/5547758_eea9edfd54_n.jpg" path1 = "flower_photos/dandelion/7355522_b66e5d3078_m.jpg" path3 = "flower_photos/roses/394990940_7af082cf8d_n.jpg" path4 = "flower_photos/sunflowers/6953297_8576bf4ea3.jpg" path5 = "flower_photos/tulips/10791227_7168491604.jpg"flower_dict = {0:'dasiy',1:'dandelion',2:'roses',3:'sunflowers',4:'tulips'}w=100 h=100 c=3def read_one_image(path): img = io.imread(path) img = transform.resize(img,(w,h)) return np.asarray(img)with tf.Session() as sess: data = https://www.it610.com/article/[] data1 = read_one_image(path1) data2 = read_one_image(path2) data3 = read_one_image(path3) data4 = read_one_image(path4) data5 = read_one_image(path5) data.append(data1) data.append(data2) data.append(data3) data.append(data4) data.append(data5)saver = tf.train.import_meta_graph('C:/Users/zhf/Desktop/flower/model/model.ckpt.meta') saver.restore(sess, tf.train.latest_checkpoint('C:/Users/zhf/Desktop/flower/model/'))graph = tf.get_default_graph() x = graph.get_tensor_by_name("x:0") feed_dict = {x : data}logits = graph.get_tensor_by_name("logits_eval:0")classification_result = sess.run(logits, feed_dict)# 打印出预测矩阵 print(classification_result)# 打印出预测矩阵每一行最大值的索引,根据最大值的下标,对应花的类别 print(tf.argmax(classification_result, 1).eval())# 根据索引通过字典对应花的分类 output = tf.argmax(classification_result, 1).eval()for i in range(len(output)): print("第", i+1, "朵花预测:"+flower_dict[output[i]])

深度学习|基于传统神经网络,使用Keras训练自己的数据集
文章图片

使用Keras训练自己的数据集——以图像多分类为例(基于卷积神经网络)

    推荐阅读