介绍
本文介绍了搭建简单的卷积网络模型进行手势数字识别
数据集:https://github.com/stormstone/deeplearning.ai/tree/c38b8ea7cc7fef5caf88be6e06f4e3452690fde7
工具:TensorFlow 1.9.0 + Python 3.6.3
1.相关包导入
// An highlighted block
import math
import numpy as np
import h5py
import matplotlib.pyplot as plt
import scipy
from PIL import Image
from scipy import ndimage
import tensorflow as tf
from tensorflow.python.framework import ops
from cnn_utils import *
%matplotlib inline
np.random.seed(1)
2.数据准备
本实验使用SIGNS数据集是6个符号的集合,表示从0到5的数字。
训练集:1080个图像(64乘64像素)的符号表示从0到5的数字(每个数字180个图像)。
测试集:120张图片(64乘64像素)的符号,表示从0到5的数字(每个数字20张图片)。
2.1数据实例
下面将显示数据集中标记图像的示例。 随意更改下面的index
的值,然后重新运行以查看不同的示例。
index = 6
plt.imshow(X_train_orig[index])
print ("y = " + str(np.squeeze(Y_train_orig[:, index])))
y = 2
2.2数据检查
X_train = X_train_orig/255.
X_test = X_test_orig/255.
Y_train = convert_to_one_hot(Y_train_orig, 6).T
Y_test = convert_to_one_hot(Y_test_orig, 6).T
print ("number of training examples = " + str(X_train.shape[0]))
print ("number of test examples = " + str(X_test.shape[0]))
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))
conv_layers = {
}
运行结果:
number of training examples = 1080
number of test examples = 120
X_train shape: (1080, 64, 64, 3)
Y_train shape: (1080, 6)
X_test shape: (120, 64, 64, 3)
Y_test shape: (120, 6)
3.参数初始化
# GRADED FUNCTION: initialize_parameters
def initialize_parameters():
"""
Initializes weight parameters to build a neural network with tensorflow. The shapes are:
W1 : [4, 4, 3, 8]
W2 : [2, 2, 8, 16]
Returns:
parameters -- a dictionary of tensors containing W1, W2
"""
tf.set_random_seed(1) # so that your "random" numbers match ours
### START CODE HERE ### (approx. 2 lines of code)
W1 = tf.get_variable("W1", [4,4,3,8], initializer = tf.contrib.layers.xavier_initializer(seed = 0))
W2 = tf.get_variable(&#