我们从Python开源项目中,提取了以下7个代码示例,用于说明如何使用utils.load_params()。
def deconvoluter(params_fn, specstr, shape): input_var = T.tensor4('input') decnet = build_deconv_net(input_var, shape=shape, specstr=specstr) u.load_params(decnet, params_fn) return theano.function([input_var], nn.layers.get_output(decnet)) # builds a CAE and returns functions to go from image space to the layer denoted by # layersplit, and from that layer back to images. However, the second function only # works correctly if layersplit='encode', due to the structure of the Lasagne layer # implementation, so if we split on a different layer then it is necessary to # build a separate function for going from conv-space to images.
def encoder_decoder(paramsfile, specstr, channels=3, layersplit='encode', shape=(64,64), poolinv=False): inp = T.tensor4('inputs') w,h=shape build_fn = build_cae if poolinv else build_cae_nopoolinv network = build_fn(inp, shape=shape,channels=channels,specstr=specstr) u.load_params(network, paramsfile) laylist = nn.layers.get_all_layers(network) enc_layer_idx = next(i for i in xrange(len(laylist)) if laylist[i].name==layersplit) enc_layer = laylist[enc_layer_idx] return (lambda x: nn.layers.get_output(enc_layer, inputs=x,deterministic=True).eval(), lambda x: nn.layers.get_output(network, inputs={laylist[0]:np.zeros((x.shape[0],channels,w,h), dtype=theano.config.floatX), enc_layer:x}, deterministic=True).eval().reshape(-1,channels,w,h))
def get_segmenter_function(params_loc, img_size, NCV=1, version=1, param_file_key = None): shape = (None, 1, img_size, img_size) input_var = T.tensor4('input') if NCV> 1: expr = 0 params_files = filter(lambda s: 'fcn_v{}'.format(version) in s, os.listdir(params_loc)) if param_file_key is not None: params_files = filter(lambda s: param_file_key in s, params_files) for pfn in params_files: net, _, output_det = build_fcn_segmenter(input_var, shape, version) u.load_params(net['output'], os.path.join(params_loc, pfn)) cv = int(pfn.split('_')[-1][1]); if cv == NCV: expr = expr + output_det * NCV; else: expr = expr + output_det print 'loaded {}'.format(pfn) assert(len(params_files)==NCV+1); expr = expr / NCV /2; print 'loaded {} in ensemble'.format(len(params_files)) else: net, _, output_det = build_fcn_segmenter(input_var, shape, version) u.load_params(net['output'], params_loc) expr = output_det print 'loaded indiv function {}'.format(params_loc) return theano.function([input_var], expr)
def get_segmenter_function(params_loc, img_size, ensemble=False, version=2, param_file_key = '.npz', weight_full_params=0.33): shape = (None, 1, img_size, img_size) input_var = T.tensor4('input') if ensemble: expr = 0 params_files = filter(lambda s: 'v{}'.format(version) in s, os.listdir(params_loc)) if param_file_key is not None: params_files = filter(lambda s: param_file_key in s, params_files) full_params_indices = [i for i,a in enumerate(params_files) if 'f-1' in a] if len(full_params_indices) > 0: wt_norm = (1. - weight_full_params)/(len(params_files) - len(full_params_indices)) wt_full = weight_full_params / len(full_params_indices) params_weights = [(wt_norm if i not in full_params_indices else wt_full) \ for i in xrange(len(params_files))] else: params_weights = [1./len(params_files)] * len(params_files) for pfn,w in zip(params_files, params_weights): net, _, output_det = build_fcn_segmenter(input_var, shape, version) u.load_params(net['output'], os.path.join(params_loc, pfn)) expr = expr + w*output_det print 'loaded {} wt {}'.format(pfn, w) print 'loaded {} in ensemble'.format(len(params_files)) else: net, _, output_det = build_fcn_segmenter(input_var, shape, version) u.load_params(net['output'], params_loc) expr = output_det print 'loaded indiv function {}'.format(params_loc) return theano.function([input_var], expr)
def load_model(embed_map=None): """ Load all model components + apply vocab expansion """ # Load the worddict print 'Loading dictionary...' with open(path_to_dictionary, 'rb') as f: worddict = pkl.load(f) # Create inverted dictionary print 'Creating inverted dictionary...' word_idict = dict() for kk, vv in worddict.iteritems(): word_idict[vv] = kk word_idict[0] = '<eos>' word_idict[1] = 'UNK' # Load model options print 'Loading model options...' with open('%s.pkl'%path_to_model, 'rb') as f: options = pkl.load(f) # Load parameters print 'Loading model parameters...' params = init_params(options) params = load_params(path_to_model, params) tparams = init_tparams(params) # Extractor functions print 'Compiling encoder...' trng = RandomStreams(1234) trng, x, x_mask, ctx, emb = build_encoder(tparams, options) f_enc = theano.function([x, x_mask], ctx, name='f_enc') f_emb = theano.function([x], emb, name='f_emb') trng, embedding, x_mask, ctxw2v = build_encoder_w2v(tparams, options) f_w2v = theano.function([embedding, x_mask], ctxw2v, name='f_w2v') # Load word2vec, if applicable if embed_map == None: print 'Loading word2vec embeddings...' embed_map = load_googlenews_vectors(path_to_word2vec) # Lookup table using vocab expansion trick print 'Creating word lookup tables...' table = lookup_table(options, embed_map, worddict, word_idict, f_emb) # Store everything we need in a dictionary print 'Packing up...' model = {} model['options'] = options model['table'] = table model['f_w2v'] = f_w2v return model
def load_model(): """ Load a trained model for decoding """ # Load the worddict print 'Loading dictionary...' with open(path_to_dictionary, 'rb') as f: worddict = pkl.load(f) # Create inverted dictionary print 'Creating inverted dictionary...' word_idict = dict() for kk, vv in worddict.iteritems(): word_idict[vv] = kk word_idict[0] = '<eos>' word_idict[1] = 'UNK' # Load model options print 'Loading model options...' with open('%s.pkl'%path_to_model, 'rb') as f: options = pkl.load(f) # Load parameters print 'Loading model parameters...' params = init_params(options) params = load_params(path_to_model, params) tparams = init_tparams(params) # Sampler. trng = RandomStreams(1234) f_init, f_next = build_sampler(tparams, options, trng) # Pack everything up dec = dict() dec['options'] = options dec['trng'] = trng dec['worddict'] = worddict dec['word_idict'] = word_idict dec['tparams'] = tparams dec['f_init'] = f_init dec['f_next'] = f_next return dec
def generate(model_options_file='model_options.pkl', model_file='model_best_so_far.npz'): from_dir = 'model_files/' print 'preparing reload' model_options = utils.load_pkl(from_dir+model_options_file) print 'Loading data' engine = data_engine.Movie2Caption('attention', model_options['dataset'], model_options['video_feature'], model_options['batch_size'], model_options['valid_batch_size'], model_options['maxlen'], model_options['n_words'], model_options['K'], model_options['OutOf']) feat = numpy.load('datas/vid1715.npy') ctx = engine.get_sub_frames(feat) ctx_mask = engine.get_ctx_mask(ctx) print 'init params' t0 = time.time() model = Model() params = model.init_params(model_options) model_saved = from_dir + model_file assert os.path.isfile(model_saved) print "Reloading model params..." params = utils.load_params(model_saved, params) tparams = utils.init_tparams(params) print tparams.keys print 'buliding sampler' use_noise = theano.shared(numpy.float32(0.)) use_noise.set_value(0.) trng = RandomStreams(1234) f_init, f_next = model.build_sampler(tparams, model_options, use_noise, trng) print 'start generate...' g_t0 = time.time() sample, sample_score, _, _ = model.gen_sample(None, f_init, f_next, ctx, ctx_mask, model_options, None, 5, maxlen=model_options['maxlen']) print sample # best_one = numpy.argmin(sample_score) # sample = sample[best_one] for s in sample: for kk, ss in enumerate([s]): for vv in ss: if vv == 0: break if vv in engine.word_idict: print engine.word_idict[vv], else: print 'UNK', print