コード例 #1
0
ファイル: memonger_test.py プロジェクト: Ralfhund/caffe2
    def test_topological_sort_longest_path_multi_target(self):
        # two outputs: conv2 and data4
        m = model_helper.ModelHelper()
        # 0
        m.Copy("conv0_w_comp", "conv0_w")
        # 1
        conv0 = brew.conv(m, "data", "conv0", 32, 32, 4)
        # 2
        m.Copy("conv2_w", "conv2_w")
        # 3
        brew.conv(m, conv0, "conv2", 16, 32, 4)
        # 4
        m.Copy("data1", "data2")
        # 5
        m.Copy("data2", "data3")

        g = memonger.compute_interference_graph(m.net.Proto().op)

        orders_org = memonger.topological_sort_traversal(g)
        orders_gt_org = [4, 5, 2, 0, 1, 3]
        self.assertEqual(orders_gt_org, list(orders_org))

        orders = memonger.topological_sort_traversal_longest_path(g)
        # longer path is in front of the shorter one
        orders_gt = [0, 1, 2, 3, 4, 5]
        self.assertEqual(orders_gt, list(orders))
コード例 #2
0
ファイル: brew_test.py プロジェクト: darthsuogles/caphi
    def test_model_helper(self):
        X = np.random.rand(64, 32, 32, 3).astype(np.float32) - 0.5

        workspace.FeedBlob("x", X)
        my_arg_scope = {'order': 'NHWC'}
        model = ModelHelper(name="test_model", arg_scope=my_arg_scope)
        with brew.arg_scope(
            brew.conv,
            stride=2,
            pad=2,
            weight_init=('XavierFill', {}),
            bias_init=('ConstantFill', {})
        ):
            brew.conv(
                model=model,
                blob_in="x",
                blob_out="out",
                dim_in=3,
                dim_out=64,
                kernel=3,
            )
        model.Validate()
        workspace.RunNetOnce(model.param_init_net)
        workspace.RunNetOnce(model.net)
        out = workspace.FetchBlob("out")
        self.assertEqual(out.shape, (64, 17, 17, 64))
コード例 #3
0
ファイル: MNIST.py プロジェクト: Yangqing/caffe2
def AddLeNetModel(model, data):
    '''
    This part is the standard LeNet model: from data to the softmax prediction.
    
    For each convolutional layer we specify dim_in - number of input channels
    and dim_out - number or output channels. Also each Conv and MaxPool layer changes the
    image size. For example, kernel of size 5 reduces each side of an image by 4.

    While when we have kernel and stride sizes equal 2 in a MaxPool layer, it divides
    each side in half.
    '''
    # Image size: 28 x 28 -> 24 x 24
    conv1 = brew.conv(model, data, 'conv1', dim_in=1, dim_out=20, kernel=5)
    # Image size: 24 x 24 -> 12 x 12
    pool1 = model.net.MaxPool(conv1, 'pool1', kernel=2, stride=2)
    # Image size: 12 x 12 -> 8 x 8
    conv2 = brew.conv(model, pool1, 'conv2', dim_in=20, dim_out=50, kernel=5)
    # Image size: 8 x 8 -> 4 x 4
    pool2 = model.net.MaxPool(conv2, 'pool2', kernel=2, stride=2)
    # 50 * 4 * 4 stands for dim_out from previous layer multiplied by the image size
    fc3 = brew.fc(model, pool2, 'fc3', dim_in=50 * 4 * 4, dim_out=500)
    fc3 = model.net.Relu(fc3, 'relu3')
    pred = brew.fc(model, fc3, 'pred', 500, 10)
    softmax = model.net.Softmax(pred, 'softmax')
    return softmax
コード例 #4
0
    def test_mobile_exporter(self):
        model = ModelHelper(name="mobile_exporter_test_model")
        # Test LeNet
        brew.conv(model, 'data', 'conv1', dim_in=1, dim_out=20, kernel=5)
        brew.max_pool(model, 'conv1', 'pool1', kernel=2, stride=2)
        brew.conv(model, 'pool1', 'conv2', dim_in=20, dim_out=50, kernel=5)
        brew.max_pool(model, 'conv2', 'pool2', kernel=2, stride=2)
        brew.fc(model, 'pool2', 'fc3', dim_in=50 * 4 * 4, dim_out=500)
        brew.relu(model, 'fc3', 'fc3')
        brew.fc(model, 'fc3', 'pred', 500, 10)
        brew.softmax(model, 'pred', 'out')

        # Create our mobile exportable networks
        workspace.RunNetOnce(model.param_init_net)
        init_net, predict_net = mobile_exporter.Export(
            workspace, model.net, model.params
        )

        # Populate the workspace with data
        np_data = np.random.rand(1, 1, 28, 28).astype(np.float32)
        workspace.FeedBlob("data", np_data)

        workspace.CreateNet(model.net)
        workspace.RunNet(model.net)
        ref_out = workspace.FetchBlob("out")

        # Clear the workspace
        workspace.ResetWorkspace()

        # Populate the workspace with data
        workspace.RunNetOnce(init_net)
        # Fake "data" is populated by init_net, we have to replace it
        workspace.FeedBlob("data", np_data)

        # Overwrite the old net
        workspace.CreateNet(predict_net, True)
        workspace.RunNet(predict_net.name)
        manual_run_out = workspace.FetchBlob("out")
        np.testing.assert_allclose(
            ref_out, manual_run_out, atol=1e-10, rtol=1e-10
        )

        # Clear the workspace
        workspace.ResetWorkspace()

        # Predictor interface test (simulates writing to disk)
        predictor = workspace.Predictor(
            init_net.SerializeToString(), predict_net.SerializeToString()
        )

        # Output is a vector of outputs but we only care about the first and only result
        predictor_out = predictor.run([np_data])
        assert len(predictor_out) == 1
        predictor_out = predictor_out[0]

        np.testing.assert_allclose(
            ref_out, predictor_out, atol=1e-10, rtol=1e-10
        )
コード例 #5
0
ファイル: rewrite_graph_test.py プロジェクト: Sissipei/caffe2
def simple_cnn():
    model = ModelHelper(name="r", arg_scope={"order": "NCHW", "is_test": True})
    brew.conv(
        model, "data", 'conv1', 3, 16, kernel=3, stride=1
    )
    brew.spatial_bn(
        model, 'conv1', 'conv1_spatbn', 16, epsilon=1e-3
    )
    brew.relu(model, 'conv1_spatbn', 'relu1')
    return model, (1, 3, 32, 32)
コード例 #6
0
    def testShapeInferenceConvNet(self):
        model = model_helper.ModelHelper(name="convtest")
        model.NHWC2NCHW("data", "data_nchw")
        brew.conv(model, "data_nchw", 'conv1', 3, 64,
                   weight_init=("MSRAFill", {}), kernel=7,
                   stride=2, pad=3, no_bias=0)
        brew.spatial_bn(model, 'conv1', 'conv1_spatbn_relu', 64, epsilon=1e-3, is_test=False)
        brew.relu(model, 'conv1_spatbn_relu', 'conv1_spatbn_relu')
        brew.max_pool(model, 'conv1_spatbn_relu', 'pool1', kernel=3, stride=2)
        brew.fc(model, 'pool1', 'fc', dim_in=(64 * 56 * 56), dim_out=100)
        brew.dropout(model, 'fc', 'fc_drop', is_test=False)
        model.Sigmoid('fc_drop', 'fc_sigm')
        brew.softmax(model, 'fc_sigm', 'softmax')
        model.LabelCrossEntropy(['softmax', 'label'], 'xent')
        loss = model.AveragedLoss('xent', 'loss')

        model.AddGradientOperators([loss])

        LR = model.param_init_net.ConstantFill(
            [], 'LR', shape=[1], value=0.1
        )

        for param in model.GetParams():
            param_grad = model.param_to_grad[param]
            param_momentum = model.param_init_net.ConstantFill(
                [param], param + '_momentum', value=0.0
            )
            model.net.MomentumSGDUpdate(
                [param_grad, param_momentum, LR, param],
                [param_grad, param_momentum, param],
            )

        workspace.FeedBlob(
            "data",
            np.random.rand(16, 227, 227, 3).astype(np.float32),
        )
        workspace.FeedBlob(
            "label",
            (100 * np.random.rand(16)).astype(np.int32),
        )
        workspace.FeedBlob(
            "label",
            (100 * np.random.rand(16)).astype(np.int32),
        )
        # Then do automatic comparison test: run the next once to
        # initialize everything
        self.InferTensorRunAndCompare(model)
コード例 #7
0
ファイル: optimizer_test.py プロジェクト: Sissipei/caffe2
    def test_optimizer_context(self):
        from caffe2.python import brew, optimizer
        from caffe2.python.model_helper import ModelHelper

        model = ModelHelper(name="test", arg_scope={'order': 'NCHW'})
        count = optimizer._optimizer_instance_count['SgdOptimizer']
        cnv_optim = SgdOptimizer(0.15)
        weight_optim = SgdOptimizer(0.2)
        bias_optim = SgdOptimizer(0.1)

        with UseOptimizer(cnv_optim):
            cnv = brew.conv(model, 'data', 'cnv', 32, 32, 4)
        with UseOptimizer({'WEIGHT': weight_optim, 'BIAS': bias_optim}):
            a = brew.fc(model, cnv, 'a', 100, 200)
        pred = brew.fc(model, a, 'b', 200, 5)
        (softmax, loss) = model.SoftmaxWithLoss(
            [pred, 'label'],
            ['softmax', 'loss'],
        )
        model.AddGradientOperators([loss])

        add_weight_decay(model, weight_decay=1e-4)
        # use the following optimizer if none specified in param_info
        build_sgd(model, 0.11)
        expected_weight_grad = {'b_w_grad', 'a_w_grad', 'cnv_w_grad'}
        expected_learning_rate = {
            "SgdOptimizer_{}_lr_cpu".format(count): -0.15,
            "SgdOptimizer_{}_lr_cpu".format(count + 1): -0.2,
            "SgdOptimizer_{}_lr_cpu".format(count + 2): -0.1,
            "SgdOptimizer_{}_lr_cpu".format(count + 3): -0.11
        }

        for op in model.net.Proto().op:
            # Check the proto that all weights are decayed and not non-weights
            # are decayed.
            if op.type == 'WeightedSum' and 'wd_0_0' in op.input:
                if op.output[0] not in expected_weight_grad:
                    print(
                        "Unexpected param for weight_decay: {}".
                        format(op.output[0])
                    )
                self.assertTrue(op.output[0] in expected_weight_grad)
                expected_weight_grad.remove(op.output[0])
            # Check the learning rate for each parameter
            if op.type == 'LearningRate':
                val = 0
                for arg in op.arg:
                    if arg.name == 'base_lr':
                        val = arg.f
                self.assertAlmostEqual(
                    val,
                    expected_learning_rate[op.output[0]]
                )

        self.assertEqual(
            expected_weight_grad,
            set(),
            "Not all weights were decayed: {}".format(expected_weight_grad)
        )
コード例 #8
0
ファイル: resnet.py プロジェクト: bittnt/caffe2
    def add_simple_block(
        self,
        input_filters,
        num_filters,
        down_sampling=False,
        spatial_batch_norm=True
    ):
        self.comp_idx = 0
        shortcut_blob = self.prev_blob

        # 3x3
        self.add_conv(
            input_filters,
            num_filters,
            kernel=3,
            stride=(1 if down_sampling is False else 2),
            pad=1
        )

        if spatial_batch_norm:
            self.add_spatial_bn(num_filters)
        self.add_relu()

        last_conv = self.add_conv(num_filters, num_filters, kernel=3, pad=1)
        if spatial_batch_norm:
            last_conv = self.add_spatial_bn(num_filters)

        # Increase of dimensions, need a projection for the shortcut
        if (num_filters != input_filters):
            shortcut_blob = brew.conv(
                self.model,
                shortcut_blob,
                'shortcut_projection_%d' % self.comp_count,
                input_filters,
                num_filters,
                weight_init=("MSRAFill", {}),
                kernel=1,
                stride=(1 if down_sampling is False else 2),
                no_bias=self.no_bias,
            )
            if spatial_batch_norm:
                shortcut_blob = brew.spatial_bn(
                    self.model,
                    shortcut_blob,
                    'shortcut_projection_%d_spatbn' % self.comp_count,
                    num_filters,
                    epsilon=1e-3,
                    is_test=self.is_test,
                )

        self.prev_blob = brew.sum(
            self.model, [shortcut_blob, last_conv],
            'comp_%d_sum_%d' % (self.comp_count, self.comp_idx)
        )
        self.comp_idx += 1
        self.add_relu()

        # Keep track of number of high level components if this ResNetBuilder
        self.comp_count += 1
コード例 #9
0
ファイル: brew_test.py プロジェクト: darthsuogles/caphi
 def test_param_consistence(self):
     model = ModelHelper(name='test_mode')
     cnv = brew.conv(model, 'data', 'cnv', 32, 32, 4)
     step_model = ModelHelper(name='step_model', param_model=model)
     a = brew.fc(step_model, cnv, 'a', 100, 200)
     brew.fc(model, a, 'b', 200, 5)
     # test the _parameters_info is shared between model and step_model
     self.assertEqual(model._parameters_info, step_model._parameters_info)
コード例 #10
0
ファイル: cnn.py プロジェクト: Yangqing/caffe2
 def Conv(self, *args, **kwargs):
     return brew.conv(
         self,
         *args,
         use_cudnn=self.use_cudnn,
         order=self.order,
         cudnn_exhaustive_search=self.cudnn_exhaustive_search,
         ws_nbytes_limit=self.ws_nbytes_limit,
         **kwargs
     )
コード例 #11
0
ファイル: memonger_test.py プロジェクト: darthsuogles/caphi
    def test_topological_sort_longest_path(self):
        m = model_helper.ModelHelper()
        # 0
        m.Copy("conv0_w_comp", "conv0_w")
        # 1
        conv0 = brew.conv(m, "data", "conv0", 32, 32, 4)
        # 2
        m.Copy("conv2_w", "conv2_w")
        # 3
        brew.conv(m, conv0, "conv2", 16, 32, 4)

        g = memonger.compute_interference_graph(m.net.Proto().op)

        orders_org = memonger.topological_sort_traversal(g)
        orders_gt_org = [2, 0, 1, 3]
        self.assertEqual(orders_gt_org, orders_org)

        orders = memonger.topological_sort_traversal_longest_path(g)
        # longer path is in front of the shorter one
        orders_gt = [0, 1, 2, 3]
        self.assertEqual(orders_gt, orders)
コード例 #12
0
ファイル: resnet.py プロジェクト: bittnt/caffe2
def create_resnet_32x32(
    model, data, num_input_channels, num_groups, num_labels, is_test=False
):
    '''
    Create residual net for smaller images (sec 4.2 of He et. al (2015))
    num_groups = 'n' in the paper
    '''
    # conv1 + maxpool
    brew.conv(
        model, data, 'conv1', num_input_channels, 16, kernel=3, stride=1
    )
    brew.spatial_bn(
        model, 'conv1', 'conv1_spatbn', 16, epsilon=1e-3, is_test=is_test
    )
    brew.relu(model, 'conv1_spatbn', 'relu1')

    # Number of blocks as described in sec 4.2
    filters = [16, 32, 64]

    builder = ResNetBuilder(model, 'relu1', is_test=is_test)
    prev_filters = 16
    for groupidx in range(0, 3):
        for blockidx in range(0, 2 * num_groups):
            builder.add_simple_block(
                prev_filters if blockidx == 0 else filters[groupidx],
                filters[groupidx],
                down_sampling=(True if blockidx == 0 and
                               groupidx > 0 else False))
        prev_filters = filters[groupidx]

    # Final layers
    brew.average_pool(
        model, builder.prev_blob, 'final_avg', kernel=8, stride=1
    )
    brew.fc(model, 'final_avg', 'last_out', 64, num_labels)
    softmax = brew.softmax(model, 'last_out', 'softmax')
    return softmax
コード例 #13
0
ファイル: core_test.py プロジェクト: RichieMay/pytorch
    def test_extract_simple(self):
        from caffe2.python import brew
        from caffe2.python.model_helper import ModelHelper, ExtractPredictorNet

        model = ModelHelper(name="test", arg_scope={'order': 'NCHW'})
        [data, label] = brew.image_input(
            model,
            "reader", ["xx/data", "label"],
            is_test=1,
        )
        cnv = brew.conv(model, data, 'cnv', 32, 32, 4)
        a = brew.fc(model, cnv, 'a', 100, 200)
        pred = brew.fc(model, a, 'pred', 200, 5)
        brew.softmax(model, [pred, label], "softmax")

        (predict_net, export_blobs) = ExtractPredictorNet(
            net_proto=model.net.Proto(),
            input_blobs=["xx/data"],
            output_blobs=["pred"],
            renames={"xx/data": "image"},
        )
        export_blobs = set(export_blobs)

        ops = list(predict_net.Proto().op)
        for op in ops:
            self.assertFalse(op.type == "Softmax")
            self.assertFalse("xx/data" in op.input)

        # Note: image input should not be included
        self.assertEquals(ops[0].type, "Conv")
        self.assertEquals(ops[1].type, "FC")
        self.assertEquals(ops[2].type, "FC")
        self.assertEquals(len(ops), 3)

        # test rename happened
        self.assertEquals(ops[0].input[0], "image")

        # Check export blobs
        self.assertTrue("image" not in export_blobs)
        self.assertTrue("xx/data" not in export_blobs)
        self.assertEqual(set([str(p) for p in model.params]), export_blobs)

        # Check external inputs/outputs
        self.assertTrue("image" in predict_net.Proto().external_input)
        self.assertEquals(set(["pred"]), set(predict_net.Proto().external_output))
        self.assertEqual(
            set(predict_net.Proto().external_input) -
            set([str(p) for p in model.params]), set(["image"])
        )
コード例 #14
0
ファイル: resnet.py プロジェクト: bittnt/caffe2
 def add_conv(self, in_filters, out_filters, kernel, stride=1, pad=0):
     self.comp_idx += 1
     self.prev_blob = brew.conv(
         self.model,
         self.prev_blob,
         'comp_%d_conv_%d' % (self.comp_count, self.comp_idx),
         in_filters,
         out_filters,
         weight_init=("MSRAFill", {}),
         kernel=kernel,
         stride=stride,
         pad=pad,
         no_bias=self.no_bias,
     )
     return self.prev_blob
コード例 #15
0
    def test_tensorboard_graphs(self):
        model = model_helper.ModelHelper(name="overfeat")
        data, label = brew.image_input(
            model, ["db"], ["data", "label"], is_test=0
        )
        with core.NameScope("conv1"):
            conv1 = brew.conv(model, data, "conv1", 3, 96, 11, stride=4)
            relu1 = brew.relu(model, conv1, conv1)
            pool1 = brew.max_pool(model, relu1, "pool1", kernel=2, stride=2)
        with core.NameScope("classifier"):
            fc = brew.fc(model, pool1, "fc", 4096, 1000)
            pred = brew.softmax(model, fc, "pred")
            xent = model.LabelCrossEntropy([pred, label], "xent")
            loss = model.AveragedLoss(xent, "loss")
        model.AddGradientOperators([loss], skip=1)

        c2_dir = tempfile.mkdtemp()
        tf_dir = tempfile.mkdtemp()

        with open(os.path.join(c2_dir, "init"), "w") as f:
            f.write(str(model.param_init_net.Proto()))
        with open(os.path.join(c2_dir, "net"), "w") as f:
            f.write(str(model.net.Proto()))
        runner = click.testing.CliRunner()
        result = runner.invoke(
            tb.cli,
            ["tensorboard-graphs",
             "--c2-netdef", os.path.join(c2_dir, "init"),
             "--c2-netdef", os.path.join(c2_dir, "net"),
             "--tf-dir", tf_dir])
        self.assertEqual(result.exit_code, 0)
        entries = list(os.walk(tf_dir))
        self.assertEqual(len(entries), 1)
        ((d, _, (fname,)),) = entries
        self.assertEqual(tf_dir, d)
        events = list(tf.train.summary_iterator(os.path.join(tf_dir, fname)))
        self.assertEqual(len(events), 3)
        events = events[1:]
        nets = [model.param_init_net, model.net]
        for i, (event, net) in enumerate(zip(events, nets), start=1):
            self.assertEqual(event.step, i)
            self.assertEqual(event.wall_time, i)
            g = tf.GraphDef()
            g.ParseFromString(event.graph_def)
            self.assertMultiLineEqual(
                str(g),
                str(tb_exporter.nets_to_graph_def([net])))
コード例 #16
0
ファイル: workspace_test.py プロジェクト: darthsuogles/caphi
    def test_simple_transform(self, input_dim, output_dim, batch_size):
        m = model_helper.ModelHelper()
        fc1 = brew.fc(m, "data", "fc1", dim_in=input_dim, dim_out=output_dim)
        fc2 = brew.fc(m, fc1, "fc2", dim_in=output_dim, dim_out=output_dim)
        conv = brew.conv(m, fc2, "conv",
                            dim_in=output_dim,
                            dim_out=output_dim,
                            use_cudnn=True,
                            engine="CUDNN",
                            kernel=3)

        conv.Relu([], conv)\
           .Softmax([], "pred") \
           .LabelCrossEntropy(["label"], ["xent"]) \
           .AveragedLoss([], "loss")

        transformed_net_proto = workspace.ApplyTransform(
            "ConvToNNPack",
            m.net.Proto())

        self.assertEqual(transformed_net_proto.op[2].engine, "NNPACK")
コード例 #17
0
ファイル: workspace_test.py プロジェクト: sixTiger/caffe2
    def test_apply_transform_if_faster(self, value):

        init_net = core.Net("init_net")
        init_net.ConstantFill([], ["data"], shape=[5, 5, 5, 5], value=value)
        init_net.ConstantFill([], ["conv_w"], shape=[5, 5, 3, 3], value=value)
        init_net.ConstantFill([], ["conv_b"], shape=[5], value=value)

        self.assertEqual(
            workspace.RunNetOnce(init_net.Proto().SerializeToString()), True)

        m = model_helper.ModelHelper()
        conv = brew.conv(m, "data", "conv",
                            dim_in=5,
                            dim_out=5,
                            kernel=3,
                            use_cudnn=True,
                            engine="CUDNN")

        conv.Relu([], conv)\
           .Softmax([], "pred") \
           .AveragedLoss([], "loss")

        self.assertEqual(
            workspace.RunNetOnce(m.net.Proto().SerializeToString()), True)

        proto = workspace.ApplyTransformIfFaster(
            "ConvToNNPack",
            m.net.Proto(),
            init_net.Proto())
        self.assertEqual(
            workspace.RunNetOnce(proto.SerializeToString()), True)
        proto = workspace.ApplyTransformIfFaster(
            "ConvToNNPack",
            m.net.Proto(),
            init_net.Proto(),
            warmup_runs=10,
            main_runs=100,
            improvement_threshold=2.0)
        self.assertEqual(
            workspace.RunNetOnce(proto.SerializeToString()), True)
コード例 #18
0
ファイル: optimizer_test.py プロジェクト: Sissipei/caffe2
    def test_weight_decay(self):
        from caffe2.python import brew
        from caffe2.python.model_helper import ModelHelper

        model = ModelHelper(name="test", arg_scope={'order': 'NCHW'})
        cnv = brew.conv(model, 'data', 'cnv', 32, 32, 4)
        a = brew.fc(model, cnv, 'a', 100, 200)
        pred = brew.fc(model, a, 'b', 200, 5)
        (softmax, loss) = model.SoftmaxWithLoss(
            [pred, 'label'],
            ['softmax', 'loss'],
        )
        model.AddGradientOperators([loss])

        add_weight_decay(model, weight_decay=1e-4)
        build_sgd(model, 0.11)

        expected_weight_grad = {'b_w_grad', 'a_w_grad', 'cnv_w_grad'}

        # Check the proto that all weights are decayed and not non-weights
        # are decayed.
        for op in model.net.Proto().op:
            if op.type == 'WeightedSum' and 'wd_0_0' in op.input:
                if op.output[0] not in expected_weight_grad:
                    print(
                        "Unexpected param for weight_decay: {}".
                        format(op.output[0])
                    )
                self.assertTrue(op.output[0] in expected_weight_grad)
                expected_weight_grad.remove(op.output[0])

        self.assertEqual(
            expected_weight_grad,
            set(),
            "Not all weights were decayed: {}".format(expected_weight_grad)
        )
コード例 #19
0
ファイル: model_device_test.py プロジェクト: Ralfhund/caffe2
 def _MiniAlexNetNoDropout(self, order):
     # First, AlexNet using the cnn wrapper.
     model = model_helper.ModelHelper(name="alexnet")
     conv1 = brew.conv(
         model,
         "data",
         "conv1",
         3,
         16,
         11,
         ("XavierFill", {}),
         ("ConstantFill", {}),
         stride=4,
         pad=0
     )
     relu1 = brew.relu(model, conv1, "relu1")
     norm1 = brew.lrn(model, relu1, "norm1", size=5, alpha=0.0001, beta=0.75)
     pool1 = brew.max_pool(model, norm1, "pool1", kernel=3, stride=2)
     conv2 = brew.group_conv(
         model,
         pool1,
         "conv2",
         16,
         32,
         5,
         ("XavierFill", {}),
         ("ConstantFill", {"value": 0.1}),
         group=2,
         stride=1,
         pad=2
     )
     relu2 = brew.relu(model, conv2, "relu2")
     norm2 = brew.lrn(model, relu2, "norm2", size=5, alpha=0.0001, beta=0.75)
     pool2 = brew.max_pool(model, norm2, "pool2", kernel=3, stride=2)
     conv3 = brew.conv(
         model,
         pool2,
         "conv3",
         32,
         64,
         3,
         ("XavierFill", {'std': 0.01}),
         ("ConstantFill", {}),
         pad=1
     )
     relu3 = brew.relu(model, conv3, "relu3")
     conv4 = brew.group_conv(
         model,
         relu3,
         "conv4",
         64,
         64,
         3,
         ("XavierFill", {}),
         ("ConstantFill", {"value": 0.1}),
         group=2,
         pad=1
     )
     relu4 = brew.relu(model, conv4, "relu4")
     conv5 = brew.group_conv(
         model,
         relu4,
         "conv5",
         64,
         32,
         3,
         ("XavierFill", {}),
         ("ConstantFill", {"value": 0.1}),
         group=2,
         pad=1
     )
     relu5 = brew.relu(model, conv5, "relu5")
     pool5 = brew.max_pool(model, relu5, "pool5", kernel=3, stride=2)
     fc6 = brew.fc(
         model, pool5, "fc6", 1152, 1024, ("XavierFill", {}),
         ("ConstantFill", {"value": 0.1})
     )
     relu6 = brew.relu(model, fc6, "relu6")
     fc7 = brew.fc(
         model, relu6, "fc7", 1024, 1024, ("XavierFill", {}),
         ("ConstantFill", {"value": 0.1})
     )
     relu7 = brew.relu(model, fc7, "relu7")
     fc8 = brew.fc(
         model, relu7, "fc8", 1024, 5, ("XavierFill", {}),
         ("ConstantFill", {"value": 0.0})
     )
     pred = brew.softmax(model, fc8, "pred")
     xent = model.LabelCrossEntropy([pred, "label"], "xent")
     loss = model.AveragedLoss([xent], ["loss"])
     model.AddGradientOperators([loss])
     return model
コード例 #20
0
def _InceptionModule(
    model, input_blob, input_depth, output_name, conv1_depth, conv3_depths,
    conv5_depths, pool_depth
):
    # path 1: 1x1 conv
    conv1 = brew.conv(
        model, input_blob, output_name + ":conv1", input_depth, conv1_depth, 1,
        ('XavierFill', {}), ('ConstantFill', {})
    )
    conv1 = brew.relu(model, conv1, conv1)
    # path 2: 1x1 conv + 3x3 conv
    conv3_reduce = brew.conv(
        model, input_blob, output_name + ":conv3_reduce", input_depth,
        conv3_depths[0], 1, ('XavierFill', {}), ('ConstantFill', {})
    )
    conv3_reduce = brew.relu(model, conv3_reduce, conv3_reduce)
    conv3 = brew.conv(
        model,
        conv3_reduce,
        output_name + ":conv3",
        conv3_depths[0],
        conv3_depths[1],
        3,
        ('XavierFill', {}),
        ('ConstantFill', {}),
        pad=1
    )
    conv3 = brew.relu(model, conv3, conv3)
    # path 3: 1x1 conv + 5x5 conv
    conv5_reduce = brew.conv(
        model, input_blob, output_name + ":conv5_reduce", input_depth,
        conv5_depths[0], 1, ('XavierFill', {}), ('ConstantFill', {})
    )
    conv5_reduce = brew.relu(model, conv5_reduce, conv5_reduce)
    conv5 = brew.conv(
        model,
        conv5_reduce,
        output_name + ":conv5",
        conv5_depths[0],
        conv5_depths[1],
        5,
        ('XavierFill', {}),
        ('ConstantFill', {}),
        pad=2
    )
    conv5 = brew.relu(model, conv5, conv5)
    # path 4: pool + 1x1 conv
    pool = brew.max_pool(
        model,
        input_blob,
        output_name + ":pool",
        kernel=3,
        stride=1,
        pad=1
    )
    pool_proj = brew.conv(
        model, pool, output_name + ":pool_proj", input_depth, pool_depth, 1,
        ('XavierFill', {}), ('ConstantFill', {})
    )
    pool_proj = brew.relu(model, pool_proj, pool_proj)
    output = brew.concat(model, [conv1, conv3, conv5, pool_proj], output_name)
    return output
コード例 #21
0
 def _MiniAlexNetNoDropout(self, order):
     # First, AlexNet using the cnn wrapper.
     model = model_helper.ModelHelper(name="alexnet")
     conv1 = brew.conv(model,
                       "data",
                       "conv1",
                       3,
                       16,
                       11, ("XavierFill", {}), ("ConstantFill", {}),
                       stride=4,
                       pad=0)
     relu1 = brew.relu(model, conv1, "relu1")
     norm1 = brew.lrn(model,
                      relu1,
                      "norm1",
                      size=5,
                      alpha=0.0001,
                      beta=0.75)
     pool1 = brew.max_pool(model, norm1, "pool1", kernel=3, stride=2)
     conv2 = brew.group_conv(model,
                             pool1,
                             "conv2",
                             16,
                             32,
                             5, ("XavierFill", {}), ("ConstantFill", {
                                 "value": 0.1
                             }),
                             group=2,
                             stride=1,
                             pad=2)
     relu2 = brew.relu(model, conv2, "relu2")
     norm2 = brew.lrn(model,
                      relu2,
                      "norm2",
                      size=5,
                      alpha=0.0001,
                      beta=0.75)
     pool2 = brew.max_pool(model, norm2, "pool2", kernel=3, stride=2)
     conv3 = brew.conv(model,
                       pool2,
                       "conv3",
                       32,
                       64,
                       3, ("XavierFill", {
                           'std': 0.01
                       }), ("ConstantFill", {}),
                       pad=1)
     relu3 = brew.relu(model, conv3, "relu3")
     conv4 = brew.group_conv(model,
                             relu3,
                             "conv4",
                             64,
                             64,
                             3, ("XavierFill", {}), ("ConstantFill", {
                                 "value": 0.1
                             }),
                             group=2,
                             pad=1)
     relu4 = brew.relu(model, conv4, "relu4")
     conv5 = brew.group_conv(model,
                             relu4,
                             "conv5",
                             64,
                             32,
                             3, ("XavierFill", {}), ("ConstantFill", {
                                 "value": 0.1
                             }),
                             group=2,
                             pad=1)
     relu5 = brew.relu(model, conv5, "relu5")
     pool5 = brew.max_pool(model, relu5, "pool5", kernel=3, stride=2)
     fc6 = brew.fc(model, pool5, "fc6", 1152, 1024, ("XavierFill", {}),
                   ("ConstantFill", {
                       "value": 0.1
                   }))
     relu6 = brew.relu(model, fc6, "relu6")
     fc7 = brew.fc(model, relu6, "fc7", 1024, 1024, ("XavierFill", {}),
                   ("ConstantFill", {
                       "value": 0.1
                   }))
     relu7 = brew.relu(model, fc7, "relu7")
     fc8 = brew.fc(model, relu7, "fc8", 1024, 5, ("XavierFill", {}),
                   ("ConstantFill", {
                       "value": 0.0
                   }))
     pred = brew.softmax(model, fc8, "pred")
     xent = model.LabelCrossEntropy([pred, "label"], "xent")
     loss = model.AveragedLoss([xent], ["loss"])
     model.AddGradientOperators([loss])
     return model
コード例 #22
0
def AddNetModel_6(model, data):
    # 0 params, dims [1, 146, 146]
    input1 = data
    # 72 params, dims [8, 144, 144]
    conv2 = brew.conv(model,
                      input1,
                      'conv2',
                      dim_in=1,
                      dim_out=8,
                      kernel=3,
                      stride=1)
    # 0 params, dims [8, 144, 144]
    relu3 = brew.relu(model, conv2, 'relu3')
    # 128 params, dims [4, 72, 72]
    conv4 = brew.conv(model,
                      relu3,
                      'conv4',
                      dim_in=8,
                      dim_out=4,
                      kernel=2,
                      stride=2)
    # 1600 params, dims [16, 68, 68]
    conv5 = brew.conv(model,
                      conv4,
                      'conv5',
                      dim_in=4,
                      dim_out=16,
                      kernel=5,
                      stride=1)
    # 0 params, dims [16, 68, 68]
    relu6 = brew.relu(model, conv5, 'relu6')
    # 512 params, dims [8, 34, 34]
    conv7 = brew.conv(model,
                      relu6,
                      'conv7',
                      dim_in=16,
                      dim_out=8,
                      kernel=2,
                      stride=2)
    # 2304 params, dims [32, 32, 32]
    conv8 = brew.conv(model,
                      conv7,
                      'conv8',
                      dim_in=8,
                      dim_out=32,
                      kernel=3,
                      stride=1)
    # 0 params, dims [32, 32, 32]
    relu9 = brew.relu(model, conv8, 'relu9')
    # 2048 params, dims [16, 16, 16]
    conv10 = brew.conv(model,
                       relu9,
                       'conv10',
                       dim_in=32,
                       dim_out=16,
                       kernel=2,
                       stride=2)
    # 9216 params, dims [64, 14, 14]
    conv11 = brew.conv(model,
                       conv10,
                       'conv11',
                       dim_in=16,
                       dim_out=64,
                       kernel=3,
                       stride=1)
    # 0 params, dims [64, 14, 14]
    relu12 = brew.relu(model, conv11, 'relu12')
    # 8192 params, dims [32, 7, 7]
    conv13 = brew.conv(model,
                       relu12,
                       'conv13',
                       dim_in=64,
                       dim_out=32,
                       kernel=2,
                       stride=2)
    # 0 params, dims [32, 7, 7]
    relu14 = brew.relu(model, conv13, 'relu14')
    # 301056 params, dims [192]
    fc15 = brew.fc(model, relu14, 'fc15', dim_in=1568, dim_out=192)
    # 0 params, dims [192]
    relu16 = brew.relu(model, fc15, 'relu16')
    # 384 params, dims [2]
    output = brew.fc(model, relu16, 'output', dim_in=192, dim_out=2)
    # 325512 parameters total
    return output
コード例 #23
0
    def test_convolution_sync(self, net_type, num_workers, engine, gc, dc):
        m = ModelHelper(name="test_model")
        n = 1
        d = 2
        depth = 3
        iters = 5
        h = 5
        w = 5
        workspace.ResetWorkspace()

        use_cudnn = engine == "CUDNN"

        np.random.seed(1701)
        # Build a binary tree of conv layers, summing at each node.
        for i in reversed(range(depth)):
            for j in range(2**i):
                bottom_1 = "{}_{}".format(i + 1, 2 * j)
                bottom_2 = "{}_{}".format(i + 1, 2 * j + 1)
                mid_1 = "{}_{}_m".format(i + 1, 2 * j)
                mid_2 = "{}_{}_m".format(i + 1, 2 * j + 1)
                top = "{}_{}".format(i, j)
                w1, b1, w2, b2 = np.random.randn(4).tolist()
                brew.conv(
                    m,
                    bottom_1,
                    mid_1,
                    dim_in=d,
                    dim_out=d,
                    kernel=3,
                    weight_init=("ConstantFill", {
                        "value": w1
                    }),
                    bias_init=("ConstantFill", {
                        "value": b1
                    }),
                    cudnn_state=np.random.randint(0, 3),
                    stride=1,
                    pad=1,
                    deterministic=1,
                    use_cudnn=use_cudnn,
                    engine=engine,
                )
                brew.conv(
                    m,
                    bottom_2,
                    mid_2,
                    dim_in=d,
                    dim_out=d,
                    kernel=3,
                    stride=1,
                    pad=1,
                    weight_init=("ConstantFill", {
                        "value": w2
                    }),
                    bias_init=("ConstantFill", {
                        "value": b2
                    }),
                    deterministic=1,
                    cudnn_state=np.random.randint(0, 3),
                    use_cudnn=use_cudnn,
                    engine=engine,
                )
                m.net.Sum([mid_1, mid_2], top)

        m.net.Flatten(["0_0"], ["0_0_flat"])
        m.net.SquaredL2Distance(["0_0_flat", "label"], "xent")
        m.net.AveragedLoss("xent", "loss")
        input_to_grad = m.AddGradientOperators(["loss"])
        m.Proto().device_option.CopyFrom(gc)
        m.param_init_net.Proto().device_option.CopyFrom(gc)
        m.Proto().type = net_type
        m.Proto().num_workers = num_workers
        self.ws.run(m.param_init_net)

        def run():
            import numpy as np

            np.random.seed(1701)
            input_blobs = ["{}_{}".format(depth, j) for j in range(2**depth)]
            for input_blob in input_blobs:
                self.ws.create_blob(input_blob).feed(np.random.randn(
                    n, d, h, w).astype(np.float32),
                                                     device_option=gc)
                self.ws.create_blob("label").feed(np.random.randn(
                    n, d * h * w).astype(np.float32),
                                                  device_option=gc)
            self.ws.run(m.net)
            gradients = [
                self.ws.blobs[str(input_to_grad[input_blob])].fetch()
                for input_blob in input_blobs
            ]
            return gradients

        outputs = [run() for _ in range(iters)]
        for output in outputs[1:]:
            np.testing.assert_array_equal(outputs[0], output)
            np.testing.assert_allclose(np.sum(np.square(output)),
                                       1763719461732352.0,
                                       rtol=1e-5)
コード例 #24
0
ファイル: keras2caffe2.py プロジェクト: syilma/keras2caffe2
def create_caffe2_model(model,
                        input_shape,
                        use_cudnn=True,
                        init_params=False,
                        keras_channel_last=True):

    arg_scope = {'order': 'NCHW', 'use_cudnn': use_cudnn}
    caffe2_model = model_helper.ModelHelper(name='model',
                                            init_params=init_params,
                                            arg_scope=arg_scope)

    num_conv_layers = 0

    layer_num = 0
    layer_sizes = {}
    prev_layer_name = ''

    for layer in model.layers:

        inb_node = layer._inbound_nodes[0]
        num_input_layers = len(inb_node.inbound_layers)

        input_name_list = []

        for ii in range(0, num_input_layers):
            inp_layer = inb_node.inbound_layers[ii]

            input_name_list.append(inp_layer.name)
            prev_layer_name = inp_layer.name

            if isinstance(inp_layer, keras.layers.Flatten):
                pass
                #pinb_node = inp_layer._inbound_nodes[0]
                #prev_layer_name = pinb_node.inbound_layers[0].name

        name = layer.name

        config = layer.get_config()
        inputShape = layer.input_shape
        outputShape = layer.output_shape

        if isinstance(layer, keras.engine.input_layer.InputLayer):
            input_sizes = (input_shape[2], input_shape[3])
            layer_sizes[name] = input_sizes
        else:
            if (input_name_list[0] not in layer_sizes):
                raise ValueError("Can't find layer size for ",
                                 input_name_list[0])
            else:
                input_sizes = layer_sizes[input_name_list[0]]

        layer_dim = len(outputShape)
        if (layer_dim == 4):
            if (keras_channel_last):
                out_sizes = (outputShape[1], outputShape[2])
            else:
                out_sizes = (outputShape[2], outputShape[3])
        elif (layer_dim == 2):
            out_sizes = (0, 0)  #flattened
        else:
            raise ValueError(
                'Unsupported layer dimension : {0}'.format(layer_dim))

        if isinstance(layer, keras.layers.Flatten):
            tmp_prev = prev_layer_name

            if (keras_channel_last):
                tmp_prev = prev_layer_name + '_transpose'  #nb, img_h, img_w, chan <-- nb, chan, img_h, img_w
                c2_layer = brew.transpose(caffe2_model,
                                          prev_layer_name,
                                          tmp_prev,
                                          axes=(0, 2, 3, 1))

            c2_layer = caffe2_model.net.Flatten(tmp_prev, name)

            #print('FLatten previous layer ', prev_layer_name, ' current layer ', name , 'inputshape ', inputShape)

            layer_sizes[name] = out_sizes

        elif isinstance(layer, keras.layers.Dropout):
            #print('name is ', name, ' prev_layer_name ', prev_layer_name)
            c2_layer = caffe2_model.net.Dropout(prev_layer_name,
                                                name,
                                                is_test=True
                                                #ratio=config['rate']
                                                )

            #same size
            layer_sizes[name] = input_sizes

        elif (isinstance(layer, keras.layers.convolutional.Conv2D)):

            dim_in = inputShape[-1]
            dim_out = outputShape[-1]
            kernel = config['kernel_size'][0]
            stride = config['strides'][0]

            if (config['padding'] == 'same'):
                pad_sizes = get_padding_sizes(input_sizes,
                                              config['kernel_size'],
                                              config['strides'])
            elif (config['padding'] == 'valid'):
                pad_sizes = ((0, 0), (0, 0))
            else:
                raise ValueError('unsupported padding')

            #print('pad sizes ', pad_sizes)

            layer_sizes[name] = out_sizes

            c2_layer = brew.conv(caffe2_model,
                                 prev_layer_name,
                                 name,
                                 dim_in=dim_in,
                                 dim_out=dim_out,
                                 kernel=kernel,
                                 stride=stride,
                                 pad_l=pad_sizes[0][0],
                                 pad_r=pad_sizes[0][1],
                                 pad_t=pad_sizes[1][0],
                                 pad_b=pad_sizes[1][1])

            if config['activation'] == 'linear':
                pass
            elif config['activation'] == 'relu':
                c2_layer = brew.relu(caffe2_model, name, name)
            elif config['activation'] == 'softmax':
                #c2_layer = brew.softmax(caffe2_model, name, name)
                c2_layer = brew.softmax(caffe2_model, name, 'softmax')
            else:
                raise ValueError(
                    'The only supported activation for conv layer is relu')

        elif isinstance(layer, keras.layers.MaxPooling2D):
            kernel = config['pool_size'][0]
            stride = config['strides'][0]

            pad_size = ((0, 0), (0, 0))
            layer_sizes[name] = out_sizes

            c2_layer = brew.max_pool(caffe2_model,
                                     prev_layer_name,
                                     name,
                                     kernel=kernel,
                                     stride=stride)

        elif isinstance(layer, keras.layers.AveragePooling2D):
            kernel = config['pool_size'][0]
            stride = config['strides'][0]

            pad_size = ((0, 0), (0, 0))
            layer_sizes[name] = out_sizes

            c2_layer = brew.average_pool(caffe2_model,
                                         prev_layer_name,
                                         name,
                                         kernel=kernel,
                                         stride=stride)

        elif isinstance(layer, keras.layers.BatchNormalization):

            dim_in = inputShape[-1]
            epsilon = config['epsilon']
            momentum = config['momentum']
            c2_layer = brew.spatial_bn(caffe2_model,
                                       prev_layer_name,
                                       name,
                                       dim_in=dim_in,
                                       epsilon=epsilon,
                                       momentum=momentum,
                                       is_test=True)

            #same size
            layer_sizes[name] = input_sizes

        elif (isinstance(layer, keras.layers.core.Dense)):

            dim_in = inputShape[-1]
            dim_out = outputShape[-1]

            #print('input shape for dense is ', inputShape)
            if (len(inputShape) == 2):  #flattened input
                c2_layer = brew.fc(caffe2_model,
                                   prev_layer_name,
                                   name,
                                   dim_in=dim_in,
                                   dim_out=dim_out)
            else:  #fully convolutional input
                c2_layer = brew.conv(caffe2_model,
                                     prev_layer_name,
                                     name,
                                     dim_in=dim_in,
                                     dim_out=dim_out,
                                     kernel=1,
                                     stride=1)

            activation = config['activation']
            if activation == 'relu':
                c2_layer = brew.relu(caffe2_model, name, name)
            elif activation == 'softmax':
                c2_layer = brew.softmax(caffe2_model, name, 'softmax')
            elif activation == 'linear':
                pass  #
            else:
                raise ValueError(
                    'The only supported activations for fc layer are relu and softmax'
                )

            #same size
            layer_sizes[name] = input_sizes

        elif (isinstance(layer, keras.layers.advanced_activations.LeakyReLU)):

            dim_in = inputShape[-1]

            c2_layer = caffe2_model.net.LeakyRelu(prev_layer_name,
                                                  name,
                                                  alpha=config['alpha'])

            #same size
            layer_sizes[name] = input_sizes

        elif (isinstance(layer, keras.layers.merge.Add)):

            c2_layer = brew.sum(caffe2_model,
                                [input_name_list[0], input_name_list[1]], name)

            #same size
            layer_sizes[name] = input_sizes

    layer_num = layer_num + 1
    if (layer_num == len(model.layers)):
        caffe2_model.net.AddExternalOutput(c2_layer)

    return caffe2_model
コード例 #25
0
def VGGA(order, cudnn_ws, device):
    my_arg_scope = {'order': order, 'use_cudnn': True,
                    'cudnn_exhaustive_search': True,
                    'ws_nbytes_limit': str(cudnn_ws)}
    model = ModelHelper(name='vgg-a', arg_scope=my_arg_scope)
    conv1 = brew.conv(
        model,
        "data",
        "conv1",
        3,
        64,
        3,
        ('XavierFill', {}),
        ('ConstantFill', {}),
        pad=1
    )
    relu1 = brew.relu(model, conv1, "conv1")
    pool1 = brew.max_pool(model, relu1, "pool1", kernel=2, stride=2)
    conv2 = brew.conv(
        model,
        pool1,
        "conv2",
        64,
        128,
        3,
        ('XavierFill', {}),
        ('ConstantFill', {}),
        pad=1
    )
    relu2 = brew.relu(model, conv2, "conv2")
    pool2 = brew.max_pool(model, relu2, "pool2", kernel=2, stride=2)
    conv3 = brew.conv(
        model,
        pool2,
        "conv3",
        128,
        256,
        3,
        ('XavierFill', {}),
        ('ConstantFill', {}),
        pad=1
    )
    relu3 = brew.relu(model, conv3, "conv3")
    conv4 = brew.conv(
        model,
        relu3,
        "conv4",
        256,
        256,
        3,
        ('XavierFill', {}),
        ('ConstantFill', {}),
        pad=1
    )
    relu4 = brew.relu(model, conv4, "conv4")
    pool4 = brew.max_pool(model, relu4, "pool4", kernel=2, stride=2)
    conv5 = brew.conv(
        model,
        pool4,
        "conv5",
        256,
        512,
        3,
        ('XavierFill', {}),
        ('ConstantFill', {}),
        pad=1
    )
    relu5 = brew.relu(model, conv5, "conv5")
    conv6 = brew.conv(
        model,
        relu5,
        "conv6",
        512,
        512,
        3,
        ('XavierFill', {}),
        ('ConstantFill', {}),
        pad=1
    )
    relu6 = brew.relu(model, conv6, "conv6")
    pool6 = brew.max_pool(model, relu6, "pool6", kernel=2, stride=2)
    conv7 = brew.conv(
        model,
        pool6,
        "conv7",
        512,
        512,
        3,
        ('XavierFill', {}),
        ('ConstantFill', {}),
        pad=1
    )
    relu7 = brew.relu(model, conv7, "conv7")
    conv8 = brew.conv(
        model,
        relu7,
        "conv8",
        512,
        512,
        3,
        ('XavierFill', {}),
        ('ConstantFill', {}),
        pad=1
    )
    relu8 = brew.relu(model, conv8, "conv8")
    pool8 = brew.max_pool(model, relu8, "pool8", kernel=2, stride=2)

    fcix = brew.fc(
        model, pool8, "fcix", 512 * 7 * 7, 4096, ('XavierFill', {}),
        ('ConstantFill', {})
    )
    reluix = brew.relu(model, fcix, "fcix")
    fcx = brew.fc(
        model, reluix, "fcx", 4096, 4096, ('XavierFill', {}), ('ConstantFill', {})
    )
    relux = brew.relu(model, fcx, "fcx")
    fcxi = brew.fc(
        model, relux, "fcxi", 4096, 1000, ('XavierFill', {}), ('ConstantFill', {})
    )
    pred = brew.softmax(model, fcxi, "pred")
    xent = model.LabelCrossEntropy([pred, "label"], "xent")
    if device != 'MKL':
        loss = model.AveragedLoss(xent, "loss")
    return model, 231
コード例 #26
0
def nate_VGG_d(model, data):

    # (?) - what is default weight fillers of these layers
    # (?) - should i add dropout to the fc layers?
    # (*) - make sure the output dimension is kosher

    # Shape here = 3x224x224

    # Conv group
    conv1_1 = brew.conv(model,
                        data,
                        'conv1_1',
                        dim_in=3,
                        dim_out=64,
                        kernel=3,
                        pad=1)
    relu1_1 = brew.relu(model, conv1_1, 'relu1_1')
    conv1_2 = brew.conv(model,
                        relu1_1,
                        'conv1_2',
                        dim_in=64,
                        dim_out=64,
                        kernel=3,
                        pad=1)
    relu1_2 = brew.relu(model, conv1_2, 'relu1_2')

    # Shape here = 64x224x224

    # Max Pool
    pool1 = brew.max_pool(model, relu1_2, 'pool1', kernel=2, stride=2)

    # Shape here = 64x112x112

    # Conv group
    conv2_1 = brew.conv(model,
                        pool1,
                        'conv2_1',
                        dim_in=64,
                        dim_out=128,
                        kernel=3,
                        pad=1)
    relu2_1 = brew.relu(model, conv2_1, 'relu2_1')
    conv2_2 = brew.conv(model,
                        relu2_1,
                        'conv2_2',
                        dim_in=128,
                        dim_out=128,
                        kernel=3,
                        pad=1)
    relu2_2 = brew.relu(model, conv2_2, 'relu2_2')

    # Shape here = 128x112x112

    # Max Pool
    pool2 = brew.max_pool(model, relu2_2, 'pool2', kernel=2, stride=2)

    # Shape here = 128x56x56

    # Conv group
    conv3_1 = brew.conv(model,
                        pool2,
                        'conv3_1',
                        dim_in=128,
                        dim_out=256,
                        kernel=3,
                        pad=1)
    relu3_1 = brew.relu(model, conv3_1, 'relu3_1')
    conv3_2 = brew.conv(model,
                        relu3_1,
                        'conv3_2',
                        dim_in=256,
                        dim_out=256,
                        kernel=3,
                        pad=1)
    relu3_2 = brew.relu(model, conv3_2, 'relu3_2')
    conv3_3 = brew.conv(model,
                        relu3_2,
                        'conv3_3',
                        dim_in=256,
                        dim_out=256,
                        kernel=3,
                        pad=1)
    relu3_3 = brew.relu(model, conv3_3, 'relu3_3')

    # Shape here = 256x56x56

    # Max Pool
    pool3 = brew.max_pool(model, relu3_3, 'pool3', kernel=2, stride=2)

    # Shape here = 256x28x28

    # Conv group
    conv4_1 = brew.conv(model,
                        pool3,
                        'conv4_1',
                        dim_in=256,
                        dim_out=512,
                        kernel=3,
                        pad=1)
    relu4_1 = brew.relu(model, conv4_1, 'relu4_1')
    conv4_2 = brew.conv(model,
                        relu4_1,
                        'conv4_2',
                        dim_in=512,
                        dim_out=512,
                        kernel=3,
                        pad=1)
    relu4_2 = brew.relu(model, conv4_2, 'relu4_2')
    conv4_3 = brew.conv(model,
                        relu4_2,
                        'conv4_3',
                        dim_in=512,
                        dim_out=512,
                        kernel=3,
                        pad=1)
    relu4_3 = brew.relu(model, conv4_3, 'relu4_3')

    # Shape here = 512x28x28

    # Max Pool
    pool4 = brew.max_pool(model, relu4_3, 'pool4', kernel=2, stride=2)

    # Shape here = 512x14x14

    # Conv group
    conv5_1 = brew.conv(model,
                        pool4,
                        'conv5_1',
                        dim_in=512,
                        dim_out=512,
                        kernel=3,
                        pad=1)
    relu5_1 = brew.relu(model, conv5_1, 'relu5_1')
    conv5_2 = brew.conv(model,
                        relu5_1,
                        'conv5_2',
                        dim_in=512,
                        dim_out=512,
                        kernel=3,
                        pad=1)
    relu5_2 = brew.relu(model, conv5_2, 'relu5_2')
    conv5_3 = brew.conv(model,
                        relu5_2,
                        'conv5_3',
                        dim_in=512,
                        dim_out=512,
                        kernel=3,
                        pad=1)
    relu5_3 = brew.relu(model, conv5_3, 'relu5_3')

    # Shape here = 512x14x14

    # Max Pool
    pool5 = brew.max_pool(model, relu5_3, 'pool5', kernel=2, stride=2)

    # Shape here = 512x7x7

    fc1 = brew.fc(model, pool5, 'fc1', dim_in=512 * 7 * 7, dim_out=4096)
    relu1 = brew.relu(model, fc1, 'relu1')

    # Shape here = 1x4096

    fc2 = brew.fc(model, relu1, 'fc2', dim_in=4096, dim_out=4096)
    relu2 = brew.relu(model, fc2, 'relu2')

    # Shape here = 1x4096

    fc3 = brew.fc(model, relu2, 'fc3', dim_in=4096, dim_out=11)

    # Shape here = 1x11

    softmax = brew.softmax(model, fc3, 'softmax')

    return softmax
コード例 #27
0
def create_mobilenet(
        model, data, num_input_channels, num_labels, label, is_test=False
):
    '''
    Create residual net for smaller images (sec 4.2 of He et. al (2015))
    num_groups = 'n' in the paper
    '''
    # conv1
    brew.conv(
        model,
        data,
        'conv1',
        3,
        32,
        weight_init=("MSRAFill", {}),
        kernel=3,
        stride=2,
        pad=2,
        no_bias=True,
    )
    brew.spatial_bn(
        model, 'conv1', 'conv1_spatbn', 32, epsilon=1e-3, is_test=is_test
    )
    brew.relu(model, 'conv1_spatbn', 'relu1')

    builder = MobileNetBuilder(model, 'relu1', no_bias=True, is_test=is_test)

    # block1
    builder.add_simple_block(input_filters=32, output_filters=64, down_sampling=False, spatial_batch_norm=True)
    # block2
    builder.add_simple_block(input_filters=64, output_filters=128, down_sampling=True, spatial_batch_norm=True)
    # block3
    builder.add_simple_block(input_filters=128, output_filters=128, down_sampling=False, spatial_batch_norm=True)
    # block4
    builder.add_simple_block(input_filters=128, output_filters=256, down_sampling=True, spatial_batch_norm=True)
    # block5
    builder.add_simple_block(input_filters=256, output_filters=256, down_sampling=False, spatial_batch_norm=True)
    # block6
    builder.add_simple_block(input_filters=256, output_filters=512, down_sampling=True, spatial_batch_norm=True)
    # block7-11
    for i in xrange(7, 12):
        builder.add_simple_block(input_filters=512, output_filters=512, down_sampling=False, spatial_batch_norm=True)
    # block12
    builder.add_simple_block(input_filters=512, output_filters=1024, down_sampling=True, spatial_batch_norm=True)
    # block13
    builder.add_simple_block(input_filters=1024, output_filters=1024, down_sampling=False, spatial_batch_norm=True)

    # Final layers
    brew.average_pool(
        model, builder.prev_blob, 'final_avg', kernel=7, stride=7)
    last_out = brew.fc(model, 'final_avg', 'last_out', 1024, num_labels)

    if (label is not None):
        (softmax, loss) = model.SoftmaxWithLoss(
            [last_out, label],
            ['softmax', 'loss'],
        )

        return (softmax, loss)
    else:
        return brew.softmax(model, 'last_out', 'softmax')
コード例 #28
0
def Add_Action_Tufts_Model(model,
                           num_classes,
                           image_height,
                           image_width,
                           image_channels,
                           is_test=0):
    ################################## Block 1 ############################
    # Convolutional layer 1
    #    conv1_1 = brew.conv(model, 'data', 'conv1_1', dim_in=image_channels, dim_out=64, kernel=3, stride=2, pad=0)
    #    h,w = update_dims(height=image_height, width=image_width, kernel=3, stride=2, pad=0)
    # ReLU layer 1
    #    relu1_1 = brew.relu(model, conv1_1, 'relu1_1')
    # Batch normalization layer 1
    #    bn1_1 = brew.spatial_bn(model, relu1_1, 'bn1_1', dim_in=64, epsilon=1e-3, momentum=0.1, is_test=is_test)
    # Drop out with p=0.25
    #    dropout1_1 = brew.dropout(model, bn1_1, 'dropout1_1', ratio=0.25, is_test=is_test)

    # Convolutional layer 2
    #    conv1_2 = brew.conv(model, dropout1_1, 'conv1_2', dim_in=64, dim_out=64, kernel=3, stride=1, pad=0)
    #    h,w = update_dims(height=h, width=w, kernel=3, stride=1, pad=0)
    # ReLU layer 1
    #    relu1_2 = brew.relu(model, conv1_2, 'relu1_2')
    # Batch normalization layer 1
    #    bn1_2 = brew.spatial_bn(model, relu1_2, 'bn1_2', dim_in=64, epsilon=1e-3, momentum=0.1, is_test=is_test)
    # Drop out with p=0.25
    #    dropout1_2 = brew.dropout(model, bn1_2, 'dropout1_2', ratio=0.25, is_test=is_test)
    ##################################### Block 2 ##########################
    # Convolutional layer 3
    conv2_1 = brew.conv(model,
                        'data',
                        'conv2_1',
                        dim_in=image_channels,
                        dim_out=128,
                        kernel=3,
                        stride=2,
                        pad=0)
    h, w = update_dims(height=image_height,
                       width=image_width,
                       kernel=3,
                       stride=2,
                       pad=0)
    # ReLU layer 1
    relu2_1 = brew.relu(model, conv2_1, 'relu2_1')
    # Batch normalization layer 1
    bn2_1 = brew.spatial_bn(model,
                            relu2_1,
                            'bn2_1',
                            dim_in=128,
                            epsilon=1e-3,
                            momentum=0.1,
                            is_test=is_test)
    # Drop out with p=0.25
    dropout2_1 = brew.dropout(model,
                              bn2_1,
                              'dropout2_1',
                              ratio=0.25,
                              is_test=is_test)

    # Convolutional layer 4
    conv2_2 = brew.conv(model,
                        dropout2_1,
                        'conv2_2',
                        dim_in=128,
                        dim_out=128,
                        kernel=3,
                        stride=1,
                        pad=0)
    h, w = update_dims(height=h, width=w, kernel=3, stride=1, pad=0)
    # ReLU layer 1
    relu2_2 = brew.relu(model, conv2_2, 'relu2_2')
    # Batch normalization layer 1
    bn2_2 = brew.spatial_bn(model,
                            relu2_2,
                            'bn2_2',
                            dim_in=128,
                            epsilon=1e-3,
                            momentum=0.1,
                            is_test=is_test)
    # Drop out with p=0.25
    dropout2_2 = brew.dropout(model,
                              bn2_2,
                              'dropout2_2',
                              ratio=0.25,
                              is_test=is_test)
    ##################################### Block 3 ############################
    # Convolutional layer 5
    conv3_1 = brew.conv(model,
                        dropout2_2,
                        'conv3_1',
                        dim_in=128,
                        dim_out=256,
                        kernel=3,
                        stride=2,
                        pad=0)
    h, w = update_dims(height=h, width=w, kernel=3, stride=2, pad=0)
    # ReLU layer 1
    relu3_1 = brew.relu(model, conv3_1, 'relu3_1')
    # Batch normalization layer 1
    bn3_1 = brew.spatial_bn(model,
                            relu3_1,
                            'bn3_1',
                            dim_in=256,
                            epsilon=1e-3,
                            momentum=0.1,
                            is_test=is_test)
    # Drop out with p=0.25
    dropout3_1 = brew.dropout(model,
                              bn3_1,
                              'dropout3_1',
                              ratio=0.25,
                              is_test=is_test)

    # Convolutional layer 4
    conv3_2 = brew.conv(model,
                        dropout3_1,
                        'conv3_2',
                        dim_in=256,
                        dim_out=256,
                        kernel=3,
                        stride=1,
                        pad=0)
    h, w = update_dims(height=h, width=w, kernel=3, stride=1, pad=0)
    # ReLU layer 1
    relu3_2 = brew.relu(model, conv3_2, 'relu3_2')
    # Batch normalization layer 1
    bn3_2 = brew.spatial_bn(model,
                            relu3_2,
                            'bn3_2',
                            dim_in=256,
                            epsilon=1e-3,
                            momentum=0.1,
                            is_test=is_test)
    # Drop out with p=0.25
    dropout3_2 = brew.dropout(model,
                              bn3_2,
                              'dropout3_2',
                              ratio=0.25,
                              is_test=is_test)

    # Global average pooling
    pool1 = brew.average_pool(model, dropout3_2, 'pool1', global_pooling=True)
    # Fully connected layers
    pred = brew.fc(model, pool1, 'fc1', dim_in=256, dim_out=num_classes)
    # Softmax layer
    softmax, loss = model.SoftmaxWithLoss([pred, 'label'], ['softmax', 'loss'])
    brew.accuracy(model, [softmax, 'label'], 'accuracy')
    model.net.MultiClassAccuracy([softmax, 'label'],
                                 ['accuracy_per_class', 'amount_per_class'])
    return [loss]
コード例 #29
0
ファイル: ResNet.py プロジェクト: shawnwinder/caffe2-meProp
def meta_conv_conv_topk(
    model,
    inputs,
    dim_in,
    dim_out,
    kernel,
    pad,
    stride,
    topk=5,
    no_bias=False,
    is_test=False,
    has_relu=False,
    module_seq=None, # group seq
    sub_seq=None, # block seq
    branch_seq=None,
    conv_seq=None
):
    '''
    add basic conv module of resnet 50
    '''
    # set projection branch
    if branch_seq == "2": # branch of 2 normal part
        conv_name = "conv{}".format(conv_seq)
    elif branch_seq == "1" : # branch of 1 projection part
        conv_name = "proj"

    # convolution layer
    conv = brew.conv(
        model,
        inputs,
        'group{}_block{}_{}'.format(module_seq, sub_seq, conv_name),
        dim_in=dim_in,
        dim_out=dim_out,
        kernel=kernel,
        stride=stride,
        pad=pad,
        no_bias=no_bias,
    )

    conv_hook = model.net.TopKGradHook(
        conv,
        'group{}_block{}_{}_hook'.format(module_seq, sub_seq, conv_name),
        k=topk,
    )

    # spaial batchnormalization layer
    bn = brew.spatial_bn(
        model,
        conv_hook,
        'group{}_block{}_{}_bn'.format(module_seq, sub_seq, conv_name),
        # conv,    # in-place
        dim_in = dim_out,
        epsilon = 1e-05,
        is_test = is_test,
    )

    # ReLU layer
    if has_relu:
        relu = brew.relu(model, bn, bn)
        return relu
    else:
        return bn
コード例 #30
0





model = model_helper.ModelHelper('xx')
def recalculateDim(size, kernel, stride, pad):
	nSize = ((size - kernel + (2 * pad)) / stride) + 1
	return nSize

s = 224
dims = 3, 10, 10, 10, 10, 20, 20, 20, 64, 128, 3
layer = 'data'
for i in range(10):
	layer = brew.conv(model, layer, 'conv_%d' % i, dim_in=dims[i], dim_out=dims[i + 1], kernel=5, stride=2)
	s = recalculateDim(s, 5, 2, 0)
	layer = brew.relu(model, layer, 'relu_%d' % i)
	if i % 3 == 0:
		layer = brew.max_pool(model, layer, 'pool_%d' % i, kernel=2, stride=2)
		s = recalculateDim(s, 2, 2, 0)


layer = brew.fc(model, layer, 'last_fc', dim_in=s*s*dims[-2], dim_out=dims[-1])
softmax = brew.softmax(model, layer, 'softmax')





コード例 #31
0
def OverFeat(order, cudnn_ws):
    my_arg_scope = {
        'order': order,
        'use_cudnn': True,
        'cudnn_exhaustive_search': True,
    }
    if cudnn_ws:
        my_arg_scope['ws_nbytes_limit'] = cudnn_ws
    model = model_helper.ModelHelper(
        name="overfeat",
        arg_scope=my_arg_scope,
    )
    conv1 = brew.conv(
        model,
        "data",
        "conv1",
        3,
        96,
        11,
        ('XavierFill', {}),
        ('ConstantFill', {}),
        stride=4,
    )
    relu1 = brew.relu(model, conv1, "conv1")
    pool1 = brew.max_pool(model, relu1, "pool1", kernel=2, stride=2)
    conv2 = brew.conv(model, pool1, "conv2", 96, 256, 5, ('XavierFill', {}),
                      ('ConstantFill', {}))
    relu2 = brew.relu(model, conv2, "conv2")
    pool2 = brew.max_pool(model, relu2, "pool2", kernel=2, stride=2)
    conv3 = brew.conv(
        model,
        pool2,
        "conv3",
        256,
        512,
        3,
        ('XavierFill', {}),
        ('ConstantFill', {}),
        pad=1,
    )
    relu3 = brew.relu(model, conv3, "conv3")
    conv4 = brew.conv(
        model,
        relu3,
        "conv4",
        512,
        1024,
        3,
        ('XavierFill', {}),
        ('ConstantFill', {}),
        pad=1,
    )
    relu4 = brew.relu(model, conv4, "conv4")
    conv5 = brew.conv(
        model,
        relu4,
        "conv5",
        1024,
        1024,
        3,
        ('XavierFill', {}),
        ('ConstantFill', {}),
        pad=1,
    )
    relu5 = brew.relu(model, conv5, "conv5")
    pool5 = brew.max_pool(model, relu5, "pool5", kernel=2, stride=2)
    fc6 = brew.fc(model, pool5, "fc6", 1024 * 6 * 6, 3072, ('XavierFill', {}),
                  ('ConstantFill', {}))
    relu6 = brew.relu(model, fc6, "fc6")
    fc7 = brew.fc(model, relu6, "fc7", 3072, 4096, ('XavierFill', {}),
                  ('ConstantFill', {}))
    relu7 = brew.relu(model, fc7, "fc7")
    fc8 = brew.fc(model, relu7, "fc8", 4096, 1000, ('XavierFill', {}),
                  ('ConstantFill', {}))
    pred = brew.softmax(model, fc8, "pred")
    xent = model.net.LabelCrossEntropy([pred, "label"], "xent")
    model.net.AveragedLoss(xent, "loss")
    return model, 231
コード例 #32
0
ファイル: conv_test.py プロジェクト: Sissipei/caffe2
    def test_convolution_sync(self, net_type, num_workers, do, engine):
        m = ModelHelper(name="test_model")
        n = 1
        d = 2
        depth = 3
        iters = 5
        h = 5
        w = 5
        workspace.ResetWorkspace()

        use_cudnn = (engine == 'CUDNN')

        np.random.seed(1701)
        # Build a binary tree of conv layers, summing at each node.
        for i in reversed(range(depth)):
            for j in range(2 ** i):
                bottom_1 = "{}_{}".format(i + 1, 2 * j)
                bottom_2 = "{}_{}".format(i + 1, 2 * j + 1)
                mid_1 = "{}_{}_m".format(i + 1, 2 * j)
                mid_2 = "{}_{}_m".format(i + 1, 2 * j + 1)
                top = "{}_{}".format(i, j)
                w1, b1, w2, b2 = np.random.randn(4).tolist()
                brew.conv(
                    m, bottom_1, mid_1,
                    dim_in=d, dim_out=d,
                    kernel=3,
                    weight_init=('ConstantFill', dict(value=w1)),
                    bias_init=('ConstantFill', dict(value=b1)),
                    cudnn_state=np.random.randint(0, 3),
                    stride=1,
                    pad=1,
                    deterministic=1,
                    use_cudnn=use_cudnn,
                    engine=engine)
                brew.conv(
                    m, bottom_2, mid_2,
                    dim_in=d, dim_out=d,
                    kernel=3,
                    stride=1,
                    pad=1,
                    weight_init=('ConstantFill', dict(value=w2)),
                    bias_init=('ConstantFill', dict(value=b2)),
                    deterministic=1,
                    cudnn_state=np.random.randint(0, 3),
                    use_cudnn=use_cudnn,
                    engine=engine)
                m.net.Sum([mid_1, mid_2], top)

        m.net.Flatten(["0_0"], ["0_0_flat"])
        m.net.SquaredL2Distance(["0_0_flat", "label"], "xent")
        m.net.AveragedLoss("xent", "loss")
        input_to_grad = m.AddGradientOperators(["loss"])
        m.Proto().device_option.CopyFrom(do)
        m.param_init_net.Proto().device_option.CopyFrom(do)
        m.Proto().type = net_type
        m.Proto().num_workers = num_workers
        self.ws.run(m.param_init_net)

        def run():
            import numpy as np
            np.random.seed(1701)
            input_blobs = ["{}_{}".format(depth, j) for j in range(2 ** depth)]
            for input_blob in input_blobs:
                self.ws.create_blob(input_blob).feed(
                    np.random.randn(n, d, h, w).astype(np.float32),
                    device_option=do)
                self.ws.create_blob("label").feed(
                    np.random.randn(n, d * h * w).astype(np.float32),
                    device_option=do)
            self.ws.run(m.net)
            gradients = [
                self.ws.blobs[str(input_to_grad[input_blob])].fetch()
                for input_blob in input_blobs]
            return gradients

        outputs = [run() for _ in range(iters)]
        for output in outputs[1:]:
            np.testing.assert_array_equal(outputs[0], output)
            np.testing.assert_allclose(
                np.sum(np.square(output)),
                1763719461732352.0,
                rtol=1e-5)
コード例 #33
0
ファイル: resnet.py プロジェクト: bittnt/caffe2
def create_resnet50(
    model,
    data,
    num_input_channels,
    num_labels,
    label=None,
    is_test=False,
    no_loss=False,
    no_bias=0,
    conv1_kernel=7,
    conv1_stride=2,
    final_avg_kernel=7,
):
    # conv1 + maxpool
    brew.conv(
        model,
        data,
        'conv1',
        num_input_channels,
        64,
        weight_init=("MSRAFill", {}),
        kernel=conv1_kernel,
        stride=conv1_stride,
        pad=3,
        no_bias=no_bias
    )

    brew.spatial_bn(
        model,
        'conv1',
        'conv1_spatbn_relu',
        64,
        epsilon=1e-3,
        momentum=0.1,
        is_test=is_test
    )
    brew.relu(model, 'conv1_spatbn_relu', 'conv1_spatbn_relu')
    brew.max_pool(model, 'conv1_spatbn_relu', 'pool1', kernel=3, stride=2)

    # Residual blocks...
    builder = ResNetBuilder(model, 'pool1', no_bias=no_bias,
                            is_test=is_test, spatial_bn_mom=0.1)

    # conv2_x (ref Table 1 in He et al. (2015))
    builder.add_bottleneck(64, 64, 256)
    builder.add_bottleneck(256, 64, 256)
    builder.add_bottleneck(256, 64, 256)

    # conv3_x
    builder.add_bottleneck(256, 128, 512, down_sampling=True)
    for _ in range(1, 4):
        builder.add_bottleneck(512, 128, 512)

    # conv4_x
    builder.add_bottleneck(512, 256, 1024, down_sampling=True)
    for _ in range(1, 6):
        builder.add_bottleneck(1024, 256, 1024)

    # conv5_x
    builder.add_bottleneck(1024, 512, 2048, down_sampling=True)
    builder.add_bottleneck(2048, 512, 2048)
    builder.add_bottleneck(2048, 512, 2048)

    # Final layers
    final_avg = brew.average_pool(
        model,
        builder.prev_blob,
        'final_avg',
        kernel=final_avg_kernel,
        stride=1,
    )

    # Final dimension of the "image" is reduced to 7x7
    last_out = brew.fc(
        model, final_avg, 'last_out_L{}'.format(num_labels), 2048, num_labels
    )

    if no_loss:
        return last_out

    # If we create model for training, use softmax-with-loss
    if (label is not None):
        (softmax, loss) = model.SoftmaxWithLoss(
            [last_out, label],
            ["softmax", "loss"],
        )

        return (softmax, loss)
    else:
        # For inference, we just return softmax
        return brew.softmax(model, last_out, "softmax")
コード例 #34
0
    def test_simple_model(self):
        model = model_helper.ModelHelper(name="mnist")
        # how come those inputs don't break the forward pass =.=a
        workspace.FeedBlob("data",
                           np.random.randn(1, 3, 64, 64).astype(np.float32))
        workspace.FeedBlob("label", np.random.randn(1, 1000).astype(np.int))

        with core.NameScope("conv1"):
            conv1 = brew.conv(model,
                              "data",
                              'conv1',
                              dim_in=1,
                              dim_out=20,
                              kernel=5)
            # Image size: 24 x 24 -> 12 x 12
            pool1 = brew.max_pool(model, conv1, 'pool1', kernel=2, stride=2)
            # Image size: 12 x 12 -> 8 x 8
            conv2 = brew.conv(model,
                              pool1,
                              'conv2',
                              dim_in=20,
                              dim_out=100,
                              kernel=5)
            # Image size: 8 x 8 -> 4 x 4
            pool2 = brew.max_pool(model, conv2, 'pool2', kernel=2, stride=2)
        with core.NameScope("classifier"):
            # 50 * 4 * 4 stands for dim_out from previous layer multiplied by the image size
            fc3 = brew.fc(model, pool2, 'fc3', dim_in=100 * 4 * 4, dim_out=500)
            relu = brew.relu(model, fc3, fc3)
            pred = brew.fc(model, relu, 'pred', 500, 10)
            softmax = brew.softmax(model, pred, 'softmax')
            xent = model.LabelCrossEntropy([softmax, "label"], 'xent')
            # compute the expected loss
            loss = model.AveragedLoss(xent, "loss")
        model.net.RunAllOnMKL()
        model.param_init_net.RunAllOnMKL()
        model.AddGradientOperators([loss], skip=1)
        blob_name_tracker = {}
        graph = tb.model_to_graph_def(
            model,
            blob_name_tracker=blob_name_tracker,
            shapes={},
            show_simplified=False,
        )
        #self.assertEqual(
        #    blob_name_tracker['GRADIENTS/conv1/conv1_b_grad'],
        #    'conv1/conv1_b_grad',
        #)
        self.maxDiff = None
        # We can't guarantee the order in which they appear, so we sort
        # both before we compare them
        with open('tests/expect/caffe_mnist.expect') as f:
            EXPECTED_MNIST = f.read()
        sep = "node {"
        expected = "\n".join(
            sorted(sep + "\n  " + part.strip()
                   for part in EXPECTED_MNIST.strip().split(sep)
                   if part.strip()))
        actual = "\n".join(
            sorted(sep + "\n  " + part.strip()
                   for part in str(graph).strip().split(sep) if part.strip()))
コード例 #35
0
def OverFeat(order, cudnn_ws, device):
    my_arg_scope = {'order': order, 'use_cudnn': True,
                    'cudnn_exhaustive_search': True,
                    'ws_nbytes_limit': str(cudnn_ws)}
    model = ModelHelper(name='overfeat', arg_scope=my_arg_scope)
    conv1 = brew.conv(
        model,
        "data",
        "conv1",
        3,
        96,
        11,
        ('XavierFill', {}),
        ('ConstantFill', {}),
        stride=4
    )
    relu1 = brew.relu(model, conv1, "conv1")
    pool1 = brew.max_pool(model, relu1, "pool1", kernel=2, stride=2)
    conv2 = brew.conv(
        model, pool1, "conv2", 96, 256, 5, ('XavierFill', {}), ('ConstantFill', {})
    )
    relu2 = brew.relu(model, conv2, "conv2")
    pool2 = brew.max_pool(model, relu2, "pool2", kernel=2, stride=2)
    conv3 = brew.conv(
        model,
        pool2,
        "conv3",
        256,
        512,
        3,
        ('XavierFill', {}),
        ('ConstantFill', {}),
        pad=1
    )
    relu3 = brew.relu(model, conv3, "conv3")
    conv4 = brew.conv(
        model,
        relu3,
        "conv4",
        512,
        1024,
        3,
        ('XavierFill', {}),
        ('ConstantFill', {}),
        pad=1
    )
    relu4 = brew.relu(model, conv4, "conv4")
    conv5 = brew.conv(
        model,
        relu4,
        "conv5",
        1024,
        1024,
        3,
        ('XavierFill', {}),
        ('ConstantFill', {}),
        pad=1
    )
    relu5 = brew.relu(model, conv5, "conv5")
    pool5 = brew.max_pool(model, relu5, "pool5", kernel=2, stride=2)
    fc6 = brew.fc(
        model, pool5, "fc6", 1024 * 6 * 6, 3072, ('XavierFill', {}),
        ('ConstantFill', {})
    )
    relu6 = brew.relu(model, fc6, "fc6")
    fc7 = brew.fc(
        model, relu6, "fc7", 3072, 4096, ('XavierFill', {}), ('ConstantFill', {})
    )
    relu7 = brew.relu(model, fc7, "fc7")
    fc8 = brew.fc(
        model, relu7, "fc8", 4096, 1000, ('XavierFill', {}), ('ConstantFill', {})
    )
    pred = brew.softmax(model, fc8, "pred")
    xent = model.LabelCrossEntropy([pred, "label"], "xent")
    if device != 'MKL':
        loss = model.AveragedLoss(xent, "loss")
    return model, 231
コード例 #36
0
ファイル: mode_test.py プロジェクト: Randy-jun/VOC-tools
def TestModel_ops(model, data, label):
    # Image size: 227 x 227 -> 224 x 224
    conv1_s2 = brew.conv(model,
                         data,
                         'conv1_s2',
                         dim_in=3,
                         dim_out=32,
                         kernel=4,
                         stride=2)
    conv1_s2_s = brew.spatial_bn(model,
                                 conv1_s2,
                                 'conv1_s2_s',
                                 32,
                                 epsilon=1e-3,
                                 momentum=0.9)

    conv1_dw = brew.conv(model,
                         conv1_s2_s,
                         'conv1_dw',
                         dim_in=32,
                         dim_out=32,
                         kernel=3,
                         pad=2)
    # conv1_dw_s = brew.spatial_bn(model, conv1_dw, 'conv1_dw_s', 32, epsilon=1e-3, momentum=0.9)
    conv1_s1 = brew.conv(model,
                         conv1_dw,
                         'conv1_s1',
                         dim_in=32,
                         dim_out=64,
                         kernel=1)
    conv1_s1_s = brew.spatial_bn(model,
                                 conv1_s1,
                                 'conv1_s1_s',
                                 64,
                                 epsilon=1e-3,
                                 momentum=0.9)

    conv2_s2 = brew.conv(model,
                         conv1_s1_s,
                         'conv2_s2',
                         dim_in=64,
                         dim_out=64,
                         kernel=3,
                         pad=2,
                         stride=2)
    conv2_dw = brew.conv(model,
                         conv2_s2,
                         'conv2_dw',
                         dim_in=64,
                         dim_out=128,
                         kernel=1)

    conv2_k3 = brew.conv(model,
                         conv2_dw,
                         'conv2_k3',
                         dim_in=128,
                         dim_out=128,
                         kernel=3,
                         pad=2)
    conv2_s1 = brew.conv(model,
                         conv2_k3,
                         'conv2_s1',
                         dim_in=128,
                         dim_out=128,
                         kernel=1)

    conv3_s2 = brew.conv(model,
                         conv2_s1,
                         'conv3_s2',
                         dim_in=128,
                         dim_out=128,
                         kernel=3,
                         pad=2,
                         stride=2)
    conv3_dw = brew.conv(model,
                         conv3_s2,
                         'conv3_dw',
                         dim_in=128,
                         dim_out=256,
                         kernel=1)

    conv3_k3 = brew.conv(model,
                         conv3_dw,
                         'conv3_k3',
                         dim_in=256,
                         dim_out=256,
                         kernel=3,
                         pad=2)
    conv3_s1 = brew.conv(model,
                         conv3_k3,
                         'conv3_s1',
                         dim_in=256,
                         dim_out=256,
                         kernel=1)

    conv4_s2 = brew.conv(model,
                         conv3_s1,
                         'conv4_s2',
                         dim_in=256,
                         dim_out=256,
                         kernel=3,
                         pad=2,
                         stride=2)
    conv4_dw = brew.conv(model,
                         conv4_s2,
                         'conv4_dw',
                         dim_in=256,
                         dim_out=512,
                         kernel=1)
    #5
    conv5_s2 = brew.conv(model,
                         conv4_dw,
                         'conv5_s2',
                         dim_in=512,
                         dim_out=512,
                         kernel=3,
                         pad=2)
    conv5_dw = brew.conv(model,
                         conv5_s2,
                         'conv5_dw',
                         dim_in=512,
                         dim_out=512,
                         kernel=1)
    #4
    conv6_s2 = brew.conv(model,
                         conv5_dw,
                         'conv6_s2',
                         dim_in=512,
                         dim_out=512,
                         kernel=3,
                         pad=2)
    conv6_dw = brew.conv(model,
                         conv6_s2,
                         'conv6_dw',
                         dim_in=512,
                         dim_out=512,
                         kernel=1)
    #3
    conv7_s2 = brew.conv(model,
                         conv6_dw,
                         'conv7_s2',
                         dim_in=512,
                         dim_out=512,
                         kernel=3,
                         pad=2)
    conv7_dw = brew.conv(model,
                         conv7_s2,
                         'conv7_dw',
                         dim_in=512,
                         dim_out=512,
                         kernel=1)
    #2
    conv8_s2 = brew.conv(model,
                         conv7_dw,
                         'conv8_s2',
                         dim_in=512,
                         dim_out=512,
                         kernel=3,
                         pad=2)
    conv8_dw = brew.conv(model,
                         conv8_s2,
                         'conv8_dw',
                         dim_in=512,
                         dim_out=512,
                         kernel=1)
    #1
    conv9_s2 = brew.conv(model,
                         conv8_dw,
                         'conv9_s2',
                         dim_in=512,
                         dim_out=512,
                         kernel=3,
                         pad=2)
    conv9_dw = brew.conv(model,
                         conv9_s2,
                         'conv9_dw',
                         dim_in=512,
                         dim_out=512,
                         kernel=1)

    conv10_s2 = brew.conv(model,
                          conv9_dw,
                          'conv10_s2',
                          dim_in=512,
                          dim_out=512,
                          kernel=3,
                          pad=2,
                          stride=2)
    conv10_dw = brew.conv(model,
                          conv10_s2,
                          'conv10_dw',
                          dim_in=512,
                          dim_out=1024,
                          kernel=1)  # out 1024
    conv10_k3 = brew.conv(model,
                          conv10_dw,
                          'conv10_k3',
                          dim_in=1024,
                          dim_out=1024,
                          kernel=3)
    conv10_s1 = brew.conv(model,
                          conv10_k3,
                          'conv10_s1',
                          dim_in=1024,
                          dim_out=1024,
                          kernel=1)

    pool1 = brew.average_pool(model, conv10_s1, 'pool1', kernel=7, stride=7)
    # pool1 = brew.max_pool(model, conv10_s1, 'pool1', kernel=7, stride=7)

    fc1 = brew.fc(model, pool1, 'fc1', dim_in=1 * 1 * 1024, dim_out=num_labels)

    # softmax = brew.softmax(model, fc1, 'softmax')

    [softmax, loss] = model.SoftmaxWithLoss(
        [fc1, label],
        ["softmax", "loss"],
    )

    return [softmax, loss]
コード例 #37
0
ファイル: vgg.py プロジェクト: Luo-Liang/caffe2
def create_vgg(
    model,
    data,
    num_input_channels,
    num_labels,
    num_layers=11,
    is_test=False,
):

    if num_layers == 11:  # VGG configuration A
        first_layers_count = 1
        last_layers_count = 2
    elif num_layers == 13:  # VGG configuration D
        first_layers_count = 2
        last_layers_count = 2
    elif num_layers == 16:  # VGG configuration D
        first_layers_count = 2
        last_layers_count = 3
    elif num_layers == 19:  # VGG configuration E
        first_layers_count = 2
        last_layers_count = 4
    else:
        raise NotImplementedError(
            "not currently supported: try one of {11, 13, 16, 19}, corresponding to VGG A, B, D, and E."
        )

    conv1 = brew.conv(
        model,
        data,
        "conv1",
        num_input_channels,
        64,
        3,
        ('XavierFill', {}),
        ('ConstantFill', {}),
        pad=1,
    )
    relu1 = brew.relu(model, conv1, "conv1")
    for i in range(0, first_layers_count - 1):
        conv1 = brew.conv(
            model,
            relu1,
            "conv1{}".format(i),
            64,
            64,
            3,
            ('XavierFill', {}),
            ('ConstantFill', {}),
            pad=1,
        )
        relu1 = brew.relu(model, conv1, "conv1{}".format(i))

    pool1 = brew.max_pool(model, relu1, "pool1", kernel=2, stride=2)
    conv2 = brew.conv(
        model,
        pool1,
        "conv2",
        64,
        128,
        3,
        ('XavierFill', {}),
        ('ConstantFill', {}),
        pad=1,
    )
    relu2 = brew.relu(model, conv2, "conv2")
    for i in range(0, first_layers_count - 1):
        conv2 = brew.conv(
            model,
            relu2,
            "conv2{}".format(i),
            128,
            128,
            3,
            ('XavierFill', {}),
            ('ConstantFill', {}),
            pad=1,
        )
        relu2 = brew.relu(model, conv2, "conv2{}".format(i))

    pool2 = brew.max_pool(model, relu2, "pool2", kernel=2, stride=2)
    conv3 = brew.conv(
        model,
        pool2,
        "conv3",
        128,
        256,
        3,
        ('XavierFill', {}),
        ('ConstantFill', {}),
        pad=1,
    )
    relu3 = brew.relu(model, conv3, "conv3")
    for i in range(0, last_layers_count - 1):
        conv4 = brew.conv(
            model,
            relu3,
            "conv4{}".format(i),
            256,
            256,
            3,
            ('XavierFill', {}),
            ('ConstantFill', {}),
            pad=1,
        )
        relu4 = brew.relu(model, conv4, "conv4{}".format(i))
    pool4 = brew.max_pool(model, relu4, "pool4", kernel=2, stride=2)
    conv5 = brew.conv(
        model,
        pool4,
        "conv5",
        256,
        512,
        3,
        ('XavierFill', {}),
        ('ConstantFill', {}),
        pad=1,
    )
    relu5 = brew.relu(model, conv5, "conv5")
    for i in range(0, last_layers_count - 1):
        conv6 = brew.conv(
            model,
            relu5,
            "conv6{}".format(i),
            512,
            512,
            3,
            ('XavierFill', {}),
            ('ConstantFill', {}),
            pad=1,
        )
        relu6 = brew.relu(model, conv6, "conv6{}".format(i))
    pool6 = brew.max_pool(model, relu6, "pool6", kernel=2, stride=2)
    conv7 = brew.conv(
        model,
        pool6,
        "conv7",
        512,
        512,
        3,
        ('XavierFill', {}),
        ('ConstantFill', {}),
        pad=1,
    )
    relu7 = brew.relu(model, conv7, "conv7")
    for i in range(0, last_layers_count - 1):
        conv8 = brew.conv(
            model,
            relu7,
            "conv8{}".format(i),
            512,
            512,
            3,
            ('XavierFill', {}),
            ('ConstantFill', {}),
            pad=1,
        )
        relu8 = brew.relu(model, conv8, "conv8{}".format(i))
    pool8 = brew.max_pool(model, relu8, "pool8", kernel=2, stride=2)

    fcix = brew.fc(model, pool8, "fcix", 512 * 7 * 7, 4096, ('XavierFill', {}),
                   ('ConstantFill', {}))
    reluix = brew.relu(model, fcix, "fcix")
    fcx = brew.fc(model, reluix, "fcx", 4096, 4096, ('XavierFill', {}),
                  ('ConstantFill', {}))
    relux = brew.relu(model, fcx, "fcx")
    fcxi = brew.fc(model, relux, "fcxi", 4096, num_labels, ('XavierFill', {}),
                   ('ConstantFill', {}))

    return fcxi
コード例 #38
0
def create_unet_model(m, device_opts, is_test):

    base_n_filters = 16
    kernel_size = 3
    pad = (kernel_size - 1) / 2
    do_dropout = True
    num_classes = 3

    weight_init = ("MSRAFill", {})

    with core.DeviceScope(device_opts):

        contr_1_1 = brew.spatial_bn(m,
                                    brew.relu(
                                        m,
                                        brew.conv(m,
                                                  'data',
                                                  'conv_1_1',
                                                  dim_in=num_classes,
                                                  dim_out=base_n_filters,
                                                  kernel=kernel_size,
                                                  pad=pad,
                                                  weight_init=weight_init),
                                        'nonl_1_1'),
                                    'contr_1_1',
                                    dim_in=base_n_filters,
                                    epsilon=1e-3,
                                    momentum=0.1,
                                    is_test=is_test)
        contr_1_2 = brew.spatial_bn(m,
                                    brew.relu(
                                        m,
                                        brew.conv(m,
                                                  contr_1_1,
                                                  'conv_1_2',
                                                  dim_in=base_n_filters,
                                                  dim_out=base_n_filters,
                                                  kernel=kernel_size,
                                                  pad=pad,
                                                  weight_init=weight_init),
                                        'nonl_1_2'),
                                    'contr_1_2',
                                    dim_in=base_n_filters,
                                    epsilon=1e-3,
                                    momentum=0.1,
                                    is_test=is_test)
        pool1 = brew.max_pool(m, contr_1_2, 'pool1', kernel=2, stride=2)

        contr_2_1 = brew.spatial_bn(m,
                                    brew.relu(
                                        m,
                                        brew.conv(m,
                                                  pool1,
                                                  'conv_2_1',
                                                  dim_in=base_n_filters,
                                                  dim_out=base_n_filters * 2,
                                                  kernel=kernel_size,
                                                  pad=pad,
                                                  weight_init=weight_init),
                                        'nonl_2_1'),
                                    'contr_2_1',
                                    dim_in=base_n_filters * 2,
                                    epsilon=1e-3,
                                    momentum=0.1,
                                    is_test=is_test)
        contr_2_2 = brew.spatial_bn(m,
                                    brew.relu(
                                        m,
                                        brew.conv(m,
                                                  contr_2_1,
                                                  'conv_2_2',
                                                  dim_in=base_n_filters * 2,
                                                  dim_out=base_n_filters * 2,
                                                  kernel=kernel_size,
                                                  pad=pad,
                                                  weight_init=weight_init),
                                        'nonl_2_2'),
                                    'contr_2_2',
                                    dim_in=base_n_filters * 2,
                                    epsilon=1e-3,
                                    momentum=0.1,
                                    is_test=is_test)
        pool2 = brew.max_pool(m, contr_2_2, 'pool2', kernel=2, stride=2)

        contr_3_1 = brew.spatial_bn(m,
                                    brew.relu(
                                        m,
                                        brew.conv(m,
                                                  pool2,
                                                  'conv_3_1',
                                                  dim_in=base_n_filters * 2,
                                                  dim_out=base_n_filters * 4,
                                                  kernel=kernel_size,
                                                  pad=pad,
                                                  weight_init=weight_init),
                                        'nonl_3_1'),
                                    'contr_3_1',
                                    dim_in=base_n_filters * 4,
                                    epsilon=1e-3,
                                    momentum=0.1,
                                    is_test=is_test)
        contr_3_2 = brew.spatial_bn(m,
                                    brew.relu(
                                        m,
                                        brew.conv(m,
                                                  contr_3_1,
                                                  'conv_3_2',
                                                  dim_in=base_n_filters * 4,
                                                  dim_out=base_n_filters * 4,
                                                  kernel=kernel_size,
                                                  pad=pad,
                                                  weight_init=weight_init),
                                        'nonl_3_2'),
                                    'contr_3_2',
                                    dim_in=base_n_filters * 4,
                                    epsilon=1e-3,
                                    momentum=0.1,
                                    is_test=is_test)
        pool3 = brew.max_pool(m, contr_3_2, 'pool3', kernel=2, stride=2)

        contr_4_1 = brew.spatial_bn(m,
                                    brew.relu(
                                        m,
                                        brew.conv(m,
                                                  pool3,
                                                  'conv_4_1',
                                                  dim_in=base_n_filters * 4,
                                                  dim_out=base_n_filters * 8,
                                                  kernel=kernel_size,
                                                  pad=pad,
                                                  weight_init=weight_init),
                                        'nonl_4_1'),
                                    'contr_4_1',
                                    dim_in=base_n_filters * 8,
                                    epsilon=1e-3,
                                    momentum=0.1,
                                    is_test=is_test)
        contr_4_2 = brew.spatial_bn(m,
                                    brew.relu(
                                        m,
                                        brew.conv(m,
                                                  contr_4_1,
                                                  'conv_4_2',
                                                  dim_in=base_n_filters * 8,
                                                  dim_out=base_n_filters * 8,
                                                  kernel=kernel_size,
                                                  pad=pad,
                                                  weight_init=weight_init),
                                        'nonl_4_2'),
                                    'contr_4_2',
                                    dim_in=base_n_filters * 8,
                                    epsilon=1e-3,
                                    momentum=0.1,
                                    is_test=is_test)
        pool4 = brew.max_pool(m, contr_4_2, 'pool4', kernel=2, stride=2)

        if do_dropout:
            pool4 = brew.dropout(m, pool4, 'drop', ratio=0.4)

        encode_5_1 = brew.spatial_bn(m,
                                     brew.relu(
                                         m,
                                         brew.conv(m,
                                                   pool4,
                                                   'conv_5_1',
                                                   dim_in=base_n_filters * 8,
                                                   dim_out=base_n_filters * 16,
                                                   kernel=kernel_size,
                                                   pad=pad,
                                                   weight_init=weight_init),
                                         'nonl_5_1'),
                                     'encode_5_1',
                                     dim_in=base_n_filters * 16,
                                     epsilon=1e-3,
                                     momentum=0.1,
                                     is_test=is_test)
        encode_5_2 = brew.spatial_bn(m,
                                     brew.relu(
                                         m,
                                         brew.conv(m,
                                                   encode_5_1,
                                                   'conv_5_2',
                                                   dim_in=base_n_filters * 16,
                                                   dim_out=base_n_filters * 16,
                                                   kernel=kernel_size,
                                                   pad=pad,
                                                   weight_init=weight_init),
                                         'nonl_5_2'),
                                     'encode_5_2',
                                     dim_in=base_n_filters * 16,
                                     epsilon=1e-3,
                                     momentum=0.1,
                                     is_test=is_test)
        upscale5 = brew.conv_transpose(m,
                                       encode_5_2,
                                       'upscale5',
                                       dim_in=base_n_filters * 16,
                                       dim_out=base_n_filters * 16,
                                       kernel=2,
                                       stride=2,
                                       weight_init=weight_init)

        concat6 = brew.concat(m, [upscale5, contr_4_2], 'concat6')  #, axis=1)
        expand_6_1 = brew.spatial_bn(
            m,
            brew.relu(
                m,
                brew.conv(m,
                          concat6,
                          'conv_6_1',
                          dim_in=base_n_filters * 8 * 3,
                          dim_out=base_n_filters * 8,
                          kernel=kernel_size,
                          pad=pad,
                          weight_init=weight_init), 'nonl_6_1'),
            'expand_6_1',
            dim_in=base_n_filters * 8,
            epsilon=1e-3,
            momentum=0.1,
            is_test=is_test)
        expand_6_2 = brew.spatial_bn(m,
                                     brew.relu(
                                         m,
                                         brew.conv(m,
                                                   expand_6_1,
                                                   'conv_6_2',
                                                   dim_in=base_n_filters * 8,
                                                   dim_out=base_n_filters * 8,
                                                   kernel=kernel_size,
                                                   pad=pad,
                                                   weight_init=weight_init),
                                         'nonl_6_2'),
                                     'expand_6_2',
                                     dim_in=base_n_filters * 8,
                                     epsilon=1e-3,
                                     momentum=0.1,
                                     is_test=is_test)
        upscale6 = brew.conv_transpose(m,
                                       expand_6_2,
                                       'upscale6',
                                       dim_in=base_n_filters * 8,
                                       dim_out=base_n_filters * 8,
                                       kernel=2,
                                       stride=2,
                                       weight_init=weight_init)

        concat7 = brew.concat(m, [upscale6, contr_3_2], 'concat7')
        expand_7_1 = brew.spatial_bn(
            m,
            brew.relu(
                m,
                brew.conv(m,
                          concat7,
                          'conv_7_1',
                          dim_in=base_n_filters * 4 * 3,
                          dim_out=base_n_filters * 4,
                          kernel=kernel_size,
                          pad=pad,
                          weight_init=weight_init), 'nonl_7_1'),
            'expand_7_1',
            dim_in=base_n_filters * 4,
            epsilon=1e-3,
            momentum=0.1,
            is_test=is_test)
        expand_7_2 = brew.spatial_bn(m,
                                     brew.relu(
                                         m,
                                         brew.conv(m,
                                                   expand_7_1,
                                                   'conv_7_2',
                                                   dim_in=base_n_filters * 4,
                                                   dim_out=base_n_filters * 4,
                                                   kernel=kernel_size,
                                                   pad=pad,
                                                   weight_init=weight_init),
                                         'nonl_7_2'),
                                     'expand_7_2',
                                     dim_in=base_n_filters * 4,
                                     epsilon=1e-3,
                                     momentum=0.1,
                                     is_test=is_test)
        upscale7 = brew.conv_transpose(m,
                                       expand_7_2,
                                       'upscale7',
                                       dim_in=base_n_filters * 4,
                                       dim_out=base_n_filters * 4,
                                       kernel=2,
                                       stride=2,
                                       weight_init=weight_init)

        concat8 = brew.concat(m, [upscale7, contr_2_2], 'concat8')
        expand_8_1 = brew.spatial_bn(
            m,
            brew.relu(
                m,
                brew.conv(m,
                          concat8,
                          'conv_8_1',
                          dim_in=base_n_filters * 2 * 3,
                          dim_out=base_n_filters * 2,
                          kernel=kernel_size,
                          pad=pad,
                          weight_init=weight_init), 'nonl_8_1'),
            'expand_8_1',
            dim_in=base_n_filters * 2,
            epsilon=1e-3,
            momentum=0.1,
            is_test=is_test)
        expand_8_2 = brew.spatial_bn(m,
                                     brew.relu(
                                         m,
                                         brew.conv(m,
                                                   expand_8_1,
                                                   'conv_8_2',
                                                   dim_in=base_n_filters * 2,
                                                   dim_out=base_n_filters * 2,
                                                   kernel=kernel_size,
                                                   pad=pad,
                                                   weight_init=weight_init),
                                         'nonl_8_2'),
                                     'expand_8_2',
                                     dim_in=base_n_filters * 2,
                                     epsilon=1e-3,
                                     momentum=0.1,
                                     is_test=is_test)
        upscale8 = brew.conv_transpose(m,
                                       expand_8_2,
                                       'upscale8',
                                       dim_in=base_n_filters * 2,
                                       dim_out=base_n_filters * 2,
                                       kernel=2,
                                       stride=2,
                                       weight_init=weight_init)

        concat9 = brew.concat(m, [upscale8, contr_1_2], 'concat9')
        expand_9_1 = brew.spatial_bn(m,
                                     brew.relu(
                                         m,
                                         brew.conv(m,
                                                   concat9,
                                                   'conv_9_1',
                                                   dim_in=base_n_filters * 3,
                                                   dim_out=base_n_filters,
                                                   kernel=kernel_size,
                                                   pad=pad,
                                                   weight_init=weight_init),
                                         'nonl_9_1'),
                                     'expand_9_1',
                                     dim_in=base_n_filters,
                                     epsilon=1e-3,
                                     momentum=0.1,
                                     is_test=is_test)
        expand_9_2 = brew.spatial_bn(m,
                                     brew.relu(
                                         m,
                                         brew.conv(m,
                                                   expand_9_1,
                                                   'conv_9_2',
                                                   dim_in=base_n_filters,
                                                   dim_out=base_n_filters,
                                                   kernel=kernel_size,
                                                   pad=pad,
                                                   weight_init=weight_init),
                                         'nonl_9_2'),
                                     'expand_9_2',
                                     dim_in=base_n_filters,
                                     epsilon=1e-3,
                                     momentum=0.1,
                                     is_test=is_test)

        output_segmentation = brew.conv(m,
                                        expand_9_2,
                                        'output_segmentation',
                                        dim_in=base_n_filters,
                                        dim_out=num_classes,
                                        kernel=1,
                                        pad=0,
                                        stride=1,
                                        weight_init=weight_init)
        m.net.AddExternalOutput(output_segmentation)

        output_sigmoid = m.Sigmoid(output_segmentation, 'output_sigmoid')
        m.net.AddExternalOutput(output_sigmoid)

        return output_segmentation
コード例 #39
0
ファイル: rewrite_graph_test.py プロジェクト: Yangqing/caffe2
def alexnet():
    model = ModelHelper(name="r", arg_scope={"order": "NCHW", "is_test": True})
    conv1 = brew.conv(
        model,
        "data",
        "conv1",
        3,
        64,
        11, ('XavierFill', {}), ('ConstantFill', {}),
        stride=4,
        pad=2
    )
    relu1 = brew.relu(model, conv1, "conv1")
    pool1 = brew.max_pool(model, relu1, "pool1", kernel=3, stride=2, pad=0,
                          legacy_pad=3)
    lrn1 = brew.lrn(
        model, pool1, "pool1_lrn", size=5, alpha=1.0e-4, beta=0.75, bias=1.0)
    conv2 = brew.conv(
        model,
        lrn1,
        "conv2",
        64,
        192,
        5,
        ('XavierFill', {}),
        ('ConstantFill', {}),
        pad=2
    )
    relu2 = brew.relu(model, conv2, "conv2")
    pool2 = brew.max_pool(model, relu2, "pool2", kernel=3, stride=2)
    lrn2 = brew.lrn(
        model, pool2, "pool2_lrn", size=5, alpha=1.0e-4, beta=0.75, bias=1.0)
    conv3 = brew.conv(
        model,
        lrn2,
        "conv3",
        192,
        384,
        3,
        ('XavierFill', {}),
        ('ConstantFill', {}),
        pad=1
    )
    relu3 = brew.relu(model, conv3, "conv3")
    conv4 = brew.conv(
        model,
        relu3,
        "conv4",
        384,
        256,
        3,
        ('XavierFill', {}),
        ('ConstantFill', {}),
        pad=1
    )
    relu4 = brew.relu(model, conv4, "conv4")
    conv5 = brew.conv(
        model,
        relu4,
        "conv5",
        256,
        256,
        3,
        ('XavierFill', {}),
        ('ConstantFill', {}),
        pad=1
    )
    relu5 = brew.relu(model, conv5, "conv5")
    pool5 = brew.max_pool(model, relu5, "pool5", kernel=3, stride=2)
    fc6 = brew.fc(
        model,
        pool5, "fc6", 256 * 6 * 6, 4096, ('XavierFill', {}),
        ('ConstantFill', {})
    )
    relu6 = brew.relu(model, fc6, "fc6")
    fc7 = brew.fc(
        model, relu6, "fc7", 4096, 4096, ('XavierFill', {}), ('ConstantFill', {})
    )
    relu7 = brew.relu(model, fc7, "fc7")
    drop7 = brew.dropout(model, relu7, "fc7_dropout", is_test=1, ratio=0.5)
    fc8 = brew.fc(
        model, drop7, "fc8", 4096, 1000, ('XavierFill', {}), ('ConstantFill', {})
    )
    relu8 = brew.relu(model, fc8, "fc8")
    _ = brew.dropout(model, relu8, "fc8_dropout", is_test=1, ratio=0.5)
    return model, [(1, 3, 224, 224)]
コード例 #40
0
ファイル: alexnet.py プロジェクト: Luo-Liang/caffe2
def create_alexnetv0(
    model,
    data,
    num_input_channels,
    num_labels,
    is_test=False,
):
    conv1 = brew.conv(
        model,
        data,
        "conv1",
        num_input_channels,  # dim_in
        96,  # dim_out
        11,  # kernel
        ('XavierFill', {}),
        ('ConstantFill', {}),
        stride=4,
        pad=2)
    relu1 = brew.relu(model, conv1, "conv1")
    norm1 = brew.lrn(model, relu1, "norm1", size=5, alpha=0.0001, beta=0.75)
    pool1 = brew.max_pool(model, norm1, "pool1", kernel=3, stride=2)
    conv2 = brew.conv(model,
                      pool1,
                      "conv2",
                      96,
                      256,
                      5, ('XavierFill', {}), ('ConstantFill', {}),
                      pad=2)
    relu2 = brew.relu(model, conv2, "conv2")
    norm2 = brew.lrn(model, relu2, "norm2", size=5, alpha=0.0001, beta=0.75)
    pool2 = brew.max_pool(model, norm2, "pool2", kernel=3, stride=2)
    conv3 = brew.conv(model,
                      pool2,
                      "conv3",
                      256,
                      384,
                      3, ('XavierFill', {}), ('ConstantFill', {}),
                      pad=1)
    relu3 = brew.relu(model, conv3, "conv3")
    conv4 = brew.conv(model,
                      relu3,
                      "conv4",
                      384,
                      384,
                      3, ('XavierFill', {}), ('ConstantFill', {}),
                      pad=1)
    relu4 = brew.relu(model, conv4, "conv4")
    conv5 = brew.conv(model,
                      relu4,
                      "conv5",
                      384,
                      256,
                      3, ('XavierFill', {}), ('ConstantFill', {}),
                      pad=1)
    relu5 = brew.relu(model, conv5, "conv5")
    pool5 = brew.max_pool(model, relu5, "pool5", kernel=3, stride=2)
    fc6 = brew.fc(model, pool5, "fc6", 256 * 6 * 6, 4096, ('XavierFill', {}),
                  ('ConstantFill', {}))
    relu6 = brew.relu(model, fc6, "fc6")
    dropout1 = brew.dropout(model,
                            relu6,
                            'dropout1',
                            ratio=0.5,
                            is_test=is_test)

    fc7 = brew.fc(model, dropout1, "fc7", 4096, 4096, ('XavierFill', {}),
                  ('ConstantFill', {}))
    relu7 = brew.relu(model, fc7, "fc7")
    dropout2 = brew.dropout(model,
                            relu7,
                            'dropout2',
                            ratio=0.5,
                            is_test=is_test)

    fc8 = brew.fc(model, dropout2, "fc8", 4096, num_labels, ('XavierFill', {}),
                  ('ConstantFill', {}))
    return fc8
コード例 #41
0
    def vgg16_model(self, model, data, precision):
        initializer = Initializer if precision == 'fp32' else pFP16Initializer
        with brew.arg_scope([brew.conv, brew.fc],
                            WeightInitializer=initializer,
                            BiasInitializer=initializer,
                            enable_tensor_core=True):
            conv1_1 = brew.conv(model,
                                data,
                                'conv1_1',
                                dim_in=3,
                                dim_out=64,
                                kernel=3,
                                pad=1)
            conv1_1 = brew.relu(model, conv1_1, conv1_1)
            conv1_2 = brew.conv(model,
                                conv1_1,
                                'conv1_2',
                                dim_in=64,
                                dim_out=64,
                                kernel=3,
                                pad=1)
            conv1_2 = brew.relu(model, conv1_2, conv1_2)
            pool1 = brew.max_pool(model, conv1_2, 'pool1', kernel=2, stride=2)

            conv2_1 = brew.conv(model,
                                pool1,
                                'conv2_1',
                                dim_in=64,
                                dim_out=128,
                                kernel=3,
                                pad=1)
            conv2_1 = brew.relu(model, conv2_1, conv2_1)
            conv2_2 = brew.conv(model,
                                conv2_1,
                                'conv2_2',
                                dim_in=128,
                                dim_out=128,
                                kernel=3,
                                pad=1)
            conv2_2 = brew.relu(model, conv2_2, conv2_2)
            pool2 = brew.max_pool(model, conv2_2, 'pool2', kernel=2, stride=2)

            conv3_1 = brew.conv(model,
                                pool2,
                                'conv3_1',
                                dim_in=128,
                                dim_out=256,
                                kernel=3,
                                pad=1)
            conv3_1 = brew.relu(model, conv3_1, conv3_1)
            conv3_2 = brew.conv(model,
                                conv3_1,
                                'conv3_2',
                                dim_in=256,
                                dim_out=256,
                                kernel=3,
                                pad=1)
            conv3_2 = brew.relu(model, conv3_2, conv3_2)
            conv3_3 = brew.conv(model,
                                conv3_2,
                                'conv3_3',
                                dim_in=256,
                                dim_out=256,
                                kernel=3,
                                pad=1)
            conv3_3 = brew.relu(model, conv3_3, conv3_3)
            pool3 = brew.max_pool(model, conv3_3, 'pool3', kernel=2, stride=2)

            conv4_1 = brew.conv(model,
                                pool3,
                                'conv4_1',
                                dim_in=256,
                                dim_out=512,
                                kernel=3,
                                pad=1)
            conv4_1 = brew.relu(model, conv4_1, conv4_1)
            conv4_2 = brew.conv(model,
                                conv4_1,
                                'conv4_2',
                                dim_in=512,
                                dim_out=512,
                                kernel=3,
                                pad=1)
            conv4_2 = brew.relu(model, conv4_2, conv4_2)
            conv4_3 = brew.conv(model,
                                conv4_2,
                                'conv4_3',
                                dim_in=512,
                                dim_out=512,
                                kernel=3,
                                pad=1)
            conv4_3 = brew.relu(model, conv4_3, conv4_3)
            pool4 = brew.max_pool(model, conv4_3, 'pool4', kernel=2, stride=2)

            conv5_1 = brew.conv(model,
                                pool4,
                                'conv5_1',
                                dim_in=512,
                                dim_out=512,
                                kernel=3,
                                pad=1)
            conv5_1 = brew.relu(model, conv5_1, conv5_1)
            conv5_2 = brew.conv(model,
                                conv5_1,
                                'conv5_2',
                                dim_in=512,
                                dim_out=512,
                                kernel=3,
                                pad=1)
            conv5_2 = brew.relu(model, conv5_2, conv5_2)
            conv5_3 = brew.conv(model,
                                conv5_2,
                                'conv5_3',
                                dim_in=512,
                                dim_out=512,
                                kernel=3,
                                pad=1)
            conv5_3 = brew.relu(model, conv5_3, conv5_3)
            pool5 = brew.max_pool(model, conv5_3, 'pool5', kernel=2, stride=2)

            fc6 = brew.fc(model, pool5, 'fc6', dim_in=25088, dim_out=4096)
            fc6 = brew.relu(model, fc6, fc6)
            fc7 = brew.fc(model, fc6, 'fc7', dim_in=4096, dim_out=4096)
            fc7 = brew.relu(model, fc7, fc7)
            pred = brew.fc(model, fc7, 'pred', 4096, 1000)
            softmax = brew.softmax(model, pred, 'softmax')
            return softmax
コード例 #42
0
def alexnet():
    model = ModelHelper(name="r", arg_scope={"order": "NCHW", "is_test": True})
    conv1 = brew.conv(
        model,
        "data",
        "conv1",
        3,
        64,
        11, ('XavierFill', {}), ('ConstantFill', {}),
        stride=4,
        pad=2
    )
    relu1 = brew.relu(model, conv1, "conv1")
    pool1 = brew.max_pool(model, relu1, "pool1", kernel=3, stride=2, pad=0,
                          legacy_pad=3)
    lrn1 = brew.lrn(
        model, pool1, "pool1_lrn", size=5, alpha=1.0e-4, beta=0.75, bias=1.0)
    conv2 = brew.conv(
        model,
        lrn1,
        "conv2",
        64,
        192,
        5,
        ('XavierFill', {}),
        ('ConstantFill', {}),
        pad=2
    )
    relu2 = brew.relu(model, conv2, "conv2")
    pool2 = brew.max_pool(model, relu2, "pool2", kernel=3, stride=2)
    lrn2 = brew.lrn(
        model, pool2, "pool2_lrn", size=5, alpha=1.0e-4, beta=0.75, bias=1.0)
    conv3 = brew.conv(
        model,
        lrn2,
        "conv3",
        192,
        384,
        3,
        ('XavierFill', {}),
        ('ConstantFill', {}),
        pad=1
    )
    relu3 = brew.relu(model, conv3, "conv3")
    conv4 = brew.conv(
        model,
        relu3,
        "conv4",
        384,
        256,
        3,
        ('XavierFill', {}),
        ('ConstantFill', {}),
        pad=1
    )
    relu4 = brew.relu(model, conv4, "conv4")
    conv5 = brew.conv(
        model,
        relu4,
        "conv5",
        256,
        256,
        3,
        ('XavierFill', {}),
        ('ConstantFill', {}),
        pad=1
    )
    relu5 = brew.relu(model, conv5, "conv5")
    pool5 = brew.max_pool(model, relu5, "pool5", kernel=3, stride=2)
    fc6 = brew.fc(
        model,
        pool5, "fc6", 256 * 6 * 6, 4096, ('XavierFill', {}),
        ('ConstantFill', {})
    )
    relu6 = brew.relu(model, fc6, "fc6")
    fc7 = brew.fc(
        model, relu6, "fc7", 4096, 4096, ('XavierFill', {}), ('ConstantFill', {})
    )
    relu7 = brew.relu(model, fc7, "fc7")
    drop7 = brew.dropout(model, relu7, "fc7_dropout", is_test=1, ratio=0.5)
    fc8 = brew.fc(
        model, drop7, "fc8", 4096, 1000, ('XavierFill', {}), ('ConstantFill', {})
    )
    relu8 = brew.relu(model, fc8, "fc8")
    brew.dropout(model, relu8, "fc8_dropout", is_test=1, ratio=0.5)
    return model, [(1, 3, 224, 224)]
コード例 #43
0
    def __init__(self):
        self.Init_Net = None
        self.Temple_Net = None
        self.Track_Net = None
        self.Net_Enum = [
            'init', 'temple', 'track', 'adjust', 'CorrelationFilter'
        ]

        curinput = 'data'
        for net in self.Net_Enum:
            model = model_helper.ModelHelper(
                name=net,
                arg_scope=cnn_arg_scope,
            )
            if net == 'init' or net == 'temple' or net == 'track':
                p = brew.conv(model,
                              'data',
                              'conv_1',
                              3,
                              192,
                              kernel=11,
                              stride=2)
                p = brew.spatial_bn(model,
                                    'conv_1',
                                    'bn_1',
                                    192,
                                    is_test=True,
                                    epsilon=1e-5,
                                    momentum=0.1)
                p = brew.relu(model, 'bn_1', 'bn_1')
                p = brew.max_pool(model, 'bn_1', 'pool_1', kernel=3, stride=2)

                p = brew.conv(model, 'pool_1', 'conv_2', 192, 512, kernel=5)
                p = brew.spatial_bn(model,
                                    'conv_2',
                                    'bn_2',
                                    512,
                                    is_test=True,
                                    epsilon=1e-5,
                                    momentum=0.1)
                p = brew.relu(model, 'bn_2', 'bn_2')
                p = brew.max_pool(model, 'bn_2', 'pool_2', kernel=3, stride=2)

                p = brew.conv(model, 'pool_2', 'conv_3', 512, 768, kernel=3)
                p = brew.spatial_bn(model,
                                    'conv_3',
                                    'bn_3',
                                    768,
                                    is_test=True,
                                    epsilon=1e-5,
                                    momentum=0.1)
                p = brew.relu(model, 'bn_3', 'bn_3')

                p = brew.conv(model, 'bn_3', 'conv_4', 768, 768, kernel=3)
                p = brew.spatial_bn(model,
                                    'conv_4',
                                    'bn_4',
                                    768,
                                    is_test=True,
                                    epsilon=1e-5,
                                    momentum=0.1)
                p = brew.relu(model, 'bn_4', 'bn_4')

                p = brew.conv(model, 'bn_4', 'conv_5', 768, 512, kernel=3)
                p = brew.spatial_bn(model,
                                    'conv_5',
                                    'feature_out',
                                    512,
                                    is_test=True,
                                    epsilon=1e-5,
                                    momentum=0.1)

                #rpn sub networks
                if net == 'init':
                    p = brew.conv(model,
                                  'feature_out',
                                  'conv_r1',
                                  512,
                                  512 * 4 * 5,
                                  kernel=3)
                    p = brew.conv(model,
                                  'feature_out',
                                  'conv_r2',
                                  512,
                                  512,
                                  kernel=3)
                    p = brew.conv(model,
                                  'feature_out',
                                  'conv_cls1',
                                  512,
                                  512 * 2 * 5,
                                  kernel=3)
                    p = brew.conv(model,
                                  'feature_out',
                                  'conv_cls2',
                                  512,
                                  512,
                                  kernel=3)
                    self.add_inference_inputs(model)
                    self.Init_Net = model.param_init_net
                    self.InitModel = model
                elif net == 'temple':
                    p = brew.conv(model,
                                  'feature_out',
                                  'conv_r1',
                                  512,
                                  512 * 4 * 5,
                                  kernel=3)
                    p = brew.conv(model,
                                  'feature_out',
                                  'conv_cls1',
                                  512,
                                  512 * 2 * 5,
                                  kernel=3)
                    self.add_inference_inputs(model)
                    self.Temple_Net = model.net
                    self.TempleModel = model
                elif net == 'track':
                    p = brew.conv(model,
                                  'feature_out',
                                  'conv_r2',
                                  512,
                                  512,
                                  kernel=3)
                    p = brew.conv(model,
                                  'feature_out',
                                  'conv_cls2',
                                  512,
                                  512,
                                  kernel=3)
                    self.add_inference_inputs(model)
                    self.Track_Net = model.net
                    self.TrackModel = model
            elif net == 'adjust':
                p = brew.conv(model,
                              'conv_conv_r2',
                              'r2_out',
                              4 * 5,
                              5 * 4,
                              kernel=1)
                self.add_inference_inputs(model)
                self.addJustModel = model
            elif net == 'CorrelationFilter':
                p = brew.conv(model,
                              'conv_r2',
                              'conv_conv_r2',
                              4 * 5,
                              5 * 4,
                              kernel=1)
                self.add_inference_inputs(model)
                self.CorrelationFilterModel = model
コード例 #44
0
ファイル: convnet_benchmarks.py プロジェクト: Ralfhund/caffe2
def AlexNet(order, cudnn_ws):
    my_arg_scope = {
        'order': order,
        'use_cudnn': True,
        'cudnn_exhaustive_search': True,
    }
    if cudnn_ws:
        my_arg_scope['ws_nbytes_limit'] = cudnn_ws
    model = model_helper.ModelHelper(
        name="alexnet",
        arg_scope=my_arg_scope,
    )
    conv1 = brew.conv(
        model,
        "data",
        "conv1",
        3,
        64,
        11, ('XavierFill', {}), ('ConstantFill', {}),
        stride=4,
        pad=2
    )
    relu1 = brew.relu(model, conv1, "conv1")
    pool1 = brew.max_pool(model, relu1, "pool1", kernel=3, stride=2)
    conv2 = brew.conv(
        model,
        pool1,
        "conv2",
        64,
        192,
        5,
        ('XavierFill', {}),
        ('ConstantFill', {}),
        pad=2
    )
    relu2 = brew.relu(model, conv2, "conv2")
    pool2 = brew.max_pool(model, relu2, "pool2", kernel=3, stride=2)
    conv3 = brew.conv(
        model,
        pool2,
        "conv3",
        192,
        384,
        3,
        ('XavierFill', {}),
        ('ConstantFill', {}),
        pad=1
    )
    relu3 = brew.relu(model, conv3, "conv3")
    conv4 = brew.conv(
        model,
        relu3,
        "conv4",
        384,
        256,
        3,
        ('XavierFill', {}),
        ('ConstantFill', {}),
        pad=1
    )
    relu4 = brew.relu(model, conv4, "conv4")
    conv5 = brew.conv(
        model,
        relu4,
        "conv5",
        256,
        256,
        3,
        ('XavierFill', {}),
        ('ConstantFill', {}),
        pad=1
    )
    relu5 = brew.relu(model, conv5, "conv5")
    pool5 = brew.max_pool(model, relu5, "pool5", kernel=3, stride=2)
    fc6 = brew.fc(
        model,
        pool5, "fc6", 256 * 6 * 6, 4096, ('XavierFill', {}),
        ('ConstantFill', {})
    )
    relu6 = brew.relu(model, fc6, "fc6")
    fc7 = brew.fc(
        model, relu6, "fc7", 4096, 4096, ('XavierFill', {}), ('ConstantFill', {})
    )
    relu7 = brew.relu(model, fc7, "fc7")
    fc8 = brew.fc(
        model, relu7, "fc8", 4096, 1000, ('XavierFill', {}), ('ConstantFill', {})
    )
    pred = brew.softmax(model, fc8, "pred")
    xent = model.net.LabelCrossEntropy([pred, "label"], "xent")
    model.net.AveragedLoss(xent, "loss")
    return model, 224
コード例 #45
0
ファイル: alexnet.py プロジェクト: Luo-Liang/caffe2
def create_alexnet(
    model,
    data,
    num_input_channels,
    num_labels,
    is_test=False,
):
    conv1 = brew.conv(
        model,
        data,
        "conv1",
        num_input_channels,  # dim_in
        64,  # dim_out
        11,  # kernel
        ('XavierFill', {}),
        ('ConstantFill', {}),
        stride=4,
        pad=2)
    relu1 = brew.relu(model, conv1, "conv1")
    norm1 = brew.lrn(model, relu1, "norm1", size=5, alpha=0.0001, beta=0.75)
    pool1 = brew.max_pool(model, norm1, "pool1", kernel=3, stride=2)
    conv2 = brew.conv(model,
                      pool1,
                      "conv2",
                      64,
                      192,
                      5, ('XavierFill', {}), ('ConstantFill', {}),
                      pad=2)
    relu2 = brew.relu(model, conv2, "conv2")
    norm2 = brew.lrn(model, relu2, "norm2", size=5, alpha=0.0001, beta=0.75)
    pool2 = brew.max_pool(model, norm2, "pool2", kernel=3, stride=2)
    conv3 = brew.conv(model,
                      pool2,
                      "conv3",
                      192,
                      384,
                      3, ('XavierFill', {}), ('ConstantFill', {}),
                      pad=1)
    relu3 = brew.relu(model, conv3, "conv3")
    conv4 = brew.conv(model,
                      relu3,
                      "conv4",
                      384,
                      256,
                      3, ('XavierFill', {}), ('ConstantFill', {}),
                      pad=1)
    relu4 = brew.relu(model, conv4, "conv4")
    conv5 = brew.conv(model,
                      relu4,
                      "conv5",
                      256,
                      256,
                      3, ('XavierFill', {}), ('ConstantFill', {}),
                      pad=1)
    relu5 = brew.relu(model, conv5, "conv5")
    pool5 = brew.max_pool(model, relu5, "pool5", kernel=3, stride=2)
    fc6 = brew.fc(model, pool5, "fc6", 256 * 6 * 6, 4096, ('XavierFill', {}),
                  ('ConstantFill', {}))
    relu6 = brew.relu(model, fc6, "fc6")
    dropout1 = brew.dropout(model,
                            relu6,
                            'dropout1',
                            ratio=0.5,
                            is_test=is_test)

    fc7 = brew.fc(model, dropout1, "fc7", 4096, 4096, ('XavierFill', {}),
                  ('ConstantFill', {}))
    relu7 = brew.relu(model, fc7, "fc7")
    dropout2 = brew.dropout(model,
                            relu7,
                            'dropout2',
                            ratio=0.5,
                            is_test=is_test)

    fc8 = brew.fc(model, dropout2, "fc8", 4096, num_labels, ('XavierFill', {}),
                  ('ConstantFill', {}))
    # pred = brew.softmax(model, fc8, "pred")
    # xent = model.net.LabelCrossEntropy([pred, "label"], "xent")
    # model.net.AveragedLoss(xent, "loss")
    return fc8
コード例 #46
0
ファイル: convnet_benchmarks.py プロジェクト: inbei/rocket
def VGGA(order, cudnn_ws, device):
    my_arg_scope = {
        'order': order,
        'use_cudnn': True,
        'cudnn_exhaustive_search': True,
        'ws_nbytes_limit': str(cudnn_ws)
    }
    model = ModelHelper(name='vgg-a', arg_scope=my_arg_scope)
    conv1 = brew.conv(model,
                      "data",
                      "conv1",
                      3,
                      64,
                      3, ('XavierFill', {}), ('ConstantFill', {}),
                      pad=1)
    relu1 = brew.relu(model, conv1, "conv1")
    pool1 = brew.max_pool(model, relu1, "pool1", kernel=2, stride=2)
    conv2 = brew.conv(model,
                      pool1,
                      "conv2",
                      64,
                      128,
                      3, ('XavierFill', {}), ('ConstantFill', {}),
                      pad=1)
    relu2 = brew.relu(model, conv2, "conv2")
    pool2 = brew.max_pool(model, relu2, "pool2", kernel=2, stride=2)
    conv3 = brew.conv(model,
                      pool2,
                      "conv3",
                      128,
                      256,
                      3, ('XavierFill', {}), ('ConstantFill', {}),
                      pad=1)
    relu3 = brew.relu(model, conv3, "conv3")
    conv4 = brew.conv(model,
                      relu3,
                      "conv4",
                      256,
                      256,
                      3, ('XavierFill', {}), ('ConstantFill', {}),
                      pad=1)
    relu4 = brew.relu(model, conv4, "conv4")
    pool4 = brew.max_pool(model, relu4, "pool4", kernel=2, stride=2)
    conv5 = brew.conv(model,
                      pool4,
                      "conv5",
                      256,
                      512,
                      3, ('XavierFill', {}), ('ConstantFill', {}),
                      pad=1)
    relu5 = brew.relu(model, conv5, "conv5")
    conv6 = brew.conv(model,
                      relu5,
                      "conv6",
                      512,
                      512,
                      3, ('XavierFill', {}), ('ConstantFill', {}),
                      pad=1)
    relu6 = brew.relu(model, conv6, "conv6")
    pool6 = brew.max_pool(model, relu6, "pool6", kernel=2, stride=2)
    conv7 = brew.conv(model,
                      pool6,
                      "conv7",
                      512,
                      512,
                      3, ('XavierFill', {}), ('ConstantFill', {}),
                      pad=1)
    relu7 = brew.relu(model, conv7, "conv7")
    conv8 = brew.conv(model,
                      relu7,
                      "conv8",
                      512,
                      512,
                      3, ('XavierFill', {}), ('ConstantFill', {}),
                      pad=1)
    relu8 = brew.relu(model, conv8, "conv8")
    pool8 = brew.max_pool(model, relu8, "pool8", kernel=2, stride=2)

    fcix = brew.fc(model, pool8, "fcix", 512 * 7 * 7, 4096, ('XavierFill', {}),
                   ('ConstantFill', {}))
    reluix = brew.relu(model, fcix, "fcix")
    fcx = brew.fc(model, reluix, "fcx", 4096, 4096, ('XavierFill', {}),
                  ('ConstantFill', {}))
    relux = brew.relu(model, fcx, "fcx")
    fcxi = brew.fc(model, relux, "fcxi", 4096, 1000, ('XavierFill', {}),
                   ('ConstantFill', {}))
    pred = brew.softmax(model, fcxi, "pred")
    xent = model.LabelCrossEntropy([pred, "label"], "xent")
    if device != 'MKL':
        loss = model.AveragedLoss(xent, "loss")
    return model, 231
コード例 #47
0
ファイル: convnet_benchmarks.py プロジェクト: inbei/rocket
def Inception(order, cudnn_ws, device):
    my_arg_scope = {
        'order': order,
        'use_cudnn': True,
        'cudnn_exhaustive_search': True,
        'ws_nbytes_limit': str(cudnn_ws)
    }
    model = ModelHelper(name="inception", arg_scope=my_arg_scope)
    conv1 = brew.conv(model,
                      "data",
                      "conv1",
                      3,
                      64,
                      7, ('XavierFill', {}), ('ConstantFill', {}),
                      stride=2,
                      pad=3)
    relu1 = brew.relu(model, conv1, "conv1")
    pool1 = brew.max_pool(model, relu1, "pool1", kernel=3, stride=2, pad=1)
    conv2a = brew.conv(model, pool1, "conv2a", 64, 64, 1, ('XavierFill', {}),
                       ('ConstantFill', {}))
    conv2a = brew.relu(model, conv2a, conv2a)
    conv2 = brew.conv(model,
                      conv2a,
                      "conv2",
                      64,
                      192,
                      3, ('XavierFill', {}), ('ConstantFill', {}),
                      pad=1)
    relu2 = brew.relu(model, conv2, "conv2")
    pool2 = brew.max_pool(model, relu2, "pool2", kernel=3, stride=2, pad=1)
    # Inception modules
    inc3 = _InceptionModule(model, pool2, 192, "inc3", 64, [96, 128], [16, 32],
                            32)
    inc4 = _InceptionModule(model, inc3, 256, "inc4", 128, [128, 192],
                            [32, 96], 64)
    pool5 = brew.max_pool(model, inc4, "pool5", kernel=3, stride=2, pad=1)
    inc5 = _InceptionModule(model, pool5, 480, "inc5", 192, [96, 208],
                            [16, 48], 64)
    inc6 = _InceptionModule(model, inc5, 512, "inc6", 160, [112, 224],
                            [24, 64], 64)
    inc7 = _InceptionModule(model, inc6, 512, "inc7", 128, [128, 256],
                            [24, 64], 64)
    inc8 = _InceptionModule(model, inc7, 512, "inc8", 112, [144, 288],
                            [32, 64], 64)
    inc9 = _InceptionModule(model, inc8, 528, "inc9", 256, [160, 320],
                            [32, 128], 128)
    pool9 = brew.max_pool(model, inc9, "pool9", kernel=3, stride=2, pad=1)
    inc10 = _InceptionModule(model, pool9, 832, "inc10", 256, [160, 320],
                             [32, 128], 128)
    inc11 = _InceptionModule(model, inc10, 832, "inc11", 384, [192, 384],
                             [48, 128], 128)
    pool11 = brew.average_pool(model, inc11, "pool11", kernel=7, stride=1)
    fc = brew.fc(model, pool11, "fc", 1024, 1000, ('XavierFill', {}),
                 ('ConstantFill', {}))
    # It seems that Soumith's benchmark does not have softmax on top
    # for Inception. We will add it anyway so we can have a proper
    # backward pass.
    pred = brew.softmax(model, fc, "pred")
    xent = model.LabelCrossEntropy([pred, "label"], "xent")
    if device != 'MKL':
        loss = model.AveragedLoss(xent, "loss")
    return model, 224
コード例 #48
0
    def add_bottleneck(
        self,
        input_filters,  # num of feature maps from preceding layer
        base_filters,  # num of filters internally in the component
        output_filters,  # num of feature maps to output
        down_sampling=False,
        spatial_batch_norm=True,
    ):
        self.comp_idx = 0
        shortcut_blob = self.prev_blob

        # 1x1
        self.add_conv(input_filters, base_filters, kernel=1, stride=1)

        if spatial_batch_norm:
            self.add_spatial_bn(base_filters)

        self.add_relu()

        # 3x3 (note the pad, required for keeping dimensions)
        self.add_conv(base_filters,
                      base_filters,
                      kernel=3,
                      stride=(1 if down_sampling is False else 2),
                      pad=1)

        if spatial_batch_norm:
            self.add_spatial_bn(base_filters)
        self.add_relu()

        # 1x1
        last_conv = self.add_conv(base_filters, output_filters, kernel=1)
        if spatial_batch_norm:
            last_conv = self.add_spatial_bn(output_filters)

        # Summation with input signal (shortcut)
        # If we need to increase dimensions (feature maps), need to
        # do a projection for the short cut
        if (output_filters > input_filters):
            shortcut_blob = brew.conv(
                self.model,
                shortcut_blob,
                'shortcut_projection_%d' % self.comp_count,
                input_filters,
                output_filters,
                weight_init=("MSRAFill", {}),
                kernel=1,
                stride=(1 if down_sampling is False else 2),
                no_bias=self.no_bias,
            )
            if spatial_batch_norm:
                shortcut_blob = brew.spatial_bn(
                    self.model,
                    shortcut_blob,
                    'shortcut_projection_%d_spatbn' % self.comp_count,
                    output_filters,
                    epsilon=1e-3,
                    momentum=self.spatial_bn_mom,
                    is_test=self.is_test,
                )

        self.prev_blob = brew.sum(
            self.model, [shortcut_blob, last_conv],
            'comp_%d_sum_%d' % (self.comp_count, self.comp_idx))
        self.comp_idx += 1
        self.add_relu()

        # Keep track of number of high level components if this ResNetBuilder
        self.comp_count += 1
コード例 #49
0
def Inception(model, loss_scale, dtype='float'):
    initializer = (PseudoFP16Initializer
                   if dtype == 'float16' else Initializer)
    with brew.arg_scope(
        [brew.conv, brew.fc],
            WeightInitializer=initializer,
            BiasInitializer=initializer,
    ):
        conv1 = brew.conv(
            model,
            "data",
            "conv1",
            3,
            64,
            7,
            ('XavierFill', {}),
            ('ConstantFill', {}),
            stride=2,
            pad=3,
        )
        relu1 = brew.relu(model, conv1, "conv1")
        pool1 = brew.max_pool(model, relu1, "pool1", kernel=3, stride=2, pad=1)
        conv2a = brew.conv(model, pool1, "conv2a", 64, 64, 1,
                           ('XavierFill', {}), ('ConstantFill', {}))
        conv2a = brew.relu(model, conv2a, conv2a)
        conv2 = brew.conv(
            model,
            conv2a,
            "conv2",
            64,
            192,
            3,
            ('XavierFill', {}),
            ('ConstantFill', {}),
            pad=1,
        )
        relu2 = brew.relu(model, conv2, "conv2")
        pool2 = brew.max_pool(model, relu2, "pool2", kernel=3, stride=2, pad=1)
        # Inception modules
        inc3 = _InceptionModule(model, pool2, 192, "inc3", 64, [96, 128],
                                [16, 32], 32)
        inc4 = _InceptionModule(model, inc3, 256, "inc4", 128, [128, 192],
                                [32, 96], 64)
        pool5 = brew.max_pool(model, inc4, "pool5", kernel=3, stride=2, pad=1)
        inc5 = _InceptionModule(model, pool5, 480, "inc5", 192, [96, 208],
                                [16, 48], 64)
        inc6 = _InceptionModule(model, inc5, 512, "inc6", 160, [112, 224],
                                [24, 64], 64)
        inc7 = _InceptionModule(model, inc6, 512, "inc7", 128, [128, 256],
                                [24, 64], 64)
        inc8 = _InceptionModule(model, inc7, 512, "inc8", 112, [144, 288],
                                [32, 64], 64)
        inc9 = _InceptionModule(model, inc8, 528, "inc9", 256, [160, 320],
                                [32, 128], 128)
        pool9 = brew.max_pool(model, inc9, "pool9", kernel=3, stride=2, pad=1)
        inc10 = _InceptionModule(model, pool9, 832, "inc10", 256, [160, 320],
                                 [32, 128], 128)
        inc11 = _InceptionModule(model, inc10, 832, "inc11", 384, [192, 384],
                                 [48, 128], 128)
        pool11 = brew.average_pool(model, inc11, "pool11", kernel=7, stride=1)
        fc = brew.fc(model, pool11, "fc", 1024, 1000, ('XavierFill', {}),
                     ('ConstantFill', {}))
    if dtype == 'float16':
        fc = model.net.HalfToFloat(fc, fc + '_fp32')
    # It seems that Soumith's benchmark does not have softmax on top
    # for Inception. We will add it anyway so we can have a proper
    # backward pass.
    pred = brew.softmax(model, fc, "pred")
    xent = model.net.LabelCrossEntropy([pred, "label"], "xent")
    loss = model.net.AveragedLoss(xent, "loss")
    return [loss]
コード例 #50
0
def Add_Original_CIFAR10_Model(model, data, num_classes, image_height,
                               image_width, image_channels):
    # Convolutional layer 1
    conv1 = brew.conv(model,
                      data,
                      'conv1',
                      dim_in=image_channels,
                      dim_out=32,
                      kernel=5,
                      stride=1,
                      pad=2)
    h, w = update_dims(height=image_height,
                       width=image_width,
                       kernel=5,
                       stride=1,
                       pad=2)
    # Pooling layer 1
    pool1 = brew.max_pool(model, conv1, 'pool1', kernel=3, stride=2)
    h, w = update_dims(height=h, width=w, kernel=3, stride=2, pad=0)
    # ReLU layer 1
    relu1 = brew.relu(model, pool1, 'relu1')

    # Convolutional layer 2
    conv2 = brew.conv(model,
                      relu1,
                      'conv2',
                      dim_in=32,
                      dim_out=32,
                      kernel=5,
                      stride=1,
                      pad=2)
    h, w = update_dims(height=h, width=w, kernel=5, stride=1, pad=2)
    # ReLU layer 2
    relu2 = brew.relu(model, conv2, 'relu2')
    # Pooling layer 1
    pool2 = brew.average_pool(model, relu2, 'pool2', kernel=3, stride=2)
    h, w = update_dims(height=h, width=w, kernel=3, stride=2, pad=0)

    # Convolutional layer 3
    conv3 = brew.conv(model,
                      pool2,
                      'conv3',
                      dim_in=32,
                      dim_out=64,
                      kernel=5,
                      stride=1,
                      pad=2)
    h, w = update_dims(height=h, width=w, kernel=5, stride=1, pad=2)
    # ReLU layer 3
    relu3 = brew.relu(model, conv3, 'relu3')
    # Pooling layer 3
    pool3 = brew.average_pool(model, relu3, 'pool3', kernel=3, stride=2)
    h, w = update_dims(height=h, width=w, kernel=3, stride=2, pad=0)

    # Fully connected layers
    fc1 = brew.fc(model, pool3, 'fc1', dim_in=64 * h * w, dim_out=64)
    fc2 = brew.fc(model, fc1, 'fc2', dim_in=64, dim_out=num_classes)

    # Softmax layer
    softmax = brew.softmax(model, fc2, 'softmax')
    return softmax
コード例 #51
0
def Inception(order, cudnn_ws, device):
    my_arg_scope = {'order': order, 'use_cudnn': True,
                    'cudnn_exhaustive_search': True,
                    'ws_nbytes_limit': str(cudnn_ws)}
    model = ModelHelper(name="inception", arg_scope=my_arg_scope)
    conv1 = brew.conv(
        model,
        "data",
        "conv1",
        3,
        64,
        7,
        ('XavierFill', {}),
        ('ConstantFill', {}),
        stride=2,
        pad=3
    )
    relu1 = brew.relu(model, conv1, "conv1")
    pool1 = brew.max_pool(model, relu1, "pool1", kernel=3, stride=2, pad=1)
    conv2a = brew.conv(
        model, pool1, "conv2a", 64, 64, 1,
        ('XavierFill', {}), ('ConstantFill', {})
    )
    conv2a = brew.relu(model, conv2a, conv2a)
    conv2 = brew.conv(
        model,
        conv2a,
        "conv2",
        64,
        192,
        3,
        ('XavierFill', {}),
        ('ConstantFill', {}),
        pad=1
    )
    relu2 = brew.relu(model, conv2, "conv2")
    pool2 = brew.max_pool(model, relu2, "pool2", kernel=3, stride=2, pad=1)
    # Inception modules
    inc3 = _InceptionModule(
        model, pool2, 192, "inc3", 64, [96, 128], [16, 32], 32
    )
    inc4 = _InceptionModule(
        model, inc3, 256, "inc4", 128, [128, 192], [32, 96], 64
    )
    pool5 = brew.max_pool(model, inc4, "pool5", kernel=3, stride=2, pad=1)
    inc5 = _InceptionModule(
        model, pool5, 480, "inc5", 192, [96, 208], [16, 48], 64
    )
    inc6 = _InceptionModule(
        model, inc5, 512, "inc6", 160, [112, 224], [24, 64], 64
    )
    inc7 = _InceptionModule(
        model, inc6, 512, "inc7", 128, [128, 256], [24, 64], 64
    )
    inc8 = _InceptionModule(
        model, inc7, 512, "inc8", 112, [144, 288], [32, 64], 64
    )
    inc9 = _InceptionModule(
        model, inc8, 528, "inc9", 256, [160, 320], [32, 128], 128
    )
    pool9 = brew.max_pool(model, inc9, "pool9", kernel=3, stride=2, pad=1)
    inc10 = _InceptionModule(
        model, pool9, 832, "inc10", 256, [160, 320], [32, 128], 128
    )
    inc11 = _InceptionModule(
        model, inc10, 832, "inc11", 384, [192, 384], [48, 128], 128
    )
    pool11 = brew.average_pool(model, inc11, "pool11", kernel=7, stride=1)
    fc = brew.fc(
        model, pool11, "fc", 1024, 1000,
        ('XavierFill', {}), ('ConstantFill', {})
    )
    # It seems that Soumith's benchmark does not have softmax on top
    # for Inception. We will add it anyway so we can have a proper
    # backward pass.
    pred = brew.softmax(model, fc, "pred")
    xent = model.LabelCrossEntropy([pred, "label"], "xent")
    if device != 'MKL':
        loss = model.AveragedLoss(xent, "loss")
    return model, 224
コード例 #52
0
import caffe2.contrib.tensorboard.tensorboard as tb
import caffe2.contrib.tensorboard.tensorboard_exporter as tb_exporter
from caffe2.python import brew, core, model_helper

model = model_helper.ModelHelper(name="overfeat")
data, label = brew.image_input(model, ["db"], ["data", "label"], is_test=0)

with core.NameScope("conv1"):
    conv1 = brew.conv(model, data, "conv1", 3, 96, 11, stride=4)
    relu1 = brew.relu(model, conv1, conv1)
    pool1 = brew.max_pool(model, relu1, "pool1", kernel=2, stride=2)
with core.NameScope("conv2"):
    conv2 = brew.conv(model, pool1, "conv2", 96, 256, 5)
    relu2 = brew.relu(model, conv2, conv2)
    pool2 = brew.max_pool(model, relu2, "pool2", kernel=2, stride=2)
with core.NameScope("conv3"):
    conv3 = brew.conv(model, pool2, "conv3", 256, 512, 3, pad=1)
    relu3 = brew.relu(model, conv3, conv3)
with core.NameScope("conv4"):
    conv4 = brew.conv(model, relu3, "conv4", 512, 1024, 3, pad=1)
    relu4 = brew.relu(model, conv4, conv4)
with core.NameScope("conv5"):
    conv5 = brew.conv(model, relu4, "conv5", 1024, 1024, 3, pad=1)
    relu5 = brew.relu(model, conv5, conv5)
    pool5 = brew.max_pool(model, relu5, "pool5", kernel=2, stride=2)
with core.NameScope("fc6"):
    fc6 = brew.fc(model, pool5, "fc6", 1024 * 6 * 6, 3072)
    relu6 = brew.relu(model, fc6, "fc6")
with core.NameScope("fc7"):
    fc7 = brew.fc(model, relu6, "fc7", 3072, 4096)
    relu7 = brew.relu(model, fc7, "fc7")
コード例 #53
0
def _InceptionModule(
    model, input_blob, input_depth, output_name, conv1_depth, conv3_depths,
    conv5_depths, pool_depth
):
# path 1: 1x1 conv
    conv1 = brew.conv(
        model, input_blob, output_name + ":conv1", input_depth, conv1_depth, 1,
        ('XavierFill', {}), ('ConstantFill', {})
    )
    conv1 = brew.relu(model, conv1, conv1)
    # path 2: 1x1 conv + 3x3 conv
    conv3_reduce = brew.conv(
        model, input_blob, output_name + ":conv3_reduce", input_depth,
        conv3_depths[0], 1, ('XavierFill', {}), ('ConstantFill', {})
    )
    conv3_reduce = brew.relu(model, conv3_reduce, conv3_reduce)
    conv3 = brew.conv(
        model,
        conv3_reduce,
        output_name + ":conv3",
        conv3_depths[0],
        conv3_depths[1],
        3,
        ('XavierFill', {}),
        ('ConstantFill', {}),
        pad=1
    )
    conv3 = brew.relu(model, conv3, conv3)
    # path 3: 1x1 conv + 5x5 conv
    conv5_reduce = brew.conv(
        model, input_blob, output_name + ":conv5_reduce", input_depth,
        conv5_depths[0], 1, ('XavierFill', {}), ('ConstantFill', {})
    )
    conv5_reduce = brew.relu(model, conv5_reduce, conv5_reduce)
    conv5 = brew.conv(
        model,
        conv5_reduce,
        output_name + ":conv5",
        conv5_depths[0],
        conv5_depths[1],
        5,
        ('XavierFill', {}),
        ('ConstantFill', {}),
        pad=2
    )
    conv5 = brew.relu(model, conv5, conv5)
    # path 4: pool + 1x1 conv
    pool = brew.max_pool(
        model,
        input_blob,
        output_name + ":pool",
        kernel=3,
        stride=1,
        pad=1
    )
    pool_proj = brew.conv(
        model, pool, output_name + ":pool_proj", input_depth, pool_depth, 1,
        ('XavierFill', {}), ('ConstantFill', {})
    )
    pool_proj = brew.relu(model, pool_proj, pool_proj)
    output = brew.concat(model, [conv1, conv3, conv5, pool_proj], output_name)
    return output
コード例 #54
0
def create_resnet152(
    model,
    data,
    num_input_channels,
    num_labels,
    label=None,
    is_test=False,
    no_loss=False,
    no_bias=0,
    conv1_kernel=7,
    conv1_stride=2,
    final_avg_kernel=7,
):
    # conv1 + maxpool
    brew.conv(model,
              data,
              'conv1',
              num_input_channels,
              64,
              weight_init=("MSRAFill", {}),
              kernel=conv1_kernel,
              stride=conv1_stride,
              pad=3,
              no_bias=no_bias)

    brew.spatial_bn(model,
                    'conv1',
                    'conv1_spatbn_relu',
                    64,
                    epsilon=1e-3,
                    momentum=0.1,
                    is_test=is_test)
    brew.relu(model, 'conv1_spatbn_relu', 'conv1_spatbn_relu')
    brew.max_pool(model, 'conv1_spatbn_relu', 'pool1', kernel=3, stride=2)

    # Residual blocks...
    builder = ResNetBuilder(model,
                            'pool1',
                            no_bias=no_bias,
                            is_test=is_test,
                            spatial_bn_mom=0.1)

    # conv2_x (ref Table 1 in He et al. (2015))
    builder.add_bottleneck(64, 64, 256)
    builder.add_bottleneck(256, 64, 256)
    builder.add_bottleneck(256, 64, 256)

    # conv3_x
    builder.add_bottleneck(256, 128, 512, down_sampling=True)
    for _ in range(1, 8):
        builder.add_bottleneck(512, 128, 512)

    # conv4_x
    builder.add_bottleneck(512, 256, 1024, down_sampling=True)
    for _ in range(1, 36):
        builder.add_bottleneck(1024, 256, 1024)

    # conv5_x
    builder.add_bottleneck(1024, 512, 2048, down_sampling=True)
    builder.add_bottleneck(2048, 512, 2048)
    builder.add_bottleneck(2048, 512, 2048)

    # Final layers
    final_avg = brew.average_pool(
        model,
        builder.prev_blob,
        'final_avg',
        kernel=final_avg_kernel,
        stride=1,
    )

    # Final dimension of the "image" is reduced to 7x7
    last_out = brew.fc(model, final_avg, 'last_out_L{}'.format(num_labels),
                       2048, num_labels)

    if no_loss:
        return last_out

    # If we create model for training, use softmax-with-loss
    if (label is not None):
        (softmax, loss) = model.SoftmaxWithLoss(
            [last_out, label],
            ["softmax", "loss"],
        )

        return (softmax, loss)
    else:
        # For inference, we just return softmax
        return brew.softmax(model, last_out, "softmax")
コード例 #55
0
ファイル: resnet.py プロジェクト: bittnt/caffe2
    def add_bottleneck(
        self,
        input_filters,   # num of feature maps from preceding layer
        base_filters,    # num of filters internally in the component
        output_filters,  # num of feature maps to output
        down_sampling=False,
        spatial_batch_norm=True,
    ):
        self.comp_idx = 0
        shortcut_blob = self.prev_blob

        # 1x1
        self.add_conv(
            input_filters,
            base_filters,
            kernel=1,
            stride=1
        )

        if spatial_batch_norm:
            self.add_spatial_bn(base_filters)

        self.add_relu()

        # 3x3 (note the pad, required for keeping dimensions)
        self.add_conv(
            base_filters,
            base_filters,
            kernel=3,
            stride=(1 if down_sampling is False else 2),
            pad=1
        )

        if spatial_batch_norm:
            self.add_spatial_bn(base_filters)
        self.add_relu()

        # 1x1
        last_conv = self.add_conv(base_filters, output_filters, kernel=1)
        if spatial_batch_norm:
            last_conv = self.add_spatial_bn(output_filters)

        # Summation with input signal (shortcut)
        # If we need to increase dimensions (feature maps), need to
        # do a projection for the short cut
        if (output_filters > input_filters):
            shortcut_blob = brew.conv(
                self.model,
                shortcut_blob,
                'shortcut_projection_%d' % self.comp_count,
                input_filters,
                output_filters,
                weight_init=("MSRAFill", {}),
                kernel=1,
                stride=(1 if down_sampling is False else 2),
                no_bias=self.no_bias,
            )
            if spatial_batch_norm:
                shortcut_blob = brew.spatial_bn(
                    self.model,
                    shortcut_blob,
                    'shortcut_projection_%d_spatbn' % self.comp_count,
                    output_filters,
                    epsilon=1e-3,
                    momentum=self.spatial_bn_mom,
                    is_test=self.is_test,
                )

        self.prev_blob = brew.sum(
            self.model, [shortcut_blob, last_conv],
            'comp_%d_sum_%d' % (self.comp_count, self.comp_idx)
        )
        self.comp_idx += 1
        self.add_relu()

        # Keep track of number of high level components if this ResNetBuilder
        self.comp_count += 1
コード例 #56
0
    def add_bottleneck(
        self,
        input_filters,
        base_filters,
        output_filters,
        down_sampling=False,
        spatial_batch_norm=True,
    ):
        self.comp_idx = 0
        shortcut_blob = self.prev_blob

        # 1x1
        self.add_conv(input_filters, base_filters, kernel=1, stride=1)

        if spatial_batch_norm:
            self.add_spatial_bn(base_filters)

        self.add_relu()

        # 3x3 (note the pad, required for keeping dimensions)
        self.add_conv(base_filters,
                      base_filters,
                      kernel=3,
                      stride=(1 if down_sampling is False else 2),
                      pad=1)

        if spatial_batch_norm:
            self.add_spatial_bn(base_filters)
        self.add_relu()

        # 1x1
        last_conv = self.add_conv(base_filters, output_filters, kernel=1)
        if spatial_batch_norm:
            last_conv = self.add_spatial_bn(output_filters)

        if (output_filters > input_filters):
            shortcut_blob = brew.conv(
                self.model,
                shortcut_blob,
                'shortcut_projection_%d' % self.comp_count,
                input_filters,
                output_filters,
                weight_init=("MSRAFill", {}),
                kernel=1,
                stride=(1 if down_sampling is False else 2),
                no_bias=self.no_bias,
            )
            if spatial_batch_norm:
                shortcut_blob = brew.spatial_bn(
                    self.model,
                    shortcut_blob,
                    'shortcut_projection_%d_spatbn' % self.comp_count,
                    output_filters,
                    epsilon=1e-3,
                    momentum=self.spatial_bn_mom,
                    is_test=self.is_test,
                )

        self.prev_blob = brew.sum(
            self.model, [shortcut_blob, last_conv],
            'comp_%d_sum_%d' % (self.comp_count, self.comp_idx))
        self.comp_idx += 1
        self.add_relu()
        self.comp_count += 1
コード例 #57
0
ファイル: convnet_benchmarks.py プロジェクト: inbei/rocket
def AlexNet(order, cudnn_ws, device):
    my_arg_scope = {
        'order': order,
        'use_cudnn': True,
        'cudnn_exhaustive_search': True,
        'ws_nbytes_limit': str(cudnn_ws)
    }
    model = ModelHelper(name="alexnet", arg_scope=my_arg_scope)
    conv1 = brew.conv(model,
                      "data",
                      "conv1",
                      3,
                      64,
                      11, ('XavierFill', {}), ('ConstantFill', {}),
                      stride=4,
                      pad=2)
    relu1 = brew.relu(model, conv1, "conv1")
    pool1 = brew.max_pool(model, relu1, "pool1", kernel=3, stride=2)
    conv2 = brew.conv(model,
                      pool1,
                      "conv2",
                      64,
                      192,
                      5, ('XavierFill', {}), ('ConstantFill', {}),
                      pad=2)
    relu2 = brew.relu(model, conv2, "conv2")
    pool2 = brew.max_pool(model, relu2, "pool2", kernel=3, stride=2)
    conv3 = brew.conv(model,
                      pool2,
                      "conv3",
                      192,
                      384,
                      3, ('XavierFill', {}), ('ConstantFill', {}),
                      pad=1)
    relu3 = brew.relu(model, conv3, "conv3")
    conv4 = brew.conv(model,
                      relu3,
                      "conv4",
                      384,
                      256,
                      3, ('XavierFill', {}), ('ConstantFill', {}),
                      pad=1)
    relu4 = brew.relu(model, conv4, "conv4")
    conv5 = brew.conv(model,
                      relu4,
                      "conv5",
                      256,
                      256,
                      3, ('XavierFill', {}), ('ConstantFill', {}),
                      pad=1)
    relu5 = brew.relu(model, conv5, "conv5")
    pool5 = brew.max_pool(model, relu5, "pool5", kernel=3, stride=2)
    fc6 = brew.fc(model, pool5, "fc6", 256 * 6 * 6, 4096, ('XavierFill', {}),
                  ('ConstantFill', {}))
    relu6 = brew.relu(model, fc6, "fc6")
    fc7 = brew.fc(model, relu6, "fc7", 4096, 4096, ('XavierFill', {}),
                  ('ConstantFill', {}))
    relu7 = brew.relu(model, fc7, "fc7")
    fc8 = brew.fc(model, relu7, "fc8", 4096, 1000, ('XavierFill', {}),
                  ('ConstantFill', {}))
    pred = brew.softmax(model, fc8, "pred")
    xent = model.LabelCrossEntropy([pred, "label"], "xent")
    if device != 'MKL':
        loss = model.AveragedLoss(xent, "loss")
    return model, 224
コード例 #58
0
def simple_cnn():
    model = ModelHelper(name="r", arg_scope={"order": "NCHW", "is_test": True})
    brew.conv(model, "data", 'conv1', 3, 16, kernel=3, stride=1)
    brew.spatial_bn(model, 'conv1', 'conv1_spatbn', 16, epsilon=1e-3)
    brew.relu(model, 'conv1_spatbn', 'relu1')
    return model, [(1, 3, 32, 32)]