Tensorflow训练识别自定义图片

很多正在入门或刚入门TensorFlow机器学习的同学希望能够通过自己指定图片源对模型进行训练,然后识别和分类自己指定的图片。但是,在TensorFlow官方入门教程中,并无明确给出如何把自定义数据输入训练模型的方法。现在,我们就参考官方入门课程《Deep MNIST for Experts》一节的内容(传送门:https://www.tensorflow.org/get_started/mnist/pros),介绍如何将自定义图片输入到TensorFlow的训练模型。

在《Deep MNISTfor Experts》一节的代码中,程序将TensorFlow自带的mnist图片数据集mnist.train.images作为训练输入,将mnist.test.images作为验证输入。当学习了该节内容后,我们会惊叹卷积神经网络的超高识别率,但对于刚开始学习TensorFlow的同学,内心可能会产生一个问号:如何将mnist数据集替换为自己指定的图片源?譬如,我要将图片源改为自己C盘里面的图片,应该怎么调整代码?

我们先看下该节课程中涉及到mnist图片调用的代码:

from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets(‘MNIST_data‘, one_hot=True)
batch = mnist.train.next_batch(50)
train_accuracy = accuracy.eval(feed_dict={x: batch[0], y_: batch[1], keep_prob: 1.0})
train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})
print(‘test accuracy %g‘ % accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}))

要实现输入自定义图片,需要自己先准备好一套图片集。为节省时间,我们把mnist的手写体数字集一张一张地解析出来,存放到自己的本地硬盘,保存为bmp格式,然后再把本地硬盘的手写体图片一张一张地读取出来,组成集合,再输入神经网络。mnist手写体数字集的提取方式详见《如何从TensorFlow的mnist数据集导出手写体数字图片》。

将mnist手写体数字集导出图片到本地后,就可以仿照以下python代码,实现自定义图片的训练:

import os

import numpy as np
import tensorflow as tf

from PIL import Image

# 第一次遍历图片目录是为了获取图片总数
input_count = 0
for i in range(0, 10):
    dir = ‘./mnist_digits_images/%s/‘ % i  # 这里可以改成你自己的图片目录,i为分类标签
    for rt, dirs, files in os.walk(dir):
        for filename in files:
            input_count += 1

# 定义对应维数和各维长度的数组
input_images = np.array([[0] * 784 for i in range(input_count)])
input_labels = np.array([[0] * 10 for i in range(input_count)])

# 第二次遍历图片目录是为了生成图片数据和标签
index = 0
for i in range(0, 10):
    dir = ‘./mnist_digits_images/%s/‘ % i  # 这里可以改成你自己的图片目录,i为分类标签
    for rt, dirs, files in os.walk(dir):
        for filename in files:
            filename = dir + filename
            img = Image.open(filename)
            width = img.size[0]
            height = img.size[1]
            for h in range(0, height):
                for w in range(0, width):
                    # 通过这样的处理,使数字的线条变细,有利于提高识别准确率
                    if img.getpixel((w, h)) > 230:
                        input_images[index][w + h * width] = 0  # 之前已经将图片转换成了一维
                    else:
                        input_images[index][w + h * width] = 1
            input_labels[index][i] = 1
            index += 1

# 定义输入节点,对应于图片像素值矩阵集合和图片标签(即所代表的数字)
x = tf.placeholder(tf.float32, shape=[None, 784])
y_ = tf.placeholder(tf.float32, shape=[None, 10])

x_image = tf.reshape(x, [-1, 28, 28, 1])

# 定义第一个卷积层的variables和ops
W_conv1 = tf.Variable(tf.truncated_normal([7, 7, 1, 32], stddev=0.1))
b_conv1 = tf.Variable(tf.constant(0.1, shape=[32]))

L1_conv = tf.nn.conv2d(x_image, W_conv1, strides=[1, 1, 1, 1], padding=‘SAME‘)
L1_relu = tf.nn.relu(L1_conv + b_conv1)
L1_pool = tf.nn.max_pool(L1_relu, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding=‘SAME‘)

# 定义第二个卷积层的variables和ops
W_conv2 = tf.Variable(tf.truncated_normal([3, 3, 32, 64], stddev=0.1))
b_conv2 = tf.Variable(tf.constant(0.1, shape=[64]))

L2_conv = tf.nn.conv2d(L1_pool, W_conv2, strides=[1, 1, 1, 1], padding=‘SAME‘)
L2_relu = tf.nn.relu(L2_conv + b_conv2)
L2_pool = tf.nn.max_pool(L2_relu, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding=‘SAME‘)

# 全连接层
W_fc1 = tf.Variable(tf.truncated_normal([7 * 7 * 64, 1024], stddev=0.1))
b_fc1 = tf.Variable(tf.constant(0.1, shape=[1024]))

h_pool2_flat = tf.reshape(L2_pool, [-1, 7 * 7 * 64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)

# dropout
keep_prob = tf.placeholder(tf.float32)
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)

# readout层
W_fc2 = tf.Variable(tf.truncated_normal([1024, 10], stddev=0.1))
b_fc2 = tf.Variable(tf.constant(0.1, shape=[10]))

y_conv = tf.matmul(h_fc1_drop, W_fc2) + b_fc2

# 定义优化器和训练op
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y_conv))
train_step = tf.train.AdamOptimizer((1e-4)).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

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

    print("一共读取了 %s 个输入图像, %s 个标签" % (input_count, input_count))

    # 设置每次训练op的输入个数和迭代次数,这里为了支持任意图片总数,定义了一个余数remainder,譬如,如果每次训练op的输入个数为60,图片总数为150张,则前面两次各输入60张,最后一次输入30张(余数30)
    batch_size = 60
    iterations = 100
    batches_count = int(input_count / batch_size)
    remainder = input_count % batch_size
    print("数据集分成 %s 批, 前面每批 %s 个数据,最后一批 %s 个数据" % (batches_count + 1, batch_size, remainder))

    # 执行训练迭代
    for it in range(iterations):
        # 这里的关键是要把输入数组转为np.array
        for n in range(batches_count):
            train_step.run(feed_dict={x: input_images[n * batch_size:(n + 1) * batch_size],
                                      y_: input_labels[n * batch_size:(n + 1) * batch_size], keep_prob: 0.5})
        if remainder > 0:
            start_index = batches_count * batch_size;
            train_step.run(
                feed_dict={x: input_images[start_index:input_count - 1], y_: input_labels[start_index:input_count - 1],
                           keep_prob: 0.5})

        # 每完成五次迭代,判断准确度是否已达到100%,达到则退出迭代循环
        iterate_accuracy = 0
        if it % 5 == 0:
            iterate_accuracy = accuracy.eval(feed_dict={x: input_images, y_: input_labels, keep_prob: 1.0})
            print(‘iteration %d: accuracy %s‘ % (it, iterate_accuracy))
            if iterate_accuracy >= 1:
                break;

    print(‘完成训练!‘)

上述python代码的执行结果截图如下:

对于上述代码中与模型构建相关的代码,请查阅官方《Deep MNIST for Experts》一节的内容进行理解。在本文中,需要重点掌握的是如何将本地图片源整合成为feed_dict可接受的格式。其中最关键的是这两行:

# 定义对应维数和各维长度的数组
input_images = np.array([[0]*784 for i in range(input_count)])
input_labels = np.array([[0]*10 for i in range(input_count)])

它们对应于feed_dict的两个placeholder:

x = tf.placeholder(tf.float32, shape=[None, 784])
y_ = tf.placeholder(tf.float32, shape=[None, 10])

转自https://www.jb51.net/article/166937.htm

原文地址:https://www.cnblogs.com/answerThe/p/11453458.html

时间: 2024-07-29 20:49:24

Tensorflow训练识别自定义图片的相关文章

tensorflow训练自己的数据集实现CNN图像分类

利用卷积神经网络训练图像数据分为以下几个步骤 读取图片文件 产生用于训练的批次 定义训练的模型(包括初始化参数,卷积.池化层等参数.网络) 训练 1 读取图片文件 1 def get_files(filename): 2 class_train = [] 3 label_train = [] 4 for train_class in os.listdir(filename): 5 for pic in os.listdir(filename+train_class): 6 class_train

百度地图上自定义图片覆盖物上加点击事件

在百度地图上加自定义图片覆盖物之后,在这个覆盖物上加点击手势,发现并不识别.原因不太清楚.不过可以通过别的方法来实现这个功能. 需要用到BMKMapViewDelegate中的方法: - (void)mapView:(BMKMapView *)mapView onClickedMapBlank:(CLLocationCoordinate2D)coordinate 然后判断所点击的坐标是否在指定区域内,在指定区域内之后,调用自己写的相应地方法即可. 百度地图上自定义图片覆盖物上加点击事件,布布扣,

Python 实现识别弱图片验证码

目前,很多网站为了防止爬虫肆意模拟浏览器登录,采用增加验证码的方式来拦截爬虫.验证码的形式有多种,最常见的就是图片验证码.其他验证码的形式有音频验证码,滑动验证码等.图片验证码越来越高级,识别难度也大幅提高,就算人为输入也经常会输错.本文主要讲解识别弱图片验证码. 1 图片验证码强度 图片验证码主要采用加干扰线.字符粘连.字符扭曲方式来增强识别难度. 加干扰线 加干扰线也分为两种,一种是线条跟字符同等颜色,另一种则线条的颜色是五颜六色. 字符粘连 各个字符之间的间隔比较小,互相依靠,能以分割.

目标检测 的标注数据 .xml 转为 tfrecord 的格式用于 TensorFlow 训练

将目标检测 的标注数据 .xml 转为 tfrecord 的格式用于 TensorFlow 训练. import xml.etree.ElementTree as ET import numpy as np import os import tensorflow as tf from PIL import Image classes = ["aeroplane", "bicycle", "bird", "boat", &quo

自定义图片的progressbar

Android系统自带的Progressbar的样式是固定的,当想使用自定义的进度条时,也很简单 首先在布局文件中加入进度条 <ProgressBar android:id="@+id/progressBar1" style="?android:attr/progressBarStyleHorizontal" android:layout_width="fill_parent" android:progressDrawable="

【问题收集·中级】关于指示器自定义图片与UUID

博友问题: 大哥 求教一下 iOS7 能否获取到 uuid 大哥 忙不忙 iOS的加载的时候 动态旋转效果 是 图片 嘛 ? 我的回答 05:43:34hud指示器我用的是这个MBProgressHUD,我见你用过hud,估计你是想改自定义动态旋转的效果吧 MMProgressHUD这个可以自定义,去github上搜下就能搜到可以获得uuid我的回答 05:51:37不能在程序中用的是udid.我的回答 05:53:01但是uuid不稳定我的回答 05:53:06新安装程序后两次获取的标识符不一

Android drawable 玩转自定义图片以及bug的解决

很久没有空更新博客了,以至于挺多东西都用过之后就忘记了,没有很好的记录下来,之前在工作的时候也是这样,用完就忘记,所以觉得还是很有必要把自己用过的一些东西,解决的一些问题记录下来的,所以以后尽量坚持一周写一次博客,记录一下自己解决的问题,也与大学共享一下,建议大家也写一下博客或笔记什么的,因为在工作中,自己接触的东西并不可能只是自己刚开始的东西,比如说Android,其实在开发一个app或平时在公司工作的时候,还需要用到很多的东西,而且还有可能有一段时间去使用别的语言去开发,如果自己不记录一下,

Button 文字阴影,自定义图片,代码绘制样式,添加音效的方法

1.Button自己在xml文件中绑定监听器 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent&qu

Mono自定义图片按钮

首先,我们编写一个MyImageButton类,继承自LinearLayout public class MyPhoneImageButton:LinearLayout { private ImageView mButtonImage = null; private TextView mButtonText = null; public MyPhoneImageButton (Context context) : base (context) { mButtonImage = new Image