• 读取MNIST文件图片进行上采样、下采样(图片放大两倍)


    MNIST进行上采样(图片放大两倍)

    说实话,要对于像素没有要求直接使用cv2.resize(img, (size_m, size_n)) #其中size_m,size_n可以设置任意值,之前生成的人脸没有达到所要的分辨率,就直接resize的。

    这次让上采样,那就上采样吧

    两种方法nn.Upsample和cv2.pyrUp

    
    import cv2
    import scipy.io as scio
    import torch
    import torch.nn as nn
    import torch.nn.functional as F
    from ball_edge_me3 import main_clu4
    import numpy as np
    
    # 1.读取图片
    dataFile = '../../data_mnist/mnist.mat'
    data = scio.loadmat(dataFile)
    traindata = np.double(data['train_x']) # 转为浮点类型好计算
    the_one = torch.tensor(traindata[0])
    print(the_one)# 一维的张量,一长串类似torch.tensor([0.,0.,0.,0.,0.,255.,0.,189.,0.,...],dtype=torch.float64)
    
    
    # 2.展示一下数字图片,注意参数需要np.array了类型,二维的!!!
    jk = np.resize(the_one, (28, 28))
    cv2.imshow("jk1", jk) #
    cv2.imwrite('jk1.png', jk)
    
    # 3.改变张量维度
    # resize成二维再reshap,还不如直接reshape
    # re_roi = the_one.resize(28, 28)
    # re_roi.reshape(1,28,28) # 事实3维也不行,要四维上采样
    res = the_one.reshape(1, 1, 28, 28)
    # 要四维的才能采样成功,试过reshape(1,28,28)这样相当于torch.tensor([[[1,2],[3,4]]])结果只有两行,
    # 实际上需要torch.tensor([[[[1,2],[3,4]]]])这样的
    # print(res)#可以调试查看像素值是否在原来位置的两倍处
    
    # 方法一:采用pytorch的库进行上采样
    upsample = nn.Upsample(scale_factor=2, mode='nearest')# 这里一定要是浮点类型才能上采样
    kk = upsample(res)
    cv2.imshow("jk2", np.array(kk.reshape(56,56)))
    cv2.imwrite('jk2.png', np.array(kk.reshape(56,56))) # 因为保存图片,把tensor转为二维的,再是array好保存
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    
    
    # 方法二:采用cv2.pyrUp进行上采样(但是这个里面包含采样后会高斯模糊)而我不需要高斯模糊
    # import cv2
    # img='yy.jpg' # 如果不是图片文件而是像上面读写的,一样转array进行输入成src
    # src = cv2.imread(img)
    # res = cv2.pyrUp(src) # 下采样对应的pyrDown
    # print(res)
    # cv2.imshow("res", res)
    # cv2.imwrite('res.png', res)
    # cv2.waitKey(0)
    # cv2.destroyAllWindows()
    
    # 方法一测试理解
    input = torch.arange(1, 5).view(1, 1, 2, 2).float()
    print(input)
    # 这里如果不是四维的,都会报错
    # tensor([[[[1., 2.],
    #          [3., 4.]]]])
    upsample = nn.Upsample(scale_factor=2, mode='nearest') # 下采样对应Downsample
    print(upsample(input))
    # tensor([[[[1., 1., 2., 2.],
    #          [1., 1., 2., 2.],
    #          [3., 3., 4., 4.],
    #          [3., 3., 4., 4.]]]])
    upsample = nn.Upsample(scale_factor=2, mode='bilinear') # 线性增长2倍,等距
    print(upsample(input))
    # tensor([[[[1.0000, 1.2500, 1.7500, 2.0000],
    #          [1.5000, 1.7500, 2.2500, 2.5000],
    #          [2.5000, 2.7500, 3.2500, 3.5000],
    #          [3.0000, 3.2500, 3.7500, 4.0000]]]])
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69

    注意

    • 值得注意的是,在imshow和imwrite的时候,需要的图片参数是二维的array数组,所以如果不是二维需要转化为二维,如果不是数组类型需要转化(注意观察是否是tensor),不然就像我一样报错,百度半天又找不到解决办法,最后是直觉改正过来,自己不够仔细了。
    cv2.error: OpenCV(4.6.0) :-1: error: (-5:Bad argument) in function 'imwrite'
    > Overload resolution failed:
    >  - img is not a numpy array, neither a scalar
    >  - Expected Ptr for argument 'img'
    
    • 1
    • 2
    • 3
    • 4
    cv2.error: OpenCV(4.6.0) :-1: error: (-5:Bad argument) in function 'imshow'
    > Overload resolution failed:
    >  - mat is not a numpy array, neither a scalar
    >  - Expected Ptr for argument 'mat'
    >  - Expected Ptr for argument 'mat'
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 还有就是nn.Upsample所需要的是四维的张量,不是的话就注意转换!!!
    • 转换维度是:张量.reshape(1, 1, 28, 28) ,# 4维,每一维的维度,也可以是其他维度
    • 可以直接张量.reshape(…)这样reshape后还是张量,如果np.reshape(张量,(维度))这样转换后是array
    • 错误写法:本来最初在那里搞的是torch.tensor([[m]]) ,其中m是一个张量torch.tensor([[1,2],[3,4]],以为这样就会变成4维的张量了(毕竟看到torch.tensor([[[1,2],[3,4]]])这样的写法),nono,这样只会报下面这个错…
    ValueError: only one element tensors can be converted to Python scalars
    
    • 1

    方法一四维很重要

    • 三维的去测试
    oo = torch.tensor([[[1.,2.],[3.,4.]]])
    upsample = nn.Upsample(scale_factor=2, mode='nearest')
    
    • 1
    • 2

    在这里插入图片描述

    只横向加了,纵向没有两倍,差点看岔了

    • 四维的
    oo = torch.tensor([[[[1.,2.],[3.,4.]]]])
    upsample = nn.Upsample(scale_factor=2, mode='nearest')
    
    • 1
    • 2

    在这里插入图片描述

    • 至于更低维度的张量上采样。。是会报错的
      NotImplementedError: Input Error: Only 3D, 4D and 5D input Tensors supported (got 2D) for the modes: nearest | linear | bilinear | bicubic | trilinear (got nearest)

    在这里插入图片描述

    很认真写的,如果对您有帮助~~~~~

    • 请添加图片描述
  • 相关阅读:
    电脑多位置同时输入方法
    npm yarn 和 pnpm 之间命令的区别
    C++编写的多线程自动爬虫程序
    elasticsearch18-自动补全实战
    自然语音处理(NLP)系列(五)——详解智能问答系统
    斐波那契数列Kotlin的N种实现方式
    使用虚拟机Ubuntu进行MPI环境搭建
    Linux环境下为应用程序建立快捷方式
    leaflet 地图遮罩、扣洞
    Mac装虚拟机好不好 Mac装虚拟机和装Windows系统一样吗 PD虚拟机
  • 原文地址:https://blog.csdn.net/lanmy_dl/article/details/127462414