TensorFlow-Slim
TF-Slim是為了定義、訓(xùn)練和評估用tensorflow構(gòu)建的復(fù)雜模型而設(shè)計(jì)的一個(gè)輕量級庫炸裆。tf-slim中的組件可以自由的與原生tensorflow框架相結(jié)合,和其他框架一樣亚脆,例如tf.contrib.learn.
Usage:
import tensorflow.contrib.slim as slim
Why TF-Slim?
TF-Slim是一個(gè)輕量級庫讓構(gòu)建祷肯、訓(xùn)練和評估神經(jīng)網(wǎng)絡(luò)變得更簡單。
1市框、允許用戶用更簡潔的代碼構(gòu)建模型霞扬,這是通過使用argument scoping和高階的封裝layers
和variables來實(shí)現(xiàn)的。這些工具增加了代碼的可讀性和可維護(hù)性拾给,減少了賦值-粘貼超參數(shù)和超參數(shù)微調(diào)時(shí)出現(xiàn)錯(cuò)誤的可能性祥得。
2、通過了常用的regularizers使模型變的簡單蒋得。
3级及、一些經(jīng)典模型(比如 VGG, AlexNet)已經(jīng)在Slim中部署,傳送門available额衙。These can either be used as black boxes, or can be extended in various ways, e.g., by adding "multiple heads" to different internal layers.
4饮焦、Slim makes it easy to extend complex models, and to warm start training algorithms by using pieces of pre-existing model checkpoints.
What are the various components of TF-Slim?
TF-Slim是由一些相互獨(dú)立設(shè)計(jì)的組件組成,主要包括以下幾個(gè)點(diǎn)窍侧。
-
arg_scope: provides a new scope named
arg_scope
that allows a user to define default arguments for specific operations within that scope. - data: contains TF-slim's dataset definition, data providers, parallel_reader, and decoding utilities.
- evaluation: contains routines for evaluating models.
- layers: contains high level layers for building models using tensorflow.
- learning: contains routines for training models.
- losses: contains commonly used loss functions.
- metrics: contains popular evaluation metrics.
- nets: contains popular network definitions such as VGG and AlexNet models.
- queues: provides a context manager for easily and safely starting and closing QueueRunners.
- regularizers: contains weight regularizers.variables: provides convenience wrappers for variable creation and manipulation.
Defining Models
通過結(jié)合庫中的variables县踢、layers、scopes可以簡潔的定義模型伟件,詳細(xì)定義如下硼啤。
Variables
在原生tensorflow中創(chuàng)建變量需要預(yù)定義一個(gè)值或者一個(gè)初始化器(例如,從一個(gè)高斯分布中進(jìn)行隨機(jī)采樣)斧账。此外谴返,如果一個(gè)變量要在某個(gè)設(shè)備上創(chuàng)建,如GPU上咧织,是需要顯式設(shè)置的嗓袱。為了減少創(chuàng)建變量的代碼要求,TF-Slim提供了一系列封裝函數(shù)习绢,在variables.py中可以查看到渠抹,使調(diào)用者可以很輕易的創(chuàng)建變量。
比如創(chuàng)建一個(gè)權(quán)值變量,用truncated normal distribution初始化梧却,用l2_loss正則化權(quán)值并且是在CPU上創(chuàng)建奇颠,只需要聲明以下內(nèi)容:
weights = slim.variable('weights',
shape=[10, 10, 3 , 3],
initializer=tf.truncated_normal_initializer(stddev=0.1),
regularizer=slim.l2_regularizer(0.05),
device='/CPU:0')
在原生tensorflow中,有兩種類型的變量:常規(guī)變量和局部變量放航。大多數(shù)變量都是常規(guī)變量:一旦被創(chuàng)建大刊,就可以保存到磁盤上用saver。而局部變量僅僅是在會話期間并且沒有保存到磁盤上三椿。
TF-Slim進(jìn)一步定義了模型變量來區(qū)別不同類型的變量缺菌,模型變量就是指模型的參數(shù)。模型變量可以被訓(xùn)練和微調(diào)并且在評估和推斷時(shí)從checkpoint文件中加載出來搜锰。