本文整理汇总了Python中keras.utils.multi_gpu_model方法的典型用法代码示例。如果您正苦于以下问题:Python utils.multi_gpu_model方法的具体用法?Python utils.multi_gpu_model怎么用?Python utils.multi_gpu_model使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类keras.utils
的用法示例。
在下文中一共展示了utils.multi_gpu_model方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from keras import utils [as 别名]
# 或者: from keras.utils import multi_gpu_model [as 别名]
def __init__(self, config):
# Initialize model
model = Unet(input_shape=config["input_shape"],
n_labels=config["classes"],
activation="sigmoid")
# Transform to Keras multi GPU model
if config["gpu_number"] > 1:
model = multi_gpu_model(model, config["gpu_number"])
# Compile model
model.compile(optimizer=Adam(lr=config["learninig_rate"]),
loss=tversky_loss,
metrics=self.metrics)
self.model = model
self.config = config
# Train the Neural Network model on the provided case ids
示例2: __build_model
# 需要导入模块: from keras import utils [as 别名]
# 或者: from keras.utils import multi_gpu_model [as 别名]
def __build_model(self, emb_matrix=None):
word_input = Input(shape=(None,), dtype='int32', name="word_input")
word_emb = Embedding(self.vocab_size + 1, self.embed_dim,
weights=[emb_matrix] if emb_matrix is not None else None,
trainable=True if emb_matrix is None else False,
name='word_emb')(word_input)
bilstm_output = Bidirectional(LSTM(self.bi_lstm_units // 2,
return_sequences=True))(word_emb)
bilstm_output = Dropout(self.dropout_rate)(bilstm_output)
output = Dense(self.chunk_size + 1, kernel_initializer="he_normal")(bilstm_output)
output = CRF(self.chunk_size + 1, sparse_target=self.sparse_target)(output)
model = Model([word_input], [output])
parallel_model = model
if self.num_gpu > 1:
parallel_model = multi_gpu_model(model, gpus=self.num_gpu)
parallel_model.compile(optimizer=self.optimizer, loss=crf_loss, metrics=[crf_accuracy])
return model, parallel_model
示例3: interp_net
# 需要导入模块: from keras import utils [as 别名]
# 或者: from keras.utils import multi_gpu_model [as 别名]
def interp_net():
if gpu_num > 1:
dev = "/cpu:0"
else:
dev = "/gpu:0"
with tf.device(dev):
main_input = Input(shape=(4*num_features, timestamp), name='input')
sci = single_channel_interp(ref_points, hours_look_ahead)
cci = cross_channel_interp()
interp = cci(sci(main_input))
reconst = cci(sci(main_input, reconstruction=True),
reconstruction=True)
aux_output = Lambda(lambda x: x, name='aux_output')(reconst)
z = Permute((2, 1))(interp)
z = GRU(hid, activation='tanh', recurrent_dropout=0.2, dropout=0.2)(z)
main_output = Dense(1, activation='sigmoid', name='main_output')(z)
orig_model = Model([main_input], [main_output, aux_output])
if gpu_num > 1:
model = multi_gpu_model(orig_model, gpus=gpu_num)
else:
model = orig_model
print(orig_model.summary())
return model
示例4: multi_gpu_test_simple_model
# 需要导入模块: from keras import utils [as 别名]
# 或者: from keras.utils import multi_gpu_model [as 别名]
def multi_gpu_test_simple_model():
print('####### test simple model')
num_samples = 1000
input_dim = 10
output_dim = 1
hidden_dim = 10
gpus = 8
target_gpu_id = [0, 2, 4]
epochs = 2
model = keras.models.Sequential()
model.add(keras.layers.Dense(hidden_dim,
input_shape=(input_dim,)))
model.add(keras.layers.Dense(output_dim))
x = np.random.random((num_samples, input_dim))
y = np.random.random((num_samples, output_dim))
parallel_model = multi_gpu_model(model, gpus=gpus)
parallel_model.compile(loss='mse', optimizer='rmsprop')
parallel_model.fit(x, y, epochs=epochs)
parallel_model = multi_gpu_model(model, gpus=target_gpu_id)
parallel_model.compile(loss='mse', optimizer='rmsprop')
parallel_model.fit(x, y, epochs=epochs)
示例5: multi_gpu
# 需要导入模块: from keras import utils [as 别名]
# 或者: from keras.utils import multi_gpu_model [as 别名]
def multi_gpu(model, gpus=None, cpu_merge=True, cpu_relocation=False):
'''Takes as input the model, and returns a model
based on the number of GPUs available on the machine
or alternatively the 'gpus' user input.
NOTE: this needs to be used before model.compile() in the
model inputted to Scan in the form:
from talos.utils.gpu_utils import multi_gpu
model = multi_gpu(model)
'''
from keras.utils import multi_gpu_model
return multi_gpu_model(model,
gpus=gpus,
cpu_merge=cpu_merge,
cpu_relocation=cpu_relocation)
示例6: build_model
# 需要导入模块: from keras import utils [as 别名]
# 或者: from keras.utils import multi_gpu_model [as 别名]
def build_model(self, model, gpus=1, **compile_kwargs):
"""
Compile a Keras Functional model.
:param model: keras.models.Model: Keras functional model
:param gpus: int: number of GPU units on which to parallelize the Keras model
:param compile_kwargs: kwargs passed to the 'compile' method of the Keras model
"""
# Test the parameters
if type(gpus) is not int:
raise TypeError("'gpus' argument must be an int")
# Self-explanatory
util.make_keras_picklable()
# Build a model, either on a single GPU or on a CPU to control multiple GPUs
self.base_model = model
self._n_steps = len(model.outputs)
if gpus > 1:
import tensorflow as tf
with tf.device('/cpu:0'):
self.base_model = keras.models.clone_model(self.base_model)
self.model = multi_gpu_model(self.base_model, gpus=gpus)
self.gpus = gpus
else:
self.model = self.base_model
self.model.compile(**compile_kwargs)
示例7: build
# 需要导入模块: from keras import utils [as 别名]
# 或者: from keras.utils import multi_gpu_model [as 别名]
def build(self):
""" Build the model. Override for custom build methods """
self.add_networks()
self.load_models(swapped=False)
inputs = self.get_inputs()
try:
self.build_autoencoders(inputs)
except ValueError as err:
if "must be from the same graph" in str(err).lower():
msg = ("There was an error loading saved weights. This is most likely due to "
"model corruption during a previous save."
"\nYou should restore weights from a snapshot or from backup files. "
"You can use the 'Restore' Tool to restore from backup.")
raise FaceswapError(msg) from err
if "multi_gpu_model" in str(err).lower():
raise FaceswapError(str(err)) from err
raise err
self.log_summary()
self.compile_predictors(initialize=True)
示例8: load_model
# 需要导入模块: from keras import utils [as 别名]
# 或者: from keras.utils import multi_gpu_model [as 别名]
def load_model(model_path):
custom_layers = {
"multihead_attention": multihead_attention,
"Conv2D": L.Conv2D,
"split_heads_2d": split_heads_2d,
"local_attention_2d": local_attention_2d,
"combine_heads_2d": combine_heads_2d
}
model = model_from_yaml(open(os.path.join(model_path, "arch.yaml")).read(), custom_objects=custom_layers)
full_path = os.path.join(model_path, "weights.h5")
with h5py.File(full_path, "r") as w:
keys = list(w.keys())
is_para = any(["model" in k for k in keys])
if is_para:
para_model = multi_gpu_model(model, gpus=2)
para_model.load_weights(full_path)
model = para_model.layers[-2]
else:
model.load_weights(full_path)
print("Model " + model_path + " loaded")
return model
示例9: __init__
# 需要导入模块: from keras import utils [as 别名]
# 或者: from keras.utils import multi_gpu_model [as 别名]
def __init__(self, ser_model, gpus):
pmodel = multi_gpu_model(ser_model, gpus)
self.__dict__.update(pmodel.__dict__)
self._smodel = ser_model
示例10: generate
# 需要导入模块: from keras import utils [as 别名]
# 或者: from keras.utils import multi_gpu_model [as 别名]
def generate(self):
model_path = os.path.expanduser(self.model_path)
assert model_path.endswith('.h5'), 'Keras model or weights must be a .h5 file.'
# Load model, or construct model and load weights.
num_anchors = len(self.anchors)
num_classes = len(self.class_names)
is_tiny_version = num_anchors==6 # default setting
try:
self.yolo_model = load_model(model_path, compile=False)
except:
self.yolo_model = tiny_yolo_body(Input(shape=(None,None,3)), num_anchors//2, num_classes) \
if is_tiny_version else yolo_body(Input(shape=(None,None,3)), num_anchors//3, num_classes)
self.yolo_model.load_weights(self.model_path) # make sure model, anchors and classes match
else:
assert self.yolo_model.layers[-1].output_shape[-1] == \
num_anchors/len(self.yolo_model.output) * (num_classes + 5), \
'Mismatch between model and given anchor and class sizes'
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))
np.random.seed(10101) # Fixed seed for consistent colors across runs.
np.random.shuffle(self.colors) # Shuffle colors to decorrelate adjacent classes.
np.random.seed(None) # Reset seed to default.
# Generate output tensor targets for filtered bounding boxes.
self.input_image_shape = K.placeholder(shape=(2, ))
if self.gpu_num>=2:
self.yolo_model = multi_gpu_model(self.yolo_model, gpus=self.gpu_num)
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
示例11: instantiate_multigpu_model_if_multiple_gpus
# 需要导入模块: from keras import utils [as 别名]
# 或者: from keras.utils import multi_gpu_model [as 别名]
def instantiate_multigpu_model_if_multiple_gpus(training_model):
if len(cfg.gpus) > 1:
training_model = multi_gpu_model(training_model, len(cfg.gpus))
return training_model
示例12: cnn
# 需要导入模块: from keras import utils [as 别名]
# 或者: from keras.utils import multi_gpu_model [as 别名]
def cnn(embedding_matrix, char_matrix, num_classes, max_seq_len, max_ll3_seq_len,
num_filters=64, l2_weight_decay=0.0001, dropout_val=0.5,
dense_dim=32, add_sigmoid=True, train_embeds=False, gpus=0,
n_cnn_layers=1, pool='max', add_embeds=False):
if pool == 'max':
Pooling = MaxPooling1D
GlobalPooling = GlobalMaxPooling1D
elif pool == 'avg':
Pooling = AveragePooling1D
GlobalPooling = GlobalAveragePooling1D
input_ = Input(shape=(max_seq_len,))
embeds = Embedding(embedding_matrix.shape[0],
embedding_matrix.shape[1],
weights=[embedding_matrix],
input_length=max_seq_len,
trainable=train_embeds)(input_)
x = embeds
for i in range(n_cnn_layers-1):
x = Conv1D(num_filters, 7, activation='relu', padding='same')(x)
x = Pooling(2)(x)
x = Conv1D(num_filters, 7, activation='relu', padding='same')(x)
x = GlobalPooling()(x)
if add_embeds:
x1 = Conv1D(num_filters, 7, activation='relu', padding='same')(embeds)
x1 = GlobalPooling()(x1)
x = Concatenate()([x, x1])
x = BatchNormalization()(x)
x = Dropout(dropout_val)(x)
x = Dense(dense_dim, activation='relu', kernel_regularizer=regularizers.l2(l2_weight_decay))(x)
if add_sigmoid:
x = Dense(num_classes, activation='sigmoid')(x)
model = Model(inputs=input_, outputs=x)
if gpus > 0:
model = multi_gpu_model(model, gpus=gpus)
return model
示例13: create_models
# 需要导入模块: from keras import utils [as 别名]
# 或者: from keras.utils import multi_gpu_model [as 别名]
def create_models(backbone_retinanet, num_classes, weights, multi_gpu=0, freeze_backbone=False):
""" Creates three models (model, training_model, prediction_model).
Args
backbone_retinanet : A function to call to create a retinanet model with a given backbone.
num_classes : The number of classes to train.
weights : The weights to load into the model.
multi_gpu : The number of GPUs to use for training.
freeze_backbone : If True, disables learning for the backbone.
Returns
model : The base model. This is also the model that is saved in snapshots.
training_model : The training model. If multi_gpu=0, this is identical to model.
prediction_model : The model wrapped with utility functions to perform object detection (applies regression values and performs NMS).
"""
modifier = freeze_model if freeze_backbone else None
# Keras recommends initialising a multi-gpu model on the CPU to ease weight sharing, and to prevent OOM errors.
# optionally wrap in a parallel model
if multi_gpu > 1:
from keras.utils import multi_gpu_model
with tf.device('/cpu:0'):
model = model_with_weights(backbone_retinanet(num_classes, modifier=modifier), weights=weights, skip_mismatch=True)
training_model = multi_gpu_model(model, gpus=multi_gpu)
else:
model = model_with_weights(backbone_retinanet(num_classes, modifier=modifier), weights=weights, skip_mismatch=True)
training_model = model
# make prediction model
prediction_model = retinanet_bbox(model=model)
# compile model
training_model.compile(
loss={
'regression' : losses.smooth_l1(),
'classification': losses.focal()
},
optimizer=keras.optimizers.adam(lr=1e-5, clipnorm=0.001)
)
return model, training_model, prediction_model
示例14: __init__
# 需要导入模块: from keras import utils [as 别名]
# 或者: from keras.utils import multi_gpu_model [as 别名]
def __init__(self, ser_model, gpus):
pmodel = multi_gpu_model(ser_model, gpus, cpu_relocation=False, cpu_merge=False)
self.__dict__.update(pmodel.__dict__)
self._smodel = ser_model
示例15: compiled_model
# 需要导入模块: from keras import utils [as 别名]
# 或者: from keras.utils import multi_gpu_model [as 别名]
def compiled_model(self):
if self.config.gpu_count > 1:
# 複数GPUで並列処理
with tf.device('/cpu:0'):
model, n_outputs = self._build_model()
model = multi_gpu_model(model, self.config.gpu_count)
else:
model, n_outputs = self._build_model()
# compile()ではlossを指定しないが、空ではエラーになるためNoneのリストを指定する。
model.compile(optimizer=Adam(lr=self.config.learning_rate),
loss=[None] * n_outputs)
return model