latent_inputs = Input(shape=(latent_dim,), name='z_sampling')
x = Dense(hidden_dim, activation='relu')(latent_inputs)
outputs = Dense(image_size, activation='sigmoid')(x)
# instantiate decoder model
decoder = Model(latent_inputs, outputs, name='decoder')
plot_model(decoder, to_file='vae_mlp_decoder.png', show_shapes=True)
# instantiate VAE model
outputs = decoder(encoder(inputs)[2])
vae = Model(inputs, outputs, name='vae_mlp')
models = (encoder, decoder)
data = (x_test, y_test)
# calculate reconstruction loss
reconstruction_loss = binary_crossentropy(inputs, outputs) * image_size
# KL divergence loss
k_loss = 1 + z_log_var - K.square(z_mean) - K.exp(z_log_var)
k_loss = -0.5 * K.sum(k_loss, axis=-1)
# create custom loss layer
vae_loss = K.mean(reconstruction_loss + k_loss)
vae.add_loss(vae_loss)
# compile vae
vae.compile(optimizer='adam')
vae.summary()
plot_model(vae,
           to_file='vae_mlp.png',
           show_shapes=True)

# train the VAE
history = vae.fit(x_train,
        epochs=epochs,
        batch_size=batch_size,
        validation_data=(x_valid, None))
Esempio n. 2
0
def yolo_wh_loss(y_true, y_pred, t):
    error = K.square(K.sqrt(y_true)-K.sqrt(y_pred))
    object_true = COORD_SCALE*(error)
    object_false = K.zeros_like(y_true, dtype='float32')
    loss = tf.where(t, object_true, object_false)
    return K.sum(loss)
Esempio n. 3
0
def custom_loss(y_actual, y_predicted):
    custom_loss_value = K.square(y_predicted - y_actual)
    custom_loss_value = K.sum(custom_loss_value, axis=1)
    return custom_loss_value
Esempio n. 4
0
def kl_loss(z_mean, z_log_var):
    kl_loss = -0.5 * K.sum(1 + z_log_var - K.square(z_mean) - K.exp(z_log_var),
                           axis=-1)
    return kl_loss
Esempio n. 5
0
def yolo_loss(args, anchors, num_classes, ignore_thresh=.5, print_loss=False):
    '''Return yolo_loss tensor

    Parameters
    ----------
    yolo_outputs: list of tensor, the output of yolo_body or tiny_yolo_body
    y_true: list of array, the output of preprocess_true_boxes
    anchors: array, shape=(N, 2), wh
    num_classes: integer
    ignore_thresh: float, the iou threshold whether to ignore object confidence loss

    Returns
    -------
    loss: tensor, shape=(1,)

    '''
    num_layers = len(anchors)//3 # default setting
    yolo_outputs = args[:num_layers]
    y_true = args[num_layers:]
    anchor_mask = [[6,7,8], [3,4,5], [0,1,2]] if num_layers==3 else [[3,4,5], [1,2,3]]
    input_shape = K.cast(K.shape(yolo_outputs[0])[1:3] * 32, K.dtype(y_true[0]))
    grid_shapes = [K.cast(K.shape(yolo_outputs[l])[1:3], K.dtype(y_true[0])) for l in range(num_layers)]
    loss = 0
    m = K.shape(yolo_outputs[0])[0] # batch size, tensor
    mf = K.cast(m, K.dtype(yolo_outputs[0]))

    for l in range(num_layers):
        object_mask = y_true[l][..., 4:5]
        true_class_probs = y_true[l][..., 5:]

        grid, raw_pred, pred_xy, pred_wh = yolo_head(yolo_outputs[l],
             anchors[anchor_mask[l]], num_classes, input_shape, calc_loss=True)
        pred_box = K.concatenate([pred_xy, pred_wh])

        # Darknet raw box to calculate loss.
        raw_true_xy = y_true[l][..., :2]*grid_shapes[l][::-1] - grid
        raw_true_wh = K.log(y_true[l][..., 2:4] / anchors[anchor_mask[l]] * input_shape[::-1])
        raw_true_wh = K.switch(object_mask, raw_true_wh, K.zeros_like(raw_true_wh)) # avoid log(0)=-inf
        box_loss_scale = 2 - y_true[l][...,2:3]*y_true[l][...,3:4]

        # Find ignore mask, iterate over each of batch.
        ignore_mask = tf.TensorArray(K.dtype(y_true[0]), size=1, dynamic_size=True)
        object_mask_bool = K.cast(object_mask, 'bool')
        def loop_body(b, ignore_mask):
            true_box = tf.boolean_mask(y_true[l][b,...,0:4], object_mask_bool[b,...,0])
            iou = box_iou(pred_box[b], true_box)
            best_iou = K.max(iou, axis=-1)
            ignore_mask = ignore_mask.write(b, K.cast(best_iou<ignore_thresh, K.dtype(true_box)))
            return b+1, ignore_mask
        _, ignore_mask = tf.while_loop(lambda b,*args: b<m, loop_body, [0, ignore_mask])
        ignore_mask = ignore_mask.stack()
        ignore_mask = K.expand_dims(ignore_mask, -1)

        # K.binary_crossentropy is helpful to avoid exp overflow.
        xy_loss = object_mask * box_loss_scale * K.binary_crossentropy(raw_true_xy, raw_pred[...,0:2], from_logits=True)
        wh_loss = object_mask * box_loss_scale * 0.5 * K.square(raw_true_wh-raw_pred[...,2:4])
        confidence_loss = object_mask * K.binary_crossentropy(object_mask, raw_pred[...,4:5], from_logits=True)+ \
            (1-object_mask) * K.binary_crossentropy(object_mask, raw_pred[...,4:5], from_logits=True) * ignore_mask
        class_loss = object_mask * K.binary_crossentropy(true_class_probs, raw_pred[...,5:], from_logits=True)

        xy_loss = K.sum(xy_loss) / mf
        wh_loss = K.sum(wh_loss) / mf
        confidence_loss = K.sum(confidence_loss) / mf
        class_loss = K.sum(class_loss) / mf
        loss += xy_loss + wh_loss + confidence_loss + class_loss
        if print_loss:
            loss = tf.Print(loss, [loss, xy_loss, wh_loss, confidence_loss, class_loss, K.sum(ignore_mask)], message='loss: ')
    return loss
Esempio n. 6
0
 def call(self, x, mask=None):
     norm = K.sqrt(K.sum(K.square(x), axis=-1, keepdims=True)) + K.epsilon()
     x = x / norm * self.gamma
     return x
Esempio n. 7
0
    help_ = "Use binary cross entropy instead of mse (default)"
    parser.add_argument("--bce", help=help_, action='store_true')
    args = parser.parse_args()
    models = (encoder, decoder)
    data = (x_test, y_test)

    # VAE loss = mse_loss or xent_loss + kl_loss
    if args.bce:
        reconstruction_loss = binary_crossentropy(K.flatten(inputs),
                                                  K.flatten(outputs))
    else:
        reconstruction_loss = mse(K.flatten(inputs), K.flatten(outputs))

    reconstruction_loss *= image_size * image_size
    kl_loss = 1 + z_log_var - K.square(z_mean) - K.exp(z_log_var)
    kl_loss = K.sum(kl_loss, axis=-1)
    kl_loss *= -0.5
    vae_loss = K.mean(reconstruction_loss + kl_loss)
    vae.add_loss(vae_loss)
    vae.compile(optimizer='rmsprop')
    vae.summary()
    plot_model(vae, to_file='vae_cnn.png', show_shapes=True)

    save_dir = "vae_cnn_weights"
    if not os.path.isdir(save_dir):
        os.makedirs(save_dir)
    if args.weights:
        filepath = os.path.join(save_dir, args.weights)
        vae = vae.load_weights(filepath)
    else:
        # train the autoencoder
Esempio n. 8
0
 def kl_loss(self, w, mu, sigma):
     variational_dist = tfp.distributions.Normal(mu, sigma)
     return self.kl_weight * K.sum(
         variational_dist.log_prob(w) - self.log_prior_prob(w))
Esempio n. 9
0
def dice_coef(y_true, y_pred):
    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 + 1) / (keras.sum(y_true_f) + keras.sum(y_pred_f) + 1)
Esempio n. 10
0
def yolo_loss(y_true, y_pred):
    label_class = y_true[..., :1]  # ? * 7 * 7 * 1
    # 分类
    label_box = y_true[..., 1:5]  # ? * 7 * 7 * 4
    # BB1的坐标
    response_mask = y_true[..., 5]  # ? * 7 * 7
    # BB1的置信度
    response_mask = K.expand_dims(response_mask)  # ? * 7 * 7 * 1

    predict_class = y_pred[..., :1]  # ? * 7 * 7 * 1
    # 分类
    predict_trust = y_pred[..., 1:3]  # ? * 7 * 7 * 2
    # BB1和BB2的置信度
    predict_box = y_pred[..., 3:]  # ? * 7 * 7 * 8
    # BB1和BB2的坐标

    _label_box = K.reshape(label_box, [-1, 7, 7, 1, 4])
    _predict_box = K.reshape(predict_box, [-1, 7, 7, 2, 4])

    label_xy, label_wh = yolo_head(_label_box, img_size=224)  # ? * 7 * 7 * 1 * 2, ? * 7 * 7 * 1 * 2
    label_xy = K.expand_dims(label_xy, 3)  # ? * 7 * 7 * 1 * 1 * 2
    label_wh = K.expand_dims(label_wh, 3)  # ? * 7 * 7 * 1 * 1 * 2
    label_xy_min, label_xy_max = X_Y_W_H_To_Min_Max(label_xy,
                                                    label_wh)  # ? * 7 * 7 * 1 * 1 * 2, ? * 7 * 7 * 1 * 1 * 2

    predict_xy, predict_wh = yolo_head(_predict_box, img_size=224)  # ? * 7 * 7 * 2 * 2, ? * 7 * 7 * 2 * 2
    predict_xy = K.expand_dims(predict_xy, 4)  # ? * 7 * 7 * 2 * 1 * 2
    predict_wh = K.expand_dims(predict_wh, 4)  # ? * 7 * 7 * 2 * 1 * 2
    predict_xy_min, predict_xy_max = X_Y_W_H_To_Min_Max(predict_xy,
                                                        predict_wh)  # ? * 7 * 7 * 2 * 1 * 2, ? * 7 * 7 * 2 * 1 * 2

    iou_scores = iou(predict_xy_min, predict_xy_max, label_xy_min, label_xy_max)  # ? * 7 * 7 * 2 * 1
    best_ious = K.max(iou_scores, axis=4)  # ? * 7 * 7 * 2
    best_box = K.max(best_ious, axis=3, keepdims=True)  # ? * 7 * 7 * 1

    box_mask = K.cast(best_ious >= best_box, K.dtype(best_ious))  # ? * 7 * 7 * 2

    no_object_loss = 0.5 * (1 - box_mask * response_mask) * K.square(0 - predict_trust)
    object_loss = box_mask * response_mask * K.square(1 - predict_trust)
    confidence_loss = no_object_loss + object_loss
    confidence_loss = K.sum(confidence_loss)

    class_loss = response_mask * K.square(label_class - predict_class)
    class_loss = K.sum(class_loss)

    _label_box = K.reshape(label_box, [-1, 7, 7, 1, 4])
    _predict_box = K.reshape(predict_box, [-1, 7, 7, 2, 4])

    label_xy, label_wh = yolo_head(_label_box, img_size=224)  # ? * 7 * 7 * 1 * 2, ? * 7 * 7 * 1 * 2
    predict_xy, predict_wh = yolo_head(_predict_box, img_size=224)  # ? * 7 * 7 * 2 * 2, ? * 7 * 7 * 2 * 2

    box_mask = K.expand_dims(box_mask)
    response_mask = K.expand_dims(response_mask)

    box_loss = 5 * box_mask * response_mask * K.square((label_xy - predict_xy) / 224)
    box_loss += 5 * box_mask * response_mask * K.square((K.sqrt(label_wh) - K.sqrt(predict_wh)) / 224)
    box_loss = K.sum(box_loss)

    loss = confidence_loss + class_loss + box_loss

    return loss
Esempio n. 11
0
def neg_log_likelihood(y_obs, y_pred, sigma=noise):
    dist = tfp.distributions.Normal(loc=y_pred, scale=sigma)
    return K.sum(-dist.log_prob(y_obs))
Esempio n. 12
0
alignment_probs = K.softmax(
    dot([Dense(hidden_dim)(h_enc), h_dec], axes=-1, normalize=False), -2)
h_enc_rep = K.tile(K.expand_dims(h_enc, -2), [1, 1, T_y, 1])
h_dec_rep = K.tile(K.expand_dims(h_dec, -3), [1, T_x, 1, 1])
h_rep = K.concatenate([h_enc_rep, h_dec_rep], -1)

alignment_probs_ = []
for i in range(T_y):
    if i == 0:
        align_prev_curr = tf.gather(alignment_probs, i, axis=-1)
    if i > 0:
        align_prev_curr = tf.einsum('nx,ny->nxy',
                                    tf.gather(alignment_probs, i, axis=-1),
                                    alignment_probs_[i - 1])
        align_prev_curr *= struc_zeros
        align_prev_curr = K.sum(align_prev_curr, 1) + 1e-6
        align_prev_curr /= K.sum(align_prev_curr, -1, keepdims=True)
    alignment_probs_.append(align_prev_curr)

alignment_probs_ = K.stack(alignment_probs_, -1)
emission_probs = Dense(hidden_dim * 3, activation='tanh')(h_rep)
emission_probs = Dense(Y, activation='softmax')(emission_probs)

alignment_probs_ = Lambda(lambda x: x)(alignment_probs_)

alignment_model = Model([enc_in_, dec_in_], alignment_probs_)
alignment_output = alignment_model([enc_in_, dec_in_])
alignment = Model([enc_in_, dec_in_], alignment_output)

alphas = tf.expand_dims(alignment_probs_, -1) * emission_probs
dec_out_ = tf.reduce_sum(alphas, -3)
Esempio n. 13
0
def _l2normalizer(v, epsilon=1e-4):
    return v / (K.sum(v**2)**0.5 + epsilon)
Esempio n. 14
0
    def call(self, inputs, states, targets, training=None):
        h_tm1 = states[
            0]  # previous set of memory states: shape (batch_size, number of particles, number of units)
        c_tm1 = states[
            1]  # previous set of carry states: shape (batch_size, number of particles, number of units)
        # adding the set of weights
        w_tm1 = states[
            2]  # previous set of weights (for SMC): shape (batch_size, number of particles)

        # RESAMPLING STEP FOR LSTM with particle filter
        indices = tf.random.categorical(
            logits=w_tm1, num_samples=self.particles
        )  # see if we need to use numpy functions instead
        indices = list(K.eval(indices))
        h_sampl = h_tm1[:, indices, :]
        c_sampl = c_tm1[:, indices, :]

        # TO DO: check these 2 functions to see if they need to be adjusted for PF LSTM Cell.
        dp_mask = self.get_dropout_mask_for_cell(inputs, training, count=4)
        rec_dp_mask = self.get_recurrent_dropout_mask_for_cell(h_tm1,
                                                               training,
                                                               count=4)

        if self.implementation == 1:
            if 0 < self.dropout < 1.:
                inputs_i = inputs * dp_mask[0]
                inputs_f = inputs * dp_mask[1]
                inputs_c = inputs * dp_mask[2]
                inputs_o = inputs * dp_mask[3]
            else:
                inputs_i = inputs
                inputs_f = inputs
                inputs_c = inputs
                inputs_o = inputs
            # split on last axis
            k_i, k_f, k_c, k_o = array_ops.split(self.kernel,
                                                 num_or_size_splits=4,
                                                 axis=-1)
            x_i = K.dot(inputs_i, k_i)
            x_f = K.dot(inputs_f, k_f)
            x_c = K.dot(inputs_c, k_c)
            x_o = K.dot(inputs_o, k_o)
            if self.use_bias:
                b_i, b_f, b_c, b_o = array_ops.split(self.bias,
                                                     num_or_size_splits=4,
                                                     axis=0)
                x_i = K.bias_add(x_i, b_i)
                x_f = K.bias_add(x_f, b_f)
                x_c = K.bias_add(x_c, b_c)
                x_o = K.bias_add(x_o, b_o)

            if 0 < self.recurrent_dropout < 1.:
                h_sampl_i = h_sampl * rec_dp_mask[0]
                h_sampl_f = h_sampl * rec_dp_mask[1]
                h_sampl_c = h_sampl * rec_dp_mask[2]
                h_sampl_o = h_sampl * rec_dp_mask[3]
            else:
                h_tm1_i = h_sampl
                h_tm1_f = h_sampl
                h_tm1_c = h_sampl
                h_tm1_o = h_sampl
            x = (x_i, x_f, x_c, x_o)
            h_sampl = (h_sampl_i, h_sampl_f, h_sampl_c, h_sampl_o)
            c, o = self._compute_carry_and_output(x, h_sampl, c_sampl)
        else:
            if 0. < self.dropout < 1.:
                inputs *= dp_mask[0]
            z = K.dot(inputs, self.kernel)
            if 0. < self.recurrent_dropout < 1.:
                h_tm1 *= rec_dp_mask[0]
            z += K.dot(h_sampl, self.recurrent_kernel)
            if self.use_bias:
                z = K.bias_add(z, self.bias)

            z = array_ops.split(z, num_or_size_splits=4, axis=1)
            c, o = self._compute_carry_and_output_fused(z, c_sampl)

        h = o * self.activation(c)

        # COMPUTATION OF THE NEW SET OF WEIGHTS
        w = K.softmax(K.dot(h, self.output_kernel) + self.bias_output
                      )  # shape (batch_size, num of particles, num of words)
        # w=keras.layers.Dense(self.num_words)(h)
        # w=keras.layers.softmax(w)
        w = w * targets  # assuming that targets are one-hot encoded.
        w = K.sum(w, axis=-1)

        # add a linear combination with a uniform sampling (soft resampling: presumed trick from the 'Particle Filter Recurrent Networks' to make everything differentiable)
        # link to the article:
        w_final = self.alpha * w + (1 - self.alpha) * K.random_uniform(
            shape=[self.batch_size, self.particles],
            minval=0,
            maxval=1 / self.particles)

        w_final = w / w_final
        w_final = w_final / K.sum(w_final, axis=-1)

        def average_state(h_states, weights):
            # TO SIMPLIFY???!!!
            mean_weights = tf.expand_dims(weights, axis=-1)
            h_mean = tf.squeeze(
                tf.matmul(h_states, mean_weights, transpose_a=True))
            sum_weights = tf.tile(
                tf.expand_dims(tf.reduce_sum(weights, axis=-1), axis=-1),
                [1, h_mean.shape[-1]])
            h_mean = h_mean / sum_weights
            return h_mean

        # COMPUTATION OF HMEAN
        h_mean = average_state(h, w_final)

        return h_mean, [h, c, w_final]
Esempio n. 15
0
def customLoss(y_true, y_pred):
    log1 = 1.5 * y_true * K.log(y_pred + 1e-9) * K.pow(1 - y_pred, 2)
    log0 = 0.5 * (1 - y_true) * K.log((1 - y_pred) + 1e-9) * K.pow(y_pred, 2)
    return (-K.sum(K.mean(log0 + log1, axis=0)))
Esempio n. 16
0
def vae_loss(x, y):
  x = K.reshape(x, shape=(batch_size, 28 * 28))
  y = K.reshape(y, shape=(batch_size, 28 * 28))
  loss = K.sum(K.square(x - y), axis=-1)
  kl_loss = -0.5 * K.sum(1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis=-1)
  return (loss + kl_loss) / 2 / 28 / 28
Esempio n. 17
0
def dice_coef(y_true, y_pred, smooth=1):
    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 + smooth) / (K.sum(y_true_f * y_true_f) +
                                           K.sum(y_pred_f * y_pred_f) + smooth)
Esempio n. 18
0
def jacard_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 (intersection + 1.0) / (K.sum(y_true_f) + K.sum(y_pred_f) -
                                   intersection + 1.0)
Esempio n. 19
0
def get_MRI_VAE_3D(input_shape=(64, 64, 64, 1),
                   latent_dim=2,
                   batch_size=32,
                   disentangle=False,
                   gamma=1):
    #TODO: add discriminator loss, see if there is improvement. Perhaps try on shapes dataset if it's easier...

    image_size, _, _, channels = input_shape
    kernel_size = 3
    filters = 16
    intermediate_dim = 128
    epochs = 10
    nlayers = 2

    # VAE model = encoder + decoder
    # build encoder model
    inputs = Input(shape=input_shape, name='encoder_input')
    x = inputs
    for i in range(nlayers):
        filters *= 2
        x = Conv3D(filters=filters,
                   kernel_size=kernel_size,
                   activation='relu',
                   strides=2,
                   padding='same')(x)

    # shape info needed to build decoder model
    shape = K.int_shape(x)

    # generate latent vector Q(z|X)
    x = Flatten()(x)
    x = Dense(intermediate_dim, activation='relu')(x)
    z_mean = Dense(latent_dim, name='z_mean')(x)
    z_log_var = Dense(latent_dim, name='z_log_var')(x)

    # use reparameterization trick to push the sampling out as input
    # note that "output_shape" isn't necessary with the TensorFlow backend
    z = Lambda(sampling, output_shape=(latent_dim, ),
               name='z')([z_mean, z_log_var])

    # instantiate encoder model
    encoder = Model(inputs, [z_mean, z_log_var, z], name='encoder')

    # build decoder model
    latent_inputs = Input(shape=(latent_dim, ), name='z_sampling')
    x = Dense(intermediate_dim, activation='relu')(latent_inputs)
    x = Dense(shape[1] * shape[2] * shape[3] * shape[4], activation='relu')(x)
    x = Reshape((shape[1], shape[2], shape[3], shape[4]))(x)

    for i in range(nlayers):
        x = Conv3DTranspose(filters=filters,
                            kernel_size=kernel_size,
                            activation='relu',
                            strides=2,
                            padding='same')(x)
        filters //= 2

    outputs = Conv3DTranspose(filters=1,
                              kernel_size=kernel_size,
                              activation='sigmoid',
                              padding='same',
                              name='decoder_output')(x)

    # instantiate decoder model
    decoder = Model(latent_inputs, outputs, name='decoder')

    #     decoder.summary()

    # instantiate VAE model
    outputs = decoder(encoder(inputs)[2])
    vae = Model(inputs, outputs, name='vae')

    if disentangle:
        discriminator = Dense(1, activation='sigmoid')

        z1 = Lambda(lambda x: x[:int(batch_size / 2), :int(latent_dim / 2)])(z)
        z2 = Lambda(lambda x: x[int(batch_size / 2):, :int(latent_dim / 2)])(z)
        s1 = Lambda(lambda x: x[:int(batch_size / 2), int(latent_dim / 2):])(z)
        s2 = Lambda(lambda x: x[int(batch_size / 2):, int(latent_dim / 2):])(z)

        q_bar = tf.keras.layers.concatenate([
            tf.keras.layers.concatenate([s1, z2], axis=1),
            tf.keras.layers.concatenate([s2, z1], axis=1)
        ],
                                            axis=0)
        q = tf.keras.layers.concatenate([
            tf.keras.layers.concatenate([s1, z1], axis=1),
            tf.keras.layers.concatenate([s2, z2], axis=1)
        ],
                                        axis=0)

        #         q_bar_score = discriminator(q_bar)
        #         q_score = discriminator(q)
        #         tc_loss = K.log(q_score / (1 - q_score))

        q_bar_score = (discriminator(q_bar) +
                       .1) * .85  # +.1 * .85 so that it's 0<x<1
        q_score = (discriminator(q) + .1) * .85
        tc_loss = K.log(q_score / (1 - q_score))

        discriminator_loss = -K.log(q_score) - K.log(1 - q_bar_score)

    reconstruction_loss = mse(K.flatten(inputs), K.flatten(outputs))
    reconstruction_loss *= image_size * image_size

    kl_loss = 1 + z_log_var - K.square(z_mean) - K.exp(z_log_var)
    kl_loss = K.sum(kl_loss, axis=-1)
    kl_loss *= -0.5
    if disentangle:
        vae_loss = K.mean(reconstruction_loss) + K.mean(
            kl_loss) + gamma * K.mean(tc_loss) + K.mean(discriminator_loss)
    else:
        vae_loss = K.mean(reconstruction_loss) + K.mean(kl_loss)

    vae.add_loss(vae_loss)
    opt = tf.keras.optimizers.Adam(learning_rate=0.001,
                                   beta_1=0.9,
                                   beta_2=0.999,
                                   epsilon=1e-07,
                                   amsgrad=False,
                                   name='Adam')

    #vae.compile(optimizer='rmsprop')
    vae.compile(optimizer=opt)

    if disentangle:
        vae.metrics_tensors = [
            reconstruction_loss, kl_loss, tc_loss, discriminator_loss
        ]
        #     vae.summary()
    return encoder, decoder, vae
Esempio n. 20
0
def sensitivity(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)))
    return true_positives / (possible_positives + K.epsilon())
Esempio n. 21
0
    def call(self, inputs, mask=None, **kwargs):
        if isinstance(inputs, list):
            inputs, positions = inputs
            positions = K.cast(positions, 'int32')
            mask = mask[1]
        else:
            positions = None

        input_len = K.shape(inputs)[1]

        if self.attention_type == SeqSelfAttention.ATTENTION_TYPE_ADD:
            e = self._call_additive_emission(inputs)
        elif self.attention_type == SeqSelfAttention.ATTENTION_TYPE_MUL:
            e = self._call_multiplicative_emission(inputs)

        if self.attention_activation is not None:
            e = self.attention_activation(e)
        e = K.exp(e - K.max(e, axis=-1, keepdims=True))
        if self.attention_width is not None:
            ones = tf.ones((input_len, input_len))
            if self.history_only:
                local = tf.linalg.band_part(
                    ones,
                    K.minimum(input_len, self.attention_width - 1),
                    0,
                )
            else:
                local = tf.linalg.band_part(
                    ones,
                    K.minimum(input_len, self.attention_width // 2),
                    K.minimum(input_len, (self.attention_width - 1) // 2),
                )
            e = e * K.expand_dims(local, 0)
        if mask is not None:
            mask = K.cast(mask, K.floatx())
            mask = K.expand_dims(mask)
            e = K.permute_dimensions(
                K.permute_dimensions(e * mask, (0, 2, 1)) * mask, (0, 2, 1))

        # a_{t} = \text{softmax}(e_t)
        s = K.sum(e, axis=-1)
        s = K.tile(K.expand_dims(s, axis=-1), K.stack([1, 1, input_len]))
        a = e / (s + K.epsilon())

        # l_t = \sum_{t'} a_{t, t'} x_{t'}
        v = K.batch_dot(a, inputs)
        if self.attention_regularizer_weight > 0.0:
            self.add_loss(self._attention_regularizer(a))

        if positions is not None:
            pos_num = K.shape(positions)[1]
            batch_indices = K.tile(
                K.expand_dims(K.arange(K.shape(inputs)[0]), axis=-1),
                K.stack([1, pos_num]))
            pos_indices = K.stack([batch_indices, positions], axis=-1)
            v = tf.gather_nd(v, pos_indices)
            a = tf.gather_nd(a, pos_indices)

        if self.return_attention:
            return [v, a]
        return v
Esempio n. 22
0
def total_error(y_true, y_pred):
    """
    I think this is simply the variance, but as a sum...
    """
    return K.sum(K.square(y_true - K.mean(y_true)))
Esempio n. 23
0
def calculate_content_loss(content_features, const_content_features):
    test_l_content = 0.5 * K.sum(
        K.square(content_features - const_content_features))

    return test_l_content
Esempio n. 24
0
def unexplained_error(y_true, y_pred):
    """
    I think this is simply the squared error, but the sum not the mean as in mse
    """
    return K.sum(K.square(y_true - y_pred))
Esempio n. 25
0
 def content_loss(base, combination):
     return K.sum(K.square(combination - base))
Esempio n. 26
0
def total_error_avgAx0(y_true, y_pred):
    """
    """
    avgY = K.mean(y_true, axis=0, keepdims=True)   # 0 is sample axis
    return K.sum(K.square(y_true - avgY))
Esempio n. 27
0
def yolo_class_loss(y_true, y_pred, t):
    error = K.square(y_true - y_pred)
    object_true = CLASS_SCALE*(error)
    object_false = K.zeros_like(y_true)
    loss = tf.where(t, object_true, object_false)
    return K.sum(loss)
Esempio n. 28
0
def euclidean_distance_loss(y_true, y_pred):
    return K.sqrt(K.sum(K.square(y_pred - y_true), axis=-1))        
def dice_coef(y_true, y_pred, smooth=1):
    intersection = K.sum(y_true * y_pred, axis=0)
    union = K.sum(y_true, axis=0) + K.sum(y_pred, axis=0)
    return K.mean((2. * intersection + smooth) / (union + smooth), axis=0)
Esempio n. 30
0
def action_augmentation_loss(y_true, y_pred):
    return K.sum(K.square(y_true - y_pred))