Неверный заполнитель в тензорном потоке - PullRequest
0 голосов
/ 02 августа 2020

Я пытаюсь написать собственную функцию потерь следующим образом.

def vgg16_feature_model(flayers, weights='imagenet'):
    """
    Feature exctraction VGG16 model.

    # Arguments
        flayers: list of strings with names of layers to get the features for.
            The length of `flayers` should be > 1, otherwise the output shape
            is one axis less.
        weights: ether "imagenet" or path to the file with weights.
    # Returns
        features_model: keras.models.Model instance to extract the features.

    # Raises
        AssertionError: in case of `flayers` is not a list.
        AssertionError: in case of length of 'flayers' < 2.
    """

    assert isinstance(flayers,list), "First argument 'flayers' must be a list"
    assert len(flayers) > 1, "Length of 'flayers' must be > 1."

    base_model = VGG16(include_top=False, weights=weights)

    vgg16_outputs = [base_model.get_layer(flayers[i]).output for i in range(len(flayers))]

    features_model = Model(inputs=[base_model.input], outputs=vgg16_outputs, name='vgg16_features')
    features_model.trainable = False
    features_model.compile(loss='mse', optimizer='adam')

    return features_model


# Losses:
# -------

def total_loss(mask, vgg16_weights='imagenet'):
    """
    Total loss defined in Eq 7 of Liu et al 2018 with:
    y_true = I_gt,
    y_pred = I_out,
    y_comp = I_comp.
    """
    vgg16_lnames = ['block1_pool', 'block2_pool', 'block3_pool']
    vgg_model = vgg16_feature_model(vgg16_lnames, weights=vgg16_weights)
    def loss(y_true, y_pred):
        mask_inv = 1 - mask
        y_comp   = mask * y_true + mask_inv * y_pred
        print("y_pred", y_pred)
        print(y_comp)
        input()
        vgg_out  = vgg_model(y_pred)
        vgg_gt   = vgg_model(y_true)
        print("abc-----------------------------------")
        vgg_comp = vgg_model(y_comp)
        print("abc")
        l_valid = loss_per_pixel(y_true, y_pred, mask)
        l_hole  = loss_per_pixel(y_true, y_pred, mask_inv)
        l_perc  = loss_perc(vgg_out, vgg_gt, vgg_comp)
        l_style = loss_style(vgg_out, vgg_gt, vgg_comp)
        l_tv    = loss_tv(y_comp, mask_inv)

        return l_valid + 6.*l_hole + 0.05*l_perc + 120.*l_style + 0.1*l_tv

    return loss

Я получаю сообщение об ошибке

Traceback (most recent call last):
  File "inpainter_main.py", line 46, in <module>
    model = pconv_model(lr=LR_STAGE1, image_size=IMAGE_SIZE, vgg16_weights=VGG16_WEIGHTS)
  File "/home/bitsy-chuck/Downloads/PConv2D-2ndimp/inpainter_utils/pconv2d_model.py", line 118, in pconv_model
    model.compile(Adam(lr=lr), loss=total_loss(mask_input, vgg16_weights=vgg16_weights))
  File "/home/bitsy-chuck/anaconda3/lib/python3.7/site-packages/tensorflow/python/training/tracking/base.py", line 456, in _method_wrapper
    result = method(self, *args, **kwargs)
  File "/home/bitsy-chuck/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/training_v1.py", line 446, in compile
    self._compile_weights_loss_and_weighted_metrics()
  File "/home/bitsy-chuck/anaconda3/lib/python3.7/site-packages/tensorflow/python/training/tracking/base.py", line 456, in _method_wrapper
    result = method(self, *args, **kwargs)
  File "/home/bitsy-chuck/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/training_v1.py", line 1515, in _compile_weights_loss_and_weighted_metrics
    self.total_loss = self._prepare_total_loss(masks)
  File "/home/bitsy-chuck/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/training_v1.py", line 1575, in _prepare_total_loss
    per_sample_losses = loss_fn.call(y_true, y_pred)
  File "/home/bitsy-chuck/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/losses.py", line 246, in call
    return self.fn(y_true, y_pred, **self._fn_kwargs)
  File "/home/bitsy-chuck/Downloads/PConv2D-2ndimp/inpainter_utils/pconv2d_loss.py", line 58, in loss
    vgg_comp = vgg_model(y_comp)
  File "/home/bitsy-chuck/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer_v1.py", line 737, in __call__
    base_layer_utils.create_keras_history(inputs)
  File "/home/bitsy-chuck/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer_utils.py", line 186, in create_keras_history
    _, created_layers = _create_keras_history_helper(tensors, set(), [])
  File "/home/bitsy-chuck/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer_utils.py", line 249, in _create_keras_history_helper
    layer_inputs, processed_ops, created_layers)
  File "/home/bitsy-chuck/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer_utils.py", line 246, in _create_keras_history_helper
    constants[i] = backend.function([], op_input)([])
  File "/home/bitsy-chuck/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/backend.py", line 3632, in __call__
    run_metadata=self.run_metadata)
  File "/home/bitsy-chuck/anaconda3/lib/python3.7/site-packages/tensorflow/python/client/session.py", line 1472, in __call__
    run_metadata_ptr)
tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a value for placeholder tensor 'pconv2d_dec_16_target' with dtype float and shape [?,?,?,?]
     [[{{node pconv2d_dec_16_target}}]]

Сначала я подумал, что y_comp неверно, но

y_pred ---> Tensor("pconv2d_dec_16/BiasAdd:0", shape=(None, 512, 512, 3), dtype=float32)
y_comp ---> Tensor("loss_1/pconv2d_dec_16_loss/add:0", shape=(None, 512, 512, 3), dtype=float32)

Они оба кажутся мне одинаковыми, и, по моему мнению, это должно работать. ошибка находится в строке vgg_comp = vgg_model(y_comp)

Кто-нибудь может также объяснить, почему я получаю ошибку заполнителя?

Tf version 1.3 keras 2.2.4

...