参考https://www.bookstack.cn/read/keras-docs-zh/sources-layers-convolutional.md
keras.layers.UpSampling2D(size=(2, 2), data_format=None)
2D输入的上采样层
沿着数据的行和列分别重复size[0],size[1]次
参数:
size: 整数,或2个整数的元组,行和列的上采样因子
data_format:字符串, 默认channels_last
输入尺寸:
若data_format是channels_last,输入4D张量,尺寸为(batch,rows,cols,channels)
输出尺寸:
若data_format是channels_last,输出4D张量,尺寸为(batch,upsample_rows,upsampled_cols,channels)
代码实现:
import keras
import numpy as np
import tensorflow as tf
UpSampling2D = keras.layers.UpSampling2D
a=np.array([[1,2,3],[5,6,7]])
a=a.reshape((1,2,-1 ,3))#[[[[1 2 3]],变为4维向量量
#[[5 6 7]]]]
print(a)
ta = tf.convert_to_tensor(a)#变为张量
shape=ta.get_shape().as_list()
# print(shape[1],shape[2])
b=UpSampling2D(2)(ta)#用keras方法对a进行上采样
b2=tf.image.resize_nearest_neighbor(ta,(2*shape[1],2*shape[2]))#用tf方法对其进行上采样
with tf.Session() as sess:
b1=sess.run(b2)#
print(b1)#[[[[1 2 3]
# [1 2 3]]
#
# [[1 2 3]
# [1 2 3]]
#
# [[5 6 7]
# [5 6 7]]
#
# [[5 6 7]
# [5 6 7]]]]