caffe data layer example step by step
Asked Answered
F

2

10

I want to find a caffe python data layer example to learn. I know that Fast-RCNN has a python data layer, but it's rather complicated since I am not familiar with object detection.
So my question is, is there a python data layer example where I can learn how to define my own data preparation procedure?
For example, how to do define a python data layer do much more data augmentation (such as translation, rotation etc.) than caffe "ImageDataLayer".

Thank you very much

Feckless answered 25/1, 2016 at 15:20 Comment(0)
S
12

You can use a "Python" layer: a layer implemented in python to feed data into your net. (See an example for adding a type: "Python" layer here).

import sys, os
sys.path.insert(0, os.environ['CAFFE_ROOT']+'/python')
import caffe
class myInputLayer(caffe.Layer):
  def setup(self,bottom,top):
    # read parameters from `self.param_str`
    ...
  def reshape(self,bottom,top):
    # no "bottom"s for input layer
    if len(bottom)>0:
      raise Exception('cannot have bottoms for input layer')
    # make sure you have the right number of "top"s
    if len(top)!= ...
       raise ...
    top[0].reshape( ... ) # reshape the outputs to the proper sizes
    
  def forward(self,bottom,top): 
    # do your magic here... feed **one** batch to `top`
    top[0].data[...] = one_batch_of_data


  def backward(self, top, propagate_down, bottom):
    # no back-prop for input layers
    pass

For more information on param_str see this thread.
You can find a sketch of a data loading layer with pre-fetch here.

Smelter answered 25/1, 2016 at 15:47 Comment(8)
Thank you very much for your explanation, I will try to implement one and post my code here. o(^▽^)oFeckless
In fact I find one PR at caffe website. github.com/BVLC/caffe/pull/3471/filesFeckless
Is it possible to use multithreading here, to load data quicker?Nutting
@kishensurajP you can use Python threading module to prefetch data.Smelter
Thanks. So there has to be a loop for the total number of images in the batch within the forward method, so that top[0].data[...] outputs the full batch?Founder
@Founder yes. each call of forward must yield a new batchSmelter
@Shai: that's cool. Do yo know any way to speed up the loop over images in the batch? Something like itertools?Founder
@Founder you may try speed things up using multiprocessing.Process to fetch the data. See this answer for a sketch.Smelter
F
5

@Shai's answer is great. At the same time, I find another detailed example about python data layer in one PR of caffe-master. https://github.com/BVLC/caffe/pull/3471/files I hope this detailed example be helpful for anyone else.

Feckless answered 26/1, 2016 at 15:38 Comment(2)
Thank you very much, do you happen to know how we should configure the prototxt file? actually I am trying to do exactly what you asked, but I am confused. even after looking at the code. my problem is first how we define the image source in the prototxt and then how we read different parameters from it. I would appreciate if you could share your implementation with us. it helps us greatly.Fibrinolysin
Done it :) Thank you very much for your link. I followed couple of Shais answer and could thanks to dear God, get every thing running :)Fibrinolysin

© 2022 - 2024 — McMap. All rights reserved.