TensorFlow-slim 訓(xùn)練 CNN 分類(lèi)模型(續(xù))

????????在前面的文章 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)出的文件:

TensorFlow models/research/object_detection/export.py

然后基于該文件做部分修改,用來(lái)導(dǎo)出我們的模型揍瑟。請(qǐng)期待下一篇文章白翻,我們將完成這一任務(wù)。

預(yù)告:下一篇文章將詳細(xì)說(shuō)明自定義的將 .ckpt 文件導(dǎo)出為 .pb 文件绢片,敬請(qǐng)期待滤馍。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市底循,隨后出現(xiàn)的幾起案子巢株,更是在濱河造成了極大的恐慌,老刑警劉巖熙涤,帶你破解...
    沈念sama閱讀 218,640評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件阁苞,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡祠挫,警方通過(guò)查閱死者的電腦和手機(jī)那槽,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,254評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)等舔,“玉大人骚灸,你說(shuō)我怎么就攤上這事∪硐梗” “怎么了逢唤?”我有些...
    開(kāi)封第一講書(shū)人閱讀 165,011評(píng)論 0 355
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)涤浇。 經(jīng)常有香客問(wèn)我鳖藕,道長(zhǎng),這世上最難降的妖魔是什么只锭? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,755評(píng)論 1 294
  • 正文 為了忘掉前任著恩,我火速辦了婚禮,結(jié)果婚禮上蜻展,老公的妹妹穿的比我還像新娘喉誊。我一直安慰自己,他們只是感情好纵顾,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,774評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布伍茄。 她就那樣靜靜地躺著,像睡著了一般施逾。 火紅的嫁衣襯著肌膚如雪敷矫。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,610評(píng)論 1 305
  • 那天汉额,我揣著相機(jī)與錄音曹仗,去河邊找鬼。 笑死蠕搜,一個(gè)胖子當(dāng)著我的面吹牛怎茫,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播妓灌,決...
    沈念sama閱讀 40,352評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼轨蛤,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了虫埂?” 一聲冷哼從身側(cè)響起俱萍,我...
    開(kāi)封第一講書(shū)人閱讀 39,257評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎告丢,沒(méi)想到半個(gè)月后枪蘑,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,717評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡岖免,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,894評(píng)論 3 336
  • 正文 我和宋清朗相戀三年岳颇,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片颅湘。...
    茶點(diǎn)故事閱讀 40,021評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡话侧,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出闯参,到底是詐尸還是另有隱情瞻鹏,我是刑警寧澤悲立,帶...
    沈念sama閱讀 35,735評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站新博,受9級(jí)特大地震影響薪夕,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜赫悄,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,354評(píng)論 3 330
  • 文/蒙蒙 一原献、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧埂淮,春花似錦姑隅、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,936評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至痪蝇,卻和暖如春叮盘,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背霹俺。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,054評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工柔吼, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人丙唧。 一個(gè)月前我還...
    沈念sama閱讀 48,224評(píng)論 3 371
  • 正文 我出身青樓愈魏,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親想际。 傳聞我的和親對(duì)象是個(gè)殘疾皇子培漏,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,974評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容