Skip to content

Commit

Permalink
convnet template with db + unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
beniz committed Jan 1, 2016
1 parent af24292 commit 80620af
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 28 deletions.
6 changes: 3 additions & 3 deletions src/caffeinputconns.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ namespace dd
{
public:
ImgCaffeInputFileConn()
:ImgInputFileConn() {}
:ImgInputFileConn() { _db = true; }
ImgCaffeInputFileConn(const ImgCaffeInputFileConn &i)
:ImgInputFileConn(i),CaffeInputInterface(i) {}
:ImgInputFileConn(i),CaffeInputInterface(i) { _db = true; }
~ImgCaffeInputFileConn() {}

// size of each element in Caffe jargon
Expand Down Expand Up @@ -663,7 +663,7 @@ namespace dd
for (int c=0;c<_sequence;c++)
{
std::vector<float> v(_alphabet.size(),0.0);
if (c<vals.size() && vals[c] != -1)
if (c<(int)vals.size() && vals[c] != -1)
v[vals[c]] = 1.0;
for (float f: v)
datum.add_float_data(f);
Expand Down
37 changes: 28 additions & 9 deletions src/caffelib.cc
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ namespace dd
}
if (ad.has("dropout"))
dropout = ad.get("dropout").get<double>();
bool db = false;
bool db = inputc._db;
if (ad.has("db") && ad.get("db").get<bool>())
db = true;
if (!db && layers.empty() && activation == "ReLU" && dropout == 0.5)
Expand Down Expand Up @@ -559,21 +559,14 @@ namespace dd
}
}

if (ad.has("rotate") || ad.has("mirror"))
{
caffe::LayerParameter *lparam = net_param.mutable_layer(0); // data input layer
lparam->mutable_transform_param()->set_mirror(ad.get("mirror").get<bool>());
lparam->mutable_transform_param()->set_rotate(ad.get("rotate").get<bool>());
}

// default params
uint32_t conv_kernel_size = 3;
uint32_t conv1d_early_kernel_size = 7;
std::string conv_wfill_type = "gaussian";
double conv_wfill_std = 0.001;
bool flat1dconv = inputc._flat1dconv; // whether the model uses 1d-conv (e.g. character-level convnet for text)
std::string conv_b_type = "constant";
caffe::PoolingParameter_PoolMethod pool_type = caffe::PoolingParameter_PoolMethod_MAX;
bool flat1dconv = inputc._flat1dconv; // whether the model uses 1d-conv (e.g. character-level convnet for text)
int pool_kernel_size = 2;
int pool_stride = 2;
int nclasses = 0;
Expand Down Expand Up @@ -603,6 +596,8 @@ namespace dd
lparam->mutable_memory_data_param()->set_channels(targets); // XXX: temporary value
else
{
if (flat1dconv) // not dealing with images
lparam->clear_transform_param();
lparam->clear_memory_data_param();
lparam->set_type("Data");
caffe::DataParameter *ldparam = lparam->mutable_data_param();
Expand Down Expand Up @@ -657,12 +652,36 @@ namespace dd
{
// fixing input layer so that it takes data in from db
lparam = net_param.mutable_layer(0);
if (!flat1dconv)
{
if (ad.has("rotate") || ad.has("mirror"))
{
lparam->mutable_transform_param()->set_mirror(ad.get("mirror").get<bool>());
lparam->mutable_transform_param()->set_rotate(ad.get("rotate").get<bool>());
}
std::string mf = "mean.binaryproto";
lparam->mutable_transform_param()->set_mean_file(mf.c_str());
}
else if (flat1dconv) // not dealing with images
lparam->clear_transform_param();
lparam->clear_memory_data_param();
lparam->set_type("Data");
caffe::DataParameter *ldparam = lparam->mutable_data_param();
ldparam->set_source("train.lmdb");
ldparam->set_batch_size(1000); // dummy value, updated before training
ldparam->set_backend(caffe::DataParameter_DB_LMDB);

if (flat1dconv)
{
lparam = net_param.mutable_layer(1);
lparam->mutable_memory_data_param()->set_channels(1);
lparam->mutable_memory_data_param()->set_height(inputc.height());
lparam->mutable_memory_data_param()->set_width(inputc.width());
lparam = deploy_net_param.mutable_layer(0);
lparam->mutable_memory_data_param()->set_channels(1);
lparam->mutable_memory_data_param()->set_height(inputc.height());
lparam->mutable_memory_data_param()->set_width(inputc.width());
}
}
}
else if (l > 0)
Expand Down
16 changes: 6 additions & 10 deletions templates/caffe/convnet/convnet.prototxt
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
name: "convnet"
layer{
name: "convnet"
type: "Data"
name: "data"
type: "MemoryData"
top: "data"
top: "label"
include {
phase: TRAIN
}
transform_param {
rotate: false
mirror: false
mean_file: "mean.binaryproto"
}
data_param {
source: "train.lmdb"
memory_data_param {
batch_size: 64
backend: LMDB
channels: 3
height: 64
width: 64
}
}
layer {
Expand Down
30 changes: 24 additions & 6 deletions tests/ut-caffe-mlp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ TEST(caffelib,configure_convnet_template_1)
ad.add("activation","prelu");
ad.add("dropout",0.2);

CaffeLib<CSVCaffeInputFileConn,SupervisedOutput,CaffeModel>::configure_convnet_template(ad,false,0,nclasses,CSVCaffeInputFileConn(),net_param,deploy_net_param);
CaffeLib<ImgCaffeInputFileConn,SupervisedOutput,CaffeModel>::configure_convnet_template(ad,false,0,nclasses,ImgCaffeInputFileConn(),net_param,deploy_net_param);

caffe::WriteProtoToTextFile(net_param,oconvnet_file);
caffe::WriteProtoToTextFile(deploy_net_param,doconvnet_file);
Expand Down Expand Up @@ -322,7 +322,7 @@ TEST(caffelib,configure_convnet_template_1_db)
ad.add("dropout",0.2);
ad.add("db",true);

CaffeLib<CSVCaffeInputFileConn,SupervisedOutput,CaffeModel>::configure_convnet_template(ad,false,0,nclasses,CSVCaffeInputFileConn(),net_param,deploy_net_param);
CaffeLib<ImgCaffeInputFileConn,SupervisedOutput,CaffeModel>::configure_convnet_template(ad,false,0,nclasses,ImgCaffeInputFileConn(),net_param,deploy_net_param);

caffe::WriteProtoToTextFile(net_param,oconvnet_file);
caffe::WriteProtoToTextFile(deploy_net_param,doconvnet_file);
Expand Down Expand Up @@ -377,7 +377,7 @@ TEST(caffelib,configure_convnet_template_2)
ad.add("activation","prelu");
ad.add("dropout",0.2);

CaffeLib<CSVCaffeInputFileConn,SupervisedOutput,CaffeModel>::configure_convnet_template(ad,false,0,nclasses,CSVCaffeInputFileConn(),net_param,deploy_net_param);
CaffeLib<ImgCaffeInputFileConn,SupervisedOutput,CaffeModel>::configure_convnet_template(ad,false,0,nclasses,ImgCaffeInputFileConn(),net_param,deploy_net_param);

caffe::WriteProtoToTextFile(net_param,oconvnet_file);
caffe::WriteProtoToTextFile(deploy_net_param,doconvnet_file);
Expand Down Expand Up @@ -459,7 +459,7 @@ TEST(caffelib,configure_convnet_template_3)
ad.add("activation","prelu");
ad.add("dropout",0.2);

CaffeLib<CSVCaffeInputFileConn,SupervisedOutput,CaffeModel>::configure_convnet_template(ad,false,0,nclasses,CSVCaffeInputFileConn(),net_param,deploy_net_param);
CaffeLib<ImgCaffeInputFileConn,SupervisedOutput,CaffeModel>::configure_convnet_template(ad,false,0,nclasses,ImgCaffeInputFileConn(),net_param,deploy_net_param);

caffe::WriteProtoToTextFile(net_param,oconvnet_file);
caffe::WriteProtoToTextFile(deploy_net_param,doconvnet_file);
Expand Down Expand Up @@ -520,7 +520,7 @@ TEST(caffelib,configure_convnet_template_n)
ad.add("activation","prelu");
ad.add("dropout",0.2);

CaffeLib<CSVCaffeInputFileConn,SupervisedOutput,CaffeModel>::configure_convnet_template(ad,false,0,nclasses,CSVCaffeInputFileConn(),net_param,deploy_net_param);
CaffeLib<ImgCaffeInputFileConn,SupervisedOutput,CaffeModel>::configure_convnet_template(ad,false,0,nclasses,ImgCaffeInputFileConn(),net_param,deploy_net_param);

caffe::WriteProtoToTextFile(net_param,oconvnet_file);
caffe::WriteProtoToTextFile(deploy_net_param,doconvnet_file);
Expand All @@ -530,6 +530,9 @@ TEST(caffelib,configure_convnet_template_n)
ASSERT_TRUE(succ);

ASSERT_EQ(26,net_param.layer_size());
caffe::LayerParameter *lparam = net_param.mutable_layer(0);
ASSERT_EQ("Data",lparam->type());
ASSERT_EQ("mean.binaryproto",lparam->mutable_transform_param()->mean_file());
ASSERT_EQ(22,deploy_net_param.layer_size());
}

Expand All @@ -551,7 +554,9 @@ TEST(caffelib,configure_convnet_template_n_1D)
TxtCaffeInputFileConn tcif;
tcif._characters = true;
tcif._flat1dconv = true;
tcif._db = true;
tcif.build_alphabet();
tcif._sequence = 1014;
CaffeLib<TxtCaffeInputFileConn,SupervisedOutput,CaffeModel>::configure_convnet_template(ad,false,0,nclasses,tcif,net_param,deploy_net_param);

caffe::WriteProtoToTextFile(net_param,oconvnet_file);
Expand All @@ -562,7 +567,15 @@ TEST(caffelib,configure_convnet_template_n_1D)
ASSERT_TRUE(succ);

ASSERT_EQ(26,net_param.layer_size());
caffe::LayerParameter *lparam = net_param.mutable_layer(2);
caffe::LayerParameter *lparam = net_param.mutable_layer(0);
ASSERT_EQ("Data",lparam->type());
ASSERT_TRUE(lparam->mutable_transform_param()->mean_file().empty());
lparam = net_param.mutable_layer(1);
ASSERT_EQ("MemoryData",lparam->type());
ASSERT_EQ(1,lparam->mutable_memory_data_param()->channels());
ASSERT_EQ(tcif._sequence,lparam->mutable_memory_data_param()->height());
ASSERT_EQ(tcif._alphabet.size(),lparam->mutable_memory_data_param()->width());
lparam = net_param.mutable_layer(2);
ASSERT_EQ("Convolution",lparam->type());
ASSERT_EQ(256,lparam->mutable_convolution_param()->num_output());
ASSERT_EQ(7,lparam->mutable_convolution_param()->kernel_h());
Expand All @@ -580,6 +593,11 @@ TEST(caffelib,configure_convnet_template_n_1D)
ASSERT_EQ(1,lparam->mutable_convolution_param()->kernel_w());

ASSERT_EQ(22,deploy_net_param.layer_size());
lparam = deploy_net_param.mutable_layer(0);
ASSERT_EQ("MemoryData",lparam->type());
ASSERT_EQ(1,lparam->mutable_memory_data_param()->channels());
ASSERT_EQ(tcif._sequence,lparam->mutable_memory_data_param()->height());
ASSERT_EQ(tcif._alphabet.size(),lparam->mutable_memory_data_param()->width());
lparam = deploy_net_param.mutable_layer(1);
ASSERT_EQ("Convolution",lparam->type());
ASSERT_EQ(256,lparam->mutable_convolution_param()->num_output());
Expand Down

0 comments on commit 80620af

Please sign in to comment.