1.矩阵和tensor转换
# tensor转numpy
matrix_numpy = matrix_tensor.numpy()
# numpy转tensor
matrix_tensor = torch.from_numpy(matrix_numpy)
2.矩阵切割
# eg:切割部分图像,前两个维度分别代表高和宽,第三个维度是RGB,全选
clip_image = whole_img[left:right,top:bottom,:]
3.根据由RGB组成的二维矩阵显示并保存图像
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
pil_img = Image.fromarray(np.uint8(clip_image))
# 保存到指定路径
pil_img.save("savePath")
4.设置图像尺寸(resize)
pil_img = Image.fromarray(np.uint8(clip_image)).resize((224,224),Image.ANTIALIAS)
5.矩阵维度转换
# w*h*3转换为3*w*h
img=img.transpose((2,0,1)
6.CPU->GPU
self.model= self.model.to(opt.device)
out = self.model(