Пример #1
0
 def style_net(train=True, learn_all=False, subset=None):
     if subset is None:
         subset = 'train' if train else 'test'
     source = '%s/labels.txt' % subset
     transform_param = dict(mirror=train,
                            crop_size=227,
                            mean_file=caffe_root +
                            'data/ilsvrc12/imagenet_mean.binaryproto')
     if (subset == 'train'):
         style_data, style_label = L.ImageData(
             transform_param=transform_param,
             source=source,
             batch_size=NUM_STYLE_IMAGES,
             new_height=256,
             new_width=256,
             ntop=2)
     else:
         style_data, style_label = L.ImageData(
             transform_param=transform_param,
             source=source,
             batch_size=NUM_TEST_IMAGES,
             new_height=256,
             new_width=256,
             ntop=2)
     return caffenet(data=style_data,
                     label=style_label,
                     train=train,
                     num_classes=NUM_STYLE_LABELS,
                     classifier_name='fc8_muic17',
                     learn_all=learn_all)
Пример #2
0
    def compile_time_operation(self, learning_option, cluster):
        """
        define input placeholder for JPEG input.
        """

        # get attr from learning option
        option = learning_option.get('option') #TODO: only consider training
        file_format = learning_option.get("file_format")
        data_path = learning_option.get('data_path')
        label_path = learning_option.get('label_path')
        batch_size = learning_option.get('batch_size')
        iteration = learning_option.get('iteration')

        # get attr
        # required field
        image_size = self.get_attr('image_size', default=None)
        if image_size is None:
            raise Exception('[DLMDL ERROR]: {0} in {1} layer must be declared.'.format('image_size', self.name))
        num_class= self.get_attr('num_class', default=None)
        if num_class is None:
            raise Exception('[DLMDL ERROR]: {0} in {1} layer must be declared.'.format('num_classes', self.name))

        # optional field
        shuffle = self.get_attr('shuffle', default=False)

        # Image Data layer setting
        image, label = L.ImageData(name=self.name,
                                   source=label_path.split('.')[0] + '_caffelist.txt',
                                   batch_size=batch_size, include=dict(phase=caffe.TRAIN), ntop=2, root_folder=data_path,
                                   new_height=image_size[1], new_width=image_size[0], shuffle=shuffle)

        test_data_path = learning_option.get('test_data_path')
        test_label_path = learning_option.get('test_label_path')
        test_batch_size = learning_option.get('test_batch_size')
        test_iteration = learning_option.get("test_iteration")
        test_interval = learning_option.get("test_interval")

        # Image Data layer for test dataset
        if test_data_path is not None:
            # Test image data layer setting
            temp_image, temp_label = L.ImageData(name=self.name,
                                                 source=test_label_path.split('.')[
                                                     0] + '_caffelist.txt',
                                                 batch_size=test_batch_size, include=dict(phase=caffe.TEST), ntop=2,
                                                 root_folder=test_data_path, new_height=image_size[0],
                                                 new_width=image_size[1])
            setattr(tempNet, str(self.name) + '.image', temp_image)
            setattr(tempNet, str(self.name) + '.label', temp_label)

        image_size = [batch_size, 3, image_size[0], image_size[1]]

        # Record the layer output information
        self.set_output('image', image)
        self.set_output('label', label)
        self.set_dimension('image', image_size)

        # required value set to learning_option
        learning_option['max_iter'] = iteration
        learning_option['test_iter'] = test_iteration
        learning_option['test_interval'] = test_interval
Пример #3
0
def make_net_test(desired_level=2):
    net = caffe.NetSpec()
    net.img0_nomean_resize= L.ImageData(image_data_param=dict(source="tmp/img1.txt",batch_size=1))
    net.img1_nomean_resize= L.ImageData(image_data_param=dict(source="tmp/img2.txt",batch_size=1))
    
    predict_flow_name = 'predict_flow{}'.format(desired_level)

    net = make_pwc_net_encoder_plus(net, net.img0_nomean_resize, net.img1_nomean_resize) 
    net.blob44 = L.Eltwise(getattr(net, predict_flow_name), eltwise_param=dict(operation=1, coeff=20.0))  

    return net.to_proto()
def style_net(train=True, learn_all=False, subset=None):
    if subset is None:
        subset = 'train' if train else 'test'
    source = caffe_root + 'data/flickr_style/%s.txt' % subset
    transform_param = dict(mirror=train,
                           crop_size=227,
                           mean_file=caffe_root +
                           'data/ilsvrc12/imagenet_mean.binaryproto')
    style_data, style_label = L.ImageData(transform_param=transform_param,
                                          source=source,
                                          batch_size=50,
                                          new_height=256,
                                          new_width=256,
                                          ntop=2)

    # HDF5 data source
    style_data, style_label = L.HDF5Data(source=hdf5DataFolderPath +
                                         'train_h5_list.txt',
                                         batch_size=50,
                                         ntop=2)

    # test
    print type(style_label)
    return caffenet(data=style_data,
                    label=style_label,
                    train=train,
                    num_classes=NUM_STYLE_LABELS,
                    classifier_name='fc8_flickr',
                    learn_all=learn_all)
Пример #5
0
def get_train_prototxt_deephi(caffe_path, src_prototxt, train_prototxt, image_list, directory_path):
    sys.path.insert(0, caffe_path)
    import caffe
    from caffe import layers as L
    from caffe import params as P
    from caffe.proto import caffe_pb2
    import google.protobuf.text_format as tfmt

    net_shape = []
    net_parameter = caffe.proto.caffe_pb2.NetParameter()
    with open(src_prototxt, "r") as f:
        tfmt.Merge(f.read(), net_parameter)
    net_shape = net_parameter.layer[0].input_param.shape[0].dim

    print(net_shape[2], net_shape[3] )
    n = caffe.NetSpec()
    print(type(n))
    n.data = L.ImageData(top='label', include=dict(phase=caffe_pb2.Phase.Value('TRAIN')), transform_param=dict(mirror=False,mean_value=128.0),
                                      image_data_param=dict(source=image_list,batch_size=50, new_height=net_shape[2],new_width=net_shape[3],shuffle=False,root_folder=directory_path))
    with open(train_prototxt, 'w') as f:
        f.write(str(n.to_proto()))
        print(n.to_proto())

    net_parameter = caffe.proto.caffe_pb2.NetParameter()
    with open(src_prototxt, "r") as f, open(train_prototxt, "a") as g:
        tfmt.Merge(f.read(), net_parameter)
        print("before\n", (net_parameter))
        #L = next(L for L in net_parameter.layer if L.name == 'data')

        print(net_parameter.layer[0])
        print(net_parameter.layer[0].input_param.shape[0].dim)

        del net_parameter.layer[0]
        print("after\n", (net_parameter))
        g.write(tfmt.MessageToString(net_parameter))
Пример #6
0
def CreateDataLayer_IMG(source,
                        batch_size=28,
                        shuffle=True,
                        new_width=224,
                        new_height=224,
                        is_color=True,
                        train=True,
                        transform_param={}):
    if train:
        kwargs = {
            'include': dict(phase=caffe_pb2.Phase.Value('TRAIN')),
            'transform_param': transform_param,
        }
    else:
        kwargs = {
            'include': dict(phase=caffe_pb2.Phase.Value('TEST')),
            'transform_param': transform_param,
        }
    return L.ImageData(name="data",
                       image_data_param=dict(source=source,
                                             shuffle=shuffle,
                                             new_width=new_width,
                                             new_height=new_height,
                                             is_color=is_color,
                                             batch_size=batch_size),
                       ntop=2,
                       **kwargs)
Пример #7
0
def create_data_layer(split="train", from_lmdb=True):
    if split == "train":
        shuffle = True
        batch_size = 32
        mirror = True
    else:
        shuffle = False
        batch_size = 8
        mirror = False
    transform_param = dict(crop_size=227,
                           mean_file="modeldef/mean.binaryproto",
                           mirror=mirror)
    if from_lmdb:
        source = "lmdb/" + split + "_lmdb"
        data, label = L.Data(data_param=dict(source=source,
                                             batch_size=batch_size,
                                             backend=P.Data.LMDB),
                             ntop=2,
                             transform_param=transform_param)
    else:
        source = "util/" + split + ".txt"
        data, label = L.ImageData(image_data_param=dict(
            source=source,
            root_folder=root_folder,
            batch_size=batch_size,
            shuffle=shuffle,
            new_width=256,
            new_height=256),
                                  ntop=2,
                                  transform_param=transform_param)
    return data, label
Пример #8
0
def caffenet(batch_size=256, include_acc=False, train = True, learn_all=True):

    subset = 'train' if train else 'test'
    source = caffe_root + 'data/flickr_style/%s.txt' % subset
    transform_param = dict(mirror=train, crop_size=227,
        mean_file=caffe_root + 'data/ilsvrc12/imagenet_mean.binaryproto')
    
    n=caffe.NetSpec()
    n.style_data, n.style_label = L.ImageData(
        transform_param=transform_param, source=source,
        batch_size=50, new_height=256, new_width=256, ntop=2)

    param = learned_param if learn_all else frozen_param

    # the net itself
    n.conv1, n.relu1 = conv_relu(n.style_data, 11, 96, stride=4, param=param)
    n.pool1 = max_pool(n.relu1, 3, stride=2)
    n.norm1 = L.LRN(n.pool1, local_size=5, alpha=1e-4, beta=0.75)
    n.conv2, n.relu2 = conv_relu(n.norm1, 5, 256, pad=2, group=2, param=param)
    n.pool2 = max_pool(n.relu2, 3, stride=2)
    n.norm2 = L.LRN(n.pool2, local_size=5, alpha=1e-4, beta=0.75)
    n.conv3, n.relu3 = conv_relu(n.norm2, 3, 384, pad=1, param=param)
    n.conv4, n.relu4 = conv_relu(n.relu3, 3, 384, pad=1, group=2, param=param)
    n.conv5, n.relu5 = conv_relu(n.relu4, 3, 256, pad=1, group=2, param=param)
    n.pool5 = max_pool(n.relu5, 3, stride=2)
    n.fc6, n.relu6 = fc_relu(n.pool5, 4096, param=param)
    n.drop6 = L.Dropout(n.relu6, in_place=True)
    n.fc7, n.relu7 = fc_relu(n.drop6, 4096, param=param)
    n.drop7 = L.Dropout(n.relu7, in_place=True)
    n.fc8_style = L.InnerProduct(n.drop7, num_output=CLASS_NUM, param=learned_param)
    n.loss = L.SoftmaxWithLoss(n.fc8_style, n.style_label)

    n.acc = L.Accuracy(n.fc8_style, n.style_label)
    return n.to_proto()
Пример #9
0
def Lenet(img_list, batch_size, include_acc=False):
    # 第一层,数据输入层,以ImageData格式输入
    data, label = L.ImageData(source=img_list, batch_size=batch_size, ntop=2, root_folder=root,
                              transform_param=dict(scale=0.00390625))
    # 第二层:卷积层
    conv1 = L.Convolution(data, kernel_size=5, stride=1, num_output=20, pad=0, weight_filler=dict(type='xavier'))
    # 池化层
    pool1 = L.Pooling(conv1, pool=P.Pooling.MAX, kernel_size=2, stride=2)
    # 卷积层
    conv2 = L.Convolution(pool1, kernel_size=5, stride=1, num_output=50, pad=0, weight_filler=dict(type='xavier'))
    # 池化层
    pool2 = L.Pooling(conv2, pool=P.Pooling.MAX, kernel_size=2, stride=2)
    # 全连接层
    fc3 = L.InnerProduct(pool2, num_output=500, weight_filler=dict(type='xavier'))
    # 激活函数层
    relu3 = L.ReLU(fc3, in_place=True)
    # 全连接层
    fc4 = L.InnerProduct(relu3, num_output=10, weight_filler=dict(type='xavier'))
    # softmax层
    loss = L.SoftmaxWithLoss(fc4, label)

    if include_acc:  # test阶段需要有accuracy层
        acc = L.Accuracy(fc4, label)
        return to_proto(loss, acc)
    else:
        return to_proto(loss)
Пример #10
0
def lenet_imagedata(source, batch_size):
    n = caffe.NetSpec()

    n.data, n.label = L.ImageData(
        batch_size=batch_size,
        source=source,
        transform_param=dict(scale=1. / 255),
        shuffle=True,  # Don't omit this pllllllllllllllllllllllllllz!!!!
        ntop=2,
        is_color=False)
    n.conv1 = L.Convolution(n.data,
                            kernel_size=5,
                            num_output=20,
                            weight_filler=dict(type='xavier'))
    n.pool1 = L.Pooling(n.conv1, kernel_size=2, stride=2, pool=P.Pooling.MAX)
    n.conv2 = L.Convolution(n.pool1,
                            kernel_size=5,
                            num_output=50,
                            weight_filler=dict(type='xavier'))
    n.pool2 = L.Pooling(n.conv2, kernel_size=2, stride=2, pool=P.Pooling.MAX)
    n.fc1 = L.InnerProduct(n.pool2,
                           num_output=500,
                           weight_filler=dict(type='xavier'))
    n.relu1 = L.ReLU(n.fc1, in_place=True)
    n.score = L.InnerProduct(n.relu1,
                             num_output=10,
                             weight_filler=dict(type='xavier'))
    n.loss = L.SoftmaxWithLoss(n.score, n.label)
    n.acc = L.Accuracy(n.score, n.label)

    return n.to_proto()
def create_landmark_net(train_list, batch_size):
    spec = caffe.NetSpec()
    spec.data, spec.label = L.ImageData(source=train_list, batch_size=batch_size, shuffle=True, ntop=2,
                           transform_param=dict(crop_size=112, mirror=False, scale=0.0078125, mean_value=127.5), include=dict(phase=caffe.TRAIN))
    spec.conv0  = data_block(spec.data,      kernel_size = 3, num_output = 8,  stride = 2, pad = 1)#采样
    spec.conv1  = mobile2_block(spec.conv0,  kernel_size = 3, num_middle = 48,  num_output = 8,  stride = 2, pad = 1)#采样
    spec.conv2  = shuffle_block(spec.conv1,  kernel_size = 3, num_middle = 48,  num_output = 8,  stride = 1, pad = 1)
    spec.conv3  = mobile2_block(spec.conv2,  kernel_size = 3, num_middle = 48,  num_output = 8,  stride = 2, pad = 1)#采样
    spec.conv4  = shuffle_block(spec.conv3,  kernel_size = 3, num_middle = 48,  num_output = 8,  stride = 1, pad = 1)
    spec.conv5  = shuffle_block(spec.conv4,  kernel_size = 3, num_middle = 48,  num_output = 8,  stride = 1, pad = 1)
    spec.conv6  = mobile2_block(spec.conv5,  kernel_size = 3, num_middle = 48,  num_output = 16, stride = 2, pad = 1)#采样
    spec.conv7  = shuffle_block(spec.conv6,  kernel_size = 3, num_middle = 96,  num_output = 16, stride = 1, pad = 1)
    spec.conv8  = shuffle_block(spec.conv7,  kernel_size = 3, num_middle = 96,  num_output = 16, stride = 1, pad = 1)
    spec.conv9  = shuffle_block(spec.conv8,  kernel_size = 3, num_middle = 96,  num_output = 16, stride = 1, pad = 1)
    spec.conv10 = mobile2_block(spec.conv9,  kernel_size = 3, num_middle = 96,  num_output = 24, stride = 1, pad = 1)
    spec.conv11 = shuffle_block(spec.conv10, kernel_size = 3, num_middle = 144, num_output = 24, stride = 1, pad = 1)
    spec.conv12 = shuffle_block(spec.conv11, kernel_size = 3, num_middle = 144, num_output = 24, stride = 1, pad = 1)
    spec.conv13 = mobile2_block(spec.conv12, kernel_size = 3, num_middle = 144, num_output = 40, stride = 2, pad = 1)#采样
    spec.conv14 = shuffle_block(spec.conv13, kernel_size = 3, num_middle = 240, num_output = 40, stride = 1, pad = 1)
    spec.conv15 = shuffle_block(spec.conv14, kernel_size = 3, num_middle = 240, num_output = 40, stride = 1, pad = 1)
    spec.conv16 = mobile2_block(spec.conv15, kernel_size = 3, num_middle = 240, num_output = 80, stride = 1, pad = 1)

    spec.conv17 = L.Convolution(spec.conv16, kernel_size = 1, stride = 1, num_output = 1280, bias_term = False, pad = 0, weight_filler = dict(type = 'xavier'))
    spec.conv18 = L.Convolution(spec.conv17, kernel_size = 1, stride = 1, num_output = 64,   bias_term = False, pad = 0, weight_filler = dict(type = 'xavier'))
    
    spec.fc0  = L.InnerProduct(spec.conv18, num_output = 212, weight_filler=dict(type='xavier'))

    spec.loss = L.EuclideanLoss(spec.fc0, spec.label, include=dict(phase=caffe.TRAIN))

    return spec.to_proto()
Пример #12
0
    def train_head(self, subset):
        n = NetSpec()
        # train
        image_data_param = dict(source=subset.get_list_absolute_path(),
                                batch_size=self.batch_sizes[0],
                                new_width=self.infmt.new_width,
                                new_height=self.infmt.new_height,
                                rand_skip=self.batch_size,
                                shuffle=True)

        if subset.root_folder is not None:
            image_data_param['root_folder'] = subset.root_folder

        transform_param = dict(
            mirror=self.infmt.mirror,
            crop_size=self.infmt.crop_size,
            # mean_value = self.infmt.mean_pixel,
        )

        if self.infmt.scale is not None:
            transform_param['scale'] = self.infmt.scale

        if self.infmt.mean_file is not None:
            transform_param['mean_file'] = self.infmt.mean_file
        elif self.infmt.mean_pixel is not None:
            transform_param['mean_value'] = self.infmt.mean_pixel

        n.data, n.label = L.ImageData(ntop=2,
                                      image_data_param=image_data_param,
                                      transform_param=transform_param,
                                      include=dict(phase=caffe.TRAIN))
        net = n.to_proto()

        net.name = self.name
        return net
def zero1_net(source, batch_size):

    n = caffe.NetSpec()

    n.data, n.label = L.ImageData(source=source, batch_size=batch_size, ntop=2)

    n.conv1 = L.Convolution(n.data,
                            kernel_size=3,
                            num_output=20,
                            weight_filler=dict(type='xavier'))
    n.pool1 = L.Pooling(n.conv1, kernel_size=2, stride=2, pool=P.Pooling.MAX)

    n.conv2 = L.Convolution(n.pool1,
                            kernel_size=3,
                            num_output=50,
                            weight_filler=dict(type='xavier'))
    n.pool2 = L.Pooling(n.conv2, kernel_size=2, stride=2, pool=P.Pooling.MAX)

    n.fc1 = L.InnerProduct(n.pool2,
                           num_output=50,
                           weight_filler=dict(type='xavier'))

    n.prob = L.InnerProduct(n.fc1,
                            num_output=10,
                            weight_filler=dict(type='xavier'))
    n.loss = L.SoftmaxWithLoss(n.prob, n.label)

    return n.to_proto()
Пример #14
0
def style_net(train=True, learn_all=False, subset=None):
    if subset is None:
        subset = 'train' if train else 'test'

    # 'data/flickr_style/train.txt'存储内容:/home/momo/ZZ/caffe/data/flickr_style/images/10344996196_1117743cfe.jpg 0
    # /home/momo/ZZ/caffe/data/flickr_style/images/10344996196_1117743cfe.jpg是style_data, 0是style_label
    # L.ImagData()中的source直接输入图,不是lmdb文件
    source = caffe_root + 'data/flickr_style/%s.txt' % subset

    # mirror:是否对输入数据采取随机水平镜像,增加训练数据
    # crop_size:尺寸超过277就会进行裁剪
    # mean_file: 数据均值文件存放
    transform_param = dict(mirror=train,
                           crop_size=227,
                           mean_file=caffe_root +
                           'data/ilsvrc12/imagenet_mean.binaryproto')

    style_data, style_label = L.ImageData(transform_param=transform_param,
                                          source=source,
                                          batch_size=50,
                                          new_height=256,
                                          new_width=256,
                                          ntop=2)
    return caffenet(data=style_data,
                    label=style_label,
                    train=train,
                    num_classes=NUM_STYLE_LABELS,
                    classifier_name='fc8_flickr',
                    learn_all=learn_all)
Пример #15
0
def main():
    # Pretrained models on ImageNet
    weights = './mobilenet.caffemodel'
    assert os.path.exists(weights)

    # deploy
    deploy_data = L.Input(input_param=dict(shape=dict(dim=[1, 3, 224, 224])))
    deploy_proto = './deploy.prototxt'
    with open(deploy_proto, 'w') as f:
        f.write(str(mobilenet(data=deploy_data, num_classes=9)))

    # train
    train_source = './data/train_shuffled.txt'
    train_transform_param = dict(scale=0.017,
                                 mirror=True,
                                 crop_size=224,
                                 mean_value=[103.94, 116.78, 123.68])
    train_data, train_label = L.ImageData(
        transform_param=train_transform_param,
        source=train_source,
        root_folder="./data/",
        batch_size=16,
        new_height=256,
        new_width=256,
        ntop=2)
    train_proto = './train.prototxt'
    with open(train_proto, 'w') as f:
        f.write(
            str(mobilenet(data=train_data, label=train_label, num_classes=9)))

    # test
    test_source = './data/test_shuffled.txt'
    test_transform_param = dict(scale=0.017,
                                mirror=False,
                                crop_size=224,
                                mean_value=[103.94, 116.78, 123.68])
    test_data, test_label = L.ImageData(transform_param=test_transform_param,
                                        source=test_source,
                                        root_folder="./data/",
                                        batch_size=2,
                                        new_height=256,
                                        new_width=256,
                                        ntop=2)
    test_proto = './test.prototxt'
    with open(test_proto, 'w') as f:
        f.write(str(mobilenet(data=test_data, label=test_label,
                              num_classes=9)))
Пример #16
0
def conv1_autoencoder(split, batch_sz):
    n = caffe.NetSpec()
    n.data, n.label = L.ImageData(image_data_param=dict(source=split,
                                                        batch_size=batch_sz,
                                                        new_height=height,
                                                        new_width=width,
                                                        is_color=False),
                                  ntop=2)
    n.silence = L.Silence(n.label, ntop=0)
    n.flatdata_i = L.Flatten(n.data)

    n.conv1 = conv(n.data, 5, 5, 64, pad=2)
    n.bn1 = L.BatchNorm(n.conv1,
                        use_global_stats=False,
                        in_place=True,
                        param=[{
                            "lr_mult": 0
                        }, {
                            "lr_mult": 0
                        }, {
                            "lr_mult": 0
                        }])
    n.scale1 = L.Scale(n.bn1, bias_term=True, in_place=True)
    n.relu1 = L.ReLU(n.scale1, relu_param=dict(negative_slope=0.1))
    n.pool1 = max_pool(n.relu1, 2, stride=2)

    n.code = conv(n.pool1, 5, 5, 64, pad=2)

    n.upsample1 = L.Deconvolution(n.code,
                                  param=dict(lr_mult=0, decay_mult=0),
                                  convolution_param=dict(
                                      group=64,
                                      num_output=64,
                                      kernel_size=4,
                                      stride=2,
                                      pad=1,
                                      bias_term=False,
                                      weight_filler=dict(type="bilinear")))
    n.deconv1 = conv(n.upsample1, 5, 5, 1, pad=2)
    n.debn1 = L.BatchNorm(n.deconv1,
                          use_global_stats=False,
                          in_place=True,
                          param=[{
                              "lr_mult": 0
                          }, {
                              "lr_mult": 0
                          }, {
                              "lr_mult": 0
                          }])
    n.descale1 = L.Scale(n.debn1, bias_term=True, in_place=True)
    n.derelu1 = L.ReLU(n.descale1, relu_param=dict(negative_slope=0.1))

    n.flatdata_o = L.Flatten(n.derelu1)
    n.loss_s = L.SigmoidCrossEntropyLoss(n.flatdata_o,
                                         n.flatdata_i,
                                         loss_weight=1)
    n.loss_e = L.EuclideanLoss(n.flatdata_o, n.flatdata_i, loss_weight=0)

    return str(n.to_proto())
def generate_data(method):
    if method == "train_test":
        net.data, net.label = Layers.ImageData(
            name="cifar",
            ntop=2,
            transform_param=dict(
                mirror=True,
                crop_size=32,
                mean_value=[104, 117, 123],
            ),
            image_data_param=dict(
                source="/home/cengsl14/dataset/cifar10_train_list.txt",
                batch_size=128,
                shuffle=True,
                is_color=True,
                root_folder="/home/cengsl14/dataset/cifar10_train/",
            ),
            include=dict(phase=caffe.TRAIN, ),
        )
        # add repetive train part
        train_part = str(net.to_proto())
        ## test part
        net.data, net.label = Layers.ImageData(
            name="cifar",
            ntop=2,
            transform_param=dict(
                mirror=False,
                mean_value=[104, 117, 123],
            ),
            image_data_param=dict(
                source="/home/cengsl14/dataset/cifar10_test_list.txt",
                batch_size=32,
                shuffle=False,
                is_color=True,
                mirror=False,
                root_folder="/home/cengsl14/dataset/cifar10_test/",
            ),
            include=dict(phase=caffe.TEST),
        )
        return train_part
    elif method == "deploy":
        net.tops["data"] = Layers.Input(
            name="cifar", input_param=dict(shape=dict(dim=[1, 3, 32, 32])))
        return ""
    else:
        raise NotImplementedError
Пример #18
0
def net(img_list, batch_size, mean_value=0):
    n = caffe.NetSpec()
    n.data, n.label=L.ImageData(source=img_list,batch_size=batch_size,new_width=28,new_height=28,ntop=2,transform_param=dict(scale = 1/255.0, mean_value=mean_value))
    n.ip1 = L.InnerProduct(n.data, num_output=50, weight_filler=dict(type='xavier'))
    n.relu1 = L.ReLU(n.ip1, in_place=True)
    n.ip2 = L.InnerProduct(n.relu1, num_output=10, weight_filler=dict(type='xavier'))
    n.loss = L.SoftmaxWithLoss(n.ip2, n.label)
    return n.to_proto()
Пример #19
0
def resnet18_fer2013(source, batch_size):
    n = caffe.NetSpec()
    #n.data, n.label = L.ImageData(image_data_param=dict(source='tmp' , batch_size=100),
    #                               ntop=2, transform_param=dict(mean_file='tmp'))
    #n.data, n.label = Custom_Data_Layer(src_file=, batch_size=64, im_shape=(64, 64), ntop=2) # 64 x 64

    n.data, n.label = L.ImageData(batch_size=batch_size,
                            source=source,
                            transform_param=dict(scale=1./255),
                            shuffle=True, # Don't omit this pllllllllllllllllllllllllllz!!!!
                            new_width=64,
                            new_height=64,
                            ntop=2,
                            is_color=True)


    output_size = 64
    n.conv_layer = conv_factory(n.data, ks=3, nout=output_size, stride=1, pad=1)
    #n.pool = max_pool(n.conv_layer, ks=3, stride=2)

    n.part1 = part_block(n.conv_layer, nout=output_size, stride=1)
    n.layer1 = layer_block(n.conv_layer, nout=output_size, upper_stride=1) # 64
    n.concat1 = concat_block(n.part1, n.layer1)

    n.layer2 = layer_block(n.concat1, nout=output_size, upper_stride=1) # 64
    n.concat2 = concat_block(n.concat1, n.layer2)

    output_size *= 2
    n.part2 = part_block(n.concat2, nout=output_size)
    n.layer3 = layer_block(n.concat2, nout=output_size, upper_stride=2) # 128
    n.concat3 = concat_block(n.part2, n.layer3)

    n.layer4 = layer_block(n.concat3, nout=output_size, upper_stride=1) # 128
    n.concat4 = concat_block(n.concat3, n.layer4)

    output_size *= 2
    n.part3 = part_block(n.concat4, nout=output_size)
    n.layer5 = layer_block(n.concat4, nout=output_size, upper_stride=2) # 256
    n.concat5 = concat_block(n.part3, n.layer5)

    n.layer6 = layer_block(n.concat5, nout=output_size, upper_stride=1) # 256
    n.concat6 = concat_block(n.concat5, n.layer6)

    output_size *= 2
    n.part4 = part_block(n.concat6, nout=output_size)
    n.layer7 = layer_block(n.concat6, nout=output_size, upper_stride=2) # 512
    n.concat7 = concat_block(n.part4, n.layer7)

    n.layer8 = layer_block(n.concat7, nout=output_size, upper_stride=1) # 512
    n.concat8 = concat_block(n.concat7, n.layer8)

    n.pool2 = L.Pooling(n.concat8, pool=P.Pooling.AVE, global_pooling=True)
    n.fc = L.InnerProduct(n.pool2, num_output=7, weight_filler=dict(type='xavier'))

    n.loss = L.SoftmaxWithLoss(n.fc, n.label)
    n.acc = L.Accuracy(n.fc, n.label, include=dict(phase=getattr(caffe_pb2, 'TEST'))) # same as caffe.TEST

    return n.to_proto()
Пример #20
0
def test_eltwise_relu_lego():
    from lego.hybrid import EltwiseReLULego
    n = caffe.NetSpec()
    n.data, n.label = L.ImageData(image_data_param=dict(source='tmp',
                                                        batch_size=100),
                                  ntop=2,
                                  transform_param=dict(mean_file='tmp'))
    EltwiseReLULego(dict(name='1')).attach(n, [n.data, n.label])
    assert n['eltwise_1'] is not None
Пример #21
0
def miniplaces_net(source, train=False, with_labels=True):
    mean = [104, 117, 123]  # per-channel mean of the BGR image pixels
    transform_param = dict(mirror=train, crop_size=args.crop, mean_value=mean)
    batch_size = args.batch if train else 100
    places_data, places_labels = L.ImageData(transform_param=transform_param,
        source=source, root_folder=args.image_root, shuffle=train,
        batch_size=batch_size, ntop=2)
    return minialexnet(data=places_data, labels=places_labels, train=train,
                       with_labels=with_labels)
Пример #22
0
def test_relu():
    from lego.base import BaseLegoFunction
    n = caffe.NetSpec()
    n.data, n.label = L.ImageData(image_data_param=dict(source='tmp',
                                                        batch_size=100),
                                  ntop=2,
                                  transform_param=dict(mean_file='tmp'))
    BaseLegoFunction('ReLU', dict(name='relu1')).attach(n, [n.data])
    assert n['relu1'] is not None
Пример #23
0
def get_data_layer(source, mirror):
    transform_param = dict(mirror=mirror, crop_size=227)
    species_data, species_label = L.ImageData(transform_param=transform_param,
                                              source=source,
                                              batch_size=50,
                                              new_height=256,
                                              new_width=256,
                                              ntop=2)
    return species_data, species_label
Пример #24
0
def test_fire_lego():
    from lego.hybrid import FireLego
    n = caffe.NetSpec()
    n.data, n.label = L.ImageData(image_data_param=dict(source='tmp',
                                                        batch_size=100),
                                  ntop=2,
                                  transform_param=dict(mean_file='tmp'))
    params = dict(name='fire1', squeeze_num_output=16, use_global_stats=True)
    FireLego(params).attach(n, [n.data])
Пример #25
0
def ImageDataLayer(source_, batch_size_, root_folder_):
    kwargs = {
        'image_data_param':
        dict(source=source_,
             batch_size=batch_size_,
             root_folder=root_folder_,
             shuffle=True)
    }
    data, label = L.ImageData(ntop=2, **kwargs)
    return [data, label]
Пример #26
0
def test_prediction_lego():
    from lego.ssd import PredictionLego
    n = caffe.NetSpec()
    n.data, n.label = L.ImageData(image_data_param=dict(source='tmp',
                                                        batch_size=100),
                                  ntop=2,
                                  transform_param=dict(mean_file='tmp'))
    params = dict(name='1', num_output=16, stride=1)
    lego = PredictionLego(params)
    lego.attach(n, [n.data])
Пример #27
0
def test_inception_v1_lego():
    from lego.hybrid import InceptionV1Lego
    n = caffe.NetSpec()
    n.data, n.label = L.ImageData(image_data_param=dict(source='tmp',
                                                        batch_size=100),
                                  ntop=2,
                                  transform_param=dict(mean_file='tmp'))
    params = dict(name='inception1',
                  num_outputs=[16, 96, 128, 16, 32, 32],
                  use_global_stats=True)
    InceptionV1Lego(params).attach(n, [n.data])
def srcnn_net(source, target=None, train=False, with_labels=True):
    transform_param = dict(scale=0.0039215684)
    batch_size = args.batch if train else 100
    source_images, source_labels = L.ImageData(transform_param=transform_param,
                                               source=source,
                                               root_folder=args.image_root,
                                               batch_size=batch_size,
                                               ntop=2)
    target_images = None
    if target:
        target_images, target_labels = L.ImageData(
            transform_param=transform_param,
            source=target,
            root_folder=args.image_root,
            batch_size=batch_size,
            ntop=2)
    return srcnn(data=source_images,
                 labels=target_images,
                 train=train,
                 with_labels=(target != None))
Пример #29
0
def fcn():
    n = caffe.NetSpec()

    n.data = L.ImageData()
    n.label = L.ImageData()
    # the base net
    n.conv1_1, n.relu1_1 = conv_relu(n.data, 64, pad=1)
    n.conv1_2, n.relu1_2 = conv_relu(n.relu1_1, 64)
    n.pool1 = max_pool(n.relu1_2)

    n.conv2_1, n.relu2_1 = conv_relu(n.pool1, 128)
    n.conv2_2, n.relu2_2 = conv_relu(n.relu2_1, 128)
    n.pool2 = max_pool(n.relu2_2)

    n.conv3_1, n.relu3_1 = conv_relu(n.pool2, 256)
    n.conv3_2, n.relu3_2 = conv_relu(n.relu3_1, 256)
    n.conv3_3, n.relu3_3 = conv_relu(n.relu3_2, 256)
    n.pool3 = max_pool(n.relu3_3)

    n.conv4_1, n.relu4_1 = conv_relu(n.pool3, 512)
    n.conv4_2, n.relu4_2 = conv_relu(n.relu4_1, 512)
    n.conv4_3, n.relu4_3 = conv_relu(n.relu4_2, 512)
    n.pool4 = max_pool(n.relu4_3)

    n.conv5_1, n.relu5_1 = conv_relu(n.pool4, 512)
    n.conv5_2, n.relu5_2 = conv_relu(n.relu5_1, 512)
    n.conv5_3, n.relu5_3 = conv_relu(n.relu5_2, 512)
    n.pool5 = max_pool(n.relu5_3)

    # fully conv
    n.fc6, n.relu6 = fc_relu(n.pool5, 4096)
    n.drop6 = L.Dropout(n.relu6, dropout_ratio=0.5, in_place=True)
    n.fc7, n.relu7 = fc_relu(n.drop6, 4096)
    n.drop7 = L.Dropout(n.relu7, dropout_ratio=0.5, in_place=True)

    n.fc8_minc = L.InnerProduct(n.drop7, num_output=23, weight_filler=dict(type='gaussian',std=0.01))

    n.loss = L.SoftmaxWithLoss(n.fc8_minc, n.label,
            loss_param=dict(normalize=False))

    return n.to_proto()
Пример #30
0
    def val_head(self, subset, stage=None):

        image_data_param = dict(
            source=subset.get_list_absolute_path(),
            batch_size=self.batch_sizes[1],
            # root_folder=subset.root_folder,
            rand_skip=self.batch_sizes[1],
            shuffle=True,
            # new_width,
            # new_height
        )

        transform_param = dict(
            mirror=False,
            # crop_size = self.infmt.crop_size,
            # mean_value = self.infmt.mean_pixel,
            # mean_file,
            # scale,
        )

        if subset.root_folder is not None:
            image_data_param['root_folder'] = subset.root_folder

        if self.crop_on_test:
            image_data_param['new_width'] = self.infmt.new_width
            image_data_param['new_height'] = self.infmt.new_height
            transform_param['crop_size'] = self.infmt.crop_size
        else:
            image_data_param['new_width'] = self.infmt.crop_size
            image_data_param['new_height'] = self.infmt.crop_size

        if self.infmt.scale is not None:
            transform_param['scale'] = self.infmt.scale

        if self.infmt.mean_file is not None:
            transform_param['mean_file'] = self.infmt.mean_file
        elif self.infmt.mean_pixel is not None:
            transform_param['mean_value'] = self.infmt.mean_pixel

        include_param = dict(phase=caffe.TEST)
        if stage is not None:
            include_param['stage'] = stage

        n = NetSpec()
        n.data, n.label = L.ImageData(ntop=2,
                                      image_data_param=image_data_param,
                                      transform_param=transform_param,
                                      include=include_param)

        net = n.to_proto()
        net.name = self.name
        return net