예제 #1
0
def custom_loss(y_true, y_pred):
    # y_true[i] = (row, col, height, width, p(object_appeared|img))
    # the bounding box loss:
    bce_1 = binary_crossentropy(y_true[:, :-1], y_pred[:, :-1])
    # the binary prediction (about an object being present in an image) loss:
    bce_2 = binary_crossentropy(y_true[:, -1], y_pred[:, -1])
    return ALPHA * y_true[:, -1] * bce_1 + BETA * bce_2
예제 #2
0
    def yolo_loss(y_true, y_pred):
        # 1. transform all pred outputs
        # y_pred: (batch_size, grid, grid, anchors, (x, y, w, h, obj, ...cls))
        pred_box, pred_obj, pred_class, pred_xywh = yolo_boxes(
            y_pred, anchors, classes)
        pred_xy = pred_xywh[..., 0:2]
        pred_wh = pred_xywh[..., 2:4]

        # 2. transform all true outputs
        # y_true: (batch_size, grid, grid, anchors, (x1, y1, x2, y2, obj, cls))
        true_box, true_obj, true_class_idx = tf.split(y_true, (4, 1, 1),
                                                      axis=-1)
        true_xy = (true_box[..., 0:2] + true_box[..., 2:4]) / 2
        true_wh = true_box[..., 2:4] - true_box[..., 0:2]

        # give higher weights to small boxes
        box_loss_scale = 2 - true_wh[..., 0] * true_wh[..., 1]

        # 3. inverting the pred box equations
        grid_size = tf.shape(y_true)[1]
        grid = tf.meshgrid(tf.range(grid_size), tf.range(grid_size))
        grid = tf.expand_dims(tf.stack(grid, axis=-1), axis=2)
        true_xy = true_xy * tf.cast(grid_size, tf.float32) - \
            tf.cast(grid, tf.float32)
        true_wh = tf.math.log(true_wh / anchors)
        true_wh = tf.where(tf.math.is_inf(true_wh), tf.zeros_like(true_wh),
                           true_wh)

        # 4. calculate all masks
        obj_mask = tf.squeeze(true_obj, -1)
        # ignore false positive when iou is over threshold
        best_iou = tf.map_fn(
            lambda x: tf.reduce_max(broadcast_iou(
                x[0], tf.boolean_mask(x[1], tf.cast(x[2], tf.bool))),
                                    axis=-1), (pred_box, true_box, obj_mask),
            tf.float32)
        ignore_mask = tf.cast(best_iou < ignore_thresh, tf.float32)

        # 5. calculate all losses
        xy_loss = obj_mask * box_loss_scale * \
            tf.reduce_sum(tf.square(true_xy - pred_xy), axis=-1)
        wh_loss = obj_mask * box_loss_scale * \
            tf.reduce_sum(tf.square(true_wh - pred_wh), axis=-1)
        obj_loss = binary_crossentropy(true_obj, pred_obj)
        obj_loss = obj_mask * obj_loss + \
            (1 - obj_mask) * ignore_mask * obj_loss
        # TODO: use binary_crossentropy instead
        if FLAGS.num_classes == 1:
            class_loss = obj_mask * binary_crossentropy(
                true_class_idx, pred_class, from_logits=True)
        else:
            class_loss = obj_mask * sparse_categorical_crossentropy(
                true_class_idx, pred_class, from_logits=True)
        # 6. sum over (batch, gridx, gridy, anchors) => (batch, 1)
        xy_loss = tf.reduce_sum(xy_loss, axis=(1, 2, 3))
        wh_loss = tf.reduce_sum(wh_loss, axis=(1, 2, 3))
        obj_loss = tf.reduce_sum(obj_loss, axis=(1, 2, 3))
        class_loss = tf.reduce_sum(class_loss, axis=(1, 2, 3))

        return xy_loss + wh_loss + obj_loss + class_loss
예제 #3
0
    def _train_body(self, real_imgs, noise):
        with tf.device(self._device):
            with tf.GradientTape(persistent=True) as tape:
                fake_imgs = self._G(noise)
                real_logits = self._D(real_imgs)
                fake_logits = self._D(fake_imgs)

                loss_G = tf.reduce_mean(
                    binary_crossentropy(y_true=tf.ones_like(fake_logits),
                                        y_pred=fake_logits,
                                        from_logits=True))
                loss_D = tf.reduce_mean(
                    binary_crossentropy(y_true=tf.ones_like(real_logits),
                                        y_pred=real_logits,
                                        from_logits=True) +
                    binary_crossentropy(y_true=tf.zeros_like(fake_logits),
                                        y_pred=fake_logits,
                                        from_logits=True))

            vars_G = self._G.trainable_variables
            vars_D = self._D.trainable_variables
            self._optimizer_G.apply_gradients(
                zip(tape.gradient(loss_G, vars_G), vars_G))
            self._optimizer_D.apply_gradients(
                zip(tape.gradient(loss_D, vars_D), vars_D))

            self._train_acc.update_state(tf.ones_like(real_logits),
                                         tf.nn.sigmoid(real_logits))
            self._train_acc.update_state(tf.zeros_like(fake_logits),
                                         tf.nn.sigmoid(fake_logits))

        return {"loss_G": loss_G, "loss_D": loss_D}
예제 #4
0
def compute_loss_v3(nn_output, instance_label_ground_truth, P, class_nr,
                    pool_method, r, bbox_weight):
    '''
    Computes image prediction and compares it with the image label in a binary cross entropy loss functions
    :param nn_output: Patch predictions
    :param instance_label_ground_truth: patch ground truth
    :param P: number of patches to divide the image into, horizontally and vertically
    :param class_nr: number of classes
    :param pool_method: pooling method to derive image prediction
    :param bbox_weight: weight in loss for samples with localization annotation
    :return: loss value
    '''
    m = P * P
    sum_active_patches, class_label_ground_truth, has_bbox = compute_ground_truth(
        instance_label_ground_truth, m, class_nr)
    img_label_pred = compute_image_label_prediction_v2(
        has_bbox, nn_output, instance_label_ground_truth, P, class_nr,
        pool_method, r)

    loss = tf.where(
        tf.reshape(has_bbox, (-1, )),
        bbox_weight *
        binary_crossentropy(class_label_ground_truth, img_label_pred),
        binary_crossentropy(class_label_ground_truth, img_label_pred))
    return loss
예제 #5
0
    def yolo_loss(y_true, y_pred):
        #y_pred: 該層的model output(batch_size, grid, grid,  anchor數, (5 + cls))
        #y_true: (batch_size, grid, grid, anchor數, (5+cls))
        batch_size = tf.shape(y_pred)[0]
        bf = tf.cast(batch_size, tf.float32)
        grid_shape = tf.shape(y_pred)[1:3]
        pred_xy, pred_wh, pred_cf, pred_cls = predict_raw_xywh(
            y_pred, input_shape, anchors)

        box_loss_scale = 2 - y_true[..., 2:3] * y_true[..., 3:4]
        object_mask = y_true[..., 4:5]
        #對於每張圖輸出ignore mask
        ignore_mask = tf.TensorArray(tf.float32, size=1, dynamic_size=True)

        def loop_box(index, ignore_mask):
            #從y_true中找出有值得列
            #shape: (N, (x,y,w,h))
            true_box = tf.boolean_mask(y_true[index, ..., 0:4],
                                       tf.squeeze(object_mask[index], -1))
            #每個grid與true_box的IOU,shape:(gird,grid,anchors數,trub_box數)
            iou = box_iou(tf.concat((pred_xy[index], pred_wh[index]), axis=-1),
                          true_box)
            #每個anchors最大的IOU,shape:(grid,grid,anchor數)
            best_iou = tf.reduce_max(iou, axis=-1)
            #根據ignore_thresh添加ignore mask
            ignore_mask = ignore_mask.write(
                index, tf.cast(best_iou < ignore_thresh, tf.float32))
            return index + 1, ignore_mask

        _, ignore_mask = tf.while_loop(lambda index, *args: index < batch_size,
                                       loop_box, [0, ignore_mask])
        ignore_mask = ignore_mask.stack()

        #loss
        xy_loss = object_mask * box_loss_scale * tf.square(y_true[..., 0:2] -
                                                           pred_xy)
        xy_loss = tf.reduce_sum(xy_loss) / bf

        wh_loss = object_mask * box_loss_scale * tf.square(y_true[..., 2:4] -
                                                           pred_wh)
        wh_loss = tf.reduce_sum(wh_loss) / bf

        #print(tf.boolean_mask(y_true[...,0:4], tf.squeeze(object_mask,-1)))

        #obj loss分為兩種
        #是obj,計算confidence loss
        #不是obj,如果與GT的 IOU > thread,則不計算損失,以一個ignore mask計算

        cf_loss = tf.squeeze(object_mask, -1) * binary_crossentropy(
            y_true[..., 4:5], pred_cf)
        cf_loss = cf_loss + tf.squeeze(
            (1 - object_mask), -1) * binary_crossentropy(
                y_true[..., 4:5], pred_cf) * ignore_mask
        cf_loss = tf.reduce_sum(cf_loss) / bf

        cls_loss = tf.squeeze(object_mask, -1) * binary_crossentropy(
            y_true[..., 5:], pred_cls)
        cls_loss = tf.reduce_sum(cls_loss) / bf
        return xy_loss + wh_loss + cf_loss + cls_loss
예제 #6
0
 def discriminator_loss(preds_real, preds_fake):
     loss_real = binary_crossentropy(tf.ones_like(preds_real),
                                     preds_real,
                                     from_logits=True)
     loss_fake = binary_crossentropy(tf.zeros_like(preds_fake),
                                     preds_fake,
                                     from_logits=True)
     return loss_real + loss_fake
def custom_loss(y_true, y_pred):
  # target is a 8 - tuple 
  # (row, col, depth, width, class1, class2, class3, object_appeared)

  bce = binary_crossentropy(y_true[:, :4], y_pred[:, :4]) # location
  cce = categorical_crossentropy(y_true[:, 4:7], y_pred[:, 4:7]) #object class
  bce2 = binary_crossentropy(y_true[:, -1], y_pred[:, -1]) #object appeared

  return bce * y_true[:, -1] + cce * y_true[:, -1] + 0.5 * bce2
def custom_loss(y_true, y_pred):
    # target is a 5-tuple
    # (row, col, depth, width, object_appeared)
    bce = binary_crossentropy(y_true[:, :-1], y_pred[:, :-1])
    bce2 = binary_crossentropy(y_true[:, -1], y_pred[:, -1])
    # Prediction Scale
    pred_scale = 2
    # Binary Classification Scale
    bin_class_scale = 0.5
    return pred_scale * bce * y_true[:, -1] + bin_class_scale * bce2
예제 #9
0
def custom_loss(y_true, y_pred):
	# y_true[i] = (row, col, height, width, 
	#              p_class1, p_class2, p_class3, 
	#              p(object_appeared|img))
	# the bounding box loss:
	bce_1 = binary_crossentropy(y_true[:, :4], y_pred[:, :4])
	# the object class prediction loss:
	cce = categorical_crossentropy(y_true[:, 4:7], y_pred[:, 4:7])
	# the binary prediction (about an object being present in an image) loss:
	bce_2 = binary_crossentropy(y_true[:, -1], y_pred[:, -1])
	return ALPHA * y_true[:, -1] * bce_1 + BETA * y_true[:, -1] * cce + GAMMA * bce_2
예제 #10
0
  def make_model(self):
    # Input layer
    self.inputs = Input(shape=(self.in_dim,), name="enc_in")
    
    # Hidden Layers
    x = self.inputs
    if self.h_dim is not None:
      for (i,d) in enumerate(self.h_dim):
        x = Dense( d, activation='relu', name="enc_l{}".format(i))(x)
    # Mean and Variance
    self.z_mean = Dense(self.latent_dim, name="z_mean")(x)
    
    # Channel
    self.z = Lambda(self.channel, output_shape=(self.latent_dim,), name="z")(self.z_mean)
    
    # Encoder model
    self.encoder = Model(self.inputs, [self.z_mean,self.z], name="encoder")
    
    # Decoder
    self.latent_inputs = Input(shape=(self.latent_dim,), name="z_sample")
    
    # Hidden layers
    x = self.latent_inputs
    if self.h_dim is not None:
      for (i,d) in enumerate(self.h_dim[::-1]):
        x = Dense( d, activation='relu', name="dec_l{}".format(i))(x)
    self.dec_outputs = Dense( self.in_dim, activation='sigmoid', name="decoder_out")(x)
    
    # Decoder model
    self.decoder = Model(self.latent_inputs, self.dec_outputs, name="decoder")
    
    # VAE
    self.outputs = self.decoder(self.encoder(self.inputs)[1])
    self.model = Model(self.inputs, self.outputs, name="VAE")
    
    # Losses
    self.recon_loss = binary_crossentropy(self.inputs, self.outputs)
    self.recon_loss *= self.in_dim # Because binary_crossentropy divides by N


    if self.obj_fn == 'AWGN':
      # print( "Model with AWGN ")
      sig_pow = 0.5/self.sigma0_2 * K.sum(K.square(self.z_mean), axis=-1) 
      noise_term = 0.5 * self.latent_dim * (self.n0/self.latent_dim/self.sigma0_2 - 1.0 - K.log(self.n0/self.latent_dim/self.sigma0_2))
      self.kl_loss = sig_pow + noise_term
    elif self.obj_fn == 'RBF':
      # print( "Model with RBF")
      sig_pow = 0.5/self.sigma0_2 * K.sum(K.square(self.z_mean), axis=-1) 
      noise_term = 0.5 * self.latent_dim * (self.n0/self.latent_dim/self.sigma0_2 - 1.0 - K.log(self.n0/self.latent_dim/self.sigma0_2))
      rbf_term = K.log(1.0 + 0.5*self.latent_dim/self.n0 * sig_pow)
      self.kl_loss = sig_pow + noise_term - rbf_term
    else:
      raise NotImplementedError("Unknown obj_fn: {}".format(self.obj_fn))
    
    
    self.vae_loss = K.mean( self.recon_loss + self.kl_loss )

    self.model.add_loss( self.vae_loss )
    
    self.model.compile( optimizer = 'adam' )
예제 #11
0
    def loop_loss(index, sum_loss):
        #pred box (box, (xmin, ymin, xmax, ymax))
        pred_box = y_pred[index, :, 0:4]
        #找出標記ground truth 的anchor
        obj_mask = tf.reshape(y_true[index, :, 4:5], (-1, ))
        boxes = tf.boolean_mask(y_true[index, :, 0:4], obj_mask)
        #使用nms還原ground truth box
        box_ind = tf.image.non_max_suppression(
            boxes, tf.ones(tf.shape(boxes)[0], tf.float32), 40)
        boxes = tf.gather(boxes, box_ind)

        #計算所有預測bbox 與 ground truth bbox 的 CIoU
        CIoU = box_ciou(y_pred[index, :, 0:4], boxes)
        CIoU_loss = tf.reduce_sum(tf.boolean_mask((1 - CIoU), obj_mask))

        #classes loss (只計算正樣本)
        cls_loss = categorical_crossentropy(y_true[index, :, 5:],
                                            y_pred[index, :, 5:])
        cls_loss = tf.reduce_sum(tf.boolean_mask(cls_loss, obj_mask))

        #計算其他預測bbox與ground truth bbox IoU,如果大於ignore_thresh則不計算conf loss
        ignore_mask = CIoU < 0.7
        #利用ignore_mask與obj_mask算出參與計算的conf loss
        mask = tf.math.logical_or(ignore_mask, tf.cast(obj_mask, tf.bool))
        #confidience loss
        conf_loss = binary_crossentropy(y_true[index, :, 4:5], y_pred[index, :,
                                                                      4:5])
        conf_loss = tf.reduce_sum(tf.boolean_mask(conf_loss, mask))
        return index + 1, sum_loss + CIoU_loss + cls_loss + conf_loss
예제 #12
0
파일: seglosses.py 프로젝트: quantran14/HC
def bce_loss(y_true, y_pred, reduction="mean"):
    """
        BCE(p, 'p) = -(p * log('p) + (1 - p) * log(1 - 'p)
    """
    loss = binary_crossentropy(y_true, y_pred)

    return apply_reduction(loss, reduction=reduction)
예제 #13
0
파일: tf2unet.py 프로젝트: Zenodia/A100TF2
def combined_dice_binary_loss(y_true, y_pred):
    def dice_loss(y_true, y_pred):
        numerator = 2 * tf.reduce_sum(y_true * y_pred, axis=(1, 2, 3))
        denominator = tf.reduce_sum(y_true + y_pred, axis=(1, 2, 3))
        return tf.reshape(1 - numerator / denominator, (-1, 1, 1))

    return binary_crossentropy(y_true, y_pred) + dice_loss(y_true, y_pred)
예제 #14
0
def select_mask_logistic_loss_v1(true, pred):
    print('c', pred.shape, true.shape)
    pred = K.reshape(pred, (-1, 63, 63, 1))
    print('d', pred.shape, true.shape)
    # https://www.tensorflow.org/api_docs/python/tf/image/resize
    pred = tf.image.resize(pred, [127, 127])
    print('a', pred.shape, true.shape)
    pred = K.reshape(pred, (-1, 17, 17, 127 * 127))

    true = K.reshape(true, (-1, 255, 255, 1))
    print('e', pred.shape, true.shape)
    true = tf.image.extract_patches(true,
                                    sizes=(1, 127, 127, 1),
                                    strides=[1, 8, 8, 1],
                                    rates=[1, 1, 1, 1],
                                    padding='VALID')
    print('b', pred.shape, true.shape)
    # weight = K.mean(true, axis=-1)
    # print('weight.shape:', weight.shape)
    # true = (true * 2) - 1
    # pred = K.tanh(pred)
    # loss = K.log(1 + K.exp(-pred * true))
    pred = K.sigmoid(pred)
    loss = binary_crossentropy(true, pred)
    # loss = K.mean(loss, axis=-1)
    # loss = K.mean(loss * weight)
    # loss = K.sum(loss) / K.sum(weight)
    # loss = K.log(1 + K.exp(-pred[:, 8, 8] * true[:, 8, 8]))
    print('loss:', loss.shape)
    return loss
    def call(self, y_true, y_pred):
        if 0:  # TODO experiment:
            # bump y_true false labels to help w/ false false positives
            # (false negatives in data due to incomplete masks)
            t_true = tf.add(y_true, .2, name="false neg bump?")
            t_true = tf.clip_by_value(y_true, 0, 1, name="clip after bump")

        loss = losses.binary_crossentropy(y_true, y_pred)
        # TODO: should entropy be w/ log base 2?
        entropy = ImageEntropy._tf_mean_entropy(y_pred, axes=(1, 2, 3))

        std = ImageEntropy.sliding_stddev(y_pred)
        pos = ImageEntropy.positive_cross_entropy(y_true, y_pred)

        loss = tf.reduce_mean(loss, axis=(1, 2))

        #loss = tf.multiply(loss, self.loss_weights[0], name="weight_bin_crossentropy")
        #entropy = tf.multiply(entropy, self.loss_weights[1], name="weight_entropy")
        #pos = tf.multiply(entropy, self.loss_weights[2], name="weight_entropy")

        return tf.math.accumulate_n((
            loss,
            entropy,
            pos,
            std,
        ), )
예제 #16
0
def my_binary_crossentropy(y_true, y_pred, class_weight_0, class_weight_1):
    #     print(f"y_true: {y_true}")
    mask_value = tf.constant(-1)
    #     mask_y_true = tf.not_equal(tf.cast(y_true, dtype=tf.int32), tf.cast(mask_value, dtype=tf.int32))

    #     mask = tf.zeros(shape=y_true.shape)
    zero_value = tf.constant(0)
    #     print(f"cond0: {tf.equal(y_true, mask_value)}")
    #     print(f"cond1: {tf.equal(y_true, zero_value)}")
    #     weight = [tf.cond(tf.equal(x, mask_value), lambda: 0, tf.cond(tf.equal(x, zero_value), lambda: class_weights[0], lambda: class_weights[1])) for x in y_true]
    #     weight = [0 if x[0]==-1 else class_weights[x[0]] for x in y_true]
    y_true_0 = tf.equal(tf.cast(y_true, dtype=tf.int32),
                        tf.cast(tf.constant(0), dtype=tf.int32))
    weight_0 = tf.cast(y_true_0, dtype=tf.float32) * tf.cast(
        tf.constant(class_weight_0), dtype=tf.float32)
    y_true_1 = tf.equal(tf.cast(y_true, dtype=tf.int32),
                        tf.cast(tf.constant(1), dtype=tf.int32))
    weight_1 = tf.cast(y_true_1, dtype=tf.float32) * tf.cast(
        tf.constant(class_weight_1), dtype=tf.float32)
    weight = weight_0 + weight_1
    #     print(f"weight: {weight}")

    bin_loss = binary_crossentropy(y_true, y_pred)
    #     print(f"bin_loss: {bin_loss}")
    #     f1_loss = f1_loss(y_true, y_pred)
    #     loss = bin_loss + f1_loss

    loss_ = tf.cast(bin_loss, dtype=tf.float32) * tf.cast(weight,
                                                          dtype=tf.float32)
    #     print(f"loss_: {loss_}")
    loss_abs = tf.abs(loss_)
    #     print(f"loss_abs: {loss_abs}")

    return loss_abs
예제 #17
0
    def loss(y_true, y_pred):
        obj_true = y_true[...,4:5]
        obj_pred = y_pred[...,4:5]

        # get the box with the highest percentage in each image
        true_box = get_box_highest_percentage(y_true)
        pred_box = get_box_highest_percentage(y_pred)

        # get all boxes with overlap > iou_threshold or ground truth boxes
        pred_boxes = transform_pred_boxes(y_pred)
        iou = calculate_iou(true_box, pred_boxes)
        mask = tf.cast(tf.greater(iou, iou_threshold) | tf.equal(obj_true, 1), tf.float32)

        # object loss based on previous mask
        # iou > 0.5 means we found a valid box position
        obj_loss = binary_crossentropy(mask, obj_pred)

        # mse with the boxes that have the highest percentage
        box_loss = tf.reduce_mean(tf.squared_difference(true_box[...,:-1], pred_box[...,:-1]))

        # mse with all boxes weighted by iou
        shape = tf.shape(y_true)
        true_box = tf.reshape(true_box, (shape[0], 1, 1, -1))
        true_box = tf.tile(true_box, (1, shape[1], shape[2], 1))
        box_loss_w = tf.reduce_mean(iou ** gamma * tf.squared_difference(true_box[...,:-1], pred_boxes[...,:-1]))

        return obj_loss + box_loss + box_loss_w
예제 #18
0
    def vae_loss(self, x, x_decoded_mean, z, z_mean, z_log_var):
        gamma = self.compute_p_c_z(z)
        gamma_t = K.repeat(gamma, self.latent_dim)

        u_tensor3 = self.compute_u_tensor3()
        lambda_tensor3 = self.compute_lambda_tensor3()

        assert z_mean.shape[1:] == (
            self.latent_dim, ), 'z_mean.shape[1:] {} != {}'.format(
                z_mean.shape[1:], (self.latent_dim, ))
        z_mean_t = K.permute_dimensions(K.repeat(z_mean, self.n_clusters),
                                        [0, 2, 1])
        assert z_mean_t.shape[1:] == (
            self.latent_dim,
            self.n_clusters), 'z_mean_t.shape[1:] {} != {}'.format(
                z_mean_t.shape[1:], (self.latent_dim, self.n_clusters))

        assert z_log_var.shape[1:] == (
            self.latent_dim, ), 'z_log_var.shape[1:] {} != {}'.format(
                z_log_var.shape[1:], (self.latent_dim, ))
        z_log_var_t = K.permute_dimensions(
            K.repeat(z_log_var, self.n_clusters), [0, 2, 1])
        assert z_log_var_t.shape[1:] == (
            self.latent_dim,
            self.n_clusters), 'z_log_var_t.shape[1:] {} != {}'.format(
                z_log_var_t.shape[1:], (self.latent_dim, self.n_clusters))

        loss=self.input_dim * losses.binary_crossentropy(x, x_decoded_mean)\
        +K.sum(0.5*gamma_t*(self.latent_dim*K.log(math.pi*2)+K.log(lambda_tensor3)+K.exp(z_log_var_t)/lambda_tensor3+K.square(z_mean_t-u_tensor3)/lambda_tensor3),axis=(1,2))\
        -0.5*K.sum(z_log_var+1,axis=-1)\
        -K.sum(K.log(K.repeat_elements(K.expand_dims(self.theta_p, axis=0),self.batch_size,0))*gamma,axis=-1)\
        +K.sum(K.log(gamma)*gamma,axis=-1)

        return loss
예제 #19
0
def vae_loss(x, x_decoded_mean):
    xent_loss = binary_crossentropy(x, x_decoded_mean)
    kl_loss = VAE_B2 * tf.keras.backend.mean(
        1 + z_log_sigma_sq - tf.keras.backend.square(z_mean) -
        tf.keras.backend.exp(z_log_sigma_sq),
        axis=None)
    return xent_loss - kl_loss
예제 #20
0
 def vae_loss_fn(x, x_decoded_mean):
     x = K.flatten(x)
     x_decoded_mean = K.flatten(x_decoded_mean)
     flat_dim = np.product(self.img_dim)
     reconst_loss = flat_dim * binary_crossentropy(x, x_decoded_mean)
     kl_loss = -0.5 * K.sum(
         1 + z_log_sigma - K.square(z_mu) - K.exp(z_log_sigma), axis=-1)
     return reconst_loss + (self.beta * kl_loss)
예제 #21
0
        def yolo_loss(y_true, y_pred):
            """ Code from: https://github.com/zzh8829/yolov3-tf2/blob/master/yolov3_tf2/dataset.py """
            # 1. transform all pred outputs
            # y_pred: (batch_size, grid, grid, anchors, (x, y, w, h, obj, ...cls))
            pred_box, pred_obj, pred_class, pred_xywh = self.extract_from_predictions(
                y_pred, scale_index)
            pred_xy = pred_xywh[..., 0:2]
            pred_wh = pred_xywh[..., 2:4]

            # 2. transform all true outputs
            # y_true: (batch_size, grid, grid, anchors, (x1, y1, x2, y2, obj, cls))
            true_box, true_obj, true_class_idx = tf.split(y_true, (4, 1, 1),
                                                          axis=-1)
            true_xy = (true_box[..., 0:2] + true_box[..., 2:4]) / 2
            true_wh = true_box[..., 2:4] - true_box[..., 0:2]

            # give higher weights to small boxes
            box_loss_scale = 2 - true_wh[..., 0] * true_wh[..., 1]

            # 3. inverting the pred box equations
            grid_size = tf.shape(y_true)[1]
            grid = tf.meshgrid(tf.range(grid_size), tf.range(grid_size))
            grid = tf.expand_dims(tf.stack(grid, axis=-1), axis=2)
            true_xy = true_xy * tf.cast(grid_size, tf.float32) - tf.cast(
                grid, tf.float32)
            true_wh = tf.math.log(true_wh /
                                  self.anchors[self.masks[scale_index]])
            true_wh = tf.where(tf.math.is_inf(true_wh), tf.zeros_like(true_wh),
                               true_wh)

            # 4. calculate all masks
            obj_mask = tf.squeeze(true_obj, -1)
            # ignore false positive when iou is over threshold
            true_box_flat = tf.boolean_mask(true_box,
                                            tf.cast(obj_mask, tf.bool))
            best_iou = tf.reduce_max(helpers.broadcast_iou(
                pred_box, true_box_flat),
                                     axis=-1)
            ignore_mask = tf.cast(best_iou < self.iou_threshold, tf.float32)

            # 5. calculate all losses
            xy_loss = obj_mask * box_loss_scale * tf.reduce_sum(
                tf.square(true_xy - pred_xy), axis=-1)
            wh_loss = obj_mask * box_loss_scale * tf.reduce_sum(
                tf.square(true_wh - pred_wh), axis=-1)
            obj_loss = binary_crossentropy(true_obj, pred_obj)
            obj_loss = obj_mask * obj_loss + (
                1 - obj_mask) * ignore_mask * obj_loss
            class_loss = obj_mask * sparse_categorical_crossentropy(
                true_class_idx, pred_class)

            # 6. sum over (batch, gridx, gridy, anchors) => (batch, 1)
            xy_loss = tf.reduce_sum(xy_loss, axis=(1, 2, 3))
            wh_loss = tf.reduce_sum(wh_loss, axis=(1, 2, 3))
            obj_loss = tf.reduce_sum(obj_loss, axis=(1, 2, 3))
            class_loss = tf.reduce_sum(class_loss, axis=(1, 2, 3))

            return xy_loss + wh_loss + obj_loss + class_loss
예제 #22
0
def vae_loss(x, x_decoded):
  flatten_x = K.flatten(x)
  flatten_x_decoded = K.flatten(x_decoded)
  rec_loss = binary_crossentropy(flatten_x, flatten_x_decoded) * 28 * 28
  kl_loss = 1 + z_log_var - K.square(z_mu) - K.exp(z_log_var)
  kl_loss = K.sum(kl_loss, axis=-1)
  kl_loss *= -0.8

  return K.mean(rec_loss + kl_loss)
예제 #23
0
def dual_loss(y_true, y_pred):
    def dice_loss(y_true, y_pred):
        numerator = 2 * tf.math.reduce_sum(y_true * y_pred, axis=(1, 2))
        denominator = tf.math.reduce_sum(y_true + y_pred, axis=(1, 2))

        return tf.reshape(1 - numerator / denominator, (-1, 1, 1))

    return (0.5 * binary_crossentropy(y_true, y_pred)) + (
        0.5 * dice_loss(y_true, y_pred))
예제 #24
0
 def _vae_loss1(self, input, output):
     input_flat = K.flatten(input)
     output_flat = K.flatten(output)
     xent_loss = (
         self.image_size[0]
         * self.image_size[1]
         * objectives.binary_crossentropy(input_flat, output_flat)
     )
     return xent_loss
예제 #25
0
    def _get_vae_loss(self, x, x_bar): 
        """Calculate negative ELBO (NELBO)."""
        # Reconstruction error: logistic log likelihood 
        reconst_loss = self.original_dim * binary_crossentropy(x, x_bar)

        # Kullback–Leibler divergence 
        kl_loss = 0.5 * K.sum(-1 - self.z_log_var + K.square(self.z_mean) + K.exp(self.z_log_var), axis=-1)

        return reconst_loss + self.beta * kl_loss
예제 #26
0
    def vae_loss(self):

        xent_loss = 784 * binary_crossentropy(K.flatten(self.inputs),
                                              K.flatten(self.outputs))
        kl_loss = (-0.5 - 0.5 * self.z_log_var + 0.5 * K.square(self.z_mean) +
                   0.5 * K.exp(self.z_log_var))
        kl_loss = K.sum(kl_loss, axis=-1)

        return K.mean(xent_loss + kl_loss)
예제 #27
0
def weighted_loss(y_true,y_pred):
    from tensorflow.keras.losses import binary_crossentropy
    n_classes=4
    class_weight={0:0.2,1:350.0,2:160.0,3:465.0}
    total=0.0
    for i in range(n_classes):
        # total_weigth+=class_weight[i]
        total+=binary_crossentropy(y_true[...,i],y_pred[...,i])*class_weight[i]
    return total
예제 #28
0
def loss(y_true, y_pred):
    def dice_coefficient(y_true, y_pred):
        numerator = 2 * tf.reduce_sum(y_true * y_pred, axis=-1)
        denominator = tf.reduce_sum(y_true + y_pred, axis=-1)

        return numerator / (denominator + epsilon())

    return binary_crossentropy(
        y_true, y_pred) - tf.log(dice_coefficient(y_true, y_pred) + epsilon())
예제 #29
0
def build_cvae(z_dim = 8,
    inter_dim = 256,
    seq_length = 128,                  
    n_features = 13):
        inputs = Input(shape=(seq_length,n_features))
        x_encoded = Conv1D(inter_dim, 3, padding='same', activation='relu')(inputs)
        x_encoded = MaxPooling1D(pool_size=2, padding='same')(x_encoded)
        x_encoded = Dropout(0.5)(x_encoded)
        x_encoded = Conv1D(inter_dim, 3, padding='same', activation='relu')(x_encoded)
        x_encoded = MaxPooling1D(pool_size=2, padding='same')(x_encoded)
        x_encoded = BatchNormalization()(x_encoded)
        x_encoded = Conv1D(inter_dim, 3, padding='same', activation='relu')(x_encoded)
        x_encoded = MaxPooling1D(pool_size=2, padding='same')(x_encoded)
        x_encoded = Dropout(0.5)(x_encoded)
        x_encoded = Flatten()(x_encoded)
        x_encoded = Dense(500, activation='relu')(x_encoded)
        x_encoded = Dropout(0.5)(x_encoded)
        x_encoded = Dense(25, activation='relu')(x_encoded)
        mu = Dense(z_dim, activation='linear')(x_encoded)
        log_var = Dense(z_dim, activation='linear')(x_encoded)
        z = Lambda(sampling, output_shape=(z_dim,))([mu, log_var])
        encoder = Model(inputs, [mu, log_var, z], name='encoder')

        latent_inputs = Input(shape=(z_dim,))
        z_decoder = Dense(z_dim, activation='relu')(latent_inputs)
        z_decoder = Dense(25, activation='relu')(z_decoder)
        z_decoder = Dense(500, activation='relu')(z_decoder)
        z_decoder = Dense(int(seq_length/8)*inter_dim, activation='relu')(z_decoder)
        z_decoder = Reshape((int(seq_length/8), inter_dim))(z_decoder)
        z_decoder = UpSampling1D(2)(z_decoder)
        z_decoder = Conv1D(inter_dim,3,padding='same', activation='relu')(z_decoder)
        z_decoder = UpSampling1D(2)(z_decoder)
        z_decoder = Conv1D(inter_dim,3,padding='same', activation='relu')(z_decoder)
        z_decoder = UpSampling1D(2)(z_decoder)
        z_decoder = Conv1D(inter_dim,3,padding='same', activation='relu')(z_decoder)
                        # No activation
        decoder_output = Dense(n_features, activation='relu')(z_decoder)

        decoder = Model(latent_inputs, decoder_output, name='decoder')
        
        outputs = decoder(encoder(inputs)[2])
        # build model
        cvae = Model(inputs, outputs)
        
        # loss
        reconstruction_loss = tf.reduce_mean(binary_crossentropy(inputs, outputs)) * (seq_length*n_features)
        kl_loss = 1 + log_var - tf.square(mu) - tf.exp(log_var)
        kl_loss = tf.reduce_mean(kl_loss)
        kl_loss *= -0.5
        total_loss = reconstruction_loss + kl_loss

        # build model
        optimizer = tf.keras.optimizers.Adam(learning_rate=0.0005)
        cvae.add_loss(total_loss)
        cvae.compile(optimizer='rmsprop')
        return encoder, decoder, cvae
예제 #30
0
파일: vae.py 프로젝트: andy94077/HYML
def build_vae(input_shape):
    '''
    Arguments:
        input_shape(tuple): the shape of input images (H, W, C)
    
    Returns:
        encoder, decoder, autoencoder models
    '''
    def build_encoder(input_shape, latent_dim):
        inputs = Input(input_shape)
        x = Conv2D(256, 4, strides=2, padding='same', activation='relu')(inputs)
        x = Conv2D(128, 4, strides=2, padding='same', activation='relu')(x)
        x = Conv2D(64, 4, strides=2, padding='same', activation='relu')(x)
        # x = Conv2D(64, 4, strides=2, padding='same', activation='relu')(x)
        x = Flatten()(x)
        mean = Dense(latent_dim)(x)
        logvar = Dense(latent_dim)(x)

        epsilon = K.random_normal(K.shape(mean))
        z = mean + K.exp(0.5 * logvar) * epsilon
        encoder = Model(inputs, [z, mean, logvar], name='encoder')
        return encoder

    def build_decoder(latent_dim):
        decoder = Sequential([
            Dense(4* 4* 64, activation='relu', input_shape=(latent_dim,)),
            Reshape((4, 4, 64)),
            # Conv2DTranspose(128, 4, strides=2, padding='same', activation='relu'),
            Conv2DTranspose(64, 4, strides=2, padding='same', activation='relu'),
            Conv2DTranspose(32, 4, strides=2, padding='same', activation='relu'),
            Conv2DTranspose(3, 4, strides=2, padding='same', activation='sigmoid')
        ], name='decoder')
        return decoder

    latent_dim = 512
    encoder = build_encoder(input_shape, latent_dim)
    # encoder.summary()
    decoder = build_decoder(latent_dim)
    # decoder.summary()

    inputs = Input(input_shape)
    z, mean, logvar = encoder(inputs)
    decoder_out = decoder(z)
    autoencoder = Model(inputs, decoder_out)

    bce_loss = K.sum(binary_crossentropy(inputs, decoder_out), axis=[1, 2])
    kl_loss = -0.5 * K.sum(1 + logvar - K.square(mean) - K.exp(logvar), axis=-1)
    vae_loss = K.mean(bce_loss + kl_loss)
    autoencoder.add_loss(vae_loss)

    autoencoder.add_metric(tf.reduce_mean(bce_loss), name='bce_sum', aggregation='mean')
    autoencoder.add_metric(tf.reduce_mean(bce_loss) / input_shape[0] / input_shape[1], name='bce', aggregation='mean')
    autoencoder.add_metric(tf.reduce_mean(kl_loss), name='KL', aggregation='mean')
    autoencoder.compile(Adam(1e-3, decay=5e-4))

    return encoder, decoder, autoencoder