13 用卷积神经网络进行图像识别

卷积层的具体计算过程

13.1 conv1
程序:

import numpy as np

xData1 = np.array([[0, 0], [0, 1]], dtype=np.float32)

xData2 = np.array([[0, 1], [1, 0]], dtype=np.float32)

filterT = np.array([[1, 2], [3, 4]], dtype=np.float32)

y = np.sum(xData1 * filterT)
print(y)

print(np.sum(xData2 * filterT))

结果:

4.0
5.0

卷积层的原理和优点

13.2 conv2
程序:

import tensorflow as tf

xData = tf.constant([[[0],[0],[1],[1],[0],[0]], [[0],[1],[0],[0],[1],[0]], [[0],[0],[0],[0],[1],[0]] , [[0],[0],[0],[1],[0],[0]] , [[0],[0],[1],[0],[0],[0]] , [[0],[1],[1],[1],[1],[0]]], dtype=tf.float32)

filterT = tf.constant([[1, 2], [3, 4]], dtype=tf.float32)
#conv2d(一次要处理多少张图片,输入图片点阵的高,输入图片点阵的宽,图片彩色的通道数)  strides卷积核移动不常  padding 卷积方式
y = tf.nn.conv2d(input=tf.reshape(xData, [1, 6, 6, 1]), filter=tf.reshape(filterT, [2, 2, 1, 1]), strides=[1, 1, 1, 1], padding='VALID')

sess = tf.Session()

result = sess.run(y)

print(result)

结果:

[[[[4.]
   [5.]
   [3.]
   [5.]
   [3.]]

  [[2.]
   [1.]
   [0.]
   [6.]
   [4.]]

  [[0.]
   [0.]
   [4.]
   [5.]
   [1.]]

  [[0.]
   [4.]
   [5.]
   [1.]
   [0.]]

  [[4.]
   [9.]
   [8.]
   [7.]
   [3.]]]]

设计钩叉问题的神经网络模型并实现

13.3 conv3

test0712.py 程序:

import tensorflow as tf
import numpy as np
import pandas as pd
import sys


roundCount = 100
learnRate = 0.01

argt = sys.argv[1:]

for v in argt:
    if v.startswith("-round="):
        roundCount = int(v[len("-round="):])
    if v.startswith("-learnrate="):
        learnRate = float(v[len("-learnrate="):])


fileData = pd.read_csv('checkData.txt', dtype=np.float32, header=None)

wholeData = fileData.as_matrix()

rowCount = wholeData.shape[0]

print("wholeData=%s" % wholeData)
print("rowCount=%d" % rowCount)

x = tf.placeholder(shape=[25], dtype=tf.float32)
yTrain = tf.placeholder(shape=[3], dtype=tf.float32)

filter1T = tf.Variable(tf.ones([2, 2, 1, 1]), dtype=tf.float32)

n1 = tf.nn.conv2d(input=tf.reshape(x, [1, 5, 5, 1]), filter=filter1T, strides=[1, 1, 1, 1], padding='SAME')

filter2T = tf.Variable(tf.ones([2, 2, 1, 1]), dtype=tf.float32)

n2 = tf.nn.conv2d(input=tf.reshape(n1, [1, 5, 5, 1]), filter=filter2T, strides=[1, 1, 1, 1], padding='VALID')

filter3T = tf.Variable(tf.ones([2, 2, 1, 1]), dtype=tf.float32)

n3 = tf.nn.conv2d(input=tf.reshape(n2, [1, 4, 4, 1]), filter=filter3T, strides=[1, 1, 1, 1], padding='VALID')

n3f = tf.reshape(n3, [1, 9])

w4 = tf.Variable(tf.random_normal([9, 16]), dtype=tf.float32)
b4 = tf.Variable(0, dtype=tf.float32)

n4 = tf.nn.tanh(tf.matmul(n3f, w4) + b4)

w5 = tf.Variable(tf.random_normal([16, 3]), dtype=tf.float32)
b5 = tf.Variable(0, dtype=tf.float32)

n5 = tf.reshape(tf.matmul(n4, w5) + b5, [-1])

y = tf.nn.softmax(n5)

loss = -tf.reduce_mean(yTrain * tf.log(tf.clip_by_value(y, 1e-10, 1.0)))
optimizer = tf.train.RMSPropOptimizer(learnRate)

train = optimizer.minimize(loss)

sess = tf.Session()
sess.run(tf.global_variables_initializer())

for i in range(roundCount):
    lossSum = 0.0

    for j in range(rowCount):
        result = sess.run([train, x, yTrain, y, loss], feed_dict={x: wholeData[j][0:25], yTrain: wholeData[j][25:28]})

        lossT = float(result[len(result) - 1])

        lossSum = lossSum + lossT

        if j == (rowCount - 1):
            print("i: %d, loss: %10.10f, avgLoss: %10.10f" % (i, lossT, lossSum / (rowCount + 1)))

print(sess.run([y, loss], feed_dict={x: [1,0,0,0,1, 0,1,0,1,0, 0,0,1,0,0, 0,0,0,0,0, 0,0,0,0,0], yTrain: [1,0,0]}))
print(sess.run([y, loss], feed_dict={x: [1,0,0,0,1, 0,1,0,1,0, 0,0,1,0,0, 0,1,0,1,0, 1,0,0,0,1], yTrain: [0,1,0]}))
print(sess.run([y, loss], feed_dict={x: [0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 1,1,1,1,1, 0,0,0,0,0], yTrain: [0,0,1]}))

代码当前目录下命令行输入:

python test0712.py -round=10000 -learnrate=0.0001
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值