mean = np.array([0.485, 0.456, 0.406]).reshape(1, 3, 1, 1)
时间: 2023-05-19 18:02:41 浏览: 109
这是一个 numpy 数组的定义,用于图像处理中的归一化操作。其中,np.array 表示创建一个 numpy 数组,[0.485, 0.456, 0.406] 表示数组的元素,reshape(1, 3, 1, 1) 表示将数组变形为 1 行 3 列 1 个通道 1 个样本的形式。
相关问题
def __call__(self): # 图像转换 data_transorform = torchvision.transforms.Compose([ torchvision.transforms.Resize((224, 224)), torchvision.transforms.CenterCrop((224, 224)), torchvision.transforms.ToTensor(), torchvision.transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) img_list = glob.glob(self.img_dir + os.sep + "*.png") for imgpath in img_list: img = cv2.imread(imgpath) new_img = self.expend_img(img) # 补边 img = Image.fromarray(new_img) img = data_transorform(img) # 转换 img = torch.reshape(img, (-1, 3, self.imgsz, self.imgsz)).to(device) # 维度转换[B,C,H,W] pred = self.model(img) _, pred = torch.max(pred, 1) outputs = self.class_name[pred] print("Image path:", imgpath, " pred:", outputs) # 补边为正方形 def expend_img(self, img, fill_pix=122): ''' :param img: 图片数据 :param fill_pix: 填充像素, 默认为灰色,自行更改 :return: ''' h, w = img.shape[:2] # 获取图像的宽高 if h >= w: # 左右填充 padd_width = int(h - w) // 2 padd_h, padd_b, padd_l, padd_r = 0, 0, padd_width, padd_width # 获取上下左右四个方向需要填充的像素 elif h < w: # 上下填充 padd_high = int(w - h) // 2 padd_h, padd_b, padd_l, padd_r = padd_high, padd_high, 0, 0 new_img = cv2.copyMakeBorder(img, padd_h, padd_b, padd_l, padd_r, borderType=cv2.BORDER_CONSTANT, value=[fill_pix, fill_pix, fill_pix]) return new_img if __name__ == '__main__': opt = parser_opt() test_img = Test_model(opt) test_img()
这段代码是一个使用PyTorch实现的图像分类模型的测试程序。主要实现了以下功能:
1.读取指定目录下的所有png格式图像文件。
2.对读取到的图像进行补边操作,使其变为正方形。
3.将图像进行预处理,包括图像尺寸的调整、中心裁剪、像素归一化等操作。
4.将处理后的图像输入模型进行分类预测,打印输出预测结果。
其中,模型的具体实现没有在这段代码中给出,可以通过调用Test_model类中的self.model属性来获取模型,该模型需要预先训练好并保存在文件中。
import torch, os, cv2 from model.model import parsingNet from utils.common import merge_config from utils.dist_utils import dist_print import torch import scipy.special, tqdm import numpy as np import torchvision.transforms as transforms from data.dataset import LaneTestDataset from data.constant import culane_row_anchor, tusimple_row_anchor if __name__ == "__main__": torch.backends.cudnn.benchmark = True args, cfg = merge_config() dist_print('start testing...') assert cfg.backbone in ['18','34','50','101','152','50next','101next','50wide','101wide'] if cfg.dataset == 'CULane': cls_num_per_lane = 18 elif cfg.dataset == 'Tusimple': cls_num_per_lane = 56 else: raise NotImplementedError net = parsingNet(pretrained = False, backbone=cfg.backbone,cls_dim = (cfg.griding_num+1,cls_num_per_lane,4), use_aux=False).cuda() # we dont need auxiliary segmentation in testing state_dict = torch.load(cfg.test_model, map_location='cpu')['model'] compatible_state_dict = {} for k, v in state_dict.items(): if 'module.' in k: compatible_state_dict[k[7:]] = v else: compatible_state_dict[k] = v net.load_state_dict(compatible_state_dict, strict=False) net.eval() img_transforms = transforms.Compose([ transforms.Resize((288, 800)), transforms.ToTensor(), transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)), ]) if cfg.dataset == 'CULane': splits = ['test0_normal.txt', 'test1_crowd.txt', 'test2_hlight.txt', 'test3_shadow.txt', 'test4_noline.txt', 'test5_arrow.txt', 'test6_curve.txt', 'test7_cross.txt', 'test8_night.txt'] datasets = [LaneTestDataset(cfg.data_root,os.path.join(cfg.data_root, 'list/test_split/'+split),img_transform = img_transforms) for split in splits] img_w, img_h = 1640, 590 row_anchor = culane_row_anchor elif cfg.dataset == 'Tusimple': splits = ['test.txt'] datasets = [LaneTestDataset(cfg.data_root,os.path.join(cfg.data_root, split),img_transform = img_transforms) for split in splits] img_w, img_h = 1280, 720 row_anchor = tusimple_row_anchor else: raise NotImplementedError for split, dataset in zip(splits, datasets): loader = torch.utils.data.DataLoader(dataset, batch_size=1, shuffle = False, num_workers=1) fourcc = cv2.VideoWriter_fourcc(*'MJPG') print(split[:-3]+'avi') vout = cv2.VideoWriter(split[:-3]+'avi', fourcc , 30.0, (img_w, img_h)) for i, data in enumerate(tqdm.tqdm(loader)): imgs, names = data imgs = imgs.cuda() with torch.no_grad(): out = net(imgs) col_sample = np.linspace(0, 800 - 1, cfg.griding_num) col_sample_w = col_sample[1] - col_sample[0] out_j = out[0].data.cpu().numpy() out_j = out_j[:, ::-1, :] prob = scipy.special.softmax(out_j[:-1, :, :], axis=0) idx = np.arange(cfg.griding_num) + 1 idx = idx.reshape(-1, 1, 1) loc = np.sum(prob * idx, axis=0) out_j = np.argmax(out_j, axis=0) loc[out_j == cfg.griding_num] = 0 out_j = loc # import pdb; pdb.set_trace() vis = cv2.imread(os.path.join(cfg.data_root,names[0])) for i in range(out_j.shape[1]): if np.sum(out_j[:, i] != 0) > 2: for k in range(out_j.shape[0]): if out_j[k, i] > 0: ppp = (int(out_j[k, i] * col_sample_w * img_w / 800) - 1, int(img_h * (row_anchor[cls_num_per_lane-1-k]/288)) - 1 ) cv2.circle(vis,ppp,5,(0,255,0),-1) vout.write(vis) vout.release()
这是一个使用PyTorch框架测试模型的代码,代码中导入了许多必要的库以及自定义的模型和数据集等。通过调用PyTorch的后端,启用一些加速技术,然后解析配置参数,并根据数据集类型设置类别数。创建一个模型实例,传入必要的参数并将其转移到GPU上以加速处理。最后进行模型测试。
阅读全文