model.py 建立模型
import tensorflow as tf
import numpy as np
def weight_variable(shape):
# 产生截断正态分布随机数
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
def bias_variable(shape):
# 误差初始值定义为0.1
return tf.Variable(tf.constant(0.1, shape=shape))
def MLP(_IMAGE_SIZE=28, _IMAGE_CHANNELS = 1,_HIDDEN1 = 128,_HIDDEN2 = 64,_NUM_CLASSES = 10):
image_pixels = _IMAGE_SIZE * _IMAGE_SIZE * _IMAGE_CHANNELS
with tf.name_scope('main_params'):
x = tf.placeholder(tf.float32, shape=[None, image_pixels], name='Input')
y = tf.placeholder(tf.float32, shape=[None, _NUM_CLASSES], name='Output')
with tf.variable_scope('hidden1'):
w_h = weight_variable([image_pixels, _HIDDEN1])
h = tf.nn.relu(tf.matmul(x, w_h)) # hidden layer1
with tf.variable_scope('hidden2'):
w_h2 = weight_variable([_HIDDEN1, _HIDDEN2])
h2 = tf.nn.relu(tf.matmul(h, w_h2)) # hidden layer2
with tf.variable_scope('out'):
w_out = weight_variable([_HIDDEN2, _NUM_CLASSES])
logit = tf.matmul(h2, w_out) # 输出层
return x, y, logit
def CNN(_IMAGE_SIZE = 32,_IMAGE_CHANNELS = 3,_NUM_CLASSES = 10):
with tf.name_scope('main_params'):
x = tf.placeholder(tf.float32, shape=[None, _IMAGE_SIZE * _IMAGE_SIZE * _IMAGE_CHANNELS], name='Input')
y = tf.placeholder(tf.float32, shape=[None, _NUM_CLASSES], name='Output')
x_image = tf.reshape(x, [-1, _IMAGE_SIZE, _IMAGE_SIZE, _IMAGE_CHANNELS], name='images')
with tf.variable_scope('conv1') as scope:
conv = tf.layers.conv2d(
inputs=x_image,
filters=32,
kernel_size=[3, 3],
padding='SAME',
activation=tf.nn.relu
)
pool = tf.layers.max_pooling2d(conv, pool_size=[2, 2], strides=2, padding='SAME')
with tf.variable_scope('conv2') as scope:
conv = tf.layers.conv2d(
inputs=pool,
filters=64,
kernel_size=[3, 3],
padding='SAME',
activation=tf.nn.relu
)
pool = tf.layers.max_pooling2d(conv, pool_size=[2, 2], strides=2, padding='SAME')
with tf.variable_scope('fully_connected') as scope:
flat = tf.layers.flatten(pool)
# flat = tf.reshape(pool, [-1, 8 * 8 * 64])
# flat = tf.reshape(pool, [-1, 7 * 7 * 64])
fc = tf.layers.dense(inputs=flat, units=1500, activation=tf.nn.relu)
drop = tf.layers.dropout(fc, rate=0.5)
logit = tf.layers.dense(inputs=drop, units=_NUM_CLASSES, name=scope.name)
return x, y, logit
def LeNet(_IMAGE_SIZE = 32,_IMAGE_CHANNELS = 3,_NUM_CLASSES = 10):
with tf.name_scope('main_params'):
x = tf.placeholder(tf.float32, shape=[None, _IMAGE_SIZE * _IMAGE_SIZE * _IMAGE_CHANNELS], name='Input')
y = tf.placeholder(tf.float32, shape=[None, _NUM_CLASSES], name='Output')
x_image = tf.reshape(x, [-1, _IMAGE_SIZE, _IMAGE_SIZE, _IMAGE_CHANNELS], name='images')
with tf.variable_scope('layer1-conv1'):
W_conv1 = weight_variable([5, 5, 1, 6])
b_conv1 = bias_variable([6])
conv1 = tf.nn.conv2d(x_image, W_conv1, strides=[1, 1, 1, 1], padding='VALID')
relu1 = tf.nn.relu(tf.nn.bias_add(conv1, b_conv1))
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'):
W_conv2 = weight_variable([5, 5, 6, 16])
b_conv2 = bias_variable([16])
conv2 = tf.nn.conv2d(pool1, W_conv2, strides=[1, 1, 1, 1], padding='VALID')
relu2 = tf.nn.relu(tf.nn.bias_add(conv2, b_conv2))
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-fc1'):
W_fc1 = weight_variable([400, 120])
b_fc1 = bias_variable([120])
flat = tf.layers.flatten(pool2)
# flat = tf.reshape(pool2, [-1, 5 * 5 * 16])
fc1 = tf.nn.relu(tf.matmul(flat, W_fc1) + b_fc1)
with tf.variable_scope('layer6-fc2'):
W_fc2 = weight_variable([120, 84])
b_fc2 = bias_variable([84])
fc2 = tf.matmul(fc1, W_fc2) + b_fc2
with tf.variable_scope('layer7-fc3'):
W_fc3 = weight_variable([84, 10])
b_fc3 = bias_variable([10])
logit = tf.matmul(fc2, W_fc3) + b_fc3
return x,y,logit
def loss(logit, labels):
with tf.name_scope('cross_entropy'):
# Operation to determine the cross entropy between logits and labels
loss = tf.reduce_mean(
tf.nn.softmax_cross_entropy_with_logits_v2(
logits=logit, labels=labels, name='cross_entropy'))
return loss
def training(loss, learning_rate):
global_step = tf.Variable(0, name='global_step', trainable=False)
# 优化器
optimizer = tf.train.AdamOptimizer(learning_rate, 0.9)
# 参数更新
train_op = optimizer.minimize(loss, global_step=global_step)
return train_op,global_step
def evaluation(logits, labels):
with tf.name_scope('Accuracy'):
# Operation comparing prediction with true label
correct_prediction = tf.equal(tf.argmax(logits,1), tf.argmax(labels,1))
# Operation calculating the accuracy of the predictions
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
# Summary operation for the accuracy
# tf.summary.scalar('train_accuracy', accuracy)
return accuracy
def lr(epoch):
learning_rate = 1e-3
if epoch > 80:
learning_rate *= 0.5e-3
elif epoch > 60:
learning_rate *= 1e-3
elif epoch > 40:
learning_rate *= 1e-2
elif epoch > 20:
learning_rate *= 1e-1
return learning_rate
train.py 训练过程
# import os
# os.environ["CUDA_VISIBLE_DEVICES"] = "-1" # 这一行注释掉就是使用gpu,不注释就是使用cpu
import numpy as np
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import model
from model import LeNet, MLP, CNN, lr
from data import get_data_set
train_x, train_y = get_data_set("train")
test_x, test_y = get_data_set("test")
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) # 读取MNIST手写数字识别数据集
epoch = 2
batch_size = 64
def train():
# tensorflow变量占位符,在执行运算时才传入数据
# X, Y, logit = MLP(_IMAGE_SIZE=32, _IMAGE_CHANNELS = 3)
X, Y, logit = CNN(_IMAGE_SIZE=28, _IMAGE_CHANNELS = 1)
# X, Y, logit = LeNet(_IMAGE_SIZE=32, _IMAGE_CHANNELS = 3)
# 计算损失
loss = model.loss(logit, Y)
# softmx = tf.argmax(tf.nn.softmax(logit),1)
# 计算准确率
accuracy = model.evaluation(logit, Y)
# 优化器
train_op,global_step = model.training(loss, learning_rate=0.001)
saver = tf.train.Saver()
with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as sess:
sess.run(tf.global_variables_initializer()) # 初始化各种变量
for i in range(epoch):
# num = len(train_x) // batch_size
num = mnist.train.num_examples // batch_size
for step in range(num):
# batch_x = train_x[step * batch_size: (step + 1) * batch_size]
# batch_y = train_y[step * batch_size: (step + 1) * batch_size]
batch_x, batch_y = mnist.train.next_batch(batch_size) # 读取相应一批的图片和标签数量
_,i_global,batch_loss, batch_acc = sess.run([train_op,global_step,loss, accuracy], feed_dict={X: batch_x, Y: batch_y}) # 通过神经网络训练
if step % 128 == 0: # 输出训练记录
msg = "Global step: {:>5} - acc: {:.4f} - loss: {:.4f}"
print(msg.format(i_global, batch_acc, batch_loss))
print("Epoch: {}".format(i), "Testing Accuracy: {:0.5f}".format(sess.run(accuracy,feed_dict={X: mnist.test.images, Y: mnist.test.labels})))
# i = 0
# predicted_class = []
# while i < len(test_x):
# j = min(i + batch_size, len(test_x))
# batch_xs = test_x[i:j, :]
# batch_ys = test_y[i:j, :]
# predicted_class.append(sess.run(accuracy, feed_dict={X: batch_xs, Y: batch_ys}))
# i = j
# acc = np.mean(predicted_class)
# print("Accuracy on Test-Set: {0:.2f})".format(acc))
saver.save(sess, './models/model.ckpt')
train()
load.py 加载预测
import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow.examples.tutorials.mnist import input_data
import numpy as np
from model import MLP, CNN
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
def display_compare(num):
x_train = mnist.test.images[num, :].reshape(1, 784)
y_train = mnist.test.labels[num, :]
np.set_printoptions(precision=1)
np.set_printoptions(suppress=True)
label = y_train.argmax()
result = sess.run(logit, feed_dict={X: x_train})
prediction = result.argmax()
plt.title('Prediction: %d Label: %s' % (prediction, label))
plt.imshow(x_train.reshape([28, 28]), cmap=plt.get_cmap('gray_r'))
plt.show()
module_file = tf.train.latest_checkpoint('./models/')
if __name__ == '__main__':
with tf.Session() as sess:
X, Y, logit = CNN(_IMAGE_SIZE=28, _IMAGE_CHANNELS = 1)
saver = tf.train.Saver()
saver.restore(sess, module_file)
while True:
n = int(input('请输入想要查看的图像'))
display_compare(n)
data.py 加载cifar10数据集
import pickle
import numpy as np
import os
from urllib.request import urlretrieve
import tarfile
import zipfile
import sys
def get_data_set(name="train"):
x = None
y = None
# maybe_download_and_extract()
folder_name = "./cifar_10"
# f = open('./data_set/'+folder_name+'/batches.meta', 'rb')
# f.close()
if name is "train":
for i in range(5):
f = open(folder_name+'/data_batch_' + str(i + 1), 'rb')
datadict = pickle.load(f, encoding='latin1')
f.close()
_X = datadict["data"]
_Y = datadict['labels']
_X = np.array(_X, dtype=float) / 255.0
_X = _X.reshape([-1, 3, 32, 32])
_X = _X.transpose([0, 2, 3, 1])
_X = _X.reshape(-1, 32*32*3)
if x is None:
x = _X
y = _Y
else:
x = np.concatenate((x, _X), axis=0)
y = np.concatenate((y, _Y), axis=0)
elif name is "test":
f = open(folder_name+'/test_batch', 'rb')
datadict = pickle.load(f, encoding='latin1')
f.close()
x = datadict["data"]
y = np.array(datadict['labels'])
x = np.array(x, dtype=float) / 255.0
x = x.reshape([-1, 3, 32, 32])
x = x.transpose([0, 2, 3, 1])
x = x.reshape(-1, 32*32*3)
return x, dense_to_one_hot(y)
def dense_to_one_hot(labels_dense, num_classes=10):
num_labels = labels_dense.shape[0]
index_offset = np.arange(num_labels) * num_classes
labels_one_hot = np.zeros((num_labels, num_classes))
labels_one_hot.flat[index_offset + labels_dense.ravel()] = 1
return labels_one_hot
#
# def _print_download_progress(count, block_size, total_size):
# pct_complete = float(count * block_size) / total_size
# msg = "\r- Download progress: {0:.1%}".format(pct_complete)
# sys.stdout.write(msg)
# sys.stdout.flush()
#
#
# def maybe_download_and_extract():
# main_directory = "./data_set/"
# cifar_10_directory = main_directory+"cifar_10/"
# if not os.path.exists(main_directory):
# os.makedirs(main_directory)
#
# url = "http://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz"
# filename = url.split('/')[-1]
# file_path = os.path.join(main_directory, filename)
# zip_cifar_10 = file_path
# file_path, _ = urlretrieve(url=url, filename=file_path, reporthook=_print_download_progress)
#
# print()
# print("Download finished. Extracting files.")
# if file_path.endswith(".zip"):
# zipfile.ZipFile(file=file_path, mode="r").extractall(main_directory)
# elif file_path.endswith((".tar.gz", ".tgz")):
# tarfile.open(name=file_path, mode="r:gz").extractall(main_directory)
# print("Done.")
#
# os.rename(main_directory+"./cifar-10-batches-py", cifar_10_directory)
# os.remove(zip_cifar_10)
链接:https://pan.baidu.com/s/1eGk_NXuvQK-5-LeYP6Bzyg 提取码:vqog
复制这段内容后打开百度网盘手机App,操作更方便哦