利用tensorflow的CNN教程和google在udacity上的课程所使用的Not_MNIST数据集,也算第一次用代码实现了一个CNN模型。因为 MNIST和not_MNIST的数据很像,都是28x28的图,所以基本上没改训练模型代码。除了加载数据那里,以及一些教程上落后于库版本的地方,还有windows8.1不得不设置的地方,其他代码,都是完全照着打下来。值得注意的地方是2g显存的GTX960m的显卡,没有办法对整个Not_MNIST的测试集做测试,所以我只选了前一部分做测试。
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from six.moves import cPickle as pickle
import tensorflow as tf
from tensorflow.contrib import learn
from tensorflow.contrib.learn.python.learn.estimators import model_fn as model_fn_lib
tf.logging.set_verbosity(tf.logging.INFO)
def cnn_model_fn(fearures, labels, mode):
input_layer = tf.reshape(fearures, [-1, 28, 28, 1])
conv1 = tf.layers.conv2d(
inputs=input_layer,
filters=32,
kernel_size=[5, 5],
padding='same',
activation=tf.nn.relu)
pool1 = tf.layers.max_pooling2d(inputs=conv1, pool_size=[2, 2], strides=2)
conv2 = tf.layers.conv2d(
inputs=pool1,
filters=64,
kernel_size=[5, 5],
padding='same',
activation=tf.nn.relu)
pool2 = tf.layers.max_pooling2d(inputs=conv2, pool_size=[2, 2], strides=2)
pool2_flat = tf.reshape(pool2, [-1, 7 * 7 * 64])
dense = tf.layers.dense(inputs=pool2_flat, units=1024, activation=tf.nn.relu)
dropout = tf.layers.dropout(inputs=dense, rate=0.4, training=mode == learn.ModeKeys.TRAIN)
logits = tf.layers.dense(inputs=dropout, units=10)
loss = None
train_op = None
if mode != learn.ModeKeys.INFER:
onehot_labels = tf.one_hot(indices=tf.cast(labels, tf.int32), depth=10)
loss = tf.losses.softmax_cross_entropy(onehot_labels=onehot_labels, logits=logits)
if mode == learn.ModeKeys.TRAIN:
train_op = tf.contrib.layers.optimize_loss(
loss=loss,
global_step=tf.contrib.framework.get_global_step(),
learning_rate=0.001,
optimizer='SGD'
)
predictions = {
'class': tf.argmax(input=logits, axis=1),
'probabilities': tf.nn.softmax(logits, name='softmax_tensor')
}
return model_fn_lib.ModelFnOps(mode=mode, predictions=predictions, loss=loss, train_op=train_op)
def main(unused_argv):
# dataload
pkl_file = open(r'F:\ML\notMNIST.pickle', 'rb')
p = pickle.load(pkl_file)
train_dataset, train_labels = p['train_dataset'], p['train_labels']
test_dataset, test_labels = p['test_dataset'][:900], p['test_labels'][:900]
mnist_classifier = learn.Estimator(
model_fn=cnn_model_fn, model_dir='/tmp/not_mnist_convnet_model'
)
tensors_to_log = {'probabilities': 'softmax_tensor'}
logging_hook = tf.train.LoggingTensorHook(tensors=tensors_to_log, every_n_iter=50)
mnist_classifier.fit(x=train_dataset, y=train_labels, batch_size=10, steps=20000, monitors=[logging_hook])
metrics = {
'accuray':
learn.MetricSpec(
metric_fn=tf.metrics.accuracy,prediction_key='class'
)
}
eval_results = mnist_classifier.evaluate(
x=test_dataset, y=test_labels, metrics=metrics
)
print(eval_results);
if __name__ == "__main__":
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
session = tf.Session(config=config)
tf.app.run()