示例#1
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
示例#2
0
def main():
	from argparse import ArgumentParser
	from os import path
	
	parser = ArgumentParser()
	parser.add_argument('prototxt')
	parser.add_argument('-l', '--load', help='Load a caffemodel')
	parser.add_argument('-d', '--data', default=None, help='Image list to use [default prototxt data]')
	#parser.add_argument('-q', action='store_true', help='Quiet execution')
	parser.add_argument('-sm', action='store_true', help='Summary only')
	parser.add_argument('-q', action='store_true', help='Quiet execution')
	parser.add_argument('-a', '--all', action='store_true', help='Show the statistic for all layers')
	parser.add_argument('-nc', action='store_true', help='Do not use color')
	parser.add_argument('-s', type=float, default=1.0, help='Scale the input [only custom data "-d"]')
	parser.add_argument('-bs', type=int, default=16, help='Batch size [only custom data "-d"]')
	parser.add_argument('-nit', type=int, default=10, help='Number of iterations')
	parser.add_argument('--gpu', type=int, default=0, help='What gpu to run it on?')
	args = parser.parse_args()
	
	if args.q:
		from os import environ
		environ['GLOG_minloglevel'] = '2'
	import caffe, load
	from caffe import NetSpec, layers as L
	
	caffe.set_mode_gpu()
	if args.gpu is not None:
		caffe.set_device(args.gpu)
	
	model = load.ProtoDesc(args.prototxt)
	net = NetSpec()
	if args.data is not None:
		fl = getFileList(args.data)
		if len(fl) == 0:
			print("Unknown data type for '%s'"%args.data)
			exit(1)
		from tempfile import NamedTemporaryFile
		f = NamedTemporaryFile('w')
		f.write('\n'.join([path.abspath(i)+' 0' for i in fl]))
		f.flush()
		net.data, net.label = L.ImageData(source=f.name, batch_size=args.bs, new_width=model.input_dim[-1], new_height=model.input_dim[-1], transform_param=dict(mean_value=[104,117,123], scale=args.s),ntop=2)
		net.out = model(data=net.data, label=net.label)
	else:
		net.out = model()
	
	n = netFromString('force_backward:true\n'+str(net.to_proto()), caffe.TRAIN )
	
	if args.load is not None:
		n.copy_from(args.load)
	
	cvar = printMeanStddev(n, NIT=args.nit, show_all=args.all, show_color=not args.nc, quiet=args.sm)
	cv, gr = computeGraidentRatio(n, NIT=args.nit)
	print()
	print('  Summary  ')
	print('-----------')
	print()
	print('layer name                         out cvar    rate cvar    rate mean')
	for l in n._layer_names:
		if l in cvar and l in cv and l in gr:
			print('%-30s   %10.2f   %10.2f   %10.2f'%(l, cvar[l], cv[l], gr[l]) )
示例#3
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
示例#4
0
文件: MyLeNet.py 项目: jeiks/VisCog
def lenet(lmdbData, lmdbLabel, batch_size):
    n = NetSpec()
    
    n.data  = L.Data(batch_size=batch_size, backend=P.Data.LMDB, source=lmdbData,
                    transform_param=dict(scale=1./255), ntop=1)
    
    n.label = L.Data(batch_size=batch_size, backend=P.Data.LMDB, source=lmdbLabel,
                    transform_param=dict(scale=1./255), ntop=1)

    n.conv1 = L.Convolution(n.data, kernel_size=4, num_output=200, 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=1, pool=P.Pooling.MAX)
    n.fc1   = L.InnerProduct(n.pool2, num_output=200, weight_filler=dict(type='xavier'))
    n.relu1 = L.ReLU(n.fc1, in_place=True)
    n.score = L.InnerProduct(n.relu1, num_output=1200, weight_filler=dict(type='xavier'))
    n.loss  = L.Python(n.score, n.label, module='pyloss', layer='EuclideanLossLayer')

    return n.to_proto()
示例#5
0
    def extract_head(self, subset):

        image_data_param = dict(
            source=subset.get_list_absolute_path(),
            batch_size=self.batch_size,
            root_folder=subset.root_folder,
            # 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 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

        n = NetSpec()
        n.data, n.label = L.ImageData(ntop=2,
                                      image_data_param=image_data_param,
                                      transform_param=transform_param
                                      )  # , include=dict(phase=caffe.TEST))

        net = n.to_proto()
        net.name = self.name
        return net
示例#6
0
def gen_net(batch_size=512):
    n=NetSpec();
    n.data = L.DummyData(shape={"dim":[batch_size,3,96,96]})
    n.select1 = L.DummyData(shape={"dim":[2]})
    n.select2 = L.DummyData(shape={"dim":[2]})
    n.label = L.DummyData(shape={"dim":[2]})
    caffenet_stack(n.data, n)
    n.first = L.BatchReindex(n.fc6, n.select1)
    n.second = L.BatchReindex(n.fc6, n.select2)
    n.fc6_concat=L.Concat(n.first, n.second);

    n.fc7, n.bn7, n.relu7 = fc_relu(n.fc6_concat, 4096, batchnorm=True);
    n.fc8, n.relu8 = fc_relu(n.relu7, 4096);
    n.fc9 = L.InnerProduct(n.relu8, num_output=8,
                            weight_filler=dict(type='xavier'));
    n.loss = L.SoftmaxWithLoss(n.fc9, n.label, loss_param=dict(normalization=P.Loss.NONE));

    prot=n.to_proto()
    prot.debug_info=True
    return prot;
示例#7
0
def main():
    from argparse import ArgumentParser
    from os import path

    parser = ArgumentParser()
    parser.add_argument('prototxt')
    parser.add_argument('-l', '--load', help='Load a caffemodel')
    parser.add_argument('-d',
                        '--data',
                        default=None,
                        help='Image list to use [default prototxt data]')
    #parser.add_argument('-q', action='store_true', help='Quiet execution')
    parser.add_argument('-sm', action='store_true', help='Summary only')
    parser.add_argument('-q', action='store_true', help='Quiet execution')
    parser.add_argument('-a',
                        '--all',
                        action='store_true',
                        help='Show the statistic for all layers')
    parser.add_argument('-nc', action='store_true', help='Do not use color')
    parser.add_argument('-s',
                        type=float,
                        default=1.0,
                        help='Scale the input [only custom data "-d"]')
    parser.add_argument('-bs',
                        type=int,
                        default=16,
                        help='Batch size [only custom data "-d"]')
    parser.add_argument('-nit',
                        type=int,
                        default=10,
                        help='Number of iterations')
    parser.add_argument('--gpu',
                        type=int,
                        default=0,
                        help='What gpu to run it on?')
    args = parser.parse_args()

    if args.q:
        from os import environ
        environ['GLOG_minloglevel'] = '2'
    import caffe, load
    from caffe import NetSpec, layers as L

    caffe.set_mode_gpu()
    if args.gpu is not None:
        caffe.set_device(args.gpu)

    if args.data is not None:
        model = load.ProtoDesc(args.prototxt)
        net = NetSpec()
        fl = getFileList(args.data)
        if len(fl) == 0:
            print("Unknown data type for '%s'" % args.data)
            exit(1)
        from tempfile import NamedTemporaryFile
        f = NamedTemporaryFile('w')
        f.write('\n'.join([path.abspath(i) + ' 0' for i in fl]))
        f.flush()
        net.data, net.label = L.ImageData(source=f.name,
                                          batch_size=args.bs,
                                          new_width=model.input_dim[-1],
                                          new_height=model.input_dim[-1],
                                          transform_param=dict(
                                              mean_value=[104, 117, 123],
                                              scale=args.s),
                                          ntop=2)
        net.out = model(data=net.data, label=net.label)
        n = netFromString('force_backward:true\n' + str(net.to_proto()),
                          caffe.TRAIN)
    else:
        n = caffe.Net(args.prototxt, caffe.TRAIN)

    if args.load is not None:
        n.copy_from(args.load)

    cvar = printMeanStddev(n,
                           NIT=args.nit,
                           show_all=args.all,
                           show_color=not args.nc,
                           quiet=args.sm)
    cv, gr = computeGradientRatio(n, NIT=args.nit)
    print()
    print('  Summary  ')
    print('-----------')
    print()
    print(
        'layer name                         out cvar    rate cvar    rate mean'
    )
    for l in n._layer_names:
        if l in cvar and l in cv and l in gr:
            print('%-30s   %10.2f   %10.2f   %10.2f' %
                  (l, cvar[l], cv[l], gr[l]))
示例#8
0
def main():
	from argparse import ArgumentParser
	from os import path
	import numpy as np
	parser = ArgumentParser()
	parser.add_argument('prototxt')
	parser.add_argument('output_caffemodel')
	parser.add_argument('-l', '--load', help='Load a pretrained model and rescale it [bias and type are not supported]')
	parser.add_argument('-d', '--data', default=None, help='Image list to use [default prototxt data]')
	parser.add_argument('-b', '--bias', type=float, default=0.1, help='Bias')
	parser.add_argument('-t', '--type', default='elwise', help='Type: elwise, pca, zca, kmeans, rand (random input patches). Add fast_ to speed up the initialization, but you might lose in precision.')
	parser.add_argument('-z', action='store_true', help='Zero all weights and reinitialize')
	parser.add_argument('-cs',  action='store_true', help='Correct for scaling')
	parser.add_argument('-q', action='store_true', help='Quiet execution')
	parser.add_argument('-s', type=float, default=1.0, help='Scale the input [only custom data "-d"]')
	parser.add_argument('-bs', type=int, default=16, help='Batch size [only custom data "-d"]')
	parser.add_argument('-nit', type=int, default=10, help='Number of iterations')
	parser.add_argument('--mem-limit', type=int, default=500, help='How much memory should we use for the data buffer (MB)?')
	parser.add_argument('--gpu', type=int, default=0, help='What gpu to run it on?')
	args = parser.parse_args()
	
	if args.q:
		from os import environ
		environ['GLOG_minloglevel'] = '2'
	import caffe, load
	from caffe import NetSpec, layers as L
	
	caffe.set_mode_gpu()
	if args.gpu is not None:
		caffe.set_device(args.gpu)
	
	if args.data is not None:
		model = load.ProtoDesc(args.prototxt)
		net = NetSpec()
		fl = getFileList(args.data)
		if len(fl) == 0:
			print("Unknown data type for '%s'"%args.data)
			exit(1)
		from tempfile import NamedTemporaryFile
		f = NamedTemporaryFile('w')
		f.write('\n'.join([path.abspath(i)+' 0' for i in fl]))
		f.flush()
		net.data, net.label = L.ImageData(source=f.name, batch_size=args.bs, new_width=model.input_dim[-1], new_height=model.input_dim[-1], transform_param=dict(mean_value=[104,117,123], scale=args.s),ntop=2)
		net.out = model(data=net.data, label=net.label)
		n = netFromString('force_backward:true\n'+str(net.to_proto()), caffe.TRAIN )
	else:
		n = caffe.Net(args.prototxt, caffe.TRAIN)
	
	if args.load is not None:
		n.copy_from(args.load)
		# Rescale existing layers?
		#if args.fix:
			#magicFix(n, args.nit)

	if args.z:
		# Zero out all layers
		for l in n.layers:
			for b in l.blobs:
				b.data[...] = 0
	if any([np.abs(l.blobs[0].data).sum() < 1e-10 for l in n.layers if len(l.blobs) > 0]):
		print( [m for l,m in zip(n.layers, n._layer_names) if len(l.blobs) > 0 and np.abs(l.blobs[0].data).sum() < 1e-10] )
		magicInitialize(n, args.bias, NIT=args.nit, type=args.type, max_data=args.mem_limit*1024*1024/4)
	else:
		print( "Network already initialized, skipping magic init" )
	if args.cs:
		# A simply helper function that lets you figure out which layers are not
		# homogeneous
		#print( estimateHomogenety(n) )
		calibrateGradientRatio(n)
	n.save(args.output_caffemodel)
def main():
    from argparse import ArgumentParser
    from os import path
    import numpy as np
    parser = ArgumentParser()
    parser.add_argument('prototxt')
    parser.add_argument('output_caffemodel')
    parser.add_argument(
        '-l',
        '--load',
        help=
        'Load a pretrained model and rescale it [bias and type are not supported]'
    )
    parser.add_argument('-d',
                        '--data',
                        default=None,
                        help='Image list to use [default prototxt data]')
    parser.add_argument('-b', '--bias', type=float, default=0.1, help='Bias')
    parser.add_argument(
        '-t',
        '--type',
        default='elwise',
        help=
        'Type: elwise, pca, zca, kmeans, rand (random input patches). Add fast_ to speed up the initialization, but you might lose in precision.'
    )
    parser.add_argument(
        '--zero_from',
        default=None,
        help='Zero weights starting from this layer and reinitialize')
    parser.add_argument('-z',
                        action='store_true',
                        help='Zero all weights and reinitialize')
    parser.add_argument(
        '--post_zero_from',
        default=None,
        help=
        'AFTER everything else, zero weights starting from this layer (they will NOT be reinitialized)'
    )
    parser.add_argument('-cs', action='store_true', help='Correct for scaling')
    parser.add_argument('-q', action='store_true', help='Quiet execution')
    parser.add_argument('-s',
                        type=float,
                        default=1.0,
                        help='Scale the input [only custom data "-d"]')
    parser.add_argument('-bs',
                        type=int,
                        default=16,
                        help='Batch size [only custom data "-d"]')
    parser.add_argument('-nit',
                        type=int,
                        default=10,
                        help='Number of iterations')
    parser.add_argument(
        '--mem-limit',
        type=int,
        default=500,
        help='How much memory should we use for the data buffer (MB)?')
    parser.add_argument('--gpu',
                        type=int,
                        default=0,
                        help='What gpu to run it on?')
    args = parser.parse_args()

    if args.q:
        from os import environ
        environ['GLOG_minloglevel'] = '2'
    import caffe, load
    from caffe import NetSpec, layers as L

    caffe.set_mode_gpu()
    if args.gpu is not None:
        caffe.set_device(args.gpu)

    if args.data is not None:
        model = load.ProtoDesc(args.prototxt)
        net = NetSpec()
        fl = getFileList(args.data)
        if len(fl) == 0:
            print("Unknown data type for '%s'" % args.data)
            exit(1)
        from tempfile import NamedTemporaryFile
        f = NamedTemporaryFile('w')
        f.write('\n'.join([path.abspath(i) + ' 0' for i in fl]))
        f.flush()
        net.data, net.label = L.ImageData(source=f.name,
                                          batch_size=args.bs,
                                          new_width=model.input_dim[-1],
                                          new_height=model.input_dim[-1],
                                          transform_param=dict(
                                              mean_value=[104, 117, 123],
                                              scale=args.s),
                                          ntop=2)
        net.out = model(data=net.data, label=net.label)
        n = netFromString('force_backward:true\n' + str(net.to_proto()),
                          caffe.TRAIN)
    else:
        n = caffe.Net(args.prototxt, caffe.TRAIN)

    if args.load is not None:
        n.copy_from(args.load)
        # Rescale existing layers?
        #if args.fix:
        #magicFix(n, args.nit)

    if args.z or args.zero_from:
        zeroLayers(n, start=args.zero_from)
    if any([
            np.abs(l.blobs[0].data).sum() < 1e-10 for l in n.layers
            if len(l.blobs) > 0
    ]):
        print([
            m for l, m in zip(n.layers, n._layer_names)
            if len(l.blobs) > 0 and np.abs(l.blobs[0].data).sum() < 1e-10
        ])
        magicInitialize(n,
                        args.bias,
                        NIT=args.nit,
                        type=args.type,
                        max_data=args.mem_limit * 1024 * 1024 / 4)
    else:
        print("Network already initialized, skipping magic init")
    if args.cs:
        # A simply helper function that lets you figure out which layers are not
        # homogeneous
        #print( estimateHomogenety(n) )
        print('Calibrating gradient ratio')
        calibrateGradientRatio(n)
    if args.post_zero_from:
        zeroLayers(n, start=args.post_zero_from)
    n.save(args.output_caffemodel)
示例#10
0
def get_train_data_layer(batch_size):
    #important: conf.train_lmdb
    n = NetSpec()
    n.data, n.label = L.Data(batch_size=batch_size, backend=P.Data.LMDB, source=conf.train_lmdb, include=dict(phase=caffe_pb2.TRAIN),
                             transform_param=dict(crop_size=227, mean_value=[104, 117, 123]), ntop=2)
    return n.to_proto()