我想为我的caffe模型绘制训练和测试精度。设置如下: 我有1200个培训样本和300个开发样本。 (简单问题的小数字)。我将火车划分为100个小批量,每批有12个样本,而Dev分为100个小批量,每个有3个样本。我的目标是循环训练并测试每1个时期= 100次迭代。
解算器文件:
test_iter: 3
test_interval: 100
# The base learning rate, momentum and the weight decay of the network.
base_lr: 0.001
#momentum: 0.9
weight_decay: 0.0005
# The learning rate policy
lr_policy: "inv"
gamma: 0.0001
power: 0.75
# Display every 100 iterations
display: 10000
# The maximum number of iterations
max_iter: 200
现在我想迭代测试集,每1个时期我要保存并测试:
base_weights = "VGG_CNN_F.caffemodel"
solver = caffe.SGDSolver('solver.prototxt')
solver.net.copy_from(base_weights)
niter = 200+1
test_interval = 100
test_iter = 3
save_interval = 100
#losses will also be stored in the log
train_loss = np.zeros(niter)
train_acc = np.zeros(niter)
test_acc = np.zeros(int(np.ceil(niter / test_interval)))
test_counter = 0
#the main solver loop
for it in range(niter):
solver.step(1) #(forward, backward, update) on 1 mini-batch
#store the train loss
train_loss[it] = solver.net.blobs['loss'].data
train_acc[it] = solver.net.blobs['accuracy'].data
if it%save_interval== 0:
solver.net.save('classifier.caffemodel')
print('Train loss at itr', it,' = ' ,train_loss[it])
print('Train accuracy at itr ', it,' = ' ,train_acc[it])
if (it%test_interval == 0) and (it!=0):
temp = np.zeros(test_iter)
for t_it in range(test_iter): # test on all Dev mini-batches
solver.test_nets[0].forward()
temp[t_it] = solver.test_nets[0].blobs['accuracy'].data
test_acc[test_counter] = temp.mean()
print ("Mean TEST acc at iteration = " + str(it) + " is " + str(test_acc[test_counter]))
test_counter = test_counter + 1
solver.net.save('classifier.caffemodel')
输出:
I1128 15:24:50.677237 24165 sgd_solver.cpp:105] Iteration 0, lr = 0.001
('Train loss at itr', 0, ' = ', 9.5767021179199219)
('Train accuracy at itr ', 0, ' = ', 0.3333333432674408)
I1128 15:26:05.196552 24175 data_layer.cpp:73] Restarting data prefetching from start.
I1128 15:26:09.776504 24165 solver.cpp:330] Iteration 100, Testing net (#0)
I1128 15:26:10.819236 24165 solver.cpp:397] Test net output #0: accuracy = 0.888889
I1128 15:26:10.819289 24165 solver.cpp:397] Test net output #1: loss = 0.141346 (* 1 = 0.141346 loss)
I1128 15:26:10.819301 24165 solver.cpp:397] Test net output #2: prob = 3.90094e-18
I1128 15:26:10.819314 24165 solver.cpp:397] Test net output #3: prob = 0.333333
I1128 15:26:10.819324 24165 solver.cpp:397] Test net output #4: prob = 0.666667
I1128 15:26:10.819335 24165 solver.cpp:397] Test net output #5: prob = 0.333331
I1128 15:26:10.819345 24165 solver.cpp:397] Test net output #6: prob = 0.333336
I1128 15:26:10.819356 24165 solver.cpp:397] Test net output #7: prob = 0.333333
I1128 15:26:10.819366 24165 solver.cpp:397] Test net output #8: prob = 0.0934135
I1128 15:26:10.819376 24165 solver.cpp:397] Test net output #9: prob = 0.23992
I1128 15:26:10.819387 24165 solver.cpp:397] Test net output #10: prob = 0.666667
('Train loss at itr', 100, ' = ', 19.135543823242188)
('Train accuracy at itr ', 100, ' = ', 0.58333331346511841)
Mean TEST acc at iteration = 100 is 0.777777791023
I1128 15:27:25.426235 24175 data_layer.cpp:73] Restarting data prefetching from start.
I1128 15:27:27.963992 24165 solver.cpp:330] Iteration 200, Testing net (#0)
I1128 15:27:28.392573 24165 solver.cpp:397] Test net output #0: accuracy = 0.777778
I1128 15:27:28.392724 24165 solver.cpp:397] Test net output #1: loss = 1.75344 (* 1 = 1.75344 loss)
I1128 15:27:28.392793 24165 solver.cpp:397] Test net output #2: prob = 0.333333
I1128 15:27:28.392859 24165 solver.cpp:397] Test net output #3: prob = 0.333333
I1128 15:27:28.392925 24165 solver.cpp:397] Test net output #4: prob = 0.333333
I1128 15:27:28.392989 24165 solver.cpp:397] Test net output #5: prob = 0.357607
I1128 15:27:28.393054 24165 solver.cpp:397] Test net output #6: prob = 0.642377
I1128 15:27:28.393160 24165 solver.cpp:397] Test net output #7: prob = 1.57992e-05
I1128 15:27:28.393239 24165 solver.cpp:397] Test net output #8: prob = 0.333334
I1128 15:27:28.393307 24165 solver.cpp:397] Test net output #9: prob = 0.333333
I1128 15:27:28.393416 24165 solver.cpp:397] Test net output #10: prob = 0.333333
('Train loss at itr', 200, ' = ', 4.1576399803161621)
('Train accuracy at itr ', 200, ' = ', 0.75)
Mean TEST acc at iteration = 200 is 0.666666686535
我的问题是为什么我计算的Dev set的准确度与求解器的输出不同?
迭代#100:
迭代#200: