How to predict a single digit using Caffe and C++

2,886 views
Skip to first unread message

Yaroslav Melekhov

unread,
Feb 17, 2015, 6:26:10 AM2/17/15
to caffe...@googlegroups.com
 I am trying to use Caffe to predict a digit in MNIST dataset. I run a solver to pretrain a net as follow:
    // Set to TRAIN Phase
   
caffe::Caffe::set_phase(caffe::Caffe::TRAIN);
   
// set GPU
   
caffe::Caffe::set_mode(caffe::Caffe::GPU);
   
int deviceId = 0;
   
caffe::Caffe::SetDevice(deviceId);
   
LOG(INFO) << "Using GPU";
   
// Solver initialization and evaluation
    caffe
::SolverParameter solverParams;
   
caffe::ReadProtoFromTextFileOrDie("/home/iar/caffe/caffe/examples/mnist/lenet_solver.prototxt", &solverParams);
    caffe
::Solver<float>* solver = new caffe::SGDSolver<float>(solverParams);
    solver
->Solve();

I just use LeNet prototxt file from examples to do this. Ok, this stage is crystal clear. Then I found a script which converts mnist dataset (lmdb) to a set of grayscale images (.jpg) each of them is a representation of a handwritten digit. I added Image-Data layer as mention here (). Therefore, my .prototxt file is (my_lenet_test.prototxt):
name: "LeNet"
layers
{
  name
: "mnist"
  type
: DATA
  top
: "data"
  top
: "label"
  data_param
{
    source
: "/home/iar/caffe/caffe/examples/mnist/mnist_train_lmdb"
    backend
: LMDB
    batch_size
: 64
 
}
  transform_param
{
    scale
: 0.00390625
 
}
  include
: { phase: TRAIN }
}
layers
{
  name
: "data"
  type
: IMAGE_DATA
  top
: "data"
  top
: "label"
  image_data_param
{
    source
: "/home/iar/caffe/caffe/examples/mnist/file_list.txt"
    batch_size
: 1
    new_height
: 28
    new_width
: 28
 
}
  include
: { phase: TEST }
}

layers
{
  name
: "conv1"
  type
: CONVOLUTION
  bottom
: "data"
  top
: "conv1"
  blobs_lr
: 1
  blobs_lr
: 2
  convolution_param
{
    num_output
: 20
    kernel_size
: 5
    stride
: 1
    weight_filler
{
      type
: "xavier"
   
}
    bias_filler
{
      type
: "constant"
   
}
 
}
}
layers
{
  name
: "pool1"
  type
: POOLING
  bottom
: "conv1"
  top
: "pool1"
  pooling_param
{
    pool
: MAX
    kernel_size
: 2
    stride
: 2
 
}
}
layers
{
  name
: "conv2"
  type
: CONVOLUTION
  bottom
: "pool1"
  top
: "conv2"
  blobs_lr
: 1
  blobs_lr
: 2
  convolution_param
{
    num_output
: 50
    kernel_size
: 5
    stride
: 1
    weight_filler
{
      type
: "xavier"
   
}
    bias_filler
{
      type
: "constant"
   
}
 
}
}
layers
{
  name
: "pool2"
  type
: POOLING
  bottom
: "conv2"
  top
: "pool2"
  pooling_param
{
    pool
: MAX
    kernel_size
: 2
    stride
: 2
 
}
}
layers
{
  name
: "ip1"
  type
: INNER_PRODUCT
  bottom
: "pool2"
  top
: "ip1"
  blobs_lr
: 1
  blobs_lr
: 2
  inner_product_param
{
    num_output
: 500
    weight_filler
{
      type
: "xavier"
   
}
    bias_filler
{
      type
: "constant"
   
}
 
}
}
layers
{
  name
: "relu1"
  type
: RELU
  bottom
: "ip1"
  top
: "ip1"
}
layers
{
  name
: "ip2"
  type
: INNER_PRODUCT
  bottom
: "ip1"
  top
: "ip2"
  blobs_lr
: 1
  blobs_lr
: 2
  inner_product_param
{
    num_output
: 10
    weight_filler
{
      type
: "xavier"
   
}
    bias_filler
{
      type
: "constant"
   
}
 
}
}
layers
{
  name
: "accuracy"
  type
: ACCURACY
  bottom
: "ip2"
  bottom
: "label"
  top
: "accuracy"
  include
: { phase: TEST }
}
layers
{
  name
: "loss"
  type
: SOFTMAX_LOSS
  bottom
: "ip2"
  bottom
: "label"
  top
: "loss"
}
layers
{
  name
: "output"
  type
: ARGMAX
  bottom
: "loss"
  top
: "output"
  include
: { phase: TEST }
}
Then I construct my net:
caffe::Net<float> mnistLeNet("my_lenet_test.prototxt");
and load pre-trained layers from the solver
mnistLeNet.CopyTrainedLayersFrom("/home/iar/caffe/caffe/examples/mnist/lenet_iter_10000.caffemodel");
But on the last stage I got SIGABRT signal from my IDE and got the error:
Check failed: target_blobs[j]->channels() == source_layer.blobs(j).channels() (3 vs. 1) 
 would highly appreciate for any help. I am stuck and have no idea what to do... Thank you!


Friendly User

unread,
Feb 17, 2015, 6:43:51 AM2/17/15
to caffe...@googlegroups.com
hint: greyscale vs color (3 channels)

Yaroslav Melekhov

unread,
Feb 17, 2015, 6:53:07 AM2/17/15
to caffe...@googlegroups.com
Yes, I understand the difference between grayscale and color images but do not understand what do you mean :). Actually, what target_blobs and source_layer.blobs() are? I think that source_layer.blobs() is my input image but target_blobs?

sunil surineni

unread,
Feb 18, 2015, 1:19:11 AM2/18/15
to caffe...@googlegroups.com

  include
: { phue: TEST }
}

Hei did you come with any solution . why dont you check out
... this http://ibuzzlog.blogspot.tw/search/label/Caffe
please let me know if you found out 

J. Yegerlehner

unread,
Feb 27, 2015, 5:48:06 PM2/27/15
to caffe...@googlegroups.com
It's telling you that the net expects a blob with a shape that has 1 color channel. That's what you created when you trained against the gray scale MNIST digits. When you load the jpg images, those have 3 channels. One for each of Red, green and blue. They don't match. You can't create a network that has inputs of a certain shape, and then give it an input that has the wrong shape.

Pasta farianist

unread,
Mar 9, 2015, 1:18:25 PM3/9/15
to caffe...@googlegroups.com
So how would I tell Caffe to either load images as grayscale or drop two out of three channels?

суббота, 28 февраля 2015 г., 1:48:06 UTC+3 пользователь J. Yegerlehner написал:

Pastafarianist

unread,
Mar 22, 2015, 4:12:07 PM3/22/15
to caffe...@googlegroups.com
Well, I have found a solution, but it only works in very recent versions (initialneil's build for Windows is too old). You have to specify is_color: false in IMAGE_DATA layer, like this:

layers {
  name: "mnist"
  type: IMAGE_DATA
  top: "data"
  top: "label"
  image_data_param {
    source: "mnist_train_index.txt"
    batch_size: 64
    is_color: false
  }
  transform_param {
    scale: 0.00390625
  }
  include: { phase: TRAIN }
}

понедельник, 9 марта 2015 г., 20:18:25 UTC+3 пользователь Pastafarianist написал:

Michael George Hart

unread,
Apr 11, 2015, 8:56:34 PM4/11/15
to caffe...@googlegroups.com
I have been getting this error
      F0411 20:48:10.789644 28542 blob.cpp:454] Check failed: ShapeEquals(proto) shape mismatch (reshape not set)
And believed it is/was the same problem that you are/were having....

I pulled the caffe.master on 03/31/2015
and still have the same issue.
My my network AlexNet trained with gray-scale images
Do you have any other recommendations?
Where did you get the C++ code to train and test your network?

Thanks
Message has been deleted

Jayshree Jadhav

unread,
Feb 6, 2017, 6:53:54 AM2/6/17
to Caffe Users
6 Feb
Can you please tell me how to create "mnist_train_index.txt"
Reply all
Reply to author
Forward
0 new messages