本文整理匯總了Python中keras.models.load_model方法的典型用法代碼示例。如果您正苦於以下問題:Python models.load_model方法的具體用法?Python models.load_model怎麽用?Python models.load_model使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類keras.models
的用法示例。
在下文中一共展示了models.load_model方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: load
# 需要導入模塊: from keras import models [as 別名]
# 或者: from keras.models import load_model [as 別名]
def load(self, model_dir, architecture, image_size):
from keras.models import load_model
from vergeml.sources.features import get_preprocess_input
labels_txt = os.path.join(model_dir, "labels.txt")
if not os.path.exists(labels_txt):
raise VergeMLError("labels.txt not found: {}".format(labels_txt))
model_h5 = os.path.join(model_dir, "model.h5")
if not os.path.exists(model_h5):
raise VergeMLError("model.h5 not found: {}".format(model_h5))
with open(labels_txt, "r") as f:
self.labels = f.read().splitlines()
self.model = load_model(model_h5)
self.image_size = image_size
self.preprocess_input = get_preprocess_input(architecture)
示例2: CNN_test
# 需要導入模塊: from keras import models [as 別名]
# 或者: from keras.models import load_model [as 別名]
def CNN_test(test_fold, feat):
"""
Test model using test set
:param test_fold: test fold of 5-fold cross validation
:param feat: which feature to use
"""
# 讀取測試數據
_, _, test_features, test_labels = esc10_input.get_data(test_fold, feat)
# 導入訓練好的模型
model = load_model('./saved_model/cnn_{}_fold{}.h5'.format(feat, test_fold))
# 輸出訓練好的模型在測試集上的表現
score = model.evaluate(test_features, test_labels)
print('Test score:', score[0])
print('Test accuracy:', score[1])
return score[1]
示例3: generate
# 需要導入模塊: from keras import models [as 別名]
# 或者: from keras.models import load_model [as 別名]
def generate(self):
model_path = os.path.expanduser(self.model_path)
assert model_path.endswith('.h5'), 'Keras model must be a .h5 file.'
self.yolo_model = load_model(model_path, compile=False)
print('{} model, anchors, and classes loaded.'.format(model_path))
# Generate colors for drawing bounding boxes.
hsv_tuples = [(x / len(self.class_names), 1., 1.)
for x in range(len(self.class_names))]
self.colors = list(map(lambda x: colorsys.hsv_to_rgb(*x), hsv_tuples))
self.colors = list(
map(lambda x: (int(x[0] * 255), int(x[1] * 255), int(x[2] * 255)),
self.colors))
random.seed(10101) # Fixed seed for consistent colors across runs.
random.shuffle(self.colors) # Shuffle colors to decorrelate adjacent classes.
random.seed(None) # Reset seed to default.
# Generate output tensor targets for filtered bounding boxes.
self.input_image_shape = K.placeholder(shape=(2, ))
boxes, scores, classes = yolo_eval(self.yolo_model.output, self.anchors,
len(self.class_names), self.input_image_shape,
score_threshold=self.score, iou_threshold=self.iou)
return boxes, scores, classes
示例4: get_custom_architecture
# 需要導入模塊: from keras import models [as 別名]
# 或者: from keras.models import load_model [as 別名]
def get_custom_architecture(name, trainings_dir, output_layer):
from keras.models import load_model, Model
name = name.lstrip("@")
model = load_model(os.path.join(trainings_dir, name, 'checkpoints', 'model.h5'))
try:
if isinstance(output_layer, int):
layer = model.layers[output_layer]
else:
layer = model.get_layer(output_layer)
except Exception:
if isinstance(output_layer, int):
raise VergeMLError(f'output-layer {output_layer} not found - model has only {len(model.layers)} layers.')
else:
candidates = list(map(lambda l: l.name, model.layers))
raise VergeMLError(f'output-layer named {output_layer} not found.',
suggestion=did_you_mean(candidates, output_layer))
model = Model(inputs=model.input, outputs=layer.output)
return model
示例5: __init__
# 需要導入模塊: from keras import models [as 別名]
# 或者: from keras.models import load_model [as 別名]
def __init__(self, model_path=None):
if model_path is not None:
self.model = self.load_model(model_path)
else:
# VGG16 last conv features
inputs = Input(shape=(7, 7, 512))
x = Convolution2D(128, 1, 1)(inputs)
x = Flatten()(x)
# Cls head
h_cls = Dense(256, activation='relu', W_regularizer=l2(l=0.01))(x)
h_cls = Dropout(p=0.5)(h_cls)
cls_head = Dense(20, activation='softmax', name='cls')(h_cls)
# Reg head
h_reg = Dense(256, activation='relu', W_regularizer=l2(l=0.01))(x)
h_reg = Dropout(p=0.5)(h_reg)
reg_head = Dense(4, activation='linear', name='reg')(h_reg)
# Joint model
self.model = Model(input=inputs, output=[cls_head, reg_head])
示例6: _loadTFGraph
# 需要導入模塊: from keras import models [as 別名]
# 或者: from keras.models import load_model [as 別名]
def _loadTFGraph(self, sess, graph):
"""
Loads the Keras model into memory, then uses the passed-in session to load the
model's inference-related ops into the passed-in Tensorflow graph.
:return: A tuple (graph, input_name, output_name) where graph is the TF graph
corresponding to the Keras model's inference subgraph, input_name is the name of the
Keras model's input tensor, and output_name is the name of the Keras model's output tensor.
"""
keras_backend = K.backend()
assert keras_backend == "tensorflow", \
"Only tensorflow-backed Keras models are supported, tried to load Keras model " \
"with backend %s." % (keras_backend)
with graph.as_default():
K.set_learning_phase(0) # Inference phase
model = load_model(self.getModelFile())
out_op_name = tfx.op_name(model.output, graph)
stripped_graph = tfx.strip_and_freeze_until([out_op_name], graph, sess,
return_graph=True)
return stripped_graph, model.input.name, model.output.name
示例7: test_model
# 需要導入模塊: from keras import models [as 別名]
# 或者: from keras.models import load_model [as 別名]
def test_model(self):
model = load_model(self.PATH)
intermediate_layer_model = Model(input=model.input, output=model.get_layer("utter").output)
intermediate_output_train = intermediate_layer_model.predict(self.train_x)
intermediate_output_val = intermediate_layer_model.predict(self.val_x)
intermediate_output_test = intermediate_layer_model.predict(self.test_x)
train_emb, val_emb, test_emb = {}, {}, {}
for idx, ID in enumerate(self.train_id):
train_emb[ID] = intermediate_output_train[idx]
for idx, ID in enumerate(self.val_id):
val_emb[ID] = intermediate_output_val[idx]
for idx, ID in enumerate(self.test_id):
test_emb[ID] = intermediate_output_test[idx]
pickle.dump([train_emb, val_emb, test_emb], open(self.OUTPUT_PATH, "wb"))
self.calc_test_result(model.predict(self.test_x), self.test_y, self.test_mask)
示例8: modify_backprop
# 需要導入模塊: from keras import models [as 別名]
# 或者: from keras.models import load_model [as 別名]
def modify_backprop(model, name, task):
graph = tf.get_default_graph()
with graph.gradient_override_map({'Relu': name}):
# get layers that have an activation
activation_layers = [layer for layer in model.layers
if hasattr(layer, 'activation')]
# replace relu activation
for layer in activation_layers:
if layer.activation == keras.activations.relu:
layer.activation = tf.nn.relu
# re-instanciate a new model
if task == 'gender':
model_path = '../trained_models/gender_models/gender_mini_XCEPTION.21-0.95.hdf5'
elif task == 'emotion':
model_path = '../trained_models/emotion_models/fer2013_mini_XCEPTION.102-0.66.hdf5'
# model_path = '../trained_models/fer2013_mini_XCEPTION.119-0.65.hdf5'
# model_path = '../trained_models/fer2013_big_XCEPTION.54-0.66.hdf5'
new_model = load_model(model_path, compile=False)
return new_model
示例9: __init__
# 需要導入模塊: from keras import models [as 別名]
# 或者: from keras.models import load_model [as 別名]
def __init__(self,
init_state,
max_simulation=AlphaZeroConfig.MAX_SIMULATION_AGENT,
MODEL_PATH=AlphaZeroConfig.DEFAULT_MODEL_AGENT):
"""
Contructor of AlphaZero Agent
:param init_state: the initial state of the game. It will be used continuously
:param max_simulation: MCTS max simulation
:param MODEL_PATH: Model Path used for AlphaZero Agent
"""
self.max_simulation = max_simulation
all_action_spaces = action_spaces_new()
self.ae = ActionEncoder()
self.ae.fit(list_all_action=all_action_spaces)
self.stacked_state = StackedState(init_state)
self.deepnet_model = PawnNetZero(len(all_action_spaces))
self.deepnet_model.model = load_model(MODEL_PATH)
self.mcts = MCTreeSearch(self.deepnet_model.model, 1, self.max_simulation, self.ae, self.stacked_state)
示例10: predict
# 需要導入模塊: from keras import models [as 別名]
# 或者: from keras.models import load_model [as 別名]
def predict(self, data_root, model_root, test_root, test_div, out_path, readable=False):
meta_path = os.path.join(data_root, 'meta')
meta = cPickle.loads(open(meta_path, 'rb').read())
model_fname = os.path.join(model_root, 'model.h5')
self.logger.info('# of classes(train): %s' % len(meta['y_vocab']))
model = load_model(model_fname,
custom_objects={'top1_acc': top1_acc})
test_path = os.path.join(test_root, 'data.h5py')
test_data = h5py.File(test_path, 'r')
test = test_data[test_div]
batch_size = opt.batch_size
pred_y = []
test_gen = ThreadsafeIter(self.get_sample_generator(test, batch_size, raise_stop_event=True))
total_test_samples = test['uni'].shape[0]
with tqdm.tqdm(total=total_test_samples) as pbar:
for chunk in test_gen:
total_test_samples = test['uni'].shape[0]
X, _ = chunk
_pred_y = model.predict(X)
pred_y.extend([np.argmax(y) for y in _pred_y])
pbar.update(X[0].shape[0])
self.write_prediction_result(test, pred_y, meta, out_path, readable=readable)
示例11: __init__
# 需要導入模塊: from keras import models [as 別名]
# 或者: from keras.models import load_model [as 別名]
def __init__(self, nb_classes, resnet_layers, input_shape, weights):
self.input_shape = input_shape
self.num_classes = nb_classes
json_path = join("weights", "keras", weights + ".json")
h5_path = join("weights", "keras", weights + ".h5")
if 'pspnet' in weights:
if os.path.isfile(json_path) and os.path.isfile(h5_path):
print("Keras model & weights found, loading...")
with CustomObjectScope({'Interp': layers.Interp}):
with open(json_path) as file_handle:
self.model = model_from_json(file_handle.read())
self.model.load_weights(h5_path)
else:
print("No Keras model & weights found, import from npy weights.")
self.model = layers.build_pspnet(nb_classes=nb_classes,
resnet_layers=resnet_layers,
input_shape=self.input_shape)
self.set_npy_weights(weights)
else:
print('Load pre-trained weights')
self.model = load_model(weights)
示例12: __init__
# 需要導入模塊: from keras import models [as 別名]
# 或者: from keras.models import load_model [as 別名]
def __init__(self,
resource_path='./resources/',
learning_rate=0.0002,
decay_rate=2e-6,
gpus = 1):
self.gpus = gpus
self.learning_rate = learning_rate
self.decay_rate = decay_rate
def zero_loss(y_true, y_pred):
return K.zeros_like(y_true)
discriminator_full = load_model(resource_path + 'discriminator_full.h5', custom_objects={'Conv2D_r': Conv2D_r, 'InstanceNormalization': InstanceNormalization, 'tf': tf, 'zero_loss': zero_loss, 'ConvSN2D': ConvSN2D, 'DenseSN': DenseSN})
discriminator_full.trainable = True
discriminator_full.name = "discriminator_full"
self.model = discriminator_full
self.save_model = discriminator_full
示例13: __init__
# 需要導入模塊: from keras import models [as 別名]
# 或者: from keras.models import load_model [as 別名]
def __init__(self, args):
"""
Tokenizer model loading etc goes here
"""
from keras.models import load_model
self.model = load_model(args.model)
with open(args.vocab,'rb') as inf:
self.vocab = pickle.load(inf)
self.args=args
示例14: __init__
# 需要導入模塊: from keras import models [as 別名]
# 或者: from keras.models import load_model [as 別名]
def __init__(self, weights, postproc_mean, postproc_sd):
"""Simple keras model that also runs the postprocessin
"""
self.weights = weights
self.model = load_model(weights)
self.postproc_mean = postproc_mean
self.postproc_sd = postproc_sd
示例15: __init__
# 需要導入模塊: from keras import models [as 別名]
# 或者: from keras.models import load_model [as 別名]
def __init__(self, model_file):
self.model_file = model_file
K.clear_session() # restart session
self.model = load_model(model_file, compile=False)
self.contrib_fns = {}