alex
alexnet version 1
參考git上的大神代碼,自己學着一個cnn finetune的代碼。
代碼
代碼結構
name | usage |
---|---|
image_process.py | 將圖片做索引: path label 用於datagenerator讀取 |
datagenerator.py | 讀取圖片,reszie,反轉,banch 等基礎操作 |
alexnet.py | net 核心代碼 |
finetune | finetune 代碼 |
使用
1.生成圖片路徑和label
image_process.py :
- arg1 是訓練圖片位置
- arg2 是tain.txt 和 val.txt 存放路徑
- arg3 用於訓練的比例 剩下的交叉驗證
- Usage:
python image_process.py ../../data/dogvscat/train ../../data/dogvscat 0.8
train.txt 例子:
tensorlearn/data/dogvscat/train/dog.9528.jpg 1
tensorlearn/data/dogvscat/train/cat.9138.jpg 0
tensorlearn/data/dogvscat/train/cat.2848.jpg 0
2.finetune
finetune.py
設置:
# Path to the textfiles for the trainings and validation set
train_file = '../../data/dogvscat/train.txt'
val_file = '../../data/dogvscat/val.txt'
# Learning params
learning_rate = 0.01
num_epochs = 10
batch_size = 128
# Network params
dropout_rate = 0.5
num_classes = 2
train_layers = ['fc8', 'fc7']
# How often we want to write the tf.summary data to disk
display_step = 1
# Path for tf.summary.FileWriter and to store model checkpoints
filewriter_path = "../../data/checkpoint"
checkpoint_path = "../../data/filewriter"
3. tensorboard
運行 python finetune.py 會有提示
tensorboard --logdir ../../data/checkpoint --port 8080
訪問對應ip:port 就會有如下界面
TODO
- conf 設置參數
- fc訓練後的前向網絡
ALEX的結構
共8層,5層convolutional,3層full connected
.conv(11, 11, 96, 4, 4, padding='VALID', name='conv1')
.lrn(2, 2e-05, 0.75, name='norm1')
.max_pool(3, 3, 2, 2, padding='VALID', name='pool1')
.conv(5, 5, 256, 1, 1, group=2, name='conv2')
.lrn(2, 2e-05, 0.75, name='norm2')
.max_pool(3, 3, 2, 2, padding='VALID', name='pool2')
.conv(3, 3, 384, 1, 1, name='conv3')
.conv(3, 3, 384, 1, 1, group=2, name='conv4')
.conv(3, 3, 256, 1, 1, group=2, name='conv5')
.max_pool(3, 3, 2, 2, padding='VALID', name='pool5')
.fc(4096, name='fc6')
.fc(4096, name='fc7')
.fc(1000, relu=False, name='fc8')
.softmax(name='prob'))
LRN(local response normalization)
局部響應歸一化
公式
參數
$k=2,n=5,\alpha=10^{-4},\beta=0.75$
圖解
比較:響應歸一化將我們的top-1與top-5誤差率分別減少了1.4%與1.2%。我們也驗證了該方案在CIFAR-10數據集上的有效性:四層CNN不帶歸一化時的測試誤差率是13%,帶歸一化時是11%
減少過擬合
data Augmentation
- 256 取5個227片 水平反轉,共10個
- PCA:對於每個訓練圖像,我們成倍增加已有主成分,比例大小爲對應特徵值乘以一個從均值爲0,標準差爲0.1的高斯分佈中提取的隨機變量。
drop out
pass
訓練參數
動力爲0.9、權重衰減爲0.0005
當驗證誤差率在當前學習率下不再提高時,就將學習率除以10。學習率初始化爲0.01,在終止前降低三次。我們訓練該網絡時大致將這120萬張圖像的訓練集循環了90次,在兩個NVIDIA GTX 580 3GB GPU上花了五到六天。
定量評價
定性評價
GPU1上的核大多數顏色不明確,而GPU2上的核大多數顏色明確
(左圖)八個ILSVRC-2010測試圖像,以及被我們的模型認爲最有可能的五個標籤。正確的標籤寫在每個圖像下面,正確標籤的概率也以紅色條予以顯示(若它在前5之內)。(右圖)第一列是五個ILSVRC-2010測試圖像。其餘列顯示了六個訓練圖像,它們在最後的隱層產生的特徵向量與測試圖像的特徵向量有最小的歐氏距離。
reference
- https://kratzert.github.io/kratzert.github.io/2017/02/24/finetuning-alexnet-with-tensorflow.html
- http://hacker.duanshishi.com/?p=1661
- https://kratzert.github.io/kratzert.github.io/2017/02/24/finetuning-alexnet-with-tensorflow.html
代碼
https://github.com/chenlongzhen/tensorlearn/tree/master/script/finetune_alexnet_with_tensorflow