一.数据增强
from keras.preprocessing.image import ImageDataGenerator, img_to_array, load_img
import os
import time
datagen = ImageDataGenerator(
rotation_range=20,
width_shift_range=0.15,
height_shift_range=0.15,
zoom_range=0.15,
shear_range=0.2,
horizontal_flip=True,
fill_mode='nearest')
print("start.....: " + str((time.strftime('%Y-%m-%d %H:%M:%S'))))
dirs = os.listdir("D:/360MoveData/Users/ASUS/Desktop/gesture/音量减")
for filename in dirs:
img = load_img("D:/360MoveData/Users/ASUS/Desktop/gesture/音量减/{}".format(filename))
x = img_to_array(img)
# print(x.shape)
x = x.reshape((1,) + x.shape) #datagen.flow要求rank为4
# print(x.shape)
datagen.fit(x)
prefix = filename.split('.')[0]
print(prefix)
counter = 0
for batch in datagen.flow(x, batch_size=4 , save_to_dir='D:/360MoveData/Users/ASUS/Desktop/gesture_data/音量减', save_prefix=prefix, save_format='jpg'):
counter += 1
if counter > 150:
break # 否则生成器会退出循环
print("end....: " + str((time.strftime('%Y-%m-%d %H:%M:%S'))))
import tensorflow as tf
from tensorflow.python.framework import graph_util
import matplotlib.pyplot as plt
from input_data import get_batch
import os
W = 100 # 图片原来的长度
H = 100 # 图片原来的高度
Channels = 3 # 图片原来的通道数
batch_size = 20 # 定义组合数据batch的大小
num_epochs = 60000 # 训练轮数
n_classes = 12 # 类别数
logdir='./graphs/gesture' # 输出日志保存的路径
pb_file_path = "./gesture_model.pb"
MODEL_SAVE_PATH="./model/"
MODEL_NAME="gesture_model"
tra_data_dir = './data/gesture_train.tfrecords'
val_data_dir ='./data/gesture_test.tfrecords'
dropout=0.8
"""构造卷积神经网络"""
# 定义两个placeholder,用于输入数据
with tf.name_scope('input'):
x = tf.placeholder(tf.float32, shape=[None, H, W, Channels],name="input_x") ####这个名称很重要!!!
y = tf.placeholder(tf.int32, shape=[None, n_classes], name="input_y")
keep_prob = tf.placeholder(tf.float32, name='keep_prob')
global_step = tf.Variable(0, trainable=False)
regularizer = tf.contrib.layers.l2_regularizer(0.0001)
with tf.variable_scope('layer1-conv1'):
conv1_weights = tf.get_variable(
"weight", [5, 5, 3, 32],
initializer=tf.truncated_normal_initializer(stddev=0.1))
conv1_biases = tf.get_variable(
"bias", [32], initializer=tf.constant_initializer(0.0))
conv1 = tf.nn.conv2d(
x, conv1_weights, strides=[1, 1, 1, 1], padding='SAME')
relu1 = tf.nn.relu(tf.nn.bias_add(conv1, conv1_biases))
with tf.name_scope("layer2-pool1"):
pool1 = tf.nn.max_pool(
relu1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="VALID")
with tf.variable_scope("layer3-conv2"):
conv2_weights = tf.get_variable(
"weight", [5, 5, 32, 64],
initializer=tf.truncated_normal_initializer(stddev=0.1))
conv2_biases = tf.get_variable(
"bias", [64], initializer=tf.constant_initializer(0.0))
conv2 = tf.nn.conv2d(
pool1, conv2_weights, strides=[1, 1, 1, 1], padding='SAME')
relu2 = tf.nn.relu(tf.nn.bias_add(conv2, conv2_biases))
with tf.name_scope("layer4-pool2"):
pool2 = tf.nn.max_pool(
relu2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1],
padding='VALID')
with tf.variable_scope("layer5-conv3"):
conv3_weights = tf.get_variable(
"weight", [3, 3, 64, 128],
initializer=tf.truncated_normal_initializer(stddev=0.1))
conv3_biases = tf.get_variable(
"bias", [128], initializer=tf.constant_initializer(0.0))
conv3