文章写的不清晰请大家原谅QAQ
这篇文章我们将用 CIFAR-10数据集做一个很简易的图片分类器。 在 CIFAR-10数据集包含了60,000张图片。在此数据集中,有10个不同的类别,每个类别中有6,000个图像。每幅图像的大小为32 x 32像素。虽然这么小的尺寸通常给人类识别正确的类别带来了困难,但它实际上是对计算机模型的简化并且减少了分析图像所需的计算。
CIFAR-10数据集
我们可以通过输入模型的大量数字序列将这些图像输入到我们的模型中。每个像素由三个浮点数标识,这三个浮点数表示该像素的红色,绿色和蓝色值(RGB值)。所以每个图像有32 x 32 x 3 = 3,072 个值0.
使用非常大的卷积神经网络可以实现高质量的结果,你可以在这个连接中学习Rodrigo Benenson’s page
下载CIFAR-10数据集,网址:Python version of the dataset, 并把他安装在我们分类器代码所在的文件夹下
先上源代码
模型的源代码:
import numpy as np import tensorflow as tf import time import data_helpers beginTime = time.time() batch_size = 100 learning_rate = 0.005 max_steps = 1000 data_sets = data_helpers.load_data() # Define input placeholders images_placeholder = tf.placeholder(tf.float32, shape=[None, 3072]) labels_placeholder = tf.placeholder(tf.int64, shape=[None]) # Define variables (these are the values we want to optimize) weights = tf.Variable(tf.zeros([3072, 10])) biases = tf.Variable(tf.zeros([10])) # Define the classifier‘s result logits = tf.matmul(images_placeholder, weights) + biases # Define the loss function loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=labels_placeholder)) # Define the training operation train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss) # Operation comparing prediction with true label correct_prediction = tf.equal(tf.argmax(logits, 1), labels_placeholder) # Operation calculating the accuracy of our predictions accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) with tf.Session() as sess: # Initialize variables sess.run(tf.global_variables_initializer()) # Repeat max_steps times for i in range(max_steps): # Generate input data batch indices = np.random.choice(data_sets[‘images_train‘].shape[0], batch_size) images_batch = data_sets[‘images_train‘][indices] labels_batch = data_sets[‘labels_train‘][indices] # Periodically print out the model‘s current accuracy if i % 100 == 0: train_accuracy = sess.run(accuracy, feed_dict={ images_placeholder: images_batch, labels_placeholder: labels_batch}) print(‘Step {:5d}: training accuracy {:g}‘.format(i, train_accuracy)) # Perform a single training step sess.run(train_step, feed_dict={images_placeholder: images_batch, labels_placeholder: labels_batch}) # After finishing the training, evaluate on the test set test_accuracy = sess.run(accuracy, feed_dict={ images_placeholder: data_sets[‘images_test‘], labels_placeholder: data_sets[‘labels_test‘]}) print(‘Test accuracy {:g}‘.format(test_accuracy)) endTime = time.time() print(‘Total time: {:5.2f}s‘.format(endTime - beginTime))
处理数据集的代码
import numpy as np import pickle import sys def load_CIFAR10_batch(filename): ‘‘‘load data from single CIFAR-10 file‘‘‘ with open(filename, ‘rb‘) as f: if sys.version_info[0] < 3: dict = pickle.load(f) else: dict = pickle.load(f, encoding=‘latin1‘) x = dict[‘data‘] y = dict[‘labels‘] x = x.astype(float) y = np.array(y) return x, y def load_data(): ‘‘‘load all CIFAR-10 data and merge training batches‘‘‘ xs = [] ys = [] for i in range(1, 6): filename = ‘cifar-10-batches-py/data_batch_‘ + str(i) X, Y = load_CIFAR10_batch(filename) xs.append(X) ys.append(Y) x_train = np.concatenate(xs) y_train = np.concatenate(ys) del xs, ys x_test, y_test = load_CIFAR10_batch(‘cifar-10-batches-py/test_batch‘) classes = [‘plane‘, ‘car‘, ‘bird‘, ‘cat‘, ‘deer‘, ‘dog‘, ‘frog‘, ‘horse‘, ‘ship‘, ‘truck‘] # Normalize Data mean_image = np.mean(x_train, axis=0) x_train -= mean_image x_test -= mean_image data_dict = { ‘images_train‘: x_train, ‘labels_train‘: y_train, ‘images_test‘: x_test, ‘labels_test‘: y_test, ‘classes‘: classes } return data_dict def reshape_data(data_dict): im_tr = np.array(data_dict[‘images_train‘]) im_tr = np.reshape(im_tr, (-1, 3, 32, 32)) im_tr = np.transpose(im_tr, (0, 2, 3, 1)) data_dict[‘images_train‘] = im_tr im_te = np.array(data_dict[‘images_test‘]) im_te = np.reshape(im_te, (-1, 3, 32, 32)) im_te = np.transpose(im_te, (0, 2, 3, 1)) data_dict[‘images_test‘] = im_te return data_dict def gen_batch(data, batch_size, num_iter): data = np.array(data) index = len(data) for i in range(num_iter): index += batch_size if (index + batch_size > len(data)): index = 0 shuffled_indices = np.random.permutation(np.arange(len(data))) data = data[shuffled_indices] yield data[index:index + batch_size] def main(): data_sets = load_data() print(data_sets[‘images_train‘].shape) print(data_sets[‘labels_train‘].shape) print(data_sets[‘images_test‘].shape) print(data_sets[‘labels_test‘].shape) if __name__ == ‘__main__‘: main()
首先我们导入了tensorflow numpy time 以及自己写的data_help包
time是为了计算整个代码的运行时间。 data_help是将数据集做成我们训练用的数据结构
data_help中的load_data()会把60000张的CIFAR数据集分成两块:500000张的训练集和100000张的测试集,具体来说他会返回这样的一个包含如下内容的字典
images_train
: 训练集。一个500000张 包含3072(32x32像素点x3颜色通道)值labels_train
: 训练集的50,000个标签(每个标签在0到9之间,代表训练图像所属的10个类别中的哪一个)images_test
: 测试集(10,000 by 3,072)labels_test
: 测试集的10,000个标签classes
: 10个文本标签,用于将数字类值转换为单词(例如0代表‘plane‘,1代表‘car‘)
原文地址:https://www.cnblogs.com/francischeng/p/9833201.html