Waiting~G

一起走过的日子

Archive for 一月, 2018

Tensorflow-cnn卷积神经网络

without comments

卷积神经网络 CNN (Convolutional Neural Network)

一、CNN在图片上的简单运用

 

比较流行的一种搭建结构是这样, 从下到上的顺序, 首先是输入的图片(image), 经过一层卷积层 (convolution), 然后在用池化(pooling)方式处理卷积的信息, 这里使用的是 max pooling 的方式. 然后在经过一次同样的处理, 把得到的第二次处理的信息传入两层全连接的神经层 (fully connected),这也是一般的两层神经网络层,最后在接上一个分类器(classifier)进行分类预测

二、CNN在图片上的简单运用


# -*- coding: utf-8 -*-
“””
————————————————-
File Name:
     cnn
Author :       skymaxu
date:
          2017/12/6

————————————————-
“””
__author__ = ‘skymaxu’

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

mnist = input_data.read_data_sets(‘MNIST_data’, one_hot=True)

def add_layer(inputs, in_size, out_size, active_function=None):
Weight = tf.Variable(tf.random_normal([in_size, out_size]))
biasis = tf.Variable(tf.zeros([1, out_size]) + 0.1)
Wx_plus_b = tf.matmul(inputs, Weight) + biasis
if active_function is None:
outputs = Wx_plus_b
else:
outputs = active_function(Wx_plus_b)
return outputs

def compute_accuracy(v_xs, v_ys):
global predcition
y_pre = sess.run(predcition, feed_dict={xs: v_xs, keep_prob: 1})
correct_prediction = tf.equal(tf.argmax(y_pre, 1), tf.argmax(v_ys, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
result = sess.run(accuracy, feed_dict={xs: v_ys, ys: v_ys, keep_prob: 1})
return result

def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.1)
# inital=tf.random_normal()
return tf.Variable(initial)

def bia_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)

def conv2d(x, W):
# stride=[1,x_movement,y_movement,1]
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding=‘SAME’)

def max_pool_2x2(x):
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding=‘SAME’)

def compute_accuracy1(v_xs, v_ys):
global prediction
y_pre = sess.run(prediction, feed_dict={xs: v_xs})
# tf.argmax(vector, 1):返回的是vector中的最大值的索引号,
# 如果vector是一个向量,那就返回一个值,如果是一个矩阵,那就返回一个向量,
# 这个向量的每一个维度都是相对应矩阵行的最大值元素的索引号。

# cast(x, dtype, name=None)
# 将
x的数据格式转化成dtype.例如,原来x的数据格式是bool,
# 那么将其转化成float以后,就能够将其转化成0和1的序列。反之也可以

# tf.equal(A, B)
# 是对比这两个矩阵或者向量的相等的元素,如果是相等的那就返回
True,反正返回False,返回的值的矩阵维度和A是一样的
correct_prediction = tf.equal(tf.argmax(y_pre, 1), tf.argmax(v_ys, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
result = sess.run(accuracy, feed_dict={xs: v_xs, ys: v_ys})
return result

# define placeholder for inputs to network
keep_prob = tf.placeholder(tf.float32)
xs = tf.placeholder(tf.float32, [None, 784])
ys = tf.placeholder(tf.float32, [None, 10])
# print(x_image.shape) [n_samples,28,28,1]
x_image = tf.reshape(xs, [-1, 28, 28, 1])

# add output layer
# conv1 layer
# patch/kernel 5*5 in_size=1,out_size=32
W_conv1 = weight_variable([5, 5, 1, 32])
b_conv1 = bia_variable([32])
# output size 28*28*32
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
# output size 14*14*32
h_pool1 = max_pool_2x2(h_conv1)

# conv2 layer
# patch/kernel 5*5 in_size=32,out_size=64
W_conv2 = weight_variable([5, 5, 32, 64])
b_conv2 = bia_variable([64])
# output size 14*14*64
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
# output size 7*7*32
h_pool2 = max_pool_2x2(h_conv2)

# func1 layer
# in_size=7 * 7 * 64,out_size=1024
W_fc1 = weight_variable([7 * 7 * 64, 1024])
b_cf1 = bia_variable([1024])
h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1 + b_cf1))
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
# func2 layer
# in_size=1024,out_size=10
W_fc2 = weight_variable([1024, 10])
b_cf2 = bia_variable([10])
predcition = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2 + b_cf2))

# the error between prediction and real data
# oss函数(即最优化目标函数)选用交叉熵函数。交叉熵用来衡量预测值和真实值的相似程度,如果完全相同,它们的交叉熵等于零
cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys * tf.log(predcition), reduction_indices=[1]))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)

sess = tf.Session()
sess.run(tf.global_variables_initializer())

for i in range(1000):
batch_xs, batch_ys = mnist.train.next_batch(100)
sess.run(train_step, feed_dict={xs: batch_xs, ys: batch_ys, keep_prob: 0.5})
if i % 50 == 0:
print(compute_accuracy(mnist.test.images, mnist.test.labels))

*Ps:机器性能不好的,别乱运行,小心挂掉

 

三、Saver 保存读取

我们搭建好了一个神经网络, 训练好了, 肯定也想保存起来, 用于再次加载.用 Tensorflow 中的 saver 保存和加载.

PS:当前tensorflow仅仅能保存变量,并不能保存模型;

 

保存:

# -*- coding: utf-8 -*-
“””
————————————————-
File Name:
     saver
Author :       skymaxu
date:
          2017/12/13

————————————————-
“””
__author__ = ‘skymaxu’

import tensorflow as tf
import numpy as np

# save to file
W = tf.Variable([[1, 2, 3], [3, 4, 5]], dtype=tf.float32, name=‘Weight’)
b = tf.Variable([[1, 2, 3]], dtype=tf.float32, name=‘biasis’)

init = tf.global_variables_initializer()
saver = tf.train.Saver()

with tf.Session() as sess:
sess.run(init)
save_path = saver.save(sess, ‘model/save_net.ckpt’)
print(‘Save to path:’, save_path)

 

提取:

# -*- coding: utf-8 -*-
“””
————————————————-
File Name:
     saver
Author :       skymaxu
date:
          2017/12/13

————————————————-
“””
__author__ = ‘skymaxu’

import tensorflow as tf
import numpy as np


#restore
W = tf.Variable(np.arange(6).reshape((2, 3)), dtype=tf.float32, name=‘weight’)
b = tf.Variable(np.arange(3).reshape((1, 3)), dtype=tf.float32, name=‘biasis’)

saver = tf.train.Saver()
with tf.Session()  as sess:
saver.restore(sess, ‘model/save_net.ckpt’)
print(‘weight:’, sess.run(W))
print(‘biasis:’, sess.run(b))

 

Written by Skyma

一月 1st, 2018 at 1:36 下午

Posted in 学.机器学习