MNIST进行上采样(图片放大两倍)
说实话,要对于像素没有要求直接使用cv2.resize(img, (size_m, size_n)) #其中size_m,size_n可以设置任意值,之前生成的人脸没有达到所要的分辨率,就直接resize的。
这次让上采样,那就上采样吧
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]]]])
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'
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'
错误写法:本来最初在那里搞的是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
oo = torch.tensor([[[1.,2.],[3.,4.]]])
upsample = nn.Upsample(scale_factor=2, mode='nearest')

只横向加了,纵向没有两倍,差点看岔了
oo = torch.tensor([[[[1.,2.],[3.,4.]]]])
upsample = nn.Upsample(scale_factor=2, mode='nearest')


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