網站首頁 編程語言 正文
引言
caffe是C++語言寫的,可能很多人不太熟悉,因此想用更簡單的腳本語言來實現。caffe提供matlab接口和python接口,這兩種語言就非常簡單,而且非常容易進行可視化,使得學習更加快速,理解更加深入。
半年前,我在學習CAFFE的時候,為了加深理解,因此寫下了隨筆,有了一系列的caffe學習文章。半年過去,很多人問到關于python接口和可視化的一些問題,現在有點空閑時間,就再次寫下一些隨筆,大家一起來學習。有些重復的內容,我就不再多講,如果大家有興趣可移步:
如何配置CAFFE的python接口?
如何將圖片轉換成LMDB文件?
如何計算訓練數據的均值文件?
以上這些操作都是訓練之前的預處理操作,不管是用什么接口,都要用到。
如何寫配置文件
首先,我們需要掌握的,就是如何寫配置文件,通過下面的代碼來學習:
# -*- coding: utf-8 -*-
"""
Spyder Editor
"""
from caffe import layers as L,params as P,to_proto
path='/home/xxx/data/' #保存數據和配置文件的路徑
train_lmdb=path+'train_db' #訓練數據LMDB文件的位置
val_lmdb=path+'val_db' #驗證數據LMDB文件的位置
mean_file=path+'mean.binaryproto' #均值文件的位置
train_proto=path+'train.prototxt' #生成的訓練配置文件保存的位置
val_proto=path+'val.prototxt' #生成的驗證配置文件保存的位置
#編寫一個函數,用于生成網絡
def create_net(lmdb,batch_size,include_acc=False):
#創建第一層:數據層。向上傳遞兩類數據:圖片數據和對應的標簽
data, label = L.Data(source=lmdb, backend=P.Data.LMDB, batch_size=batch_size, ntop=2,
transform_param=dict(crop_size=40,mean_file=mean_file,mirror=True))
#創建第二屋:卷積層
conv1=L.Convolution(data, kernel_size=5, stride=1,num_output=16, pad=2,weight_filler=dict(type='xavier'))
#創建激活函數層
relu1=L.ReLU(conv1, in_place=True)
#創建池化層
pool1=L.Pooling(relu1, pool=P.Pooling.MAX, kernel_size=3, stride=2)
conv2=L.Convolution(pool1, kernel_size=3, stride=1,num_output=32, pad=1,weight_filler=dict(type='xavier'))
relu2=L.ReLU(conv2, in_place=True)
pool2=L.Pooling(relu2, pool=P.Pooling.MAX, kernel_size=3, stride=2)
#創建一個全連接層
fc3=L.InnerProduct(pool2, num_output=1024,weight_filler=dict(type='xavier'))
relu3=L.ReLU(fc3, in_place=True)
#創建一個dropout層
drop3 = L.Dropout(relu3, in_place=True)
fc4 = L.InnerProduct(drop3, num_output=10,weight_filler=dict(type='xavier'))
#創建一個softmax層
loss = L.SoftmaxWithLoss(fc4, label)
if include_acc: #在訓練階段,不需要accuracy層,但是在驗證階段,是需要的
acc = L.Accuracy(fc4, label)
return to_proto(loss, acc)
else:
return to_proto(loss)
def write_net():
#將以上的設置寫入到prototxt文件
with open(train_proto, 'w') as f:
f.write(str(create_net(train_lmdb,batch_size=64)))
#寫入配置文件
with open(val_proto, 'w') as f:
f.write(str(create_net(val_lmdb,batch_size=32, include_acc=True)))
if __name__ == '__main__':
write_net()
通過上面這個文件的執行,我們就會得到兩個配置文件:train.prototxt和val.prototxt,分別用于訓練階段和驗證階段。
圖片轉換成LMDB文件
這種方式生成配置文件,必須有個前提,就是要先把原始圖片轉換成LMDB文件才行。如果我們已經把原始圖片做成了一個列表清單(txt文件,一行一張圖片),則可以不用LMDB格式作為輸入數據,可以用ImageData作為數據源輸入,代碼如下:
# -*- coding: utf-8 -*-
from caffe import layers as L,params as P,to_proto
path='/home/xxx/data/'
train_list=path+'train.txt'
val_list=path+'val.txt'
train_proto=path+'train.prototxt'
val_proto=path+'val.prototxt'
def create_net(img_list,batch_size,include_acc=False):
data,label=L.ImageData(source=img_list,batch_size=batch_size,new_width=48,new_height=48,ntop=2,
transform_param=dict(crop_size=40,mirror=True))
conv1=L.Convolution(data, kernel_size=5, stride=1,num_output=16, pad=2,weight_filler=dict(type='xavier'))
relu1=L.ReLU(conv1, in_place=True)
pool1=L.Pooling(relu1, pool=P.Pooling.MAX, kernel_size=3, stride=2)
conv2=L.Convolution(pool1, kernel_size=53, stride=1,num_output=32, pad=1,weight_filler=dict(type='xavier'))
relu2=L.ReLU(conv2, in_place=True)
pool2=L.Pooling(relu2, pool=P.Pooling.MAX, kernel_size=3, stride=2)
conv3=L.Convolution(pool2, kernel_size=53, stride=1,num_output=32, pad=1,weight_filler=dict(type='xavier'))
relu3=L.ReLU(conv3, in_place=True)
pool3=L.Pooling(relu3, pool=P.Pooling.MAX, kernel_size=3, stride=2)
fc4=L.InnerProduct(pool3, num_output=1024,weight_filler=dict(type='xavier'))
relu4=L.ReLU(fc4, in_place=True)
drop4 = L.Dropout(relu4, in_place=True)
fc5 = L.InnerProduct(drop4, num_output=7,weight_filler=dict(type='xavier'))
loss = L.SoftmaxWithLoss(fc5, label)
if include_acc:
acc = L.Accuracy(fc5, label)
return to_proto(loss, acc)
else:
return to_proto(loss)
def write_net():
#
with open(train_proto, 'w') as f:
f.write(str(create_net(train_list,batch_size=64)))
#
with open(val_proto, 'w') as f:
f.write(str(create_net(val_list,batch_size=32, include_acc=True)))
if __name__ == '__main__':
write_net()
?即第一層由原來的Data類型,變成了ImageData類型,不需要LMDB文件和均值文件,但需要一個txt文件。
原文鏈接:https://www.cnblogs.com/denny402/p/5679037.html
相關推薦
- 2022-05-12 Kotlin map 高級函數返回新的集合
- 2024-02-17 通過springMVC統一設置localhost的時間格式
- 2022-11-02 Android?Studio模擬器運行apk文件_Android
- 2022-09-21 flutter實現底部不規則導航欄_Android
- 2022-08-23 C++超詳細探究new/delete的使用_C 語言
- 2024-01-16 解決 git pull 操作后文件權限變化
- 2022-05-06 Docker遠程連接設置的實現示例_docker
- 2022-12-05 python?os.stat()如何獲取相關文件的系統狀態信息_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支