【TensoFlow学习笔记】项目篇【手势识别APP开发】(一)— —准备图片训练集

本文是TensorFlow学习笔记的第一部分,主要介绍了手势识别APP开发的数据准备工作,包括数据增强和数据预处理的详细步骤。通过INPUT_DATA.py文件,对图像进行预处理,为训练模型构建高质量的图片训练集。
摘要由CSDN通过智能技术生成

一.数据增强

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 
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值