关键字'interpolation'的UpSampling2D抛出错误

时间:2019-02-27 10:02:56

标签: tensorflow keras

当我手动将interpolate关键字设置为bilinear时,尝试使用上采样层创建网络时出现奇怪的错误。 如果我不考虑它,则使用默认的“最近邻居”;它工作正常。 有人知道怎么回事吗?

型号代码。在图层“ up1”上引发了错误

def build_model(self):

    chnl4_input = Input(shape=(368, 256, 4))
    chnl3_input = Input(shape=(736, 512, 3))

    conv1 = Conv2D(26, self.kernel_size, activation='relu', padding='same')(chnl4_input)
    conv2 = Conv2D(26, self.kernel_size, strides=(2, 2), activation='relu', padding='same')(conv1)

    conv5 = Conv2D(64, self.kernel_size, activation='relu', padding='same')(conv2)
    conv6 = Conv2D(64, self.kernel_size, activation='relu', padding='same')(conv5)

    up1 = concatenate([UpSampling2D(size=(2, 2), interpolation='bilinear')(conv6), conv1], axis=-1)
    conv7 = Conv2D(64, self.kernel_size, activation='relu', padding='same')(up1)

    conv8 = Conv2D(64, self.kernel_size, activation='relu', padding='same')(conv7)
    conv9 = Conv2D(64, self.kernel_size, activation='relu', padding='same')(conv8)

    conv11 = Conv2D(64, self.kernel_size, activation='relu', padding='same')(conv9)
    conv12 = Conv2D(64, self.kernel_size, activation='relu', padding='same')(conv11)

    up3 = concatenate([UpSampling2D(size=(2, 2), interpolation='bilinear')(conv12), chnl3_input], axis=-1)
    conv13 = Conv2D(67, self.kernel_size, activation='relu', padding='same')(up3)

    conv14 = Conv2D(67, self.kernel_size, activation='relu', padding='same')(conv13)
    conv15 = Conv2D(32, self.kernel_size, activation='relu', padding='same')(conv14)
    conv16 = Conv2D(3, self.kernel_size, activation='relu', padding='same')(conv15)

    out = conv16

    self.model = Model(inputs=[chnl4_input, chnl3_input], outputs=[out])

    self.model.compile(optimizer=self.optimizer_func, loss=self.loss_func)
    self.model.name = 'UNET'

    return self.modele here

错误:TypeError :(“关键字参数无法理解:”,“插值”)

 ~/MastersWork/Fergal/Scripts/models.py in build_model(self)
     29         conv6 = Conv2D(64, self.kernel_size, activation='relu', padding='same')(conv5)
     30 
---> 31         up1 = concatenate([UpSampling2D(size=(2, 2), interpolation='bilinear')(conv6), conv1], axis=-1)
     32         conv7 = Conv2D(64, self.kernel_size, activation='relu', padding='same')(up1)
     33 

~/anaconda3/envs/rhys_tensorflow/lib/python3.6/site-packages/keras/legacy/interfaces.py in wrapper(*args, **kwargs)
     89                 warnings.warn('Update your `' + object_name +
     90                               '` call to the Keras 2 API: ' + signature, stacklevel=2)
---> 91             return func(*args, **kwargs)
     92         wrapper._original_function = func
     93         return wrapper

~/anaconda3/envs/rhys_tensorflow/lib/python3.6/site-packages/keras/layers/convolutional.py in __init__(self, size, data_format, **kwargs)
   1804     @interfaces.legacy_upsampling2d_support
   1805     def __init__(self, size=(2, 2), data_format=None, **kwargs):
-> 1806         super(UpSampling2D, self).__init__(**kwargs)
   1807         self.data_format = conv_utils.normalize_data_format(data_format)
   1808         self.size = conv_utils.normalize_tuple(size, 2, 'size')

~/anaconda3/envs/rhys_tensorflow/lib/python3.6/site-packages/keras/engine/topology.py in __init__(self, **kwargs)
    291         for kwarg in kwargs:
    292             if kwarg not in allowed_kwargs:
--> 293                 raise TypeError('Keyword argument not understood:', kwarg)
    294         name = kwargs.get('name')
    295         if not name:

有关upSampling2D的Keras页面供参考, https://www.tensorflow.org/api_docs/python/tf/keras/layers/UpSampling2D

1 个答案:

答案 0 :(得分:0)

def bilinear_upsameple(tensor, size):
    y = tf.image.resize_bilinear(images=tensor, size=size)
    return y
dims = K.int_shape(input_tensor)
y_scaled = Lambda(lambda x : bilinear_upsameple(tensor=x, size=(dims[1]*scale, dims[2]*scale)))(input_tensor)

这是使用lambda层和tf.image.resize_bilinear进行双线性升采样的一种解决方法 在tf 1.12.0上可以正常工作