Tensor flow小案例——06识别手势

首先训练并保存模型

这里保存模型的方法就是把权重和偏置保存到变量,载入的时候只载入了变量的值

保存变量的方法就是,以初值的形式把训练好的权重和偏置的值赋给新定义的变量,并保存新变量,即:

W1 = tf.Variable(dict_wb['w1'], dtype=tf.float32, name='W1')
W2 = tf.Variable(dict_wb['w2'], dtype=tf.float32, name='W2')
W3 = tf.Variable(dict_wb['w3'], dtype=tf.float32, name='W3')
B1 = tf.Variable(dict_wb['b1'], dtype=tf.float32, name='B1')
B2 = tf.Variable(dict_wb['b2'], dtype=tf.float32, name='B2')
B3 = tf.Variable(dict_wb['b3'], dtype=tf.float32, name='B3')

 

import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
import tf_utils
import time
import os

# 导入数据
X_train_orig, Y_train_orig, X_test_orig, Y_test_orig, classes = tf_utils.load_dataset()
'''
X_train_orig.shape (1080, 64, 64, 3)
Y_train_orig.shape (1, 1080) 
X_test_orig.shape  (120, 64, 64, 3) 
Y_test_orig.shape  (1, 120)
'''

# # 检测图片
# index = 11
# plt.imshow(X_train_orig[index])
# print("Y = " + str(np.squeeze(Y_train_orig[:, index])))
# plt.show()

# 扁平化处理
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

'''
X_train_flatten.shape (12288, 1080)
X_test_flatten.shape  (12288, 120)
'''

# 归一化处理
X_train = X_train_flatten / 255
X_test = X_test_flatten / 255

#转换为独热矩阵
Y_train = tf_utils.convert_to_one_hot(Y_train_orig, 6)
Y_test = tf_utils.convert_to_one_hot(Y_test_orig, 6)

def createPlaceholder(n_x, n_y):
    '''
    :param n_x: 一个输入的大小(64*64*3)
    :param n_y:一个输出的大小(6)
    :return:
    '''
    X = tf.placeholder(tf.float32, [n_x, None], name='X')
    Y = tf.placeholder(tf.float32, [n_y, None], name='Y')
    return X, Y

def initial_wb():
    """
        初始化神经网络的参数,参数的维度如下:
            W1 : [25, 12288]
            b1 : [25, 1]
            W2 : [12, 25]
            b2 : [12, 1]
            W3 : [6, 12]
            b3 : [6, 1]
        返回:
            parameters - 包含了W和b的字典
    """
    tf.set_random_seed(1)

    w1 = tf.Variable(np.random.randn(25*12288).reshape((25, 12288)) * 0.1, dtype=tf.float32)
    w2 = tf.Variable(np.random.randn(12*25).reshape((12, 25)) * 0.1, dtype=tf.float32)
    w3 = tf.Variable(np.random.randn(6*12).reshape((6, 12)) * 0.1, dtype=tf.float32)
    b1 = tf.Variable(np.zeros(25*1).reshape((25, 1)), dtype=tf.float32)
    b2 = tf.Variable(np.zeros(12*1).reshape((12, 1)), dtype=tf.float32)
    b3 = tf.Variable(np.zeros(6*1).reshape((6, 1)), dtype=tf.float32)

    Dict_wb = {
        'w1': w1,
        'w2': w2,
        'w3': w3,
        'b1': b1,
        'b2': b2,
        'b3': b3
    }
    return Dict_wb

def forward_propagation(X, dict_wb):
    '''
    向前传播
    '''
    w1 = dict_wb['w1']
    w2 = dict_wb['w2']
    w3 = dict_wb['w3']
    b1 = dict_wb['b1']
    b2 = dict_wb['b2']
    b3 = dict_wb['b3']

    z1 = tf.add(tf.matmul(w1, X), b1)
    a1 = tf.nn.relu(z1)
    z2 = tf.matmul(w2, a1) + b2
    a2 = tf.nn.relu(z2)
    z3 = tf.matmul(w3, a2) + b3
    return z3

def compute_cost(z3, Y):
    """
        计算成本
        参数:
            Z3 - 前向传播的结果
            Y - 标签,一个占位符,和Z3的维度相同
        返回:
            cost - 成本值
    """
    logits = tf.transpose(z3)  # 转置
    labels = tf.transpose(Y)  # 转置
    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=labels))
    return cost

# def model(X_train, Y_train, X_test, Y_test, learning_rate = 0.001,
#           num_iter = 1000, minibatch_size = 32, print_cost = True,is_plot = True):
#     """
#         实现一个三层的TensorFlow神经网络:LINEAR->RELU->LINEAR->RELU->LINEAR->SOFTMAX
#         参数:
#             X_train - 训练集,维度为(输入大小(输入节点数量) = 12288, 样本数量 = 1080)
#             Y_train - 训练集分类数量,维度为(输出大小(输出节点数量) = 6, 样本数量 = 1080)
#             X_test - 测试集,维度为(输入大小(输入节点数量) = 12288, 样本数量 = 120)
#             Y_test - 测试集分类数量,维度为(输出大小(输出节点数量) = 6, 样本数量 = 120)
#             learning_rate - 学习速率
#             num_iter - 整个训练集的遍历次数
#             mini_batch_size - 每个小批量数据集的大小
#             print_cost - 是否打印成本,每100代打印一次
#             is_plot - 是否绘制曲线图
#         返回:
#             dict_model - 学习后的参数
#     """
#
#     ops.reset_default_graph()  # 能够重新运行模型而不覆盖tf变量
#     (n_x, m) = X_train.shape
#     n_y = Y_train.shape[0]
#
#     costs=[]
#
#     X, Y = createPlaceholder(n_x, n_y)
#
#     dict_wb = initial_wb()
#
#     z3 = forward_propagation(X, dict_wb)
#
#     cost = compute_cost(z3, Y)
#
#     optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
#
#     init = tf.initialize_all_variables()
#
#     with tf.Session() as sess:
#         sess.run(init)
#
#         for i in range(num_iter):
#             sess.run([optimizer, cost], feed_dict={X: X_train, Y: Y_train})
#             if i % 10 == 0:
#                 print(i)
#
#         if is_plot:
#             plt.plot(np.squeeze(costs))
#             plt.ylabel('y')
#             plt.xlabel('x')
#             plt.title('title')
#             plt.show()
#
#         # save the model
#
#         # 计算当前的预测结果
#         correct_prediction = tf.equal(tf.argmax(z3), tf.argmax(Y))
#         # 计算准确率
#         accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
#         print("训练集的准确率:", accuracy.eval({X: X_train, Y: Y_train}))
#         print("测试集的准确率:", accuracy.eval({X: X_test, Y: Y_t

def model(X_train, Y_train, X_test, Y_test, learning_rate = 0.0005,
          num_iter = 1000, minibatch_size = 32, print_cost = True,is_plot = True):
    """
        实现一个三层的TensorFlow神经网络:LINEAR->RELU->LINEAR->RELU->LINEAR->SOFTMAX
        参数:
            X_train - 训练集,维度为(输入大小(输入节点数量) = 12288, 样本数量 = 1080)
            Y_train - 训练集分类数量,维度为(输出大小(输出节点数量) = 6, 样本数量 = 1080)
            X_test - 测试集,维度为(输入大小(输入节点数量) = 12288, 样本数量 = 120)
            Y_test - 测试集分类数量,维度为(输出大小(输出节点数量) = 6, 样本数量 = 120)
            learning_rate - 学习速率
            num_iter - 整个训练集的遍历次数
            mini_batch_size - 每个小批量数据集的大小
            print_cost - 是否打印成本,每100代打印一次
            is_plot - 是否绘制曲线图
        返回:
            dict_model - 学习后的参数
    """
    (n_x, m) = X_train.shape
    n_y = Y_train.shape[0]

    costs=[]

    X, Y = createPlaceholder(n_x, n_y)

    dict_wb = initial_wb()

    z3 = forward_propagation(X, dict_wb)

    cost = compute_cost(z3, Y)

    optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)

    init = tf.initialize_all_variables()

    with tf.Session() as sess:
        sess.run(init)

        for i in range(num_iter):
            iter_cost = 0

            num_minibatches = int(m / minibatch_size)

            minibatches = tf_utils.random_mini_batches(X_train, Y_train, minibatch_size)

            for minibatch in minibatches:
                (minibatch_X, minibatch_Y) = minibatch

                _, minibatch_cost = sess.run([optimizer, cost], feed_dict={X: minibatch_X, Y: minibatch_Y})

                # 计算这个minibatch在这一代中所占的误差
                iter_cost = iter_cost + minibatch_cost / num_minibatches

            if i % 5 == 0:
                costs.append(iter_cost)
                if print_cost and i % 10 == 0:
                    print("epoch = " + str(i) + "    epoch_cost = " + str(iter_cost))

        if is_plot:
            plt.plot(np.squeeze(costs))
            plt.ylabel('y')
            plt.xlabel('x')
            plt.title('title')
            plt.show()

        # 计算当前的预测结果
        correct_prediction = tf.equal(tf.argmax(z3), tf.argmax(Y))
        # 计算准确率
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
        print("训练集的准确率:", accuracy.eval({X: X_train, Y: Y_train}))
        print("测试集的准确率:", accuracy.eval({X: X_test, Y: Y_test}))

    return dict_wb

#开始时间
start_time = time.clock()

# #开始训练
dict_wb = initial_wb()
dict_wb = model(X_train, Y_train, X_test, Y_test)

W1 = tf.Variable(dict_wb['w1'], dtype=tf.float32, name='W1')
W2 = tf.Variable(dict_wb['w2'], dtype=tf.float32, name='W2')
W3 = tf.Variable(dict_wb['w3'], dtype=tf.float32, name='W3')
B1 = tf.Variable(dict_wb['b1'], dtype=tf.float32, name='B1')
B2 = tf.Variable(dict_wb['b2'], dtype=tf.float32, name='B2')
B3 = tf.Variable(dict_wb['b3'], dtype=tf.float32, name='B3')

init = tf.global_variables_initializer()

# 声明tf.train.Saver用于保存模型
saver = tf.train.Saver()

# 载入模型 & 测试
with tf.Session() as sess:
    sess.run(init)
    # save the model
    Path = '../Model/RecognitionGesture'
    if os.path.exists(Path):
        pass
    else:
        os.makedirs(Path)
    saver.save(sess, "../Model/RecognitionGesture/RecognitionGesture.ckpt")

#结束时间
end_time = time.clock()

#计算时差
print("CPU的执行时间 = " + str(end_time - start_time) + " 秒")

加载模型,也就是加载保存的参数:

注意,变量的形式一定要和保存之前的变量的形式一样

import numpy as np
import tensorflow as tf
from PIL import Image


w1 = tf.Variable(np.random.randn(25*12288).reshape((25, 12288)) * 0.1, dtype=tf.float32, name="W1")
w2 = tf.Variable(np.random.randn(12*25).reshape((12, 25)) * 0.1, dtype=tf.float32, name="W2")
w3 = tf.Variable(np.random.randn(6*12).reshape((6, 12)) * 0.1, dtype=tf.float32, name="W3")
b1 = tf.Variable(np.zeros(25*1).reshape((25, 1)), dtype=tf.float32, name="B1")
b2 = tf.Variable(np.zeros(12*1).reshape((12, 1)), dtype=tf.float32, name="B2")
b3 = tf.Variable(np.zeros(6*1).reshape((6, 1)), dtype=tf.float32, name="B3")


def predict(dict_wb, X):
    W1 = dict_wb['W1']
    b1 = dict_wb['B1']
    W2 = dict_wb['W2']
    b2 = dict_wb['B2']
    W3 = dict_wb['W3']
    b3 = dict_wb['B3']
    # Numpy Equivalents:
    Z1 = tf.add(tf.matmul(W1, X), b1)  # Z1 = np.dot(W1, X) + b1
    A1 = tf.nn.relu(Z1)  # A1 = relu(Z1)
    Z2 = tf.add(tf.matmul(W2, A1), b2)  # Z2 = np.dot(W2, a1) + b2
    A2 = tf.nn.relu(Z2)  # A2 = relu(Z2)
    Z3 = tf.add(tf.matmul(W3, A2), b3)  # Z3 = np.dot(W3,Z2) + b3
    return Z3


saver = tf.train.Saver()

with tf.Session() as sess:
    # 提取变量
    saver.restore(sess, "../Model/RecognitionGesture/RecognitionGesture.ckpt")
    root1 = '../Image/t13.png'  # cat
    root2 = '../Image/t11.png'  # dog
    root3 = '../Image/t12.png'  # deer
    Input = Image.open(root3)
    Input = Input.convert("RGB").resize((64, 64))
    InputArray = np.array(Input).reshape(-1, 1)
    InputArray = (InputArray) / 255

    print(InputArray)

    x = tf.placeholder("float", [12288, 1])

    dict_wb = {"W1": w1,
              "B1": b1,
              "W2": w2,
              "B2": b2,
              "W3": w3,
              "B3": b3}

    p = predict(dict_wb, x)
    # p = tf.argmax(p)
    print(sess.run(p, feed_dict={x: InputArray}))

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值