def call(self, inputs): inputs, weights = inputs weights = weights / tf.reduce_sum(weights) # Normalize sample weights weights_expand = tf.expand_dims(weights, axis=1) mean, variance = tf.nn.weighted_moments( inputs, [0], weights_expand) # Compute weighed mean and variance counter = K.update_add( self.counter, K.ones_like(self.counter) ) # Count number of times the data passes through the model init = K.sign( counter - K.ones_like(counter) ) # Indicator is 1 if model is being initalized, 0 otherwise mean = K.update( self.mean, init * self.mean + (1.0 - init) * mean) # Store the mean when the indicator is 1 variance = K.update( self.variance, init * self.variance + (1.0 - init) * variance) # Store the variance when the indicator is 1 mean_expand = tf.expand_dims(mean, axis=0) variance_expand = tf.expand_dims(variance, axis=0) outputs = (inputs - mean_expand) / tf.sqrt( variance_expand + self.epsilon) # Normalize the inputs return outputs
def _init_cel(self, A_graph, b_graph, c_graph, y): # Sanity Checks y = tf.check_numerics(y, 'Problem with input y') # Find intersection points between Ax-b and the line joining the c and y Ac = tf.reduce_sum(A_graph * tf.expand_dims(c_graph, axis=-2), axis=-1) bMinusAc = b_graph - Ac yMinusc = y - c_graph ADotyMinusc = tf.reduce_sum((A_graph * tf.expand_dims(yMinusc, -2)), axis=-1) intersection_alphas = bMinusAc / (ADotyMinusc + K.epsilon()) # Enforce intersection_alpha > 0 because the point must lie on the ray from c to y less_equal_0 = K.less_equal(intersection_alphas, K.zeros_like(intersection_alphas)) candidate_alpha = K.switch( less_equal_0, K.ones_like(intersection_alphas) * tf.constant(np.inf, dtype='float32'), intersection_alphas) # Find closest the intersection point closest to the interior point to get projection point intersection_alpha = K.min(candidate_alpha, axis=-1, keepdims=True) # If it is an interior point, y itself is the projection point is_interior_point = K.greater_equal(intersection_alpha, K.ones_like(intersection_alpha)) alpha = K.switch(is_interior_point, K.ones_like(intersection_alpha), intersection_alpha) # Return z = \alpha.y + (1 - \alpha).c z = alpha * y + ((1 - alpha) * c_graph) return z
def f1(y_true, y_pred): def recall(y_true, y_pred): """Recall metric. Only computes a batch-wise average of recall. Computes the recall, a metric for multi-label classification of how many relevant items are selected. """ true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1))) possible_positives = K.sum(K.round(K.clip(y_true, 0, 1))) recall = true_positives / (possible_positives + K.epsilon()) return recall def precision(y_true, y_pred): """Precision metric. Only computes a batch-wise average of precision. Computes the precision, a metric for multi-label classification of how many selected items are relevant. """ true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1))) predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1))) precision = true_positives / (predicted_positives + K.epsilon()) return precision precision_pos = precision(y_true, y_pred) recall_pos = recall(y_true, y_pred) precision_neg = precision((K.ones_like(y_true) - y_true), (K.ones_like(y_pred) - K.clip(y_pred, 0, 1))) recall_neg = recall((K.ones_like(y_true) - y_true), (K.ones_like(y_pred) - K.clip(y_pred, 0, 1))) f_posit = 2 * ((precision_pos * recall_pos) / (precision_pos + recall_pos + K.epsilon())) f_neg = 2 * ((precision_neg * recall_neg) / (precision_neg + recall_neg + K.epsilon())) return (f_posit + f_neg) / 2
def loss_fun(y_true, y_pred): tshape = K.shape(y_true) batchSize = tshape[0] t = K.reshape(y_true, shape=(batchSize, -1)) p = K.reshape(y_pred, shape=(batchSize, -1)) tb = 1 - t pb = 1 - p tp = K.sum(t * p, -1) tn = K.sum(tb * pb, -1) tv = K.sum(t, -1) pv = K.sum(p, -1) tbv = K.sum(tb, -1) pbv = K.sum(pb, -1) if batch_mean: tv = K.mean(tv, 0, keepdims=True) pv = K.mean(pv, 0, keepdims=True) tbv = K.mean(tbv, 0, keepdims=True) pbv = K.mean(pbv, 0, keepdims=True) if square_weights: w = 1. / K.square(tv) wb = 1. / K.square(tbv) else: w = 1. / tv wb = 1. / tbv w = tf.where(tf.math.is_inf(w), K.ones_like(w), w) # regularize 0 div wb = tf.where(tf.math.is_inf(wb), K.ones_like(wb), wb) return 1 - (w * tp + wb * tn) / (0.5 * w * (tv + pv) + 0.5 * wb * (tbv + pbv))
def _diagonalize(xyz, mass): rsq = K.expand_dims(K.sum(xyz**2, axis=-1, keepdims=True), -1) # xyz::(..., num_neighbors, 3) # f1, f2::(..., num_neighbors, 3, 3) f1 = np.eye(3) * rsq f2 = K.expand_dims(xyz, -2) * K.expand_dims(xyz, -1) # mass::(..., num_neighbors) expanded_mass = K.expand_dims(K.expand_dims(mass, -1), -1) # I_s::(..., 3, 3) I_s = K.sum((f1 - f2) * expanded_mass, -3) # rotations::(..., 3, 3) rotations = _custom_eigvecsh(I_s) # swap z for -z when an inversion occurs det_sign = tf.linalg.det(rotations) inversions = K.stack( [K.ones_like(det_sign), K.ones_like(det_sign), det_sign], axis=-1) rotations = rotations * K.expand_dims(inversions, -2) rotated_xyz = K.sum( K.expand_dims(xyz, -1) * K.expand_dims(rotations, -3), -2) return rotated_xyz, I_s, rotations
def get_constants(self, inputs, training=None): constants = [] if 0 < self.dropout < 1: input_shape = K.int_shape(inputs) input_dim = input_shape[-1] ones = K.ones_like(K.reshape(inputs[:, 0, 0], (-1, 1))) ones = K.tile(ones, (1, int(input_dim))) def dropped_inputs(): return K.dropout(ones, self.dropout) dp_mask = K.in_train_phase(dropped_inputs, ones, training=training) constants.append(dp_mask) else: constants.append(K.cast_to_floatx(1.)) if 0 < self.recurrent_dropout < 1: ones = K.ones_like(K.reshape(inputs[:, 0, 0], (-1, 1))) ones = K.tile(ones, (1, self.units)) def dropped_inputs(): return K.dropout(ones, self.recurrent_dropout) rec_dp_mask = K.in_train_phase(dropped_inputs, ones, training=training) constants.append(rec_dp_mask) else: constants.append(K.cast_to_floatx(1.)) return constants
def call(self, inputs, states, training=None): if 0 < self.dropout < 1 and self._dropout_mask is None: self._dropout_mask = _generate_dropout_mask(K.ones_like(inputs), self.dropout, training=training, count=4) if (0 < self.recurrent_dropout < 1 and self._recurrent_dropout_mask is None): self._recurrent_dropout_mask = _generate_dropout_mask( K.ones_like(states[0]), self.recurrent_dropout, training=training, count=4) # dropout matrices for input units dp_mask = self._dropout_mask # dropout matrices for recurrent units rec_dp_mask = self._recurrent_dropout_mask h_tm1 = states[0] # previous memory state c_tm1 = states[1] # previous carry state # inputs: [event_dimension + 1] x_t = inputs[:, :-1] dt = inputs[:, -1] dt = tf.reshape(dt, (-1, 1)) if 0. < self.dropout < 1.: x_t *= dp_mask[0] z = K.dot(x_t, self.kernel) if 0. < self.recurrent_dropout < 1.: h_tm1 *= rec_dp_mask[0] z += K.dot(h_tm1, self.recurrent_kernel) if self.use_bias: z = K.bias_add(z, self.bias[:self.units * 4]) x_Wxt = K.dot(x_t, self.timegate_kernel[:-1, :]) dt_Wtt = dt * self.timegate_kernel[-1, :] T = x_Wxt + self.time_activation(dt_Wtt) T = K.bias_add(T, self.bias[self.units * 4:]) T = self.time_activation(T) z0 = z[:, :self.units] z1 = z[:, self.units:2 * self.units] z2 = z[:, 2 * self.units:3 * self.units] z3 = z[:, 3 * self.units:4 * self.units] i = self.recurrent_activation(z0) f = self.recurrent_activation(z1) c = f * c_tm1 + i * T * self.activation(z2) o = self.recurrent_activation(z3) h = o * self.activation(c) if 0 < self.dropout + self.recurrent_dropout: if training is None: h._uses_learning_phase = True return h, [h, c]
def discriminator_loss(self, discrimator, real_image, fake_image, recreated_image): real_output = discrimator([real_image]) fake_output = discrimator([fake_image]) discriminator_loss_real = self.loss_function(real_output, K.ones_like(real_output)) discriminator_loss_fake = self.loss_function(fake_output, K.ones_like(fake_output)) discriminator_loss = discriminator_loss_fake + discriminator_loss_real generator_loss = self.loss_function(fake_output, K.ones_like(fake_output)) cycle_loss = K.mean(K.abs(recreated_image, real_image)) return discriminator_loss, generator_loss, cycle_loss
def getPrecision(y_true, y_pred): TP = K.sum(K.round(K.clip(y_true * y_pred, 0, 1))) # TP N = (-1) * K.sum(K.round(K.clip(y_true - K.ones_like(y_true), -1, 0))) # N TN = K.sum( K.round( K.clip((y_true - K.ones_like(y_true)) * (y_pred - K.ones_like(y_pred)), 0, 1))) # TN FP = N - TN precision = TP / (TP + FP + K.epsilon()) # TT/P return precision
def binary_focal_loss_fixed(y_true, y_pred): """ y_true shape need be (None,1) y_pred need be compute after sigmoid """ y_true = tf.cast(y_true, tf.float32) alpha_t = y_true * alpha + (K.ones_like(y_true) - y_true) * (1 - alpha) p_t = y_true * y_pred + (K.ones_like(y_true) - y_true) * (K.ones_like(y_true) - y_pred) + K.epsilon() focal_loss = - alpha_t * K.pow((K.ones_like(y_true) - p_t), gamma) * K.log(p_t) return K.mean(focal_loss)
def focal_loss(y_true, y_pred): gamma = 2 alpha = 0.25 y_true = K.flatten(y_true) y_pred = K.flatten(y_pred) alpha_t = y_true * alpha + (K.ones_like(y_true) - y_true) * (1 - alpha) p_t = y_true * y_pred + (K.ones_like(y_true) - y_true) * ( K.ones_like(y_true) - y_pred) + K.epsilon() focal_loss = -alpha_t * K.pow( (K.ones_like(y_true) - p_t), gamma) * K.log(p_t) return K.mean(focal_loss)
def build_tabor_regularization(self, input_raw_tensor, model, y_target_tensor, y_true_tensor): reg_losses = [] # R1 - Overly large triggers mask_l1_norm = K.sum(K.abs(self.mask_upsample_tensor)) mask_l2_norm = K.sum(K.square(self.mask_upsample_tensor)) mask_r1 = (mask_l1_norm + mask_l2_norm) pattern_tensor = (K.ones_like(self.mask_upsample_tensor) \ - self.mask_upsample_tensor) * self.pattern_raw_tensor pattern_l1_norm = K.sum(K.abs(pattern_tensor)) pattern_l2_norm = K.sum(K.square(pattern_tensor)) pattern_r1 = (pattern_l1_norm + pattern_l2_norm) # R2 - Scattered triggers pixel_dif_mask_col = K.sum(K.square( self.mask_upsample_tensor[:-1, :, :] \ - self.mask_upsample_tensor[1:, :, :])) pixel_dif_mask_row = K.sum(K.square( self.mask_upsample_tensor[:, :-1, :] \ - self.mask_upsample_tensor[:, 1:, :])) mask_r2 = pixel_dif_mask_col + pixel_dif_mask_row pixel_dif_pat_col = K.sum(K.square(pattern_tensor[:-1, :, :] \ - pattern_tensor[1:, :, :])) pixel_dif_pat_row = K.sum(K.square(pattern_tensor[:, :-1, :] \ - pattern_tensor[:, 1:, :])) pattern_r2 = pixel_dif_pat_col + pixel_dif_pat_row # R3 - Blocking triggers cropped_input_tensor = (K.ones_like(self.mask_upsample_tensor) \ - self.mask_upsample_tensor) * input_raw_tensor r3 = K.mean( categorical_crossentropy( model(cropped_input_tensor), K.reshape(y_true_tensor[0], shape=(1, -1)))) # R4 - Overlaying triggers mask_crop_tensor = self.mask_upsample_tensor * self.pattern_raw_tensor r4 = K.mean( categorical_crossentropy( model(mask_crop_tensor), K.reshape(y_target_tensor[0], shape=(1, -1)))) reg_losses.append(mask_r1) reg_losses.append(pattern_r1) reg_losses.append(mask_r2) reg_losses.append(pattern_r2) reg_losses.append(r3) reg_losses.append(r4) return K.stack(reg_losses)
def get_psp(self, output_spikes): new_spiketimes = tf.where(k.greater(output_spikes, 0), k.ones_like(output_spikes) * self.time, self.last_spiketimes) new_spiketimes = tf.where(k.less(output_spikes, 0), k.zeros_like(output_spikes) * self.time, new_spiketimes) assign_new_spiketimes = tf.assign(self.last_spiketimes, new_spiketimes) with tf.control_dependencies([assign_new_spiketimes]): last_spiketimes = self.last_spiketimes + 0 # Dummy op # psp = k.maximum(0., tf.divide(self.dt, last_spiketimes)) psp = tf.where(k.greater(last_spiketimes, 0), k.ones_like(output_spikes) * self.dt, k.zeros_like(output_spikes)) return psp
def compute_mask(self, inputs, mask=None): # pylint: disable=unused-argument if mask is None: return None mask_1, mask_2 = mask if mask_1 is None and mask_2 is None: return None if mask_1 is None: mask_1 = K.ones_like(K.sum(inputs[0], axis=-1)) if mask_2 is None: mask_2 = K.ones_like(K.sum(inputs[1], axis=-1)) # Theano can't do batch_dot on ints, so we need to cast to float and then back. mask_1 = K.cast(K.expand_dims(mask_1, axis=2), 'float32') mask_2 = K.cast(K.expand_dims(mask_2, axis=1), 'float32') return K.cast(K.batch_dot(mask_1, mask_2), 'uint8')
def get_psp(self, output_spikes): if hasattr(self, 'activation_str') \ and self.activation_str == 'softmax': psp = tf.identity(output_spikes) else: new_spiketimes = tf.where(k.not_equal(output_spikes, 0), k.ones_like(output_spikes) * self.time, self.last_spiketimes) assign_new_spiketimes = self.last_spiketimes.assign(new_spiketimes) with tf.control_dependencies([assign_new_spiketimes]): last_spiketimes = self.last_spiketimes + 0 # Dummy op psp = tf.where(k.greater(last_spiketimes, 0), k.ones_like(output_spikes) * self.dt, k.zeros_like(output_spikes)) return psp
def binary_focal_loss_fixed(y_true, y_pred): """ :param y_true: A tensor of the same shape as `y_pred` :param y_pred: A tensor resulting from a sigmoid :return: Output tensor. """ y_true = tf.cast(y_true, tf.float32) # Define epsilon so that the back-propagation will not result in NaN for 0 divisor case epsilon = K.epsilon() # Add the epsilon to prediction value # y_pred = y_pred + epsilon # Clip the prediciton value y_pred = K.clip(y_pred, epsilon, 1.0 - epsilon) # Calculate p_t p_t = tf.where(K.equal(y_true, 1), y_pred, 1 - y_pred) # Calculate alpha_t alpha_factor = K.ones_like(y_true) * alpha alpha_t = tf.where(K.equal(y_true, 1), alpha_factor, 1 - alpha_factor) # Calculate cross entropy cross_entropy = -K.log(p_t) weight = alpha_t * K.pow((1 - p_t), gamma) # Calculate focal loss loss = weight * cross_entropy # Sum the losses in mini_batch loss = K.mean(K.sum(loss, axis=1)) 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 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 yolo_eval(yolo_outputs, anchors, num_classes, image_shape, max_boxes=20, score_threshold=.6, iou_threshold=.5, eager = False): if eager: image_shape = K.reshape(yolo_outputs[-1],[-1]) num_layers = len(yolo_outputs)-1 else: # 获得特征层的数量 num_layers = len(yolo_outputs) # 特征层1对应的anchor是678 # 特征层2对应的anchor是345 # 特征层3对应的anchor是012 anchor_mask = [[3, 4, 5], [1, 2, 3]] input_shape = K.shape(yolo_outputs[0])[1:3] * 32 boxes = [] box_scores = [] # 对每个特征层进行处理 for l in range(num_layers): _boxes, _box_scores = yolo_boxes_and_scores(yolo_outputs[l], anchors[anchor_mask[l]], num_classes, input_shape, image_shape) boxes.append(_boxes) box_scores.append(_box_scores) # 将每个特征层的结果进行堆叠 boxes = K.concatenate(boxes, axis=0) box_scores = K.concatenate(box_scores, axis=0) mask = box_scores >= score_threshold max_boxes_tensor = K.constant(max_boxes, dtype='int32') boxes_ = [] scores_ = [] classes_ = [] for c in range(num_classes): # 取出所有box_scores >= score_threshold的框,和成绩 class_boxes = tf.boolean_mask(boxes, mask[:, c]) class_box_scores = tf.boolean_mask(box_scores[:, c], mask[:, c]) # 非极大抑制,去掉box重合程度高的那一些 nms_index = tf.image.non_max_suppression( class_boxes, class_box_scores, max_boxes_tensor, iou_threshold=iou_threshold) # 获取非极大抑制后的结果 # 下列三个分别是 # 框的位置,得分与种类 class_boxes = K.gather(class_boxes, nms_index) class_box_scores = K.gather(class_box_scores, nms_index) classes = K.ones_like(class_box_scores, 'int32') * c boxes_.append(class_boxes) scores_.append(class_box_scores) classes_.append(classes) boxes_ = K.concatenate(boxes_, axis=0) scores_ = K.concatenate(scores_, axis=0) classes_ = K.concatenate(classes_, axis=0) return boxes_, scores_, classes_
def make_readout_decode_model(self, max_output_len=32): src_seq_input = Input(shape=(None, ), dtype='int32') tgt_start_input = Input(shape=(1, ), dtype='int32') src_seq = src_seq_input enc_mask = Lambda(lambda x: K.cast(K.greater(x, 0), 'float32'))( src_seq) src_emb = self.i_word_emb(src_seq) if self.pos_emb: src_emb = add_layer([src_emb, self.pos_emb(src_seq)]) src_emb = self.emb_dropout(src_emb) enc_output = self.encoder(src_emb, src_seq) tgt_emb = self.o_word_emb(tgt_start_input) tgt_seq = Lambda(lambda x: K.repeat_elements(x, max_output_len, 1))( tgt_start_input) rep_input = Lambda(lambda x: K.repeat_elements(x, max_output_len, 1))( tgt_emb) cell = ReadoutDecoderCell(self.o_word_emb, self.pos_emb, self.decoder, self.target_layer) final_output = InferRNN(cell, return_sequences=True)(rep_input, initial_state=[tgt_start_input, K.ones_like(tgt_start_input), K.zeros_like(tgt_seq)] + \ [rep_input for _ in self.decoder.layers], constants=[enc_output, enc_mask]) final_output = Lambda(lambda x: K.squeeze(x, -1))(final_output) self.readout_model = Model([src_seq_input, tgt_start_input], final_output)
def call(self, inputs, states, constants, training=None): (tgt_curr_input, tgt_pos_input, dec_mask), dec_output = states[:3], list(states[3:]) enc_output, enc_mask = constants time = K.max(tgt_pos_input) col_mask = K.cast(K.equal(K.cumsum(K.ones_like(dec_mask), axis=1), time), dtype='float32') dec_mask = dec_mask + col_mask tgt_emb = self.o_word_emb(tgt_curr_input) if self.pos_emb: tgt_emb = tgt_emb + self.pos_emb(tgt_pos_input, pos_input=True) x = tgt_emb xs = [] cc = K.cast(K.expand_dims(col_mask), dtype='float32') for i, dec_layer in enumerate(self.decoder.layers): dec_last_state = dec_output[i] * (1 - cc) + tf.einsum( 'ijk,ilj->ilk', x, cc) x, _, _ = dec_layer(x, enc_output, dec_mask, enc_mask, dec_last_state=dec_last_state) xs.append(dec_last_state) ff_output = self.target_layer(x) out = K.cast(K.argmax(ff_output, -1), dtype='int32') return out, [out, tgt_pos_input + 1, dec_mask] + xs
def call(self, inputs): mu, std = inputs var_dist = tfp.MultivariateNormalDiag(loc=mu, scale_diag=std) pri_dist = tfp.MultivariateNormalDiag(loc=K.zeros_like(mu), scale_diag=K.ones_like(std)) kl_loss = self.lamb_kl * K.mean(tfp.kl_divergence(var_dist, pri_dist)) return kl_loss
def call(self, inputs, output_shape=None): 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.up_size[0], input_shape[2] * self.up_size[1], input_shape[3]) # 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 call(self, inputs): # step 1: adapative average # from (batch, rows, n_features) to (batch, n_features, rows) inputs = self.transpose(inputs) avg = K.mean(inputs, axis=2) adaptive_avg = self.mean_layer(avg) adaptive_avg = K.reshape(adaptive_avg, (-1, self.n_features, 1)) inputs -= adaptive_avg # # step 2: adapative scaling std = K.mean(inputs ** 2, axis=2) std = K.sqrt(std + self.eps) adaptive_std = self.scaling_layer(std) fn = lambda elem: K.switch(K.less_equal(elem, 1.0), K.ones_like(elem), elem) adaptive_std = K.map_fn(fn, adaptive_std) adaptive_std = K.reshape(adaptive_std, (-1, self.n_features, 1)) inputs /= adaptive_std # # step 3: gating avg = K.mean(inputs, axis=2) gate = self.gating_layer(avg) gate = K.reshape(gate, (-1, self.n_features, 1)) inputs *= gate # from (batch, n_features, rows) => (batch, rows, n_features) inputs = self.transpose(inputs) return inputs
def single_image_nms(b, batch_boxes, batch_scores, batch_classes): boxes_ = [] scores_ = [] classes_ = [] for c in range(num_classes): # TODO: use keras backend instead of tf. class_boxes = tf.boolean_mask(boxes[b], mask[b, :, c]) class_box_scores = tf.boolean_mask(box_scores[b, :, c], mask[b, :, c]) nms_index = tf.image.non_max_suppression( class_boxes, class_box_scores, max_boxes_tensor, iou_threshold=iou_threshold) class_boxes = K.gather(class_boxes, nms_index) class_box_scores = K.gather(class_box_scores, nms_index) classes = K.ones_like(class_box_scores, 'int32') * c boxes_.append(class_boxes) scores_.append(class_box_scores) classes_.append(classes) boxes_ = K.concatenate(boxes_, axis=0) scores_ = K.concatenate(scores_, axis=0) classes_ = K.concatenate(classes_, axis=0) batch_boxes = batch_boxes.write(b, boxes_) batch_scores = batch_scores.write(b, scores_) batch_classes = batch_classes.write(b, classes_) return b+1, batch_boxes, batch_scores, batch_classes
def dropped_mask(): drop_mask = K.switch( K.random_uniform(K.shape(inputs)) < self.drop_rate, K.ones_like(inputs, K.floatx()), K.zeros_like(inputs, K.floatx()), ) return target_mask * drop_mask
def recall(y_true, y_pred): y_true = K.ones_like(y_true) true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1))) all_positives = K.sum(K.round(K.clip(y_true, 0, 1))) recall = true_positives / (all_positives + K.epsilon()) return recall
def call(self, inputs): #input_shape = K.cast(K.shape(inputs), dtype='int64') #input_shape=K.cast(inputs.shape,dtype='int64') input_shape = inputs.shape output_shape = (input_shape[0], input_shape[1] * self.stride[1], input_shape[2] * self.stride[2], input_shape[3]) #output_list = [] #output_list.append(self.pooling_argmax // (output_shape[2] * output_shape[3])) #output_list.append(self.pooling_argmax % (output_shape[2] * output_shape[3]) // output_shape[3]) argmax = self.pooling_argmax #K.stack(output_list) one_like_mask = K.ones_like(argmax) batch_range = K.reshape(K.arange(start=0, stop=input_shape[0], dtype='int64'), shape=[input_shape[0], 1, 1, 1]) b = one_like_mask * batch_range y = argmax // (output_shape[2] * output_shape[3]) x = argmax % (output_shape[2] * output_shape[3]) // output_shape[3] feature_range = K.arange(start=0, stop=output_shape[3], dtype='int64') f = one_like_mask * feature_range # transpose indices & reshape update values to one dimension updates_size = tf.size(inputs) indices = K.transpose( K.reshape(K.stack([b, y, x, f]), [4, updates_size])) values = K.reshape(inputs, [updates_size]) return tf.scatter_nd(indices, values, output_shape)
def precision(y_true, y_pred): y_true = K.ones_like(y_true) true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1))) predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1))) precision = true_positives / (predicted_positives + K.epsilon()) return precision
def get_updates(self, loss, params): grads = self.get_gradients(loss, params) self.updates = [K.update_add(self.iterations, 1)] lr = self.lr if self.initial_decay > 0: lr = lr * (1. / (1. + self.decay * K.cast(self.iterations, K.dtype(self.decay)))) t = K.cast(self.iterations, K.floatx()) + 1 # Applies bounds on actual learning rate step_size = lr * (K.sqrt(1. - K.pow(self.beta_2, t)) / (1. - K.pow(self.beta_1, t))) final_lr = self.final_lr * lr / self.base_lr lower_bound = final_lr * (1. - 1. / (self.gamma * t + 1.)) upper_bound = final_lr * (1. + 1. / (self.gamma * t)) ms = [K.zeros(K.int_shape(p), dtype=K.dtype(p)) for p in params] vs = [K.zeros(K.int_shape(p), dtype=K.dtype(p)) for p in params] if self.amsbound: vhats = [K.zeros(K.int_shape(p), dtype=K.dtype(p)) for p in params] else: vhats = [K.zeros(1) for _ in params] self.weights = [self.iterations] + ms + vs + vhats for p, g, m, v, vhat in zip(params, grads, ms, vs, vhats): # apply weight decay if self.weight_decay != 0.: g += self.weight_decay * K.stop_gradient(p) m_t = (self.beta_1 * m) + (1. - self.beta_1) * g v_t = (self.beta_2 * v) + (1. - self.beta_2) * K.square(g) if self.amsbound: vhat_t = K.maximum(vhat, v_t) denom = (K.sqrt(vhat_t) + self.epsilon) self.updates.append(K.update(vhat, vhat_t)) else: denom = (K.sqrt(v_t) + self.epsilon) # Compute the bounds step_size_p = step_size * K.ones_like(denom) step_size_p_bound = step_size_p / denom bounded_lr_t = m_t * K.minimum( K.maximum(step_size_p_bound, lower_bound), upper_bound) p_t = p - bounded_lr_t self.updates.append(K.update(m, m_t)) self.updates.append(K.update(v, v_t)) new_p = p_t # Apply constraints. if getattr(p, 'constraint', None) is not None: new_p = p.constraint(new_p) self.updates.append(K.update(p, new_p)) return self.updates
def focal(y_true, y_pred, alpha=0.25, gamma=2.0, axis=None): """Compute the focal loss given the target tensor and the predicted tensor. As defined in https://arxiv.org/abs/1708.02002 Args: y_true: Tensor of target data with shape (B, N, num_classes). y_pred: Tensor of predicted data with shape (B, N, num_classes). alpha: Scale the focal weight with alpha. gamma: Take the power of the focal weight with gamma. Returns: The focal loss of y_pred w.r.t. y_true. """ if axis is None: axis = 1 if K.image_data_format() == 'channels_first' else K.ndim(y_pred) - 1 # compute the focal loss alpha_factor = K.ones_like(y_true) * alpha alpha_factor = tf.where(K.equal(y_true, 1), alpha_factor, 1 - alpha_factor) focal_weight = tf.where(K.equal(y_true, 1), 1 - y_pred, y_pred) focal_weight = alpha_factor * focal_weight ** gamma cls_loss = focal_weight * K.binary_crossentropy(y_true, y_pred) return K.sum(cls_loss, axis=axis)