|
| 1 | +#!/usr/bin/python3.5 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +import sys |
| 5 | +import os |
| 6 | +import time |
| 7 | +import random |
| 8 | + |
| 9 | +import numpy as np |
| 10 | +import tensorflow as tf |
| 11 | +import cv2 |
| 12 | +from PIL import Image |
| 13 | + |
| 14 | + |
| 15 | +SIZE = 784 |
| 16 | +WIDTH = 28 |
| 17 | +HEIGHT = 28 |
| 18 | +NUM_CLASSES = 34 |
| 19 | +iterations = 100 |
| 20 | + |
| 21 | +SAVER_DIR = "python/train-saver/digits/" |
| 22 | + |
| 23 | +LETTERS_DIGITS = ("0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","G","H","J","K","L","M","N","P","Q","R","S","T","U","V","W","X","Y","Z") |
| 24 | +license_num = "" |
| 25 | +time_begin = time.time() |
| 26 | +# 定义输入节点,对应于图片像素值矩阵集合和图片标签(即所代表的数字) |
| 27 | +x = tf.placeholder(tf.float32, shape=[None, SIZE]) |
| 28 | +y_ = tf.placeholder(tf.float32, shape=[None, NUM_CLASSES]) |
| 29 | +x_image = tf.reshape(x, [-1, WIDTH, HEIGHT, 1]) |
| 30 | +# 定义卷积函数 |
| 31 | +def conv_layer(inputs, W, b, conv_strides, kernel_size, pool_strides, padding): |
| 32 | + L1_conv = tf.nn.conv2d(inputs, W, strides=conv_strides, padding=padding) |
| 33 | + L1_relu = tf.nn.relu(L1_conv + b) |
| 34 | + return tf.nn.max_pool(L1_relu, ksize=kernel_size, strides=pool_strides, padding='SAME') |
| 35 | + # 定义全连接层函数 |
| 36 | +def full_connect(inputs, W, b): |
| 37 | + return tf.nn.relu(tf.matmul(inputs, W) + b) |
| 38 | +if __name__ =='__main__': |
| 39 | + # 第一次遍历图片目录是为了获取图片总数 |
| 40 | + input_count = 0 |
| 41 | + for i in range(0,NUM_CLASSES): |
| 42 | + dir = 'python/train_images/training-set/%s/' % i |
| 43 | + for rt, dirs, files in os.walk(dir): |
| 44 | + for filename in files: |
| 45 | + input_count += 1 |
| 46 | + |
| 47 | + # 定义对应维数和各维长度的数组 |
| 48 | + input_images = np.array([[0]*SIZE for i in range(input_count)]) |
| 49 | + input_labels = np.array([[0]*NUM_CLASSES for i in range(input_count)]) |
| 50 | + |
| 51 | + # 第二次遍历图片目录是为了生成图片数据和标签 |
| 52 | + index = 0 |
| 53 | + for i in range(0,NUM_CLASSES): |
| 54 | + dir = 'python/train_images/training-set/%s/' % i |
| 55 | + for rt, dirs, files in os.walk(dir): |
| 56 | + for filename in files: |
| 57 | + filename = dir + filename |
| 58 | + img = Image.open(filename) |
| 59 | + img = img.convert('1') |
| 60 | + width = img.size[0] |
| 61 | + height = img.size[1] |
| 62 | + for h in range(0, height): |
| 63 | + for w in range(0, width): |
| 64 | + # 通过这样的处理,使数字的线条变细,有利于提高识别准确率 |
| 65 | + if img.getpixel((w, h)) > 230: |
| 66 | + input_images[index][w+h*width] = 0 |
| 67 | + else: |
| 68 | + input_images[index][w+h*width] = 1 |
| 69 | + input_labels[index][i] = 1 |
| 70 | + index += 1 |
| 71 | + # 第一次遍历图片目录是为了获取图片总数 |
| 72 | + val_count = 0 |
| 73 | + for i in range(0,NUM_CLASSES): |
| 74 | + dir = 'python/train_images/validation-set/%s/' % i |
| 75 | + for rt, dirs, files in os.walk(dir): |
| 76 | + for filename in files: |
| 77 | + val_count += 1 |
| 78 | + |
| 79 | + # 定义对应维数和各维长度的数组 |
| 80 | + val_images = np.array([[0]*SIZE for i in range(val_count)]) |
| 81 | + val_labels = np.array([[0]*NUM_CLASSES for i in range(val_count)]) |
| 82 | + |
| 83 | + # 第二次遍历图片目录是为了生成图片数据和标签 |
| 84 | + index = 0 |
| 85 | + for i in range(0,NUM_CLASSES): |
| 86 | + dir = 'python/train_images/validation-set/%s/' % i |
| 87 | + for rt, dirs, files in os.walk(dir): |
| 88 | + for filename in files: |
| 89 | + filename = dir + filename |
| 90 | + img = Image.open(filename) |
| 91 | + img = img.convert('1') |
| 92 | + width = img.size[0] |
| 93 | + height = img.size[1] |
| 94 | + for h in range(0, height): |
| 95 | + for w in range(0, width): |
| 96 | + # 通过这样的处理,使数字的线条变细,有利于提高识别准确率 |
| 97 | + if img.getpixel((w, h)) > 230: |
| 98 | + val_images[index][w+h*width] = 0 |
| 99 | + else: |
| 100 | + val_images[index][w+h*width] = 1 |
| 101 | + val_labels[index][i] = 1 |
| 102 | + index += 1 |
| 103 | + |
| 104 | + with tf.Session() as sess: |
| 105 | + # 第一个卷积层 |
| 106 | + W_conv1 = tf.Variable(tf.truncated_normal([5, 5, 1, 32], stddev=0.1), name="W_conv1") |
| 107 | + b_conv1 = tf.Variable(tf.constant(0.1, shape=[32]), name="b_conv1") |
| 108 | + conv_strides = [1, 1, 1, 1] |
| 109 | + kernel_size = [1, 2, 2, 1] |
| 110 | + pool_strides = [1, 2, 2, 1] |
| 111 | + L1_pool = conv_layer(x_image, W_conv1, b_conv1, conv_strides, kernel_size, pool_strides, padding='SAME') |
| 112 | + # 第二个卷积层 |
| 113 | + W_conv2 = tf.Variable(tf.truncated_normal([5, 5, 32, 64], stddev=0.1), name="W_conv2") |
| 114 | + b_conv2 = tf.Variable(tf.constant(0.1, shape=[64]), name="b_conv2") |
| 115 | + conv_strides = [1, 1, 1, 1] |
| 116 | + kernel_size = [1, 2, 2, 1] |
| 117 | + pool_strides = [1, 2, 2, 1] |
| 118 | + L2_pool = conv_layer(L1_pool, W_conv2, b_conv2, conv_strides, kernel_size, pool_strides, padding='SAME') |
| 119 | + # 全连接层 |
| 120 | + W_fc1 = tf.Variable(tf.truncated_normal([7 * 7 * 64, 1024], stddev=0.1), name="W_fc1") |
| 121 | + b_fc1 = tf.Variable(tf.constant(0.1, shape=[1024]), name="b_fc1") |
| 122 | + h_pool2_flat = tf.reshape(L2_pool, [-1, 7 * 7 * 64]) |
| 123 | + h_fc1 = full_connect(h_pool2_flat, W_fc1, b_fc1) |
| 124 | + # dropout |
| 125 | + keep_prob = tf.placeholder(tf.float32) |
| 126 | + h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) |
| 127 | + # readout层 |
| 128 | + W_fc2 = tf.Variable(tf.truncated_normal([1024, NUM_CLASSES], stddev=0.1), name="W_fc2") |
| 129 | + b_fc2 = tf.Variable(tf.constant(0.1, shape=[NUM_CLASSES]), name="b_fc2") |
| 130 | + |
| 131 | + # 定义优化器和训练op |
| 132 | + y_conv = tf.matmul(h_fc1_drop, W_fc2) + b_fc2 |
| 133 | + cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y_conv)) |
| 134 | + train_step = tf.train.AdamOptimizer((1e-4)).minimize(cross_entropy) |
| 135 | + correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1)) |
| 136 | + accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) |
| 137 | + sess.run(tf.global_variables_initializer()) |
| 138 | + time_elapsed = time.time() - time_begin |
| 139 | + print("读取图片文件耗费时间:%d秒" % time_elapsed) |
| 140 | + time_begin = time.time() |
| 141 | + print ("一共读取了 %s 个训练图像, %s 个标签" % (input_count, input_count)) |
| 142 | + # 设置每次训练op的输入个数和迭代次数,这里为了支持任意图片总数,定义了一个余数remainder,譬如,如果每次训练op的输入个数为60,图片总数为150张,则前面两次各输入60张,最后一次输入30张(余数30) |
| 143 | + batch_size = 60 |
| 144 | + iterations = iterations |
| 145 | + batches_count = int(input_count / batch_size) |
| 146 | + remainder = input_count % batch_size |
| 147 | + print ("训练数据集分成 %s 批, 前面每批 %s 个数据,最后一批 %s 个数据" % (batches_count+1, batch_size, remainder)) |
| 148 | + |
| 149 | + # 执行训练迭代 |
| 150 | + for it in range(iterations): |
| 151 | + # 这里的关键是要把输入数组转为np.array |
| 152 | + for n in range(batches_count): |
| 153 | + train_step.run(feed_dict={x: input_images[n*batch_size:(n+1)*batch_size], y_: input_labels[n*batch_size:(n+1)*batch_size], keep_prob: 0.5}) |
| 154 | + if remainder > 0: |
| 155 | + start_index = batches_count * batch_size; |
| 156 | + train_step.run(feed_dict={x: input_images[start_index:input_count-1], y_: input_labels[start_index:input_count-1], keep_prob: 0.5}) |
| 157 | + |
| 158 | + # 每完成五次迭代,判断准确度是否已达到100%,达到则退出迭代循环 |
| 159 | + iterate_accuracy = 0 |
| 160 | + if it%1 == 0: |
| 161 | + iterate_accuracy = accuracy.eval(feed_dict={x: val_images, y_: val_labels, keep_prob: 1.0}) |
| 162 | + print ('第 %d 次训练迭代: 准确率 %0.5f%%' % (it, iterate_accuracy*100)) |
| 163 | + if iterate_accuracy >= 0.9999 and it >= iterations: |
| 164 | + break |
| 165 | + |
| 166 | + print ('完成训练!') |
| 167 | + time_elapsed = time.time() - time_begin |
| 168 | + print ("训练耗费时间:%d秒" % time_elapsed) |
| 169 | + time_begin = time.time() |
| 170 | + |
| 171 | + # 保存训练结果 |
| 172 | + if not os.path.exists(SAVER_DIR): |
| 173 | + print ('不存在训练数据保存目录,现在创建保存目录') |
| 174 | + os.makedirs(SAVER_DIR) |
| 175 | + # 初始化saver |
| 176 | + saver = tf.train.Saver() |
| 177 | + saver_path = saver.save(sess, "%smodel.ckpt"%(SAVER_DIR)) |
0 commit comments