歡迎您光臨本站 註冊首頁

在keras裡實現自定義上採樣層

←手機掃碼閱讀     ml5rwbikls @ 2020-06-29 , reply:0

Keras裡的UpSampling2D層不是中的雙線性內插,而是簡單的重複圖像。這點和pytorch不一樣,pytorch默認使用的是雙線性內插。

同樣:這裡仍然使用的是keras而不是tf.keras.

keras裡UpSampling2D的部分定義說明如下:

  class UpSampling2D(Layer):    """Upsampling layer for 2D inputs.    Repeats the rows and columns of the data    by size[0] and size[1] respectively.

 

可以看出,這裡的上採樣確實只是簡單的圖像重複。

要想使用雙線性或者最近鄰或者雙三次插值上採樣,則需要在tf的tf.image.resize_images函數基礎上進行包裝,代碼如下:

  ####定義:  def my_upsampling(x,img_w,img_h,method=0):    """0:雙線性差值。1:最近鄰居法。2:雙三次插值法。3:面積插值法"""    return tf.image.resize_images(x,(img_w,img_h),0)     ###調用:  Lambda(my_upsampling,arguments={'img_w':self.img_w,'img_h':self.img_h})(output_6)     ###load_model時注意加上tf:  model = keras.models.load_model('my_model.h5', custom_objects={'tf': tf})

 

補充知識:keras中使用內置模型語義分割上採樣維度不匹配

1.卷積時要使用padding=same因此要修改原來的padding=valid

x = conv2d_bn(img_input, 32, 3, strides=2, padding='same')

2.池化時卷積核大小修改為2而不是原來的3

branch_pool = layers.MaxPooling2D(2, strides=2, padding='same')(x)
 

 

   


[ml5rwbikls ] 在keras裡實現自定義上採樣層已經有269次圍觀

http://coctec.com/docs/python/shhow-post-240273.html