基于CNN的图片识别

 1 # 导入工具包
  2 import math
  3 import time
  4 import numpy as np
  5 import h5py
  6 import matplotlib.pyplot as plt
  7 from scipy import ndimage
  8 from PIL import Image
  9 import tensorflow as tf
 10 from tensorflow.python.framework import ops 
 11 
 12 %matplotlib inline 
 13 %pylab
 14 np.random.seed(1)
 15 conv_layers = {}
 16 
 17 #独热编码
 18 def convert_to_one_hot(Y,C):
 19     """
 20     Y:需要编码的输入
 21     C:编码位数
 22     """
 23     Y = np.eye(C)[Y.reshape(-1)].T
 24     return Y
 25 
 26 #图片显示
 27 def show_image(X,Y,index):
 28     plt.imshow(X[index]) 
 29     print("y = "+str(np.squeeze(Y[:,index]))) #squeeze 压缩成数字
 30     plt.show()
 31 
 32 #MINI-BATCH函数
 33 def random_mini_batch(X,Y,mini_batch_size=64,seed=0):
 34     """
 35     X:输入数据,(m,Hi,Wi,Ci)
 36     Y:直值(m.n_y)
 37     mini_batch_size:mini_batch的数量,整数
 38     return:
 39         mini_batches:(mini_batch_X,mini_batch_Y)
 40     """
 41     m = X.shape[0] #训练数据的数量
 42     mini_batches = []
 43     np.random.seed(seed)
 44     
 45     nums = m//mini_batch_size
 46     
 47     #将(X,Y)洗牌 _permulation 用来打乱
 48     permutation = list(np.random.permutation(m))
 49     Shuffled_X = X[permutation,:,:,:]
 50     Shuffled_Y = Y[permutation,:]
 51     
 52     for k in range(0,nums):
 53         mini_batch_X = Shuffled_X[k*mini_batch_size:(k+1)*mini_batch_size,:,:,:]
 54         mini_batch_Y = Shuffled_Y[k*mini_batch_size:(k+1)*mini_batch_size,:]
 55         mini_batch = (mini_batch_X,mini_batch_Y)
 56         mini_batches.append(mini_batch)
 57     
 58     if m%mini_batch_size != 0 :
 59         mini_batch_X = Shuffled_X[nums*mini_batch_size:m,:,:,:]
 60         mini_batch_Y = Shuffled_Y[nums*mini_batch_size:m,:]
 61         mini_batch = (mini_batch_X,mini_batch_Y)
 62         mini_batches.append(mini_batch)
 63         
 64     return mini_batches
 65 
 66 #数据导入
 67 def load_dataset():
 68     #训练集
 69     train_dataset = h5py.File('datasets/train_signs.h5',"r") #html 5
 70     train_set_x_orig = np.array(train_dataset["train_set_x"][:])
 71     train_set_y_orig = np.array(train_dataset["train_set_y"][:])
 72     #测试集
 73     test_dataset = h5py.File('datasets/test_signs.h5','r') 
 74     test_set_x_orig = np.array(test_dataset["test_set_x"][:])
 75     test_set_y_orig = np.array(test_dataset["test_set_y"][:])
 76     #类别
 77     classes = np.array(test_dataset["list_classes"][:])
 78     #打印维度
 79     print("Original Training datasets' dimensions::X:{0},Y:{1}".format(train_set_x_orig.shape,
 80                                            train_set_y_orig.shape))
 81     print("Original Testing datasets' dimensions:X:{0},Y:{1}".format(test_set_x_orig.shape,
 82                                            test_set_y_orig.shape))    
 83     #将y转换成列向量
 84     train_set_y_orig = train_set_y_orig.reshape((1,train_set_y_orig.shape[0]))
 85     test_set_y_orig = test_set_y_orig.reshape((1,test_set_y_orig.shape[0]))
 86     Y_train = train_set_y_orig
 87     Y_test = test_set_y_orig
 88     #归一化
 89     X_train = train_set_x_orig/255
 90     X_test = test_set_x_orig/255
 91     #将Y进行独热编码 0-100000 1-010000...并且转换为横向量
 92     Y_train_1hot = convert_to_one_hot(Y_train,6).T
 93     Y_test_1hot = convert_to_one_hot(Y_test,6).T
 94     #打印其余数据维度
 95     print ("number of training examples = " + str(X_train.shape[0]))
 96     print ("number of test examples = " + str(X_test.shape[0]))
 97     print ("X_train shape: " + str(X_train.shape))
 98     print ("Y_train shape: " + str(Y_train_1hot.shape))
 99     print ("X_test shape: " + str(X_test.shape))
100     print ("Y_test shape: " + str(Y_test_1hot.shape))
101     
102     return train_set_x_orig,test_set_x_orig,Y_train,Y_test,\
103            X_train,X_test,Y_train_1hot,Y_test_1hot,classes
104 
105 #编写占位符函数
106 def create_placeholders(n_H0,n_W0,n_C0,n_y):
107     """
108     n_H0:标量,图片高 height
109     n_W0:标量,图片宽 weight
110     n_c0:标量,图片输入的通道数 channels
111     n_y:标量,分类数
112     """
113     X = tf.placeholder(tf.float32,shape=(None,n_H0,n_W0,n_C0))
114     Y = tf.placeholder(tf.float32,shape=(None,n_y))
115     return X,Y
116 
117 #参数初始化函数
118 def initialize_parameters():
119     """
120     初始化权重参数W1,W2
121     只需要初始化conv2d函数的权重(filters)
122     对于FC层,Tensorflow会自动初始化
123     """
124     tf.set_random_seed(1)
125     W1 = tf.get_variable("W1",[4,4,3,8],
126                         initializer=tf.contrib.layers.xavier_initializer(seed=0))
127     W2 = tf.get_variable("W2",[2,2,8,16],
128                         initializer=tf.contrib.layers.xavier_initializer(seed=0))
129     parameters={"W1":W1,
130                 "W2":W2}
131     return parameters
132 
133 #前向传播
134 def forward_propagation(X,parameters):
135     #取回参数
136     W1 = parameters['W1']
137     W2 = parameters['W2']
138     #CONV2D
139     Z1 = tf.nn.conv2d(X,W1,strides=[1,1,1,1],padding='SAME')
140     #RELU
141     A1 = tf.nn.relu(Z1)
142     #MAXPOOL:WINDOW_8X8; STRIDE_8; PADDING_SAME;
143     P1 = tf.nn.max_pool(A1,ksize=[1,8,8,1],strides=[1,8,8,1],padding='SAME')
144     #CONV2D
145     Z2 = tf.nn.conv2d(P1,W2,strides = [1,1,1,1],padding='SAME')
146     #RELU
147     A2 = tf.nn.relu(Z2)
148     #MAXPOOL
149     P2 = tf.nn.max_pool(A2,ksize=[1,4,4,1],strides=[1,4,4,1],padding='SAME')
150     #FALTTEN
151     P2 = tf.contrib.layers.flatten(P2)
152     #FC全连接层,暂时不用进行非线性激活,不调用softmax,最后连接到6个神经元做 softmax
153     Z3 = tf.contrib.layers.fully_connected(P2,num_outputs=6,activation_fn=None)
154     return Z3
155 
156 #计算损失
157 def compute_cost(Z3,Y):
158     cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=Z3,
159                                 labels=Y))
160     return cost
161 
162 #模型
163 def model(X_train,Y_train,X_test,Y_test,learning_rate=0.009,num_epochs=100,
164              mini_batch_size = 64,print_cost = True):
165     #可以不需要重新写入来重复运行这个模型
166     ops.reset_default_graph()
167     tf.set_random_seed(1)
168     seed = 3
169     (m,n_H0,n_W0,n_C0) = X_train.shape
170     n_y = Y_train.shape[1]
171     costs = [] #用于绘图
172     #建立占位符
173     X,Y = create_placeholders(n_H0,n_W0,n_C0,n_y)
174     #初始化参数
175     parameters = initialize_parameters()
176     #前向传播
177     Z3 = forward_propagation(X,parameters)
178     #计算损失
179     cost = compute_cost(Z3,Y)
180     #反向传播,定义tensorflow优化器,使用Adam优化器来最小化cost
181     optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
182     #初始化全局变量
183     init = tf.global_variables_initializer()
184     #开始进行tensorflow计算
185     with tf.Session() as sess:
186         #初始化
187         sess.run(init)
188         #循环训练 
189         for epoch in range(num_epochs):
190             minibatch_cost = 0
191 #             seed = seed+1
192             nums = m//mini_batch_size
193             minibatches=random_mini_batch(X_train,Y_train,mini_batch_size,seed)
194             for minibatch in minibatches:
195                 #选择一组minibatch
196                 (minibatch_X,minibatch_Y) = minibatch
197                 _,temp_cost = sess.run([optimizer,cost],
198                                     feed_dict = {X:minibatch_X,Y:minibatch_Y})
199                 minibatch_cost += temp_cost/nums
200             #打印损失
201             if print_cost == True and epoch%5 == 0:
202                 print("第{0}次迭代,当前cost:{1}".format(epoch,minibatch_cost))
203             if print_cost == True and epoch%1 == 0:
204                 costs.append(minibatch_cost)
205         #绘出损失图
206         plt.plot(np.squeeze(costs))
207         plt.plot(np.squeeze(costs))
208         plt.grid(True)
209         plt.ylabel('cost')
210         plt.xlabel('iterations (per tens)')
211         plt.title("Learning rate =" + str(learning_rate))
212         plt.show()
213         #计算正确预测
214             #返回最大的索引
215         predict_op = tf.argmax(Z3,1)
216             #判断是否相同 返回布尔值
217         correct_prediction = tf.equal(predict_op,tf.argmax(Y,1)) 
218 
219         #计算测试集精度 #转换为数字了 tf.cast
220         accuracy = tf.reduce_mean(tf.cast(correct_prediction,"float"))
221         print(accuracy)
222     
223         train_accuracy = accuracy.eval({X:X_train,Y:Y_train})
224         test_accuracy = accuracy.eval({X:X_test,Y:Y_test})
225         print("Train Accuracy:", train_accuracy)
226         print("Test Accuracy:", test_accuracy)
227 
228     return train_accuracy, test_accuracy, parameters
229     
230 #数据导入:
231 X_orig_train,X_orig_test,Y_orig_train,Y_orig_test,\
232     X_train,X_test,Y_train,Y_test,classes = load_dataset()
233 
234 #运行模型
235 _,_,parameters = model(X_train,Y_train,X_test,Y_test)

测试代码:

 1 #测试代码
 2 #显示其中一张图片
 3 show_image(X_train,Y_orig_train,0)
 4 #占位符测试
 5 X,Y = create_placeholder(100,100,3,6)
 6 print(str(X))
 7 print(str(Y))
 8 #filters初始化测试
 9 tf.reset_default_graph()
10 with tf.Session() as sess_test:
11     parameters = initialize_parameters()
12     init = tf.global_variables_initializer()
13     sess_test.run(init)
14     print("W1="+str(parameters["W1"].eval().shape))
15     print("W2="+str(parameters["W2"].eval().shape))
16 #前向传播测试
17 tf.reset_default_graph()
18 with tf.Session() as sess:
19     np.random.seed(1)
20     X,Y = create_placeholders(64,64,3,6)
21     parameters = initialize_parameters()
22     Z3 = forward_propagation(X,parameters)
23     init = tf.global_variables_initializer()
24     sess.run(init)
25     a = sess.run(Z3,{X:np.random.randn(4,64,64,3),Y:np.random.randn(4,6)})
26     print("Z3="+str(a))
27 #损失值测试
28 tf.reset_default_graph()
29 with tf.Session() as sess:
30     np.random.seed(1)
31     X, Y = create_placeholders(64, 64, 3, 6)
32     parameters = initialize_parameters()
33     Z3 = forward_propagation(X, parameters)
34     cost = compute_cost(Z3, Y)
35     init = tf.global_variables_initializer()
36     sess.run(init)
37     a = sess.run(cost, {X: np.random.randn(4,64,64,3), Y: np.random.randn(4,6)})
38     print("cost = " + str(a))
39 #mini_batches测试
40 X = np.random.randn(660,10,10,3)
41 Y = np.random.randn(660,6)
42 mini_batches = random_mini_batch(X,Y,mini_batch_size=5,seed=0)
43             # print(mini_batches[1:10])

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

春哥一号

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值