Pytorch实现上采用upsample和下采用downsample 简单调用函数即可实现,超简单的代码块调用


# 上采用函数,输入数据格式示例:tensor维度[3,300,300],即3通道RGB,大小300×300,当然4通道图像也能做
def upsample(image_tensor, width, height, mode):
    # mode可用:最近邻插值"nearest",双线性插值"bilinear",双三次插值"bicubic",如mode="nearest"
    image_upsample_tensor = torch.nn.functional.interpolate(image_tensor.unsqueeze_(0), size=[width, height], mode=mode)
    image_upsample_tensor.squeeze_(0)
    # 将数据归一到正常范围,尺寸改变过程可能数值范围溢出,此处浮点数据位[0,1],整数数据为[0,255]
    image_upsample_tensor = image_upsample_tensor.clamp(0, 1)  
    return image_upsample_tensor


# 下采用函数,输入数据格式示例:tensor维度[3,300,300],即3通道RGB,大小300×300,当然4通道图像也能做
def downsample(image_tensor, width, height):
    image_upsample_tensor = torch.nn.functional.interpolate(image_tensor.unsqueeze_(0), size=[width, height])
    image_upsample_tensor.squeeze_(0)
    # 将数据归一到正常范围,尺寸改变过程可能数值范围溢出,此处浮点数据位[0,1],整数数据为[0,255]
    image_upsample_tensor = image_upsample_tensor.clamp(0, 1)
    return image_upsample_tensor


分享创造快乐,欢迎复制使用!

你可能感兴趣的:(Pytorch实现上采用upsample和下采用downsample 简单调用函数即可实现,超简单的代码块调用)