实验说明

本实验为吴恩达课后编程作业第二课第三周内容,通过引导我们将完成一个深度学习框架,使我们可以更轻松地构建神经网络。编程框架不仅可以缩短编码时间,而且有时还可以执行加速代码的优化。
数据集下载地址:[https://github.com/stormstone/deeplearning.ai/tree/c38b8ea7cc7fef5caf88be6e06f4e3452690fde7]
工具:Jupyter Notebook (tensorflow) + Python 3.6.3
问题陈述:一天下午,我和一些朋友决定教我们的电脑破译手语。 我们花了几个小时在白墙前拍照,想出了以下数据集。 现在,您的工作是构建一种算法,以促进从语言障碍者到不懂手语的人的通信。
训练集:1080个图像(64乘64像素)的符号表示从0到5的数字(每个数字180个图像)。
测试集:120张图片(64乘64像素)的符号,表示从0到5的数字(每个数字20张图片)。
以下是每个数字的示例,以及如何解释我们如何表示标签。 在我们将图像重新降低到64 x 64像素之前,这些是原始图片。

1、Exploring the Tensorflow Library

1.1 首先导入库:
import math
import numpy as np
import h5py
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.python.framework import ops
from tf_utils import load_dataset, random_mini_batches, convert_to_one_hot, predict%matplotlib inline
np.random.seed(1)
1.2 加载数据集:
X_train_orig, Y_train_orig, X_test_orig, Y_test_orig, classes = load_dataset()
print(X_train_orig.shape)
print(Y_train_orig.shape)
print(X_test_orig.shape)
print(Y_test_orig.shape)

运行结果展示:

1.3 图片示例
index = 2
plt.imshow(X_train_orig[index])
print ("y = " + str(np.squeeze(Y_train_orig[:, index])))

运行结果展示:

1.4 输出数据集信息
# Flatten the training and test images
X_train_flatten = X_train_orig.reshape(X_train_orig.shape[0], -1).T
X_test_flatten = X_test_orig.reshape(X_test_orig.shape[0], -1).T
# Normalize image vectors
X_train = X_train_flatten/255.
X_test = X_test_flatten/255.
# Convert training and test labels to one hot matrices
Y_train = convert_to_one_hot(Y_train_orig, 6)
Y_test = convert_to_one_hot(Y_test_orig, 6)print ("number of training examples = " + str(X_train.shape[1]))
print ("number of test examples = " + str(X_test.shape[1]))
print ("X_train shape: " + str(X_train.shape))
print ("Y_train shape: " + str(Y_train.shape))
print ("X_test shape: " + str(X_test.shape))
print ("Y_test shape: " + str(Y_test.shape))
print(Y_test_orig[0][9])
print(Y_test_orig[0][8])
print(Y_test_orig[0][7])
print(Y_test_orig[0][6])
print(Y_test_orig[0][5])
print(Y_test_orig[0][4])

运行结果:

2.1 - Create placeholders

# GRADED FUNCTION: create_placeholdersdef create_placeholders(n_x, n_y):"""Creates the placeholders for the tensorflow session.Arguments:n_x -- scalar, size of an image vector (num_px * num_px = 64 * 64 * 3 = 12288)n_y -- scalar, number of classes (from 0 to 5, so -> 6)Returns:X -- placeholder for the data input, of shape [n_x, None] and dtype "float"Y -- placeholder for the input labels, of shape [n_y, None] and dtype "float"Tips:- You will use None because it let's us be flexible on the number of examples you will for the placeholders.In fact, the number of examples during test/train is different."""### START CODE HERE ### (approx. 2 lines)X = tf.placeholder(dtype = tf.float32, shape = [n_x, None])Y = tf.placeholder(dtype = tf.float32, shape = [n_y, None])### END CODE HERE ###return X, Y
X, Y = create_placeholders(12288, 6)
print ("X = " + str(X))
print ("Y = " + str(Y))

运行结果:

2.2 - Initializing the parameters

# GRADED FUNCTION: initialize_parametersdef initialize_parameters():"""Initializes parameters to build a neural network with tensorflow. The shapes are:W1 : [25, 12288]b1 : [25, 1]W2 : [12, 25]b2 : [12, 1]W3 : [6, 12]b3 : [6, 1]Returns:parameters -- a dictionary of tensors containing W1, b1, W2, b2, W3, b3"""tf.set_random_seed(1)                   # so that your "random" numbers match ours### START CODE HERE ### (approx. 6 lines of code)W1 = tf.get_variable("W1", [25, 12288], initializer = tf.contrib.layers.xavier_initializer(seed = 1))b1 = tf.get_variable("b1", [25, 1], initializer = tf.zeros_initializer())W2 = tf.get_variable("W2", [12, 25], initializer = tf.contrib.layers.xavier_initializer(seed = 1))b2 = tf.get_variable("b2", [12, 1], initializer = tf.zeros_initializer())W3 = tf.get_variable("W3", [6, 12], initializer = tf.contrib.layers.xavier_initializer(seed = 1))b3 = tf.get_variable("b3", [6, 1], initializer = tf.zeros_initializer())### END CODE HERE ###parameters = {"W1": W1,"b1": b1,"W2": W2,"b2": b2,"W3": W3,"b3": b3}return parameters
tf.reset_default_graph()
with tf.Session() as sess:parameters = initialize_parameters()print("W1 = " + str(parameters["W1"]))print("b1 = " + str(parameters["b1"]))print("W2 = " + str(parameters["W2"]))print("b2 = " + str(parameters["b2"]))

运行结果:

2.3 - Forward propagation in tensorflow

# GRADED FUNCTION: forward_propagationdef forward_propagation(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']### START CODE HERE ### (approx. 5 lines)              # 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) + b3### END CODE HERE ###return Z3
tf.reset_default_graph()with tf.Session() as sess:X, Y = create_placeholders(12288, 6)parameters = initialize_parameters()Z3 = forward_propagation(X, parameters)print("Z3 = " + str(Z3))

运行结果:

2.4 Compute cost

# GRADED FUNCTION: compute_cost def compute_cost(Z3, Y):"""Computes the costArguments:Z3 -- output of forward propagation (output of the last LINEAR unit), of shape (6, number of examples)Y -- "true" labels vector placeholder, same shape as Z3Returns:cost - Tensor of the cost function"""# to fit the tensorflow requirement for tf.nn.softmax_cross_entropy_with_logits(...,...)logits = tf.transpose(Z3)labels = tf.transpose(Y)### START CODE HERE ### (1 line of code)cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits = logits, labels = labels))### END CODE HERE ###return cost
tf.reset_default_graph()with tf.Session() as sess:X, Y = create_placeholders(12288, 6)parameters = initialize_parameters()Z3 = forward_propagation(X, parameters)cost = compute_cost(Z3, Y)print("cost = " + str(cost))

运行结果:

2.5 - Building the model

def model(X_train, Y_train, X_test, Y_test, learning_rate = 0.0001,num_epochs = 1500, minibatch_size = 32, print_cost = True):"""Implements a three-layer tensorflow neural network: LINEAR->RELU->LINEAR->RELU->LINEAR->SOFTMAX.Arguments:X_train -- training set, of shape (input size = 12288, number of training examples = 1080)Y_train -- test set, of shape (output size = 6, number of training examples = 1080)X_test -- training set, of shape (input size = 12288, number of training examples = 120)Y_test -- test set, of shape (output size = 6, number of test examples = 120)learning_rate -- learning rate of the optimizationnum_epochs -- number of epochs of the optimization loopminibatch_size -- size of a minibatchprint_cost -- True to print the cost every 100 epochsReturns:parameters -- parameters learnt by the model. They can then be used to predict."""ops.reset_default_graph()                         # to be able to rerun the model without overwriting tf variablestf.set_random_seed(1)                             # to keep consistent resultsseed = 3                                          # to keep consistent results(n_x, m) = X_train.shape                          # (n_x: input size, m : number of examples in the train set)n_y = Y_train.shape[0]                            # n_y : output sizecosts = []                                        # To keep track of the cost# Create Placeholders of shape (n_x, n_y)### START CODE HERE ### (1 line)X, Y = create_placeholders(n_x, n_y)### END CODE HERE #### Initialize parameters### START CODE HERE ### (1 line)parameters = initialize_parameters()### END CODE HERE #### Forward propagation: Build the forward propagation in the tensorflow graph### START CODE HERE ### (1 line)Z3 = forward_propagation(X, parameters)### END CODE HERE #### Cost function: Add cost function to tensorflow graph### START CODE HERE ### (1 line)cost = compute_cost(Z3, Y)### END CODE HERE #### Backpropagation: Define the tensorflow optimizer. Use an AdamOptimizer.### START CODE HERE ### (1 line)optimizer = tf.train.AdamOptimizer(learning_rate = learning_rate).minimize(cost)### END CODE HERE #### Initialize all the variablesinit = tf.global_variables_initializer()# Start the session to compute the tensorflow graphwith tf.Session() as sess:# Run the initializationsess.run(init)# Do the training loopfor epoch in range(num_epochs):epoch_cost = 0.                       # Defines a cost related to an epochnum_minibatches = int(m / minibatch_size) # number of minibatches of size minibatch_size in the train setseed = seed + 1minibatches = random_mini_batches(X_train, Y_train, minibatch_size, seed)for minibatch in minibatches:# Select a minibatch(minibatch_X, minibatch_Y) = minibatch# IMPORTANT: The line that runs the graph on a minibatch.# Run the session to execute the "optimizer" and the "cost", the feedict should contain a minibatch for (X,Y).### START CODE HERE ### (1 line)_ , minibatch_cost = sess.run([optimizer, cost], feed_dict = {X : minibatch_X, Y : minibatch_Y})### END CODE HERE ###epoch_cost += minibatch_cost / num_minibatches# Print the cost every epochif print_cost == True and epoch % 100 == 0:print ("Cost after epoch %i: %f" % (epoch, epoch_cost))if print_cost == True and epoch % 5 == 0:costs.append(epoch_cost)# plot the costplt.plot(np.squeeze(costs))plt.ylabel('cost')plt.xlabel('iterations (per tens)')plt.title("Learning rate =" + str(learning_rate))plt.show()# lets save the parameters in a variableparameters = sess.run(parameters)print ("Parameters have been trained!")# Calculate the correct predictionscorrect_prediction = tf.equal(tf.argmax(Z3), tf.argmax(Y))# Calculate accuracy on the test setaccuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))print ("Train Accuracy:", accuracy.eval({X: X_train, Y: Y_train}))print ("Test Accuracy:", accuracy.eval({X: X_test, Y: Y_test}))return parameters
parameters = model(X_train, Y_train, X_test, Y_test)

运行结果:

2.6 - Test with your own image (optional / ungraded exercise)

恭喜你完成了这项任务。 现在你可以拍摄手的图片并查看模型的输出。 要做到这一点:

1.拍摄手势图片。
2.将图像添加到此代码运行目录中的images文件夹内。
3.在以下代码中写下该图像名称(此处照片名字为:thumbs_up.jpg)。
4.运行代码并检查算法是否正确!

import scipy
from PIL import Image
from scipy import ndimage## START CODE HERE ## (PUT YOUR IMAGE NAME)
my_image = "thumbs_up.jpg"
## END CODE HERE ### We preprocess your image to fit your algorithm.
fname = "images/" + my_image
image = np.array(ndimage.imread(fname, flatten=False))
my_image = scipy.misc.imresize(image, size=(64,64)).reshape((1, 64*64*3)).T
my_image_prediction = predict(my_image, parameters)plt.imshow(image)
print("Your algorithm predicts: y = " + str(np.squeeze(my_image_prediction)))

运行结果:

本文内容编辑:崔昊

[AI教程]TensorFlow入门:手势数字识别相关推荐

  1. TensorFlow解决MNIST数字识别问题

    TensorFlow解决MNIST数字识别问题 废话 这个MNIST数字识别问题是我实现的第一个神经网络,虽然过程基本上都是对着书上的代码敲,但还是对神经网络的训练过程有了一定的了解,同时也复习了前面 ...

  2. 深度学习 第三章 tensorflow手写数字识别

    深度学习入门视频-唐宇迪 (笔记加自我整理) 深度学习 第三章 tensorflow手写数字识别 1.tensorflow常见操作 这里使用的是tensorflow1.x版本,tensorflow基本 ...

  3. Python 基础 之 jupyter notebook 中机器学习的简单入门书写数字识别 demo 操作学习

    Python 基础 之 jupyter notebook 中机器学习的简单入门书写数字识别 demo 操作学习 目录 Python 基础 之 jupyter notebook 中机器学习的简单入门书写 ...

  4. TensorFlow手写数字识别与一步一步实现卷积神经网络(附代码实战)

    编译 | fendouai 编辑 | 安可 [导读]:本篇文章将说明 TensorFlow 手写数字识别与一步一步实现卷积神经网络.欢迎大家点击上方蓝字关注我们的公众号:深度学习与计算机视觉. 手写数 ...

  5. Python中用TensorFlow框架实现数字识别

    TensorFlow框架实现数字识别 安装TensorFlow 安装TensorFlow有CPU安装和GPU安装之分,每个都有自己的优缺点,看自己喜好安装 具体安装可以参考 https://blog. ...

  6. TensorFlow 2.0 快速上手教程与手写数字识别例子讲解

    文章目录 TensorFlow 基础 自动求导机制 参数优化 TensorFlow 模型建立.训练与评估 通用模型的类结构 多层感知机手写数字识别 Keras Pipeline * TensorFlo ...

  7. 基于TensorFlow的手写体数字识别

    目录 一.MNIST数据集介绍 二.原理 2.1.卷积神经网络简介( convolutional neural network 简称CNN) 2.1.1卷积运算过程 2.1.2滑动的步长 2.1.3卷 ...

  8. TensorFlow入门之二:tensorflow手写数字识别

    一.基础知识 基础知识可以跳过,可以直接看后面的代码实现 MNIST数据集 MNIST数据集的官网是Yann LeCun's website.可以使用下面的python代码自动下载数据集. #已经下载 ...

  9. 基于mediapipe的手势数字识别

    基于mediapipe识别手势所对应的数字(一.二.三.四.五). mediapipe的官网 总体思路:mediapipe可以识别手掌的关键点,我的思路是识别单根手指是否弯曲,然后根据五根手指的弯曲程 ...

最新文章

  1. 数学与当代生命科学(吴家睿)
  2. 【转】深入探讨 Android 传感器
  3. WebApi项目创建CURD
  4. php curl 使用方法,php curl使用方法与步骤
  5. C语言实现简单计算器(可以处理小括号)
  6. [POJ1952]BUY LOW, BUY LOWER
  7. JS 使用RSA加密解密
  8. 慢性病管理系统/案列/APP/小程序/网站
  9. Blockchain -Corda框架研究一 sendfile-Attachments学习笔记
  10. 基于transformor的拼音转汉字语言模型。
  11. 各种风格登录页响应式html5模板 Admin后台管理系统模板手机wap登录页html模板html会员登录页面模板源码70多套高大尚响应式网站模板html5网页静态模板Bootstrap扁平化网站源码
  12. Itext pdf的页眉页脚
  13. 汉明窗口Hamming Window
  14. 对话《深入理解Java虚拟机》作者周志明:电竞选手成为Java大神之路
  15. linux中的lnk格式,ink文件扩展名,ink文件怎么打开?
  16. [附源码]Nodejs计算机毕业设计基于JAVA的校园电车租赁系统Express(程序+LW)
  17. 【30天从入门到放弃】我的机器学习之路 4
  18. Java小程序——模仿Win系统画板
  19. IO流的小细节(很小很细很重要)
  20. Netty作为服务端的websocket通信

热门文章

  1. 高中计算机备课教案模板,高中信息技术教案:《程序设计基础》教案模板
  2. 使用视频下载工具 you-get 下载视频
  3. 前端实现base64格式文件的下载
  4. 开源神器:可快速在 iOS 设备上安装 Windows、Linux 等操作系统!
  5. 关于ASA5520防火墙搭配WEB服务器的非常规设置
  6. 宝塔面板安装使用教程
  7. 新疆和田计算机考试成绩查询,2019新疆和田公务员笔试成绩查询入口_合格分数线...
  8. html网站制作项目ppt制作,项目4用HTML制作框架网页.ppt
  9. 常用ftp大全.txt
  10. 捷联惯导基础知识解析之五(低成本姿态航向参考系统)