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)
# .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()))
order="NCHW").MaxPool(outputs=2, kernel=2, stride=2, order="NCHW")) softmax = pool2.Flatten().FC([W3, B3]).Relu().FC([W4, B4]).Softmax() # Cross entropy, and accuracy xent = softmax.LabelCrossEntropy([label], "xent") # The loss function. loss, xent_grad = xent.AveragedLoss([], ["loss", xent.Grad()]) # Get gradient train_net.AddGradientOperators() accuracy = softmax.Accuracy([label], "accuracy") # parameter update. for param in params: train_net.WeightedSum([param, ONE, param.Grad(), LR], param) LR = train_net.Mul([LR, DECAY], "LR") train_net.Print([accuracy], []) # Run all on GPU. #init_net.RunAllOnGPU() #train_net.RunAllOnGPU() plan = core.Plan("mnist_lenet_gc") plan.AddNets([init_net, train_net]) plan.AddStep(core.ExecutionStep("init", init_net)) plan.AddStep(core.ExecutionStep("train", train_net, 1000)) with open('mnist_lenet_group_convolution.pbtxt', 'w') as fid: fid.write(str(plan.Proto()))
db="gen/data/mnist/mnist-train-minidb", db_type="minidb") softmax = data.Flatten([], "data_flatten").FC([W, B], "pred").Softmax([], "softmax") xent = softmax.LabelCrossEntropy([label], "xent") loss, xent_grad = xent.AveragedLoss([], ["loss", xent.Grad()]) # Get gradient train_net.AddGradientOperators() accuracy = softmax.Accuracy([label], "accuracy") # parameter update. W = train_net.WeightedSum([W, ONE, "W_grad", LR], "W") B = train_net.WeightedSum([B, ONE, "B_grad", LR], "B") LR = train_net.Mul([LR, DECAY], "LR") train_net.PrintInt([it], []) train_net.Print([loss, accuracy, LR], []) train_net.Snapshot([it, W, B], db=snapshot_db_pattern, db_type="protodb", every=100) # Run all on GPU. # init_net.RunAllOnGPU() # train_net.RunAllOnGPU() plan = core.Plan("mnist_train") plan.AddNets([init_net, train_net]) plan.AddStep(core.ExecutionStep("init", init_net)) plan.AddStep(core.ExecutionStep("train", train_net, 1000)) with open('linear_classifier_plan.pbtxt', 'w') as fid: fid.write(str(plan.Proto()))
xent = softmax.LabelCrossEntropy([label], "xent") # The loss function. loss, xent_grad = xent.AveragedLoss([], ["loss", xent.Grad()]) # Get gradient, skipping the input and flatten layers. train_net.AddGradientOperators(skip=1) accuracy = softmax.Accuracy([label], "accuracy") # parameter update. for param in [filter1, bias1, filter2, bias2, W3, B3, W4, B4]: train_net.WeightedSum([param, ONE, param.Grad(), LR], param) LR = train_net.Mul([LR, DECAY], "LR") train_net.Print([accuracy], []) train_net._net.net_type = 'parallel' train_net._net.num_workers = 8 # CPU version plan = core.Plan("mnist_lenet") plan.AddNets([init_net, train_net]) plan.AddStep(core.ExecutionStep("init", init_net)) plan.AddStep(core.ExecutionStep("train", train_net, 1000)) with open('mnist_lenet.pbtxt', 'w') as fid: fid.write(str(plan.Proto())) # GPU version init_net.RunAllOnGPU() train_net.RunAllOnGPU() plan = core.Plan("mnist_lenet") plan.AddNets([init_net, train_net]) plan.AddStep(core.ExecutionStep("init", init_net)) plan.AddStep(core.ExecutionStep("train", train_net, 1000)) with open('mnist_lenet_gpu.pbtxt', 'w') as fid: fid.write(str(plan.Proto()))
LR = init_net.ConstantFill([], "LR", shape=[1], value=-0.1) ONE = init_net.ConstantFill([], "ONE", shape=[1], value=1.) train_net = core.Net("train") X = train_net.GaussianFill([], "X", shape=[64, 2], mean=0.0, std=1.0) Y_gt = X.FC([W_gt, B_gt], "Y_gt") Y_pred = X.FC([W, B], "Y_pred") dist = train_net.SquaredL2Distance([Y_gt, Y_pred], "dist") loss = dist.AveragedLoss([], ["loss"]) # Get gradients for all the computations above. Note that in fact we don't need # to get the gradient the Y_gt computation, but we'll just leave it there. In # many cases, I am expecting one to load X and Y from the disk, so there is # really no operator that will calculate the Y_gt input. train_net.AddGradientOperators(skip=2) # updates train_net.WeightedSum([W, ONE, "W_grad", LR], W) train_net.WeightedSum([B, ONE, "B_grad", LR], B) train_net.Print([loss, W, B], []) # Run all on GPU. #init_net.RunAllOnGPU() #train_net.RunAllOnGPU() plan = core.Plan("toy_regression") plan.AddNets([init_net, train_net]) plan.AddStep(core.ExecutionStep("init", init_net)) plan.AddStep(core.ExecutionStep("train", train_net, 100)) with open('toy_regression.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()))