Caffe Sample

Gen HDF5 Data

  1. import pickle
  2. import numpy as np
  3. import h5py
  4.  
  5. with open('data.pkl', 'rb') as f:
  6.     samples, labels = pickle.load(f)
  7. sample_size = len(labels)
  8.  
  9. samples = np.array(samples).reshape((sample_size, 2))
  10. labels = np.array(labels).reshape((sample_size, 1))
  11.  
  12. h5_filename = 'data.h5'
  13. with h5py.File(h5_filename, 'w') as h:
  14.     h.create_dataset('data', data=samples)
  15.     h.create_dataset('label', data=labels)
  16.  
  17. with open('data_h5.txt', 'w') as f:
  18.     f.write(h5_filename)

Network Train

  1. name: "SimpleMLP"
  2. layer {
  3.     name:   "data"
  4.     type:   "HDF5Data"
  5.     top:    "data"
  6.     top:    "label"
  7.     include {
  8.         phase:  TRAIN
  9.     }
  10.     hdf5_data_param {
  11.         source: "data_h5.txt"
  12.         batch_size: 41
  13.     }
  14. }
  15. layer {
  16.     name:   "fc1"
  17.     type:   "InnerProduct"
  18.     bottom: "data"
  19.     top:    "fc1"
  20.     inner_product_param {
  21.         num_output: 2
  22.         weight_filler   {
  23.             type:   "uniform"
  24.         }
  25.     }
  26. }
  27. layer {
  28.     name:   "sigmoid1"
  29.     type:   "Sigmoid"
  30.     bottom: "fc1"
  31.     top:    "sigmoid1"
  32. }
  33. layer {
  34.     name:   "fc2"
  35.     type:   "InnerProduct"
  36.     bottom: "sigmoid1"
  37.     top:    "fc2"
  38.     inner_product_param {
  39.         num_output: 2
  40.         weight_filler   {
  41.             type:   "uniform"
  42.         }
  43.     }
  44. }
  45. layer {
  46.     name:   "loss"
  47.     type:   "SoftmaxWithLoss"
  48.     bottom: "fc2"
  49.     bottom: "label"
  50.     top:    "loss"
  51. }

Gen Network Picture

  1. python /home/d/Documents/caffe/python/draw_net.py train.prototxt mlp_train.png --rankdir BT

Network Solver

  1. net:    "train.prototxt"
  2. base_lr:    0.15
  3. lr_policy:  "fixed"
  4. display:    100
  5. max_iter:   2000
  6. momentum:   0.95
  7. snapshot_prefix:    "simple_mlp"
  8. solver_mode:    CPU

Start Train

  1. /home/d/Documents/caffe/build/tools/caffe train -solver solver.prototxt

Network Test

  1. name: "SimpleMLP"
  2. input:  "data"
  3. input_shape {
  4.     dim:    1
  5.     dim:    2
  6. }
  7. layer {
  8.     name:   "fc1"
  9.     type:   "InnerProduct"
  10.     bottom: "data"
  11.     top:    "fc1"
  12.     inner_product_param {
  13.         num_output: 2
  14.     }
  15. }
  16. layer {
  17.     name:   "sigmoid1"
  18.     type:   "Sigmoid"
  19.     bottom: "fc1"
  20.     top:    "sigmoid1"
  21. }
  22. layer {
  23.     name:   "fc2"
  24.     type:   "InnerProduct"
  25.     bottom: "sigmoid1"
  26.     top:    "fc2"
  27.     inner_product_param {
  28.         num_output: 2
  29.     }
  30. }
  31. layer {
  32.     name:   "softmax"
  33.     type:   "Softmax"
  34.     bottom: "fc2"
  35.     top:    "prob"
  36. }

Start Test

  1. import sys
  2. import pickle
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. from mpl_toolkits.mplot3d import Axes3D
  6. sys.path.append('/home/d/Documents/caffe/python')
  7. import caffe
  8.  
  9. net = caffe.Net('test.prototxt', 'simple_mlp_iter_2000.caffemodel', caffe.TEST)
  10.  
  11. with open('data.pkl', 'rb') as f:
  12.     samples, labels = pickle.load(f)
  13. samples = np.array(samples)
  14. labels = np.array(labels)
  15.  
  16. X = np.arange(0, 1.05, 0.05)
  17. Y = np.arange(0, 1.05, 0.05)
  18. X, Y = np.meshgrid(X, Y)
  19.  
  20. grids = np.array([[X[i][j], Y[i][j]] for i in range(X.shape[0]) for j in range(X.shape[1])])
  21.  
  22. grid_probs = []
  23. for grid in grids:
  24.     net.blobs['data'].data[...] = grid.reshape((1, 2))[...]
  25.     output = net.forward()
  26.     grid_probs.append(output['prob'][0][1])
  27. grid_probs = np.array(grid_probs).reshape(X.shape)
  28. fig = plt.figure('Sample Surface')
  29. ax = fig.gca(projection='3d')
  30. ax.plot_surface(X, Y, grid_probs, alpha=0.15, color='k', rstride=2, cstride=2, lw=0.5)
  31.  
  32. samples0 = samples[labels==0]
  33. samples0_probs = []
  34. for sample in samples0:
  35.     net.blobs['data'].data[...] = sample.reshape((1, 2))[...]
  36.     output = net.forward()
  37.     samples0_probs.append(output['prob'][0][1])
  38. samples1 = samples[labels==1]
  39. samples1_probs = []
  40. for sample in samples1:
  41.     net.blobs['data'].data[...] = sample.reshape((1, 2))[...]
  42.     output = net.forward()
  43.     samples1_probs.append(output['prob'][0][1])
  44.  
  45. ax.scatter(samples0[:, 0], samples0[:, 1], samples0_probs, c='r', marker='o', s=50)
  46. ax.scatter(samples1[:, 0], samples1[:, 1], samples1_probs, c='b', marker='^', s=50)
  47.  
  48. plt.show()

Leave a Reply

Your email address will not be published. Required fields are marked *