Water's Home

Just another Life Style

0%

Caffe Sample

Gen HDF5 Data

import pickle
import numpy as np
import h5py

with open(‘data.pkl’, ‘rb’) as f:
samples, labels = pickle.load(f)
sample_size = len(labels)

samples = np.array(samples).reshape((sample_size, 2))
labels = np.array(labels).reshape((sample_size, 1))

h5_filename = ‘data.h5’
with h5py.File(h5_filename, ‘w’) as h:
h.create_dataset(‘data’, data=samples)
h.create_dataset(‘label’, data=labels)

with open(‘data_h5.txt’, ‘w’) as f:
f.write(h5_filename)

Network Train

name: “SimpleMLP”
layer {
name: “data”
type: “HDF5Data”
top: “data”
top: “label”
include {
phase: TRAIN
}
hdf5_data_param {
source: “data_h5.txt”
batch_size: 41
}
}
layer {
name: “fc1”
type: “InnerProduct”
bottom: “data”
top: “fc1”
inner_product_param {
num_output: 2
weight_filler {
type: “uniform”
}
}
}
layer {
name: “sigmoid1”
type: “Sigmoid”
bottom: “fc1”
top: “sigmoid1”
}
layer {
name: “fc2”
type: “InnerProduct”
bottom: “sigmoid1”
top: “fc2”
inner_product_param {
num_output: 2
weight_filler {
type: “uniform”
}
}
}
layer {
name: “loss”
type: “SoftmaxWithLoss”
bottom: “fc2”
bottom: “label”
top: “loss”
}

Gen Network Picture

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

Network Solver

net: “train.prototxt”
base_lr: 0.15
lr_policy: “fixed”
display: 100
max_iter: 2000
momentum: 0.95
snapshot_prefix: “simple_mlp”
solver_mode: CPU

Start Train

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

Network Test

name: “SimpleMLP”
input: “data”
input_shape {
dim: 1
dim: 2
}
layer {
name: “fc1”
type: “InnerProduct”
bottom: “data”
top: “fc1”
inner_product_param {
num_output: 2
}
}
layer {
name: “sigmoid1”
type: “Sigmoid”
bottom: “fc1”
top: “sigmoid1”
}
layer {
name: “fc2”
type: “InnerProduct”
bottom: “sigmoid1”
top: “fc2”
inner_product_param {
num_output: 2
}
}
layer {
name: “softmax”
type: “Softmax”
bottom: “fc2”
top: “prob”
}

Start Test

import sys
import pickle
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
sys.path.append(‘/home/d/Documents/caffe/python’)
import caffe

net = caffe.Net(‘test.prototxt’, ‘simple_mlp_iter_2000.caffemodel’, caffe.TEST)

with open(‘data.pkl’, ‘rb’) as f:
samples, labels = pickle.load(f)
samples = np.array(samples)
labels = np.array(labels)

X = np.arange(0, 1.05, 0.05)
Y = np.arange(0, 1.05, 0.05)
X, Y = np.meshgrid(X, Y)

grids = np.array([[X[i][j], Y[i][j]] for i in range(X.shape[0]) for j in range(X.shape[1])])

grid_probs = []
for grid in grids:
net.blobs[‘data’].data[…] = grid.reshape((1, 2))[…]
output = net.forward()
grid_probs.append(output[‘prob’][0][1])
grid_probs = np.array(grid_probs).reshape(X.shape)
fig = plt.figure(‘Sample Surface’)
ax = fig.gca(projection=’3d’)
ax.plot_surface(X, Y, grid_probs, alpha=0.15, color=’k’, rstride=2, cstride=2, lw=0.5)

samples0 = samples[labels==0]
samples0_probs = []
for sample in samples0:
net.blobs[‘data’].data[…] = sample.reshape((1, 2))[…]
output = net.forward()
samples0_probs.append(output[‘prob’][0][1])
samples1 = samples[labels==1]
samples1_probs = []
for sample in samples1:
net.blobs[‘data’].data[…] = sample.reshape((1, 2))[…]
output = net.forward()
samples1_probs.append(output[‘prob’][0][1])

ax.scatter(samples0[:, 0], samples0[:, 1], samples0_probs, c=’r’, marker=’o’, s=50)
ax.scatter(samples1[:, 0], samples1[:, 1], samples1_probs, c=’b’, marker=’^’, s=50)

plt.show()