def load_all_params(self, agent, key=None, errors='raise'): """loads agent params from the database under the given name""" assert errors in ( 'raise', 'warn', 'print', 'ignore'), "errors must be 'raise','warn','print' or 'ignore'" if errors == 'raise': #Main function key = key or self.default_params_key raw = self.redis.get(key) if raw is None: raise redis.ResponseError( "Params not found under key '%s' (got None)" % key) all_params = self.loads(raw) set_all_param_values( list(agent.agent_states) + agent.policy + agent.action_layers, all_params) else: #Error handling try: return self.load_all_params(agent, key=key, errors='raise') except: exc_type, exc, tb = sys.exc_info() if errors == 'warn': warn(str(exc)) elif errors == 'print': print(str(exc))
def reload_model(args, network, filename, rslts_src): """ Returns the network loaded of the parameters Will try to get filename in '/data/lisatmp3/lacaillp/results/rslts_src/saved_models/filename' """ if args.mila: src_dir = os.path.join('/data/lisatmp3/lacaillp/results/', str(rslts_src), 'saved_models/') dst_dir = '/Tmp/lacaillp/input/saved_models/' elif args.laptop: src_dir = '/Users/phil/input/saved_models/' dst_dir = src_dir if not os.path.exists(dst_dir): os.makedirs(dst_dir) full_path = os.path.join(dst_dir, filename) print 'Copying saved model %s locally...' % filename shutil.copy(os.path.join(src_dir, filename), full_path) print 'Completed.' print 'Initiating loading...' try: with open(full_path, 'rb') as f: values = pkl.load(f) except: print 'An error occured, model wasn\'t loaded.' return False else: lyr.set_all_param_values(network, values) print 'Network was successfully loaded from %s' % full_path return True
def test_net(snapshot): state = load_net(snapshot) if state == None: print('Could not load saved net ', snapshot) return print('Loading data and saved model...', end = ' ') tstart = time.time() dataset = load_data(state['dataset_path'], state['neighbors'], state['channels']) output_layer = state['model']( input_height = dataset['input_height'], input_width = dataset['input_width'], output_dim = dataset['output_dim'], batch_size = None, ) set_all_param_values(output_layer, state['learned_params']) print('{:.3f} s'.format(time.time() - tstart)) print('Evaluating net...', end = ' ') tstart = time.time() targets, predictions = predict(dataset, output_layer) print('{:.3f} s'.format(time.time() - tstart)) print('') print('Test accuracy: {:.3f}%'.format( metrics.accuracy_score(targets, predictions) * 100.)) print('Weighted F1-score: {:.3f}'.format( metrics.f1_score(targets, predictions, average = 'weighted')))
def build_segmentation_function(param_file='../data/trained-weights.npz'): input_var = T.tensor4('x') num_classes = 8 softmax, net, net_crf, _ = build_network(input_var, num_classes) # Initialize with pretrained weights with np.load(param_file) as f: param_values = [f['arr_%d' % i] for i in xrange(len(f.files))] nn.set_all_param_values(softmax, param_values) # Get the output of the networks. output, output_crf = nn.get_output([net, net_crf], deterministic=True) # Process info for network path with & without CRF shape = (output.shape[2], output.shape[3]) output = T.argmax(output, axis=1).reshape(shape) output_crf = T.argmax(output_crf, axis=1).reshape(shape) # Compile the function function = theano.function([input_var], [output, output_crf], allow_input_downcast=True) return function
def warp_images(): print('building model') layers = vgg16.build_model((None, 3, 227, 227)) batch_size = 32 infer_dir = join('data', 'inference') weightsfile = join('weights', 'weights.pickle') with open(weightsfile, 'rb') as f: param_values = pickle.load(f) set_all_param_values(layers['trans'], param_values) pretrainfile = join('weights', 'vgg16.pkl') with open(pretrainfile, 'rb') as f: data = pickle.load(f) mean = data['mean value'] image_fpaths = [('Cars_013b.png', 'Cars_009b.png'), ('060_0071.png', '060_0000.png'), ('246_0052.png', '246_0042.png')] print('compiling theano functions for inference') num_infer_idx = (len(image_fpaths) + batch_size - 1) / batch_size infer_func = theano_funcs.create_infer_func(layers) infer_iter = utils.get_batch_idx(len(image_fpaths), batch_size) for i, idx in tqdm(infer_iter, total=num_infer_idx, leave=False): Xa, Xb = utils.prepare_batch(image_fpaths[idx], mean) M = infer_func(Xa, Xb) utils.plot_samples(Xa, Xb, M, mean, prefix=join(infer_dir, 'infer_%d' % i))
def load_check_point(train_id, path=None): """ """ if path is None: path = os.getcwd() param_fn = os.path.join(path, str(train_id) + '.param.npz') config_fn = os.path.join(path, str(train_id) + '.nnconfig.gz') params = joblib.load(config_fn) mdl, net = build(network(params), params) layers = L.get_all_layers(L.ConcatLayer(net.values(), axis=1)) if os.path.exists(param_fn): try: print('Loadong pre-trained weight...') with np.load(param_fn) as f: param_values = [f['arr_%d' % i] for i in range(len(f.files))] L.set_all_param_values(layers, param_values) except Exception as e: print(e) print('Cannot load parameters!') else: print('Cannot find parameters!') return net, mdl, params
def loadNetwork(self,filename='Weights.pkl'): """ load the network weights and build the corresponding network """ thefile=open(filename,'r') oldvals=cPickle.load(thefile) layers.set_all_param_values(self.l_out,oldvals)
def load_model(self, load_path): with open(load_path, 'r') as f: data = pkl.load(f) L.set_all_param_values(self.network, data) for item in self.trackers: data = pkl.load(f) L.set_all_param_values(item, data)
def load(filename, game=None, config_file=None, quiet=False): if not quiet: print "Loading qengine from " + filename + "..." params = pickle.load(open(filename, "rb")) qengine_args = params[0] network_weights = params[1] steps = qengine_args["steps"] epsilon = qengine_args["epsilon"] del (qengine_args["epsilon"]) del (qengine_args["steps"]) if game is None: if config_file is not None: game = initialize_doom(config_file) qengine_args["config_file"] = config_file elif "config_file" in qengine_args and qengine_args["config_file"] is not None: game = initialize_doom(qengine_args["config_file"]) else: raise Exception("No game, no config file. Dunno how to initialize doom.") else: qengine_args["config_file"] = None qengine_args["game"] = game qengine = QEngine(**qengine_args) set_all_param_values(qengine.approximator.network, network_weights) set_all_param_values(qengine.approximator.frozen_network, network_weights) if not quiet: print "Loading finished." qengine.steps = steps qengine.epsilon = epsilon return qengine
def load_weights(layer, filename): """ Load network weights from either a pickle or a numpy file and set the parameters of all layers below layer (including the layer itself) to the given values. Parameters ---------- layer : Layer The :class:`Layer` instance for which to set all parameter values filename : str with ending .pkl or .npz """ if filename.endswith('.npz'): with np.load(filename) as f: param_values = [f['arr_%d' % i] for i in range(len(f.files))] set_all_param_values(layer, param_values) return if filename.endswith('.pkl'): with open(filename) as handle: model = pickle.load(handle) set_all_param_values(layer, model['param values']) return raise NotImplementedError('Format of {filename} not known'.format( filename=filename))
def get_ae_fn( include_dec = None ): x = T.tensor3( 'input' ) encoding, decoding = cnn( x, config.input_length, config.output_length ) print 'Loading parameters' with np.load( config.init_model ) as f: param_values = [ f['arr_%d' % i] for i in range( len( f.files ) ) ] set_all_param_values( decoding, param_values ) prediction = get_output( encoding, deterministic = True ) encode_fn = function( [x], prediction, allow_input_downcast = True ) decode_fn = None if include_dec: y = T.matrix( 'output' ) error = squared_error( y, prediction ) decode_fn = function( [x,y], [prediction, error], \ allow_input_downcast = True ) return encode_fn, decode_fn 1,0-1 Top
def load_vgg19(pkl_filename="vgg19_normalized.pkl"): net = {} net['input'] = InputLayer((1, 3, None, None)) net['conv1_1'] = ConvLayer(net['input'], 64, 3, pad=1) net['conv1_2'] = ConvLayer(net['conv1_1'], 64, 3, pad=1) net['pool1'] = PoolLayer(net['conv1_2'], 2, mode='average_exc_pad') net['conv2_1'] = ConvLayer(net['pool1'], 128, 3, pad=1) net['conv2_2'] = ConvLayer(net['conv2_1'], 128, 3, pad=1) net['pool2'] = PoolLayer(net['conv2_2'], 2, mode='average_exc_pad') net['conv3_1'] = ConvLayer(net['pool2'], 256, 3, pad=1) net['conv3_2'] = ConvLayer(net['conv3_1'], 256, 3, pad=1) net['conv3_3'] = ConvLayer(net['conv3_2'], 256, 3, pad=1) net['conv3_4'] = ConvLayer(net['conv3_3'], 256, 3, pad=1) net['pool3'] = PoolLayer(net['conv3_4'], 2, mode='average_exc_pad') net['conv4_1'] = ConvLayer(net['pool3'], 512, 3, pad=1) net['conv4_2'] = ConvLayer(net['conv4_1'], 512, 3, pad=1) net['conv4_3'] = ConvLayer(net['conv4_2'], 512, 3, pad=1) net['conv4_4'] = ConvLayer(net['conv4_3'], 512, 3, pad=1) net['pool4'] = PoolLayer(net['conv4_4'], 2, mode='average_exc_pad') net['conv5_1'] = ConvLayer(net['pool4'], 512, 3, pad=1) net['conv5_2'] = ConvLayer(net['conv5_1'], 512, 3, pad=1) net['conv5_3'] = ConvLayer(net['conv5_2'], 512, 3, pad=1) net['conv5_4'] = ConvLayer(net['conv5_3'], 512, 3, pad=1) net['pool5'] = PoolLayer(net['conv5_4'], 2, mode='average_exc_pad') if bytes == str: # Python 2 values = pickle.load(open(pkl_filename, 'rb'))['param values'] else: #Python 3 values = pickle.load(open(pkl_filename, 'rb'), encoding='latin1')['param values'] set_all_param_values(net['pool5'], values[:32]) return net
def load_weights_pickle(self): with open(self.weights_dir, 'rb') as f: print 'Loading weights from {0:s}...\n'.format(self.weights_dir) param_values = pickle.load(f) print 'Setting the weights to the model...\n' set_all_param_values(self.net['output'], param_values, trainable=True)
def set_param_values(self, param_values): [cnn_params_vals, nn_params_vals] = param_values set_all_param_values(self.cnn, cnn_params_vals) set_all_param_values(get_all_layers([self.mean_nn, self.cov_nn]), nn_params_vals)
def update_target(): updates = [] theta = get_all_param_values(q_) theta_target = get_all_param_values(q_target_) for p, p_target in zip(*(theta, theta_target)): updates.append(learning_tau * p + (1 - learning_tau) * p_target) set_all_param_values(q_target_, updates)
def use_best_param(self): L.set_all_param_values(self.network, self.best_param) self.curr_epoch = self.best_epoch # Remove the network_train_info enries newer than self.best_epoch del self.network_train_info[0][self.best_epoch:] del self.network_train_info[1][self.best_epoch:] del self.network_train_info[2][self.best_epoch:]
def __init__(self, istrained, name=None, args=None): self.istrained = istrained self.X = T.tensor4('X') self.y = T.ivector('y') self.outprob = build_model(self.X) if self.istrained: params = cPickle.load(open(dataset_path + 'plain_cnn.pkl', 'r')) layers.set_all_param_values(self.outprob, params) self.yFullProb = layers.get_output(self.outprob, deterministic=True) self.predfn = makeFunc([self.X, ], [self.yFullProb, ], None) else: self.lr, self.C, self.momentum = args self.params = layers.get_all_params(self.outprob, trainable=True) reg = regularization.regularize_network_params(self.outprob, regularization.l2) reg /= layers.helper.count_params(self.outprob) # 训练集 self.yDropProb = layers.get_output(self.outprob) trCrossentropy = objectives.categorical_crossentropy(self.yDropProb, self.y) self.trCost = trCrossentropy.mean() + self.C * reg # 验证、测试集 self.yFullProb = layers.get_output(self.outprob, deterministic=True) vateCrossentropy = objectives.categorical_crossentropy(self.yFullProb, self.y) self.vateCost = vateCrossentropy.mean() + self.C * reg # 训练函数,输入训练集,输出训练损失和误差 updatesDict = updates.nesterov_momentum(self.trCost, self.params, self.lr, self.momentum) self.trainfn = makeFunc([self.X, self.y], [self.trCost, self.yDropProb], updatesDict) # 验证或测试函数,输入验证或测试集,输出损失和误差,不进行更新 self.vatefn = makeFunc([self.X, self.y], [self.vateCost, self.yFullProb], None)
def loadNetwork(self, filename='Weights.pkl'): """ load the network weights and build the corresponding network """ thefile = open(filename, 'r') oldvals = cPickle.load(thefile) layers.set_all_param_values(self.l_out, oldvals)
def load_model(self, model_name, logger=logger_RNNtools): if self.network is not None: try: logger.info("Loading stored model...") # restore network weights with np.load(model_name) as f: param_values = [ f['arr_%d' % i] for i in range(len(f.files)) ] L.set_all_param_values(self.network_lout, *param_values) # # restore 'updates' training parameters # with np.load(model_name + "_updates.npz") as f: # updates_values = [f['arr_%d' % i] for i in range(len(f.files))] # for p, value in zip(self.updates.keys(), updates_values): # p.set_value(value) logger.info("Loading parameters successful.") return 0 except IOError as e: print(os.strerror(e.errno)) logger.warning('Model: {} not found. No weights loaded'.format( model_name)) return -1 else: raise IOError( 'You must build the network before loading the weights.') return -1
def fit(self, time_series, test_size = 0.4): self.ar.fit(time_series, test_size = test_size) set_all_param_values(self.l_, get_all_param_values(self.ar.l_))
def init_cnn(model_file, hidden_units, num_filters, filter_hs, dropout_rate, n_words, n_dim): """ initializes CNN by loading weights of a previously trained model. note that the model trained and this model need to have same parameters. see trainCNN.py for explanation of neural network architecture :param model_file: :param hidden_units: :param num_filters: :param filter_hs: :param dropout_rate: :param n_words: :param n_dim: :return: """ assert len(num_filters) == len(filter_hs) filter_shapes = [] pool_sizes = [] for filter_h in filter_hs: filter_shapes.append((filter_h, n_dim)) pool_sizes.append((n_words - filter_h + 1, 1)) l_in = LL.InputLayer(shape=(None, 1, n_words, n_dim)) layer_list = [] for i in range(len(filter_hs)): l_conv = LL.Conv2DLayer(l_in, num_filters=num_filters[i], filter_size=filter_shapes[i], nonlinearity=L.nonlinearities.rectify, W=L.init.HeNormal(gain='relu')) l_pool = LL.MaxPool2DLayer(l_conv, pool_size=pool_sizes[i]) layer_list.append(l_pool) mergedLayer = LL.ConcatLayer(layer_list) l_hidden1 = LL.DenseLayer(mergedLayer, num_units=hidden_units[0], nonlinearity=L.nonlinearities.tanh, W=L.init.HeNormal(gain='relu')) l_hidden1_dropout = LL.DropoutLayer(l_hidden1, p=dropout_rate[0]) l_hidden2 = LL.DenseLayer(l_hidden1_dropout, num_units=hidden_units[1], nonlinearity=L.nonlinearities.tanh, W=L.init.HeNormal(gain='relu')) l_hidden2_dropout = LL.DropoutLayer(l_hidden2, p=dropout_rate[1]) l_output = LL.DenseLayer(l_hidden2_dropout, num_units=hidden_units[2], nonlinearity=L.nonlinearities.tanh) net_output = theano.function([l_in.input_var], LL.get_output(l_output, deterministic=True)) with np.load(model_file) as f: param_values = [f['arr_%d' % i] for i in range(len(f.files))] LL.set_all_param_values(l_output, param_values) return net_output
def loadParams(epoch, filename=None): print "IMPORTING MODEL PARAMS...", net_filename = MODEL_PATH + filename with open(net_filename, 'rb') as f: params = pickle.load(f) l.set_all_param_values(NET, params) print "DONE!"
def __init__(self, video_base_path, show_visualization=True, show_plots=True): self.video_base_path = video_base_path self.show_visualization = show_visualization self.show_plots = show_plots self.pad_context = 0.5 self.input_size = 101 #input size of network self.label_size = 61 # build network print 'building model...' x, y = T.tensor4(), T.tensor4() pnet, _, net, _ = build_fft_scale(x, y, 31) params = ll.get_all_params(net) print params #params_path = '/home/elliott/hezhping/correlation_net/model_hanning_Gaussian.npz' params_path = '/home/hp1/HeZhangPing/correlation_net/model_101label.npz' global param_values with np.load(params_path) as f: param_values = [f['arr_%d' % i] for i in range(len(f.files))] ll.set_all_param_values([pnet, net], param_values) pre = ll.get_output(net, deterministic=True) self.pre_f = theano.function([x, y], pre)
def build_segmentation_function(param_file, num_classes=8): input_var = T.tensor4('x') softmax, net, net_crf, _ = build_network(input_var, num_classes) # Initialize with pretrained weights with np.load(param_file) as f: param_values = [f['arr_%d' % i] for i in xrange(len(f.files))] nn.set_all_param_values(softmax, param_values) # Get the output of the networks. output, output_crf = nn.get_output([net, net_crf], deterministic=True) # Process info for network path with & without CRF. The output of the # network is stored in 'n' different channels, where n represents the # number of unique classes in the dataset. To make a prediction, we take # the most probable class using argmax shape = (output.shape[2], output.shape[3]) output = T.argmax(output, axis=1).reshape(shape) output_crf = T.argmax(output_crf, axis=1).reshape(shape) # Compile the function return theano.function([input_var], [output, output_crf], allow_input_downcast=True)
def load_vgg16(path_to_pkl): net = build_model() output_layer = net['prob'] with open(path_to_pkl, 'rb') as f: params = pickle.load(f) MEAN_IMAGE = params['mean value'] set_all_param_values(output_layer, params['param values']) return (output_layer, MEAN_IMAGE)
def restore(self, path): """ Load the weights """ with np.load(path) as f: saved_params_values = [ f['arr_%d' % i] for i in range(len(f.files)) ] set_all_param_values(self.output_layer, saved_params_values)
def load_embedder(self, save_dir): if not self.embed_network: raise Exception('Must build model before loading embedding values') filename = os.path.join(save_dir, 'embedder_values.save') with open(filename, 'rb') as f: values = cPickle.load(f) L.set_all_param_values(self.embed_network, values)
def load_params(self, folder): print 'reading model from %s' % folder # with np.load(os.path.join(folder, 'tagger.npz')) as f: # param_values = [f['arr_%d' %i] for i in range(len(f.files))] # L.set_all_param_values(self.taggers.values(), param_values) with np.load(os.path.join(folder, 'tagger_avg.npz')) as f: param_values = [f['arr_%d' % i] for i in range(len(f.files))] L.set_all_param_values(self.taggers_avg.values(), param_values)
def load_model(network, file_name, directory='models'): file_path = directory + '/' + file_name with open(file_path, 'r') as load_file: dict = pickle.load(load_file) layers.set_all_param_values(network, dict['params']) return {'epoch': dict['epoch'], 'learning_rate': dict['learning_rate']}
def set_param_values(self, param_values): """Set the parameters of the model""" [rnn_params_vals, canvas_update_params_vals] = param_values set_all_param_values(self.rnn, rnn_params_vals) for i in range(len(self.canvas_update_params)): self.canvas_update_params[i].set_value( canvas_update_params_vals[i])
def load(fp): ''' returns the BaseModel from the given fp ''' params, last_layer, compile_kwargs = pkl.load(open(fp)) model = BaseModel(last_layer) layers.set_all_param_values(model.last_layer, params) model.compile(**compile_kwargs) return model
def load_weights(self): with open(self.model_load_path, 'rb') as f: loaded_file = np.load(f) # Just using .values() would't work here because we need to keep the order of elements ordered_params = [ loaded_file['arr_%d' % i] for i in xrange(len(loaded_file.files)) ] set_all_param_values(self._net['dist'], ordered_params)
def load_params(self, filename, quiet=False): if not quiet: print "Loading network weights from " + filename + "..." params = pickle.load(open(filename, "rb")) set_all_param_values(self.approximator.network, params) set_all_param_values(self.approximator.frozen_network, params) if not quiet: print "Loading finished."
def loadParams(net, params): log.p('IMPORTING MODEL PARAMS...', new_line=False) l.set_all_param_values(net, params) log.p('DONE!') return net
def train(self, X_train1, X_train2, y_train, X_val1, X_val2, y_val, max_epochs=500, patience=20): def train_batch(batch_data): return self.train_fn(*batch_data) def val_batch(batch_data): return self.val_fn(*batch_data) best_weights, run = train( [X_train1, X_train2, y_train], [X_val1, X_val2, y_val], train_batch, val_batch, self.network, max_epochs=max_epochs, patience=patience) layers.set_all_param_values(self.network, best_weights) return run
def load_network(prototypes, network_path = 'network_eyes.npy'): """ Loads a saved network :param prototypes: :param network_path: :return: """ network = create_network(prototypes) layers.set_all_param_values(network, np.load(network_path)) return network
def setup_kp_network(network_str): fn = KP_NETWORK_OPTIONS[network_str]['url'] file_url = join('https://lev.cs.rpi.edu/public/models/', fn) network_params_path = ut.grab_file_url(file_url, appname='ibeis') network_params = ut.load_cPkl(network_params_path) # network_params also includes normalization constants needed for the dataset, and is assumed to be a dictionary # with keys mean, std, and params network_exp = KP_NETWORK_OPTIONS[network_str]['exp']() ll.set_all_param_values(network_exp, network_params['params']) X = T.tensor4() network_fn = tfn([X], ll.get_output(network_exp, X, deterministic=True)) return {'mean': network_params['mean'], 'std': network_params['std'], 'networkfn': network_fn, 'input_size': KP_NETWORK_OPTIONS[network_str]['size']}
def insert_weights(self, regr): ''' In order the following operations are done: - Update mask main part: activate another node - Copy the 'new_node' weights in the main part of the net - Copy the regr weights in the 'new_node' part - Recompile the net Structure of parameters: - W1: (num_classes*num_filters1*num_nodes, num_inputs, filter_length1) - b1: (num_classes*num_filters1*num_nodes, ) - W2: (num_classes, num_classes*num_filters1*num_nodes, filter_length2) - b2: (num_classes,) ''' # ------------------ # Update mask: # ------------------ self.net.layers_['mask'].add_node() actNode = self.net.layers_['mask'].active_nodes self.active_nodes = actNode # ------------------ # Get weights: # ------------------ W1, b1, maskParam, W2, b2 = layers.get_all_param_values(self.net.layers_['conv2']) newNode_W1, newNode_b1, newNode_W2, newNode_b2 = layers.get_all_param_values(self.net.layers_['conv2_newNode']) reg_W1, reg_b1, reg_W2, reg_b2 = layers.get_all_param_values(regr.net.layers_['conv2']) # boost_const = self.net.layers_['boosting_merge'].boosting_constant.get_value() # -------------------- # Update main part: # -------------------- if actNode>0: nNodes = self.num_filters1 ### ReLU MOD start = nNodes*(actNode-1) stop = nNodes*actNode slice_weights = slice(start,stop) W1[slice_weights,:,:], b1[slice_weights] = newNode_W1, newNode_b1 # For the moment I don't touch b2... Not sure about this... W2[:,slice_weights,:], b2 = newNode_W2, b2+newNode_b2 layers.set_all_param_values(self.net.layers_['conv2'], [W1, b1, maskParam, W2, b2]) # -------------------- # Insert new node: # -------------------- newNode_W1, newNode_b1, newNode_W2, newNode_b2 = reg_W1, reg_b1, reg_W2, reg_b2 layers.set_all_param_values(self.net.layers_['conv2_newNode'], [newNode_W1, newNode_b1, newNode_W2, newNode_b2])
def setup_te_network(network_str): fn = TE_NETWORK_OPTIONS[network_str]['url'] file_url = join('https://lev.cs.rpi.edu/public/models/', fn) network_params_path = ut.grab_file_url(file_url, appname='ibeis') network_params = ut.load_cPkl(network_params_path) # network_params also includes normalization constants needed for the dataset, and is assumed to be a dictionary # with keys mean, std, and params network_exp = TE_NETWORK_OPTIONS[network_str]['exp']() ll.set_all_param_values(network_exp, network_params['params']) X = T.tensor4() network_fn = tfn([X], ll.get_output( network_exp[-1], X, deterministic=True)) retdict = {'mean': network_params['mean'], 'std': network_params[ 'std'], 'networkfn': network_fn} if any([i in network_str for i in ('upsample', 'jet')]): retdict['mod_acc'] = 8 return retdict
def load_model(network, file_name, directory = 'models'): """ Loades saved network and returns dictonary with keys epoch, learning_rate Loades from root_dir/models/file_name """ file_path = directory + '/' + file_name print "==> Loading model from %s" % file_path with open(file_path, 'r') as load_file: dict = pickle.load(load_file) layers.set_all_param_values(network, dict['params']) return { 'epoch' : dict['epoch'], 'learning_rate' : dict['learning_rate'] }
def test_set_all_param_values(self): from lasagne.layers import (InputLayer, DenseLayer, set_all_param_values) from lasagne.utils import floatX l1 = InputLayer((10, 20)) l2 = DenseLayer(l1, 30) l3 = DenseLayer(l2, 40) a = floatX(numpy.random.normal(0, 1, (1, 1))) b = floatX(numpy.random.normal(0, 1, (1,))) set_all_param_values(l3, [a, b, a, b]) assert numpy.allclose(l3.W.get_value(), a) assert numpy.allclose(l3.b.get_value(), b) assert numpy.allclose(l2.W.get_value(), a) assert numpy.allclose(l2.b.get_value(), b) with pytest.raises(ValueError): set_all_param_values(l3, [a, b, a])
def _load(self): net = self._build_model(self.input_size) model_data = pickle.load(open(self.model_filename)) values = model_data['param values'] layers.set_all_param_values(net['prob'], values) if "mean value" in model_data: mean_value = (model_data["mean value"]) else: mean_value = np.array([104.0, 116.0, 122.0]) self.mean_value = mean_value X = T.tensor4() layer_values = [layers.get_output(net[layer], X) for layer in self.layer_names] self._predict_layers = theano.function([X], layer_values) self._loaded = True self.all_layer_names = net.keys() self._net = net
def reinitiate_set_params(network, weights = None): # change weights of a trained network to a random set or a user defined value # useful in case of big networks and cross validation # instead of the long time of recompiling you can just # re-init the network weights if not weights: old = get_all_param_values(network) weights = [] for layer in old: shape = layer.shape if len(shape)<2: shape = (shape[0], 1) W= GlorotUniform()(shape) if W.shape != layer.shape: W = np.squeeze(W, axis= 1) weights.append(W) set_all_param_values(network, weights) return network
def insert_weights(self, boostedPerceptron): ''' In order the following operations are done: - Update mask main part: activate another perceptron - Copy the boostedPerceptron weights in the greedyLayer Structure of parameters: (check lasagne doc) - W1: (num_classes*num_filters1*num_nodes, num_inputs, filter_length1) - b1: (num_classes*num_filters1*num_nodes, ) - W2: (num_classes, num_classes*num_filters1*num_nodes, filter_length2) - b2: (num_classes,) ''' # ------------------ # Update mask: # ------------------ self.net.layers_['mask'].add_perceptron() self.active_perceptrons = self.net.layers_['mask'].active_perceptrons # ------------------ # Get weights: # ------------------ all_net_params = layers.get_all_param_values(self.net.layers_['greedyConv_2']) W1, b1, maskParam, W2, b2 = all_net_params[-5:] perc_W1, perc_b1, perc_W2, perc_b2 = layers.get_all_param_values(boostedPerceptron.net.layers_['greedyConv_2'])[-4:] # -------------------- # Update main part: # -------------------- start = self.nodes_partition[self.active_perceptrons-1] stop = self.nodes_partition[self.active_perceptrons] slice_weights = slice(start,stop) # !!! For the moment I don't touch b2... !!! # b1[slice_weights] = perc_b1 if self.layer_type=="conv": W1[slice_weights,:,:] = perc_W1 W2[:,slice_weights,:] = perc_W2 if self.layer_type=="trans_conv": W1[:,slice_weights,:] = perc_W1 W2[slice_weights,:,:] = perc_W2 layers.set_all_param_values(self.net.layers_['greedyConv_2'], all_net_params[:-5] + [W1, b1, maskParam, W2, b2])
def test_batch_size(): input_var01, input_var16 = T.tensor3s('input01', 'input16') l_output01 = model(input_var01, batch_size=1) l_output16 = model(input_var16, batch_size=16) # Share the parameters for both models params01 = get_all_param_values(l_output01) set_all_param_values(l_output16, params01) posterior_fn01 = theano.function([input_var01], get_output(l_output01)) posterior_fn16 = theano.function([input_var16], get_output(l_output16)) example_input = np.random.rand(16, 30, 8) example_output16 = posterior_fn16(example_input) example_output01 = np.zeros_like(example_output16) for i in range(16): example_output01[i] = posterior_fn01(example_input[i][np.newaxis, :, :]) assert example_output16.shape == (16, 30, 8) assert np.allclose(example_output16, example_output01, atol=1e-3)
def test_set_all_param_values(self): from lasagne.layers import (InputLayer, DenseLayer, set_all_param_values) from lasagne.utils import floatX l1 = InputLayer((10, 20)) l2 = DenseLayer(l1, 30) l3 = DenseLayer(l2, 40) a2 = floatX(numpy.random.normal(0, 1, (20, 30))) b2 = floatX(numpy.random.normal(0, 1, (30,))) a3 = floatX(numpy.random.normal(0, 1, (30, 40))) b3 = floatX(numpy.random.normal(0, 1, (40,))) set_all_param_values(l3, [a2, b2, a3, b3]) assert numpy.allclose(l3.W.get_value(), a3) assert numpy.allclose(l3.b.get_value(), b3) assert numpy.allclose(l2.W.get_value(), a2) assert numpy.allclose(l2.b.get_value(), b2) with pytest.raises(ValueError): set_all_param_values(l3, [a3, b3, a2]) with pytest.raises(ValueError): a3_bad = floatX(numpy.random.normal(0, 1, (25, 40))) set_all_param_values(l3, [a2, b2, a3_bad, b3])
def load(cls, prefix, prod=True): import json with open(prefix + "_config.json") as data: params = json.load(data) network = cls( # positional params params.get('emb_dim', defs['emb_dim']), params.get('rnn_dim', defs['rnn_dim']), params.get('hid_dim', defs['hid_dim']), params['vocab_size'], params.get('context', defs['context']), # keyword params prod=prod, cell=params.get('cell', defs['cell']), add_dense=params.get('add_dense', defs['add_dense']), depths=params.get('depth', defs['depth']), # kwarg params **params.get('cell_args', {})) with np.load(prefix + '_weights.npz') as data: params = [data['arr_%d' % i] for i in range(len(data.files))] set_all_param_values(network.output, params) return network
def __init__(self, rc): # steal stuff from world/initialize self.rc = rc self.world = mj.MJCWorld(rc['model_file']) self.model = self.world.get_model() self.dX = self.model['nq'] + self.model['nv'] self.dU = self.model['nu'] # compute dO # TODO: put this somewhere else, reorganize observation module dO = 0 ndims = np.sum(rc['obs_dims']) fields = rc['obs_fields'] if 'qpos' in fields: dO += self.model['nq'] if 'qvel' in fields: dO += self.model['nv'] if 'xipos' in fields: dO += ndims*(self.model['nbody'] - 1) if 'ximat' in fields: dO += ndims*ndims*(self.model['nbody'] - 1) if 'site_xpos' in fields: dO += ndims*self.model['nsite'] if 'to_target' in fields: dO += ndims self.dO = dO # TODO: start with synthetic data included/reorganize buffer storage self.buf = ReplayBuffer(self.dO, self.dU) # create nets and copy train net to target net self.net = ActorCriticNet(self.dO, self.dU, rc['num_units'], rc['ctrl_limits']) self.target_net = ActorCriticNet(self.dO, self.dU, rc['num_units'], rc['ctrl_limits']) values = ll.get_all_param_values(self.net.critic) ll.set_all_param_values(self.target_net.critic, values) # compile all the theano functions self._compile()
def build_vgg16_class(): net = {} net['input'] = ll.InputLayer((None, 3, 224, 224), name='inp') net['conv1_1'] = ll.Conv2DLayer(net['input'], 64, 3, pad='same', name='conv1') net['drop1'] = ll.DropoutLayer(net['conv1_1'], p=0.5) net['conv1_2'] = ll.Conv2DLayer(net['drop1'], 64, 3, pad='same', name='conv2') net['pool1'] = ll.Pool2DLayer(net['conv1_2'], 2) net['conv2_1'] = ll.Conv2DLayer(net['pool1'], 128, 3, pad='same') net['drop2'] = ll.DropoutLayer(net['conv2_1'], p=0.5) net['conv2_2'] = ll.Conv2DLayer(net['drop2'], 128, 3, pad='same') net['pool2'] = ll.Pool2DLayer(net['conv2_2'], 2) net['conv3_1'] = ll.Conv2DLayer(net['pool2'], 256, 3, pad='same') net['drop3'] = ll.DropoutLayer(net['conv3_1'], p=0.5) net['conv3_2'] = ll.Conv2DLayer(net['drop3'], 256, 3, pad='same') net['conv3_3'] = ll.Conv2DLayer(net['conv3_2'], 256, 3, pad='same') net['drop4'] = ll.DropoutLayer(net['conv3_3'], p=0.5) net['pool3'] = ll.Pool2DLayer(net['drop4'], 2) net['conv4_1'] = ll.Conv2DLayer(net['pool3'], 512, 3, pad='same') net['conv4_2'] = ll.Conv2DLayer(net['conv4_1'], 512, 3, pad='same') net['drop5'] = ll.DropoutLayer(net['conv4_2'], p=0.5) net['conv4_3'] = ll.Conv2DLayer(net['drop5'], 512, 3, pad='same') net['pool4'] = ll.Pool2DLayer(net['conv4_3'], 2) net['conv5_1'] = ll.Conv2DLayer(net['pool4'], 512, 3, pad='same') net['conv5_2'] = ll.Conv2DLayer(net['conv5_1'], 512, 3, pad='same') net['conv5_3'] = ll.Conv2DLayer(net['conv5_2'], 512, 3, pad='same') net['pool5'] = ll.Pool2DLayer(net['conv5_3'], 2) net['fc6'] = ll.DenseLayer(net['pool5'], num_units=4096) net['fc7'] = ll.DenseLayer(net['fc6'], num_units=4096) net['fc8'] = ll.DenseLayer(net['fc7'], num_units=1000, nonlinearity=None) net['prob'] = ll.NonlinearityLayer(net['fc8'], nonlinearity=softmax) # load parameters param_file = join(dataset_loc, "vgg16.pkl") with open(param_file, 'r') as f: params = pickle.load(f) ll.set_all_param_values(net['fc8'], params['param values']) return net
def build_vgg16_seg(): net = {} net['input'] = ll.InputLayer((None, 3, None, None), name='inp') net['conv1_1'] = ll.Conv2DLayer(net['input'], 64, 3, pad='same', name='conv1') net['drop1'] = ll.DropoutLayer(net['conv1_1'], p=0.5) net['conv1_2'] = ll.Conv2DLayer(net['drop1'], 64, 3, pad='same', name='conv2') #net['pool1'] = ll.Pool2DLayer(net['conv1_2'], 2) net['conv2_1'] = ll.Conv2DLayer(net['conv1_2'], 128, 3, pad='same') net['drop2'] = ll.DropoutLayer(net['conv2_1'], p=0.5) net['conv2_2'] = ll.Conv2DLayer(net['drop2'], 128, 3, pad='same') #net['pool2'] = ll.Pool2DLayer(net['conv2_2'], 2) net['conv3_1'] = ll.Conv2DLayer(net['conv2_2'], 256, 3, pad='same') net['drop3'] = ll.DropoutLayer(net['conv3_1'], p=0.5) net['conv3_2'] = ll.Conv2DLayer(net['drop3'], 256, 3, pad='same') net['conv3_3'] = ll.Conv2DLayer(net['conv3_2'], 256, 3, pad='same') net['drop4'] = ll.DropoutLayer(net['conv3_3'], p=0.5) #net['pool3'] = ll.Pool2DLayer(net['conv3_3'], 2) net['conv4_1'] = ll.Conv2DLayer(net['drop4'], 512, 3, pad='same') net['conv4_2'] = ll.Conv2DLayer(net['conv4_1'], 512, 3, pad='same') net['drop5'] = ll.DropoutLayer(net['conv4_2'], p=0.5) net['conv4_3'] = ll.Conv2DLayer(net['drop5'], 512, 3, pad='same') #net['pool4'] = ll.Pool2DLayer(net['conv4_3'], 2) net['conv5_1'] = ll.Conv2DLayer(net['conv4_3'], 512, 3, pad='same') net['conv5_2'] = ll.Conv2DLayer(net['conv5_1'], 512, 3, pad='same') net['conv5_3'] = ll.Conv2DLayer(net['conv5_2'], 512, 3, pad='same') #net['pool5'] = ll.Pool2DLayer(net['conv5_3'], 2) #net['fc6'] = ll.DenseLayer(net['pool5'], num_units=4096) #net['fc7'] = ll.DenseLayer(net['fc6'], num_units=4096) #net['fc8'] = ll.DenseLayer(net['fc7'], num_units=1000, nonlinearity=None) #net['prob'] = ll.NonlinearityLayer(net['fc8'], softmax) # load parameters param_file = join(dataset_loc, "vgg16.pkl") params = ut.load_cPkl(param_file) ll.set_all_param_values(net['conv5_3'], params['param values'][:26]) return net
def load(game, filename, quiet=False): if not quiet: print "Loading qengine from " + filename + "..." params = pickle.load(open(filename, "rb")) qengine_args = params[0] network_params = params[1] steps = qengine_args["steps"] epsilon = qengine_args["epsilon"] del (qengine_args["epsilon"]) del (qengine_args["steps"]) qengine_args["game"] = game qengine = QEngine(**qengine_args) set_all_param_values(qengine._evaluator.network, network_params) set_all_param_values(qengine._evaluator.frozen_network, network_params) if not quiet: print "Loading finished." qengine._steps = steps qengine._epsilon = epsilon return qengine
def load_model(self): if self.conf.verbosity > 1: print "Loading pretrained model from", self.conf.load_model with np.load(self.conf.load_model) as f: param_values = [f['arr_%d' % i] for i in range(len(f.files))] set_all_param_values(self.autoencoder, param_values)
def run(train_words, val_words, alignement_features, input_features, name='abnet', dim=40, nframes=7): data_train = sample_data.read_word_clusters_file(train_words) data_test = sample_data.read_word_clusters_file(val_words) data = [data_train, data_test] layer_size = 500 n_layers_init = 2 n_layers_end = 6 n_layers = n_layers_init architecture = [[dim*nframes] + [layer_size]*n_layers_init + [39], [nl.rectify]*n_layers_init + [nl.linear], [0] + [0.2]*n_layers_init] nnet = abnet2.ABnet(*architecture, loss_type='cosine_margin', margin=0.5) features_getter = sample_data.FeaturesAPI_cached(alignement_features) input_features_getter = sample_data.FeaturesAPI_cached(input_features) # Utility functions def train_fn(batch_data): return nnet.train_fn(*batch_data) def val_fn(batch_data): return nnet.val_fn(*batch_data) def train(data, train_fn, val_fn, network, max_epochs=4000, patience=100): (train_words, train_clusters), (test_words, test_clusters) = data run = [] best_model = None if patience <= 0: patience = max_epochs patience_val = 0 best_val = None for epoch in range(max_epochs): data_train = sample_data.generate_abnet_batch( train_words, train_clusters, epoch, features_getter, input_features_getter, return_indexes=False) data_val = sample_data.generate_abnet_batch( test_words, test_clusters, epoch, features_getter, input_features_getter, return_indexes=False) start_time = time.time() train_err, val_err = abnet2.train_iteration( data_train, data_val, train_fn, val_fn) if epoch % 20 == 0: run.append(layers.get_all_param_values(network)) if np.isnan(val_err) or np.isnan(train_err): print("Train error or validation error is NaN, " "stopping now.") break # Calculating patience if best_val == None or val_err < best_val: best_val = val_err patience_val = 0 best_model = layers.get_all_param_values(network) else: patience_val += 1 if patience_val > patience: print("No improvements after {} iterations, " "stopping now".format(patience)) break # Then we print the results for this epoch: print("Epoch {} of {} took {:.3f}s".format( epoch + 1, max_epochs, time.time() - start_time)) print(" training loss:\t\t{:.6f}".format(train_err)) print(" validation loss:\t\t{:.6f}".format(val_err)) acc = nnet.eer(*data_val) print(" score eer:\t\t{:.2f} %".format(acc)) auc = nnet.auc(*data_val) print(" score auc:\t\t{:.2f} %".format(auc)) return best_model, run # Training... for i in range(n_layers_end - n_layers_init): best_model, run = train(data, train_fn, val_fn, nnet.network) # saving model weights = best_model weights_file = '{}_{}_best_model.npz'.format(name, i) run_file = '{}_{}_run.h5'.format(name, i) tryremove(weights_file) tryremove(run_file) np.savez(weights_file, weights) for epoch, weights in enumerate(run): h5py.File(run_file).create_group(str(epoch * 2)) for i, w in enumerate(weights[::2]): h5py.File(run_file)[str(epoch * 2)].create_dataset(str(i), data=w) # adding layer if i < n_layers_end - n_layers_init - 1: n_layers += 1 W = lasagne.init.GlorotUniform().sample((layer_size, layer_size)) u = np.empty((layer_size,), dtype=np.float32) architecture = [[dim*nframes] + [layer_size]*n_layers + [39], [nl.rectify]*n_layers + [nl.linear], [0] + [0.2, 0.2, 0.2, 0.2]*n_layers] init_weights = best_model[:-2] + [W, u] + best_model[-2:] nnet = abnet2.ABnet(*architecture, loss_type='cosine_margin', margin=0.5) layers.set_all_param_values(nnet.network, init_weights) layers.set_all_param_values(nnet.network, best_model) h5features_file = name + '_embeddings.h5f' try: tryremove(h5features_file) shutil.copy(input_features, h5features_file) del h5py.File(h5features_file)['features']['features'] transform = nnet.evaluate embedding_size = architecture[0][-1] with h52np.H52NP(input_features) as f_in, \ np2h5.NP2H5(h5features_file) as f_out: inp = f_in.add_dataset('features', 'features', buf_size=10000) out = f_out.add_dataset( 'features', 'features', buf_size=10000, n_rows=inp.n_rows, n_columns=embedding_size, item_type=np.float32) for X in inp: X = X.astype(np.float32) emb_wrd = transform(X) out.write(emb_wrd) except: tryremove(h5features_file) raise
def main(trial_folder, model_filepath, batch_size=8, n_steps=10, plot_and_save=True): def interpolate(x, y, n_steps): step_size = 1.0/n_steps return [x + a*(y-x) for a in np.arange(0, 1+step_size, step_size)] def get_output_size(network, name): layer = [l for l in get_all_layers(network) if l.name == name] if len(layer): return get_output_shape(layer)[0][1] return 0 def generate_output(gen_fn, noise, noise_size, cond=[], cond_size=0): if len(cond) and cond_size: return gen_fn(noise.reshape(1, noise_size), cond.reshape(1, cond_size))[0, 0] else: return gen_fn(noise.reshape(1, noise_size))[0, 0] def plotting(ax, data, filepath=''): ax.imshow(data, aspect='auto', origin='bottom', cmap='gray') def plotting_and_save(ax, data, filepath): ax.imshow(data, aspect='auto', origin='bottom', cmap='gray') np.save(filepath, data) if plot_and_save: plot_fn = plotting_and_save else: plot_fn = plotting with open(os.path.join(trial_folder, 'args.txt'), 'r') as f: args = f.read().replace('\n', '') print(args) words = args.split(' ') g_arch = words[2] g_batch_norm = None noise_size = None i = 2 while i < len(words) - 1: if words[i] == '--gbn': g_batch_norm = int(words[i+1]) i += 2 else: i += 1 # load blank network and and use saved weights to update network parameters network = pkl.load( open(os.path.join(trial_folder, 'models/generator_blank.pkl'), "rb")) # instantiate noise and condition variables noise_size = get_output_size(network, 'g_in_noise') cond_size = get_output_size(network, 'g_in_condition') noise_var = None cond_var = None if noise_size > 0: noise_var = T.fmatrix('noise') if cond_var > 0: cond_var = T.fmatrix('condition') # create blank network given params network = build_generator( noise_var, noise_size, cond_var, cond_size, g_arch, g_batch_norm) with np.load(model_filepath) as f: parameters = [f['arr_%d' % i] for i in range(len(f.files))] set_all_param_values(network, parameters) # create batch of fixed noise vectors noise = floatX(np.random.rand(batch_size, noise_size)) # create interpolations between fixed noise vectors noise_interp = [ interpolate(noise[i], noise[i+1], n_steps) for i in range(batch_size-1)] # set proper generator function given conditional or unconditional if cond_size: # create batch of fixed condition vectors cond = floatX(np.eye(cond_size)) cond_interp = [ interpolate(cond[i], cond[i+1], n_steps) for i in range(len(cond)-1)] # create generator function gen_fn = theano.function( [noise_var, cond_var], get_output(network, deterministic=True)) else: # create generator function gen_fn = theano.function( [noise_var], get_output(network, deterministic=True)) print("Plotting fixed vectors") # plot noise vectors conditioned if applicable if cond_size: for i in range(batch_size): dim = int(np.sqrt(cond_size) + 1) fig, axes = plt.subplots(dim, dim, figsize=(16, 32)) axes = axes.flatten() [plot_fn(axes[j], generate_output(gen_fn, noise[i], noise_size, cond[j], cond_size), '{}/samples/z_{}_conditioned_{}.npy'.format(trial_folder, i, j)) for j in range(cond_size)] fig.tight_layout() fig.savefig('{}/images/z_{}_conditioned.png'.format( trial_folder, i)) plt.close('all') else: fig, axes = plt.subplots(1, batch_size, figsize=(16, 4)) [plot_fn(axes[i], generate_output(gen_fn, noise[i], noise_size), '{}/samples/z_{}.npy'.format(trial_folder, i)) for i in range(batch_size)] fig.tight_layout() fig.savefig('{}/images/z.png'.format(trial_folder)) plt.close('all') print("Plotting interpolations") # plot interpolations, MUST REFACTOR if cond_size: """ # fixed noise moving condition, find alternative visualization for i in range(batch_size): fig, axes = plt.subplots(cond_size-1, n_steps+1, figsize=(32, 32)) [plotting(axes[j, k], generate_output(gen_fn, noise[i], noise_size, cond_interp[j][k], cond_size)) for j in range(batch_size-1) for k in range(n_steps+1)] fig.tight_layout() fig.savefig( '{}/samples/z_{}_cond_interp.png'.format(trial_folder, i)) plt.close('all') plt.imsave('{}/samples/z_{}_cond_interp.png'.format(trial_folder, i), (samples.reshape(12, 12, alphabet_size, n_steps) .transpose(0, 2, 1, 3) .reshape(12*alphabet_size, 12*n_steps)), origin='bottom', cmap='gray') """ # fixed condition moving noise for i in range(batch_size): fig, axes = plt.subplots(batch_size-1, n_steps+1, figsize=(32, 12)) [plotting( axes[j, k], generate_output(gen_fn, noise_interp[j][k], noise_size, cond[i], cond_size), '{}/samples/z_{}_inter_{}_cond_{}.npy'.format(trial_folder, j, k, i)) for j in range(batch_size-1) for k in range(n_steps+1)] fig.tight_layout() fig.savefig( '{}/images/z_inter_cond_{}.png'.format(trial_folder, i)) plt.close('all') else: # moving noise fig, axes = plt.subplots(batch_size-1, n_steps+1, figsize=(32, 12)) [plot_fn( axes[j, k], generate_output(gen_fn, noise_interp[j][k], noise_size), '{}/samples/z_{}_inter_{}.npy'.format(trial_folder, j, k)) for j in range(batch_size-1) for k in range(n_steps+1)] fig.tight_layout() fig.savefig( '{}/images/z_inter.png'.format(trial_folder)) plt.close('all')
def set_model_parameters(self, all_param_values, layer='output'): """initialize network with all_param_values""" if layer not in self.network.keys(): print(layer+ ': not found in network') else: layers.set_all_param_values(self.network[layer], all_param_values)