def blob_split(self, model_path, weight_path):
        """
            This function used to change layer which top name not equal layer name
        """
        import caffe
        from caffe.proto import caffe_pb2
        caffe_layer_type_list = []

        prototxt_net = caffe_pb2.NetParameter()
        try:
            for layer_type in caffe.layer_type_list():
                caffe_layer_type_list.append(layer_type)

            with open(model_path, 'r') as file_read:
                text_format.Merge(file_read.read(), prototxt_net)

            CaffeUtil.update_graph(prototxt_net)
            with open("new.prototxt", 'w') as file_read:
                file_read.write(text_format.MessageToString(prototxt_net))

            caffemodel_net = caffe.Net("new.prototxt", weight_path, caffe.TEST)
            os.remove("new.prototxt")
            for i in range(len(prototxt_net.layer)):
                if prototxt_net.layer[i].type not in caffe_layer_type_list:
                    print("Given caffe model is not supported")
                    return 0
            return 1, prototxt_net, caffemodel_net
        except IOError as except_error:
            print("Given caffe model is not supported: {es}".format(
                es=str(except_error)))
            return 0, None, None
Exemple #2
0
def main():
    """ script entry point """
    parser = argparse.ArgumentParser(description='Convert a YOLO cfg file.')
    parser.add_argument('model', type=str, help='YOLO cfg model')
    parser.add_argument('output', type=str, help='output prototxt')
    parser.add_argument('--loclayer', action='store_true',
                        help='use locally connected layer')
    parser.add_argument('--train', action='store_true',
                        help='generate train_val prototxt')
    args = parser.parse_args()

    config = load_configuration(args.model)
    caffe.set_mode_cpu()
    #layers_test = []
    #cl.Convolution(layers_test[-1], kernel_size=3)
    layer_type = caffe.layer_type_list()
    for temp in layer_type:
        if temp == "Reorg":
            print("{0} layer recognized".format(temp))
        elif temp == "convolutional":
            print("{0} layer recognized".format(temp))
    model = convert_configuration(config, args.train, args.loclayer)

    suffix = "train_val" if args.train else "deploy"
    model_filename = "{}_{}.prototxt".format(args.output, suffix)

    if args.loclayer:
        model = adjust_params(model, model_filename)

    with open(model_filename, 'w') as fproto:
        fproto.write("{0}".format(model.to_proto()))
Exemple #3
0
def local_layer(previous, name, params, train=False):
    """ create a locally connected layer given the parameters and previous
    layer """
    if 'LocalConvolution' not in caffe.layer_type_list():
        raise ValueError("Layer not available: LocalConvolution")

    fields = dict(num_output=int(params["filters"]),
                  kernel_size=int(params["size"]))
    if "stride" in params.keys():
        fields["stride"] = int(params["stride"])

    if int(params.get("pad", 0)) == 1:    # use 'same' strategy for convolutions
        fields["pad"] = fields["kernel_size"]//2
    if train:
        fields.update(weight_filler=dict(type="gaussian", std=0.01),
                      bias_filler=dict(type="constant", value=0))

    return cl.LocalConvolution(previous, name=name, **fields)
    with tempfile.NamedTemporaryFile(mode='w+', delete=False, suffix='.xml') as f:
        f.write(out_str)

        return f.name


def create_tasks_file(annotation_path, images_directory, height, width):
    template_str = '{} {},{} {}'.format(annotation_path, height, width, images_directory)

    with tempfile.NamedTemporaryFile(mode='w+', delete=False, suffix='.txt') as f:
        f.write(template_str)

        return f.name


@unittest.skipIf('Python' not in caffe.layer_type_list(), 'Caffe built without Python layer support')
class TestActionsDataLayer(unittest.TestCase):
    def setUp(self):
        pass

    def test_forward(self):
        num_images = 6
        num_tracks = 7
        batch_size = 3
        input_size = (20, 45)
        output_size = (11, 23)

        images_directory_path =\
            create_directory_with_images(height=input_size[0], width=input_size[1], num_images=num_images)
        annotation_path = create_annotation_file(num_images, num_tracks)
        tasks_path = create_tasks_file(annotation_path, images_directory_path, input_size[0], input_size[1])
def print_all_available_layers():
    layer_ist = caffe.layer_type_list()
    for el in layer_ist:
        print el
Exemple #6
0
        layer { type: 'Python' name: 'layer' bottom: 'data' top: 'top'
          python_param { module: 'test_python_layer' layer: 'ParameterLayer' } }
          """)
        return f.name


def phase_net_file():
    with tempfile.NamedTemporaryFile(mode='w+', delete=False) as f:
        f.write("""name: 'pythonnet' force_backward: true
        layer { type: 'Python' name: 'layer' top: 'phase'
          python_param { module: 'test_python_layer' layer: 'PhaseLayer' } }
          """)
        return f.name


@unittest.skipIf('Python' not in caffe.layer_type_list(),
                 'Caffe built without Python layer support')
class TestPythonLayer(unittest.TestCase):
    @classmethod
    def setUpClass(self):
        super(TestPythonLayer, self).setUpClass()
        caffe.set_device(0)
        print('TestPythonLayer.setUpClass')

    @classmethod
    def tearDownClass(self):
        super(TestPythonLayer, self).tearDownClass()
        print('TestPythonLayer.tearDownClass')

    def setUp(self):
        net_file = python_net_file()
Exemple #7
0
        input: 'data' input_shape { dim: 10 dim: 9 dim: 8 }
        layer { type: 'Python' name: 'layer' bottom: 'data' top: 'top'
          python_param { module: 'test_python_layer' layer: 'ParameterLayer' } }
          """)
        return f.name

def phase_net_file():
    with tempfile.NamedTemporaryFile(mode='w+', delete=False) as f:
        f.write("""name: 'pythonnet' force_backward: true
        layer { type: 'Python' name: 'layer' top: 'phase'
          python_param { module: 'test_python_layer' layer: 'PhaseLayer' } }
          """)
        return f.name


@unittest.skipIf('Python' not in caffe.layer_type_list(),
    'Caffe built without Python layer support')
class TestPythonLayer(unittest.TestCase):
    def setUp(self):
        net_file = python_net_file()
        self.net = caffe.Net(net_file, caffe.TRAIN)
        os.remove(net_file)

    def test_forward(self):
        x = 8
        self.net.blobs['data'].data[...] = x
        self.net.forward()
        for y in self.net.blobs['three'].data.flat:
            self.assertEqual(y, 10**3 * x)

    def test_backward(self):
 def test_standard_types(self):
     #removing 'Data' from list
     for type_name in ['Data', 'Convolution', 'InnerProduct']:
         self.assertIn(type_name, caffe.layer_type_list(),
                       '%s not in layer_type_list()' % type_name)
Exemple #9
0
class TestHWGQCaffeNet(unittest.TestCase):
    """Test HWGQ network import and streamlining using w1a2 HWGQ CaffeNet."""
    def setUp(self):
        self.nname = "caffenet-hwgq-w1a2"
        proto = config.FINN_ROOT + "/inputs/%s.prototxt" % self.nname
        weights = config.FINN_ROOT + "/inputs/%s.caffemodel" % self.nname
        # download weights if not already on disk
        weights_url = "http://www.svcl.ucsd.edu/projects/hwgq/AlexNet_HWGQ.caffemodel"
        if not os.path.exists(weights):
            print("Downloading HWGQ CaffeNet weights")
            urlretrieve(weights_url, weights)
        l = CaffeLoader(weights, proto)
        self.net = nn.NN(l)
        # use the first numImagesToTest of the test set for verification
        self.numImagesToTest = 10
        # expected number of successful predictions
        self.ok_golden = 7
        # expected number of unsuccessful predictions
        self.nok_golden = 3

    def test_import_correctness(self):
        (ok, nok) = testOnImageNet1kSubset(self.net, self.numImagesToTest)
        self.assertTrue(ok == self.ok_golden and nok == self.nok_golden)

    def test_streamline_correctness(self):
        streamlined_net = copy.deepcopy(self.net)
        streamlined_net.layers = transform.makeCromulent(
            streamlined_net.layers)
        (ok, nok) = testOnImageNet1kSubset(streamlined_net,
                                           self.numImagesToTest)
        self.assertTrue(ok == self.ok_golden and nok == self.nok_golden)

    def test_quantize_first_layer(self):
        # quantize first convolutional (float) layer to use 8-bit weights
        qnt_layers = transform.directlyQuantizeLayer(self.net.layers[0], 8)
        qnt_net = nn.NN(layers=transform.makeCromulent(qnt_layers +
                                                       self.net.layers[1:]))
        (ok, nok) = testOnImageNet1kSubset(qnt_net, self.numImagesToTest)
        self.assertTrue(ok == self.ok_golden and nok == self.nok_golden)

    def test_0_quantize_all_float_layers(self):
        qnt_net = transform.directlyQuantizeAllFloatWeights(self.net.layers, 8)
        qnt_net = nn.NN(layers=transform.makeCromulent(qnt_net))
        (ok, nok) = testOnImageNet1kSubset(qnt_net, self.numImagesToTest)
        self.assertTrue(ok == self.ok_golden and nok == self.nok_golden)

    def test_num_matrix_layers(self):
        self.assertIs(8, self.net.count_matrix_layers())

    @unittest.skipUnless("IntegerConvolution" in caffe.layer_type_list(),
                         "requires Caffe with bit-serial layer support")
    def test_cpu_backend(self):
        # build streamlined version
        streamlined_net = copy.deepcopy(self.net)
        streamlined_net.layers = transform.makeCromulent(
            streamlined_net.layers)
        # set up data layer
        lmdb_path = getImageNetVal_1ksubset_LMDB()
        # set up data transformations for CaffeNet
        tp = dict(crop_size=227, mean_value=[104, 117, 123], mirror=False)
        # set up Caffe data layer
        datal = caffe.layers.Data(source=lmdb_path,
                                  backend=caffe.params.Data.LMDB,
                                  batch_size=1,
                                  ntop=2,
                                  transform_param=tp)
        datas = [1, 3, 227, 227]
        # make a temp dir for generated Caffe model and weights
        tmpdirpath = tempfile.mkdtemp()
        # call CPU bit serial backend for synthesis
        cpu_backend.synthesize(streamlined_net.layers, tmpdirpath, datas,
                               datal, self.nname + "-")
        testproto_ok = os.path.isfile(tmpdirpath + "/" + self.nname +
                                      "-test.prototxt")
        deployproto_ok = os.path.isfile(tmpdirpath + "/" + self.nname +
                                        "-deploy.prototxt")
        weights_ok = os.path.isfile(tmpdirpath + "/" + self.nname +
                                    "-weights.caffemodel")
        # remove temp dir
        shutil.rmtree(tmpdirpath)
        # TODO can test the returned net to ensure correctness
        self.assertTrue(testproto_ok and deployproto_ok and weights_ok)
 def test_standard_types(self):
     #removing 'Data' from list 
     for type_name in ['Data', 'Convolution', 'InnerProduct']:
         self.assertIn(type_name, caffe.layer_type_list(),
                 '%s not in layer_type_list()' % type_name)
def print_layers():
    layer_lsit = caffe.layer_type_list()
    for layer in layer_lsit:
        print layer