????????在前面的文章 TensorFlow-slim 訓(xùn)練 CNN 分類(lèi)模型 我們已經(jīng)使用過(guò) tf.contrib.slim
模塊來(lái)構(gòu)建和訓(xùn)練模型了购对,今天我們繼續(xù)這一話(huà)題,但稍有不同的是我們不再定義數(shù)據(jù)輸入輸出的占位符恒削,而是使用 tf.contrib.slim
模塊來(lái)導(dǎo)入 tfrecord
數(shù)據(jù)進(jìn)行訓(xùn)練。這樣的訓(xùn)練方式主要有兩個(gè)優(yōu)點(diǎn):1.數(shù)據(jù)是并行讀取的凝危,而且并非一次性全部導(dǎo)入到內(nèi)存尉共,因此可以緩解內(nèi)存不足的問(wèn)題;2.使用 tf.contrib.slim
模塊的封裝函數(shù) slim.learning.train
來(lái)訓(xùn)練捧存,可以直接使用 Tensroboard 來(lái)監(jiān)督損失以及準(zhǔn)確率等曲線(xiàn)粪躬,還可以在中斷訓(xùn)練后繼續(xù)從上次保存的位置繼續(xù)進(jìn)行訓(xùn)練。
????????我們考慮的問(wèn)題仍然是 10 分類(lèi)由 Captcha 生成的數(shù)字昔穴,具體請(qǐng)?jiān)L問(wèn):TensorFlow 訓(xùn)練 CNN 分類(lèi)器镰官,使用的模型與文章 TensorFlow-slim 訓(xùn)練 CNN 分類(lèi)模型 的相同。為了方便閱讀吗货,再次將模型的代碼粘貼如下(命名為:model.py):
# -*- coding: utf-8 -*-
"""
Created on Fri Mar 30 16:54:02 2018
@author: shirhe-lyh
"""
import tensorflow as tf
from abc import ABCMeta
from abc import abstractmethod
slim = tf.contrib.slim
class BaseModel(object):
"""Abstract base class for any model."""
__metaclass__ = ABCMeta
def __init__(self, num_classes):
"""Constructor.
Args:
num_classes: Number of classes.
"""
self._num_classes = num_classes
@property
def num_classes(self):
return self._num_classes
@abstractmethod
def preprocess(self, inputs):
"""Input preprocessing. To be override by implementations.
Args:
inputs: A float32 tensor with shape [batch_size, height, width,
num_channels] representing a batch of images.
Returns:
preprocessed_inputs: A float32 tensor with shape [batch_size,
height, widht, num_channels] representing a batch of images.
"""
pass
@abstractmethod
def predict(self, preprocessed_inputs):
"""Predict prediction tensors from inputs tensor.
Outputs of this function can be passed to loss or postprocess functions.
Args:
preprocessed_inputs: A float32 tensor with shape [batch_size,
height, width, num_channels] representing a batch of images.
Returns:
prediction_dict: A dictionary holding prediction tensors to be
passed to the Loss or Postprocess functions.
"""
pass
@abstractmethod
def postprocess(self, prediction_dict, **params):
"""Convert predicted output tensors to final forms.
Args:
prediction_dict: A dictionary holding prediction tensors.
**params: Additional keyword arguments for specific implementations
of specified models.
Returns:
A dictionary containing the postprocessed results.
"""
pass
@abstractmethod
def loss(self, prediction_dict, groundtruth_lists):
"""Compute scalar loss tensors with respect to provided groundtruth.
Args:
prediction_dict: A dictionary holding prediction tensors.
groundtruth_lists: A list of tensors holding groundtruth
information, with one entry for each image in the batch.
Returns:
A dictionary mapping strings (loss names) to scalar tensors
representing loss values.
"""
pass
class Model(BaseModel):
"""A simple 10-classification CNN model definition."""
def __init__(self,
is_training,
num_classes):
"""Constructor.
Args:
is_training: A boolean indicating whether the training version of
computation graph should be constructed.
num_classes: Number of classes.
"""
super(Model, self).__init__(num_classes=num_classes)
self._is_training = is_training
def preprocess(self, inputs):
"""Predict prediction tensors from inputs tensor.
Outputs of this function can be passed to loss or postprocess functions.
Args:
preprocessed_inputs: A float32 tensor with shape [batch_size,
height, width, num_channels] representing a batch of images.
Returns:
prediction_dict: A dictionary holding prediction tensors to be
passed to the Loss or Postprocess functions.
"""
preprocessed_inputs = tf.to_float(inputs)
preprocessed_inputs = tf.subtract(preprocessed_inputs, 128.0)
preprocessed_inputs = tf.div(preprocessed_inputs, 128.0)
return preprocessed_inputs
def predict(self, preprocessed_inputs):
"""Predict prediction tensors from inputs tensor.
Outputs of this function can be passed to loss or postprocess functions.
Args:
preprocessed_inputs: A float32 tensor with shape [batch_size,
height, width, num_channels] representing a batch of images.
Returns:
prediction_dict: A dictionary holding prediction tensors to be
passed to the Loss or Postprocess functions.
"""
with slim.arg_scope([slim.conv2d, slim.fully_connected],
activation_fn=tf.nn.relu):
net = preprocessed_inputs
net = slim.repeat(net, 2, slim.conv2d, 32, [3, 3], scope='conv1')
net = slim.max_pool2d(net, [2, 2], scope='pool1')
net = slim.repeat(net, 2, slim.conv2d, 64, [3, 3], scope='conv2')
net = slim.max_pool2d(net, [2, 2], scope='pool2')
net = slim.repeat(net, 2, slim.conv2d, 128, [3, 3], scope='conv3')
net = slim.flatten(net, scope='flatten')
net = slim.dropout(net, keep_prob=0.5,
is_training=self._is_training)
net = slim.fully_connected(net, 512, scope='fc1')
net = slim.fully_connected(net, 512, scope='fc2')
net = slim.fully_connected(net, self.num_classes,
activation_fn=None, scope='fc3')
prediction_dict = {'logits': net}
return prediction_dict
def postprocess(self, prediction_dict):
"""Convert predicted output tensors to final forms.
Args:
prediction_dict: A dictionary holding prediction tensors.
**params: Additional keyword arguments for specific implementations
of specified models.
Returns:
A dictionary containing the postprocessed results.
"""
logits = prediction_dict['logits']
logits = tf.nn.softmax(logits)
classes = tf.cast(tf.argmax(logits, axis=1), dtype=tf.int32)
postprecessed_dict = {'classes': classes}
return postprecessed_dict
def loss(self, prediction_dict, groundtruth_lists):
"""Compute scalar loss tensors with respect to provided groundtruth.
Args:
prediction_dict: A dictionary holding prediction tensors.
groundtruth_lists: A list of tensors holding groundtruth
information, with one entry for each image in the batch.
Returns:
A dictionary mapping strings (loss names) to scalar tensors
representing loss values.
"""
logits = prediction_dict['logits']
loss = tf.reduce_mean(
tf.nn.sparse_softmax_cross_entropy_with_logits(
logits=logits, labels=groundtruth_lists))
loss_dict = {'loss': loss}
return loss_dict
????????下面重點(diǎn)說(shuō)明怎么使用 tf.contrib.slim
模塊來(lái)訓(xùn)練泳唠。本文所有代碼見(jiàn) github: slim_cnn_test。
一宙搬、slim.learning.train 訓(xùn)練 CNN 模型
????????我們已經(jīng)知道通過(guò)定義數(shù)據(jù)入口笨腥、出口的占位符 tf.placeholder
可以對(duì)上面定義的模型進(jìn)行訓(xùn)練,但現(xiàn)在我們的目標(biāo)是全部使用 tf.contrib.slim
來(lái)進(jìn)行托管式的訓(xùn)練勇垛。要完成這個(gè)目標(biāo)脖母,需要借助兩個(gè)函數(shù)的輔助,分別是上一篇文章 TensorFlow 自定義生成 .record 文件 定義的 get_record_dataset
函數(shù)和 slim 模塊封裝的 slim.learning.train
函數(shù)闲孤,前者用于獲取訓(xùn)練數(shù)據(jù)谆级,后者則執(zhí)行對(duì)模型的訓(xùn)練。以下崭放,先將訓(xùn)練用的代碼列舉出來(lái)(命名為 train.py):
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Mar 30 19:27:44 2018
@author: shirhe-lyh
"""
"""Train a CNN model to classifying 10 digits.
Example Usage:
---------------
python3 train.py \
--record_path: Path to training tfrecord file.
--logdir: Path to log directory.
"""
import tensorflow as tf
import model
slim = tf.contrib.slim
flags = tf.app.flags
flags.DEFINE_string('record_path', None, 'Path to training tfrecord file.')
flags.DEFINE_string('logdir', None, 'Path to log directory.')
FLAGS = flags.FLAGS
def get_record_dataset(record_path,
reader=None, image_shape=[28, 28, 3],
num_samples=50000, num_classes=10):
"""Get a tensorflow record file.
Args:
"""
if not reader:
reader = tf.TFRecordReader
keys_to_features = {
'image/encoded':
tf.FixedLenFeature((), tf.string, default_value=''),
'image/format':
tf.FixedLenFeature((), tf.string, default_value='jpeg'),
'image/class/label':
tf.FixedLenFeature([1], tf.int64, default_value=tf.zeros([1],
dtype=tf.int64))}
items_to_handlers = {
'image': slim.tfexample_decoder.Image(shape=image_shape,
#image_key='image/encoded',
#format_key='image/format',
channels=3),
'label': slim.tfexample_decoder.Tensor('image/class/label', shape=[])}
decoder = slim.tfexample_decoder.TFExampleDecoder(
keys_to_features, items_to_handlers)
labels_to_names = None
items_to_descriptions = {
'image': 'An image with shape image_shape.',
'label': 'A single integer between 0 and 9.'}
return slim.dataset.Dataset(
data_sources=record_path,
reader=reader,
decoder=decoder,
num_samples=num_samples,
num_classes=num_classes,
items_to_descriptions=items_to_descriptions,
labels_to_names=labels_to_names)
def main(_):
dataset = get_record_dataset(FLAGS.record_path)
data_provider = slim.dataset_data_provider.DatasetDataProvider(dataset)
image, label = data_provider.get(['image', 'label'])
inputs, labels = tf.train.batch([image, label],
batch_size=64,
allow_smaller_final_batch=True)
cls_model = model.Model(is_training=True, num_classes=10)
preprocessed_inputs = cls_model.preprocess(inputs)
prediction_dict = cls_model.predict(preprocessed_inputs)
loss_dict = cls_model.loss(prediction_dict, labels)
loss = loss_dict['loss']
postprocessed_dict = cls_model.postprocess(prediction_dict)
classes = postprocessed_dict['classes']
acc = tf.reduce_mean(tf.cast(tf.equal(classes, labels), 'float'))
tf.summary.scalar('loss', loss)
tf.summary.scalar('accuracy', acc)
optimizer = tf.train.MomentumOptimizer(learning_rate=0.01, momentum=0.9)
train_op = slim.learning.create_train_op(loss, optimizer,
summarize_gradients=True)
slim.learning.train(train_op=train_op, logdir=FLAGS.logdir,
save_summaries_secs=20, save_interval_secs=120)
if __name__ == '__main__':
tf.app.run()
????????進(jìn)行訓(xùn)練時(shí)哨苛,我們先根據(jù)上一篇文章的方式將所有訓(xùn)練圖像(以及相應(yīng)的標(biāo)簽)寫(xiě)成 tfrecord 文件鸽凶,這一步如果訓(xùn)練數(shù)據(jù)足夠多的話(huà)會(huì)耗費(fèi)較長(zhǎng)時(shí)間币砂,但可以方便后續(xù)的數(shù)據(jù)讀取(讀取數(shù)據(jù)很快)玻侥。然后决摧,我們接著上一篇文章的后一部分,要把這些數(shù)據(jù)讀出來(lái)喂給模型。我們已經(jīng)定義好了函數(shù) get_record_dataset
掌桩,它讀取 .record
文件并返回一個(gè) slim.dataset.Dataset
類(lèi)對(duì)象边锁。此時(shí),用 slim.dataset_data_provider.DatasetDataProvider 作用一下波岛,便可以用該類(lèi)的函數(shù) .get 來(lái)方便的將數(shù)據(jù)并行的提取出來(lái)(見(jiàn)上一篇文章)茅坛。因?yàn)椴⑿械木壒剩看畏祷氐氖菃螐垐D像则拷,這時(shí)你可以對(duì)圖像進(jìn)行非批量的預(yù)處理贡蓖,也可以直接使用 tf.train.batch
來(lái)生成指定大小的一個(gè)批量然后進(jìn)行批量預(yù)處理。
????????一旦將訓(xùn)練數(shù)據(jù)提取出來(lái)煌茬,我們就可以把它們傳給模型了(如上述代碼 main 函數(shù)中間片段)斥铺,最后的兩句 tf.summary.scalar
表示把損失和準(zhǔn)確率寫(xiě)入到訓(xùn)練日志文件,方便之后我們?cè)?tensorboard 中觀察損失坛善、準(zhǔn)確率曲線(xiàn)晾蜘。然后,再聲明使用動(dòng)量的隨機(jī)梯度下降優(yōu)化算法眠屎,緊接著便來(lái)到了訓(xùn)練的最后一步:
slim.learning.train(train_op=train_op, logdir=FLAGS.logdir,
save_summaries_secs=20, save_interval_secs=True)
這個(gè)函數(shù)將所有訓(xùn)練的迭代過(guò)程都封裝起來(lái)剔交,包括日志文件書(shū)寫(xiě)和模型保存。函數(shù)
slim.learning.train(train_op, logdir, train_step_fn=train_step,
train_step_kwargs=_USE_DEFAULT,
log_every_n_steps=1, graph=None, master='',
is_chief=True, global_step=None,
number_of_steps=None, init_op=_USE_DEFAULT,
init_feed_dict=None, local_init_op=_USE_DEFAULT,
init_fn=None, ready_op=_USE_DEFAULT,
summary_op=_USE_DEFAULT,
save_summaried_secs=600,
summary_writer=_USE_DEFAULT,
startup_delay_steps=0, saver=None,
save_interval_secs=600, sync_optimizer=None,
session_config=None, session_wrapper=None,
trace_every_n_steps=None,
ignore_live_threads=False)
參數(shù)眾多组力,其中重要的有:1.train_op省容,指定優(yōu)化算法;2.logdir燎字,指定訓(xùn)練數(shù)據(jù)保存文件夾腥椒;3.save_summaries_secs,指定每隔多少秒更新一次日志文件(對(duì)應(yīng) tensorboard 刷新一次的時(shí)間)候衍;4.save_interval_secs笼蛛,指定每隔多少秒保存一次模型。
????????注意這段代碼并沒(méi)有定義數(shù)據(jù)傳入的占位符蛉鹿,因此模型訓(xùn)練完成之后滨砍,我們并不知道怎么用,囧妖异。不過(guò)惋戏,沒(méi)關(guān)系,我們先訓(xùn)練完再說(shuō)他膳,在終端執(zhí)行:
python3 train.py --record_path path/to/.record --logdir path_to_log_directory
會(huì)在 logdir 指定的路徑下生成多個(gè)訓(xùn)練文件响逢,而且每隔 save_interval_secs 會(huì)自動(dòng)更新這些文件。當(dāng)覺(jué)得模型訓(xùn)練到可以終止的時(shí)候棕孙,可以使用 Ctrl + C 來(lái)強(qiáng)制終止舔亭,或者通過(guò)指定參數(shù) number_of_steps 來(lái)終止些膨。而當(dāng)覺(jué)得有必要對(duì)所訓(xùn)練的模型繼續(xù)進(jìn)行訓(xùn)練時(shí),重新執(zhí)行上述命令即可钦铺。如果你想查看訓(xùn)練的損失和準(zhǔn)確率變化情況订雾,在終端使用命令:
tensorboard --logdir path_to_log_directory
即可,至于查看的時(shí)間可以在訓(xùn)練過(guò)程中矛洞,也可以在訓(xùn)練結(jié)束后洼哎。
????????好了,有關(guān)使用 tf.contrib.slim
模塊來(lái)進(jìn)行模型訓(xùn)練的內(nèi)容就講完了沼本,接下來(lái)還要解決一個(gè)重要的問(wèn)題:沒(méi)有定義占位符谱净,我們?cè)趺凑{(diào)用模型進(jìn)行推斷?
二擅威、自定義模型導(dǎo)出(.pb 格式)
????????上面訓(xùn)練的模型會(huì)在 logdir 目錄下保存為 .ckpt 格式的文件壕探, 但一個(gè)致命的問(wèn)題是沒(méi)有數(shù)據(jù)入口,不知如何用它來(lái)對(duì)圖像進(jìn)行分類(lèi)郊丛。為此李请,需要人為的添加數(shù)據(jù)入口、出口的結(jié)點(diǎn)厉熟。
????????要達(dá)到這個(gè)目的导盅,我們需要仔細(xì)的研究一下下面這個(gè)用于模型導(dǎo)出的文件:
然后基于該文件做部分修改,用來(lái)導(dǎo)出我們的模型揍瑟。請(qǐng)期待下一篇文章白翻,我們將完成這一任務(wù)。
預(yù)告:下一篇文章將詳細(xì)說(shuō)明自定義的將 .ckpt 文件導(dǎo)出為 .pb 文件绢片,敬請(qǐng)期待滤馍。