def main():
    # Use first line of file docstring as description if a file docstring exists.
    parser = argparse.ArgumentParser(
        description=__doc__.split('\n')[0] if __doc__ else '',
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('--bitmask', type=str, required=True)
    parser.add_argument('--output', type=str, required=True)
    parser.add_argument('--snapshot_prefix',
                        default='{output}/models/alexnet_{bitmask}')
    parser.add_argument('--solver',
                        default='models/bvlc_alexnet/solver.prototxt')
    parser.add_argument('--trainval',
                        default='models/bvlc_alexnet/train_val.prototxt')
    # Do we need the deploy?
    parser.add_argument('--deploy',
                        default='models/bvlc_alexnet/deploy.prototxt')

    args = parser.parse_args()
    args.snapshot_prefix = args.snapshot_prefix.format(output=args.output,
                                                       bitmask=args.bitmask)

    bitmask = args.bitmask
    assert len(bitmask) == len(LAYERS), (
        'Expected {} bits in bitmask, received {}.'.format(
            len(LAYERS), len(bitmask)))
    try:
        int(bitmask)
    except ValueError:
        raise Exception('Invalid bitmask: {}'.format(bitmask))

    zeroed_layers = set(layer for i, layer in enumerate(LAYERS)
                        if bitmask[i] == '0')

    # Parse solver
    solver = SolverParameter()
    parse_prototxt(solver, args.solver)
    solver.snapshot_prefix = args.snapshot_prefix
    solver.net = '{}/train_val.prototxt'.format(args.output)

    # Parse trainval NetParameter
    trainval = NetParameter()
    parse_prototxt(trainval, args.trainval)

    for layer in trainval.layer:
        if layer.name in zeroed_layers:
            print('Zeroing {}'.format(layer.name))
            for param in layer.param:
                param.lr_mult = 0
                param.decay_mult = 0

    mkdir_p(args.output)
    write_prototxt(trainval, '{}/train_val.prototxt'.format(args.output))
    write_prototxt(solver, '{}/solver.prototxt'.format(args.output))
    copyfile(args.deploy, '{}/deploy.prototxt'.format(args.output))
Esempio n. 2
0
def new_solver(learning_rate):
    param = SolverParameter()
    param.solver_type = SolverParameter.ADAM
    param.momentum = 0.95
    param.base_lr = learning_rate
    param.lr_policy = "step"
    param.gamma = 0.1
    param.stepsize = 10000000
    param.max_iter = 10000000
    param.display = 0
    param.clip_gradients = 10
    return param
Esempio n. 3
0
    def init_solver(self):
        """ Helper method to initialize the solver. """
        solver_param = SolverParameter()
        solver_param.snapshot_prefix = self._hyperparams['weights_file_prefix']
        solver_param.display = 0  # Don't display anything.
        solver_param.base_lr = self._hyperparams['lr']
        solver_param.lr_policy = self._hyperparams['lr_policy']
        solver_param.momentum = self._hyperparams['momentum']
        solver_param.weight_decay = self._hyperparams['weight_decay']
        solver_param.type = self._hyperparams['solver_type']
        solver_param.random_seed = self._hyperparams['random_seed']

        # Pass in net parameter either by filename or protostring.
        if isinstance(self._hyperparams['network_model'], basestring):
            self.solver = caffe.get_solver(self._hyperparams['network_model'])
        else:
            network_arch_params = self._hyperparams['network_arch_params']
            network_arch_params['dim_input'] = self._dO
            network_arch_params['dim_output'] = self._dU

            network_arch_params['batch_size'] = self.batch_size
            network_arch_params['phase'] = TRAIN
            solver_param.train_net_param.CopyFrom(
                self._hyperparams['network_model'](**network_arch_params)
            )

            # For running forward in python.
            network_arch_params['batch_size'] = 1
            network_arch_params['phase'] = TEST
            solver_param.test_net_param.add().CopyFrom(
                self._hyperparams['network_model'](**network_arch_params)
            )

            # For running forward on the robot.
            network_arch_params['batch_size'] = 1
            network_arch_params['phase'] = 'deploy'
            solver_param.test_net_param.add().CopyFrom(
                self._hyperparams['network_model'](**network_arch_params)
            )

            # These are required by Caffe to be set, but not used.
            solver_param.test_iter.append(1)
            solver_param.test_iter.append(1)
            solver_param.test_interval = 1000000

            f = tempfile.NamedTemporaryFile(mode='w+', delete=False)
            f.write(MessageToString(solver_param))
            f.close()

            self.solver = caffe.get_solver(f.name)
Esempio n. 4
0
    def init_solver(self):
        """ Helper method to initialize the solver. """
        solver_param = SolverParameter()
        solver_param.snapshot_prefix = self._hyperparams['weights_file_prefix']
        solver_param.display = 0  # Don't display anything.
        solver_param.base_lr = self._hyperparams['lr']
        solver_param.lr_policy = self._hyperparams['lr_policy']
        solver_param.momentum = self._hyperparams['momentum']
        solver_param.weight_decay = self._hyperparams['weight_decay']
        solver_param.type = self._hyperparams['solver_type']

        # Pass in net parameter either by filename or protostring.
        if isinstance(self._hyperparams['network_model'], basestring):
            self.solver = caffe.get_solver(self._hyperparams['network_model'])
        else:
            network_arch_params = self._hyperparams['network_arch_params']
            network_arch_params['dim_input'] = self._dO
            network_arch_params['dim_output'] = self._dU

            network_arch_params['batch_size'] = self.batch_size
            network_arch_params['phase'] = TRAIN
            solver_param.train_net_param.CopyFrom(
                self._hyperparams['network_model'](**network_arch_params)
            )

            # For running forward in python.
            network_arch_params['batch_size'] = 1
            network_arch_params['phase'] = TEST
            solver_param.test_net_param.add().CopyFrom(
                self._hyperparams['network_model'](**network_arch_params)
            )

            # For running forward on the robot.
            network_arch_params['batch_size'] = 1
            network_arch_params['phase'] = 'deploy'
            solver_param.test_net_param.add().CopyFrom(
                self._hyperparams['network_model'](**network_arch_params)
            )

            # These are required by Caffe to be set, but not used.
            solver_param.test_iter.append(1)
            solver_param.test_iter.append(1)
            solver_param.test_interval = 1000000

            f = tempfile.NamedTemporaryFile(mode='w+', delete=False)
            f.write(MessageToString(solver_param))
            f.close()

            self.solver = caffe.get_solver(f.name)
Esempio n. 5
0
def get_solver(args):
    from caffe.proto.caffe_pb2 import SolverParameter
    from google.protobuf.text_format import Merge

    solver = SolverParameter()
    Merge(open(args.solver, 'r').read(), solver)
    return solver
Esempio n. 6
0
	def _init_solver(self):
		""" Helper method to initialize the solver. """
		solver_param = SolverParameter()
		solver_param.display = 0  # Don't display anything.
		solver_param.base_lr = self._hyperparams['lr']
		solver_param.lr_policy = self._hyperparams['lr_policy']
		solver_param.momentum = self._hyperparams['momentum']
		solver_param.weight_decay = self._hyperparams['weight_decay']
		solver_param.type = self._hyperparams['solver_type']
		solver_param.random_seed = self._hyperparams['random_seed']

		# Pass in net parameter by protostring (could add option to input prototxt file).
		network_arch_params = self._hyperparams['network_arch_params']

		network_arch_params['dim_input'] = self._dO
		network_arch_params['demo_batch_size'] = self._hyperparams['demo_batch_size']
		network_arch_params['sample_batch_size'] = self._hyperparams['sample_batch_size']
		network_arch_params['T'] = self._T
		network_arch_params['phase'] = TRAIN
		solver_param.train_net_param.CopyFrom(
			self._hyperparams['network_model'](**network_arch_params)
		)

		# For running forward in python.
		network_arch_params['phase'] = TEST
		solver_param.test_net_param.add().CopyFrom(
			self._hyperparams['network_model'](**network_arch_params)
		)

		network_arch_params['phase'] = 'forward_feat'
		solver_param.test_net_param.add().CopyFrom(
			self._hyperparams['network_model'](**network_arch_params)
		)

		# These are required by Caffe to be set, but not used.
		solver_param.test_iter.append(1)
		solver_param.test_iter.append(1)
		solver_param.test_interval = 1000000

		f = tempfile.NamedTemporaryFile(mode='w+', delete=False)
		f.write(MessageToString(solver_param))
		f.close()
		self.solver = caffe.get_solver(f.name)
Esempio n. 7
0
def write_solver_file(solver_file, train_model, test_models, type, base_lr, momentum, weight_decay,
                      lr_policy, gamma, power, random_seed, max_iter, clip_gradients, snapshot_prefix,display=0):
    '''Writes a solver prototxt file with parameters set to the
    corresponding argument values. In particular, the train_net
    parameter is set to train_model, and a test_net parameter is
    added for each of test_models, which should be a list.'''
    param = SolverParameter()
    param.train_net = train_model
    for test_model in test_models:
        param.test_net.append(test_model)
        param.test_iter.append(0) #don't test automatically
    param.test_interval = max_iter
    param.type = type
    param.base_lr = base_lr
    param.momentum = momentum
    param.weight_decay = weight_decay
    param.lr_policy = lr_policy
    param.gamma = gamma
    param.power = power
    param.display = display #don't print solver iterations unless requested
    param.random_seed = random_seed
    param.max_iter = max_iter
    if clip_gradients > 0:
        param.clip_gradients = clip_gradients
    param.snapshot_prefix = snapshot_prefix
    print "WRITING",solver_file
    with open(solver_file,'w') as f:
        f.write(str(param))
Esempio n. 8
0
def lenet_solver_simple():
    """A simple version of LeNet's solver proto"""
    solver = SolverParameter()

    solver.train_net = 'lenet_auto_train.prototxt'
    solver.test_net.append('lenet_auto_test.prototxt')
    solver.test_iter.append(100)
    solver.test_interval = 500
    solver.base_lr = 0.01
    solver.momentum = 0.9
    solver.weight_decay = 0.0005
    solver.lr_policy = 'inv'
    solver.gamma = 0.0001
    solver.power = 0.75
    # solver.stepsize = 2500
    solver.display = 100
    solver.max_iter = 10000
    solver.snapshot = 5000
    solver.snapshot_prefix = 'SNAPSHOT_FULL_PATH'
    solver.solver_mode = SolverParameter.GPU

    with open('SOLVER_FULL_PATH', 'w') as f:  # generating prototxt
        f.write(str(solver))
Esempio n. 9
0
def lenet_solver():
    """A simple version of LeNet's solver proto"""
    parser = argparse.ArgumentParser()

    parser.add_argument(
        '--train_net',
        default='../../Lenet/lenet_auto_train.prototxt',
        help=
        'path to train net prototxt. [DEFAULT=../../Section4/caffenet_train.prototxt]'
    )
    parser.add_argument(
        '--test_net',
        default='../../Lenet/lenet_auto_test.prototxt',
        help=
        'path to validation net prototxt. [DEFAULT=../../Section4/caffenet_valid.prototxt]'
    )
    parser.add_argument('--solver_target_folder',
                        default='../../Lenet/',
                        help='solver target FOLDER. [DEFAULT=../../Section5/]')
    parser.add_argument(
        '--solver_filename',
        default='Lenet_solver.prototxt',
        help='solver prototxt NAME. [DEFAULT=caffenet_solver.prototxt]')
    parser.add_argument(
        '--snapshot_target_folder',
        default='../../Lenet/',
        help='snapshot target FOLDER. [DEFAULT=../../Section6/')
    parser.add_argument('--snapshot_prefix',
                        default='Lenet',
                        help='snapshot NAME prefix, [DEFAULT=caffenet]')
    args = parser.parse_args()

    SOLVER_FULL_PATH = args.solver_target_folder + args.solver_filename
    SNAPSHOT_FULL_PATH = args.snapshot_target_folder + args.snapshot_prefix
    os.system('rm -rf ' + SOLVER_FULL_PATH)
    os.system('rm -rf ' + SNAPSHOT_FULL_PATH + '*')

    solver = SolverParameter()

    solver.train_net = 'lenet_auto_train.prototxt'
    solver.test_net.append('lenet_auto_test.prototxt')
    solver.test_iter.append(100)
    solver.test_interval = 500
    solver.base_lr = 0.01
    solver.momentum = 0.9
    solver.weight_decay = 0.0005
    solver.lr_policy = 'inv'
    solver.gamma = 0.0001
    solver.power = 0.75
    # solver.stepsize = 2500
    solver.display = 100
    solver.max_iter = 10000
    solver.snapshot = 5000
    solver.snapshot_prefix = SNAPSHOT_FULL_PATH
    solver.solver_mode = SolverParameter.GPU

    with open(args.solver_filename, 'w') as f:  # generating prototxt
        f.write(str(solver))
Esempio n. 10
0
def write_solver_file(solver_file, train_model, test_models, type, base_lr,
                      momentum, weight_decay, lr_policy, gamma, power,
                      random_seed, max_iter, clip_gradients, snapshot_prefix):
    '''Writes a solver prototxt file with parameters set to the
    corresponding argument values. In particular, the train_net
    parameter is set to train_model, and a test_net parameter is
    added for each of test_models, which should be a list.'''
    param = SolverParameter()
    param.train_net = train_model
    for test_model in test_models:
        param.test_net.append(test_model)
        param.test_iter.append(0)  #don't test automatically
    param.test_interval = max_iter
    param.type = type
    param.base_lr = base_lr
    param.momentum = momentum
    param.weight_decay = weight_decay
    param.lr_policy = lr_policy
    param.gamma = gamma
    param.power = power
    param.display = 0  #don't print solver iterations
    param.random_seed = random_seed
    param.max_iter = max_iter
    if clip_gradients > 0:
        param.clip_gradients = clip_gradients
    param.snapshot_prefix = snapshot_prefix
    print "WRITING", solver_file
    with open(solver_file, 'w') as f:
        f.write(str(param))
Esempio n. 11
0
import ctypes
from caffe_ext import layers as L
from caffe.proto.caffe_pb2 import SolverParameter
import numpy as np

param = SolverParameter()
param.solver_type = param.ADAM

lib = ctypes.cdll.LoadLibrary("/root/code/dqn-info/build/libdqn-c-lib.so")
lib.DQN_new.argtypes = [
    ctypes.c_char_p,
    ctypes.c_char_p,
    ctypes.c_char_p,
    ctypes.c_char_p,
    ctypes.c_char_p,
    ctypes.c_int,
    ctypes.c_int,
    ctypes.c_int,
    ctypes.c_int,
    ctypes.c_int,
    ctypes.c_float,
]
lib.DQN_new.returntype = ctypes.c_void_p

lib.DQN_SelectAction.argtypes = [
    ctypes.c_void_p,
    ctypes.POINTER(ctypes.c_float),
    ctypes.POINTER(ctypes.c_float),
]
lib.DQN_SelectAction.returntype = None