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

数据集链接: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,还不错啊,继续还可以优化。

 

 

 

 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/493595.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

AI洞观 | 戴上红帽 看IBM冲杀云计算市场

来源 | 网易智能&#xff08;公众号 smartman163&#xff09;摘要&#xff1a;可以预计&#xff0c;未来的云计算市场将越来越比拼生态综合服务能力&#xff0c;云计算行业进入下半场&#xff0c;谁在裸泳不久见分晓。IBM豪掷340亿美元收购红帽&#xff08;Red Hat&#xff09;…

基于visual Studio2013解决面试题之0608找出两个只出现一次的数

&#xfeff;&#xfeff;&#xfeff;题目解决代码及点评/*已知数组中有两个数只出现一次&#xff0c;其他成对出现&#xff0c;请找出这两个数解决办法&#xff1a;1&#xff09;简化问题&#xff0c;如果数组中只有一个数出现一次&#xff0c;那么只要对这个数组做异或即可2…

工业富联:左手工业AI,右手“雾小脑”

来源&#xff1a;先进制造业摘要&#xff1a;11月14日&#xff0c;第二十届中国国际高新技术成果交易会在深圳会展中心开幕。11月14日&#xff0c;第二十届中国国际高新技术成果交易会在深圳会展中心开幕。备受瞩目的是&#xff0c;在分论坛“2018第九届中国信息通信论坛”上&a…

吴恩达作业10:用卷积神经网络识别人脸happy(基于Keras)

数据集提供的代码放在kt_utils.py&#xff1a; import keras.backend as K import math import numpy as np import h5py import matplotlib.pyplot as pltdef mean_pred(y_true, y_pred):return K.mean(y_pred)def load_dataset():train_dataset h5py.File(datasets/train_h…

清华大学:智能驾驶背景下转向系统发展趋势

来源&#xff1a;智车科技这里近几年&#xff0c;自动驾驶汽车的研发与推广发展迅速&#xff0c;根据国家工业和信息化部等发布的《汽车产业中长期发展规划》&#xff0c;2025 年&#xff0c;高度和完全自动驾驶汽车开始进入市场&#xff0c;在此背景下&#xff0c;线控转向技术…

TensorFlow学习之——checkpoints

在看别人的训练网络中一开头就遇到这样一行代码&#xff1a; ckpt tf.train.get_checkpoint_state(directories.checkpoints) 鼠标放在函数名上&#xff0c;ctrlB&#xff0c;或者ctrl点击函数名&#xff0c;可以跳转到函数的定义&#xff0c;可以知道tf.train.get_checkpoi…

jQuery.ajax

(摘录)http://www.cnblogs.com/XuebinDing/archive/2012/03/01/2376041.html 情况一、使用WebService验证 1.新建demo.aspx页面。2.首先在该页面的后台文件demos.aspx.cs中添加引用。 using System.Web.Services; 3.无参数的方法调用. 大家注意了&#xff0c;这个版本不能低于.…

基于Keras的卷积神经网络用于猫狗分类(未进行数据增强)+卷积层可视化

首先看数据集路径&#xff1a; cats和dogs存放的就是各种大小的猫狗图片。 读取数据集代码&#xff1a; import os import matplotlib.pyplot as plt """ 读取数据 返回数据的文件夹名字&#xff0c;和具体的猫狗的路径 """ def read_data(): …

芯片植入:“增强人类”的生物黑科技

来源&#xff1a;资本实验室摘要&#xff1a;当医疗、电子、生物科技快速发展&#xff0c;并紧密融合的时候&#xff0c;许多科幻电影中的场景正在不断变为现实&#xff0c;而“增强人类”、”赛博格“、“电子人”、“生化人”正在成为这个时代最值得期待&#xff0c;又多少让…

如何通过VC的 CHttpFile 抓取网页内容

在点击一个按钮时开始请求你输入的地址。void CHttpFileDlg::OnButton1() {   CString url;   GetDlgItemText(IDC_EDIT1,url); // IDC_EDIT1 是一个输入框的名字。   char* headers"Accept:*/*\r\nAccept-Language:zh-cn\r\nUser-Agent:VCTestClient\r\n"; …

windows安装ubuntu16.04LTS 更换系统源为阿里源 安装ubuntu找不到windows 删除内核 更换pip源 升级pip源报错 ubuntu命令

一&#xff0c;安装ubuntu ubuntu镜像Index of /ubuntu-releases/18.04/ 首先在windows下硬盘划分出100G的空间&#xff0c;并且制作一个ubuntu的启动盘。在windows安装easy BCD用于开机启动ubuntu。下面看安装过程&#xff1a; 选择语言&#xff0c;我选择是英语 我选择的是…

基本系统部署完成!北斗三号闪耀中国智慧

▲ 第四十二、四十三颗北斗导航卫星乘长三乙火箭升空来源&#xff1a;航天501部2018年11月19日&#xff0c;第四十二、四十三颗北斗导航卫星在西昌卫星发射中心腾空而起。作为北斗三号第十八颗、第十九颗卫星&#xff0c;此次双星的成功发射&#xff0c;标志着北斗三号全球组网…

基于Keras的卷积神经网络用于猫狗分类(进行了数据增强)+卷积层可视化

接着我上一篇博客&#xff0c;https://blog.csdn.net/fanzonghao/article/details/81149153。 在上一篇基础上对数据集进行数据增强。函数如下&#xff1a; """ 查看图像增强是否发生作用 """ def see_pic_aug():train_datagen ImageDataGene…

深圳神经科学研究院院长谭力海: AI取代人脑? 不, 必须向人脑“学习”!

来源&#xff1a;读创科技摘要&#xff1a;从“深蓝”到“阿尔法狗”&#xff0c;人工智能技术日益成熟&#xff0c;“AI何时替代人脑”的争论也在不断升级。人工智能真的能超越人脑吗&#xff1f;11月15日在高交会“颠覆性创新技术”主题论坛上&#xff0c;深圳神经科学研究院…

好的PPT——准备工作

首先介绍PPT的一些基本技巧。 在选项界面&#xff0c;我们需要调整一些选项&#xff1a;控制最大可回退次数&#xff1b;语法自动检查&#xff1b;字体嵌入PPT&#xff0c;调整自动保存的时间间隔。 对于一些常用操作&#xff0c;可以右键添加到常用工具栏。 可以很方便地调整不…

ubuntu安装谷歌浏览器 typora+出现编码错误‘ascii‘ codec can‘t encode character ‘\u6b66‘+docker里安装tensorrt报错

一.首先下载谷歌浏览器 https://www.google.cn/chrome/ sudo dpkg -i google-chrome-stable_current_amd64.deb 就安装好了&#xff0c;search谷歌浏览器就可以啦。 二,安装typora # optional, but recommendedsudo apt-key adv --keyserver keyserver.ubuntu.com --recv-ke…

中国安防为何世界最强?中科院AI+安防报告,解密8大趋势和8大限制【附下载】| 智东西内参...

来源&#xff1a;智东西传统的安防企业、新兴的 AI 初创企业&#xff0c;开始积极从技术各个维度拥抱人工智能&#xff0c;在模式识别基础理论、图像处理、计算机视觉以及语音信息处理展开了集中研究与持续创新&#xff0c;探索模式识别机理以及有效计算方法&#xff0c;为解决…

利用Inception-V3训练的权重微调,实现猫狗分类(基于keras)

利用Inception-V3训练的权重微调实现猫狗的分类&#xff0c;其中权重的下载在我的博客下载资源处&#xff0c;https://download.csdn.net/download/fanzonghao/10566634 第一种权重不改变直接用mixed7层&#xff08;mixed7呆会把打印结果一放就知道了&#xff09;进行特征提取…

刘锋:互联网左右大脑结构与钱学森开放复杂巨系统

作者&#xff1a;刘锋 互联网进化论作者 计算机博士前言&#xff1a;1990年&#xff0c;钱学森提出了开放的复杂巨系统理论&#xff0c;并提出以人为主&#xff0c;人机结合&#xff0c;从定性到定量的综合集成研究方法&#xff0c;他也预见性的提出“因特网正好生动地体现了…

手写字母数据集转换为.pickle文件

首先是数据集&#xff0c;我上传了相关的资源&#xff0c;https://download.csdn.net/download/fanzonghao/10566701 转换代码如下&#xff1a; import numpy as np import os import matplotlib.pyplot as plt import matplotlib.image as mpig import imageio import pickle…