106
技術社區[雲棲]
【前沿】TensorFlow Pytorch Keras代碼實現深度學習大神Hinton NIPS2017 Capsule論文
10月26日,深度學習元老Hinton的NIPS2017 Capsule論文《Dynamic Routing Between Capsules》終於在arxiv上發表。今天相關關於這篇論文的TensorFlow\Pytorch\Keras實現相繼開源出來,讓我們來看下。
論文地址:https://arxiv.org/pdf/1710.09829.pdf
Capsule 是一組神經元,其活動向量(activity vector)表示特定實體類型的實例化參數,如對象或對象部分。我們使用活動向量的長度表征實體存在的概率,向量方向表示實例化參數。同一水平的活躍 capsule 通過變換矩陣對更高級別的 capsule 的實例化參數進行預測。當多個預測相同時,更高級別的 capsule 變得活躍。我們展示了判別式訓練的多層 capsule 係統在 MNIST 數據集上達到了最好的性能效果,比識別高度重疊數字的卷積網絡的性能優越很多。為了達到這些結果,我們使用迭代的路由協議機製:較低級別的 capsule 偏向於將輸出發送至高級別的 capsule,有了來自低級別 capsule 的預測,高級別 capsule 的活動向量具備較大的標量積。
python依賴包
- Python 3
- PyTorch
- TorchVision
- TorchNet
- TQDM
- Visdom
使用說明
第一步 在capsule_network.py
文件中設置訓練epochs,batch size等
BATCH_SIZE = 100NUM_CLASSES = 10NUM_EPOCHS = 30NUM_ROUTING_ITERATIONS = 3
Step 2 開始訓練. 如果本地文件夾中沒有MNIST數據集,將運行腳本自動下載到本地. 確保 PyTorch可視化工具Visdom正在運行。
$ sudo python3 -m visdom.server & python3 capsule_network.py
基準數據
經過30個epoche的訓練手寫體數字的識別率達到99.48%. 從下圖的訓練進度和損失圖的趨勢來看,這一識別率可以被進一步的提高 。
采用了PyTorch中默認的Adam梯度優化參數並沒有用到動態學習率的調整。 batch size 使用100個樣本的時候,在雷蛇GTX 1050 GPU上每個Epochs 用時3分鍾。
待完成
- 擴展到除MNIST以外的其他數據集。
Credits
主要借鑒了以下兩個 TensorFlow 和 Keras 的實現:
- Keras implementation by @XifengGuo
- TensorFlow implementation by @naturomics
Many thanks to @InnerPeace-Wu for a discussion on the dynamic routing procedure outlined in the paper.
Python依賴包
- Python
- NumPy
- Tensorflow (I'm using 1.3.0, not yet tested for older version)
- tqdm (for displaying training progress info)
- scipy (for saving image)
使用說明
訓練
第一步 用git命令下載代碼到本地.
$ git clone https://github.com/naturomics/CapsNet-Tensorflow.git
$ cd CapsNet-Tensorflow
第二步 下載MNIST數據集(https://yann.lecun.com/exdb/mnist/), 移動並解壓到data/mnist
文件夾(當你用複製wget
命令到你的終端是注意渠道花括號裏的反斜杠)
$ mkdir -p data/mnist
$ wget -c -P data/mnist https://yann.lecun.com/exdb/mnist/{train-images-idx3-ubyte.gz,train-labels-idx1-ubyte.gz,t10k-images-idx3-ubyte.gz,t10k-labels-idx1-ubyte.gz}
$ gunzip data/mnist/*.gz
第三步 開始訓練:
$ pip install tqdm # install it if you haven't installed yet
$ python train.py
tqdm包並不是必須的,隻是為了可視化訓練過程。如果你不想要在train.py
中將循環for in step ...
改成 ``for step in range(num_batch)就行了。
評估
$ python eval.py --is_training False
結果
錯誤的運行結果(Details in Issues #8):
- training loss


- test acc
Results after fixing Issues #8:
關於capsule的一點見解
- 一種新的神經單元(輸入向量輸出向量,而不是標量)
- 常規算法類似於Attention機製
- 總之是一項很有潛力的工作,有很多工作可以在之上開展
待辦:
- 完成MNIST的實現Finish the MNIST version of capsNet (progress:90%)
- 在其他數據集上驗證capsNet
- 調整模型結構
- 一篇新的投稿在ICLR2018上的後續論文(https://openreview.net/pdf?id=HJWLfGWRb) about capsules(submitted to ICLR 2018)
依賴包
- Keras
- matplotlib
使用方法
訓練
第一步 安裝 Keras:
$ pip install keras
第二步 用 git
命令下載代碼到本地.
$ git clone https://github.com/xifengguo/CapsNet-Keras.git
$ cd CapsNet-Keras
第三步 訓練:
$ python capsulenet.py
一次迭代訓練(default 3).
$ python capsulenet.py --num_routing 1
其他參數包括想 batch_size, epochs, lam_recon, shift_fraction, save_dir
可以以同樣的方式使用。 具體可以參考 capsulenet.py
測試
假設你已經有了用上麵命令訓練好的模型,訓練模型將被保存在 result/trained_model.h5
. 現在隻需要使用下麵的命令來得到測試結果。
$ python capsulenet.py --is_training 0 --weights result/trained_model.h5
將會輸出測試結果並顯示出重構後的圖片。測試數據使用的和驗證集一樣 ,同樣也可以很方便的在新數據上驗證,至於要按照你的需要修改下代碼就行了。
如果你的電腦沒有GPU來訓練模型,你可以從https://pan.baidu.com/s/1hsF2bvY下載預先訓練好的訓練模型
結果
主要結果
運行 python capsulenet.py
: epoch=1 代表訓練一個epoch 後的結果 在保存的日誌文件中,epoch從0開始。
損失和準確度:
一次常規迭代後的結果
運行 python CapsNet.py --num_routing 1
測試結果每個 epoch 在單卡GTX 1070 GPU上大概需要110s
注釋: 訓練任然是欠擬合的,歡迎在你自己的機器上驗證。學習率decay還沒有經過調試, 我隻是試了一次,你可以接續微調。
運行 python capsulenet.py --is_training 0 --weights result/trained_model.h5
模型結構:
- Kaggle (this version as self-contained notebook):
- MNIST Dataset running on the standard MNIST and predicting for test data
- MNIST Fashion running on the more challenging Fashion images.
-
TensorFlow:
- naturomics/CapsNet-Tensorflow
- Very good implementation. I referred to this repository in my code.
- InnerPeace-Wu/CapsNet-tensorflow
- I referred to the use of tf.scan when optimizing my CapsuleLayer.
- LaoDar/tf_CapsNet_simple
-
PyTorch:
- nishnik/CapsNet-PyTorch
- timomernick/pytorch-capsule
- gram-ai/capsule-networks
- andreaazzini/capsnet.pytorch
- leftthomas/CapsNet
-
MXNet:
- AaronLeong/CapsNet_Mxnet
- Lasagne (Theano):
- DeniskaMazur/CapsNet-Lasagne
- Chainer:
- soskek/dynamic_routing_between_capsules
https://github.com/gram-ai/capsule-networks
https://github.com/naturomics/CapsNet-Tensorflow
https://github.com/XifengGuo/CapsNet-Keras
原文發布時間為:2017-11-5
本文來自雲棲社區合作夥伴新智元,了解相關信息可以關注“AI_era”微信公眾號
原文鏈接:【前沿】TensorFlow Pytorch Keras代碼實現深度學習大神Hinton NIPS2017 Capsule論文
最後更新:2017-11-06 10:33:49