我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用keras.backend.cast()。
def add_boundary_energy(x, b_start=None, b_end=None, mask=None): '''Given the observations x, it adds the start boundary energy b_start (resp. end boundary energy b_end on the start (resp. end) elements and multiplies the mask.''' if mask is None: if b_start is not None: x = K.concatenate([x[:, :1, :] + b_start, x[:, 1:, :]], axis=1) if b_end is not None: x = K.concatenate([x[:, :-1, :], x[:, -1:, :] + b_end], axis=1) else: mask = K.cast(mask, K.floatx()) mask = K.expand_dims(mask, 2) x *= mask if b_start is not None: mask_r = K.concatenate([K.zeros_like(mask[:, :1]), mask[:, :-1]], axis=1) start_mask = K.cast(K.greater(mask, mask_r), K.floatx()) x = x + start_mask * b_start if b_end is not None: mask_l = K.concatenate([mask[:, 1:], K.zeros_like(mask[:, -1:])], axis=1) end_mask = K.cast(K.greater(mask, mask_l), K.floatx()) x = x + end_mask * b_end return x
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)
def get_initial_states(self, onto_nse_input, input_mask=None): input_to_read = onto_nse_input # (batch_size, num_words, num_senses, num_hyps, output_dim + 1) memory_input = input_to_read[:, :, :, :, :-1] # (bs, words, senses, hyps, output_dim) if input_mask is None: mem_0 = K.mean(memory_input, axis=(2, 3)) # (batch_size, num_words, output_dim) else: memory_mask = input_mask if K.ndim(onto_nse_input) != K.ndim(input_mask): memory_mask = K.expand_dims(input_mask) memory_mask = K.cast(memory_mask / (K.sum(memory_mask) + K.epsilon()), 'float32') mem_0 = K.sum(memory_input * memory_mask, axis=(2,3)) # (batch_size, num_words, output_dim) flattened_mem_0 = K.batch_flatten(mem_0) initial_states = self.reader.get_initial_states(input_to_read) initial_states += [flattened_mem_0] return initial_states
def call(self, x, mask=None): # x: (batch_size, input_length, input_dim) if mask is None: return K.mean(x, axis=1) # (batch_size, input_dim) else: # This is to remove padding from the computational graph. if K.ndim(mask) > K.ndim(x): # This is due to the bug in Bidirectional that is passing the input mask # instead of computing output mask. # TODO: Fix the implementation of Bidirectional. mask = K.any(mask, axis=(-2, -1)) if K.ndim(mask) < K.ndim(x): mask = K.expand_dims(mask) masked_input = switch(mask, x, K.zeros_like(x)) weights = K.cast(mask / (K.sum(mask) + K.epsilon()), 'float32') return K.sum(masked_input * weights, axis=1) # (batch_size, input_dim)
def call(self, x, mask=None): eij = dot_product(x, self.W) if self.bias: eij += self.b eij = K.tanh(eij) a = K.exp(eij) # apply mask after the exp. will be re-normalized next if mask is not None: # Cast the mask to floatX to avoid float64 upcasting in theano a *= K.cast(mask, K.floatx()) # in some cases especially in the early stages of training the sum may be almost zero # and this results in NaN's. A workaround is to add a very small positive number ? to the sum. # a /= K.cast(K.sum(a, axis=1, keepdims=True), K.floatx()) a /= K.cast(K.sum(a, axis=1, keepdims=True) + K.epsilon(), K.floatx()) a = K.expand_dims(a) weighted_input = x * a return K.sum(weighted_input, axis=1)
def call(self, x, mask=None): uit = dot_product(x, self.W) if self.bias: uit += self.b uit = K.tanh(uit) ait = K.dot(uit, self.u) a = K.exp(ait) # apply mask after the exp. will be re-normalized next if mask is not None: # Cast the mask to floatX to avoid float64 upcasting in theano a *= K.cast(mask, K.floatx()) # in some cases especially in the early stages of training the sum may be almost zero # and this results in NaN's. A workaround is to add a very small positive number ? to the sum. # a /= K.cast(K.sum(a, axis=1, keepdims=True), K.floatx()) a /= K.cast(K.sum(a, axis=1, keepdims=True) + K.epsilon(), K.floatx()) a = K.expand_dims(a) weighted_input = x * a return K.sum(weighted_input, axis=1)
def rpn_loss_regr(num_anchors): def rpn_loss_regr_fixed_num(y_true, y_pred): if K.image_dim_ordering() == 'th': x = y_true[:, 4 * num_anchors:, :, :] - y_pred x_abs = K.abs(x) x_bool = K.less_equal(x_abs, 1.0) return lambda_rpn_regr * K.sum( y_true[:, :4 * num_anchors, :, :] * (x_bool * (0.5 * x * x) + (1 - x_bool) * (x_abs - 0.5))) / K.sum(epsilon + y_true[:, :4 * num_anchors, :, :]) else: x = y_true[:, :, :, 4 * num_anchors:] - y_pred x_abs = K.abs(x) x_bool = K.cast(K.less_equal(x_abs, 1.0), tf.float32) return lambda_rpn_regr * K.sum( y_true[:, :, :, :4 * num_anchors] * (x_bool * (0.5 * x * x) + (1 - x_bool) * (x_abs - 0.5))) / K.sum(epsilon + y_true[:, :, :, :4 * num_anchors]) return rpn_loss_regr_fixed_num
def call(self, x, mask=None): # x: [..., time_steps, features] # ut = [..., time_steps, attention_dims] ut = K.dot(x, self.kernel) if self.use_bias: ut = K.bias_add(ut, self.bias) ut = K.tanh(ut) if self.use_context: ut = ut * self.context_kernel # Collapse `attention_dims` to 1. This indicates the weight for each time_step. ut = K.sum(ut, axis=-1, keepdims=True) # Convert those weights into a distribution but along time axis. # i.e., sum of alphas along `time_steps` axis should be 1. self.at = _softmax(ut, dim=1) if mask is not None: self.at *= K.cast(K.expand_dims(mask, -1), K.floatx()) # Weighted sum along `time_steps` axis. return K.sum(x * self.at, axis=-2)
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 _forward(x, reduce_step, initial_states, U, mask=None): '''Forward recurrence of the linear chain crf.''' def _forward_step(energy_matrix_t, states): alpha_tm1 = states[-1] new_states = reduce_step(K.expand_dims(alpha_tm1, 2) + energy_matrix_t) return new_states[0], new_states U_shared = K.expand_dims(K.expand_dims(U, 0), 0) if mask is not None: mask = K.cast(mask, K.floatx()) mask_U = K.expand_dims(K.expand_dims(mask[:, :-1] * mask[:, 1:], 2), 3) U_shared = U_shared * mask_U inputs = K.expand_dims(x[:, 1:, :], 2) + U_shared inputs = K.concatenate([inputs, K.zeros_like(inputs[:, -1:, :, :])], axis=1) last, values, _ = K.rnn(_forward_step, inputs, initial_states) return last, values
def KerasCost(self,y_true, y_pred): #create a random subsampling of the target instances for the test set #This is rarely going to hit the last entry sample = K.cast(K.round(K.random_uniform_variable(shape=tuple([self.MMDTargetSampleSize]), low=0, high=self.MMDTargetTrainSize-1)),IntType) #this is a subset operation (not a very pretty way to do it) MMDTargetSampleTrain = K.gather(self.MMDTargetTrain,sample) #do the same for the validation set sample = K.cast(K.round(K.random_uniform_variable(shape=tuple([self.MMDTargetSampleSize]), low=0, high=self.MMDTargetValidationSize-1)),IntType) #and the subset operation MMDTargetSampleValidation = K.gather(self.MMDTargetValidation,sample) #create the sample based on whether we are in training or validation steps MMDtargetSample = K.in_train_phase(MMDTargetSampleTrain, MMDTargetSampleValidation) #return the MMD cost for this subset ret= self.cost(self.MMDLayer,MMDtargetSample) #pretty dumb but y_treu has to be in the cost for keras to not barf when cleaning up ret = ret + 0*K.sum(y_pred)+0*K.sum(y_true) return ret
def call(self, x, mask=None): if mask is None: return K.mean(x, axis=1) mask = K.cast(mask, "float32") expanded_mask = K.expand_dims(mask) # zero embedded vectors which come from masked characters x_masked = x * expanded_mask # how many non-masked characters are in each row? mask_counts = K.sum(mask, axis=-1) # add up the vector representations along the time dimension # the result should have dimension (n_samples, n_embedding_dims) x_sums = K.sum(x_masked, axis=1) # cast the number of non-zero elements to float32 and # give it an extra dimension so it can broadcast properly in # an elementwise divsion counts_cast = K.expand_dims(mask_counts) return x_sums / counts_cast
def call(self, x, mask=None): # computes a probability distribution over the timesteps # uses 'max trick' for numerical stability # reshape is done to avoid issue with Tensorflow # and 1-dimensional weights logits = K.dot(x, self.W) x_shape = K.shape(x) logits = K.reshape(logits, (x_shape[0], x_shape[1])) ai = K.exp(logits - K.max(logits, axis=-1, keepdims=True)) # masked timesteps have zero weight if mask is not None: mask = K.cast(mask, K.floatx()) ai = ai * mask att_weights = ai / (K.sum(ai, axis=1, keepdims=True) + K.epsilon()) weighted_input = x * K.expand_dims(att_weights) result = K.sum(weighted_input, axis=1) if self.return_attention: return [result, att_weights] return result
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 )
def rpn_loss_regr(num_anchors): def rpn_loss_regr_fixed_num(y_true, y_pred): if K.image_dim_ordering() == 'th': x = y_true[:, 4 * num_anchors:, :, :] - y_pred x_abs = K.abs(x) x_bool = K.less_equal(x_abs, 1.0) return lambda_rpn_regr * K.sum( y_true[:, :4 * num_anchors, :, :] * (x_bool * (0.5 * x * x) + (1 - x_bool) * (x_abs - 0.5))) / K.sum(epsilon + y_true[:, :4 * num_anchors, :, :]) else: x = y_true[:, :, :, 4 * num_anchors:] - y_pred x_abs = K.abs(x) x_bool = K.cast(K.less_equal(x_abs, 1.0), tf.float32) #return K.sum(x_abs) return lambda_rpn_regr * K.sum( y_true[:, :, :, :4 * num_anchors] * (x_bool * (0.5 * x * x) + (1 - x_bool) * (x_abs - 0.5))) / K.sum(epsilon + y_true[:, :, :, :4 * num_anchors]) return rpn_loss_regr_fixed_num
def errorCalc(original, predicted): """ calculating l1 loss """ if original.shape[0] > predicted.shape[0]: x = original[:predicted.shape[0],:] - predicted x = np.sum(x, axis = 0)/predicted.shape[0] else: x = original - predicted[:original.shape[0],:] x = np.sum(x, axis = 0)/original.shape[0] #print x #print np.sum(x) return np.sum(x) #x_bool = K.cast(K.less_equal(x_abs, 1.0), 'float32') #return lambda_cls_regr * K.sum(y_true[:, :, :4*num_classes] * (x_bool * (0.5 * x * x) + (1 - x_bool) * (x_abs - 0.5))) / K.sum(epsilon + y_true[:, :, :4*num_classes])
def contingency_table(y, z): """Compute contingency table.""" y = K.round(y) z = K.round(z) def count_matches(a, b): tmp = K.concatenate([a, b]) return K.sum(K.cast(K.all(tmp, -1), K.floatx())) ones = K.ones_like(y) zeros = K.zeros_like(y) y_ones = K.equal(y, ones) y_zeros = K.equal(y, zeros) z_ones = K.equal(z, ones) z_zeros = K.equal(z, zeros) tp = count_matches(y_ones, z_ones) tn = count_matches(y_zeros, z_zeros) fp = count_matches(y_zeros, z_ones) fn = count_matches(y_ones, z_zeros) return (tp, tn, fp, fn)
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 SP_pixelwise_loss(y_true,y_pred): y_true_label=y_true[:,:class_number,:,:] y_true_SP_weight=y_true[:,class_number:,:,:] y_pred=K.clip(y_pred,-50.,50.)#prevent overflow sample_num_per_class=K.sum(y_true_label,axis=[2,3],keepdims=True) class_ind=K.cast(K.greater(sample_num_per_class,0.),'float32') avg_sample_num_per_class=K.sum(sample_num_per_class,axis=1,keepdims=True)/K.sum(class_ind,axis=1,keepdims=True) sample_weight_per_class=avg_sample_num_per_class/(sample_num_per_class+0.1) exp_pred=K.exp(y_pred-K.max(y_pred,axis=1,keepdims=True)) y_pred_softmax=exp_pred/K.sum(exp_pred,axis=1,keepdims=True) pixel_wise_loss=-K.log(y_pred_softmax)*y_true_label pixel_wise_loss=pixel_wise_loss*sample_weight_per_class weighter_pixel_wise_loss=K.sum(pixel_wise_loss,axis=1,keepdims=True) return K.mean(weighter_pixel_wise_loss*y_true_SP_weight) #label distribution loss
def call(self, inputs, mask=None): # In case the target shape is not fully defined, # we need access to the shape of x. # solution: # 1) rely on x._keras_shape # 2) fallback: K.int_shape target_shape = self.target_shape target_mask_shape = self.target_mask_shape if -1 in target_shape: # target shape not fully defined input_shape = None try: input_shape = K.int_shape(inputs) except TypeError: pass if input_shape is not None: target_shape = self.compute_output_shape(input_shape)[1:] _result = K.reshape(inputs, (-1,) + target_shape) reshaped_mask = K.reshape(mask, (-1,) + target_mask_shape + (1,)) result = _result * K.cast(reshaped_mask, K.floatx()) return result
def call(self, x): input_shape = K.shape(x) len_i, len_j = input_shape[1], input_shape[2] outputs = [] for nb_bins in self.nb_bins_per_level: bin_size_i = K.cast(len_i, 'int32') // nb_bins bin_size_j = K.cast(len_j, 'int32') // nb_bins for i, j in product(range(nb_bins), range(nb_bins)): # each combination of i,j is a unique rectangle i1, i2 = bin_size_i * i, bin_size_i * (i + 1) j1, j2 = bin_size_j * j, bin_size_j * (j + 1) pooled_features = K.max(x[:, i1:i2, j1:j2, :], axis=(1, 2)) outputs.append(pooled_features) return K.concatenate(outputs, axis=1)
def __init__(self, maxlen, d_L, d_C, d_D, V_C): """ maxlen = maximum input/output word size d_L = language model hidden state (= context vector) size d_C = character features (input embedding vector size) d_D = decoder hidden state h size V_C = character vocabulary """ # extend embeddings to treat zero values as zeros vectors (for y_0 = 0) # but don't do any masking class CharEmb(Embedding): def call(self, x, mask=None): y = super(CharEmb, self).call(x) return y * K.cast(K.expand_dims(x, -1), K.floatx()) c = Input(shape=(d_L,), name='c') y_tm1 = Input(shape=(maxlen,), name='y_tm1', dtype='int32') ye_tm1 = CharEmb(V_C.size + 1, d_C)(y_tm1) h = DecoderGRU(d_D, return_sequences=True)([ye_tm1, c]) s = Maxout(d_C)([h, ye_tm1, RepeatVector(maxlen)(c)]) s = Dropout(.2)(s) c_I = ProjectionOverTime(V_C.size)(s) super(W2C, self).__init__(input=[c, y_tm1], output=c_I, name='W2C')
def add_boundary_energy(x, b_start=None, b_end=None, mask=None): """Given the observations x, it adds the start boundary energy b_start (resp. end boundary energy b_end on the start (resp. end) elements and multiplies the mask.""" if mask is None: if b_start is not None: x = K.concatenate([x[:, :1, :] + b_start, x[:, 1:, :]], axis=1) if b_end is not None: x = K.concatenate([x[:, :-1, :], x[:, -1:, :] + b_end], axis=1) else: mask = K.cast(mask, K.floatx()) mask = K.expand_dims(mask, 2) x *= mask if b_start is not None: mask_r = K.concatenate([K.zeros_like(mask[:, :1]), mask[:, :-1]], axis=1) start_mask = K.cast(K.greater(mask, mask_r), K.floatx()) x = x + start_mask * b_start if b_end is not None: mask_l = K.concatenate([mask[:, 1:], K.zeros_like(mask[:, -1:])], axis=1) end_mask = K.cast(K.greater(mask, mask_l), K.floatx()) x = x + end_mask * b_end return x
def _forward(x, reduce_step, initial_states, U, mask=None): """Forward recurrence of the linear chain crf.""" def _forward_step(energy_matrix_t, states): alpha_tm1 = states[-1] new_states = reduce_step(K.expand_dims(alpha_tm1, 2) + energy_matrix_t) return new_states[0], new_states U_shared = K.expand_dims(K.expand_dims(U, 0), 0) if mask is not None: mask = K.cast(mask, K.floatx()) mask_U = K.expand_dims(K.expand_dims(mask[:, :-1] * mask[:, 1:], 2), 3) U_shared = U_shared * mask_U inputs = K.expand_dims(x[:, 1:, :], 2) + U_shared inputs = K.concatenate([inputs, K.zeros_like(inputs[:, -1:, :, :])], axis=1) last, values, _ = K.rnn(_forward_step, inputs, initial_states) return last, values
def ori_loss(y_true, y_pred, lamb=1.): # clip y_pred = K.tf.clip_by_value(y_pred, K.epsilon(), 1 - K.epsilon()) # get ROI label_seg = K.sum(y_true, axis=-1, keepdims=True) label_seg = K.tf.cast(K.tf.greater(label_seg, 0), K.tf.float32) # weighted cross entropy loss lamb_pos, lamb_neg = 1., 1. logloss = lamb_pos*y_true*K.log(y_pred)+lamb_neg*(1-y_true)*K.log(1-y_pred) logloss = logloss*label_seg # apply ROI logloss = -K.sum(logloss) / (K.sum(label_seg) + K.epsilon()) # coherence loss, nearby ori should be as near as possible mean_kernal = np.reshape(np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]], dtype=np.float32)/8, [3, 3, 1, 1]) sin2angle_ori, cos2angle_ori, modulus_ori = ori2angle(y_pred) sin2angle = K.conv2d(sin2angle_ori, mean_kernal, padding='same') cos2angle = K.conv2d(cos2angle_ori, mean_kernal, padding='same') modulus = K.conv2d(modulus_ori, mean_kernal, padding='same') coherence = K.sqrt(K.square(sin2angle) + K.square(cos2angle)) / (modulus + K.epsilon()) coherenceloss = K.sum(label_seg) / (K.sum(coherence*label_seg) + K.epsilon()) - 1 loss = logloss + lamb*coherenceloss return loss
def mnt_s_loss(y_true, y_pred): # clip y_pred = K.tf.clip_by_value(y_pred, K.epsilon(), 1 - K.epsilon()) # get ROI label_seg = K.tf.cast(K.tf.not_equal(y_true, 0.0), K.tf.float32) y_true = K.tf.where(K.tf.less(y_true,0.0), K.tf.zeros_like(y_true), y_true) # set -1 -> 0 # weighted cross entropy loss total_elements = K.sum(label_seg) + K.epsilon() lamb_pos, lamb_neg = 10., .5 logloss = lamb_pos*y_true*K.log(y_pred)+lamb_neg*(1-y_true)*K.log(1-y_pred) # apply ROI logloss = logloss*label_seg logloss = -K.sum(logloss) / total_elements return logloss # find highest peak using gaussian
def ori_acc_delta_k(y_true, y_pred, k=10, max_delta=180): # get ROI label_seg = K.sum(y_true, axis=-1) label_seg = K.tf.cast(K.tf.greater(label_seg, 0), K.tf.float32) # get pred angle angle = K.cast(K.argmax(ori_highest_peak(y_pred, max_delta), axis=-1), dtype=K.tf.float32)*2.0+1.0 # get gt angle angle_t = K.cast(K.argmax(y_true, axis=-1), dtype=K.tf.float32)*2.0+1.0 # get delta angle_delta = K.abs(angle_t - angle) acc = K.tf.less_equal(K.minimum(angle_delta, max_delta-angle_delta), k) acc = K.cast(acc, dtype=K.tf.float32) # apply ROI acc = acc*label_seg acc = K.sum(acc) / (K.sum(label_seg)+K.epsilon()) return acc
def weightedLoss(y_true, y_pred): # compute weights # a = cv2.blur(y_true, (11,11)) # ind = (a > 0.01) * (a < 0.99) # ind = ind.astype(np.float32) # weights = np.ones(a.shape) a = K.pool2d(y_true, (11,11), strides=(1, 1), padding='same', data_format=None, pool_mode='avg') ind = K.cast(K.greater(a, 0.01), dtype='float32') * K.cast(K.less(a, 0.99), dtype='float32') weights = K.cast(K.greater_equal(a, 0), dtype='float32') w0 = K.sum(weights) # w0 = weights.sum() weights = weights + ind * 2 w1 = K.sum(weights) # w1 = weights.sum() weights = weights / w1 * w0 return weightedBCELoss2d(y_true, y_pred, weights) + weightedSoftDiceLoss(y_true, y_pred, weights)
def call(self, x, mask=None): def get_node_w(node): return self.W[self.node_indices[node], :, :] def get_node_b(node): return self.b[self.node_indices[node], :] def compute_output(input, node=self.root_node): if not hasattr(node, 'left'): return zeros((K.shape(input)[0],)) + self.node_indices[node] else: node_output = K.dot(x, get_node_w(node)) if self.bias: node_output += get_node_b(node) left_prob = node_output[:, 0] right_prob = 1 - node_output[:, 0] left_node_output = compute_output(input, node.left) right_node_output = compute_output(input, node.right) return K.switch(left_prob > right_prob, left_node_output, right_node_output) return K.cast(compute_output(x), 'int32')
def weighted_dice_loss(y_true, y_pred): y_true = K.cast(y_true, 'float32') y_pred = K.cast(y_pred, 'float32') # if we want to get same size of output, kernel size must be odd number if K.int_shape(y_pred)[1] == 128: kernel_size = 11 elif K.int_shape(y_pred)[1] == 256: kernel_size = 21 elif K.int_shape(y_pred)[1] == 512: kernel_size = 21 elif K.int_shape(y_pred)[1] == 1024: kernel_size = 41 else: raise ValueError('Unexpected image size') averaged_mask = K.pool2d( y_true, pool_size=(kernel_size, kernel_size), strides=(1, 1), padding='same', pool_mode='avg') border = K.cast(K.greater(averaged_mask, 0.005), 'float32') * K.cast(K.less(averaged_mask, 0.995), 'float32') weight = K.ones_like(averaged_mask) w0 = K.sum(weight) weight += border * 2 w1 = K.sum(weight) weight *= (w0 / w1) loss = 1 - weighted_dice_coeff(y_true, y_pred, weight) return loss
def weighted_bce_dice_loss(y_true, y_pred): y_true = K.cast(y_true, 'float32') y_pred = K.cast(y_pred, 'float32') # if we want to get same size of output, kernel size must be odd number if K.int_shape(y_pred)[1] == 128: kernel_size = 11 elif K.int_shape(y_pred)[1] == 256: kernel_size = 21 elif K.int_shape(y_pred)[1] == 512: kernel_size = 21 elif K.int_shape(y_pred)[1] == 1024: kernel_size = 41 else: raise ValueError('Unexpected image size') averaged_mask = K.pool2d( y_true, pool_size=(kernel_size, kernel_size), strides=(1, 1), padding='same', pool_mode='avg') border = K.cast(K.greater(averaged_mask, 0.005), 'float32') * K.cast(K.less(averaged_mask, 0.995), 'float32') weight = K.ones_like(averaged_mask) w0 = K.sum(weight) weight += border * 2 w1 = K.sum(weight) weight *= (w0 / w1) loss = weighted_bce_loss(y_true, y_pred, weight) + (1 - weighted_dice_coeff(y_true, y_pred, weight)) return loss
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
def gen_adv_loss(logits, y, loss='logloss', mean=False): """ Generate the loss function. """ if loss == 'training': # use the model's output instead of the true labels to avoid # label leaking at training time y = K.cast(K.equal(logits, K.max(logits, 1, keepdims=True)), "float32") y = y / K.sum(y, 1, keepdims=True) out = K.categorical_crossentropy(logits, y, from_logits=True) elif loss == 'logloss': out = K.categorical_crossentropy(logits, y, from_logits=True) else: raise ValueError("Unknown loss: {}".format(loss)) if mean: out = K.mean(out) else: out = K.sum(out) return out
def call(self, inputs, mask=None): t = inputs timegate = K.abs(self.timegate) period = timegate[0] shift = timegate[1] r_on = timegate[2] phi = ((t - shift) % period) / period # K.switch not consistent between Theano and Tensorflow backend, # so write explicitly. # TODO check if still the case up = K.cast(K.less(phi, r_on * 0.5), K.floatx()) * 2 * phi / r_on mid = ( K.cast(K.less(phi, r_on), K.floatx()) * K.cast(K.greater(phi, r_on * 0.5), K.floatx()) * (2 - (2 * phi / r_on)) ) end = K.cast(K.greater(phi, r_on * 0.5), K.floatx()) * self.alpha * phi k = up + mid + end return k
def prepare_data(imagenet_dir, batch_size, num_threads): # setup image loading dataset = dataset_factory.get_dataset('imagenet', 'validation', imagenet_dir) provider = slim.dataset_data_provider.DatasetDataProvider(dataset, shuffle=False, common_queue_capacity=batch_size * 5, common_queue_min=batch_size) image, label = provider.get(['image', 'label']) # preprocess images and split into batches preprocess_input = preprocessing_factory.get_preprocessing('inception_resnet_v2', is_training=False) image = preprocess_input(image, 299, 299) images, labels = tf.train.batch([image, label], batch_size=batch_size, num_threads=num_threads, capacity=batch_size * 5) # Keras label is different from TF labels = labels - 1 # remove the "background class" labels = K.cast(K.expand_dims(labels, -1), K.floatx()) # Keras labels are 2D float tensors return images, labels, dataset.num_samples
def kde_entropy(output, var): # Kernel density estimate of entropy, in nats dims = K.cast(K.shape(output)[1], K.floatx() ) N = K.cast(K.shape(output)[0], K.floatx() ) normconst = (dims/2.0)*K.log(2*np.pi*var) # get dists matrix x2 = K.expand_dims(K.sum(K.square(output), axis=1), 1) dists = x2 + K.transpose(x2) - 2*K.dot(output, K.transpose(output)) dists = dists / (2*var) lprobs = logsumexp(-dists, axis=1) - K.log(N) - normconst h = -K.mean(lprobs) return h
def post_process(self, prediction, iou_threshold=0.6, score_threshold=0.6): """ Preform non-max suppression to calculate outputs: Using during evaluation/interference Args: out feature map from network Output: Bounding Boxes - Classes - Probabilities """ outputs = PostProcessor(score_threshold, iou_threshold, interpret_prediction, # this is a function self.anchors, self.num_classes, name="non_max_suppression")(prediction) boxes = Lambda(lambda x: x[..., :4], name="boxes")(outputs) scores = Lambda(lambda x: x[..., 4], name="scores")(outputs) classes = Lambda(lambda x: K.cast(x[..., 5], tf.float32), name="classes")(outputs) return boxes, classes, scores
def get_split_averages(input_tensor, input_mask, indices): # Splits input tensor into three parts based on the indices and # returns average of values prior to index, values at the index and # average of values after the index. # input_tensor: (batch_size, input_length, input_dim) # input_mask: (batch_size, input_length) # indices: (batch_size, 1) # (1, input_length) length_range = K.expand_dims(K.arange(K.shape(input_tensor)[1]), dim=0) # (batch_size, input_length) batched_range = K.repeat_elements(length_range, K.shape(input_tensor)[0], 0) tiled_indices = K.repeat_elements(indices, K.shape(input_tensor)[1], 1) # (batch_size, input_length) greater_mask = K.greater(batched_range, tiled_indices) # (batch_size, input_length) lesser_mask = K.lesser(batched_range, tiled_indices) # (batch_size, input_length) equal_mask = K.equal(batched_range, tiled_indices) # (batch_size, input_length) # We also need to mask these masks using the input mask. # (batch_size, input_length) if input_mask is not None: greater_mask = switch(input_mask, greater_mask, K.zeros_like(greater_mask)) lesser_mask = switch(input_mask, lesser_mask, K.zeros_like(lesser_mask)) post_sum = K.sum(switch(K.expand_dims(greater_mask), input_tensor, K.zeros_like(input_tensor)), axis=1) # (batch_size, input_dim) pre_sum = K.sum(switch(K.expand_dims(lesser_mask), input_tensor, K.zeros_like(input_tensor)), axis=1) # (batch_size, input_dim) values_at_indices = K.sum(switch(K.expand_dims(equal_mask), input_tensor, K.zeros_like(input_tensor)), axis=1) # (batch_size, input_dim) post_normalizer = K.expand_dims(K.sum(greater_mask, axis=1) + K.epsilon(), dim=1) # (batch_size, 1) pre_normalizer = K.expand_dims(K.sum(lesser_mask, axis=1) + K.epsilon(), dim=1) # (batch_size, 1) return K.cast(pre_sum / pre_normalizer, 'float32'), values_at_indices, K.cast(post_sum / post_normalizer, 'float32')
def compute_mask(self, input, mask): if mask is None or self.return_mode == "last_output": return None elif self.return_mode == "all_outputs": return mask # (batch_size, input_length) else: # Return mode is output_and_memory # Mask memory corresponding to all the inputs that are masked, and do not mask the output # (batch_size, input_length + 1) return K.cast(K.concatenate([K.zeros_like(mask[:, :1]), mask]), 'uint8')
def dice_whole_mask(mask): def dice_whole_closure(y_true, y_pred): """ Computes the Sorensen-Dice metric, where P come from class 1,2,3,4,0 TP Dice = 2 ------- T + P Parameters ---------- y_true : keras.placeholder Placeholder that contains the ground truth labels of the classes y_pred : keras.placeholder Placeholder that contains the class prediction Returns ------- scalar Dice metric """ y_pred_decision = K.cast(y_pred / K.max(y_pred, axis=1, keepdims=True), 'int8') mask_true = K.sum(y_true[:, [1, 2, 3, 4], :, :, :], axis=1) mask_pred = K.sum(y_pred_decision[:, [1, 2, 3, 4], :, :, :], axis=1) y_sum = K.sum(mask * mask_true * mask_pred) return (2. * y_sum + K.epsilon()) / (K.sum(mask * mask_true) + K.sum(mask * mask_pred) + K.epsilon()) return dice_whole_closure
def dice_core_mask(mask): def dice_core_closure(y_true, y_pred): """ Computes the Sorensen-Dice metric, where P come from class 1,2,3,4,5 TP Dice = 2 ------- T + P Parameters ---------- y_true : keras.placeholder Placeholder that contains the ground truth labels of the classes y_pred : keras.placeholder Placeholder that contains the class prediction Returns ------- scalar Dice metric """ y_pred_decision = K.cast(y_pred / K.max(y_pred, axis=1, keepdims=True), 'int8') mask_true = K.sum(y_true[:, [1, 3, 4], :, :, :], axis=1) mask_pred = K.sum(y_pred_decision[:, [1, 3, 4], :, :, :], axis=1) y_sum = K.sum(mask * mask_true * mask_pred) return (2. * y_sum + K.epsilon()) / (K.sum(mask * mask_true) + K.sum(mask * mask_pred) + K.epsilon()) return dice_core_closure
def dice_enhance_mask(mask): def dice_enhance_closure(y_true, y_pred): """ Computes the Sorensen-Dice metric, where P come from class 1,2,3,4,5 TP Dice = 2 ------- T + P Parameters ---------- y_true : keras.placeholder Placeholder that contains the ground truth labels of the classes y_pred : keras.placeholder Placeholder that contains the class prediction Returns ------- scalar Dice metric """ y_pred_decision = K.cast(y_pred / K.max(y_pred, axis=1, keepdims=True), 'int8') mask_true = y_true[:, 4, :, :, :] mask_pred = y_pred_decision[:, 4, :, :, :] y_sum = K.sum(mask * mask_true * mask_pred) return (2. * y_sum + K.epsilon()) / (K.sum(mask * mask_true) + K.sum(mask * mask_pred) + K.epsilon()) return dice_enhance_closure
def precision_0_mask(mask): def precision_0_closure(y_true, y_pred): y_pred_decision = K.cast(y_pred / K.max(y_pred, axis=1, keepdims=True), 'int8') mask_true = y_true[:, 0, :, :, :] mask_pred = y_pred_decision[:, 0, :, :, :] y_sum = K.sum(mask * mask_true * mask_pred) return (y_sum + K.epsilon()) / (K.sum(mask_pred) + K.epsilon()) return precision_0_closure