简体   繁体   中英

Keras custom loss from VGG19

I would like to use a custom loss function which uses VGG19 network in Keras.

# imports
from keras.applications import VGG19
from keras.layers import Input, UpSampling2D, Conv2D, Lambda
from keras.models import Model
import keras.backend as K

I build the vgg model using:

def build_vgg_loss():
    img = Input(shape=(300, 300, 3))
    vgg = VGG19(weights="imagenet", include_top=False)
    vgg.outputs = [
        # STOPS WORKING IF INCLUDING ALL, COMPILES WITH JUST 1 OUTPUT!!!
        vgg.get_layer('block2]1_conv1').output
        #         vgg.get_layer('block2_conv1').output,
        #         vgg.get_layer('block3_conv1').output
    ]
    model = Model(inputs=img, outputs=vgg(img))
    return model

I build loss function using:

def build_loss_fn():

    vgg_model = build_vgg_loss()

    def loss(Ics, Is):

        loss = \
            K.sqrt(
                K.sum(
                    K.square(
                        vgg_model(Is)[0] - vgg_model(Ics)[0]
                    ), axis=-1
                )
            )

        return loss

    def full_loss(y_true, y_pred):
        Ics = K.expand_dims(y_pred[0], 0)
        Is = K.expand_dims(y_pred[1], 0)

        return loss(Ics, Is)

    return full_loss

Compile the dummy model

input1 = Input(shape=(300,300,3))
input2 = Input(shape=(300,300,3))

dummy_model = Model(inputs=[input1, input2], outputs=[input1, input2])
dummy_model.compile(
            loss=build_loss_fn(),
            optimizer=Adam(0.0001, 0.9),
)

However, when I compile my model with this custom loss, I get the following traceback, and I have no clue what is going wrong.

~/script.py in loss(Ics, Is)
    107                     K.sum(
    108                         K.square(
--> 109                             vgg_model(Is)[0] - vgg_model(Ics)[0]
    110                         ), axis=-1
    111                     )

/anaconda3/envs/ml36/lib/python3.6/site-packages/keras/engine/base_layer.py in __call__(self, inputs, **kwargs)
    472             if all([s is not None
    473                     for s in to_list(input_shape)]):
--> 474                 output_shape = self.compute_output_shape(input_shape)
    475             else:
    476                 if isinstance(input_shape, list):

/anaconda3/envs/ml36/lib/python3.6/site-packages/keras/engine/network.py in compute_output_shape(self, input_shape)
    655 
    656             for i, key in enumerate(output_shape_keys):
--> 657                 assert key in layers_to_output_shapes
    658                 output_shapes.append(layers_to_output_shapes[key])
    659             # Store in cache.

AssertionError: 

My guess is that VGG model is instantiated incorrectly. If I change include_top argument to True then I get the error that

ValueError: The shape of the input to "Flatten" is not fully defined (got (None, None, 512). Make sure to pass a complete "input_shape" or "batch_input_shape" argument to the first layer in your model.

Change vgg_loss model instantiation to the following:

def build_vgg_loss(self):
    vgg = VGG19(weights="imagenet", include_top=False, input_shape=(h,w,c))
    vgg.outputs = [
        vgg.get_layer('block1_conv1').output
    ]

    model = Model(inputs=vgg.inputs, outputs=vgg.outputs)
    model.trainable = False

    return model

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM