import torch # 导入 PyTorch 库,用于张量操作 import torch.nn.functional as F # 导入 PyTorch 的函数式 API,包含各种神经网络函数 import numpy as np # 导入 NumPy 库,用于数值计算 import matplotlib as mpl # 导入 Matplotlib 库的主模块 import matplotlib.pyplot as plt # 导入 Matplotlib 的绘图模块,用于显示图像 import imageio.v2 as imageio # 导入 imageio 库,用于读取图片文件 mpl.rcParams['figure.figsize'] = (12, 8) # 设置 Matplotlib 图表默认尺寸为 12x8 英寸 img = torch.FloatTensor(imageio.imread('imgs/hills_2.png')/255) # 读取图片,像素值归一化到[0,1],转为 PyTorch 浮点张量 print(type(img)) plt.imshow(img) # 使用 Matplotlib 显示图片 plt.show() 将上述文件保存为1.py,执行后将hills_2.png显示出来。 {{.:pasted:20260311-135159.png?720}} RGB一共有三个通道 R:Red(红色) G:Green(绿色) B:Blue(蓝色) 其取值范围为[0,255] 例如: 黑色:(0,0,0) 白色:(255,255,255) 红色:(255,0,0) 蓝色:(0,0,255) 如果图像尺寸是 𝐻×𝑊 常见 tensor 形状: PyTorch:(3,𝐻,𝑊) TensorFlow:(𝐻,𝑊,3) 为了让数据和高斯噪声处在同一个零中心对称空间里,要对像素值进行线性变换。