def __init__(self, name, order): self.net = core.Net(name) self.param_init_net = core.Net(name + '_init') self.params = [] self.order = order if self.order != "NHWC" and self.order != "NCHW": raise ValueError("Cannot understand the CNN storage order.")
def _MNISTNetworks(self): init_net = core.Net("init") filter1 = init_net.XavierFill([], "filter1", shape=[20, 1, 5, 5]) bias1 = init_net.ConstantFill([], "bias1", shape=[ 20, ], value=0.0) filter2 = init_net.XavierFill([], "filter2", shape=[50, 20, 5, 5]) bias2 = init_net.ConstantFill([], "bias2", shape=[ 50, ], value=0.0) W3 = init_net.XavierFill([], "W3", shape=[500, 800]) B3 = init_net.ConstantFill([], "B3", shape=[500], value=0.0) W4 = init_net.XavierFill([], "W4", shape=[10, 500]) B4 = init_net.ConstantFill([], "B4", shape=[10], value=0.0) data, label = init_net.TensorProtosDBInput( [], ["data", "label"], batch_size=64, db="gen/data/mnist/mnist-train-nchw-minidb", db_type="minidb") LR = init_net.ConstantFill([], "LR", shape=[1], value=-0.1) ONE = init_net.ConstantFill([], "ONE", shape=[1], value=1.0) train_net = core.Net("train") conv1 = train_net.Conv([data, filter1, bias1], "conv1", kernel=5, pad=0, stride=1, order="NCHW") pool1, maxid1 = conv1.MaxPool([], ["pool1", "maxid1"], kernel=2, stride=2, order="NCHW") conv2 = pool1.Conv([filter2, bias2], "conv2", kernel=5, pad=0, stride=1, order="NCHW") pool2, maxid2 = conv2.MaxPool([], ["pool2", "maxid2"], kernel=2, stride=2, order="NCHW") flatten2 = pool2.Flatten([], "pool2_flatten") softmax = (flatten2.FC([W3, B3], "fc3").Relu([], "fc3_relu").FC( [W4, B4], "pred").Softmax([], "softmax")) # Cross entropy, and accuracy xent = softmax.LabelCrossEntropy([label], "xent") # The loss function. loss = xent.AveragedLoss([], ["loss"]) # Get gradient, skipping the input and flatten layers. train_net.AddGradientOperators() 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) return init_net, train_net
def __init__(self, order, name=None): if name is None: name = "CNN" self.net = core.Net(name) self.param_init_net = core.Net(name + '_init') self.params = [] self.weights = [] self.biases = [] self.order = order if self.order != "NHWC" and self.order != "NCHW": raise ValueError("Cannot understand the CNN storage order %s." % self.order)
def testCreateWorkspace(self): workspaces = workspace.Workspaces() self.assertEqual(len(workspaces), 1) self.assertEqual(workspaces[0], "default") self.net = core.Net("test-net") self.net.ConstantFill([], "testblob", shape=[1, 2, 3, 4], value=1.0) self.assertEqual( workspace.RunNetOnce(self.net.Proto().SerializeToString()), True) self.assertEqual(workspace.HasBlob("testblob"), True) self.assertEqual(workspace.SwitchWorkspace("test", True), True) self.assertEqual(workspace.HasBlob("testblob"), False) self.assertEqual(workspace.SwitchWorkspace("default"), True) self.assertEqual(workspace.HasBlob("testblob"), True) try: # The following should raise an error. workspace.SwitchWorkspace("non-existing") # so this should never happen. self.assertEqual(True, False) except RuntimeError: pass workspaces = workspace.Workspaces() self.assertEqual(len(workspaces), 2) workspaces.sort() self.assertEqual(workspaces[0], "default") self.assertEqual(workspaces[1], "test")
def RunningAllreduceWithGPUs(self, gpu_ids, allreduce_function): """A base function to test different scenarios.""" workspace.ResetWorkspace() net = core.Net("mujitest") for id in gpu_ids: net.ConstantFill([], "testblob_gpu_" + str(id), shape=[1, 2, 3, 4], value=float(id + 1), device_option=muji.OnGPU(id)) allreduce_function(net, ["testblob_gpu_" + str(i) for i in gpu_ids], "_reduced", gpu_ids) workspace.RunNetOnce(net) target_value = sum(gpu_ids) + len(gpu_ids) all_blobs = workspace.Blobs() all_blobs.sort() for blob in all_blobs: print blob, workspace.FetchBlob(blob) for id in gpu_ids: blob = workspace.FetchBlob("testblob_gpu_" + str(i) + "_reduced") np.testing.assert_array_equal(blob, target_value, err_msg="gpu id %d of %s" % (id, str(gpu_ids)))
from pycaffe2 import core from pycaffe2 import core_gradients init_net = core.Net("init") W1 = init_net.UniformFill([], "W1", shape=[256, 784], min=-0.1, max=0.1) B1 = init_net.ConstantFill([], "B1", shape=[256], value=0.0) W2 = init_net.UniformFill([], "W2", shape=[10, 256], min=-0.1, max=0.1) B2 = init_net.ConstantFill([], "B2", shape=[10], value=0.0) LR = init_net.ConstantFill([], "LR", shape=[1], value=-0.1) ONE = init_net.ConstantFill([], "ONE", shape=[1], value=1.0) DECAY = init_net.ConstantFill([], "DECAY", shape=[1], value=0.999) train_net = core.Net("train") data, label = train_net.TensorProtosDBInput( [], ["data", "label"], batch_size=64, db="gen/data/mnist/mnist-train-nchw-minidb", db_type="minidb") # If you would like to give names to the individual blobs, you can do the # following: # softmax = (data.Flatten([], "data_flatten") # .FC([W1, B1], "hidden") # .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")
def setUp(self): self.net = core.Net("test-net") self.net.ConstantFill([], "testblob", shape=[1, 2, 3, 4], value=1.0) workspace.ResetWorkspace()
def setUp(self): self.net = core.Net("test-net") self.net.ConstantFill([], "testblob", shape=[1, 2, 3, 4], value=1.0) self.net.RunAllOnGPU()
from pycaffe2 import core from pycaffe2 import core_gradients init_net = core.Net("init") params = [] LR = init_net.ConstantFill([], "LR", shape=[1], value=-0.1) ONE = init_net.ConstantFill([], "ONE", shape=[1], value=1.0) DECAY = init_net.ConstantFill([], "DECAY", shape=[1], value=0.999) order = "NCHW" test_net = core.Net("train") data, label = test_net.ImageInput( [], ["data", "label"], batch_size=64, db="/media/data/jiayq/Data/ILSVRC12/caffe2-train-lmdb", db_type='lmdb', mean=128., std=128., scale=256, crop=227, mirror=1) if order == "NCHW": data = data.NHWC2NCHW() filter1 = init_net.XavierFill([], "filter1", shape=[96, 3, 11, 11]) bias1 = init_net.ConstantFill([], "bias1", shape=[96,], value=0.0) pool1, _ = (data.Conv([filter1, bias1], kernel=11, pad=0, stride=4, order=order) .Relu() .LRN(outputs=2, size=5, alpha=0.0001, beta=0.75, order=order)[0] .MaxPool(outputs=2, kernel=3, stride=2, order=order)) pool1a, pool1b = pool1.DepthSplit(outputs=2, channels=[48, 48], order=order) filter2a = init_net.XavierFill([], "filter2a", shape=[128, 48, 5, 5]) bias2a = init_net.ConstantFill([], "bias2a", shape=[128,], value=0.0) filter2b = init_net.XavierFill([], "filter2b", shape=[128, 48, 5, 5]) bias2b = init_net.ConstantFill([], "bias2b", shape=[128,], value=0.0)