本文整理汇总了Python中keras.backend.clear_session方法的典型用法代码示例。如果您正苦于以下问题:Python backend.clear_session方法的具体用法?Python backend.clear_session怎么用?Python backend.clear_session使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类keras.backend
的用法示例。
在下文中一共展示了backend.clear_session方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: helper_test
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import clear_session [as 别名]
def helper_test(model):
img_path = "../examples/dog.jpg"
new_model = to_heatmap(model)
# Loading the image
img = image.load_img(img_path, target_size=(800, 800))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
out = new_model.predict(x)
s = "n02084071" # Imagenet code for "dog"
ids = synset_to_dfs_ids(s)
heatmap = out[0]
if K.image_data_format() == 'channels_first':
heatmap = heatmap[ids]
heatmap = np.sum(heatmap, axis=0)
else:
heatmap = heatmap[:, :, ids]
heatmap = np.sum(heatmap, axis=2)
print(heatmap.shape)
assert heatmap.shape[0] == heatmap.shape[1]
K.clear_session()
示例2: CapsuleNet
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import clear_session [as 别名]
def CapsuleNet(n_capsule = 10, n_routings = 5, capsule_dim = 16,
n_recurrent=100, dropout_rate=0.2, l2_penalty=0.0001):
K.clear_session()
inputs = Input(shape=(170,))
x = Embedding(21099, 300, trainable=True)(inputs)
x = SpatialDropout1D(dropout_rate)(x)
x = Bidirectional(
CuDNNGRU(n_recurrent, return_sequences=True,
kernel_regularizer=l2(l2_penalty),
recurrent_regularizer=l2(l2_penalty)))(x)
x = PReLU()(x)
x = Capsule(
num_capsule=n_capsule, dim_capsule=capsule_dim,
routings=n_routings, share_weights=True)(x)
x = Flatten(name = 'concatenate')(x)
x = Dropout(dropout_rate)(x)
# fc = Dense(128, activation='sigmoid')(x)
outputs = Dense(6, activation='softmax')(x)
model = Model(inputs=inputs, outputs=outputs)
model.compile(loss='categorical_crossentropy', optimizer='nadam', metrics=['accuracy'])
return model
示例3: CapsuleNet_v2
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import clear_session [as 别名]
def CapsuleNet_v2(n_capsule = 10, n_routings = 5, capsule_dim = 16,
n_recurrent=100, dropout_rate=0.2, l2_penalty=0.0001):
K.clear_session()
inputs = Input(shape=(200,))
x = Embedding(20000, 300, trainable=True)(inputs)
x = SpatialDropout1D(dropout_rate)(x)
x = Bidirectional(
CuDNNGRU(n_recurrent, return_sequences=True,
kernel_regularizer=l2(l2_penalty),
recurrent_regularizer=l2(l2_penalty)))(x)
x = PReLU()(x)
x = Capsule(
num_capsule=n_capsule, dim_capsule=capsule_dim,
routings=n_routings, share_weights=True)(x)
x = Flatten(name = 'concatenate')(x)
x = Dropout(dropout_rate)(x)
# fc = Dense(128, activation='sigmoid')(x)
outputs = Dense(6, activation='softmax')(x)
model = Model(inputs=inputs, outputs=outputs)
model.compile(loss='categorical_crossentropy', optimizer='nadam', metrics=['accuracy'])
return model
示例4: download
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import clear_session [as 别名]
def download(cls, architecture, path="./"):
if architecture in cls.thirdparty_map:
weight_file = download_file(cls.thirdparty_map[architecture], directory=path)
return weight_file
elif cls.sanity_check(architecture):
output_filename = path + 'imagenet_{}.h5'.format(architecture)
if os.path.exists(output_filename) == False:
model = cls.architecture_map[architecture]()
model.save(output_filename)
print("Keras model {} is saved in [{}]".format(architecture, output_filename))
K.clear_session()
del model
return output_filename
else:
print("File [{}] existed, skip download.".format(output_filename))
return output_filename
else:
return None
示例5: inference
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import clear_session [as 别名]
def inference(cls, architecture, files, path, image_path):
if architecture in cls.thirdparty_map:
model = keras.models.load_model(files)
elif cls.sanity_check(architecture):
model = cls.architecture_map[architecture]()
else:
model = None
if model:
import numpy as np
func = TestKit.preprocess_func['keras'][architecture]
img = func(image_path)
img = np.expand_dims(img, axis=0)
predict = model.predict(img)
predict = np.squeeze(predict)
K.clear_session()
del model
return predict
else:
return None
示例6: _handle_broken_model
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import clear_session [as 别名]
def _handle_broken_model(self, model, error):
del model
n = self.genome_handler.n_classes
loss = log_loss(np.concatenate(([1], np.zeros(n - 1))), np.ones(n) / n)
accuracy = 1 / n
gc.collect()
if K.backend() == 'tensorflow':
K.clear_session()
tf.reset_default_graph()
print('An error occurred and the model could not train:')
print(error)
print(('Model assigned poor score. Please ensure that your model'
'constraints live within your computational resources.'))
return loss, accuracy
示例7: initialize
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import clear_session [as 别名]
def initialize(self, model, model_as_file):
K.clear_session()
if model_as_file:
with open(model, 'r') as f:
self.json_info = json.load(f)
else:
self.json_info = json.loads(model)
model_path = self.json_info['ModelFile']
if model_as_file and not os.path.isabs(model_path):
model_path = os.path.abspath(os.path.join(os.path.dirname(model), model_path))
if arcpy.env.processorType != "GPU":
os.environ['CUDA_VISIBLE_DEVICES'] = "-1"
# load the trained model
self.model = load_model(model_path)
self.graph = tf.get_default_graph()
示例8: __init__
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import clear_session [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 = {}
示例9: __init__
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import clear_session [as 别名]
def __init__(self):
from keras import backend as K
K.clear_session()
self.model_names = read_txt("models.txt")
# hard-code the path to this models
# if we'd use `source='dir'`, then the models wouldn't
# be updated
self.models = [kipoi.get_model("CpGenie/{0}".format(m), source='kipoi',
with_dataloader=False)
for m in self.model_names]
示例10: create_model
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import clear_session [as 别名]
def create_model(input_shape, anchors, num_classes, load_pretrained=True, freeze_body=2,
weights_path='model_data/yolo_weights.h5'):
'''create the training model'''
K.clear_session() # get a new session
image_input = Input(shape=(None, None, 3))
h, w = input_shape
num_anchors = len(anchors)
# y_true = [Input(shape=(416//{0:32, 1:16, 2:8}[l], 416//{0:32, 1:16, 2:8}[l], 9//3, 80+5)) for l in range(3)]
y_true = [Input(shape=(h//{0:32, 1:16, 2:8}[l], w//{0:32, 1:16, 2:8}[l], num_anchors//3, num_classes+5)) for l in range(3)]
model_body = yolo_body(image_input, num_anchors//3, num_classes)
print('Create YOLOv3 model with {} anchors and {} classes.'.format(num_anchors, num_classes))
if load_pretrained:
model_body.load_weights(weights_path, by_name=True, skip_mismatch=True)
print('Load weights {}.'.format(weights_path))
if freeze_body in [1, 2]:
# Freeze darknet53 body or freeze all but 3 output layers.
num = (185, len(model_body.layers)-3)[freeze_body-1]
for i in range(num): model_body.layers[i].trainable = False
print('Freeze the first {} layers of total {} layers.'.format(num, len(model_body.layers)))
model_loss = Lambda(yolo_loss, output_shape=(1,), name='yolo_loss',
arguments={'anchors': anchors, 'num_classes': num_classes, 'ignore_thresh': 0.5})(
[*model_body.output, *y_true])
model = Model([model_body.input, *y_true], model_loss)
print('model_body.input: ', model_body.input)
print('model.input: ', model.input)
return model
示例11: create_tiny_model
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import clear_session [as 别名]
def create_tiny_model(input_shape, anchors, num_classes, load_pretrained=True, freeze_body=2,
weights_path='model_data/tiny_yolo_weights.h5'):
'''create the training model, for Tiny YOLOv3'''
K.clear_session() # get a new session
image_input = Input(shape=(None, None, 3))
h, w = input_shape
num_anchors = len(anchors)
y_true = [Input(shape=(h//{0:32, 1:16}[l], w//{0:32, 1:16}[l], \
num_anchors//2, num_classes+5)) for l in range(2)]
model_body = tiny_yolo_body(image_input, num_anchors//2, num_classes)
print('Create Tiny YOLOv3 model with {} anchors and {} classes.'.format(num_anchors, num_classes))
if load_pretrained:
model_body.load_weights(weights_path, by_name=True, skip_mismatch=True)
print('Load weights {}.'.format(weights_path))
if freeze_body in [1, 2]:
# Freeze the darknet body or freeze all but 2 output layers.
num = (20, len(model_body.layers)-2)[freeze_body-1]
for i in range(num): model_body.layers[i].trainable = False
print('Freeze the first {} layers of total {} layers.'.format(num, len(model_body.layers)))
model_loss = Lambda(yolo_loss, output_shape=(1,), name='yolo_loss',
arguments={'anchors': anchors, 'num_classes': num_classes, 'ignore_thresh': 0.7})(
[*model_body.output, *y_true])
model = Model([model_body.input, *y_true], model_loss)
return model
示例12: __config_gpu_for_keras
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import clear_session [as 别名]
def __config_gpu_for_keras():
import tensorflow as tf
import keras.backend as K
gpu_core_id = __parse_gpu_id()
K.clear_session()
config = tf.ConfigProto()
config.gpu_options.visible_device_list = str(gpu_core_id)
config.gpu_options.allow_growth = True
session = tf.Session(config=config)
K.set_session(session)
# set which device to be used
const.GPU_CORE_ID = gpu_core_id
示例13: create_model
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import clear_session [as 别名]
def create_model(input_shape, anchors, num_classes, load_pretrained=False, freeze_body=False,
weights_path='model_data/yolo_weights.h5'):
K.clear_session() # get a new session
image_input = Input(shape=(None, None, 3))
h, w = input_shape
num_anchors = len(anchors)
y_true = [Input(shape=(h//{0:32, 1:16, 2:8}[l], w//{0:32, 1:16, 2:8}[l], \
num_anchors//3, num_classes+5)) for l in range(3)]
model_body = yolo_body(image_input, num_anchors//3, num_classes)
print('Create YOLOv3 model with {} anchors and {} classes.'.format(num_anchors, num_classes))
if load_pretrained:
model_body.load_weights(weights_path, by_name=True, skip_mismatch=True)
print('Load weights {}.'.format(weights_path))
if freeze_body:
# Do not freeze 3 output layers.
num = len(model_body.layers)-7
for i in range(num): model_body.layers[i].trainable = False
print('Freeze the first {} layers of total {} layers.'.format(num, len(model_body.layers)))
model_loss = Lambda(yolo_loss, output_shape=(1,), name='yolo_loss',
arguments={'anchors': anchors, 'num_classes': num_classes, 'ignore_thresh': 0.5})(
[*model_body.output, *y_true])
model = Model([model_body.input, *y_true], model_loss)
return model
示例14: clear
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import clear_session [as 别名]
def clear():
K.clear_session()
## Agents
示例15: pred_evaluate
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import clear_session [as 别名]
def pred_evaluate(config):
db=config["db"] #"amazon"
out_dir=config["out_dir"]
model_type=config["model_type"]
top_n=config["top_n"] #10
vote_n=1 #1 #typically 1, we disable manual vote; when top_n=1, we optionally vote
scores={}
data=np.load("../"+db+"/data/valid_50_idx.npz")
sess=tf.Session()
K.set_session(sess)
model_fn=out_dir+"eval.h5"
model=keras.models.load_model(model_fn)
model.get_layer("embedding_1").set_weights([np.vstack([data['train_rep'], np.zeros((95000, 512))]) ])
thres=0.5
y_pred=l2ac_predict(model, data, top_n, vote_n)
weighted_f1, _, _=evaluate(data['test_Y'], y_pred, thres=thres, rejection=True, mode="weighted")
macro_f1, _, _=evaluate(data['test_Y'], y_pred, thres=thres, rejection=True, mode="macro")
micro_f1, _, _=evaluate(data['test_Y'], y_pred, thres=thres, rejection=True, mode="micro")
scores={'weighted_f1': weighted_f1, 'macro_f1': macro_f1, 'micro_f1': micro_f1}
K.clear_session()
print scores["weighted_f1"]
with open(out_dir+"valid.json", "w") as fw:
json.dump(scores, fw)