60 Python code examples are found related to "
model load
".
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
Example 1
if net_name == 'squeezenet':
net = SqueezeNet.SqueezeNet('1_1')
if net_name == 'googlenet':
net = GoogLeNet.GoogLeNet()
if net_name == 'resnet50':
net = ResNet50.resnet50()
net = torch.nn.DataParallel(net).cuda()
if net_name == 'ssd':
net = SSD.build_ssd('test', 300, 21)
pars_path = '../weights/' + net_name + '.pth.tar'
feature_path = 'Feature_float/' + net_name
net.eval()
checkpoint = torch.load(pars_path)
net.load_state_dict(checkpoint['state_dict'])
layer_name, Features = structure_hook(net,net_name)
return net, layer_name, Features, feature_path
Example 2
def load_model_value(network, model_file):
if (os.path.isfile(model_file) == True):
loaded_var = pickle.load(open(model_file, 'rb'));
mu = loaded_var[0];
sigma = loaded_var[1];
param_values = loaded_var[2];
param_set = lasagne.layers.get_all_params(network);
for param, value in zip(param_set, param_values):
param.set_value(value);
lasagne.layers.set_all_param_values(network, param_values);
return mu, sigma;
else:
return None, None;
def load_model(model_path):
Loads a tensorflow model return a tf.Session running on the loaded model (the graph).
:param str model_path: It can be a `.pb` file or directory containing checkpoint files.
:return: tf.Session running the model graph.
if model_path is None:
raise ValueError("model_path must not be None")
if not os.path.exists(model_path):
raise ValueError("model_path must exist")
if os.path.isfile(model_path) and model_path.endswith(".pb"):
return _load_pb(model_path)
if os.path.isdir(model_path):
for f in os.listdir(model_path):
if f.endswith(".pb"):
return _load_pb(os.path.join(model_path, f))
return _load_ckpt(model_path)
Example 4
def load_pretrained_model(self, data_name, name):
assert data_name in dataset.__dict__, \
'No pretrained model for {}'.format(data_name)
data = dataset.__dict__[data_name]
fc = self.fc
if self.num_classes != data.classes:
self.fc = nn.Conv2d(
self.channels[-1], data.classes,
kernel_size=1, stride=1, padding=0, bias=True)
model_url = get_model_url(data, name)
except KeyError:
raise ValueError(
'{} trained on {} does not exist.'.format(data.name, name))
self.load_state_dict(model_zoo.load_url(model_url))
self.fc = fc
def load_single_model(self, single_model_obj):
LOGGER.info("It's a binary task, start to load single model")
feature_shape = len(self.header)
weight_dict = dict(single_model_obj.weight)
coef_ = np.zeros(feature_shape)
for idx, header_name in enumerate(self.header):
coef_[idx] = weight_dict.get(header_name)
embed_ = np.zeros((feature_shape, single_model_obj.embed_size))
for idx, header_name in enumerate(self.header):
i_embed = np.array(single_model_obj.embedding[header_name].weight)
embed_[idx] = i_embed
intercept_ = 0.0
if self.fit_intercept:
intercept_ = single_model_obj.intercept
self.model_weights = \
FactorizationMachineWeights(coef_, embed_, intercept_,
fit_intercept=self.fit_intercept)
return self
Example 6
def load_model_by_name(self, model_name, logits=False, input_range_type=1, pre_filter=lambda x:x):
:params logits: return logits(input of softmax layer) if True; return softmax output otherwise.
:params input_range_type: {1: [0,1], 2:[-0.5, 0.5], 3:[-1, 1]...}
if model_name not in ["cleverhans", 'cleverhans_adv_trained', 'carlini', 'densenet']:
raise NotImplementedError("Undefined model [%s] for %s." % (model_name, self.dataset_name))
self.model_name = model_name
model_weights_fpath = "%s_%s.keras_weights.h5" % (self.dataset_name, model_name)
model_weights_fpath = os.path.join('downloads/trained_models', model_weights_fpath)
if model_name in ["cleverhans", 'cleverhans_adv_trained']:
model = cleverhans_cifar10_model(logits=logits, input_range_type=input_range_type, pre_filter=pre_filter)
elif model_name == "carlini":
model = carlini_cifar10_model(logits=logits, input_range_type=input_range_type, pre_filter=pre_filter)
elif model_name == "densenet":
model = densenet_cifar10_model(logits=logits, input_range_type=input_range_type, pre_filter=pre_filter)
model_weights_fpath = get_densenet_weights_path(self.dataset_name)
print("\n===Defined TensorFlow model graph.")
model.load_weights(model_weights_fpath)
print ("---Loaded CIFAR-10-%s model.\n" % model_name)
return model
Example 7
status = False
model_name = self._dynamic_load_model()
logger.debug('Loading model %s', model_name)
self._loader_lock.acquire()
if not self._models.get(model_name):
interpreter = self._interpreter_for_model(model_name)
self._models[model_name] = interpreter
status = True
finally:
self._loader_lock.release()
self._end_read()
return status
Example 8
def load_glove_model(filename, embed_dim):
if os.path.exists(filename + ".cache"):
logger.info("Found cache. Loading...")
with open(filename + ".cache", "rb") as fp:
return pickle.load(fp)
embedding = {"mapping": dict(), "pool": []}
with open(filename) as f:
for i, line in enumerate(f):
line = line.rstrip("\n")
vocab_word, *vec = line.rsplit(" ", maxsplit=embed_dim)
assert len(vec) == 300, "Unexpected line: '%s'" % line
embedding["pool"].append(np.array(list(map(float, vec)), dtype=np.float32))
embedding["mapping"][vocab_word] = i
embedding["pool"] = np.stack(embedding["pool"])
with open(filename + ".cache", "wb") as fp:
pickle.dump(embedding, fp)
return embedding
Example 9
def load_model(symbol_file, param_file, logger=None):
cur_path = os.path.dirname(os.path.realpath(__file__))
symbol_file_path = os.path.join(cur_path, symbol_file)
if logger is not None:
logger.info('Loading symbol from file %s' % symbol_file_path)
symbol = mx.sym.load(symbol_file_path)
param_file_path = os.path.join(cur_path, param_file)
if logger is not None:
logger.info('Loading params from file %s' % param_file_path)
save_dict = nd.load(param_file_path)
arg_params = {}
aux_params = {}
for k, v in save_dict.items():
tp, name = k.split(':', 1)
if tp == 'arg':
arg_params[name] = v
if tp == 'aux':
aux_params[name] = v
return symbol, arg_params, aux_params
def loadModel(self, tag, path, textures, matrix=None):
if not matrix:
matrix = euclid.Matrix4()
m = mesh.MeshObj(path)
m.load()
entry = ModelEntry(m, matrix)
for sampler, image in textures.items():
entry.textureMap.setTexture(sampler, image)
old = self.models.get(tag)
if old:
old.free()
self.models[tag] = entry
return entry
Example 11
def loadModel(self, modelpath):
if self.mode == 'train':
print('[INFO]: load checkpoints from %s and %s' % (modelpath, modelpath.replace('h5', 'pkl')))
self.dqn_model.load_weights(modelpath)
data_dict = pickle.load(open(modelpath.replace('h5', 'pkl'), 'rb'))
self.max_score = data_dict['max_score']
self.epsilon = data_dict['epsilon']
self.num_iters = data_dict['num_iters']
self.replay_memory_record = data_dict['replay_memory_record']
else:
print('[INFO]: load checkpoints from %s' % modelpath)
self.dqn_model.load_weights(modelpath)
self.max_score = 0
self.epsilon = self.final_epsilon
self.num_iters = 0
self.replay_memory_record = deque()
Example 12
# model file name
filename = tt.arg.save_dir + '%s.pt' % (experiment or tt.arg.experiment or model.__class__.__name__.lower())
if postfix is not None:
filename = filename + '.%s' % postfix
# load model
global_step = 0
if os.path.exists(filename):
if best:
global_step, model_state, _best = torch.load(filename + '.best', map_location=lambda storage, loc: storage)
else:
global_step, model_state = torch.load(filename, map_location=lambda storage, loc: storage)
model.load_state_dict(model_state)
# update best stat
filename += '.best'
if os.path.exists(filename):
_, _, _best = torch.load(filename, map_location=lambda storage, loc: storage)
return global_step
Example 13
# Load dictionaries
with open(os.path.join(os.path.dirname(__file__), 'data','input_lang.index2word.json')) as f:
input_lang_index2word = json.load(f)
with open(os.path.join(os.path.dirname(__file__), 'data', 'input_lang.word2index.json')) as f:
input_lang_word2index = json.load(f)
with open(os.path.join(os.path.dirname(__file__), 'data', 'output_lang.index2word.json')) as f:
output_lang_index2word = json.load(f)
with open(os.path.join(os.path.dirname(__file__), 'data', 'output_lang.word2index.json')) as f:
output_lang_word2index = json.load(f)
# Reload existing checkpoint
model = Model(args, input_lang_index2word, output_lang_index2word, input_lang_word2index, output_lang_word2index)
if args.load_param:
model.loadModel(iter=num)
return model
Example 14
def load_pretrain_model(model, gmf_model, mlp_model, num_layers):
# MF embeddings
gmf_user_embeddings = gmf_model.get_layer('user_embedding').get_weights()
gmf_item_embeddings = gmf_model.get_layer('item_embedding').get_weights()
model.get_layer('mf_embedding_user').set_weights(gmf_user_embeddings)
model.get_layer('mf_embedding_item').set_weights(gmf_item_embeddings)
# MLP embeddings
mlp_user_embeddings = mlp_model.get_layer('user_embedding').get_weights()
mlp_item_embeddings = mlp_model.get_layer('item_embedding').get_weights()
model.get_layer('mlp_embedding_user').set_weights(mlp_user_embeddings)
model.get_layer('mlp_embedding_item').set_weights(mlp_item_embeddings)
# MLP layers
for i in xrange(1, num_layers):
mlp_layer_weights = mlp_model.get_layer('layer%d' %i).get_weights()
model.get_layer('layer%d' %i).set_weights(mlp_layer_weights)
# Prediction weights
gmf_prediction = gmf_model.get_layer('prediction').get_weights()
mlp_prediction = mlp_model.get_layer('prediction').get_weights()
new_weights = np.concatenate((gmf_prediction[0], mlp_prediction[0]), axis=0)
new_b = gmf_prediction[1] + mlp_prediction[1]
model.get_layer('prediction').set_weights([0.5*new_weights, 0.5*new_b])
return model
Example 15
----------
language_model_path : string
Specifies the absolute server-side path of the acoustic model file.
self.language_model_caslib, path_after_caslib, _ = caslibify(self.conn, language_model_path, task="load")
rt = self.conn.retrieve("langModel.lmImport",
_messagelevel='error',
table=dict(name=path_after_caslib, caslib=self.language_model_caslib),
casout=dict(replace=True, name=self.language_model_name,
caslib=self.language_model_caslib))
if rt.severity > 1:
self.language_model_caslib = None
for msg in rt.messages:
print(msg)
raise DLPyError("Failed to import the language model.")
def load_model(cls, path, cuda=False, rnn_type = 'gru', rnn_activation='tanh',
hidden_threshold = 0,
qh_i = 0, qh_f = 0,
qi_i = 0, qi_f = 0,
bias = False,
bn_factor = False,
bn_bias = False,
bn_weights = False):
package = torch.load(path, map_location=lambda storage, loc: storage)
model = cls(rnn_hidden_size=package['hidden_size'], nb_layers=package['hidden_layers'],
labels=package['labels'], audio_conf=package['audio_conf'],
rnn_type=supported_rnns[rnn_type], rnn_activation=rnn_activation,
hidden_threshold = hidden_threshold,
qh_i = qh_i,
qh_f = qh_f,
qi_i = qi_i,
qi_f = qi_f,
bias = bias,
bn_factor = bn_factor,
bn_bias = bn_bias,
bn_weights = bn_weights)
model.load_state_dict(package['state_dict'])
if cuda:
model = torch.nn.DataParallel(model).cuda()
return model
Example 17
def load_model(self):
epochs, shared_steps, controller_steps = self.get_saved_models_info()
if len(epochs) == 0:
logger.info(f'[!] No checkpoint found in {self.args.model_dir}...')
return
self.epoch = self.start_epoch = max(epochs)
self.shared_step = max(shared_steps)
self.controller_step = max(controller_steps)
if self.args.num_gpu == 0:
map_location = lambda storage, loc: storage
else:
map_location = None
self.shared.load_state_dict(
torch.load(self.shared_path, map_location=map_location))
logger.info(f'[*] LOADED: {self.shared_path}')
self.controller.load_state_dict(
torch.load(self.controller_path, map_location=map_location))
logger.info(f'[*] LOADED: {self.controller_path}')
Example 18
model_file = os.path.join(self.forest_dir, self.forest_name)
with tables.open_file(model_file, filters=self.comp_filt) as mfile:
self.model = {
"thrs": mfile.get_node("/thrs")[:],
"fids": mfile.get_node("/fids")[:],
"cids": mfile.get_node("/cids")[:],
"edge_bnds": mfile.get_node("/edge_bnds")[:].flatten(),
"edge_pts": mfile.get_node("/edge_pts")[:].flatten(),
"n_seg": mfile.get_node("/n_seg")[:].flatten(),
"segs": mfile.get_node("/segs")[:],
self.trained = True
return self.model
Example 19
def load_model_decode(model_dir, data, args, name):
model_dir = model_dir + "_best"
print("Load Model from file: ", model_dir)
model = Graph(data, args)
model.load_state_dict(torch.load(model_dir))
# load model need consider if the model trained in GPU and load in CPU, or vice versa
if args.use_gpu:
model = model.cuda()
print(("Decode %s data ..." % name))
start_time = time.time()
speed, acc, p, r, f, pred_results = evaluate(data, args, model, name)
end_time = time.time()
time_cost = end_time - start_time
print(("%s: time:%.2fs, speed:%.2fst/s; acc: %.4f, p: %.4f, r: %.4f, f: %.4f" %
(name, time_cost, speed, acc, p, r, f)))
return pred_results
Example 20
if model is None:
assert isinstance(dumps, nn.Module), "model is None but load %s" % type(dumps)
model = dumps
else:
if isinstance(dumps, nn.Module):
dumps = dumps.state_dict()
else:
assert isinstance(dumps, dict), type(dumps)
res = model.load_state_dict(dumps, strict=False)
assert len(res.unexpected_keys) == 0, res.unexpected_keys
logger.info("missing keys in init-weights %s", res.missing_keys)
logger.info("load init-weights from %s", path)
return model
Example 21
splitLine = line.split()
word = splitLine[0]
embedding = [float(val) for val in splitLine[1:]]
model[word] = embedding
print "Done.",len(model)," words loaded!"
return model
Example 22
def load_model(self, epoch: int=None):
Loads a serialized model, using the ``model_serialization_prefix`` that was passed to the
constructor. If epoch is not None, we try to load the model from that epoch. If epoch is
not given, we load the best saved model.
logger.info("Loading serialized model")
# Loading serialized model
model_config_file = open("%s_config.json" % self.model_prefix)
model_config_json = model_config_file.read()
model_config_file.close()
self.model = model_from_json(model_config_json,
custom_objects=self._get_custom_objects())
if epoch is not None:
model_file = "%s_weights_epoch=%d.h5" % (self.model_prefix, epoch)
else:
model_file = "%s_weights.h5" % self.model_prefix
logger.info("Loading weights from file %s", model_file)
self.model.load_weights(model_file)
self.model.summary(show_masks=self.show_summary_with_masking)
self._load_auxiliary_files()
self._set_params_from_model()
self.model.compile(self.__compile_kwargs())
self.update_model_state_with_training_data = False
Example 23
from neuralnets.keraslayers.ChainCRF import create_custom_objects
model = keras.models.load_model(modelPath, custom_objects=create_custom_objects())
with h5py.File(modelPath, 'r') as f:
mappings = json.loads(f.attrs['mappings'])
if 'additionalFeatures' in f.attrs:
self.additionalFeatures = json.loads(f.attrs['additionalFeatures'])
if 'maxCharLen' in f.attrs:
self.maxCharLen = int(f.attrs['maxCharLen'])
self.model = model
self.setMappings(None, mappings)
Example 24
def load_trained_model(model_idx:int, model_builder:ModelBuilder, name:str='train_weights/train_') -> Model:
Load trained model from save file of the form `{name}{model_idx}.h5`
Arguments
model_idx: index of model to load
model_builder: :class:`~lumin.nn.models.model_builder.ModelBuilder` used to build the model
name: base name of file from which to load model
Returns:
Model loaded from save
model = Model(model_builder)
model.load(f'{name}{model_idx}.h5')
return model
Example 26
def load_model(self, model_name, force_reload=False):
Loads a model by passing the model path to the factory.
The factory will return a loaded model instance that will be stored in a dictionary.
:param model_name: Model name
:param force_reload: Boolean to specify if we need to reload a model on each call
:return: Boolean
if not force_reload and self.model_loaded(model_name):
return True
model_path = os.path.join(self.base_models_dir, model_name)
self.models_dict[model_name] = InferenceEngineFactory.get_engine(model_path)
return True
except ApplicationError as e:
raise e
Example 27
def load_rnn_model(self, batch_size, sess, checkpoint_path, load_weights=True):
with open(os.path.join(checkpoint_path, 'config.pkl'), 'rb') as f:
saved_args = pickle.load(f)
with open(os.path.join(checkpoint_path, 'chars_vocab.pkl'), 'rb') as f:
chars_lm, char_inds_lm = pickle.load(f)
saved_args.batch_size = batch_size
model = CharRnnLm(saved_args, training=False)
if load_weights:
if sess is None:
sess = tf.Session()
with sess.as_default():
tf.global_variables_initializer().run()
saver = tf.train.Saver(tf.global_variables())
ckpt = tf.train.get_checkpoint_state(checkpoint_path)
if ckpt and ckpt.model_checkpoint_path:
saver.restore(sess, ckpt.model_checkpoint_path)
char_inds_lm['-'] = char_inds_lm[' ']
del char_inds_lm[' ']
rnn_clm = {'model': model,
'chars': chars_lm,
'char_inds': char_inds_lm,
'saved_args': saved_args}
return rnn_clm
Example 29
def load_model(self, model_dir_path):
weight_file_path = Seq2SeqQA.get_weight_file_path(model_dir_path)
self.input_word2idx = np.load(model_dir_path + '/' + self.model_name + '-input-word2idx.npy').item()
self.input_idx2word = np.load(model_dir_path + '/' + self.model_name + '-input-idx2word.npy').item()
self.target_word2idx = np.load(model_dir_path + '/' + self.model_name + '-target-word2idx.npy').item()
self.target_idx2word = np.load(model_dir_path + '/' + self.model_name + '-target-idx2word.npy').item()
context = np.load(model_dir_path + '/' + self.model_name + '-config.npy').item()
self.max_encoder_seq_length = context['input_max_seq_length']
self.max_decoder_seq_length = context['target_max_seq_length']
self.num_encoder_tokens = context['num_input_tokens']
self.num_decoder_tokens = context['num_target_tokens']
print(self.max_encoder_seq_length)
print(self.max_decoder_seq_length)
print(self.num_encoder_tokens)
print(self.num_decoder_tokens)
model, encoder_model, decoder_model = self.create_model(self.num_encoder_tokens, self.max_encoder_seq_length,
self.num_decoder_tokens)
self.model = model
self.decoder_model = decoder_model
self.encoder_model = encoder_model
self.model.load_weights(weight_file_path)
Example 31
def load_model(self, model: str, model_path: str, max_seq_length: int):
g = tf.Graph()
with g.as_default():
hub_module = hub.Module(model_path)
self.tokens = tf.placeholder(dtype=tf.string, shape=[None, max_seq_length])
self.sequence_len = tf.placeholder(dtype=tf.int32, shape=[None])
elmo_inputs = dict(
tokens=self.tokens,
sequence_len=self.sequence_len
self.elmo_outputs = hub_module(elmo_inputs, signature="tokens", as_dict=True)
init_op = tf.group([tf.global_variables_initializer()])
g.finalize()
self.sess = tf.Session(graph=g)
self.sess.run(init_op)
self.model_name = model
self.max_seq_length = max_seq_length
Example 32
def load_model(self, iteration):
if not self.exist_save_model(iteration):
print("warning! iteration {iteration} model is not found.".format(iteration=iteration))
return False
self.model = prepare_model(self.model_config)
path_model = self._get_path_model(iteration)
print("load {} ...".format(path_model))
chainer.serializers.load_npz(path_model, self.model)
if self.gpu >= 0:
chainer.cuda.get_device(self.gpu).use()
self.model.to_gpu(self.gpu)
colorizer = self.colorization.get_colorizer()
self.forwarder = Forwarder(self.model_config, model=self.model, colorizer=colorizer)
return True
Example 33
def loadQualityModel(self):
"""Loads the coefficients from the rpy2 model estimated previously"""
execPath = os.path.dirname(os.path.realpath(__file__))+'/'
except:
execPath = os.getcwd()+'/'
if os.path.exists(self.qualityModelFn):
fn = self.qualityModelFn
elif os.path.exists(execPath+self.qualityModelFn):
fn = execPath+self.qualityModelFn
else:
raise Exception('Quality model coefficients file %s not found' % self.qualityModelFn)
with open(fn) as f:
coeffLines = f.read().split('\n')
for ii, cl in enumerate(coeffLines):
if 'intercept' in cl.lower():
coeffLines[ii] = 'intercept\t'+cl.split()[1]
break
raise Exception('Quality model %s must include an intercept' % self.qualityModelFn)
self.qualityModelCoeffs = dict([(cl.split()[0], float(cl.split()[1])) for cl in coeffLines if len(cl.split()) == 2])
knownCols = ['intercept', 'frechet_dist', 'll_dist_mean', 'll_dist_min', 'll_topol_mean', 'll_topol_min', 'll_distratio_mean', 'll_distratio_min', 'gpsMatchRatio', 'matchGpsRatio']
if any([cc not in knownCols for cc in self.qualityModelCoeffs]):
raise Exception('Error in quality model %s. Coefficient name not understood' % self.qualityModelFn)
Example 34
data = pickle.load(f)
#for evaluation, we want to load the complete model architecture and trained classes
net = data['net']
classes = data['classes']
im_size = data['im_size']
im_dim = data['im_dim']
print "DONE!"
return net, classes, im_size, im_dim
################# PREDICTION FUNCTION ####################
Example 35
def load_and_convert_caffe_model(prototxt_file_name, caffemodel_file_name):
caffenet = caffe_pb2.NetParameter()
caffenet_weights = caffe_pb2.NetParameter()
text_format.Merge(open(prototxt_file_name).read(), caffenet)
caffenet_weights.ParseFromString(open(caffemodel_file_name).read())
# C2 conv layers current require biases, but they are optional in C1
# Add zeros as biases is they are missing
add_missing_biases(caffenet_weights)
# We only care about getting parameters, so remove layers w/o parameters
remove_layers_without_parameters(caffenet, caffenet_weights)
# BatchNorm is not implemented in the translator *and* we need to fold Scale
# layers into the new C2 SpatialBN op, hence we remove the batch norm layers
# and apply custom translations code
bn_weights = remove_spatial_bn_layers(caffenet, caffenet_weights)
# Set num, channel, height and width for blobs that use shape.dim instead
normalize_shape(caffenet_weights)
# Translate the rest of the model
net, pretrained_weights = caffe_translator.TranslateModel(
caffenet, caffenet_weights
pretrained_weights.protos.extend(bn_weights)
return net, pretrained_weights
Example 36
def load_mmi_model(opt, dummy_opt):
checkpoint = torch.load(opt.mmi_model,
map_location=lambda storage, loc: storage)
fields = onmt.io.load_fields_from_vocab(
checkpoint['vocab'], data_type=opt.data_type)
model_opt = checkpoint['opt']
for arg in dummy_opt:
if arg not in model_opt:
model_opt.__dict__[arg] = dummy_opt[arg]
model = make_base_model(model_opt, fields,
use_gpu(opt), checkpoint)
model.eval()
model.generator.eval()
return fields, model, model_opt
Example 37
model: Downloaded model
base_url = 'http://download.tensorflow.org/models/object_detection/'
model_file = model_name + '.tar.gz'
model_dir = tf.keras.utils.get_file(
fname=model_name,
origin=base_url + model_file,
untar=True)
model_dir = pathlib.Path(model_dir)/"saved_model"
logger.info('Saved to {}'.format(model_dir))
model = tf.saved_model.load(str(model_dir))
model = model.signatures['serving_default']
return model
Example 38
def load_model(self, sess):
if not sess is None:
self.saver.restore(sess, self.config.LOAD_PATH)
print('Done loading model')
with open(self.config.LOAD_PATH + '.dict', 'rb') as file:
if self.subtoken_to_index is not None:
return
print('Loading dictionaries from: ' + self.config.LOAD_PATH)
self.subtoken_to_index = pickle.load(file)
self.index_to_subtoken = pickle.load(file)
self.subtoken_vocab_size = pickle.load(file)
self.target_to_index = pickle.load(file)
self.index_to_target = pickle.load(file)
self.target_vocab_size = pickle.load(file)
self.node_to_index = pickle.load(file)
self.index_to_node = pickle.load(file)
self.nodes_vocab_size = pickle.load(file)
self.num_training_examples = pickle.load(file)
self.epochs_trained = pickle.load(file)
saved_config = pickle.load(file)
self.config.take_model_hyperparams_from(saved_config)
print('Done loading dictionaries')
Example 39
def load_model(session, saver):
ckpt_dir = find_model_dir(FLAGS.checkpoint_dir)
print('Trying to load model from: {}...'.format(ckpt_dir))
# Get directory for latest run inside checkpoints folder: run_HH.MM.SS.
ckpt = tf.train.get_checkpoint_state(ckpt_dir)
if ckpt and ckpt.model_checkpoint_path:
# Restores from checkpoint
saver.restore(session, ckpt.model_checkpoint_path)
# Get global step from filename of the form model.ckpt-NNNN.
global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]
print('Loaded saved model from step {}'.format(global_step))
return global_step
else:
print('No checkpoint file found, cannot load model.')
return None
Example 40
def load_pretrained_model(self):
if not os.path.isdir(self.model_dir):
logger.info(f'Dir {self.model_dir} does not exist. Creating now.')
os.makedirs(self.model_dir)
logger.info(f'Dir {self.model_dir} created.')
model_path = os.path.join(self.model_dir, f'{self.model_type}.mdl')
if not os.path.isfile(model_path):
logger.info(f'Model {self.model_type} does not exist at {model_path}. Try to download it now.')
if self.model_type in PRETRAINED_MODEL_ARCHIVE_MAP:
fetch_pretrained_model(self.model_type, model_path)
self.load_model_weight(model_path)
else:
logger.info(f"Model {self.model_type} is not in out pretrained model list. \
Consider {list(PRETRAINED_MODEL_ARCHIVE_MAP.keys())}.")
else:
logger.info(f'Model {self.model_type} exists at {model_path}.')
self.load_model_weight(model_path, need_check=True)
Example 41
idim, odim, train_args = get_model_conf(model_path, os.path.join(get_root_dir(), os.path.dirname(model_path), 'model.json'))
# logging.warning('reading model parameters from ' + model_path)
if hasattr(train_args, "model_module"):
model_module = train_args.model_module
else:
model_module = "services.hci.speech.espnet_minimal.nets.pytorch_backend.e2e_asr:E2E"
model_class = dynamic_import(model_module)
model = model_class(idim, odim, train_args)
torch_load(model_path, model)
return model, train_args
Example 42
print("[TensorDB] FAIL! Cannot find: {}".format(args))
print ("no idtem")
return False, False
archs = self.archfs.get(fid).read()
'''print("[TensorDB] Find one params SUCCESS, {} took: {}s".format(args, round(time.time()-s, 2)))'''
return archs, fid
except Exception as e:
print("exception")
print(e)
return False, False
Example 43
def load_model(name):
'''Creates and returns an instance of the model given its class name.
The created model has a single placeholder node for feeding images.
# Find the model class from its name
all_models = get_models()
mdict = {model.__name__: model for model in all_models}
if name not in mdict:
print('Invalid model index. Options are:')
# Display a list of valid model names
for model in all_models:
print('\t* {}'.format(model.__name__))
return None
NetClass = mdict[name]
return NetClass
Example 44
def load_test_model(opt, dummy_opt):
checkpoint = torch.load(opt.model,
map_location=lambda storage, loc: storage)
fields = onmt.io.load_fields_from_vocab(
checkpoint['vocab'], data_type=opt.data_type)
model_opt = checkpoint['opt']
for arg in dummy_opt:
if arg not in model_opt:
model_opt.__dict__[arg] = dummy_opt[arg]
model = make_base_model(model_opt, fields,
use_gpu(opt), checkpoint)
model.eval()
model.generator.eval()
return fields, model, model_opt
Example 45
# load model
model_filename = os.path.join(opts.model_dir, "model_epoch_%d.pth" %epoch)
print("Load %s" %model_filename)
state_dict = torch.load(model_filename)
model.load_state_dict(state_dict['model'])
optimizer.load_state_dict(state_dict['optimizer'])
### move optimizer state to GPU
for state in optimizer.state.values():
for k, v in state.items():
if torch.is_tensor(v):
state[k] = v.cuda()
model.epoch = epoch ## reset model epoch
return model, optimizer
Example 46
def load_model_ensemble_and_task(filenames, arg_overrides=None, task=None, strict=True, suffix=''):
from fairseq import tasks
ensemble = []
for filename in filenames:
filename = filename.replace(".pt", suffix + ".pt")
if not PathManager.exists(filename):
raise IOError("Model file not found: {}".format(filename))
state = load_checkpoint_to_cpu(filename, arg_overrides)
args = state["args"]
if task is None:
task = tasks.setup_task(args)
# build model for ensemble
model = task.build_model(args)
model.load_state_dict(state["model"], strict=strict, args=args)
ensemble.append(model)
return ensemble, args, task
Example 47
def load_old_model(model_file):
print("Loading pre-trained model")
custom_objects = {'dice_coefficient_loss': dice_coefficient_loss, 'dice_coefficient': dice_coefficient,
'dice_coef': dice_coef, 'dice_coef_loss': dice_coef_loss,
'weighted_dice_coefficient': weighted_dice_coefficient,
'weighted_dice_coefficient_loss': weighted_dice_coefficient_loss}
from keras_contrib.layers import InstanceNormalization
custom_objects["InstanceNormalization"] = InstanceNormalization
except ImportError:
return load_model(model_file, custom_objects=custom_objects)
except ValueError as error:
if 'InstanceNormalization' in str(error):
raise ValueError(str(error) + "\n\nPlease install keras-contrib to use InstanceNormalization:\n"
"'pip install git+https://www.github.com/keras-team/keras-contrib.git'")
else:
raise error
Example 48
def load_model_decode(save_dir, data):
logger.info("Load Model from file: " + save_dir)
model = SeqModel(data)
model.load_state_dict(torch.load(save_dir))
logger.info(F"Decode dev data ...")
start_time = time.time()
speed, acc, p, r, f, pred_results = evaluate(data, model, 'dev')
end_time = time.time()
time_cost = end_time - start_time
logger.info(("%s: time:%.2fs, speed:%.2fst/s; acc: %.4f, p: %.4f, r: %.4f, f: %.4f"%('dev', time_cost, speed, acc, p, r, f)))
logger.info(F"Decode test data ...")
start_time = time.time()
speed, acc, p, r, f, pred_results = evaluate(data, model, 'test')
end_time = time.time()
time_cost = end_time - start_time
logger.info(("%s: time:%.2fs, speed:%.2fst/s; acc: %.4f, p: %.4f, r: %.4f, f: %.4f"%('test', time_cost, speed, acc, p, r, f)))
Example 49
def load_model(net='mnet'):
if net == 'mnet':
pretrained_path = 'retinaface/weights/mobilenet0.25_Final.pth'
# print('Loading pretrained model from {}'.format(pretrained_path))
model = RetinaFace(cfg=cfg_mnet, phase='test')
else:
pretrained_path = 'retinaface/weights/Resnet50_Final.pth'
# print('Loading pretrained model from {}'.format(pretrained_path))
model = RetinaFace(cfg=cfg_re50, phase='test')
device = torch.cuda.current_device()
pretrained_dict = torch.load(pretrained_path, map_location=lambda storage, loc: storage.cuda(device))
if "state_dict" in pretrained_dict.keys():
pretrained_dict = remove_prefix(pretrained_dict['state_dict'], 'module.')
else:
pretrained_dict = remove_prefix(pretrained_dict, 'module.')
check_keys(model, pretrained_dict)
model.load_state_dict(pretrained_dict, strict=False)
# print('Finished loading model!')
return model
Example 50
logger.debug(f"load model from {model_path}")
# assert model file
assert os.path.isfile(model_path), f"model file {model_path} not existed"
# assert model data is empty
if self._model and not overwrite:
raise RuntimeError(
f"model is not empty, you can set `overwrite` True to cover it"
# joblib raise an error ( i have no idea about how to fix it ) here, so use pickle instead
with open(model_path, "rb") as f:
self._model = pickle.load(f)
Example 51
def load_model(model, input_map=None):
# Check if the model is a model directory (containing a metagraph and a checkpoint file)
# or if it is a protobuf file with a frozen graph
model_exp = os.path.expanduser(model)
if (os.path.isfile(model_exp)):
print('Model filename: %s' % model_exp)
with gfile.FastGFile(model_exp,'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
tf.import_graph_def(graph_def, input_map=input_map, name='')
else:
print('Model directory: %s' % model_exp)
meta_file, ckpt_file = get_model_filenames(model_exp)
print('Metagraph file: %s' % meta_file)
print('Checkpoint file: %s' % ckpt_file)
saver = tf.train.import_meta_graph(os.path.join(model_exp, meta_file), input_map=input_map)
saver.restore(tf.get_default_session(), os.path.join(model_exp, ckpt_file))
Example 52
archs = self.archfs.get(fid).read()
'''print("[TensorDB] Find one params SUCCESS, {} took: {}s".format(args, round(time.time()-s, 2)))'''
return archs, fid
except Exception as e:
print("exception")
print(e)
return False, False
Example 53
model_state = checkpoint['state_dict_best']
self.centroids = checkpoint['centroids'] if 'centroids' in checkpoint else None
for key, model in self.networks.items():
weights = model_state[key]
weights = {k: weights[k] for k in weights if k in model.state_dict()}
# model.load_state_dict(model_state[key])
model.load_state_dict(weights)
Example 54
def load_model(pred_config, args, device):
# Check whether model exists
if not os.path.exists(pred_config.model_dir):
raise Exception("Model doesn't exists! Train first!")
model = MODEL_CLASSES[args.model_type][1].from_pretrained(args.model_dir,
args=args,
intent_label_lst=get_intent_labels(args),
slot_label_lst=get_slot_labels(args))
model.to(device)
model.eval()
logger.info("***** Model Loaded *****")
except:
raise Exception("Some model files might be missing...")
return model
Example 55
def load_poincare_model(path, word2vec_format=True, binary=False):
""" Load a Poincare embedding model.
:param path: path of the file of the pre-trained Poincare embedding model
:param word2vec_format: whether to load from word2vec format (default: True)
:param binary: binary format (default: False)
:return: a pre-trained Poincare embedding model
:type path: str
:type word2vec_format: bool
:type binary: bool
:rtype: gensim.models.poincare.PoincareKeyedVectors
if word2vec_format:
return PoincareKeyedVectors.load_word2vec_format(path, binary=binary)
else:
return PoincareModel.load(path).kv
Example 56
def load_model(model, input_map=None):
"""Loads a tensorflow model and restore the variables to the default session."""
# Check if the model is a model directory (containing a metagraph and a checkpoint file)
# or if it is a protobuf file with a frozen graph
model_exp = os.path.expanduser(model)
if (os.path.isfile(model_exp)):
print('Model filename: %s' % model_exp)
with tf.gfile.FastGFile(model_exp, 'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
tf.import_graph_def(graph_def, input_map=input_map, name='')
else:
print('Model directory: %s' % model_exp)
meta_file, ckpt_file = get_model_filenames(model_exp)
print('Metagraph file: %s' % meta_file)
print('Checkpoint file: %s' % ckpt_file)
saver = tf.train.import_meta_graph(os.path.join(model_exp, meta_file), input_map=input_map)
saver.restore(tf.get_default_session(), os.path.join(model_exp, ckpt_file))