Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can I create a data source for a caffe model from java? #196

Closed
altus88 opened this issue Apr 6, 2016 · 4 comments
Closed

Can I create a data source for a caffe model from java? #196

altus88 opened this issue Apr 6, 2016 · 4 comments

Comments

@altus88
Copy link

altus88 commented Apr 6, 2016

I am trying to run training a caffe model from java. I need to generate a lmdb database on the fly. There is some python code http://deepdish.io/2015/04/28/creating-lmdb-in-python/ which can do it, but in java I can not do the same since Datum class does not have method "SerializeToString()". Another approach I tried is using "MemoryData" data layer, and feed training data directly to the model:

SolverParameter solver_param = new SolverParameter();
ReadProtoFromTextFileOrDie(path/to/solver, solver_param); 
FloatSolver solver = FloatSolverRegistry.CreateSolver(solver_param);
FloatMemoryDataLayer input =  solver.net().layer_by_name(FloatMemoryDataLayer.class, 'mnist');

MatVector matVector = new MatVector();
IntPointer intPoiter = new IntPointer();

Mat img = opencv_imgcodecs.imread(path_to_img, opencv_imgcodecs.IMREAD_GRAYSCALE);
Mat img_res_float = new Mat();
img.convertTo(img_res_float, opencv_core.CV_32FC1);

matVector.put(img_res_float);
intPoiter.put(new Integer(0));

input.AddMatVector(matVector, intPoiter);
solver.Solve(new BytePointer());

Prototxt of the model:

name: "LeNet"
layer {
  name: "mnist"
  type: "MemoryData"
  top: "data"
  top: "label"
  include {
    phase: TRAIN
  }
  transform_param {
    scale: 0.00390625
  }
  memory_data_param {
    batch_size: 1
    channels: 1
    height: 28
    width: 28
  }
}
layer {
  name: "conv1"
  type: "Convolution"
  bottom: "data"
  top: "conv1"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  convolution_param {
    num_output: 20
    kernel_size: 5
    stride: 1
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
    }
  }
}
layer {
  name: "pool1"
  type: "Pooling"
  bottom: "conv1"
  top: "pool1"
  pooling_param {
    pool: MAX
    kernel_size: 2
    stride: 2
  }
}
layer {
  name: "conv2"
  type: "Convolution"
  bottom: "pool1"
  top: "conv2"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  convolution_param {
    num_output: 50
    kernel_size: 5
    stride: 1
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
    }
  }
}
layer {
  name: "pool2"
  type: "Pooling"
  bottom: "conv2"
  top: "pool2"
  pooling_param {
    pool: MAX
    kernel_size: 2
    stride: 2
  }
}
layer {
  name: "ip1"
  type: "InnerProduct"
  bottom: "pool2"
  top: "ip1"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  inner_product_param {
    num_output: 500
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
    }
  }
}
layer {
  name: "relu1"
  type: "ReLU"
  bottom: "ip1"
  top: "ip1"
}
layer {
  name: "ip2"
  type: "InnerProduct"
  bottom: "ip1"
  top: "ip2"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  inner_product_param {
    num_output: 10
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
    }
  }
}
layer {
  name: "loss"
  type: "SoftmaxWithLoss"
  bottom: "ip2"
  bottom: "label"
  top: "loss"
}

but at line
intPoiter.put(new Integer(0));
I get an error "Exception in thread "main" java.lang.NullPointerException: This pointer address is NULL." Could you please suggest something.

@altus88
Copy link
Author

altus88 commented Apr 6, 2016

If I write
input1.AddMatVector(matVector, new IntPointer(1));

I get the following error:
F0407 11:34:50.119905 6363 blob.cpp:32] Check failed: shape[i] >= 0 (-1527131614 vs. 0)
It seems something wrong with the shape of the mat, but I checked, it is ok: (1,28,28)

@saudet
Copy link
Member

saudet commented Apr 7, 2016

I've added the API that contains SerializeToString(), so if that works, please try to use that! Thanks

@altus88
Copy link
Author

altus88 commented Apr 8, 2016

Thanks! I will try and let you know

@saudet
Copy link
Member

saudet commented May 19, 2016

Included in version 1.2. Enjoy!

@saudet saudet closed this as completed May 19, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants