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)
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]))
if output_dir is None: from tempfile import mkdtemp output_dir = mkdtemp() + '/' from os import path, mkdir if not path.isdir(output_dir): mkdir(output_dir) # Define the classification model to use from caffe_all import * from data import * from util import * from python_layers import Py import load model = load.ProtoDesc(args.prototxt) # Create the training net ns = NetSpec() mean_value = [104, 117, 123] if args.no_mean: mean_value = [0, 0, 0] ns.data, ns.cls = dataLayer(args.voc_dir, output_dir, batch_size=args.bs, transform_param=dict(crop_size=model.input_dim[-1], min_scale=args.min_scale, max_scale=args.max_scale, mean_value=mean_value, mirror=True, scale=args.scale), resize=args.resize)