Esempio n. 1
0
def total_loss(boxes_gt, masks_gt, input_p, box_pred, mask_pred, rel_scores):
    y1 = K.flatten(boxes_gt)
    y2 = K.flatten(masks_gt)
    y1_pred = K.flatten(box_pred)
    y2_pred = K.flatten(mask_pred)
    input_p = K.expand_dims(input_p, axis=0)

    box_loss = losses.MSE(y1, y1_pred)
    mask_loss = losses.BinaryCrossentropy(from_logits=True)(y2, y2_pred)
    cos_sim = losses.CosineSimilarity()(boxes_gt, box_pred)

    loss_predicate = losses.categorical_crossentropy(input_p, rel_scores)

    return K.mean(box_loss * 1000 + mask_loss + cos_sim + loss_predicate)
Esempio n. 2
0
        def cosine_sim(argument):
            obj = argument[0]
            patch = argument[1]
            patch = tf.expand_dims(patch, axis=0)
            patch = tf.expand_dims(patch, axis=0)
            patch = tf.transpose(patch, [3, 0, 1, 2])

            patch = tf.image.resize(
                patch, size=[self.local_shape[0], self.local_shape[1]])

            out = tf.map_fn(
                lambda x: losses.CosineSimilarity(
                    axis=-1, reduction=losses.Reduction.NONE)(obj, x), patch)

            return out
Esempio n. 3
0
    def load_bert(word2vec_path):
        print("Tensorflow uses GPU: {0}".format(
            tf.test.is_gpu_available(cuda_only=True)))
        model = gensim.models.KeyedVectors.load_word2vec_format(word2vec_path)
        generator = read_from_file('_train.dat')
        in_docs = []
        out_docs = []
        for sample in generator:
            if sample[1] in model.wv:
                in_docs.append(sample[0])
                out_docs.append(model.wv[sample[1]])
        in_docs, out_docs = np.array(in_docs), np.array(out_docs)
        shuffle_samples(in_docs, out_docs)

        INPUT_SIZE = 768
        INITIALIZER = 'he_normal'
        REGULARIZER = regularizers.l1_l2(l1=0.01, l2=0.01)

        keras_model = Sequential()
        keras_model.add(Dense(INPUT_SIZE, input_dim=INPUT_SIZE))
        keras_model.add(Dropout(0.05))
        keras_model.add(
            Dense(INPUT_SIZE,
                  kernel_initializer=INITIALIZER,
                  kernel_regularizer=REGULARIZER,
                  activation='relu'))
        keras_model.add(Dropout(0.05))
        keras_model.add(
            Dense(INPUT_SIZE,
                  kernel_initializer=INITIALIZER,
                  kernel_regularizer=REGULARIZER,
                  activation='linear'))

        keras_model.compile('adadelta',
                            loss=losses.CosineSimilarity(axis=1),
                            metrics=['cosine_proximity'])

        callback = EarlyStopping(patience=3)
        keras_model.fit(in_docs,
                        out_docs,
                        batch_size=4000,
                        epochs=500,
                        validation_split=0.2,
                        callbacks=[callback])
        keras_model.save(model_path + ".h5")
        return (set(model.wv.vocab.keys()), model)
Esempio n. 4
0
def total_loss(boxes_gt, masks_gt, input_p, box_pred, mask_pred, rel_scores,
               loss):
    y1 = K.flatten(boxes_gt)
    y2 = K.flatten(masks_gt)
    y1_pred = K.flatten(box_pred)
    y2_pred = K.flatten(mask_pred)
    input_p = K.expand_dims(input_p, axis=0)

    if loss == 'MSE':
        box_loss = losses.MSE(y1, y1_pred)
    else:
        box_loss = losses.MAE(y1, y1_pred)
    mask_loss = losses.BinaryCrossentropy(from_logits=True)(y2, y2_pred)
    cos_sim = losses.CosineSimilarity()(boxes_gt, box_pred)

    loss_predicate = losses.categorical_crossentropy(
        input_p, K.reshape(rel_scores, input_p.shape))

    return K.mean(box_loss * 10 + 0.01 * mask_loss + 0.001 * loss_predicate)
 def call(self, img1, img2):
     return -losses.CosineSimilarity(reduction=losses.Reduction.NONE)(img1,
                                                                      img2)
Esempio n. 6
0
 def __init__(self, *args, loss=losses_module.CosineSimilarity(), **kwargs):
     super().__init__(*args, **kwargs, loss=loss)
Esempio n. 7
0
def test_compile_model_from_params():
    """Tests that if build_fn returns an un-compiled model,
    the __init__ parameters will be used to compile it
    and that if build_fn returns a compiled model
    it is not re-compiled.
    """
    # Load data
    data = fetch_california_housing()
    X, y = data.data[:100], data.target[:100]

    other_loss = losses_module.MeanAbsoluteError

    # build_fn that does not compile
    def build_fn(my_loss=None):
        model = Sequential()
        model.add(keras.layers.Dense(X.shape[1], input_shape=(X.shape[1],)))
        model.add(keras.layers.Activation("relu"))
        model.add(keras.layers.Dense(1))
        model.add(keras.layers.Activation("linear"))
        if my_loss is not None:
            model.compile(loss=my_loss)
        return model

    # Calling with loss=None (or other default)
    # and compiling within build_fn must work
    loss_obj = other_loss()
    estimator = KerasRegressor(
        model=build_fn,
        # compile_with_loss != None overrides loss
        my_loss=loss_obj,
    )
    estimator.fit(X, y)
    assert estimator.model_.loss is loss_obj

    # Passing a value for loss AND compiling with
    # the SAME loss should succeed, and the final loss
    # should be the user-supplied loss
    loss_obj = other_loss()
    estimator = KerasRegressor(
        model=build_fn,
        loss=other_loss(),
        # compile_with_loss != None overrides loss
        my_loss=loss_obj,
    )
    estimator.fit(X, y)
    assert estimator.model_.loss is loss_obj

    # Passing a non-default value for loss AND compiling with
    # a DIFFERENT loss should raise a ValueError
    loss_obj = other_loss()
    estimator = KerasRegressor(
        model=build_fn,
        loss=losses_module.CosineSimilarity(),
        # compile_with_loss != None overrides loss
        my_loss=loss_obj,
    )
    with pytest.raises(ValueError, match=" but model compiled with "):
        estimator.fit(X, y)

    # The ValueError should appear even if the default is != None
    class DefaultLossNotNone(KerasRegressor):
        def __init__(self, *args, loss=losses_module.CosineSimilarity(), **kwargs):
            super().__init__(*args, **kwargs, loss=loss)

    loss_obj = other_loss()
    estimator = DefaultLossNotNone(
        model=build_fn,
        my_loss=loss_obj,
    )
    estimator.fit(X, y)
    assert estimator.model_.loss is loss_obj

    loss_obj = other_loss()
    estimator = DefaultLossNotNone(
        model=build_fn,
        loss=losses_module.CategoricalHinge(),
        my_loss=loss_obj,
    )
    with pytest.raises(ValueError, match=" but model compiled with "):
        estimator.fit(X, y)