網(wǎng)站首頁 編程語言 正文
引言
使用python接口來運行caffe程序,主要的原因是python非常容易可視化。所以不推薦大家在命令行下面運行python程序。如果非要在命令行下面運行,還不如直接用 c++算了。
推薦使用jupyter notebook,spyder等工具來運行python代碼,這樣才和它的可視化完美結(jié)合起來。
anaconda庫
因為我是用anaconda來安裝一系列python第三方庫的,所以我使用的是spyder,與matlab界面類似的一款編輯器,在運行過程中,可以查看各變量的值,便于理解,如下圖:
只要安裝了anaconda,運行方式也非常方便,直接在終端輸入spyder命令就可以了。
python接口實現(xiàn)
在caffe的訓(xùn)練過程中,我們?nèi)绻胫滥硞€階段的loss值和accuracy值,并用圖表畫出來,用python接口就對了。
# -*- coding: utf-8 -*-
"""
Created on Tue Jul 19 16:22:22 2016
@author: root
"""
import matplotlib.pyplot as plt
import caffe
caffe.set_device(0)
caffe.set_mode_gpu()
# 使用SGDSolver,即隨機(jī)梯度下降算法
solver = caffe.SGDSolver('/home/xxx/mnist/solver.prototxt')
# 等價于solver文件中的max_iter,即最大解算次數(shù)
niter = 9380
# 每隔100次收集一次數(shù)據(jù)
display= 100
# 每次測試進(jìn)行100次解算,10000/100
test_iter = 100
# 每500次訓(xùn)練進(jìn)行一次測試(100次解算),60000/64
test_interval =938
#初始化
train_loss = zeros(ceil(niter * 1.0 / display))
test_loss = zeros(ceil(niter * 1.0 / test_interval))
test_acc = zeros(ceil(niter * 1.0 / test_interval))
# iteration 0,不計入
solver.step(1)
# 輔助變量
_train_loss = 0; _test_loss = 0; _accuracy = 0
# 進(jìn)行解算
for it in range(niter):
# 進(jìn)行一次解算
solver.step(1)
# 每迭代一次,訓(xùn)練batch_size張圖片
_train_loss += solver.net.blobs['SoftmaxWithLoss1'].data
if it % display == 0:
# 計算平均train loss
train_loss[it // display] = _train_loss / display
_train_loss = 0
if it % test_interval == 0:
for test_it in range(test_iter):
# 進(jìn)行一次測試
solver.test_nets[0].forward()
# 計算test loss
_test_loss += solver.test_nets[0].blobs['SoftmaxWithLoss1'].data
# 計算test accuracy
_accuracy += solver.test_nets[0].blobs['Accuracy1'].data
# 計算平均test loss
test_loss[it / test_interval] = _test_loss / test_iter
# 計算平均test accuracy
test_acc[it / test_interval] = _accuracy / test_iter
_test_loss = 0
_accuracy = 0
# 繪制train loss、test loss和accuracy曲線
print '\nplot the train loss and test accuracy\n'
_, ax1 = plt.subplots()
ax2 = ax1.twinx()
# train loss -> 綠色
ax1.plot(display * arange(len(train_loss)), train_loss, 'g')
# test loss -> 黃色
ax1.plot(test_interval * arange(len(test_loss)), test_loss, 'y')
# test accuracy -> 紅色
ax2.plot(test_interval * arange(len(test_acc)), test_acc, 'r')
ax1.set_xlabel('iteration')
ax1.set_ylabel('loss')
ax2.set_ylabel('accuracy')
plt.show()
最后生成的圖表在上圖中已經(jīng)顯示出來了。
原文鏈接:https://www.cnblogs.com/denny402/p/5686067.html
相關(guān)推薦
- 2023-04-01 Python之維度dim的定義及其理解使用方式_python
- 2022-02-15 使用數(shù)組的sort方法完成項目中的排序功能(數(shù)組sort方法與chart圖表展示結(jié)合)
- 2023-12-14 統(tǒng)計字符串內(nèi)某個字符出現(xiàn)的次數(shù)
- 2022-07-20 C語言通過三步翻轉(zhuǎn)法實現(xiàn)單詞倒置詳解_C 語言
- 2022-10-05 C++淺析數(shù)據(jù)在內(nèi)存中如何存儲_C 語言
- 2022-02-18 Syntax Error: Error: Node Sass version 6.0.1 is in
- 2022-06-02 詳解IIS在ASP.NET?Core下的兩種部署模式_實用技巧
- 2023-03-13 pandas按某列降序的實現(xiàn)_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支