How to define new 3D-CNN model from Python interface f

63 views
Skip to first unread message

Negishi.Yoshikazu

unread,
Feb 23, 2017, 5:31:44 AM2/23/17
to Caffe Users
Hi.

I am using caffe-folk named "Video-caffe" for action recognition which makes build 3D-CNN model easier.

I have needed for changing input shape, color dimensions ,crop size, hyper parameters ... and so force 
not editing protocol text files but programmable directly.

So I tried to build 3D convolutional models from python interfaces,
but I got an error and could not build .

I would like you to give some advice.

Here are the sample scripts and  error message i got.
(Path of the files and environmental variables are changed for my privacy...)

Thanks .


Here is the script code:

# -*- 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()



...and error code is below:

Traceback (most recent call last):
  File "pycaffetest.py", line 131, in <module>
    make_net()
  File "pycaffetest.py", line 72, in make_net
    f.write(str(caffenet_3D(train_dataset_path, include_acc = True)))
  File "pycaffetest.py", line 65, in caffenet_3D
    return to_proto(loss, acc)
  File "/path/to/caffe/python/caffe/net_spec.py", line 50, in to_proto
    top.fn._to_proto(layers, {}, autonames)
  File "/path/to/caffe/python/caffe/net_spec.py", line 136, in _to_proto
    inp._to_proto(layers, names, autonames)
  File "/path/to/caffe/python/caffe/net_spec.py", line 97, in _to_proto
    return self.fn._to_proto(layers, names, autonames)
  File "/path/to/caffe/python/caffe/net_spec.py", line 136, in _to_proto
    inp._to_proto(layers, names, autonames)
  File "/path/to/caffe/python/caffe/net_spec.py", line 97, in _to_proto
    return self.fn._to_proto(layers, names, autonames)
  File "/path/to/caffe/python/caffe/net_spec.py", line 136, in _to_proto
    inp._to_proto(layers, names, autonames)
  File "/path/to/caffe/python/caffe/net_spec.py", line 97, in _to_proto
    return self.fn._to_proto(layers, names, autonames)
  File "/path/to/caffe/python/caffe/net_spec.py", line 136, in _to_proto
    inp._to_proto(layers, names, autonames)
  File "/path/to/caffe/python/caffe/net_spec.py", line 97, in _to_proto
    return self.fn._to_proto(layers, names, autonames)
  File "/path/to/caffe/python/caffe/net_spec.py", line 136, in _to_proto
    inp._to_proto(layers, names, autonames)
  File "/path/to/caffe/python/caffe/net_spec.py", line 97, in _to_proto
    return self.fn._to_proto(layers, names, autonames)
  File "/path/to/caffe/python/caffe/net_spec.py", line 136, in _to_proto
    inp._to_proto(layers, names, autonames)
  File "/path/to/caffe/python/caffe/net_spec.py", line 97, in _to_proto
    return self.fn._to_proto(layers, names, autonames)
  File "/path/to/caffe/python/caffe/net_spec.py", line 136, in _to_proto
    inp._to_proto(layers, names, autonames)
  File "/path/to/caffe/python/caffe/net_spec.py", line 97, in _to_proto
    return self.fn._to_proto(layers, names, autonames)
  File "/path/to/caffe/python/caffe/net_spec.py", line 136, in _to_proto
    inp._to_proto(layers, names, autonames)
  File "/path/to/caffe/python/caffe/net_spec.py", line 97, in _to_proto
    return self.fn._to_proto(layers, names, autonames)
  File "/path/to/caffe/python/caffe/net_spec.py", line 136, in _to_proto
    inp._to_proto(layers, names, autonames)
  File "/path/to/caffe/python/caffe/net_spec.py", line 97, in _to_proto
    return self.fn._to_proto(layers, names, autonames)
  File "/path/to/caffe/python/caffe/net_spec.py", line 158, in _to_proto
    assign_proto(layer, k, v)
  File "/path/to/caffe/python/caffe/net_spec.py", line 74, in assign_proto
    getattr(proto, name).extend(val)
  File "/path/to/anaconda-python/lib/python2.7/site-packages/protobuf-2.5.0-py2.7.egg/google/protobuf/internal/containers.py", line 130, in extend
  File "/path/to/anaconda-python/lib/python2.7/site-packages/protobuf-2.5.0-py2.7.egg/google/protobuf/internal/type_checkers.py", line 117, in CheckValue
TypeError: <caffe.net_spec.Top object at 0x7f6312f69190> has type <class 'caffe.net_spec.Top'>, but expected one of: (<type 'str'>, <type 'unicode'>)


Reply all
Reply to author
Forward
0 new messages