Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python VGG19的Keras自定义损失_Python_Tensorflow_Keras_Vgg Net - Fatal编程技术网

Python VGG19的Keras自定义损失

Python VGG19的Keras自定义损失,python,tensorflow,keras,vgg-net,Python,Tensorflow,Keras,Vgg Net,我想使用自定义损耗函数,该函数在Keras中使用VGG19网络 # imports from keras.applications import VGG19 from keras.layers import Input, UpSampling2D, Conv2D, Lambda from keras.models import Model import keras.backend as K 我使用以下方法构建vgg模型: def build_vgg_loss(): img = Inpu

我想使用自定义损耗函数,该函数在Keras中使用VGG19网络

# imports
from keras.applications import VGG19
from keras.layers import Input, UpSampling2D, Conv2D, Lambda
from keras.models import Model
import keras.backend as K
我使用以下方法构建vgg模型:

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
我使用以下方法构建损失函数:

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
编译虚拟模型

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),
)
然而,当我用这个自定义丢失编译我的模型时,我得到了以下回溯,我不知道出了什么问题

~/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: 
我的猜测是VGG模型的实例化不正确。如果我将
include_top
参数更改为
True
,则会得到


ValueError:输入到“展平”的形状没有完全定义(get(None,None,512)。请确保将完整的“input\u shape”或“batch\u input\u shape”参数传递到模型的第一层。

vgg\u loss
模型实例化更改为以下内容:

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