from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import tensorflow as tf
slim = tf.contrib.slim
def ttnet(images, num_classes=10, is_training=False,
dropout_keep_prob=0.5,
prediction_fn=slim.softmax,
scope='TtNet'):
end_points = {}
with tf.variable_scope(scope, 'TtNet', [images, num_classes]):
net = slim.conv2d(images, 32, [3, 3], scope='conv1')
# net = slim.conv2d(images, 64, [3, 3], scope='conv1_2')
net = slim.max_pool2d(net, [2, 2], 2, scope='pool1')
net = slim.batch_norm(net, activation_fn=tf.nn.relu, scope='bn1')
# net = slim.conv2d(net, 128, [3, 3], scope='conv2_1')
net = slim.conv2d(net, 64, [3, 3], scope='conv2')
net = slim.max_pool2d(net, [2, 2], 2, scope='pool2')
net = slim.conv2d(net, 128, [3, 3], scope='conv3')
net = slim.max_pool2d(net, [2, 2], 2, scope='pool3')
net = slim.conv2d(net, 256, [3, 3], scope='conv4')
net = slim.max_pool2d(net, [2, 2], 2, scope='pool4')
net = slim.batch_norm(net, activation_fn=tf.nn.relu, scope='bn2')
# net = slim.conv2d(net, 512, [3, 3], scope='conv5')
# net = slim.max_pool2d(net, [2, 2], 2, scope='pool5')
net = slim.flatten(net)
end_points['Flatten'] = net
# net = slim.fully_connected(net, 1024, scope='fc3')
net = slim.dropout(net, dropout_keep_prob, is_training=is_training,
scope='dropout3')
logits = slim.fully_connected(net, num_classes, activation_fn=None,
scope='fc4')
end_points['Logits'] = logits
end_points['Predictions'] = prediction_fn(logits, scope='Predictions')
return logits, end_points
ttnet.default_image_size = 28
def ttnet_arg_scope(weight_decay=0.0):
with slim.arg_scope(
[slim.conv2d, slim.fully_connected],
weights_regularizer=slim.l2_regularizer(weight_decay),
weights_initializer=tf.truncated_normal_initializer(stddev=0.1),
activation_fn=tf.nn.relu) as sc:
return sc
基于slim,由于是一个比较简单的分类问题,网络结构也很简单,几个卷积加池化。
测试效果是很棒的。真实样本测试集能达到99%+的准确率。
2.模型固化,生成pb文件
#coding:utf-8
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import tensorflow as tf
from nets import nets_factory
import cv2
import os
import numpy as np
from datasets import dataset_factory
from preprocessing import preprocessing_factory
from tensorflow.python.platform import gfile
slim = tf.contrib.slim
#todo
#support arbitray image size and num_class
tf.app.flags.DEFINE_string(
'checkpoint_path', '/tmp/tfmodel/',
'The directory where the model was written to or an absolute path to a '
'checkpoint file.')
tf.app.flags.DEFINE_string(
'model_name', 'inception_v3', 'The name of the architecture to evaluate.')
tf.app.flags.DEFINE_string(
'preprocessing_name', None, 'The name of the preprocessing to use. If left '
'as `None`, then the model_name flag is used.')
FLAGS = tf.app.flags.FLAGS
tf.app.flags.DEFINE_integer(
'eval_image_size', None, 'Eval image size')
tf.app.flags.DEFINE_integer(
'eval_image_height', None, 'Eval image height')
tf.app.flags.DEFINE_integer(
'eval_image_width', None, 'Eval image width')
tf.app.flags.DEFINE_string(
'export_path', './ttnet_1.0_37_32.pb', 'the export path of the pd file')
FLAGS = tf.app.flags.FLAGS
NUM_CLASSES = 37
def main(_):
network_fn = nets_factory.get_network_fn(
FLAGS.model_name,
num_classes=NUM_CLASSES,
is_training=False)
# pre_image = tf.placeholder(tf.float32, [None, None, 3], name='input_data')
# preprocessing_name = FLAGS.preprocessing_name or FLAGS.model_name
# image_preprocessing_fn = preprocessing_factory.get_preprocessing(
# preprocessing_name,
# is_training=False)
# image = image_preprocessing_fn(pre_image, FLAGS.eval_image_height, FLAGS.eval_image_width)
# images2 = tf.expand_dims(image, 0)
images2 = tf.placeholder(tf.float32, (None,32, 32, 3),name='input_data')
logits, endpoints = network_fn(images2)
with tf.Session() as sess:
output = tf.identity(endpoints['Predictions'],name="output_data")
with gfile.GFile(FLAGS.export_path, 'wb') as f:
f.write(sess.graph_def.SerializeToString())
if __name__ == '__main__':
tf.app.run()