Commit 61c0b334 authored by Carlos Alfredo Yeverino Rodriguez's avatar Carlos Alfredo Yeverino Rodriguez
Browse files

CNNCreator: Corrected way to get input data. Pass output name.

parent 3d7accef
......@@ -26,7 +26,7 @@ PREDICT_NET = 'D:/Yeverino/git_projects/Caffe2_scripts/caffe2_ema_cnncreator/pre
device_opts = core.DeviceOption(caffe2_pb2.CUDA, 0)#' for GPU processing
# randomly creates 30x30 patches of ones or zeros with label 1 and 0 respectively
def get_data(batchsize) :
def get_dummy_data(batchsize) :
data = []
label = []
for i in range(batchsize) :
......@@ -41,34 +41,39 @@ def get_data(batchsize) :
label.append(l)
return np.array(data).astype('float32'), np.array(label).astype('int32')
def AddInput(model, batch_size):
data, label = get_dummy_data(batch_size)
print '\ndata:', data
print '\nlabel:', label
return data, label
#def create_model(model, data, device_opts): #data argument is dummy at the moment
def create_model(model, device_opts):
with core.DeviceScope(device_opts):
${tc.include(tc.architecture.body)}
# add loss and optimizer
def add_training_operators(softmax, model, device_opts) :
#def add_training_operators(model, output, label, device_opts) : #label argument is dummy at the moment
def add_training_operators(model, output, device_opts) :
with core.DeviceScope(device_opts):
xent = model.LabelCrossEntropy([softmax, "label"], 'xent')
xent = model.LabelCrossEntropy([output, "label"], 'xent')
loss = model.AveragedLoss(xent, "loss")
brew.accuracy(model, [softmax, "label"], "accuracy")
brew.accuracy(model, [output, "label"], "accuracy")
model.AddGradientOperators([loss])
opt = optimizer.build_sgd(model, base_learning_rate=0.01, policy="step", stepsize=1, gamma=0.999) # , momentum=0.9
def train(INIT_NET, PREDICT_NET, epochs, batch_size, device_opts) :
data, label = get_data(batch_size)
print '\ndata:', data
print '\nlabel:', label
workspace.FeedBlob("data", data, device_option=device_opts)
workspace.FeedBlob("label", label, device_option=device_opts)
train_model= model_helper.ModelHelper(name="train_net")
softmax = create_model(train_model, device_opts=device_opts)
add_training_operators(softmax, train_model, device_opts=device_opts)
#data, label = AddInput(train_model, batch_size=100)
#predictions = create_model(train_model, data, device_opts=device_opts)
#add_training_operators(train_model, predictions, label, device_opts=device_opts)
${tc.join(tc.architectureOutputs, ",", "","")} = create_model(train_model, device_opts=device_opts)
add_training_operators(train_model, ${tc.join(tc.architectureOutputs, ",", "","")}, device_opts=device_opts)
with core.DeviceScope(device_opts):
brew.add_weight_decay(train_model, 0.001) # any effect???
......@@ -78,11 +83,6 @@ def train(INIT_NET, PREDICT_NET, epochs, batch_size, device_opts) :
print '\ntraining for', epochs, 'epochs'
for j in range(0, epochs):
data, label = get_data(batch_size)
workspace.FeedBlob("data", data, device_option=device_opts)
workspace.FeedBlob("label", label, device_option=device_opts)
workspace.RunNet(train_model.net, 10) # run for 10 times
print str(j) + ': ' + 'loss ' + str(workspace.FetchBlob("loss")) + ' - ' + 'accuracy ' + str(workspace.FetchBlob("accuracy"))
......@@ -91,24 +91,24 @@ def train(INIT_NET, PREDICT_NET, epochs, batch_size, device_opts) :
print '\nrunning test model'
test_model= model_helper.ModelHelper(name="test_net", init_params=False)
#create_model(test_model, data, device_opts=device_opts)
create_model(test_model, device_opts=device_opts)
workspace.RunNetOnce(test_model.param_init_net)
workspace.CreateNet(test_model.net, overwrite=True)
data = np.zeros((1,1,30,30)).astype('float32')
workspace.FeedBlob("data", data, device_option=device_opts)
${tc.architectureInputs[0]} = np.zeros((1,1,30,30)).astype('float32')
workspace.FeedBlob("${tc.architectureInputs[0]}", ${tc.architectureInputs[0]}, device_option=device_opts)
workspace.RunNet(test_model.net, 1)
print "\nInput: zeros"
print "Output:", workspace.FetchBlob("out1") #TODO: pass output name
print "Output class:", np.argmax(workspace.FetchBlob("out1")) #TODO: pass output name
print "Output:", workspace.FetchBlob("${tc.architectureOutputs[0]}") #TODO: Consider multiple output names
print "Output class:", np.argmax(workspace.FetchBlob("${tc.architectureOutputs[0]}")) #TODO: Consider multiple output names
data = np.ones((1,1,30,30)).astype('float32')
workspace.FeedBlob("data", data, device_option=device_opts)
${tc.architectureInputs[0]} = np.ones((1,1,30,30)).astype('float32')
workspace.FeedBlob("${tc.architectureInputs[0]}", ${tc.architectureInputs[0]}, device_option=device_opts)
workspace.RunNet(test_model.net, 1)
print "\nInput: ones"
print "Output:", workspace.FetchBlob("out1") #TODO: pass output name
print "Output class:", np.argmax(workspace.FetchBlob("out1")) #TODO: pass output name
print "Output:", workspace.FetchBlob("${tc.architectureOutputs[0]}") #TODO: Consider multiple output names
print "Output class:", np.argmax(workspace.FetchBlob("${tc.architectureOutputs[0]}")) #TODO: Consider multiple output names
print '\nsaving test model'
......@@ -157,17 +157,16 @@ def load_net(init_net_path, predict_net_path, device_opts):
net_def.device_option.CopyFrom(device_opts)
workspace.CreateNet(net_def.SerializeToString(), overwrite=True)
train(INIT_NET, PREDICT_NET, epochs=20, batch_size=100, device_opts=device_opts)
print '\n********************************************'
print 'loading test model'
load_net(INIT_NET, PREDICT_NET, device_opts=device_opts)
data = np.ones((1,1,30,30)).astype('float32')
workspace.FeedBlob("data", data, device_option=device_opts)
${tc.architectureInputs[0]} = np.ones((1,1,30,30)).astype('float32')
workspace.FeedBlob("${tc.architectureInputs[0]}", ${tc.architectureInputs[0]}, device_option=device_opts)
workspace.RunNet('test_net', 1)
print "\nInput: ones"
print "Output:", workspace.FetchBlob("out1") #TODO: pass output name
print "Output class:", np.argmax(workspace.FetchBlob("out1")) #TODO: pass output name
print "Output:", workspace.FetchBlob("${tc.architectureOutputs[0]}") #TODO: Consider multiple output names
print "Output class:", np.argmax(workspace.FetchBlob("${tc.architectureOutputs[0]}")) #TODO: Consider multiple output names
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment