# -*- coding: utf-8 -*-
# from __future__ import print_function
import os
import sys
import numpy as np
os.environ["CAFFE_ROOT"] = "/path/to/caffe"
os.environ["PYTHONPATH"] = "/path/to/caffe/python"
sys.path.append(os.environ["PYTHONPATH"])
import caffe
from caffe import layers as L, params as P, to_proto
from caffe.proto import caffe_pb2
target_dir = 'tmp'
if not os.path.isdir(target_dir):
os.makedirs(target_dir)
train_net_path = os.getcwd() + '/tmp/custom_auto_train.prototxt'
test_net_path = os.getcwd() + '/tmp/custom_auto_test.prototxt'
solver_config_path = os.getcwd() + '/tmp/custom_auto_solver.prototxt'
# def im2col(top, bottom):
# im2col = L.Im2col(top, bottom)
def conv_relu(bottom, nout, ax=1, ks=3, stride=1, pad=0, wf=dict(type='gaussian')):
conv = L.Convolution(bottom, axis=ax, kernel_size=ks, stride=stride,
num_output=nout, pad=pad, weight_filler=wf)
return conv, L.ReLU(bottom = conv, in_place=True)
def fc_relu(bottom, nout, wf=dict(type='gaussian')):
fc = L.InnerProduct(bottom, num_output=nout, weight_filler=wf)
return fc, L.ReLU(fc, in_place=True)
def max_pool(bottom, ks=2, stride=1):
return L.Pooling(bottom, pool= P.Pooling.MAX, kernel_size= ks, stride=stride)
def caffenet_3D(source, batch_size=1, new_width=64, new_height=128, new_length=16, include_acc=False):
data, label = L.VideoData(source=source, batch_size=batch_size, new_width=new_width, new_height=new_height,
new_length = new_length, transform_param=dict(mean_value=[104, 117, 123], mirror=False), ntop=2)
# def videoio(bottom, num_output):
# the net itself
# im2col1 = im2col(param=dict(shape=[3, 128, 64]), data)
conv1, relu1 = conv_relu(data, 64, stride=4)
pool1 = max_pool(relu1)
# norm1 = L.LRN(pool1, local_size=5, alpha=1e-4, beta=0.75)
conv2, relu2 = conv_relu(pool1, 128, pad=2)
pool2 = max_pool(relu2)
# norm2 = L.LRN(pool2, local_size=5, alpha=1e-4, beta=0.75)
conv3, relu3 = conv_relu(pool2, 256, pad=1)
pool3 = max_pool(relu3)
conv4, relu4 = conv_relu(pool3, 512, pad=1)
pool4 = max_pool(relu4)
conv5, relu5 = conv_relu(pool4, 512, pad=1)
pool5 = max_pool(relu5)
fc6, relu6 = fc_relu(pool5, 4096)
drop6 = L.Dropout(relu6, in_place=True)
fc7, relu7 = fc_relu(drop6, 4096)
drop7 = L.Dropout(relu7, in_place=True)
fc8 = L.InnerProduct(drop7, num_output=4, weight_filler=dict(type='gaussian'))
loss = L.SoftmaxWithLoss(fc8, label)
if include_acc:
acc = L.Accuracy(fc8, label)
return to_proto(loss, acc)
else:
return to_proto(loss)
def make_net():
with open(train_net_path, 'w') as f:
train_dataset_path = '/path/to/dataset/Train_label.txtt'
f.write(str(caffenet_3D(train_dataset_path, include_acc = True)))
# print(caffenet_3D('~/path/to/dataset//Train_label.txt'), file=f)
with open(test_net_path, 'w') as f:
test_dataset_path = '/path/to/dataset//Test_label.txt'
f.write(str(caffenet_3D(test_dataset_path, include_acc = True)))
# print(caffenet_3D('//path/to/dataset/Test_label.txt', batch_size=50, include_acc=True), file=f)
def define_and_run():
s = caffe_pb2.SolverParameter()
s.random_seed = 0xCAFFE
s.train_net = train_net_path
s.test_net.append(test_net_path)
s.test_interval = 500
s.test_iter.append(100)
s.max_iter = 10000
# defining solver training parameters
s.type = 'Adam'
s.base_lr = 0.01
s.momentum = 0.9
s.weight_decay = 5e-4
s.lr_policy = 'inv'
s.gamma = 0.0001
s.power = 0.75
s.display = 1000
s.snapshot = 5000
s.snapshot_prefix = '/path/to/temporal_workdir/caffenet_3D'
s.solver_mode = caffe_pb2.SolverParameter.CPU
with open(solver_config_path, 'w') as f:
f.write(str(s))
solver = None
solver = caffe.get_solver(solver_config_path)
niter = 250
test_interval = niter / 10
train_loss = np.zeros(niter)
test_acc = np.zeros(int(np.ceil(niter / test_interval)))
output = np.zeros((niter, 8, 10))
for it in range(niter):
solver.step(1)
train_loss[it] =solver.net.blobs['loss'].data
solver.test_nets[0].forward(start='conv1')
output['it'] = solver.test_nets[0].blobs['score'].data[:3]
if it % test_interval == 0:
print 'Iteration', it, 'testing...'
correct = 0
for test_ite in range(100):
solver.test_nets[0].forward()
correct += sum(solver.test_nets[0].blobs['score'].data.argmax(1)
== solver.test_nets[0].blobs['label'].data)
test_acc[it // test_interval] = correct / 1e4
# def record_model():
# model_data = caffe.io.caffe.pb2()
if __name__ == '__main__':
make_net()
define_and_run()