簡體   English   中英

加載具有自定義圖層的模型時,Keras中的形狀不兼容

[英]Incompatible shapes in Keras when loading a model with custom layer

我正在嘗試在Keras中實現一個Subpixel upconvolution層。 我可以毫無問題地訓練模型並保存它。 但是我無法加載那個模型。 我總是得到尺寸錯誤的錯誤。

它的唯一工作方式是保存權重,創建新模型,然后加載權重。 但是,這並不理想,因為優化器會重置,因此很難恢復訓練。

import keras
import numpy as np
import tensorflow as tf

class Subpixel(keras.layers.Conv2D):

    def __init__(self,
                 filters,
                 kernel_size,
                 scale,
                 padding='valid',
                 data_format='channels_last',
                 strides=(1, 1),
                 activation=None,
                 use_bias=True,
                 kernel_initializer='he_normal',
                 bias_initializer='zeros',
                 kernel_regularizer=None,
                 bias_regularizer=None,
                 activity_regularizer=None,
                 kernel_constraint=None,
                 bias_constraint=None,
                 **kwargs):
        super().__init__(
            filters=scale * scale * filters,
            kernel_size=kernel_size,
            strides=strides,
            padding=padding,
            data_format=data_format,
            activation=activation,
            use_bias=use_bias,
            kernel_initializer=kernel_initializer,
            bias_initializer=bias_initializer,
            kernel_regularizer=kernel_regularizer,
            bias_regularizer=bias_regularizer,
            activity_regularizer=activity_regularizer,
            kernel_constraint=kernel_constraint,
            bias_constraint=bias_constraint,
            **kwargs)
        self.scale = scale
        self.data_format = data_format

    def call(self, inputs):
        return tf.depth_to_space(super().call(inputs), self.scale)

    def compute_output_shape(self, input_shape):
        if self.data_format == 'channels_first':
            b, k, r, c = super().compute_output_shape(input_shape)
            return b, k // (self.scale ** 2), r * self.scale, c * self.scale
        else:
            b, r, c, k = super().compute_output_shape(input_shape)
            return b, r * self.scale, c * self.scale, k // (self.scale ** 2)

    def get_config(self):
        config = super(keras.layers.Conv2D, self).get_config()
        config['filters'] = int(config['filters'] / self.scale * self.scale)
        config['scale'] = self.scale
        return config

X = np.random.rand(100, 2, 2, 1)
y = np.random.rand(100, 4, 4, 1)

inputs = keras.layers.Input(shape=(2, 2, 1))
x = Subpixel(4, 4, 2, padding='same')(inputs)
output = keras.layers.Dense(1, activation='sigmoid')(x)
model = keras.models.Model(inputs, output)
model.compile(optimizer='sgd',
                          loss='mean_absolute_error',
                          metrics=[])

model.fit(X, y)
model.save('foo.h5')
foo = keras.models.load_model('foo.h5', custom_objects={'Subpixel': Subpixel})

似乎沖突是在權重文件中的形狀和加載的體系結構之間。內核形狀在加載的模型上是不正確的。 當它應該是4,4,1,16時,它是4,4,1,64。 輸出如下:

self = TensorShape([Dimension(4), Dimension(4), Dimension(1), Dimension(64)])
other = TensorShape([Dimension(4), Dimension(4), Dimension(1), Dimension(16)])

    def assert_is_compatible_with(self, other):
      """Raises exception if `self` and `other` do not represent the same shape.

      This method can be used to assert that there exists a shape that both
      `self` and `other` represent.

      Args:
        other: Another TensorShape.

      Raises:
        ValueError: If `self` and `other` do not represent the same shape.
      """
      if not self.is_compatible_with(other):
>       raise ValueError("Shapes %s and %s are incompatible" % (self, other))
E       ValueError: Shapes (4, 4, 1, 64) and (4, 4, 1, 16) are incompatible

非常愚蠢的錯誤。 這條線:

config['filters'] = int(config['filters'] / self.scale * self.scale)

應該:

config['filters'] = int(config['filters'] / (self.scale * self.scale))

否則,在序列化圖層時,會保存過濾器的錯誤輸入參數。 基本上我被運算符優先級混淆了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM