手把手教你基於torch玩轉
\html
\python
\git
做者:驍哲、李偉、小蔡、July。
說明:本教程出自七月在線助教團隊、及七月在線深度學習在線班學員之手,有何問題歡迎加Q羣交流:472899334。且探究實驗背後原理,請參看:深度學習在線班。
時間:二零一六年十月十二日。\github
\算法
\編程
咱們教梵高做畫的教程發佈以後,國慶7天,上百位朋友一一陸續動手嘗試,大有全民DL、全民實驗之感。特別是來自DL班的小蔡同窗,國慶7天連作10個開源實驗,並把這10個實驗的簡易教程(含自動聊天機器人)發佈在社區上:ask.julyedu.com/explore/cat…。盛讚。json
爲了讓每個人(是的,每個人,博客、教程、課程無不如此)都能玩一把,本教程特在小蔡簡易教程的基礎上從新整理,側重torch環境的搭建(由於根據咱們的經驗,環境一旦搭好,作實驗基本一馬平川),此外全部能想到的、能作到的、能寫上的(甚至一個sudo –i命令)都已詳盡細緻的寫出來,爲的就是讓每個人都能玩一把,無限下降初學朋友的實驗門檻。ubuntu
仍是那句話,歡迎更多朋友跟咱們一塊兒作實驗,一塊兒玩。包括學梵高做畫的7個實驗:梵高做畫、文字生成、自動聊天機器人、圖像着色、圖像生成、看圖說話、字幕生成,今2016年內,只要你作出這7個實驗中的任意一個並在微博上AT@研究者July,便送100上課券,把實驗心得發社區 ask.julyed.com 後,再送100上課券。c#
另,咱們更會在深度學習在線班上詳解實驗背後的原理,讓君知其然更知其因此然。後端
\
\
Torch是一個有大量機器學習算法支持的科學計算框架,其誕生已經有十年之久,可是真正起勢得益於Facebook開源了大量Torch的深度學習模塊和擴展。Torch另一個特殊之處是採用了編程語言Lua(該語言曾被用來開發視頻遊戲)。
Torch的優點:
*信息來源--www.leiphone.com/news/201608…
本次搭建是在Ubuntu14.04基礎上搭建,Ubuntu14.04系統安裝教程已在Tensorflow實驗中分享,還不清楚的同窗,先回顧上次實驗內容:教你從頭至尾利用DL學梵高做畫:GTX 1070 cuda 8.0 tensorflow gpu版
(1) 文字生成
(2) 自動聊天
(3) 圖像着色
(4) 圖像生成
(5) 看圖說話
(6) 字幕生成
\
\
l 參考github網址: https://github.com/torch/torch7
l 搭建torch7網址:http://torch.ch/docs/getting-started.html
步驟:
1、 筆者假設讀者已經安裝完成NVIDIA的GPU驅動以及CUDA、cudnn,若還未安裝的,請參考教你從頭至尾利用DL學梵高做畫:GTX 1070 cuda 8.0 tensorflow gpu版
2、 ubuntu終端窗口輸入:
如下全部命令均在root用戶下執行
apt-get update (更新源)
3、 打開搭建torch7網址
\
git clone github.com/torch/distr… ~/torch --recursive (克隆torch到~/torch文件下)
cd ~/torch; bash install-deps; (執行install-deps)
./install.sh (執行程序)
\
source ~/.bashrc (Ubuntu14.04通常狀況執行這個,更新.bashrc文件)
source ~/.zshrc (讀者不放心了把這個也執行了)
*******若是讀者用Lua5.2就執行以下,沒有就跳過*******
\
第一個git忽略,開始搭建時候已經下載過了
cd ~/torch (進入torch文件)
./clean.sh (執行clean.sh)
TORCH_LUA_VERSION=LUA52 ./install.sh(執行命令)
*****************結束*************************
\
luarocks install image (安裝image)
luarocks list (列出luarocks安裝的包、檢查是否安裝成功)
\
th (測試可否用torch7,出現如上圖標誌,表示能用)
4、 筆者在安裝過程當中出現torch7的環境變量未能添加到PATH內。解決辦法以下:
\
在終端輸入:vi /etc/profile
進入文件後,在最後添加以下命令:
PATH=~/torch/install/bin:$PATH
\
按Esc 接着輸入 : q 退出
執行 source /etc/profile (更新一下)
\
\
參考教程地址:https://ask.julyedu.com/question/7405
參考課程:https://www.julyedu.com/video/play/18/130
參考github:https://github.com/karpathy/char-rnn
l 下載包
luarocks install nngraph
luarocks install optim
luarocks install nn
l 若是用GPU,安裝以下包
luarocks install cutorch
luarocks install cunn
l 下載char-rnn包
git clone --recursive https://github.com/karpathy/char-rnn****
cd char-rnn;
l 自行下載你想要生成類型的模板(.txt文件),例如唐詩三百首、汪峯歌詞、韓寒小說……
l 利用cp命令和mv命令,把下載好的.txt文件覆蓋data/tinyshakespeare下的input.txt
l 訓練
th train.lua --dataset 20000 --hiddenSize 100 -data_dir data/tinyshakespeare -rnn_size 512 -num_layers 2 -dropout 0.5
l 生成
th sample.lua cv/lm_lstm_epoch(按住Tab自動補全) -gpuid -1(-gpuid -1這個是僅適用CPU的命令,GPU的同窗自行忽略)
\
\
\
參考教程地址:https://ask.julyedu.com/question/7410(本教程基本參考以上地址內容,你們能夠直接進去查看)
參考課程:七月在線深度學習課程
參考github:github.com/rustch3n/ch…
l 環境包下載
sudo ~/torch/install/bin/luarocks install nn
sudo ~/torch/install/bin/luarocks install rnn
sudo ~/torch/install/bin/luarocks install async
l 下載代碼與語料****
git clone --recursive https://github.com/rustcbf/chatbot-zh-torch7 #代碼
git clone --recursive https://github.com/rustcbf/dgk_lost_conv #語料
git clone --recursive https://github.com/chenb67/neuralconvo #以上兩個在此源碼進行改進,可做爲參考
l 語料選擇****
語料除了上述提供的語料,可自行生成本身的語料
cd dgk_lost_conv #參考cvgen.py
如需查看語料內容
python toraw.py a.cov b.txt
cd chatbot-zh-torch7
筆者原先直接使用xiaohuangji50w_fenciA.conv(估計是小黃雞聊天機器人語料,50w條數據),後來訓練時間以爲太長,換了做者提供的小樣本。
更改樣本的修改cornell_movie_dialogs.lua 第18行代碼,建議先不修改,由於筆者在做者提供的小樣本下效果不是很好,數據應該沒有通過處理。自行下載你想要生成類型的模板( .txt文件),例如唐詩三百首、汪峯歌詞、韓寒小說……
l 訓練
th train.lua (筆者實驗時提示內存不夠,所以輸入命令爲th train.lua --dataset 20000 --hiddenSize 100 )#可加參數--cuda、--opencl、--hiddenSize等等
在 data 文件夾生成有examples.t7,model.t七、vocab.t7
l 開始
修改eval.lua
在源碼後邊添加\
print("\nType a sentence and hit enter to submit.")
print("CTRL+C then enter to quit.\n")
while true do
io.write("you> ")
io.flush()
io.write(say(io.read()))
end
th eval.lua #直接命令行
一開始用64G內存的服務器跑50w語料,跑完後,發現效果還湊合
\
但若是換成普通臺式機跑50w語料的話,可能麻煩就來了。由於訓練過程當中發現臺式機的8G內存不夠,因而又加了8g內存,但即使是16g內存仍是不夠,最好只好捨棄部分語料,換成20w的語料,可正由於語料減小,訓練出的聊天機器人效果就不如先前50w語料訓練出的效果好了,可能會逼你出口成髒。
\
\
參考教程地址: ask.julyedu.com/question/74…
參考github:https://github.com/satoshiiizuka/siggraph2016_colorization
l 環境包下載
sudo ~/torch/install/bin/luarocks install nn
sudo ~/torch/install/bin/luarocks install image
sudo ~/torch/install/bin/luarocks install nngraph
l 下載模型
./download_model.sh
l 執行
th colorize.lua ***(黑白圖片地址) ***(生成圖片存放地址)
示例:th colorize.lua ansel_colorado_1941.png ansel_colorado_1941_out.png
\
\
\
參考教程地址: ask.julyedu.com/question/74…
參考github:https://github.com/soumith/dcgan.torch
l 環境包下載
sudo ~/torch/install/bin/luarocks install optnet
sudo ~/torch/install/bin/luarocks install display
sudo ~/torch/install/bin/luarocks install cudnn(GPU執行)
sudo ~/torch/install/bin/luarocks install https://raw.githubusercontent.com/szym/display/master/display-scm-0.rockspec#
l 下載模型
https://github.com/soumith/lfs/raw/master/dcgan.torch/celebA_25_net_G.t7
https://github.com/soumith/lfs/raw/master/dcgan.torch/bedrooms_4_net_G.t7
l 執行
cd dcgan.torch
gpu=0 batchSize=64 net=celebA_25_net_G.t7 th generate.lua #cpu運行 batchSize圖像數量
gpu=1 batchSize=64 net=celebA_25_net_G.t7 th generate.lua #cpu運行
**
**
\
\
參考教程地址:https://ask.julyedu.com/question/7413
參考github:https://github.com/karpathy/neuraltalk2
環境包下載
sudo ~/torch/install/bin/luarocks install nn
sudo ~/torch/install/bin/luarocks install nngraph
sudo ~/torch/install/bin/luarocks install image
sudo ~/torch/install/bin/luarocks install hdf5 #這個也是必須的
sudo ~/torch/install/bin/luarocks install loadcaffe 下載模型
沒有gpu的同窗可忽略如下的安裝命令
sudo ~/torch/install/bin/luarocks install cutorch
sudo ~/torch/install/bin/luarocks install cunn
l 模型下載
http://cs.stanford.edu/people/karpathy/neuraltalk2/checkpoint_v1.zip
l 準備圖片
cd neuraltalk2
mkdir images #將圖片放到此目錄下
l 生成圖片描述
th eval.lua -model model_id1-501-1448236541.t7_cpu.t7 -image_folder ./images/ #可加參數-num_images 等
th eval.lua -model model_id1-501-1448236541.t7_cpu.t7 -image_folder ./images/ -gpuid -1 #沒有gpu的讀者運行此命令
cd vis
python -m SimpleHTTPServer #啓動後訪問http://localhost:8000
\
\
\
參考教程地址:https://ask.julyedu.com/question/7411
參考github:https://github.com/jcjohnson/densecap
l 環境包下載
.環境依賴
讀者自行安裝torch環境
luarocks install nn
luarocks install image
luarocks install lua-cjson
luarocks install raw.githubusercontent. ... kspec
luarocks install raw.githubusercontent. ... kspec
luarocks install sys #須要多加一項
沒有gpu 忽略
luarocks install cutorch
luarocks install cunn
luarocks install cudnn
luarocks install cudnn
l 模型下載
sh scripts/download_pretrained_model.sh
l 修改代碼
run_model.lua 代碼29行自行修改Model位置
l 運行
cpu 運行
th run_model.lua -input_image imgs/elephant.jpg -gpu -1
gpu 運行
th run_model.lua -input_image imgs/elephant.jpg
l 查看效果
cd vis
python -m SimpleHTTPServer 8181
訪問:http://localhost:8181/view_results.html
例如,給定系統一張圖片,系統自動生成字幕:一男的騎單車,穿白色T恤..
\
\
後續 繼續 一塊兒玩。
七月在線出品,二零一六年十月十二日。