예제 #1
0
 def haraka_post_corrections(self, in_out):
     """
     Change any obviously wrong haraka marks according to the character and its context.
     :param in_out: input layer and prediction layers outputs.
     :return: corrected predictions.
     """
     inputs, pred_haraka, pred_shadda = in_out
     if not self.rules_enabled:
         return pred_haraka
     char_index = K.argmax(inputs[:, -1], axis=-1)
     # Force the correct haraka on some letters
     forced_diac_chars = {CHAR2INDEX['إ']: 3}
     for f_diac_char, f_diac in forced_diac_chars.items():
         mask = K.reshape(K.cast(K.not_equal(char_index, f_diac_char), 'float32'), (-1, 1))
         pred_haraka = mask * pred_haraka + (1 - mask) * K.one_hot(f_diac, K.int_shape(pred_haraka)[-1])
     # Force the correct haraka before some letters
     f_prev_diac_chars = {CHAR2INDEX['ى']: 1, CHAR2INDEX['ة']: 1}
     prev_char_index = K.argmax(inputs[:, -2], axis=-1)
     for fd_char, f_diac in f_prev_diac_chars.items():
         mask = K.cast(K.not_equal(char_index[1:], fd_char), 'float32')
         mask = K.reshape(K.concatenate([mask, K.ones((1,))], axis=0), (-1, 1))
         pred_haraka = pred_haraka * mask + (1 - mask) * K.one_hot(f_diac, K.int_shape(pred_haraka)[-1])
     # Allow only Fatha, Fathatan, or nothing before ا if it is in the end of the word
     mask = K.reshape(K.concatenate([K.clip(
         K.cast(K.not_equal(char_index[1:-1], CHAR2INDEX['ا']), 'float32') +
         K.cast(K.not_equal(char_index[2:], CHAR2INDEX[' ']), 'float32'), 0, 1), K.ones((2,))], axis=0), (-1, 1))
     pred_haraka = mask * pred_haraka + (1 - mask) * K.constant([1, 1, 0, 0, 0, 1, 0, 0], shape=(1, 8)) * pred_haraka
     # Force Fatha before ا if it is not in the end of the word
     mask = K.reshape(K.concatenate([K.clip(
         K.cast(K.not_equal(char_index[1:-1], CHAR2INDEX['ا']), 'float32') +
         K.cast(K.equal(char_index[2:], CHAR2INDEX[' ']), 'float32'), 0, 1), K.ones((2,))], axis=0), (-1, 1))
     pred_haraka = mask * pred_haraka + (1 - mask) * K.one_hot(1, K.int_shape(pred_haraka)[-1])
     # Force no sukun and tanween at the beginning of the word
     mask = K.reshape(
         K.concatenate([K.zeros((1,)), K.cast(K.not_equal(prev_char_index[1:], CHAR2INDEX[' ']), 'float32')],
                       axis=0), (-1, 1))
     pred_haraka = mask * pred_haraka + (1 - mask) * K.constant([1, 1, 1, 1, 0, 0, 0, 0], shape=(1, 8)) * pred_haraka
     # Allow tanween only at the end of the word
     mask = K.reshape(K.concatenate([K.cast(K.not_equal(char_index[1:], CHAR2INDEX[' ']), 'float32'), K.zeros((1,))],
                                    axis=0), (-1, 1))
     pred_haraka = mask * K.constant([1, 1, 1, 1, 1, 0, 0, 0], shape=(1, 8)) * pred_haraka + (1 - mask) * pred_haraka
     # Prohibit Fathatan on most letters
     mask = K.reshape(K.concatenate([K.clip(
         K.cast(K.not_equal(char_index[1:], CHAR2INDEX[' ']), 'float32') +
         K.cast(K.not_equal(char_index[:-1], CHAR2INDEX['ء']), 'float32'), 0, 1), K.ones((1,))], axis=0), (-1, 1))
     mask *= K.reshape(K.cast(K.not_equal(char_index, CHAR2INDEX['ة']), 'float32'), (-1, 1))
     mask *= K.reshape(K.concatenate([K.clip(
         K.cast(K.not_equal(char_index[1:-1], CHAR2INDEX['ا']), 'float32') +
         K.cast(K.not_equal(char_index[2:], CHAR2INDEX[' ']), 'float32'), 0, 1), K.ones((2,))], axis=0), (-1, 1))
     pred_haraka = mask * K.constant([1, 1, 1, 1, 1, 0, 1, 1], shape=(1, 8)) * pred_haraka + (1 - mask) * pred_haraka
     # Drop haraka from the forbidden characters
     forbidden_chars = [CHAR2INDEX[' '], CHAR2INDEX['0'], CHAR2INDEX['آ'], CHAR2INDEX['ى'], CHAR2INDEX['ا']]
     mask = K.cast(K.not_equal(char_index, forbidden_chars[0]), 'float32')
     for forbidden_char in forbidden_chars[1:]:
         mask *= K.cast(K.not_equal(char_index, forbidden_char), 'float32')
     mask = K.reshape(mask, (-1, 1))
     pred_haraka = mask * pred_haraka + (1 - mask) * K.one_hot(0, K.int_shape(pred_haraka)[-1])
     return pred_haraka
def dice_myo(y_true, y_pred, smooth=1e-7, num_classes=4):
    '''
    Multiclass Dice score. Ignores background pixel label 0
    Pass to model as metric during compile statement
    '''
    y_true_f = K.flatten(
        K.one_hot(K.cast(y_true, 'int32'), num_classes=4)[..., 2:3])
    y_pred_f = K.flatten(
        K.one_hot(argmax(y_pred, axis=3), num_classes=4)[..., 2:3])
    intersect = K.sum(y_true_f * y_pred_f, axis=-1)
    denom = K.sum(y_true_f + y_pred_f, axis=-1)
    return K.mean((2. * intersect / (denom + smooth)))
    def __build_train_fn(self):
        """Create a train function
        It replaces `model.fit(X, y)` because we use the output of model and use it for training.
        """
        action_prob_placeholder = self.model.model.outputs
        advantage_placeholder = K.placeholder(shape=(None, ), name="advantage")

        action_placeholder = []
        old_mu_placeholder = []
        action_prob_old = []
        loss = []
        for i in range(len(self.output_dim)):
            o_mu_pl = K.placeholder(shape=(None, ),
                                    name="old_mu_placeholder" + str(i))
            old_mu_placeholder.append(o_mu_pl)

            act_pl = K.placeholder(shape=(None, ),
                                   name="action_placeholder" + str(i),
                                   dtype='int32')
            action_placeholder.append(act_pl)

            act_prob = K.sum(K.one_hot(act_pl, self.output_dim[i]) *
                             action_prob_placeholder[i],
                             axis=1)

            act_prob_old = K.sum(K.one_hot(act_pl, self.output_dim[i]) *
                                 o_mu_pl,
                                 axis=1)
            action_prob_old.append(K.mean(-K.log(act_prob_old)))

            logp = K.log(act_prob)
            old_logp = K.log(act_prob_old)
            kl = losses.kullback_leibler_divergence(old_mu_placeholder[i],
                                                    action_prob_placeholder[i])

            l = (act_prob - act_prob_old) * advantage_placeholder - kl
            loss.append(-K.mean(l))

        entropy = K.sum(action_prob_old)
        loss = K.stack(loss)
        loss_p = K.sum(loss)

        adam = optimizers.Adam(lr=self.pi_lr)
        updates = adam.get_updates(loss=loss,
                                   params=self.model.trainable_weights)

        self.train_fn = K.function(inputs=[
            *self.model.model.inputs, *old_mu_placeholder, *action_placeholder,
            advantage_placeholder
        ],
                                   outputs=[loss_p, entropy],
                                   updates=updates)
예제 #4
0
def build_model(hidden_dim, max_seq_len, vocabulary_size):
    ## encoder Input and layers
    encoder_in = Input((max_seq_len, ), dtype='int32', name='encoder_in')
    ith_str = Input((1, ), dtype='int32', name='ith_str')
    word = Input((1, ), dtype='int32', name='word')
    OneHot = Lambda(lambda x: K.one_hot(x, vocabulary_size), name='OneHot')

    ## building encoder
    encoder_in_and_word = Concatenate()([ith_str, word, encoder_in])
    encoder_out, state = GRU(hidden_dim,
                             return_state=True)(OneHot(encoder_in_and_word))
    encoder_out_dup = RepeatVector(max_seq_len)(encoder_out)

    ## decoder Input and layers
    decoder_in = Input((max_seq_len, ), dtype='int32', name='decoder_in')
    ith = Input((1, ), dtype='int32', name='ith')
    decoder_GRU = GRU(hidden_dim, return_sequences=True, return_state=True)
    decoder_Dense = Dense(vocabulary_size,
                          activation='softmax',
                          name='decoder_out')

    ## building decoder
    ith_dup = RepeatVector(max_seq_len)(K.cast(ith, 'float'))
    word_dup = K.reshape(RepeatVector(max_seq_len)(word), (-1, max_seq_len))
    x = Concatenate()(
        [ith_dup,
         OneHot(word_dup),
         OneHot(decoder_in), encoder_out_dup])
    x, _ = decoder_GRU(x, initial_state=state)
    decoder_out = decoder_Dense(x)

    ## get the specific word
    gather = K.concatenate(
        [K.reshape(tf.range(K.shape(decoder_out)[0]), (-1, 1)), ith])
    specific_word = tf.gather_nd(decoder_out, gather)
    specific_word = Lambda(tf.identity, name='word_out')(
        specific_word
    )  # Add this layer because the name of tf.gather_nd is too ugly

    model = Model([encoder_in, decoder_in, ith, ith_str, word],
                  [decoder_out, specific_word])
    encoder_model = Model([encoder_in, ith_str, word], [encoder_out, state])

    ## building decoder model given encoder_out and states
    decoder_in_one_word = Input((1, ),
                                dtype='int32',
                                name='decoder_in_one_word')
    decoder_state_in = Input((hidden_dim, ), name='decoder_state_in')
    encoder_out = Input((hidden_dim, ), name='decoder_encoder_out')
    x = Concatenate()([
        K.cast(ith, 'float')[:, tf.newaxis],
        OneHot(word),
        OneHot(decoder_in_one_word), encoder_out[:, tf.newaxis]
    ])
    x, decoder_state = decoder_GRU(x, initial_state=decoder_state_in)
    decoder_out = decoder_Dense(x)
    decoder_model = Model(
        [decoder_in_one_word, encoder_out, decoder_state_in, ith, word],
        [decoder_out, decoder_state])
    return model, encoder_model, decoder_model
예제 #5
0
def crf_nll(y_true, y_pred):
    """The negative log-likelihood for linear chain Conditional Random Field (CRF).
    This loss function is only used when the `layers.CRF` layer
    is trained in the "join" mode.
    # Arguments
        y_true: tensor with true targets.
        y_pred: tensor with predicted targets.
    # Returns
        A scalar representing corresponding to the negative log-likelihood.
    # Raises
        TypeError: If CRF is not the last layer.
    # About GitHub
        If you open an issue or a pull request about CRF, please
        add `cc @lzfelix` to notify Luiz Felix.
    """

    crf, idx = y_pred._keras_history[:2]
    if crf._outbound_nodes:
        raise TypeError('When learn_model="join", CRF must be the last layer.')
    if crf.sparse_target:
        y_true = K.one_hot(K.cast(y_true[:, :, 0], 'int32'), crf.units)
    X = crf._inbound_nodes[idx].input_tensors
    mask = crf._inbound_nodes[idx].inbound_layers.input_mask
    nloglik = crf.get_negative_log_likelihood(y_true, X, mask)
    return nloglik
예제 #6
0
def dice_coeff(y_true, y_pred):
    smooth = 1.
    y_true_f = K.flatten(K.one_hot(K.cast(y_true, 'int32'), num_classes=11)[...,1:])
    y_pred_f = K.flatten(y_pred[...,1:])
    intersect = K.sum(y_true_f * y_pred_f, axis=-1)
    denom = K.sum(y_true_f + y_pred_f, axis=-1)
    return K.mean((2. * intersect / (denom + smooth)))
예제 #7
0
def jaccard2_macro_avg(y_true, y_pred, smooth=SMOOTH):
    y_true_ndim = K.ndim(y_true)
    y_pred_ndim = K.ndim(y_pred)

    # 3d or 2d cases
    assert (y_true_ndim == 4 and y_pred_ndim == 5) or (
        y_true_ndim == 3 and y_pred_ndim
        == 4), f"{y_true_ndim=} {y_pred_ndim=}"  # batch_idx, x, y, z (, class)

    # one hot encoding
    n_classes = y_pred.shape[-1]
    y_true = K.cast(K.one_hot(K.cast(y_true, "int32"), n_classes),
                    y_pred.dtype)

    axis = (0, 1, 2, 3) if y_pred_ndim == 5 else (
        0,
        1,
        2,
    )  # batch_idx, x, y(, z)
    intersection = K.sum(y_true * y_pred, axis=axis)
    # y_true is ohe, so y_true ** 2 is the same thing
    union = K.sum(y_true, axis=axis) + K.sum(y_pred**2,
                                             axis=axis) - intersection
    jaccards = (intersection + smooth) / (union + smooth)
    return 1. - K.mean(jaccards)
예제 #8
0
def grad_cam(input_model, image, category_index, layer_name):
    model = Sequential()
    model.add(input_model)

    nb_classes = 8

    loss = K.one_hot([category_index], nb_classes) * model.layers[0].output)
    conv_output =  [l for l in model.layers[0].layers if l.name == layer_name][0].output


    grads = normalize(K.gradients(loss, conv_output)[0])
    gradient_function = K.function([model.layers[0].input], [conv_output, grads])

    output, grads_val = gradient_function([image])
    output, grads_val = output[0, :], grads_val[0, :, :, :]

    weights = np.mean(grads_val, axis = (0, 1))
    cam = np.ones(output.shape[0 : 2], dtype = np.float32)

    for i, w in enumerate(weights):
        cam += w * output[:, :, i]

    cam = cv2.resize(cam, (112, 112))
    cam = np.maximum(cam, 0)
    heatmap = cam / np.max(cam)

    #Return to BGR [0..255] from the preprocessed image
    image = image[0, :]
    image -= np.min(image)
    image = np.minimum(image, 255)

    cam = cv2.applyColorMap(np.uint8(255*heatmap), cv2.COLORMAP_JET)
    cam = np.float32(cam) + np.float32(image)
    cam = 255 * cam / np.max(cam)
    return np.uint8(cam), heatmap
예제 #9
0
def to_one_hot(x):
    x, x_mask = x
    x = K.cast(x, 'int32')  # cast相当于转换数据类型
    x = K.one_hot(x, len(chars) + 4)  # 在原有的基础上扩展一维
    x = K.sum(x_mask * x, 1, keepdims=True)
    x = K.cast(K.greater(x, 0.5), 'float32')
    return x
예제 #10
0
    def call(self, x, **kwargs):
        assert isinstance(x, list)
        inp_a, inp_b = x

        outp_a = K.l2_normalize(inp_a, -1)
        outp_b = K.l2_normalize(inp_b, -1)
        alpha = K.batch_dot(outp_b, outp_a, axes=[2, 2])
        alpha = K.l2_normalize(alpha, 1)
        alpha = K.one_hot(K.argmax(alpha, 1), K.int_shape(inp_a)[1])
        hmax = K.batch_dot(alpha, outp_b, axes=[1, 1])
        kcon = K.eye(K.int_shape(inp_a)[1], dtype='float32')

        m = []
        for i in range(self.output_dim):
            outp_a = inp_a * self.W[i]
            outp_hmax = hmax * self.W[i]
            outp_a = K.l2_normalize(outp_a, -1)
            outp_hmax = K.l2_normalize(outp_hmax, -1)
            outp = K.batch_dot(outp_hmax, outp_a, axes=[2, 2])
            outp = K.sum(outp * kcon, -1, keepdims=True)
            m.append(outp)
        if self.output_dim > 1:
            persp = K.concatenate(m, 2)
        else:
            persp = m[0]
        return [persp, persp]
예제 #11
0
def symmetric_cross_entropy(y_actual, y_pred, A=-6, alpha=0.1, beta=1):
    '''Define the symmetric cross entropy that will be used for training '''
    q = K.one_hot(K.cast(y_actual, 'uint8'), 10)  # 200 or 10
    custom_loss = -alpha * K.mean(
        K.batch_dot(q, K.maximum(K.log(y_pred + 1e-15), A))) - beta * K.mean(
            K.batch_dot(K.maximum(K.log(q + 1e-15), A), y_pred))
    return custom_loss
예제 #12
0
    def Mask(self, inputs, seq_len, mode="add"):
        """Mask operation used in multi-head self attention

        Args:
            seq_len (obj): sequence length of inputs.
            mode (str): mode of mask.
        
        Returns:
            obj: tensors after masking.
        """

        if seq_len == None:
            return inputs
        else:
            mask = K.one_hot(indices=seq_len[:, 0],
                             num_classes=K.shape(inputs)[1])
            mask = 1 - K.cumsum(mask, axis=1)

            for _ in range(len(inputs.shape) - 2):
                mask = K.expand_dims(mask, 2)

            if mode == "mul":
                return inputs * mask
            elif mode == "add":
                return inputs - (1 - mask) * 1e12
예제 #13
0
def get_image(image_path,
              img_height=240,
              img_width=240,
              mask=False,
              flip=0,
              flip2=0):
    img = tf.io.read_file(image_path)

    if not mask:
        img = tf.cast(tf.image.decode_png(img, channels=4), dtype=tf.float32)
        img = tf.image.resize(images=img, size=[img_height, img_width]) / 255
        img_shape = tf.shape(img)
        print(img_shape)
        img = tf.case(
            [(tf.greater(flip, 0), lambda: tf.image.flip_left_right(img))],
            default=lambda: img)
        img = tf.case(
            [(tf.greater(flip2, 0), lambda: tf.image.flip_up_down(img))],
            default=lambda: img)
    else:
        img = tf.image.decode_png(img, channels=1)
        img = tf.cast(tf.image.resize(images=img, size=[img_height,
                                                        img_width]),
                      dtype=tf.uint8)
        img = tf.case(
            [(tf.greater(flip, 0), lambda: tf.image.flip_left_right(img))],
            default=lambda: img)
        img = tf.case(
            [(tf.greater(flip2, 0), lambda: tf.image.flip_up_down(img))],
            default=lambda: img)
        img = K.squeeze(img, axis=-1)
        img = K.one_hot(tf.cast(img, tf.int32), num_classes)
    return img
예제 #14
0
def WNet(feature_maps=None, nb_classes=6, dropout=0.65, model_name_suffix=""):
    if feature_maps is None:
        feature_maps = [64, 128, 256, 512, 1024]

    encoder_input, encoder_output = UNet(input_size=(256, 256, 3),
                                         feature_maps=feature_maps,
                                         nb_classes=nb_classes,
                                         dropout=dropout,
                                         conv_padding="same",
                                         build_model=False)
    encoder_output = K.one_hot(K.argmax(encoder_output), nb_classes)
    _, decoder_output = UNet(input_layer=encoder_output,
                             nb_classes=3,
                             output_layer_activation="sigmoid",
                             dropout=dropout,
                             feature_maps=feature_maps,
                             conv_padding="same",
                             build_model=False)

    nb_conv_layers = 6 + 10 * (len(feature_maps) - 1)
    dropout_suffix = "D" if dropout > 0.0 else ""
    model_name = f"WNet-{nb_conv_layers}{dropout_suffix}-{nb_classes}{model_name_suffix}"
    full_model = Model(name=model_name,
                       inputs=encoder_input,
                       outputs=decoder_output)
    encoder_model = Model(name=f"{model_name}-Encoder",
                          inputs=encoder_input,
                          outputs=encoder_output)
    return full_model, encoder_model
예제 #15
0
def path_energy0(y, x, U, mask=None):
    '''Path energy without boundary potential handling.'''
    n_classes = K.shape(x)[2]
    y_one_hot = K.one_hot(y, n_classes)

    # Tag path energy
    energy = K.sum(x * y_one_hot, 2)
    energy = K.sum(energy, 1)

    # Transition energy
    y_t = y[:, :-1]
    y_tp1 = y[:, 1:]
    U_flat = K.reshape(U, [-1])
    # Convert 2-dim indices (y_t, y_tp1) of U to 1-dim indices of U_flat:
    flat_indices = y_t * n_classes + y_tp1
    U_y_t_tp1 = K.gather(U_flat, flat_indices)

    if mask is not None:
        mask = K.cast(mask, K.floatx())
        y_t_mask = mask[:, :-1]
        y_tp1_mask = mask[:, 1:]
        U_y_t_tp1 *= y_t_mask * y_tp1_mask

    energy += K.sum(U_y_t_tp1, axis=1)

    return energy
예제 #16
0
def _label_to_one_hot(tens, nb_labels):
    """
    Transform a label nD Tensor to a one-hot 3D Tensor. The input tensor is first
    batch-flattened, and then each batch and each voxel gets a one-hot representation
    """
    y = K.batch_flatten(tens)
    return K.one_hot(y, nb_labels)
예제 #17
0
    def update_state(self, y_true, y_pred, sample_weight=None):
        # if log_metrics:
        #     wandb_log_report(report)
        # skip to count samples with label __unknown__
        mask = K.cast(K.not_equal(y_true, 1), K.floatx())
        if self.threshold is None:
            threshold = tf.reduce_max(y_pred, axis=-1, keepdims=True)
            # make sure [0, 0, 0] doesn't become [1, 1, 1]
            # Use abs(x) > eps, instead of x != 0 to check for zero
            y_pred = tf.logical_and(y_pred >= threshold,
                                    tf.abs(y_pred) > 1e-12)
        else:
            y_pred = y_pred > self.threshold
        y_true = K.one_hot(K.cast(K.flatten(y_true), tf.int32),
                           y_pred.shape[1])
        y_true = tf.cast(y_true, self.dtype)
        y_pred = tf.cast(y_pred, self.dtype)

        # # skip counting samples where the PAD token is predicted

        def _weighted_sum(val, sample_weight):
            if sample_weight is not None:
                val = tf.math.multiply(val, tf.expand_dims(sample_weight, 1))
            return tf.reduce_sum(val * mask, axis=self.axis)[2:]

        self.true_positives.assign_add(
            _weighted_sum(y_pred * y_true, sample_weight))
        self.false_positives.assign_add(
            _weighted_sum(y_pred * (1 - y_true), sample_weight))
        self.false_negatives.assign_add(
            _weighted_sum((1 - y_pred) * y_true, sample_weight))
        self.weights_intermediate.assign_add(
            _weighted_sum(y_true, sample_weight))
 def loss(y_true, y_pred):
     pred_labels = K.one_hot(K.argmax(y_pred, 1),
                             num_classes=K.shape(y_true)[1])
     y_new = alpha * y_true + (1. - alpha) * pred_labels
     y_pred /= K.sum(y_pred, axis=-1, keepdims=True)
     y_pred = K.clip(y_pred, K.epsilon(), 1.0 - K.epsilon())
     return -K.sum(y_new * K.log(y_pred), axis=-1)
예제 #19
0
    def _train(self, screens_input, action_input, select_input, reward, action,
               screen_action, screen_used):
        _entropy = _policy_loss = _value_loss = 0.

        with tf.GradientTape() as tape:
            spatial_policy, ns_policy, value = self.model(
                [screens_input, action_input, select_input])
            value = K.squeeze(value, axis=1)

            ns_action_one_hot = K.one_hot(action, len(ACTION_OPTIONS))
            screen_action_one_hot = K.one_hot(screen_action,
                                              SCREEN_SIZE * SCREEN_SIZE)

            value_loss = .5 * K.square(reward - value)

            entropy = -K.sum(ns_policy * K.log(ns_policy + 1e-10), axis=1) - \
                       K.sum(spatial_policy * K.log(spatial_policy + 1e-10), axis=1)
            ns_log_prob = K.log(
                K.sum(ns_policy * ns_action_one_hot, axis=1) + 1e-10)
            spatial_log_prob = K.log(
                K.sum(spatial_policy * screen_action_one_hot, axis=1) + 1e-10)
            advantage = reward - K.stop_gradient(value)

            # Mask out spatial_log_prob when the action taken did not use the screen
            policy_loss = -(ns_log_prob + spatial_log_prob *
                            screen_used) * advantage - entropy * ENTROPY_RATE

            total_loss = policy_loss + value_loss

            _entropy = K.mean(entropy)
            _policy_loss = K.mean(K.abs(policy_loss))
            _value_loss = K.mean(value_loss)

        gradients = tape.gradient(total_loss, self.model.trainable_variables)
        global_norm = tf.linalg.global_norm(gradients)
        print(tf.linalg.global_norm(gradients))
        gradients, _ = tf.clip_by_global_norm(
            gradients,
            GRADIENT_CLIP_MAX)  # Prevents exploding gradients...I think
        self.opt.apply_gradients(zip(gradients,
                                     self.model.trainable_variables))

        return [
            float(_value_loss),
            float(_policy_loss),
            float(_entropy), global_norm
        ]
예제 #20
0
 def call(self, inputs, **kwargs):
     if type(inputs) is list:  
         inputs, mask = inputs
     else: 
         x = K.sqrt(K.sum(K.square(inputs), -1))
         mask = K.one_hot(indices=K.argmax(x, 1), num_classes=x.get_shape().as_list()[1])
     masked = K.batch_flatten(inputs * K.expand_dims(mask, -1))
     return masked
예제 #21
0
def expand_binary(x):
    # remove last dim
    x = backend.squeeze(x, axis=-1)
    # scale to 0 or 1
    x = backend.round(x)
    x = backend.cast(x, 'int32')
    x = backend.one_hot(x, 2)
    return x
예제 #22
0
 def bi_tempered_loss(y_true, y_pred):
     y_true = K.cast(K.reshape(y_true, (-1, )), "int64")
     labels = K.one_hot(y_true, N_CLASSES)
     return bi_tempered_logistic_loss(y_pred,
                                      labels,
                                      T1,
                                      T2,
                                      label_smoothing=0.2)
예제 #23
0
def generalized_dice_loss(y_true, y_pred, smooth=1e-7, num_classes=4):

    y_true_f = K.flatten(
        K.one_hot(K.cast(y_true, 'int32'), num_classes=4)[..., 1:])
    y_pred_f = K.flatten(y_pred[..., 1:])
    intersect = K.sum(y_true_f * y_pred_f, axis=-1)
    denom = K.sum(y_true_f + y_pred_f, axis=-1)
    return 1.0 - K.mean((2. * intersect / (denom + smooth)))
예제 #24
0
def ground_truth_generator(dataset, ANCHORS, IMAGE_W, IMAGE_H, GRID_W, GRID_H,
                           CLASS):
    '''
    Ground truth batch generator from a yolo dataset, ready to compare with YOLO prediction in loss function.

    Parameters
    ----------
    - YOLO dataset. Generate batch:
        batch : tupple(images, annotations)
        batch[0] : images : tensor (shape : batch_size, IMAGE_W, IMAGE_H, 3)
        batch[1] : annotations : tensor (shape : batch_size, max annot, 5)

    Returns
    -------
    - imgs : images to predict. tensor (shape : batch_size, IMAGE_H, IMAGE_W, 3)
    - detector_mask : tensor, shape (batch size, GRID_W, GRID_H, anchors_count, 1)
        1 if bounding box detected by grid cell, else 0
    - matching_true_boxes : tensor, shape (batch_size, GRID_W, GRID_H, anchors_count, 5)
        Contains adjusted coords of bounding box in YOLO format
    - class_one_hot : tensor, shape (batch_size, GRID_W, GRID_H, anchors_count, class_count)
        One hot representation of bounding box label
    - true_boxes_grid : annotations : tensor (shape : batch_size, max annot, 5)
        true_boxes format : x, y, w, h, c, coords unit : grid cell
    '''
    for batch in dataset:
        # imgs
        imgs = batch[0]

        # true boxes
        true_boxes = batch[1]

        # matching_true_boxes and detector_mask
        batch_matching_true_boxes = []
        batch_detector_mask = []
        batch_true_boxes_grid = []

        for i in range(true_boxes.shape[0]):  # for each image in batch
            one_matching_true_boxes, one_detector_mask, true_boxes_grid = process_true_boxes(
                true_boxes[i], ANCHORS, IMAGE_W, IMAGE_H, GRID_W, GRID_H)
            batch_matching_true_boxes.append(one_matching_true_boxes)
            batch_detector_mask.append(one_detector_mask)
            batch_true_boxes_grid.append(true_boxes_grid)

        detector_mask = tf.convert_to_tensor(np.array(batch_detector_mask),
                                             dtype='float32')
        matching_true_boxes = tf.convert_to_tensor(
            np.array(batch_matching_true_boxes), dtype='float32')
        true_boxes_grid = tf.convert_to_tensor(np.array(batch_true_boxes_grid),
                                               dtype='float32')

        # class one_hot
        matching_classes = K.cast(matching_true_boxes[..., 4], 'int32')
        class_one_hot = K.one_hot(matching_classes, CLASS + 1)[:, :, :, :, 1:]
        class_one_hot = tf.cast(class_one_hot, dtype='float32')

        batch = (imgs, detector_mask, matching_true_boxes, class_one_hot,
                 true_boxes_grid)
        yield batch
예제 #25
0
	def sparse_loss(self, y_true, y_pred):
		"""y_true需要是整数形式(非one hot)
		"""
		# y_true需要重新明确一下shape和dtype
		y_true = K.reshape(y_true, K.shape(y_pred)[:-1])
		y_true = K.cast(y_true, 'int32')
		# 转为one hot
		y_true = K.one_hot(y_true, K.shape(self.trans)[0])
		return self.dense_loss(y_true, y_pred)
예제 #26
0
def dice_loss_3d(y_true, y_pred):
    smooth = 1.
    y_pred = K.reshape(y_pred, (-1, K.int_shape(y_pred)[-1]))
    softmax = tf.nn.softmax(y_pred)
    y_true = K.one_hot(tf.to_int32(K.flatten(y_true)), K.int_shape(y_pred)[-1])
    intersection = K.sum(y_true * softmax, axis=0)
    return 1 - K.mean(
        (2. * intersection + smooth) /
        (K.sum(y_true, axis=0) + K.sum(softmax, axis=0) + smooth))
예제 #27
0
 def call(self, inputs):
     encoder_output = K.one_hot(inputs['primary'], self._n_symbols)
     try:
         encoder_output = K.concatenate(
             (encoder_output, inputs['evolutionary']))
     except KeyError:
         raise TypeError("Evolutionary inputs not available for this task.")
     inputs['encoder_output'] = encoder_output
     return inputs
예제 #28
0
파일: A2C.py 프로젝트: jfacey16/A2C
 def _build_gradient_func(self, action):
     """
     Make a function which returns gradients of the `action`th component of the output
     of the actor model, with respect to the input and weights of the actor model
     """
     predicted_action_mass = K.dot(K.one_hot([action],self.action_size), # select output component
                                   K.transpose(K.log(self.actor.output)))
     grads = K.gradients([predicted_action_mass], self._get_actor_weights())
     return K.function(self.actor.inputs, grads)
예제 #29
0
    def call(self, inputs, mask=None, **kwargs):

        logits, sequence_lengths = inputs
        sequence_lengths = K.flatten(sequence_lengths)
        tags_seq, tags_score = tf.contrib.crf.crf_decode(
            logits, self.transitions, sequence_lengths)
        tags_seq = tf.identity(tags_seq, name='logits')
        outputs = K.one_hot(tags_seq, self.num_tags)
        return K.in_train_phase(logits, outputs)
예제 #30
0
 def kronecker_product(args):
     S, A = args
     if K.ndim(S) == 1 and K.dtype(S).startswith('int'):
         S = K.one_hot(S, self.env.observation_space.n)
     elif K.ndim(S) > 2:
         S = keras.layers.Flatten()(S)
     check_tensor(S, ndim=2, dtype=('float32', 'float64'))
     check_tensor(A, ndim=2, dtype=('float32', 'float64'))
     return tf.einsum('ij,ik->ijk', S, A)