1. 程式人生 > >caffe python介面訓練VGG

caffe python介面訓練VGG

# -*- coding: utf-8 -*-
"""
@author: root
"""

#matplotlib是python最著名的繪相簿
import matplotlib.pyplot as plt
import caffe
#import cv2
from numpy import *

caffe.set_device(0)
caffe.set_mode_gpu()
solver = caffe.SGDSolver('/home/hrw/caffe/examples/VGG/VGG_solver.prototxt')
solver.net.copy_from('/home/hrw/caffe/examples/VGG/vgg_face.caffemodel')
# 等價於solver檔案中的max_iter,即最大解算次數
niter = 3112800
# 每隔200次收集一次資料
display = 1000

# 每次測試進行494次解算,18612/16
test_iter = 1163
# 每7215次訓練是一個迴圈並進行一次測試
test_interval = 25940

# 初始化
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
# 進行解算# 輔助變數

for it in range(niter):
    # 進行一次解算
    solver.step(1)
    # 每迭代一次,訓練batch_size張圖片
    _train_loss += solver.net.blobs['loss'].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):
            # 進行一次測試
            solver.test_nets[0].forward()
            # 計算test loss
            _test_loss += solver.test_nets[0].blobs['loss'].data
            # 計算test accuracy
            _accuracy += solver.test_nets[0].blobs['accuracy_at_1'].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, 'r')
# 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()