数据集链接:https://download.csdn.net/download/fanzonghao/10551018

提供数据集代码放在cnn_utils.py里。

import math
import numpy as np
import h5py
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.python.framework import opsdef load_dataset():train_dataset = h5py.File('datasets/train_signs.h5', "r")train_set_x_orig = np.array(train_dataset["train_set_x"][:]) # your train set featurestrain_set_y_orig = np.array(train_dataset["train_set_y"][:]) # your train set labelstest_dataset = h5py.File('datasets/test_signs.h5', "r")test_set_x_orig = np.array(test_dataset["test_set_x"][:]) # your test set featurestest_set_y_orig = np.array(test_dataset["test_set_y"][:]) # your test set labelsclasses = np.array(test_dataset["list_classes"][:]) # the list of classestrain_set_y_orig = train_set_y_orig.reshape((1, train_set_y_orig.shape[0]))test_set_y_orig = test_set_y_orig.reshape((1, test_set_y_orig.shape[0]))return train_set_x_orig, train_set_y_orig, test_set_x_orig, test_set_y_orig, classesdef random_mini_batches(X, Y, mini_batch_size = 64, seed = 0):"""Creates a list of random minibatches from (X, Y)Arguments:X -- input data, of shape (input size, number of examples) (m, Hi, Wi, Ci)Y -- true "label" vector (containing 0 if cat, 1 if non-cat), of shape (1, number of examples) (m, n_y)mini_batch_size - size of the mini-batches, integerseed -- this is only for the purpose of grading, so that you're "random minibatches are the same as ours.Returns:mini_batches -- list of synchronous (mini_batch_X, mini_batch_Y)"""m = X.shape[0]                  # number of training examplesmini_batches = []np.random.seed(seed)# Step 1: Shuffle (X, Y)permutation = list(np.random.permutation(m))shuffled_X = X[permutation,:,:,:]shuffled_Y = Y[permutation,:]# Step 2: Partition (shuffled_X, shuffled_Y). Minus the end case.num_complete_minibatches = math.floor(m/mini_batch_size) # number of mini batches of size mini_batch_size in your partitionningfor k in range(0, num_complete_minibatches):mini_batch_X = shuffled_X[k * mini_batch_size : k * mini_batch_size + mini_batch_size,:,:,:]mini_batch_Y = shuffled_Y[k * mini_batch_size : k * mini_batch_size + mini_batch_size,:]mini_batch = (mini_batch_X, mini_batch_Y)mini_batches.append(mini_batch)# Handling the end case (last mini-batch < mini_batch_size)if m % mini_batch_size != 0:mini_batch_X = shuffled_X[num_complete_minibatches * mini_batch_size : m,:,:,:]mini_batch_Y = shuffled_Y[num_complete_minibatches * mini_batch_size : m,:]mini_batch = (mini_batch_X, mini_batch_Y)mini_batches.append(mini_batch)return mini_batchesdef convert_to_one_hot(Y, C):Y = np.eye(C)[Y.reshape(-1)].Treturn Ydef forward_propagation_for_predict(X, parameters):"""Implements the forward propagation for the model: LINEAR -> RELU -> LINEAR -> RELU -> LINEAR -> SOFTMAXArguments:X -- input dataset placeholder, of shape (input size, number of examples)parameters -- python dictionary containing your parameters "W1", "b1", "W2", "b2", "W3", "b3"the shapes are given in initialize_parametersReturns:Z3 -- the output of the last LINEAR unit"""# Retrieve the parameters from the dictionary "parameters" W1 = parameters['W1']b1 = parameters['b1']W2 = parameters['W2']b2 = parameters['b2']W3 = parameters['W3']b3 = parameters['b3'] # Numpy Equivalents:Z1 = tf.add(tf.matmul(W1, X), b1)                      # Z1 = np.dot(W1, X) + b1A1 = tf.nn.relu(Z1)                                    # A1 = relu(Z1)Z2 = tf.add(tf.matmul(W2, A1), b2)                     # Z2 = np.dot(W2, a1) + b2A2 = tf.nn.relu(Z2)                                    # A2 = relu(Z2)Z3 = tf.add(tf.matmul(W3, A2), b3)                     # Z3 = np.dot(W3,Z2) + b3return Z3def predict(X, parameters):W1 = tf.convert_to_tensor(parameters["W1"])b1 = tf.convert_to_tensor(parameters["b1"])W2 = tf.convert_to_tensor(parameters["W2"])b2 = tf.convert_to_tensor(parameters["b2"])W3 = tf.convert_to_tensor(parameters["W3"])b3 = tf.convert_to_tensor(parameters["b3"])params = {"W1": W1,"b1": b1,"W2": W2,"b2": b2,"W3": W3,"b3": b3}x = tf.placeholder("float", [12288, 1])z3 = forward_propagation_for_predict(x, params)p = tf.argmax(z3)sess = tf.Session()prediction = sess.run(p, feed_dict = {x: X})return prediction#def predict(X, parameters):
#
#    W1 = tf.convert_to_tensor(parameters["W1"])
#    b1 = tf.convert_to_tensor(parameters["b1"])
#    W2 = tf.convert_to_tensor(parameters["W2"])
#    b2 = tf.convert_to_tensor(parameters["b2"])
##    W3 = tf.convert_to_tensor(parameters["W3"])
##    b3 = tf.convert_to_tensor(parameters["b3"])
#
##    params = {"W1": W1,
##              "b1": b1,
##              "W2": W2,
##              "b2": b2,
##              "W3": W3,
##              "b3": b3}
#
#    params = {"W1": W1,
#              "b1": b1,
#              "W2": W2,
#              "b2": b2}
#
#    x = tf.placeholder("float", [12288, 1])
#
#    z3 = forward_propagation(x, params)
#    p = tf.argmax(z3)
#
#    with tf.Session() as sess:
#        prediction = sess.run(p, feed_dict = {x: X})
#
#    return prediction

看数据集,代码:

import cnn_utils
import cv2
train_set_x_orig, train_set_Y, test_set_x_orig, test_set_Y, classes = cnn_utils.load_dataset()
print('训练样本={}'.format(train_set_x_orig.shape))
print('训练样本标签={}'.format(train_set_Y.shape))
print('测试样本={}'.format(test_set_x_orig.shape))
print('测试样本标签={}'.format(test_set_Y.shape))
print('第五个样本={}'.format(train_set_Y[0,5]))
cv2.imshow('1.jpg',train_set_x_orig[5,:,:,:]/255)
cv2.waitKey()train_set_x_flatten = train_set_x_orig.reshape(train_set_x_orig.shape[0],train_set_x_orig.shape[1] * train_set_x_orig.shape[2] * 3).T
test_set_x_flatten = test_set_x_orig.reshape(test_set_x_orig.shape[0],test_set_x_orig.shape[1] * test_set_x_orig.shape[2] * 3).T
train_X = train_set_x_flatten / 255  #(12288,1080)
test_X = test_set_x_flatten / 255

打印结果:训练样本数1080个,size(64*64*3),数字4代表手势数字四

开始搭建神经网络代码如下:

import cnn_utils
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
import h5py
"""
定义卷积核
"""
def initialize_parameter():W1 = tf.get_variable('W1',shape=[4,4,3,8],initializer=tf.contrib.layers.xavier_initializer())#tf.add_to_collection("losses", tf.contrib.layers.l2_regularizer(0.07)(W1))W2 = tf.get_variable('W2', shape=[2, 2, 8, 16], initializer=tf.contrib.layers.xavier_initializer())#tf.add_to_collection("losses", tf.contrib.layers.l2_regularizer(0.07)(W2))parameters={'W1':W1,'W2':W2}return parameters
"""创建输入输出placeholder
"""
def creat_placeholder(n_xH,n_xW,n_C0,n_y):X=tf.placeholder(tf.float32,shape=(None,n_xH,n_xW,n_C0))Y = tf.placeholder(tf.float32, shape=(None, n_y))return X,Y
"""
传播过程
"""
def forward_propagation(X,parameters):W1=parameters['W1']W2 = parameters['W2']Z1=tf.nn.conv2d(X,W1,strides=[1,1,1,1],padding='SAME')print('第一次卷积尺寸={}'.format(Z1.shape))A1=tf.nn.relu(Z1)P1 = tf.nn.max_pool(A1, ksize=[1,8,8,1], strides=[1, 8, 8, 1], padding='VALID')print('第一次池化尺寸={}'.format(P1.shape))Z2 = tf.nn.conv2d(P1, W2, strides=[1, 1, 1, 1], padding='SAME')print('第二次卷积尺寸={}'.format(Z2.shape))A2 = tf.nn.relu(Z2)P2 = tf.nn.max_pool(A2, ksize=[1, 4, 4, 1], strides=[1, 4, 4, 1], padding='VALID')print('第二次池化尺寸={}'.format(P2.shape))P_flatten=tf.contrib.layers.flatten(P2)Z3=tf.contrib.layers.fully_connected(P_flatten,6,activation_fn=None)return Z3
"""
计算损失值
"""
def compute_cost(Z3,Y):cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=Z3, labels=Y))return cost
"""
模型应用过程
"""
def model(learning_rate,num_pochs,minibatch_size):train_set_x_orig, train_y_orig, test_set_x_orig, test_y_orig, classes=cnn_utils.load_dataset()train_x = train_set_x_orig / 255test_x = test_set_x_orig / 255# 转换成one-hottrain_y=cnn_utils.convert_to_one_hot(train_y_orig,6).Ttest_y = cnn_utils.convert_to_one_hot(test_y_orig, 6).Tm,n_xH, n_xW, n_C0=train_set_x_orig.shapen_y=train_y.shape[1]X, Y = creat_placeholder(n_xH, n_xW, n_C0, n_y)parameters = initialize_parameter()Z3 = forward_propagation(X, parameters)cost = compute_cost(Z3, Y)##带正则项误差# tf.add_to_collection("losses", cost)# loss = tf.add_n(tf.get_collection('losses'))optimizer=tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)init = tf.global_variables_initializer()costs=[]with tf.Session() as sess:sess.run(init)for epoch in range(num_pochs):minibatch_cost=0num_minibatches=int(m/minibatch_size)minibatchs=cnn_utils.random_mini_batches(train_x,train_y,)for minibatch in minibatchs:(mini_batch_X, mini_batch_Y)=minibatch_,temp_cost = sess.run([optimizer,cost], feed_dict={X:mini_batch_X , Y: mini_batch_Y})minibatch_cost+=temp_cost/num_minibatchesif epoch%5==0:print('after {} epochs minibatch_cost={}'.format(epoch,minibatch_cost))costs.append(minibatch_cost)#predict_y=tf.argmax(Z3,1)####1 represent hang zuidacorect_prediction=tf.equal(tf.argmax(Z3,1),tf.argmax(Y,1))accuarcy=tf.reduce_mean(tf.cast(corect_prediction,'float'))train_accuarcy=sess.run(accuarcy,feed_dict={X:train_x,Y:train_y})test_accuarcy = sess.run(accuarcy, feed_dict={X: test_x, Y: test_y})print('train_accuarcy={}'.format(train_accuarcy))print('test_accuarcy={}'.format(test_accuarcy))plt.plot(np.squeeze(costs))plt.ylabel('cost')plt.xlabel('iterations ')plt.title('learning rate={}'.format(learning_rate))plt.show()def test_model():model(learning_rate=0.009,num_pochs=100,minibatch_size=32)
def test():########test forward# init = tf.global_variables_initializer()# sess = tf.Session()# sess.run(init)with tf.Session() as sess:X,Y=creat_placeholder(64,64,3,6)parameters=initialize_parameter()Z3=forward_propagation(X,parameters)cost=compute_cost(Z3,Y)init = tf.global_variables_initializer()sess.run(init)Z3,cost=sess.run([Z3,cost],feed_dict={X:np.random.randn(2,64,64,3),Y:np.random.randn(2,6)})print('Z3={}'.format(Z3))print('cost={}'.format(cost))################if __name__=='__main__':#test()test_model()

打印结果:

其中?代表样本数,可看出最后池化维度结果为(2,2,16),在接全连接层即可。

迭代100次,最后损失值图如下:

训练精度为0.98,测试精度为0.89,还不错啊,继续还可以优化。

吴恩达作业9:卷积神经网络实现手势数字的识别(基于tensorflow)相关推荐

  1. 吴恩达深度学习卷积神经网络学习笔记(2)——经典神经网络

    目录 1.经典网络 1.1 LeNet-5(1998) 1.2 AlexNet 1.3 VGG-16 2 ResNets(残差网络) 2.1残差块(Residual block) 2.2 残差网络为什 ...

  2. 卷积神经网络学习项目--Kaggle仙人掌识别--基于TensorFlow(未完成)

    这是 Kaggle 上的一个学习项目,任务是识别航拍图像中是否有柱状仙人掌. 数据下载:https://www.kaggle.com/c/aerial-cactus-identification/da ...

  3. 吴恩达对话LeCun:神经网络跌宕四十年

    夏乙 栗子 发自 凹非寺 量子位 出品 | 公众号 QbitAI Yann LeCun,深度学习三巨头之一. 最近,这位AI领域的传奇大牛,接受了另一位大牛吴恩达的视频专访.在这次对话中,LeCun回 ...

  4. 【深度学习】吴恩达深度学习-Course1神经网络与深度学习-第四周深度神经网络的关键概念编程(下)——深度神经网络用于图像分类:应用

    在阅读这篇文章之前,请您先阅读:[深度学习]吴恩达深度学习-Course1神经网络与深度学习-第四周深度神经网络的关键概念编程(上)--一步步建立深度神经网络,这篇文章是本篇文章的前篇,没有前篇的基础 ...

  5. 2-3 Coursera吴恩达《改善深度神经网络》第三周课程笔记-超参数调试、Batch正则化和编程框架

    上节课2-2 Coursera吴恩达<改善深度神经网络>第二周课程笔记-优化算法我们主要介绍了深度神经网络的优化算法.包括对原始数据集进行分割,使用mini-batch 梯度下降(mini ...

  6. 吴恩达《优化深度神经网络》精炼笔记(3)-- 超参数调试、Batch正则化和编程框架...

    AI有道 不可错过的AI技术公众号 关注 重要通知 本公众号原名"红色石头的机器学习之路"已经改名为"AI有道",请大家留意并继续关注本公众号!谢谢! 上节课我 ...

  7. Coursera吴恩达《优化深度神经网络》课程笔记(3)-- 超参数调试、Batch正则化和编程框架

    红色石头的个人网站:redstonewill.com 上节课我们主要介绍了深度神经网络的优化算法.包括对原始数据集进行分割,使用mini-batch gradient descent.然后介绍了指数加 ...

  8. 吴恩达机器学习笔记week8——神经网络 Neutral network

    吴恩达机器学习笔记week8--神经网络 Neutral network 8-1.非线性假设 Non-linear hypotheses 8-2.神经元与大脑 Neurons and the brai ...

  9. 吴恩达【优化深度神经网络】笔记01——深度学习的实用层面

    文章目录 引言 一.训练集/验证集/测试集(Train/Dev/Test sets) 1. 数据集选择 2. 补充:交叉验证(cross validation) 二.偏差和方差(Bias/Varian ...

最新文章

  1. leetcode 203 Remove Linked List Elements
  2. Android系统在新进程中启动自定义服务过程(startService)的原理分析 (下)
  3. python安装不了jieba_python安装jieba失败怎么办?_后端开发
  4. python初学工资-python工资高还是java?
  5. vscode 好用插件
  6. 这声音酥了!萌妹程序员鼓励师24小时在线陪你写代码,给我吹爆这个VSCode插件...
  7. 宿迁学计算机的学校,宿迁计算机学校
  8. MySQL的环境变量配置详细步骤
  9. perl 远程 mysql_写的一个perl脚本,用于发送远程MySQL命令 -电脑资料
  10. css控制的代码,通过CSS控制把网页上的代码美化
  11. 【C语言笔记进阶篇】第二章:字符串函数和内存函数
  12. 【SQL】数值型函数
  13. 以弹窗形式打开页面_“弹窗广告”肆意而为!扰民?还可能侵权!
  14. python之web编程
  15. java爬虫——JSoup
  16. shoppping collection
  17. 音频 ASIO 驱动开发
  18. IT软件技术人员的职位路线(从程序员到技术总监) - 部门管理经验谈
  19. 【C语言 穷举法编程实例——韩信点兵问题(苏小红版C语言(第3版))】
  20. webpack项目运用(一)打包压缩css文件

热门文章

  1. 论文小综 | 知识图谱表示学习中的零样本实体研究
  2. 实体链指比赛方案分享
  3. JavaWeb:XML总结
  4. mysql5.7 备份
  5. Idea中在代码顶部添加自定义作者和时间
  6. day20 派生属性和方法,钻石继承
  7. 数据库 proc编程七
  8. (六)Spark-Eclipse开发环境WordCount-JavaPython版Spark
  9. mongoDB 特别指令用法
  10. Easyspy网络检测系统