title: Caffe two class multi label classification with hdf5
body: <p>I have the following structure in a .txt file:</p>
  
  <blockquote>
  <pre><code>/path/to/image x y
  /path/to/image x y
  </code></pre>
  </blockquote>
  
  <p>where x and y are integers.</p>
  
  <p>What I want to do now is: Create a hdf5 file to use in Caffe (<code>'train.prototxt'</code>)</p>
  
  <p>My Python code looks like this:</p>
  
  <pre><code>import h5py, os
  import caffe
  import numpy as np
  
  SIZE = 256
  with open( 'train.txt', 'r' ) as T :
      lines = T.readlines()
  
  
  count_files = 0
  split_after = 1000
  count = -1
  
  # If you do not have enough memory split data into
  # multiple batches and generate multiple separate h5 files
  X = np.zeros( (split_after, 3, SIZE, SIZE), dtype='f4' )
  y1 = np.zeros( (split_after, 1), dtype='f4' )
  y2 = np.zeros( (split_after, 1), dtype='f4' )
  
  for i,l in enumerate(lines):
      count += 1
      sp = l.split(' ')
      img = caffe.io.load_image( sp[0] )
      img = caffe.io.resize( img, (3, SIZE, SIZE) )
  
      X[count] = img
      y1[count] = float(sp[1])
      y2[count] = float(sp[2])
  
      if (count+1) == split_after:
          with h5py.File('train_' + str(count_files) +  '.h5','w') as H:
              H.create_dataset( 'X', data=X ) # note the name X given to the dataset!
              H.create_dataset( 'y1', data=y1 )
              H.create_dataset( 'y2', data=y2 )
  
              X = np.zeros( (split_after, 3, SIZE, SIZE), dtype='f4' )
              y1 = np.zeros( (split_after, 1), dtype='f4' )
              y2 = np.zeros( (split_after, 1), dtype='f4' )
          with open('train_h5_list.txt','a') as L:
              L.write( 'train_' + str(count_files) + '.h5') # list all h5 files you are going to use
          count_files += 1
          count = 0
  </code></pre>
  
  <p>In fact I want to estimate angles. That means I have two classes one for vertical angles one for horizontal angles. The first class ranges from 0-10 degrees the second from 10-20 and so on (for both horizontal and vertical angles). </p>
  
  <p>How would the .prototxt look like? Here are my last layers</p>
  
  <pre><code>layer {
    name: "fc8"
    type: "InnerProduct"
    bottom: "fc7"
    top: "fc8"
    param {
      lr_mult: 1
      decay_mult: 1
    }
    param {
      lr_mult: 2
      decay_mult: 0
    }
    inner_product_param {
      num_output: 36
      weight_filler {
        type: "gaussian"
        std: 0.01
      }
      bias_filler {
        type: "constant"
        value: 0
      }
    }
  }
  
  layer {
    name: "loss"
    type: "SoftmaxWithLoss"
    bottom: "fc8"
    bottom: "y"
    top: "loss"
  }
  </code></pre>