我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用keras.backend.backend()。
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
def __init__(self, output_dim, num_senses, num_hyps, use_attention=False, return_attention=False, **kwargs): # Set output_dim in kwargs so that we can pass it along to LSTM's init kwargs['output_dim'] = output_dim self.num_senses = num_senses self.num_hyps = num_hyps self.use_attention = use_attention self.return_attention = return_attention super(OntoAttentionLSTM, self).__init__(**kwargs) # Recurrent would have set the input shape to cause the input dim to be 3. Change it. self.input_spec = [InputSpec(ndim=5)] if self.consume_less == "cpu": # In the LSTM implementation in Keras, consume_less = cpu causes all gates' inputs to be precomputed # and stored in memory. However, this doesn't work with OntoLSTM since the input to the gates is # dependent on the previous timestep's output. warnings.warn("OntoLSTM does not support consume_less = cpu. Changing it to mem.") self.consume_less = "mem" #TODO: Remove this dependency. if K.backend() == "tensorflow" and not self.unroll: warnings.warn("OntoLSTM does not work with unroll=False when backend is TF. Changing it to True.") self.unroll = True
def switch(condition, then_tensor, else_tensor): """ Keras' implementation of switch for tensorflow uses tf.switch which accepts only scalar conditions. It should use tf.select instead. """ if K.backend() == 'tensorflow': import tensorflow as tf condition_shape = condition.get_shape() input_shape = then_tensor.get_shape() if condition_shape[-1] != input_shape[-1] and condition_shape[-1] == 1: # This means the last dim is an embedding dim. Keras does not mask this dimension. But tf wants # the condition and the then and else tensors to be the same shape. condition = K.dot(tf.cast(condition, tf.float32), tf.ones((1, input_shape[-1]))) return tf.select(tf.cast(condition, dtype=tf.bool), then_tensor, else_tensor) else: import theano.tensor as T return T.switch(condition, then_tensor, else_tensor)
def dot_product(x, kernel): """ Wrapper for dot product operation, in order to be compatible with both Theano and Tensorflow Args: x (): input kernel (): weights Returns: """ if K.backend() == 'tensorflow': # todo: check that this is correct return K.squeeze(K.dot(x, K.expand_dims(kernel)), axis=-1) else: return K.dot(x, kernel)
def _input_dictionary_for_loss_net(self, labeled_spectrogram_batch: List[LabeledSpectrogram]) -> Dict[str, ndarray]: spectrograms = [x.z_normalized_transposed_spectrogram() for x in labeled_spectrogram_batch] labels = [x.label for x in labeled_spectrogram_batch] input_batch, prediction_lengths = self._input_batch_and_prediction_lengths(spectrograms) # Sets learning phase to training to enable dropout (see backend.learning_phase documentation for more info): training_phase_flag_tensor = array([True]) label_lengths = reshape(array([len(label) for label in labels]), (len(labeled_spectrogram_batch), 1)) return { Wav2Letter.InputNames.input_batch: input_batch, Wav2Letter.InputNames.prediction_lengths: self._prediction_length_batch(prediction_lengths, batch_size=len(spectrograms)), Wav2Letter.InputNames.label_batch: self.grapheme_encoding.encode_label_batch(labels), Wav2Letter.InputNames.label_lengths: label_lengths, 'keras_learning_phase': training_phase_flag_tensor }
def classifier(base_layers, input_rois, batch_size, nb_classes = 3, trainable=False): # compile times tend to be very high, so we use smaller ROI pooling regions to workaround if K.backend() == 'tensorflow': pooling_regions = 14 input_shape = (batch_size,14,14,2048) elif K.backend() == 'theano': pooling_regions = 7 input_shape = (batch_size,2048,7,7) out_roi_pool = RoiPoolingConv(pooling_regions, batch_size)([base_layers, input_rois]) out = TimeDistributed(Flatten())(out_roi_pool) # out = TimeDistributed(Dropout(0.4))(out) # out = TimeDistributed(Dense(2048,activation='relu'))(out) out_class = TimeDistributed(Dense(nb_classes, activation='softmax', kernel_initializer='zero'), name='dense_class_{}'.format(nb_classes))(out) # note: no regression target for bg class out_regr = TimeDistributed(Dense(4 * nb_classes, activation='linear', kernel_initializer='zero'), name='dense_regress_{}'.format(nb_classes))(out) return [out_class, out_regr]
def classifier(base_layers, input_rois, batch_size, nb_classes = 3, trainable=False): # compile times tend to be very high, so we use smaller ROI pooling regions to workaround if K.backend() == 'tensorflow': pooling_regions = 14 input_shape = (batch_size,14,14,512) elif K.backend() == 'theano': pooling_regions = 7 input_shape = (batch_size,512,7,7) out_roi_pool = RoiPoolingConv(pooling_regions, batch_size)([base_layers, input_rois]) out = TimeDistributed(Flatten())(out_roi_pool) out = TimeDistributed(Dense(4096,activation='relu'))(out) out = TimeDistributed(Dropout(0.5))(out) out = TimeDistributed(Dense(4096,activation='relu'))(out) out = TimeDistributed(Dropout(0.5))(out) out_class = TimeDistributed(Dense(nb_classes, activation='softmax', kernel_initializer='zero'), name='dense_class_{}'.format(nb_classes))(out) # note: no regression target for bg class out_regr = TimeDistributed(Dense(4 * nb_classes, activation='linear', kernel_initializer='zero'), name='dense_regress_{}'.format(nb_classes))(out) return [out_class, out_regr]
def classifier(base_layers, input_rois, batch_size, nb_classes = 3, trainable=False): # compile times tend to be very high, so we use smaller ROI pooling regions to workaround if K.backend() == 'tensorflow': pooling_regions = 14 input_shape = (batch_size,14,14,1024) elif K.backend() == 'theano': pooling_regions = 7 input_shape = (batch_size,1024,7,7) out_roi_pool = RoiPoolingConv(pooling_regions, batch_size)([base_layers, input_rois]) out = classifier_layers(out_roi_pool, input_shape=input_shape, trainable=True) out = TimeDistributed(Flatten())(out) out_class = TimeDistributed(Dense(nb_classes, activation='softmax', kernel_initializer='zero'), name='dense_class_{}'.format(nb_classes))(out) # note: no regression target for bg class out_regr = TimeDistributed(Dense(4 * (nb_classes-1), activation='linear', kernel_initializer='zero'), name='dense_regress_{}'.format(nb_classes))(out) return [out_class, out_regr]
def classifier(base_layers, input_rois, batch_size, nb_classes = 3, trainable=False): # compile times tend to be very high, so we use smaller ROI pooling regions to workaround if K.backend() == 'tensorflow': pooling_regions = 14 input_shape = (batch_size,14,14,1024) elif K.backend() == 'theano': pooling_regions = 7 input_shape = (batch_size,1024,7,7) out_roi_pool = RoiPoolingConv(pooling_regions, batch_size)([base_layers, input_rois]) out = TimeDistributed(Flatten())(out_roi_pool) out = TimeDistributed(Dense(4096,activation='relu'))(out) out = TimeDistributed(Dropout(0.5))(out) out = TimeDistributed(Dense(4096,activation='relu'))(out) out = TimeDistributed(Dropout(0.5))(out) out_class = TimeDistributed(Dense(nb_classes, activation='softmax', kernel_initializer='zero'), name='dense_class_{}'.format(nb_classes))(out) # note: no regression target for bg class out_regr = TimeDistributed(Dense(4 * nb_classes, activation='linear', kernel_initializer='zero'), name='dense_regress_{}'.format(nb_classes))(out) return [out_class, out_regr]
def clip_norm(g, c, n): if c > 0: if K.backend() == 'tensorflow': import tensorflow as tf import copy condition = n >= c then_expression = tf.scalar_mul(c / n, g) else_expression = g if hasattr(then_expression, 'get_shape'): g_shape = copy.copy(then_expression.get_shape()) elif hasattr(then_expression, 'dense_shape'): g_shape = copy.copy(then_expression.dense_shape) if condition.dtype != tf.bool: condition = tf.cast(condition, 'bool') g = K.tensorflow_backend.control_flow_ops.cond( condition, lambda: then_expression, lambda: else_expression) if hasattr(then_expression, 'get_shape'): g.set_shape(g_shape) elif hasattr(then_expression, 'dense_shape'): g._dense_shape = g_shape else: g = K.switch(n >= c, g * c / n, g) return g
def classifier(base_layers, input_rois, num_rois, nb_classes = 21, trainable=False): # compile times on theano tend to be very high, so we use smaller ROI pooling regions to workaround if K.backend() == 'tensorflow': pooling_regions = 7 input_shape = (num_rois,7,7,512) elif K.backend() == 'theano': pooling_regions = 7 input_shape = (num_rois,512,7,7) out_roi_pool = RoiPoolingConv(pooling_regions, num_rois)([base_layers, input_rois]) out = TimeDistributed(Flatten(name='flatten'))(out_roi_pool) out = TimeDistributed(Dense(4096, activation='relu', name='fc1'))(out) out = TimeDistributed(Dropout(0.5))(out) out = TimeDistributed(Dense(4096, activation='relu', name='fc2'))(out) out = TimeDistributed(Dropout(0.5))(out) out_class = TimeDistributed(Dense(nb_classes, activation='softmax', kernel_initializer='zero'), name='dense_class_{}'.format(nb_classes))(out) # note: no regression target for bg class out_regr = TimeDistributed(Dense(4 * (nb_classes-1), activation='linear', kernel_initializer='zero'), name='dense_regress_{}'.format(nb_classes))(out) return [out_class, out_regr]
def classifier(base_layers, input_rois, num_rois, nb_classes = 21, trainable=False): # compile times on theano tend to be very high, so we use smaller ROI pooling regions to workaround if K.backend() == 'tensorflow': pooling_regions = 14 input_shape = (num_rois,14,14,1024) elif K.backend() == 'theano': pooling_regions = 7 input_shape = (num_rois,1024,7,7) out_roi_pool = RoiPoolingConv(pooling_regions, num_rois)([base_layers, input_rois]) out = classifier_layers(out_roi_pool, input_shape=input_shape, trainable=True) out = TimeDistributed(Flatten())(out) out_class = TimeDistributed(Dense(nb_classes, activation='softmax', kernel_initializer='zero'), name='dense_class_{}'.format(nb_classes))(out) # note: no regression target for bg class out_regr = TimeDistributed(Dense(4 * (nb_classes-1), activation='linear', kernel_initializer='zero'), name='dense_regress_{}'.format(nb_classes))(out) return [out_class, out_regr]
def _softmax(x, dim): """Computes softmax along a specified dim. Keras currently lacks this feature. """ if K.backend() == 'tensorflow': import tensorflow as tf return tf.nn.softmax(x, dim) elif K.backend() is 'cntk': import cntk return cntk.softmax(x, dim) elif K.backend() == 'theano': # Theano cannot softmax along an arbitrary dim. # So, we will shuffle `dim` to -1 and un-shuffle after softmax. perm = np.arange(K.ndim(x)) perm[dim], perm[-1] = perm[-1], perm[dim] x_perm = K.permute_dimensions(x, perm) output = K.softmax(x_perm) # Permute back perm[dim], perm[-1] = perm[-1], perm[dim] output = K.permute_dimensions(x, output) return output else: raise ValueError("Backend '{}' not supported".format(K.backend()))
def call(self, x, mask=None): y = K.dot(x, self.att_W) if not self.activation: if K.backend() == 'theano': weights = K.theano.tensor.tensordot(self.att_v, y, axes=[0, 2]) elif K.backend() == 'tensorflow': weights = K.tensorflow.python.ops.math_ops.tensordot(self.att_v, y, axes=[0, 2]) elif self.activation == 'tanh': if K.backend() == 'theano': weights = K.theano.tensor.tensordot(self.att_v, K.tanh(y), axes=[0, 2]) elif K.backend() == 'tensorflow': weights = K.tensorflow.python.ops.math_ops.tensordot(self.att_v, K.tanh(y), axes=[0, 2]) weights = K.softmax(weights) out = x * K.permute_dimensions(K.repeat(weights, x.shape[2]), [0, 2, 1]) if self.op == 'attsum': out = out.sum(axis=1) elif self.op == 'attmean': out = out.sum(axis=1) / mask.sum(axis=1, keepdims=True) return K.cast(out, K.floatx())
def keras_test(func): """Function wrapper to clean up after TensorFlow tests. # Arguments func: test function to clean up after. # Returns A function wrapping the input function. """ @six.wraps(func) def wrapper(*args, **kwargs): output = func(*args, **kwargs) if K.backend() == 'tensorflow': K.clear_session() return output return wrapper
def call(self, inputs): stim = inputs[0] center = inputs[1] centers_x = self.XX[None, :, :, None] - center[:, 0, None, None, None] - self.centers[0][None, None, None, :] centers_y = self.YY[None, :, :, None] - center[:, 1, None, None, None] - self.centers[1][None, None, None, :] senv = self.stds[None, None, None, :] gauss = self.gauss_scale * (K.square(self.dx) / (2 * np.pi * K.square(senv) + K.epsilon()))*K.exp(-(K.square(centers_x) + K.square(centers_y))/(2.0 * K.square(senv))) # gauss = (1 / K.sqrt(2 * np.pi * K.square(senv) + K.epsilon()))*K.exp(-(K.square(centers_x) + K.square(centers_y))/(2.0 * K.square(senv))) # gauss /= K.max(gauss, axis=(1, 2), keepdims=True) gauss = K.reshape(gauss, self.kernel_shape) if K.backend() == 'theano': output = K.sum(stim[..., None] * K.pattern_broadcast(gauss, self.kernel_broadcast), axis=self.filter_axes, keepdims=False) else: output = K.sum(stim[..., None] * gauss, axis=self.filter_axes, keepdims=False) return output
def classifier_layers(x, input_shape, stage_num, trainable=False): # compile times on theano tend to be very high, so we use smaller ROI pooling regions to workaround # (hence a smaller stride in the region that follows the ROI pool) if K.backend() == 'tensorflow': x = conv_block_td(x, 3, [512, 512, 1024], stage=stage_num, block='a', input_shape=input_shape, strides=(1, 2), trainable=trainable) elif K.backend() == 'theano': x = conv_block_td(x, 3, [512, 512, 1024], stage=stage_num, block='a', input_shape=input_shape, strides=(1, 1), trainable=trainable) print 'INFO: Classifier layers x block a: ', x x = identity_block_td(x, 3, [512, 512, 1024], stage=stage_num, block='c', trainable=trainable) print 'INFO: Classifier layers x block b: ', x x = identity_block_td(x, 3, [512, 512, 1024], stage=stage_num, block='d', trainable=trainable) print 'INFO: Classifier layers x block c: ', x #x = TimeDistributed(AveragePooling2D((2, 1)), name='avg_pool')(x) return x
def call(self, x, mask=None): if K.backend() == 'theano': T = K.theano.tensor p0, p1 = self.padding[0], self.padding[1] y = T.zeros((x.shape[0], x.shape[1], x.shape[2]+(2*p0), x.shape[3]+(2*p1)), dtype=K.theano.config.floatX) y = T.set_subtensor(y[:, :, p0:-p0, p1:-p1], x) y = T.set_subtensor(y[:, :, :p0, p1:-p1], x[:, :, p0:0:-1, :]) y = T.set_subtensor(y[:, :, -p0:, p1:-p1], x[:, :, -2:-2-p0:-1]) y = T.set_subtensor(y[:, :, p0:-p0, :p1], x[:, :, :, p1:0:-1]) y = T.set_subtensor(y[:, :, p0:-p0, -p1:], x[:, :, :, -2:-2-p1:-1]) y = T.set_subtensor(y[:, :, :p0, :p1], x[:, :, p0:0:-1, p1:0:-1]) y = T.set_subtensor(y[:, :, -p0:, :p1], x[:, :, -2:-2-p0:-1, p1:0:-1]) y = T.set_subtensor(y[:, :, :p0, -p1:], x[:, :, p0:0:-1, -2:-2-p1:-1]) y = T.set_subtensor(y[:, :, -p0:, -p1:], x[:, :, -2:-2-p0:-1, -2:-2-p1:-1]) else: raise NotImplemented("Please complete `CycGAN/layers/padding.py` to run on backend {}.".format(K.backend())) return y
def call(self, x, mask=None): def image_expand(tensor): return K.expand_dims(K.expand_dims(tensor, -1), -1) def batch_image_expand(tensor): return image_expand(K.expand_dims(tensor, 0)) hw = K.cast(x.shape[2] * x.shape[3], K.floatx()) mu = K.sum(x, [-1, -2]) / hw mu_vec = image_expand(mu) sig2 = K.sum(K.square(x - mu_vec), [-1, -2]) / hw y = (x - mu_vec) / (K.sqrt(image_expand(sig2)) + K.epsilon()) scale = batch_image_expand(self.scale) shift = batch_image_expand(self.shift) return scale*y + shift # else: # raise NotImplemented("Please complete `CycGAN/layers/padding.py` to run on backend {}.".format(K.backend()))
def configure_hardware(RAND_SEED): '''configure rand seed, GPU''' from keras import backend as K if K.backend() == 'tensorflow': K.tf.set_random_seed(RAND_SEED) else: K.theano.tensor.shared_randomstreams.RandomStreams(seed=RAND_SEED) if K.backend() != 'tensorflow': # GPU config for tf only return process_num = PARALLEL_PROCESS_NUM if args.param_selection else 1 tf = K.tf gpu_options = tf.GPUOptions( allow_growth=True, per_process_gpu_memory_fraction=1./float(process_num)) config = tf.ConfigProto( gpu_options=gpu_options, allow_soft_placement=True) sess = tf.Session(config=config) K.set_session(sess) return sess
def __init__(self, img_height, img_width, this_anchor_lwhs, **kwargs): ''' Arguments: img_height (int): The height of the input images. img_width (int): The width of the input images. this_anchor_lwhs (array): A 2D Numpy array of shape `(n, 3)` where the last axis contains `[length, width, height]` for each of the `n` box shapes for this classifier layer. Note that `n` is the number of boxes per cell, not the total number of boxes for the classifier layer. ''' if K.backend() != 'tensorflow': raise TypeError("This layer only supports TensorFlow at the moment, but you are using the {} backend.".format(K.backend())) if (this_scale < 0) or (next_scale < 0) or (this_scale > 1) or (next_scale > 1): raise ValueError("`this_scale` and `next_scale` must be in [0, 1], but `this_scale` == {}, `next_scale` == {}".format(this_scale, next_scale)) self.img_height = img_height self.img_width = img_width self.this_anchor_lwhs = this_anchor_lwhs self.n_boxes = this_anchor_lwhs.shape[0] # Compute the number of boxes per cell super(AnchorBoxes3D, self).__init__(**kwargs)
def mix_gaussian_loss(x, mu, log_sig, w): ''' Combine the mixture of gaussian distribution and the loss into a single function so that we can do the log sum exp trick for numerical stability... ''' if K.backend() == "tensorflow": x.set_shape([None, 1]) gauss = log_norm_pdf(K.repeat_elements(x=x, rep=mu.shape[1], axis=1), mu, log_sig) # TODO: get rid of clipping. gauss = K.clip(gauss, -40, 40) max_gauss = K.maximum((0.), K.max(gauss)) # log sum exp trick... gauss = gauss - max_gauss out = K.sum(w * K.exp(gauss), axis=1) loss = K.mean(-K.log(out) + max_gauss) return loss
def step(self, input_energy_t, states, return_logZ=True): # not in the following `prev_target_val` has shape = (B, F) # where B = batch_size, F = output feature dim # Note: `i` is of float32, due to the behavior of `K.rnn` prev_target_val, i, chain_energy = states[:3] t = K.cast(i[0, 0], dtype='int32') if len(states) > 3: if K.backend() == 'theano': m = states[3][:, t:(t + 2)] else: m = K.tf.slice(states[3], [0, t], [-1, 2]) input_energy_t = input_energy_t * K.expand_dims(m[:, 0]) chain_energy = chain_energy * K.expand_dims(K.expand_dims(m[:, 0] * m[:, 1])) # (1, F, F)*(B, 1, 1) -> (B, F, F) if return_logZ: energy = chain_energy + K.expand_dims(input_energy_t - prev_target_val, 2) # shapes: (1, B, F) + (B, F, 1) -> (B, F, F) new_target_val = K.logsumexp(-energy, 1) # shapes: (B, F) return new_target_val, [new_target_val, i + 1] else: energy = chain_energy + K.expand_dims(input_energy_t + prev_target_val, 2) min_energy = K.min(energy, 1) argmin_table = K.cast(K.argmin(energy, 1), K.floatx()) # cast for tf-version `K.rnn` return argmin_table, [min_energy, i + 1]
def test_experiment_instance_utils(self, get_model): new_session() model = get_model() model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy']) expe = Experiment(model) expe.model_dict = model expe.backend_name = 'another_backend' expe.model_dict = model assert expe.backend is not None expe = Experiment() print(self)
def test_experiment_generator_setups(self, get_generators): gen_t, data_t, d_stream_t, gen, data, d_stream, nb = get_generators nb_train, nb_val = nb test_model = model() test_model.compile(loss='binary_crossentropy', optimizer='rmsprop') expe = Experiment(test_model) expe.fit_gen([gen_t], [gen], nb_epoch=2, samples_per_epoch=nb_train, nb_val_samples=nb_val, verbose=2, overwrite=True) close_gens(gen_t, data_t, d_stream_t) close_gens(gen, data, d_stream) if K.backend() == 'tensorflow': K.clear_session() print(self)
def test_build_predict_func(self, get_model): """Test the build of a model""" new_session() X_tr = np.ones((train_samples, input_dim)) model = get_model() model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy']) model_name = model.__class__.__name__ pred_func = KTB.build_predict_func(model) tensors = [X_tr] if model_name != 'Model': tensors.append(1.) res = pred_func(tensors) assert len(res[0]) == len(X_tr) if K.backend() == 'tensorflow': K.clear_session() print(self)
def test_fit(self, get_model): "Test the training of a serialized model" new_session() data, data_val = make_data(train_samples, test_samples) model = get_model() model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy']) model_dict = dict() model_dict['model_arch'] = to_dict_w_opt(model) res = KTB.train(copy.deepcopy(model_dict['model_arch']), [data], [data_val], []) res = KTB.fit(NAME, VERSION, model_dict, [data], 'test', [data_val], []) assert len(res) == 4 if K.backend() == 'tensorflow': K.clear_session() print(self)
def build_predict_func(mod): """Build Keras prediction functions based on a Keras model Using inputs and outputs of the graph a prediction function (forward pass) is compiled for prediction purpose. Args: mod(keras.models): a Model or Sequential model Returns: a Keras (Theano or Tensorflow) function """ import keras.backend as K if mod.uses_learning_phase: tensors = mod.inputs + [K.learning_phase()] else: tensors = mod.inputs return K.function(tensors, mod.outputs, updates=mod.state_updates)
def changing_ndim_rnn(step_function, inputs, initial_states, go_backwards=False, mask=None, constants=None, unroll=False, input_length=None, eliminate_mask_dims=None): if K.backend() == 'tensorflow': backend_func = changing_ndim_rnn_tf else: backend_func = changing_ndim_rnn_theano return backend_func(step_function, inputs, initial_states, go_backwards, mask, constants, unroll, input_length, eliminate_mask_dims)
def call(self, x, mask=None): if K.backend() == 'tensorflow': xt = tf.transpose(x, perm=(2, 0 ,1)) gt = tf.gather(xt, self.indices) return tf.transpose(gt, perm=(1, 2, 0)) return x[:, :, self.indices]
def time_distributed_dense(x, w, b=None, dropout=None, input_dim=None, units=None, timesteps=None): """Apply `y . w + b` for every temporal slice y of x. # Arguments x: input tensor. w: weight matrix. b: optional bias vector. dropout: wether to apply dropout (same dropout mask for every temporal slice of the input). input_dim: integer; optional dimensionality of the input. units: integer; optional dimensionality of the output. timesteps: integer; optional number of timesteps. # Returns Output tensor. """ if not input_dim: input_dim = K.shape(x)[2] if not timesteps: timesteps = K.shape(x)[1] if not units: units = K.shape(w)[1] if dropout is not None and 0. < dropout < 1.: # apply the same dropout pattern at every timestep ones = K.ones_like(K.reshape(x[:, 0, :], (-1, input_dim))) dropout_matrix = K.dropout(ones, dropout) expanded_dropout_matrix = K.repeat(dropout_matrix, timesteps) x = K.in_train_phase(x * expanded_dropout_matrix, x) # collapse time dimension and batch dimension together x = K.reshape(x, (-1, input_dim)) x = K.dot(x, w) if b: x += b # reshape to 3D tensor if K.backend() == 'tensorflow': x = K.reshape(x, K.stack([-1, timesteps, units])) x.set_shape([None, None, units]) else: x = K.reshape(x, (-1, timesteps, units)) return x
def prediction_batch(self, input_batch: ndarray) -> ndarray: """Predicts a grapheme probability batch given a spectrogram batch, employing the learned predictive network.""" # Indicates to use prediction phase in order to disable dropout (see backend.learning_phase documentation): return self.get_prediction_batch([input_batch, self.prediction_phase_flag])[0]
def get_prediction_batch(self): return backend.function(self.predictive_net.inputs + [backend.learning_phase()], self.predictive_net.outputs)
def loss_net(self) -> Model: """Returns the network that yields a loss given both input spectrograms and labels. Used for training.""" input_batch = self._input_batch_input label_batch = Input(name=Wav2Letter.InputNames.label_batch, shape=(None,), dtype='int32') label_lengths = Input(name=Wav2Letter.InputNames.label_lengths, shape=(1,), dtype='int64') asg_transition_probabilities_variable = backend.variable(value=self.asg_transition_probabilities, name="asg_transition_probabilities") asg_initial_probabilities_variable = backend.variable(value=self.asg_initial_probabilities, name="asg_initial_probabilities") # Since Keras doesn't currently support loss functions with extra parameters, # we define a custom lambda layer yielding one single real-valued CTC loss given the grapheme probabilities: loss_layer = Lambda(Wav2Letter._asg_lambda if self.use_asg else Wav2Letter._ctc_lambda, name='asg_loss' if self.use_asg else 'ctc_loss', output_shape=(1,), arguments={"transition_probabilities": asg_transition_probabilities_variable, "initial_probabilities": asg_initial_probabilities_variable} if self.use_asg else None) # ([asg_transition_probabilities_variable, asg_initial_probabilities_variable] if self.use_asg else []) # This loss layer is placed atop the predictive network and provided with additional arguments, # namely the label batch and prediction/label sequence lengths: loss = loss_layer( [self.predictive_net(input_batch), label_batch, self._prediction_lengths_input, label_lengths]) loss_net = Model(inputs=[input_batch, label_batch, self._prediction_lengths_input, label_lengths], outputs=[loss]) # Since loss is already calculated in the last layer of the net, we just pass through the results here. # The loss dummy labels have to be given to satify the Keras API. loss_net.compile(loss=lambda dummy_labels, ctc_loss: ctc_loss, optimizer=self.optimizer) return loss_net
def _ctc_lambda(args): prediction_batch, label_batch, prediction_lengths, label_lengths = args return backend.ctc_batch_cost(y_true=label_batch, y_pred=prediction_batch, input_length=prediction_lengths, label_length=label_lengths)
def get_predicted_graphemes_and_loss_batch(self): return backend.function(self.loss_net.inputs + [backend.learning_phase()], [single(self.decoding_net.outputs), single(self.loss_net.outputs)])
def switch(condition, t, e): if K.backend() == 'tensorflow': import tensorflow as tf return tf.where(condition, t, e) elif K.backend() == 'theano': import theano.tensor as tt return tt.switch(condition, t, e)
def classifier_layers(x, input_shape, trainable=False): # compile times on theano tend to be very high, so we use smaller ROI pooling regions to workaround # (hence a smaller stride in the region that follows the ROI pool) if K.backend() == 'tensorflow': x = conv_block_td(x, 3, [512, 512, 2048], stage=5, block='a', input_shape=input_shape, strides=(2, 2), trainable=trainable) elif K.backend() == 'theano': x = conv_block_td(x, 3, [512, 512, 2048], stage=5, block='a', input_shape=input_shape, strides=(1, 1), trainable=trainable) x = identity_block_td(x, 3, [512, 512, 2048], stage=5, block='b', trainable=trainable) x = identity_block_td(x, 3, [512, 512, 2048], stage=5, block='c', trainable=trainable) x = TimeDistributed(AveragePooling2D((7, 7)), name='avg_pool')(x) return x
def get_weight_path(): if K.image_dim_ordering() == 'th': print('pretrained weights not available for VGG with theano backend') return else: return 'vgg16_weights_tf_dim_ordering_tf_kernels.h5'
def call(self, inputs, **kwargs): padding = self.padding pool_size = self.pool_size strides = self.strides if K.backend() == 'tensorflow': ksize = [1, pool_size[0], pool_size[1], 1] padding = padding.upper() strides = [1, strides[0], strides[1], 1] output, argmax = K.tf.nn.max_pool_with_argmax(inputs, ksize=ksize, strides=strides, padding=padding) else: errmsg = '{} backend is not supported for layer {}'.format(K.backend(), type(self).__name__) raise NotImplementedError(errmsg) argmax = K.cast(argmax, K.floatx()) return [output, argmax]
def call(self, inputs, output_shape=None): """ Seen on https://github.com/tensorflow/tensorflow/issues/2169 Replace with unpool op when/if issue merged Add theano backend """ updates, mask = inputs[0], inputs[1] with K.tf.variable_scope(self.name): mask = K.cast(mask, 'int32') input_shape = K.tf.shape(updates, out_type='int32') # calculation new shape if output_shape is None: output_shape = (input_shape[0], input_shape[1] * self.size[0], input_shape[2] * self.size[1], input_shape[3]) self.output_shape1 = output_shape # calculation indices for batch, height, width and feature maps one_like_mask = K.ones_like(mask, dtype='int32') batch_shape = K.concatenate([[input_shape[0]], [1], [1], [1]], axis=0) batch_range = K.reshape(K.tf.range(output_shape[0], dtype='int32'), shape=batch_shape) b = one_like_mask * batch_range y = mask // (output_shape[2] * output_shape[3]) x = (mask // output_shape[3]) % output_shape[2] feature_range = K.tf.range(output_shape[3], dtype='int32') f = one_like_mask * feature_range # transpose indices & reshape update values to one dimension updates_size = K.tf.size(updates) indices = K.transpose(K.reshape(K.stack([b, y, x, f]), [4, updates_size])) values = K.reshape(updates, [updates_size]) ret = K.tf.scatter_nd(indices, values, output_shape) return ret
def depth_to_scale_tf(input, scale, channels): try: import tensorflow as tf except ImportError: print("Could not import Tensorflow for depth_to_scale operation. Please install Tensorflow or switch to Theano backend") exit() def _phase_shift(I, r): ''' Function copied as is from https://github.com/Tetrachrome/subpixel/blob/master/subpixel.py''' bsize, a, b, c = I.get_shape().as_list() bsize = tf.shape(I)[0] # Handling Dimension(None) type for undefined batch dim X = tf.reshape(I, (bsize, a, b, r, r)) X = tf.transpose(X, (0, 1, 2, 4, 3)) # bsize, a, b, 1, 1 X = tf.split(1, a, X) # a, [bsize, b, r, r] X = tf.concat(2, [tf.squeeze(x) for x in X]) # bsize, b, a*r, r X = tf.split(1, b, X) # b, [bsize, a*r, r] X = tf.concat(2, [tf.squeeze(x) for x in X]) # bsize, a*r, b*r return tf.reshape(X, (bsize, a * r, b * r, 1)) if channels > 1: Xc = tf.split(3, 3, input) X = tf.concat(3, [_phase_shift(x, scale) for x in Xc]) else: X = _phase_shift(input, scale) return X
def call(self, x, mask=None): if K.backend() == "theano": y = depth_to_scale_th(x, self.r, self.channels) else: y = depth_to_scale_tf(x, self.r, self.channels) return y
def keras_backend(): if hasattr(K, 'backend'): return K.backend() else: return K._BACKEND
def _sort_weights_by_name(self, weights): """Sorts weights by name and returns them.""" if not weights: return [] if K.backend() == 'theano': key = lambda x: x.name if x.name else x.auto_name else: key = lambda x: x.name weights.sort(key=key) return weights
def with_tensorflow(f): def inner(*args, **kwargs): if K.backend() == "tensorflow": return f(K.tf, *args, **kwargs) return inner