Ejemplo n.º 1
0
 def testRunPlan(self):
     plan = core.Plan("test-plan")
     plan.AddNets([self.net])
     plan.AddStep(core.ExecutionStep("test-step", self.net))
     self.assertEqual(workspace.RunPlan(plan.Proto().SerializeToString()),
                      True)
     self.assertEqual(workspace.HasBlob("testblob"), True)
Ejemplo n.º 2
0
#                .Relu([], "hidden_relu")
#                .FC([W2, B2], 'pred')
#                .Softmax([], "softmax"))
# The following one-liner is to show how one can create a network without
# worrying about the detailed names of things.
softmax = data.Flatten().FC([W1, B1]).Relu().FC([W2, B2]).Softmax()

# Cross entropy, and accuracy
xent = softmax.LabelCrossEntropy([label], "xent")
# The loss function.
loss = xent.AveragedLoss(outputs=["loss"])
# Get gradient, skipping the input and flatten layers.
train_net.AddGradientOperators()
accuracy = softmax.Accuracy([label], "accuracy")
# parameter update.
for param in [W1, B1, W2, B2]:
    train_net.WeightedSum([param, ONE, param.Grad(), LR], param)
LR = train_net.Mul([LR, DECAY], "LR")
train_net.Print([accuracy], [])

# If you would like to, you can run all on GPU.
#init_net.RunAllOnGPU()
#train_net.RunAllOnGPU()

plan = core.Plan("mnist_relu_network")
plan.AddNets([init_net, train_net])
plan.AddStep(core.ExecutionStep("init", init_net))
plan.AddStep(core.ExecutionStep("train", train_net, 1000))

with open('mnist_relu_network.pbtxt', 'w') as fid:
    fid.write(str(plan.Proto()))
W8 = init_net.ConstantFill([], "W8", shape=[1000, 4096])
B8 = init_net.ConstantFill([], "B8", shape=[1000], value=0.0)
pred = (pool5_flatten.FC([W6, B6]).Relu().Dropout(outputs=2)[0]
                   .FC([W7, B7]).Relu().Dropout(outputs=2)[0]
                   .FC([W8, B8]).Softmax())
xent = pred.LabelCrossEntropy([label], "xent")
# The loss function.
loss, xent_grad = xent.AveragedLoss([], ["loss", xent.Grad()])
test_net.AddGradientOperators(first=2)
test_net.Print([loss], [])

dump_net = core.Net("dump")
for blob in [data, pool1, pool1a, pool1b, pool2, conv3a, conv3b, conv4a, conv4b,
             conv5a, conv5b, pool5_flatten]:
  dump_net.SaveFloatTensor([blob], [], file=str(blob))

init_net.RunAllOnGPU()
test_net.RunAllOnGPU()
dump_net.RunAllOnGPU()


plan = core.Plan("alexnet")
plan.AddNets([init_net, test_net, dump_net])
plan.AddStep(core.ExecutionStep("init", init_net))
plan.AddStep(core.ExecutionStep("first_run", test_net))
#plan.AddStep(core.ExecutionStep("subsequent_run", test_net, 10))
plan.AddStep(core.ExecutionStep("dump", dump_net))

with open('alexnet.pbtxt', 'w') as fid:
  fid.write(str(plan.Proto()))