def read(folder): log.info('Reading pretrained network from {}'.format(folder)) saver = Saver(folder) ckpt_info = saver.get_ckpt_info() model_opt = ckpt_info['model_opt'] ckpt_fname = ckpt_info['ckpt_fname'] model_id = ckpt_info['model_id'] model = attn_model.get_model(model_opt) ctrl_cnn_nlayers = len(model_opt['ctrl_cnn_filter_size']) ctrl_mlp_nlayers = model_opt['num_ctrl_mlp_layers'] attn_cnn_nlayers = len(model_opt['attn_cnn_filter_size']) attn_mlp_nlayers = model_opt['num_attn_mlp_layers'] attn_dcnn_nlayers = len(model_opt['attn_dcnn_filter_size']) timespan = model_opt['timespan'] glimpse_mlp_nlayers = model_opt['num_glimpse_mlp_layers'] score_mlp_nlayers = 1 weights = {} sess = tf.Session() saver.restore(sess, ckpt_fname) output_list = [] for net, nlayers in zip(['ctrl_cnn', 'ctrl_mlp', 'glimpse_mlp', 'score_mlp', 'attn_cnn', 'attn_mlp', 'attn_dcnn'], [ctrl_cnn_nlayers, ctrl_mlp_nlayers, glimpse_mlp_nlayers, score_mlp_nlayers, attn_cnn_nlayers, attn_mlp_nlayers, attn_dcnn_nlayers]): for ii in xrange(nlayers): for w in ['w', 'b']: key = '{}_{}_{}'.format(net, w, ii) log.info(key) output_list.append(key) if net == 'ctrl_cnn' or net == 'attn_cnn' or net == 'attn_dcnn': for tt in xrange(timespan): for w in ['beta', 'gamma']: key = '{}_{}_{}_{}'.format(net, ii, tt, w) log.info(key) output_list.append(key) for net in ['ctrl_lstm']: for w in ['w_xi', 'w_hi', 'b_i', 'w_xf', 'w_hf', 'b_f', 'w_xu', 'w_hu', 'b_u', 'w_xo', 'w_ho', 'b_o']: key = '{}_{}'.format(net, w) log.info(key) output_list.append(key) output_var = [] for key in output_list: output_var.append(model[key]) output_var_value = sess.run(output_var) for key, value in zip(output_list, output_var_value): weights[key] = value log.info(key) log.info(value.shape) return weights
def read(folder): log.info('Reading pretrained network from {}'.format(folder)) saver = Saver(folder) ckpt_info = saver.get_ckpt_info() model_opt = ckpt_info['model_opt'] ckpt_fname = ckpt_info['ckpt_fname'] model_id = ckpt_info['model_id'] model = box_model.get_model(model_opt) ctrl_cnn_nlayers = len(model_opt['ctrl_cnn_filter_size']) ctrl_mlp_nlayers = model_opt['num_ctrl_mlp_layers'] timespan = model_opt['timespan'] glimpse_mlp_nlayers = model_opt['num_glimpse_mlp_layers'] weights = {} sess = tf.Session() saver.restore(sess, ckpt_fname) output_list = [] for net, nlayers in zip(['ctrl_cnn', 'ctrl_mlp', 'glimpse_mlp'], [ctrl_cnn_nlayers, ctrl_mlp_nlayers, glimpse_mlp_nlayers]): for ii in xrange(nlayers): for w in ['w', 'b']: key = '{}_{}_{}'.format(net, w, ii) log.info(key) output_list.append(key) if net == 'ctrl_cnn': for tt in xrange(timespan): for w in ['beta', 'gamma']: key = '{}_{}_{}_{}'.format(net, ii, tt, w) log.info(key) output_list.append(key) for net in ['ctrl_lstm']: for w in ['w_xi', 'w_hi', 'b_i', 'w_xf', 'w_hf', 'b_f', 'w_xu', 'w_hu', 'b_u', 'w_xo', 'w_ho', 'b_o']: key = '{}_{}'.format(net, w) log.info(key) output_list.append(key) output_var = [] for key in output_list: output_var.append(model[key]) output_var_value = sess.run(output_var) for key, value in zip(output_list, output_var_value): weights[key] = value log.info(key) log.info(value.shape) return weights
def read(folder): log.info('Reading pretrained network from {}'.format(folder)) saver = Saver(folder) ckpt_info = saver.get_ckpt_info() model_opt = ckpt_info['model_opt'] ckpt_fname = ckpt_info['ckpt_fname'] model_id = ckpt_info['model_id'] model = patch_model.get_model(model_opt) attn_cnn_nlayers = len(model_opt['attn_cnn_filter_size']) attn_mlp_nlayers = model_opt['num_attn_mlp_layers'] attn_dcnn_nlayers = len(model_opt['attn_dcnn_filter_size']) timespan = model_opt['timespan'] weights = {} sess = tf.Session() saver.restore(sess, ckpt_fname) output_list = [] for net, nlayers in zip( ['attn_cnn', 'attn_mlp', 'attn_dcnn'], [attn_cnn_nlayers, attn_mlp_nlayers, attn_dcnn_nlayers]): for ii in xrange(nlayers): for w in ['w', 'b']: key = '{}_{}_{}'.format(net, w, ii) log.info(key) output_list.append(key) if net == 'attn_cnn' or net == 'attn_dcnn': for tt in xrange(timespan): for w in ['beta', 'gamma']: key = '{}_{}_{}_{}'.format(net, ii, tt, w) if key in model: log.info(key) output_list.append(key) output_var = [] for key in output_list: output_var.append(model[key]) output_var_value = sess.run(output_var) for key, value in zip(output_list, output_var_value): weights[key] = value log.info(key) log.info(value.shape) return weights
def read(folder): log.info('Reading foreground segmentation network from {}'.format(folder)) saver = Saver(folder) ckpt_info = saver.get_ckpt_info() model_opt = ckpt_info['model_opt'] ckpt_fname = ckpt_info['ckpt_fname'] model_id = ckpt_info['model_id'] model = models.get_model(model_opt) cnn_nlayers = len(model_opt['cnn_filter_size']) weights = {} sess = tf.Session() saver.restore(sess, ckpt_fname) for ii in xrange(cnn_nlayers): w_name = 'cnn_w_{}'.format(ii) b_name = 'cnn_b_{}'.format(ii) weights[w_name] = sess.run(model[w_name]) weights[b_name] = sess.run(model[b_name]) log.info(w_name) log.info(weights[w_name].shape) log.info(b_name) log.info(weights[b_name].shape) return weights
sess = tf.Session() # Create time series loggers if train_opt['logs']: log_manager = LogManager(logs_folder) loggers = get_ts_loggers(model_opt, restore_step=step) trainer.register_raw_logs(log_manager, log, model_opt, saver) samples = get_plot_loggers(model_opt, train_opt) log_url = 'http://{}/deep-dashboard?id={}'.format( train_opt['localhost'], model_id) log.info('Visualization can be viewed at: {}'.format(log_url)) # Restore/intialize weights if args.restore: saver.restore(sess, ckpt_fname) else: sess.run(tf.initialize_all_variables()) batch_size = args.batch_size log.info('Batch size: {}'.format(batch_size)) num_ex_train = dataset['train']['input'].shape[0] get_batch_train = trainer.get_batch_fn(dataset['train']) log.info('Number of training examples: {}'.format(num_ex_train)) if train_opt['has_valid']: num_ex_valid = dataset['valid']['input'].shape[0] get_batch_valid = trainer.get_batch_fn(dataset['valid']) log.info('Number of validation examples: {}'.format(num_ex_valid)) def run_samples():
class ExperimentBase(object): def __init__(self, name, opt, data_opt=None, model_opt=None, seed=1234): # Restore previously saved checkpoints. self.opt = opt self.name = name self.new_model_opt = None if self.opt['restore']: self.restore_options(opt, data_opt) if model_opt is not None: if 'finetune' in model_opt and model_opt['finetune']: self.model_opt['finetune'] = model_opt['finetune'] self.new_model_opt = model_opt self.step.reset() self.model_id = self.get_model_id() self.exp_folder = os.path.join(self.opt['results'], self.model_id) self.saver = Saver( self.exp_folder, model_opt=self.model_opt, data_opt=self.data_opt) self.exp_folder = opt['restore'] else: if self.opt['model_id']: self.model_id = self.opt['model_id'] else: self.model_id = self.get_model_id() if model_opt is None or data_opt is None: raise Exception('You need to specify model options and data options') self.model_opt = model_opt self.data_opt = data_opt self.step = StepCounter() self.exp_folder = os.path.join(self.opt['results'], self.model_id) self.saver = Saver( self.exp_folder, model_opt=self.model_opt, data_opt=self.data_opt) self.init_cmd_logger() self.sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) # Log arguments self.log.log_args() # Train loop options self.log.info('Building model') self.model = self.get_model() # Load dataset self.log.info('Loading dataset') self.dataset_name = self.data_opt['dataset'] self.dataset = self.get_dataset() self.init_model() self.init_logs() def restore_options(self, opt, data_opt): self.saver = Saver(opt['restore']) self.ckpt_info = self.saver.get_ckpt_info() self.model_opt = self.ckpt_info['model_opt'] if data_opt is None: self.data_opt = self.ckpt_info['data_opt'] else: self.data_opt = data_opt self.ckpt_fname = self.ckpt_info['ckpt_fname'] self.step = StepCounter(self.ckpt_info['step']) self.model_id = self.ckpt_info['model_id'] pass def init_model(self): # Restore/intialize weights if self.opt['restore']: self.saver.restore(self.sess, self.ckpt_fname) else: self.sess.run(tf.global_variables_initializer(), feed_dict={self.model["phase_train"]: True}) def init_cmd_logger(self): self.log = logger.get() def init_logs(self): pass def get_model_id(self): return '{}-{}'.format(self.name, get_id()) @classmethod def create_from_main(cls, name, parsers=None, description=None): cmd = CmdArgsBase(description) opt = None if parsers is not None: for key in parsers.iterkeys(): cmd.add_parser(key, parsers[key]) opt = cmd.get_opt('default') if 'data' in parsers: data_opt = cmd.get_opt('data') else: data_opt = None if 'model' in parsers: model_opt = cmd.get_opt('model') else: model_opt = None return cls(name, opt, data_opt=data_opt, model_opt=model_opt) def get_model(self): raise NotImplemented() def get_dataset(self): raise NotImplemented() def run(self): raise NotImplemented()
def read(folder): log.info('Reading pretrained network from {}'.format(folder)) saver = Saver(folder) ckpt_info = saver.get_ckpt_info() model_opt = ckpt_info['model_opt'] ckpt_fname = ckpt_info['ckpt_fname'] model_id = ckpt_info['model_id'] model = get_model(model_opt) ctrl_cnn_nlayers = len(model_opt['ctrl_cnn_filter_size']) ctrl_mlp_nlayers = model_opt['num_ctrl_mlp_layers'] attn_cnn_nlayers = len(model_opt['attn_cnn_filter_size']) attn_dcnn_nlayers = len(model_opt['attn_dcnn_filter_size']) timespan = model_opt['timespan'] glimpse_mlp_nlayers = model_opt['num_glimpse_mlp_layers'] weights = {} with tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) as sess: saver.restore(sess, ckpt_fname) output_list = [] for net, nlayers in zip( ['ctrl_cnn', 'ctrl_mlp', 'glimpse_mlp', 'score_mlp'], [ctrl_cnn_nlayers, ctrl_mlp_nlayers, glimpse_mlp_nlayers, 1]): for ii in range(nlayers): for w in ['w', 'b']: key = '{}_{}_{}'.format(net, w, ii) log.info(key) output_list.append(key) if net == 'ctrl_cnn': for tt in range(timespan): for w in ['beta', 'gamma']: key = '{}_{}_{}_{}'.format(net, ii, tt, w) log.info(key) output_list.append(key) for net in ['ctrl_lstm']: wlist = [ 'w_xi', 'w_hi', 'b_i', 'w_xf', 'w_hf', 'b_f', 'w_xu', 'w_hu', 'b_u', 'w_xo', 'w_ho', 'b_o' ] for w in wlist: key = '{}_{}'.format(net, w) log.info(key) output_list.append(key) net_list = ['attn_cnn', 'attn_dcnn'] nlayers_list = [attn_cnn_nlayers, attn_dcnn_nlayers] for net, nlayers in zip(net_list, nlayers_list): for ii in range(nlayers): for w in ['w', 'b']: key = '{}_{}_{}'.format(net, w, ii) log.info(key) output_list.append(key) if net == 'attn_cnn' or net == 'attn_dcnn': for tt in range(timespan): for w in ['beta', 'gamma']: key = '{}_{}_{}_{}'.format(net, ii, tt, w) if key in model: log.info(key) output_list.append(key) output_var = [] for key in output_list: output_var.append(model[key]) output_var_value = sess.run(output_var) for key, value in zip(output_list, output_var_value): weights[key] = value log.info(key) log.info(value.shape) return weights