Python keras.backend.one_hot() Examples
The following are 30
code examples of keras.backend.one_hot().
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.
You may also want to check out all available functions/classes of the module
keras.backend
, or try the search function
.
Example #1
Source File: loss_function.py From Keras-FCN with MIT License | 6 votes |
def softmax_sparse_crossentropy_ignoring_last_label(y_true, y_pred): y_pred = K.reshape(y_pred, (-1, K.int_shape(y_pred)[-1])) log_softmax = tf.nn.log_softmax(y_pred) y_true = K.one_hot(tf.to_int32(K.flatten(y_true)), K.int_shape(y_pred)[-1]+1) unpacked = tf.unstack(y_true, axis=-1) y_true = tf.stack(unpacked[:-1], axis=-1) cross_entropy = -K.sum(y_true * log_softmax, axis=1) cross_entropy_mean = K.mean(cross_entropy) return cross_entropy_mean # Softmax cross-entropy loss function for coco segmentation # and models which expect but do not apply sigmoid on each entry # tensorlow only
Example #2
Source File: losses.py From deep-learning-explorer with Apache License 2.0 | 6 votes |
def softmax_sparse_crossentropy_ignoring_last_label(y_true, y_pred): ''' Softmax cross-entropy loss function for pascal voc segmentation and models which do not perform softmax. tensorlow only ''' y_pred = KB.reshape(y_pred, (-1, KB.int_shape(y_pred)[-1])) log_softmax = tf.nn.log_softmax(y_pred) y_true = KB.one_hot(tf.to_int32(KB.flatten(y_true)), KB.int_shape(y_pred)[-1]+1) unpacked = tf.unstack(y_true, axis=-1) y_true = tf.stack(unpacked[:-1], axis=-1) cross_entropy = -KB.sum(y_true * log_softmax, axis=1) cross_entropy_mean = KB.mean(cross_entropy) return cross_entropy_mean
Example #3
Source File: memory.py From qlearning4k with MIT License | 6 votes |
def set_batch_function(self, model, input_shape, batch_size, nb_actions, gamma): input_dim = np.prod(input_shape) samples = K.placeholder(shape=(batch_size, input_dim * 2 + 3)) S = samples[:, 0 : input_dim] a = samples[:, input_dim] r = samples[:, input_dim + 1] S_prime = samples[:, input_dim + 2 : 2 * input_dim + 2] game_over = samples[:, 2 * input_dim + 2 : 2 * input_dim + 3] r = K.reshape(r, (batch_size, 1)) r = K.repeat(r, nb_actions) r = K.reshape(r, (batch_size, nb_actions)) game_over = K.repeat(game_over, nb_actions) game_over = K.reshape(game_over, (batch_size, nb_actions)) S = K.reshape(S, (batch_size, ) + input_shape) S_prime = K.reshape(S_prime, (batch_size, ) + input_shape) X = K.concatenate([S, S_prime], axis=0) Y = model(X) Qsa = K.max(Y[batch_size:], axis=1) Qsa = K.reshape(Qsa, (batch_size, 1)) Qsa = K.repeat(Qsa, nb_actions) Qsa = K.reshape(Qsa, (batch_size, nb_actions)) delta = K.reshape(self.one_hot(a, nb_actions), (batch_size, nb_actions)) targets = (1 - delta) * Y[:batch_size] + delta * (r + gamma * (1 - game_over) * Qsa) self.batch_function = K.function(inputs=[samples], outputs=[S, targets])
Example #4
Source File: layers.py From sequence-tagging-ner with Apache License 2.0 | 6 votes |
def loss_function(self): if self.learn_mode == 'join': def loss(y_true, y_pred): assert self._inbound_nodes, 'CRF has not connected to any layer.' assert not self._outbound_nodes, 'When learn_model="join", CRF must be the last layer.' if self.sparse_target: y_true = K.one_hot(K.cast(y_true[:, :, 0], 'int32'), self.units) X = self._inbound_nodes[0].input_tensors[0] mask = self._inbound_nodes[0].input_masks[0] nloglik = self.get_negative_log_likelihood(y_true, X, mask) return nloglik return loss else: if self.sparse_target: return sparse_categorical_crossentropy else: return categorical_crossentropy
Example #5
Source File: capsule.py From Keras-TextClassification with MIT License | 6 votes |
def call(self, inputs, **kwargs): if type(inputs) is list: # true label is provided with shape = [None, n_classes], i.e. one-hot code. assert len(inputs) == 2 inputs, mask = inputs else: # if no true label, mask by the max length of capsules. Mainly used for prediction # compute lengths of capsules x = K.sqrt(K.sum(K.square(inputs), -1)) # generate the mask which is a one-hot code. # mask.shape=[None, n_classes]=[None, num_capsule] mask = K.one_hot(indices=K.argmax(x, 1), num_classes=x.get_shape().as_list()[1]) # inputs.shape=[None, num_capsule, dim_capsule] # mask.shape=[None, num_capsule] # masked.shape=[None, num_capsule * dim_capsule] masked = K.batch_flatten(inputs * K.expand_dims(mask, -1)) return masked
Example #6
Source File: modeling.py From BERT_with_keras with MIT License | 6 votes |
def call(self, inputs, mask=None): if self.use_token_type: assert len(inputs) == 2, "`token_type_ids` must be specified if `use_token_type` is True." output = inputs[0] _, seq_length, input_width = K.int_shape(output) # print(inputs) assert seq_length == K.int_shape(inputs[1])[1], "width of `token_type_ids` must be equal to `seq_length`" token_type_ids = inputs[1] # assert K.int_shape(token_type_ids)[1] <= self.token_type_vocab_size flat_token_type_ids = K.reshape(token_type_ids, [-1]) flat_token_type_ids = K.cast(flat_token_type_ids, dtype='int32') token_type_one_hot_ids = K.one_hot(flat_token_type_ids, num_classes=self.token_type_vocab_size) token_type_embeddings = K.dot(token_type_one_hot_ids, self.token_type_table) token_type_embeddings = K.reshape(token_type_embeddings, shape=[-1, seq_length, input_width]) # print(token_type_embeddings) output += token_type_embeddings else: output = inputs seq_length = K.int_shape(inputs)[1] if self.use_position_embeddings: position_embeddings = K.slice(self.full_position_embeddings, [0, 0], [seq_length, -1]) output += position_embeddings return output
Example #7
Source File: capslayers.py From deepcaps with MIT License | 6 votes |
def call(self, inputs, **kwargs): if isinstance(inputs, list): # true label is provided with shape = [None, n_classes], i.e. one-hot code. assert len(inputs) == 2 inputs, mask = inputs else: # if no true label, mask by the max length of capsules. Mainly used for prediction # compute lengths of capsules x = K.sqrt(K.sum(K.square(inputs), -1)) # generate the mask which is a one-hot code. # mask.shape=[None, n_classes]=[None, num_capsule] mask = K.one_hot(indices=K.argmax(x, 1), num_classes=x.get_shape().as_list()[1]) # inputs.shape=[None, num_capsule, dim_capsule] # mask.shape=[None, num_capsule] # masked.shape=[None, num_capsule * dim_capsule] masked = K.batch_flatten(inputs * K.expand_dims(mask, -1)) return masked
Example #8
Source File: layers.py From anago with MIT License | 6 votes |
def loss_function(self): if self.learn_mode == 'join': def loss(y_true, y_pred): assert self._inbound_nodes, 'CRF has not connected to any layer.' assert not self._outbound_nodes, 'When learn_model="join", CRF must be the last layer.' if self.sparse_target: y_true = K.one_hot(K.cast(y_true[:, :, 0], 'int32'), self.units) X = self._inbound_nodes[0].input_tensors[0] mask = self._inbound_nodes[0].input_masks[0] nloglik = self.get_negative_log_likelihood(y_true, X, mask) return nloglik return loss else: if self.sparse_target: return sparse_categorical_crossentropy else: return categorical_crossentropy
Example #9
Source File: capsulelayers.py From CapsNet-Keras with MIT License | 6 votes |
def call(self, inputs, **kwargs): if type(inputs) is list: # true label is provided with shape = [None, n_classes], i.e. one-hot code. assert len(inputs) == 2 inputs, mask = inputs else: # if no true label, mask by the max length of capsules. Mainly used for prediction # compute lengths of capsules x = K.sqrt(K.sum(K.square(inputs), -1)) # generate the mask which is a one-hot code. # mask.shape=[None, n_classes]=[None, num_capsule] mask = K.one_hot(indices=K.argmax(x, 1), num_classes=x.get_shape().as_list()[1]) # inputs.shape=[None, num_capsule, dim_capsule] # mask.shape=[None, num_capsule] # masked.shape=[None, num_capsule * dim_capsule] masked = K.batch_flatten(inputs * K.expand_dims(mask, -1)) return masked
Example #10
Source File: loss.py From maskrcnn with MIT License | 6 votes |
def sparse_categorical_crossentropy(gt_ids, pred_one_hot_post_softmax): """ K.sparse_categorical_crossentropyだと結果がNaNになる。。。 0割り算が発生しているかも。 https://qiita.com/4Ui_iUrz1/items/35a8089ab0ebc98061c1 対策として、微少値を用いてlog(0)にならないよう調整した本関数を作成。 """ gt_ids = log.tfprint(gt_ids, "cross:gt_ids:") pred_one_hot_post_softmax = log.tfprint(pred_one_hot_post_softmax, "cross:pred_one_hot_post_softmax:") gt_one_hot = K.one_hot(gt_ids, K.shape(pred_one_hot_post_softmax)[-1]) gt_one_hot = log.tfprint(gt_one_hot, "cross:gt_one_hot:") epsilon = K.epsilon() # 1e-07 loss = -K.sum( gt_one_hot * K.log( tf.clip_by_value(pred_one_hot_post_softmax, epsilon, 1 - epsilon)), axis=-1) loss = log.tfprint(loss, "cross:loss:") return loss
Example #11
Source File: learn_labelembedding.py From semantic-embeddings with MIT License | 6 votes |
def labelembed_loss(out1, out2, tar, targets, tau = 2., alpha = 0.9, beta = 0.5, num_classes = 100): out2_prob = K.softmax(out2) tau2_prob = K.stop_gradient(K.softmax(out2 / tau)) soft_tar = K.stop_gradient(K.softmax(tar)) L_o1_y = K.sparse_categorical_crossentropy(output = K.softmax(out1), target = targets) pred = K.argmax(out2, axis = -1) mask = K.stop_gradient(K.cast(K.equal(pred, K.cast(targets, 'int64')), K.floatx())) L_o1_emb = -cross_entropy(out1, soft_tar) # pylint: disable=invalid-unary-operand-type L_o2_y = K.sparse_categorical_crossentropy(output = out2_prob, target = targets) L_emb_o2 = -cross_entropy(tar, tau2_prob) * mask * (K.cast(K.shape(mask)[0], K.floatx())/(K.sum(mask)+1e-8)) # pylint: disable=invalid-unary-operand-type L_re = K.relu(K.sum(out2_prob * K.one_hot(K.cast(targets, 'int64'), num_classes), axis = -1) - alpha) return beta * L_o1_y + (1-beta) * L_o1_emb + L_o2_y + L_emb_o2 + L_re
Example #12
Source File: capsulelayers.py From Multi-level-DCNet with GNU General Public License v3.0 | 6 votes |
def call(self, inputs, **kwargs): if type(inputs) is list: # true label is provided with shape = [None, n_classes], i.e. one-hot code. assert len(inputs) == 2 inputs, mask = inputs else: # if no true label, mask by the max length of capsules. Mainly used for prediction # compute lengths of capsules x = K.sqrt(K.sum(K.square(inputs), -1)) # generate the mask which is a one-hot code. # mask.shape=[None, n_classes]=[None, num_capsule] mask = K.one_hot(indices=K.argmax(x, 1), num_classes=x.get_shape().as_list()[1]) # inputs.shape=[None, num_capsule, dim_capsule] # mask.shape=[None, num_capsule] # masked.shape=[None, num_capsule * dim_capsule] masked = K.batch_flatten(inputs * K.expand_dims(mask, -1)) return masked
Example #13
Source File: self_attention.py From nlp_toolkit with MIT License | 6 votes |
def Mask(self, inputs, seq_len, mode='mul'): """ # Arguments: inputs: input tensor with shape (batch_size, seq_len, input_size) seq_len: Each sequence's actual length with shape (batch_size,) mode: mul: mask the rest dim with zero, used before fully-connected layer add: subtract a big constant from the rest, used before softmax layer # Reutrns: Masked tensors with the same shape of input tensor """ if seq_len is None: return inputs else: mask = K.one_hot(seq_len[:, 0], K.shape(inputs)[1]) mask = 1 - K.cumsum(mask, 1) for _ in range(len(inputs.shape) - 2): mask = K.expand_dims(mask, 2) if mode == 'mul': return inputs * mask if mode == 'add': return inputs - (1 - mask) * 1e12
Example #14
Source File: loss_func.py From Keras-FCN with MIT License | 6 votes |
def mean_acc(y_true, y_pred): s = K.shape(y_true) # reshape such that w and h dim are multiplied together y_true_reshaped = K.reshape( y_true, tf.stack( [-1, s[1]*s[2], s[-1]] ) ) y_pred_reshaped = K.reshape( y_pred, tf.stack( [-1, s[1]*s[2], s[-1]] ) ) # correctly classified clf_pred = K.one_hot( K.argmax(y_pred_reshaped), nb_classes = s[-1]) equal_entries = K.cast(K.equal(clf_pred,y_true_reshaped), dtype='float32') * y_true_reshaped correct_pixels_per_class = K.sum(equal_entries, axis=1) n_pixels_per_class = K.sum(y_true_reshaped,axis=1) acc = correct_pixels_per_class / n_pixels_per_class acc_mask = tf.is_finite(acc) acc_masked = tf.boolean_mask(acc,acc_mask) return K.mean(acc_masked)
Example #15
Source File: loss_func.py From Keras-FCN with MIT License | 6 votes |
def mean_IoU(y_true, y_pred): s = K.shape(y_true) # reshape such that w and h dim are multiplied together y_true_reshaped = K.reshape( y_true, tf.stack( [-1, s[1]*s[2], s[-1]] ) ) y_pred_reshaped = K.reshape( y_pred, tf.stack( [-1, s[1]*s[2], s[-1]] ) ) # correctly classified clf_pred = K.one_hot( K.argmax(y_pred_reshaped), nb_classes = s[-1]) equal_entries = K.cast(K.equal(clf_pred,y_true_reshaped), dtype='float32') * y_true_reshaped intersection = K.sum(equal_entries, axis=1) union_per_class = K.sum(y_true_reshaped,axis=1) + K.sum(y_pred_reshaped,axis=1) iou = intersection / (union_per_class - intersection) iou_mask = tf.is_finite(iou) iou_masked = tf.boolean_mask(iou,iou_mask) return K.mean( iou_masked )
Example #16
Source File: layers.py From indic_tagger with Apache License 2.0 | 6 votes |
def loss_function(self): if self.learn_mode == 'join': def loss(y_true, y_pred): assert self._inbound_nodes, 'CRF has not connected to any layer.' assert not self._outbound_nodes, 'When learn_model="join", CRF must be the last layer.' if self.sparse_target: y_true = K.one_hot(K.cast(y_true[:, :, 0], 'int32'), self.units) X = self._inbound_nodes[0].input_tensors[0] mask = self._inbound_nodes[0].input_masks[0] nloglik = self.get_negative_log_likelihood(y_true, X, mask) return nloglik return loss else: if self.sparse_target: return sparse_categorical_crossentropy else: return categorical_crossentropy
Example #17
Source File: breakout_dueling_ddqn.py From reinforcement-learning with MIT License | 6 votes |
def optimizer(self): a = K.placeholder(shape=(None, ), dtype='int32') y = K.placeholder(shape=(None, ), dtype='float32') py_x = self.model.output a_one_hot = K.one_hot(a, self.action_size) q_value = K.sum(py_x * a_one_hot, axis=1) error = K.abs(y - q_value) quadratic_part = K.clip(error, 0.0, 1.0) linear_part = error - quadratic_part loss = K.mean(0.5 * K.square(quadratic_part) + linear_part) optimizer = RMSprop(lr=0.00025, epsilon=0.01) updates = optimizer.get_updates(self.model.trainable_weights, [], loss) train = K.function([self.model.input, a, y], [loss], updates=updates) return train # approximate Q function using Convolution Neural Network # state is input and Q Value of each action is output of network # dueling network's Q Value is sum of advantages and state value
Example #18
Source File: breakout_dqn.py From reinforcement-learning with MIT License | 6 votes |
def optimizer(self): a = K.placeholder(shape=(None,), dtype='int32') y = K.placeholder(shape=(None,), dtype='float32') py_x = self.model.output a_one_hot = K.one_hot(a, self.action_size) q_value = K.sum(py_x * a_one_hot, axis=1) error = K.abs(y - q_value) quadratic_part = K.clip(error, 0.0, 1.0) linear_part = error - quadratic_part loss = K.mean(0.5 * K.square(quadratic_part) + linear_part) optimizer = RMSprop(lr=0.00025, epsilon=0.01) updates = optimizer.get_updates(self.model.trainable_weights, [], loss) train = K.function([self.model.input, a, y], [loss], updates=updates) return train # approximate Q function using Convolution Neural Network # state is input and Q Value of each action is output of network
Example #19
Source File: capsule_layers.py From SegCaps with Apache License 2.0 | 6 votes |
def call(self, inputs, **kwargs): if type(inputs) is list: assert len(inputs) == 2 input, mask = inputs _, hei, wid, _, _ = input.get_shape() if self.resize_masks: mask = tf.image.resize_bicubic(mask, (hei.value, wid.value)) mask = K.expand_dims(mask, -1) if input.get_shape().ndims == 3: masked = K.batch_flatten(mask * input) else: masked = mask * input else: if inputs.get_shape().ndims == 3: x = K.sqrt(K.sum(K.square(inputs), -1)) mask = K.one_hot(indices=K.argmax(x, 1), num_classes=x.get_shape().as_list()[1]) masked = K.batch_flatten(K.expand_dims(mask, -1) * inputs) else: masked = inputs return masked
Example #20
Source File: grad-cam.py From keras-grad-cam with MIT License | 5 votes |
def target_category_loss(x, category_index, nb_classes): return tf.multiply(x, K.one_hot([category_index], nb_classes))
Example #21
Source File: crf.py From keras-crf-layer with MIT License | 5 votes |
def call(self, inputs, mask=None, **kwargs): inputs, sequence_lengths = inputs self.sequence_lengths = K.flatten(sequence_lengths) y_pred = self.viterbi_decode(inputs, self.sequence_lengths) nb_classes = self.input_spec[0].shape[2] y_pred_one_hot = K.one_hot(y_pred, nb_classes) return K.in_train_phase(inputs, y_pred_one_hot)
Example #22
Source File: keras_bert_layer.py From nlp_xiaojiang with MIT License | 5 votes |
def viterbi_decoding(self, X, mask=None): input_energy = self.activation(K.dot(X, self.kernel) + self.bias) if self.use_boundary: input_energy = self.add_boundary_energy( input_energy, mask, self.left_boundary, self.right_boundary) argmin_tables = self.recursion(input_energy, mask, return_logZ=False) argmin_tables = K.cast(argmin_tables, 'int32') # backward to find best path, `initial_best_idx` can be any, # as all elements in the last argmin_table are the same argmin_tables = K.reverse(argmin_tables, 1) # matrix instead of vector is required by tf `K.rnn` initial_best_idx = [K.expand_dims(argmin_tables[:, 0, 0])] if K.backend() == 'theano': initial_best_idx = [K.T.unbroadcast(initial_best_idx[0], 1)] def gather_each_row(params, indices): n = K.shape(indices)[0] if K.backend() == 'theano': return params[K.T.arange(n), indices] else: indices = K.transpose(K.stack([K.tf.range(n), indices])) return K.tf.gather_nd(params, indices) def find_path(argmin_table, best_idx): next_best_idx = gather_each_row(argmin_table, best_idx[0][:, 0]) next_best_idx = K.expand_dims(next_best_idx) if K.backend() == 'theano': next_best_idx = K.T.unbroadcast(next_best_idx, 1) return next_best_idx, [next_best_idx] _, best_paths, _ = K.rnn(find_path, argmin_tables, initial_best_idx, input_length=K.int_shape(X)[1], unroll=self.unroll) best_paths = K.reverse(best_paths, 1) best_paths = K.squeeze(best_paths, 2) return K.one_hot(best_paths, self.units)
Example #23
Source File: keras_bert_layer.py From nlp_xiaojiang with MIT License | 5 votes |
def crf_nll(y_true, y_pred): """The negative log-likelihood for linear chain Conditional Random Field (CRF). This loss function is only used when the `layers.CRF` layer is trained in the "join" mode. # Arguments y_true: tensor with true targets. y_pred: tensor with predicted targets. # Returns A scalar representing corresponding to the negative log-likelihood. # Raises TypeError: If CRF is not the last layer. # About GitHub If you open an issue or a pull request about CRF, please add `cc @lzfelix` to notify Luiz Felix. """ crf, idx = y_pred._keras_history[:2] if crf._outbound_nodes: raise TypeError('When learn_model="join", CRF must be the last layer.') if crf.sparse_target: y_true = K.one_hot(K.cast(y_true[:, :, 0], 'int32'), crf.units) X = crf._inbound_nodes[idx].input_tensors[0] mask = crf._inbound_nodes[idx].input_masks[0] nloglik = crf.get_negative_log_likelihood(y_true, X, mask) # 新加的 # nloglik = k_keras.abs(nloglik) return nloglik
Example #24
Source File: modeling.py From BERT_with_keras with MIT License | 5 votes |
def gather_indexes(sequence_tensor, positions): """Gathers the vectors at the specific positions over a minibatch. sequence_tensor shape: [batch_size, seq_length, width] positions shape: [batch_size, positions_maxlen] """ positions = K.cast(positions, dtype='int32') sequence_shape = K.int_shape(sequence_tensor) seq_length = sequence_shape[1] positions_length = K.int_shape(positions)[1] flat_positions = K.reshape(positions,[-1]) flat_positions = K.one_hot(flat_positions, seq_length) flat_positions = K.reshape(flat_positions, [-1, positions_length, seq_length]) output_tensor = K.batch_dot(flat_positions, sequence_tensor, axes=(2,1)) return output_tensor
Example #25
Source File: grad_cam.py From emotion_recognition with MIT License | 5 votes |
def target_category_loss(x, category_index, num_classes): return tf.multiply(x, K.one_hot([category_index], num_classes))
Example #26
Source File: layers.py From sequence-tagging-ner with Apache License 2.0 | 5 votes |
def viterbi_decoding(self, X, mask=None): input_energy = self.activation(K.dot(X, self.kernel) + self.bias) if self.use_boundary: input_energy = self.add_boundary_energy(input_energy, mask, self.left_boundary, self.right_boundary) argmin_tables = self.recursion(input_energy, mask, return_logZ=False) argmin_tables = K.cast(argmin_tables, 'int32') # backward to find best path, `initial_best_idx` can be any, as all elements in the last argmin_table are the same argmin_tables = K.reverse(argmin_tables, 1) initial_best_idx = [K.expand_dims(argmin_tables[:, 0, 0])] # matrix instead of vector is required by tf `K.rnn` if K.backend() == 'theano': initial_best_idx = [K.T.unbroadcast(initial_best_idx[0], 1)] def gather_each_row(params, indices): n = K.shape(indices)[0] if K.backend() == 'theano': return params[K.T.arange(n), indices] else: indices = K.transpose(K.stack([K.tf.range(n), indices])) return K.tf.gather_nd(params, indices) def find_path(argmin_table, best_idx): next_best_idx = gather_each_row(argmin_table, best_idx[0][:, 0]) next_best_idx = K.expand_dims(next_best_idx) if K.backend() == 'theano': next_best_idx = K.T.unbroadcast(next_best_idx, 1) return next_best_idx, [next_best_idx] _, best_paths, _ = K.rnn(find_path, argmin_tables, initial_best_idx, input_length=K.int_shape(X)[1], unroll=self.unroll) best_paths = K.reverse(best_paths, 1) best_paths = K.squeeze(best_paths, 2) return K.one_hot(best_paths, self.units)
Example #27
Source File: attention.py From Self-Attention-Keras with Apache License 2.0 | 5 votes |
def Mask(self, inputs, seq_len, mode='mul'): if seq_len == None: return inputs else: mask = K.one_hot(seq_len[:, 0], K.shape(inputs)[1]) mask = 1 - K.cumsum(mask, 1) for _ in range(len(inputs.shape) - 2): mask = K.expand_dims(mask, 2) if mode == 'mul': return inputs * mask if mode == 'add': return inputs - (1 - mask) * 1e12
Example #28
Source File: layers.py From anago with MIT License | 5 votes |
def viterbi_decoding(self, X, mask=None): input_energy = self.activation(K.dot(X, self.kernel) + self.bias) if self.use_boundary: input_energy = self.add_boundary_energy(input_energy, mask, self.left_boundary, self.right_boundary) argmin_tables = self.recursion(input_energy, mask, return_logZ=False) argmin_tables = K.cast(argmin_tables, 'int32') # backward to find best path, `initial_best_idx` can be any, as all elements in the last argmin_table are the same argmin_tables = K.reverse(argmin_tables, 1) initial_best_idx = [K.expand_dims(argmin_tables[:, 0, 0])] # matrix instead of vector is required by tf `K.rnn` if K.backend() == 'theano': initial_best_idx = [K.T.unbroadcast(initial_best_idx[0], 1)] def gather_each_row(params, indices): n = K.shape(indices)[0] if K.backend() == 'theano': return params[K.T.arange(n), indices] else: indices = K.transpose(K.stack([K.tf.range(n), indices])) return K.tf.gather_nd(params, indices) def find_path(argmin_table, best_idx): next_best_idx = gather_each_row(argmin_table, best_idx[0][:, 0]) next_best_idx = K.expand_dims(next_best_idx) if K.backend() == 'theano': next_best_idx = K.T.unbroadcast(next_best_idx, 1) return next_best_idx, [next_best_idx] _, best_paths, _ = K.rnn(find_path, argmin_tables, initial_best_idx, input_length=K.int_shape(X)[1], unroll=self.unroll) best_paths = K.reverse(best_paths, 1) best_paths = K.squeeze(best_paths, 2) return K.one_hot(best_paths, self.units)
Example #29
Source File: metrics.py From Keras-FCN with MIT License | 5 votes |
def sparse_accuracy_ignoring_last_label(y_true, y_pred): nb_classes = K.int_shape(y_pred)[-1] y_pred = K.reshape(y_pred, (-1, nb_classes)) y_true = K.one_hot(tf.to_int32(K.flatten(y_true)), nb_classes + 1) unpacked = tf.unstack(y_true, axis=-1) legal_labels = ~tf.cast(unpacked[-1], tf.bool) y_true = tf.stack(unpacked[:-1], axis=-1) return K.sum(tf.to_float(legal_labels & K.equal(K.argmax(y_true, axis=-1), K.argmax(y_pred, axis=-1)))) / K.sum(tf.to_float(legal_labels)) # This IOU implementation is wrong!!!
Example #30
Source File: loss_func.py From Keras-FCN with MIT License | 5 votes |
def pixel_acc(y_true, y_pred): s = K.shape(y_true) # reshape such that w and h dim are multiplied together y_true_reshaped = K.reshape( y_true, tf.stack( [-1, s[1]*s[2], s[-1]] ) ) y_pred_reshaped = K.reshape( y_pred, tf.stack( [-1, s[1]*s[2], s[-1]] ) ) # correctly classified clf_pred = K.one_hot( K.argmax(y_pred_reshaped), nb_classes = s[-1]) correct_pixels_per_class = K.cast( K.equal(clf_pred,y_true_reshaped), dtype='float32') return K.sum(correct_pixels_per_class) / K.cast(K.prod(s), dtype='float32')