The examples of image recognition with the dataset CIFAR10 via tensorflow.
CIFAR-10数据集是机器学习中的一个通用的用于图像识别的基础数据集,官网链接为:The CIFAR-10 dataset
下载使用的版本是:
将其解压后(代码中包含自动解压代码),内容为:
主要代码及作用:
文件 | 作用 |
---|---|
cifar10_input.py |
读取本地或者在线下载CIFAR-10的二进制文件格式数据集 |
cifar10.py |
建立CIFAR-10的模型 |
cifar10_train.py |
在CPU或GPU上训练CIFAR-10的模型 |
cifar10_multi_gpu_train.py |
在多个GPU上训练CIFAR-10的模型 |
cifar10_eval.py |
评估CIFAR-10模型的预测性能 |
$ $
该部分的代码,介绍了如何使用TensorFlow在CPU和GPU上训练和评估卷积神经网络(convolutional neural network, CNN)。
更加详细地介绍说明,请浏览网页:Convolutional Neural Networks
中文网站极客学院也有该部分的汉译版:卷积神经网络
代码源自tensorflow官网:tensorflow/models/image/cifar10
GitHub公布代码相对源码,主要进行了以下修正:
cifar10.py
#indices = tf.reshape(tf.range(FLAGS.batch_size), [FLAGS.batch_size, 1])
indices = tf.reshape(range(FLAGS.batch_size), [FLAGS.batch_size, 1])
# or
indices = tf.reshape(tf.range(0, FLAGS.batch_size, 1), [FLAGS.batch_size, 1])
此处,源码编译时会出现以下错误:
...
File ".../cifar10.py", line 271, in loss
indices = tf.reshape(tf.range(FLAGS.batch_size), [FLAGS.batch_size, 1])
TypeError: range() takes at least 2 arguments (1 given)
cifar10_input_test.py
#self.assertEqual("%s:%d" % (filename, i), tf.compat.as_text(key))
import compat as cp
...
self.assertEqual("%s:%d" % (filename, i), cp.as_text(key))
不然的话,我测试的时候就会出现这的错误:
AttributeError: 'module' object has no attribute 'compat'
cifar10_train.py
和cifar10_multi_gpu_train.py
源代码里的最大迭代次数max_steps
为1000000
,如使用CPU训练需要好几个小时,推荐大家使用GPU训练,GPU训练环境安装教程。