def binary_PTA(y_true, y_pred, threshold=K.variable(value=0.5)): y_pred = K.cast(y_pred >= threshold, 'float32') # P = total number of positive labels P = K.sum(y_true) # TP = total number of correct alerts, alerts from the positive class labels TP = K.sum(y_pred * y_true) return TP / P
def tversky_loss(y_true, y_pred, alpha=0.3, beta=0.7, smooth=1e-10): """ Tversky loss function. Parameters ---------- y_true : keras tensor tensor containing target mask. y_pred : keras tensor tensor containing predicted mask. alpha : float real value, weight of '0' class. beta : float real value, weight of '1' class. smooth : float small real value used for avoiding division by zero error. Returns ------- keras tensor tensor containing tversky loss. """ y_true = K.flatten(y_true) y_pred = K.flatten(y_pred) truepos = K.sum(y_true * y_pred) fp_and_fn = alpha * K.sum(y_pred * (1 - y_true)) + beta * K.sum((1 - y_pred) * y_true) answer = (truepos + smooth) / ((truepos + smooth) + fp_and_fn) return -answer # might be 1 -
def binary_PFA(y_true, y_pred, threshold=K.variable(value=0.5)): y_pred = K.cast(y_pred >= threshold, 'float32') # N = total number of negative labels N = K.sum(1 - y_true) # FP = total number of false alerts, alerts from the negative class labels FP = K.sum(y_pred - y_pred * y_true) return FP / N
def Active_Contour_Loss(y_true, y_pred): # y_pred = K.cast(y_pred, dtype = 'float64') """ lenth term """ x = y_pred[:, :, 1:, :] - y_pred[:, :, :-1, :] # horizontal and vertical directions y = y_pred[:, :, :, 1:] - y_pred[:, :, :, :-1] delta_x = x[:, :, 1:, :-2] ** 2 delta_y = y[:, :, :-2, 1:] ** 2 delta_u = K.abs(delta_x + delta_y) epsilon = 0.00000001 # where is a parameter to avoid square root is zero in practice. w = 1 lenth = w * K.sum(K.sqrt(delta_u + epsilon)) # equ.(11) in the paper """ region term """ C_1 = np.ones((480, 320)) C_2 = np.zeros((480, 320)) region_in = K.abs(K.sum(y_pred[:, 0, :, :] * ((y_true[:, 0, :, :] - C_1) ** 2))) # equ.(12) in the paper region_out = K.abs(K.sum((1 - y_pred[:, 0, :, :]) * ((y_true[:, 0, :, :] - C_2) ** 2))) # equ.(12) in the paper lambdaP = 1 # lambda parameter could be various. loss = lenth + lambdaP * (region_in + region_out) return loss
def triplet_loss(y_true, y_pred, alpha=0.4): """ Implementation of the triplet loss function Arguments: y_true -- true labels, required when you define a loss in Keras, you don't need it in this function. y_pred -- python list containing three objects: anchor -- the encodings for the anchor data positive -- the encodings for the positive data (similar to anchor) negative -- the encodings for the negative data (different from anchor) Returns: loss -- real number, value of the loss """ print('y_pred.shape = ', y_pred) total_lenght = y_pred.shape.as_list()[-1] # print('total_lenght=', total_lenght) # total_lenght =12 anchor = y_pred[:, 0:int(total_lenght * 1 / 3)] positive = y_pred[:, int(total_lenght * 1 / 3):int(total_lenght * 2 / 3)] negative = y_pred[:, int(total_lenght * 2 / 3):int(total_lenght * 3 / 3)] # distance between the anchor and the positive pos_dist = K.sum(K.square(anchor - positive), axis=1) # distance between the anchor and the negative neg_dist = K.sum(K.square(anchor - negative), axis=1) # compute loss basic_loss = pos_dist - neg_dist + alpha loss = K.maximum(basic_loss, 0.0) return loss
def sigma(y_true,y_pred): y, x = np.mgrid[:224, :224] y_pred_f = K.flatten(y_pred) m = y_pred /K.sum(y_pred) sigma_x = K.sum(K.abs(x - K.sum(x*m))*m) sigma_y = K.sum(K.abs(y - K.sum(y*m))*m) sigma = K.sqrt(sigma_x)*K.sqrt(sigma_y) return sigma
def weighted_dice_loss(y_true, y_pred): weight = get_weight_matrix(y_true) smooth = 1. w, m1, m2 = weight * weight, y_true, y_pred intersection = (m1 * m2) score = (2. * K.sum(w * intersection) + smooth) / (K.sum(w * m1) + K.sum(w * m2) + smooth) loss = 1. - K.sum(score) return loss
def correlation_coefficient_loss(y_true, y_pred): x = y_true y = y_pred mx = K.mean(x) my = K.mean(y) xm, ym = x-mx, y-my r_num = K.sum(tf.multiply(xm,ym)) r_den = K.sqrt(tf.multiply(K.sum(K.square(xm)), K.sum(K.square(ym)))) r = r_num / r_den r = K.maximum(K.minimum(r, 1.0), -1.0) return K.square(r)
def celoss(y_true, y_pred): zero = K.equal(y_true , K.zeros((1,))) one = K.equal(y_true , K.ones((1,))) weights = np.array([1,alpha]) y_true_0 = tf.boolean_mask(y_true, zero) y_pred_0 = tf.boolean_mask(y_pred, zero) y_true_1 = tf.boolean_mask(y_true, one) y_pred_1 = tf.boolean_mask(y_pred, one) loss_0 = - y_true_0 * K.log(y_pred_0) * K.variable(weights[0]) loss_1 = - y_true_1 * K.log(y_pred_1) * K.variable(weights[1]) loss = K.sum(loss_0, -1) +K.sum(loss_1,-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 averaged_mask = K.pool2d( y_true, pool_size=(11, 11), 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 = 0.0 * weighted_bce_loss(y_true, y_pred, weight) + \ weighted_dice_loss(y_true, y_pred, weight) return loss
def auc(y_true, y_pred): ptas = tf.stack([binary_PTA(y_true,y_pred,k) for k in np.linspace(0, 1, 1000)],axis=0) pfas = tf.stack([binary_PFA(y_true,y_pred,k) for k in np.linspace(0, 1, 1000)],axis=0) pfas = tf.concat([tf.ones((1,)) ,pfas],axis=0) binSizes = -(pfas[1:]-pfas[:-1]) s = ptas*binSizes return K.sum(s, axis=0)
def matthews_correlation(y_true, y_pred): y_pred_pos = K.round(K.clip(y_pred, 0, 1)) y_pred_neg = 1 - y_pred_pos y_pos = K.round(K.clip(y_true, 0, 1)) y_neg = 1 - y_pos tp = K.sum(y_pos * y_pred_pos) tn = K.sum(y_neg * y_pred_neg) fp = K.sum(y_neg * y_pred_pos) fn = K.sum(y_pos * y_pred_neg) numerator = (tp * tn - fp * fn) denominator = K.sqrt((tp + fp) * (tp + fn) * (tn + fp) * (tn + fn)) return numerator / (denominator + K.epsilon())
def dice_coef_loss(y_true, y_pred): y, x = np.mgrid[:224, :224] y_true_f = K.flatten(y_true) y_pred_f = K.flatten(y_pred) intersection = K.sum(y_true_f * y_pred_f) m = y_pred /K.sum(y_pred) sigma_x = K.sum(K.abs(x - K.sum(x*m))*m) sigma_y = K.sum(K.abs(y - K.sum(y*m))*m) loss = - (2. * intersection ) / (K.sum(y_true_f) + K.sum(y_pred_f) ) +alpha*(K.sqrt(sigma_x)*K.sqrt(sigma_y)) return loss
def vae_loss(self, y_true, y_pred): """ Calculate loss = reconstruction loss + KL loss for eatch data in minibatch """ # E[log P(X|z)] recon = K.sum(K.sum(K.binary_crossentropy(y_pred, y_true), axis=1)) recon *= 256 # D_KL(Q(z|X) || P(z|X)); calculate in closed from as both dist. are Gaussian kl = 0.5 * K.sum( K.sum(K.exp(self.log_sigma) + K.square(self.mu) - 1. - self.log_sigma, axis=1)) return recon + kl # def sample_z(self,mu,log_sigma): # #mu, log_sigma = args # eps = K.backend.random_normal(shape=(self.mini_batch_size, self.latent_space_dim), mean=0., stddev=1.) # temp=np.array(log_sigma )/ 2. # return mu + K.exp(temp) * eps
def Kaggle_IoU_Precision(y_true, y_pred, threshold=0.5): y_pred = K.squeeze(tf.to_int32(y_pred > threshold), -1) y_true = K.cast(y_true[..., 0], K.floatx()) y_pred = K.cast(y_pred, K.floatx()) truth_areas = K.sum(y_true, axis=[1, 2]) pred_areas = K.sum(y_pred, axis=[1, 2]) intersection = K.sum(y_true * y_pred, axis=[1, 2]) union = K.clip(truth_areas + pred_areas - intersection, 1e-9, 128 * 128) check = K.map_fn(lambda x: K.equal(x, 0), truth_areas + pred_areas, dtype=tf.bool) p = intersection / union iou = K.switch(check, p + 1., p) prec = K.map_fn(lambda x: K.mean(K.greater(x, np.arange(0.5, 1.0, 0.05))), iou, dtype=tf.float32) prec_iou = K.mean(prec) return prec_iou
def jaccard_coef_logloss(y_true, y_pred, smooth=1e-10): """ Loss function based on jaccard coefficient. Parameters ---------- y_true : keras tensor tensor containing target mask. y_pred : keras tensor tensor containing predicted mask. smooth : float small real value used for avoiding division by zero error. Returns ------- keras tensor tensor containing negative logarithm of jaccard coefficient. """ y_true = K.flatten(y_true) y_pred = K.flatten(y_pred) truepos = K.sum(y_true * y_pred) falsepos = K.sum(y_pred) - truepos falseneg = K.sum(y_true) - truepos jaccard = (truepos + smooth) / (smooth + truepos + falseneg + falsepos) return -K.log(jaccard + smooth) # might be 1 -
def dice_coef(y_true, y_pred, smooth=1): intersection = keras.sum(y_true * y_pred, axis=[1,2,3]) union = keras.sum(y_true, axis=[1,2,3]) + keras.sum(y_pred, axis=[1,2,3]) return keras.mean( (2. * intersection + smooth) / (union + smooth), axis=0)
def deform_center_cnn(class_num, trainable=False, GPU=1): conv_args = { 'trainable': trainable, 'kernel_initializer': Orthogonal(gain=1.0, seed=None), 'kernel_regularizer': OrthLocalReg2D, 'padding': 'same' } sep_conv_args = { 'trainable': trainable, 'kernel_initializer': Orthogonal(gain=1.0, seed=None), 'kernel_regularizer': OrthLocalRegSep2D, 'padding': 'same' } inputs = l = Input((None, None, 3), name='input') input_target = Input((1, ), name='input_target') # norm_input = RGB2Gray()(inputs) norm_input = ImageNorm()(inputs) # norm_input = inputs stem_stride = (2, 2) # conv11 l = Conv2D(32, (3, 3), strides=stem_stride, name='conv11', **conv_args)(norm_input) l = Activation('relu', name='conv11_relu')(l) l = BatchNormalization(name='conv11_bn')(l) l2 = InvConv2D(32, (3, 3), strides=stem_stride, name='inv_conv11', **conv_args)(norm_input) l2 = Activation('relu', name='inv_conv11_relu')(l2) l2 = BatchNormalization(name='inv_conv11_bn')(l2) l = concatenate([l, l2]) l5 = SeparableConv2D(64, (5, 5), strides=(2, 2), name='conv5_11_12', **sep_conv_args)(l) l5 = Activation('relu', name='conv5_11_12_relu')(l5) l5 = BatchNormalization(name='conv5_11_12_bn')(l5) l3 = SeparableConv2D(64, (3, 3), strides=(2, 2), name='conv3_11_12', **sep_conv_args)(l) l3 = Activation('relu', name='conv3_11_12_relu')(l3) l3 = BatchNormalization(name='conv3_11_12_bn')(l3) l = concatenate([l3, l5]) l = SeparableConv2D(128, (3, 3), name='conv12_1', **sep_conv_args)(l) l = Activation('relu', name='conv12_1_relu')(l) l = BatchNormalization(name='conv12_1_bn')(l) l = SeparableConv2D(128, (1, 1), name='conv12_2', **sep_conv_args)(l) l = Activation('relu', name='conv12_2_relu')(l) l = BatchNormalization(name='conv12_2_bn')(l) l = SeparableConv2D(128, (3, 3), name='conv13', **sep_conv_args)(l) l = Activation('relu', name='conv13_relu')(l) l = BatchNormalization(name='conv13_bn')(l) l = SeparableConv2D(256, (3, 3), strides=(2, 2), name='conv14', **sep_conv_args)(l) l = Activation('relu', name='conv14_relu')(l) l = l14 = BatchNormalization(name='conv14_bn')(l) l = SeparableConv2D(256, (3, 3), name='conv21', **sep_conv_args)(l) l = Activation('relu', name='conv21_relu')(l) l = BatchNormalization(name='conv21_bn')(l) l = SeparableConv2D(256, (3, 3), name='conv22', **sep_conv_args)(l) l = Activation('relu', name='conv22_relu')(l) l = BatchNormalization(name='conv22_bn')(l) l = Add(name='residual_14_22')([l14, l]) # conv22 # l_offset = ConvOffset2D(192, name='conv32_offset')(l) # l = Conv2D(256, (3, 3), strides=(2, 2), name='conv23', **conv_args)(l) # l = Activation('relu', name='conv23_relu')(l) # l = l23 = BatchNormalization(name='conv23_bn')(l) # # l = Conv2D(256, (3, 3), name='conv31', **conv_args)(l) # l = Activation('relu', name='conv31_relu')(l) # l = BatchNormalization(name='conv31_bn')(l) # l = Conv2D(256, (1, 1), name='conv32', **conv_args)(l31) # l = Activation('relu', name='conv32_relu')(l) # l = BatchNormalization(name='conv32_bn')(l) # l = Conv2D(256, (3, 3), name='conv33', **conv_args)(l) # l = Activation('relu', name='conv33_relu')(l) # l = BatchNormalization(name='conv33_bn')(l) # l = Add(name='residual_23_33')([l23, l]) l_offset = ConvOffset2D(256, name='conv33_offset')(l) l = SeparableConv2D(512, (3, 3), strides=(2, 2), name='conv41', **sep_conv_args)(l_offset) l = Activation('relu', name='conv41_relu')(l) l = BatchNormalization(name='conv41_bn')(l) l = SeparableConv2D(512, (3, 3), name='conv42', **sep_conv_args)(l) l = Activation('relu', name='conv42_relu')(l) l = BatchNormalization(name='conv42_bn')(l) # l_offset = ConvOffset2D(512, name='conv35_offset')(l) l = SeparableConv2D(512, (3, 3), name='conv43', **sep_conv_args)(l) l = Activation('relu', name='conv43_relu')(l) l = BatchNormalization(name='conv43_bn')(l) # l = LocallyConnected2D(512, (3, 3), name='conv44', padding='valid')(l) # l = Activation('relu', name='conv44_relu')(l) # l = BatchNormalization(name='conv44_bn')(l) l = SeparableConv2D(1024, (3, 3), strides=(2, 2), name='conv51', **sep_conv_args)(l) l = Activation('relu', name='conv51_relu')(l) l = BatchNormalization(name='conv51_bn')(l) # l_offset = ConvOffset2D(1024, name='conv35_offset')(l) l = SeparableConv2D(1024, (3, 3), name='conv52', **sep_conv_args)(l) l = Activation('relu', name='conv52_relu')(l) l = BatchNormalization(name='conv52_bn')(l) l = SeparableConv2D(1024, (3, 3), name='conv53', **sep_conv_args)(l) l = Activation('relu', name='conv53_relu')(l) l = BatchNormalization(name='conv53_bn')(l) # out l = GlobalAvgPool2D(name='avg_pool')(l) # l = MaxPooling2D(name='max_pool_final')(l) # l = Flatten(name='flatten_maxpool')(l) l = Dense(512, name='fc1', trainable=trainable)(l) l = Activation('relu', name='fc1_relu')(l) l = Dense(256, name='fc2', trainable=trainable)(l) l = feature = Activation('relu', name='fc2_relu')(l) l = Dense(class_num, name='fc3', trainable=trainable)(l) outputs = l = Activation('softmax', name='out')(l) if GPU == 1: centers = Embedding(class_num, 256)(input_target) l2_loss = Lambda( lambda x: K.sum(K.square(x[0] - x[1][:, 0]), 1, keepdims=True), name='l2_loss')([feature, centers]) Model(inputs=[inputs, input_target], outputs=[outputs, l2_loss]) elif GPU == 0: return inputs, outputs else: BODY = Model(inputs=[inputs, input_target], outputs=[outputs, feature]) BODY = make_parallel(BODY, GPU) softmax_output = Lambda(lambda x: x, name='output')(BODY.outputs[0]) centers = Embedding(class_num, 256)(input_target) l2_loss = Lambda( lambda x: K.sum(K.square(x[0] - x[1][:, 0]), 1, keepdims=True), name='l2_loss')([BODY.outputs[1], centers]) model_withcneter = Model(inputs=BODY.inputs, outputs=[softmax_output, l2_loss]) return model_withcneter
def dice_coef(y_true, y_pred, smooth=1): y_true_f = keras.flatten(y_true) y_pred_f = keras.flatten(y_pred) intersection = keras.sum(y_true_f * y_pred_f) return (2. * intersection + smooth) / (keras.sum(y_true_f) + keras.sum(y_pred_f) + smooth)
def get_sum(X): return K.sum(X, axis=2)
def relu_max(x): e = K.relu(x, alpha=0, max_value=None) s = K.sum(e, axis=-1, keepdims=True) return e / s
def recall_m(y_true, y_pred): 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_m(y_true, y_pred): 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 dice_coef(y_true, y_pred): y_true_f = K.flatten(y_true) y_pred_f = K.flatten(y_pred) intersection = K.sum(y_true_f * y_pred_f) return (2. * intersection ) / (K.sum(y_true_f) + K.sum(y_pred_f) )
# we instantiate these layers separately so as to reuse them later decoder_f = Dense(dense2, activation='tanh') decoder_h = Dense(dense1, activation='tanh') decoder_mean = Dense(original_dim, activation='tanh') f_decoded = decoder_f(z) h_decoded = decoder_h(f_decoded) x_decoded_mean = decoder_mean(h_decoded) x_decoded_img = Reshape(original_shape)(x_decoded_mean) # instantiate VAE model vae = Model(in_layer, x_decoded_img) # Compute VAE loss xent_loss = original_dim * metrics.binary_crossentropy(x, x_decoded_mean) kl_loss = -0.5 * K.sum(1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis=-1) vae_loss = K.mean(0.5 * xent_loss + 0.5 * kl_loss) vae.add_loss(vae_loss) vae.compile(optimizer='adam') vae.summary() vae.fit(x_train, shuffle=True, epochs=train_epoch, batch_size=batch_size, validation_data=(anomaly_test, None)) vae.save('%s = %d %d vae_dense2_model.hdf5' % (var_str, var1, var2)) encoder = Model(in_layer, z_mean)
def get_large_deform_cnn2(class_num, trainable=False, GPU=1): # init = Orthogonal(gain=1.0, seed=None) init = 'random_normal' inputs = l = Input((200, 200, 3), name='input') input_target = Input((1, ), name='input_target') #norm_input = ImageNorm()(inputs) norm_input = inputs # conv11 l = Conv2D(32, (3, 3), padding='same', name='conv11', trainable=trainable, kernel_initializer=init, kernel_regularizer=OrthLocalReg2D)(norm_input) l = Activation('relu', name='conv11_relu')(l) l = BatchNormalization(name='conv11_bn')(l) l2 = InvConv2D(32, (3, 3), padding='same', name='inv_conv11', trainable=trainable, kernel_initializer=init, kernel_regularizer=OrthLocalReg2D)(norm_input) l2 = Activation('relu', name='inv_conv11_relu')(l2) l2 = BatchNormalization(name='inv_conv11_bn')(l2) l3 = Conv2D(32, (3, 1), padding='same', name='conv11_2', trainable=trainable, kernel_initializer=init, kernel_regularizer=OrthLocalReg2D)(norm_input) l3 = Activation('relu', name='conv11_2_relu')(l3) l3 = BatchNormalization(name='conv11_2_bn')(l3) l5 = Conv2D(32, (1, 3), padding='same', name='conv11_3', trainable=trainable, kernel_initializer=init, kernel_regularizer=OrthLocalReg2D)(norm_input) l5 = Activation('relu', name='conv11_3_relu')(l5) l5 = BatchNormalization(name='conv11_3_bn')(l5) l4 = InvConv2D(32, (3, 1), padding='same', name='conv11_2i', trainable=trainable, kernel_initializer=init, kernel_regularizer=OrthLocalReg2D)(norm_input) l4 = Activation('relu', name='conv11_2i_relu')(l4) l4 = BatchNormalization(name='conv11_2i_bn')(l4) l6 = InvConv2D(32, (1, 3), padding='same', name='conv11_3i', trainable=trainable, kernel_initializer=init, kernel_regularizer=OrthLocalReg2D)(norm_input) l6 = Activation('relu', name='conv11_3i_relu')(l6) l6 = BatchNormalization(name='conv11_3i_bn')(l6) l = concatenate([l, l2, l3, l5, l4, l6]) # conv12 # l_offset = ConvOffset2D(32, name='conv12_offset')(l) l5 = Conv2D(128, (5, 5), padding='same', strides=(2, 2), name='pool5_11_12', trainable=trainable, kernel_initializer=init, kernel_regularizer=OrthLocalReg2D)(l) l5 = Activation('relu', name='pool5_11_12_relu')(l5) l5 = BatchNormalization(name='pool5_11_12_bn')(l5) l3 = Conv2D(128, (3, 3), padding='same', strides=(2, 2), name='pool3_11_12', trainable=trainable, kernel_initializer=init, kernel_regularizer=OrthLocalReg2D)(l) l3 = Activation('relu', name='pool3_11_12_relu')(l3) l3 = BatchNormalization(name='pool3_11_12_bn')(l3) l = concatenate([l3, l5]) l3 = Conv2D(32, (5, 3), padding='same', name='conv12_2', trainable=trainable, kernel_initializer=init, kernel_regularizer=OrthLocalReg2D)(l) l3 = Activation('relu', name='conv12_2_relu')(l3) l3 = BatchNormalization(name='conv12_2_bn')(l3) l5 = Conv2D(32, (3, 5), padding='same', name='conv12_3', trainable=trainable, kernel_initializer=init, kernel_regularizer=OrthLocalReg2D)(l) l5 = Activation('relu', name='conv12_3_relu')(l5) l5 = BatchNormalization(name='conv12_3_bn')(l5) l = Conv2D(128, (3, 3), padding='same', name='conv12', trainable=trainable, kernel_initializer=init, kernel_regularizer=OrthLocalReg2D)(l) l = Activation('relu', name='conv12_relu')(l) l = BatchNormalization(name='conv12_bn')(l) l = concatenate([l, l3, l5]) l = Conv2D(128, (3, 3), padding='same', name='conv13', trainable=trainable, kernel_initializer=init, kernel_regularizer=OrthLocalReg2D)(l) l = Activation('relu', name='conv13_relu')(l) l = jump = BatchNormalization(name='conv13_bn')(l) l = Conv2D(192, (3, 3), padding='same', strides=(2, 2), name='conv14', trainable=trainable, kernel_initializer=init, kernel_regularizer=OrthLocalReg2D)(l) l = Activation('relu', name='conv14_relu')(l) # l = BatchNormalization(name='conv14_bn')(l) l = Conv2D(192, (3, 3), padding='same', name='conv21', trainable=trainable, kernel_initializer=init, kernel_regularizer=OrthLocalReg2D)(l) l = Activation('relu', name='conv21_relu')(l) l = BatchNormalization(name='conv21_bn')(l) l = Conv2D(192, (3, 3), padding='same', name='conv22', trainable=trainable, kernel_initializer=init, kernel_regularizer=OrthLocalReg2D)(l) l = Activation('relu', name='conv22_relu')(l) l = BatchNormalization(name='conv22_bn')(l) # conv22 # l_offset = ConvOffset2D(192, name='conv32_offset')(l) l = Conv2D(256, (3, 3), padding='same', strides=(2, 2), name='conv23', trainable=trainable, kernel_initializer=init, kernel_regularizer=OrthLocalReg2D)(l) l = Activation('relu', name='conv23_relu')(l) l = BatchNormalization(name='conv23_bn')(l) l = Conv2D(256, (3, 3), padding='same', name='conv31', trainable=trainable, kernel_initializer=init, kernel_regularizer=OrthLocalReg2D)(l) l = Activation('relu', name='conv31_relu')(l) l31 = BatchNormalization(name='conv31_bn')(l) # l = Conv2D(256, (1, 1), padding='same', name='conv32', trainable=trainable, kernel_initializer=init, kernel_regularizer=OrthLocalReg2D)(l31) # l = Activation('relu', name='conv32_relu')(l) # l = BatchNormalization(name='conv32_bn')(l) l = Conv2D(256, (3, 3), padding='same', name='conv33', trainable=trainable, kernel_initializer=init, kernel_regularizer=OrthLocalReg2D)(l) l = Activation('relu', name='conv33_relu')(l) l = BatchNormalization(name='conv33_bn')(l) l = Add(name='residual_31_32')([l31, l]) lj = Conv2D(256, (3, 3), padding='same', strides=(2, 2), name='jump_pool', trainable=trainable, kernel_initializer=init, kernel_regularizer=OrthLocalReg2D)(jump) lj = Activation('relu', name='jump_pool_relu')(lj) lj = BatchNormalization(name='jump_pool_bn')(lj) lj = Conv2D(256, (3, 3), padding='same', strides=(2, 2), name='jump_pool2', trainable=trainable, kernel_initializer=init, kernel_regularizer=OrthLocalReg2D)(lj) lj = Activation('relu', name='jump_pool2_relu')(lj) lj = BatchNormalization(name='jump_pool2_bn')(lj) l = concatenate([l, lj]) l_offset = ConvOffset2D(512, name='conv33_offset')(l) l = Conv2D(512, (3, 3), padding='same', strides=(2, 2), name='conv41', trainable=trainable, kernel_initializer=init, kernel_regularizer=OrthLocalReg2D)(l_offset) l = Activation('relu', name='conv41_relu')(l) l = BatchNormalization(name='conv41_bn')(l) l = Conv2D(512, (3, 3), padding='same', name='conv42', trainable=trainable, kernel_initializer=init, kernel_regularizer=OrthLocalReg2D)(l) l = Activation('relu', name='conv42_relu')(l) l = BatchNormalization(name='conv42_bn')(l) # l_offset = ConvOffset2D(512, name='conv35_offset')(l) l = Conv2D(512, (3, 3), padding='same', name='conv43', trainable=trainable, kernel_initializer=init, kernel_regularizer=OrthLocalReg2D)(l) l = Activation('relu', name='conv43_relu')(l) l = BatchNormalization(name='conv43_bn')(l) # out # l = GlobalAvgPool2D(name='avg_pool')(l) l = MaxPooling2D(name='max_pool_final')(l) l = Flatten(name='flatten_maxpool')(l) l = Dense(768, name='fc1', trainable=trainable, kernel_initializer=init, kernel_regularizer=OrthLocalReg1D)(l) l = Activation('relu', name='fc1_relu')(l) l = feature = Dense(256, name='fc2', trainable=trainable, kernel_initializer=init)(l) l = Activation('relu', name='fc2_relu')(l) l = Dense(class_num, name='fc3', trainable=trainable)(l) outputs = l = Activation('softmax', name='out')(l) if GPU == 1: centers = Embedding(class_num, 256)(input_target) l2_loss = Lambda( lambda x: K.sum(K.square(x[0] - x[1][:, 0]), 1, keepdims=True), name='l2_loss')([feature, centers]) Model(inputs=[inputs, input_target], outputs=[outputs, l2_loss]) elif GPU == 0: return inputs, outputs else: BODY = Model(inputs=[inputs, input_target], outputs=[outputs, feature]) BODY = make_parallel(BODY, GPU) softmax_output = Lambda(lambda x: x, name='output')(BODY.outputs[0]) centers = Embedding(class_num, 256)(input_target) l2_loss = Lambda( lambda x: K.sum(K.square(x[0] - x[1][:, 0]), 1, keepdims=True), name='l2_loss')([BODY.outputs[1], centers]) model_withcneter = Model(inputs=BODY.inputs, outputs=[softmax_output, l2_loss]) return model_withcneter