当前位置:   article > 正文

项目pytorch-deeplab-xception为例,测试时怎么保存target、image:target.cpu().numpy()_output3.data.cpu()

output3.data.cpu()

一般性流程 

  1. '''
  2. IPL转换为tensor
  3. _img = Image.open(os.path.join(self.img_dir, path)).convert('RGB')
  4. img = np.array(img).astype(np.float32).transpose((2, 0, 1))
  5. img = torch.from_numpy(img).float()
  6. img = img.cuda()
  7. tensor转换为IPL
  8. image1 = image.data.cpu().numpy()
  9. IPLimage = numpyimg.transpose((1, 2, 0))
  10. save_img = Image.fromarray(IPLimage.astype('uint8'))
  11. '''

例子:

  1. for i, sample in enumerate(self.test_loader):
  2. image, target = sample['image'], sample['label']
  3. torch.cuda.synchronize()
  4. start = time.time()
  5. with torch.no_grad():
  6. output = self.model(image)
  7. end = time.time()
  8. times = (end - start) * 1000
  9. print(times, "ms")
  10. torch.cuda.synchronize()
  11. pred = output.data.cpu().numpy()
  12. target = target.cpu().numpy()
  13. pred = np.argmax(pred, axis=1)
  14. self.evaluator.add_batch(target, pred)

我想看一下target是否对,通过opencv保存,首先看下opencv的格式:

cv2.resize(src, dsize[, dst[, fx[, fy[, interpolation]]]]) -> dst 

fx - 水平轴上的比例因子。fy - 垂直轴上的比例因子。

numpy实现图像部分ROI截取:

  1. for index in inds:
  2. xmin_depth = int((xmin1[index] * expected + crop_start) * scale)
  3. ymin_depth = int((ymin1[index] * expected) * scale)
  4. xmax_depth = int((xmax1[index] * expected + crop_start) * scale)
  5. ymax_depth = int((ymax1[index] * expected) * scale)
  6. depth_temp = depth[ymin_depth:ymax_depth, xmin_depth:xmax_depth].astype(float)

 首先numpy是[高度h:宽度w]

如果是x1,y1,x2,y2(左上,右下)的任务,应该是img=ori_img[y1:y2, x1:x2]

  1. import cv2
  2. cvimg = cv2.imread("./dog.jpg")
  3. graycvimg = cv2.cvtColor(cvimg, cv2.COLOR_BGR2GRAY)
  4. cv2.imwrite("./dog_gray.jpg", graycvimg)
  5. graycvimg_bgr = cv2.cvtColor(graycvimg, cv2.COLOR_GRAY2BGR)
  6. cv2.imwrite("./dog_gray_bgr.jpg", graycvimg_bgr)

  1. from PIL import Image
  2. import numpy as np
  3. img = Image.open(imgsname).convert('RGB')
  4. imglabel = Image.open(imgsname).convert('P')
  5. arrayimg = np.array(img).astype(np.float32)
  6. transposeimg = arrayimg.transpose((2, 0, 1))

关于PIL和opencv还有一个区别:size的先后,PIL是W,H opencv是H,W,C

  1. imgsname = newpath + namename + '_ccvt_' + str(j) + '.jpg'
  2. img = Image.open(imgsname).convert('RGB')
  3. W, H = img.size
  4. img = np.array(img)
  5. dst, scale_factor = mmcv.imrescale(img, (1333, 800), return_scale=True)
  6. newH, newW, newC = dst.shape
  1. # tensor 转换为 numpy
  2. numpyimg = imgarray.numpy()
  3. # numpy 转换为 IPL格式
  4. IPLimage = numpyimg.transpose((1, 2, 0))
  5. '''
  6. IPL转换为tensor
  7. _img = Image.open(os.path.join(self.img_dir, path)).convert('RGB')
  8. img = np.array(img).astype(np.float32).transpose((2, 0, 1))
  9. img = torch.from_numpy(img).float()
  10. img = img.cuda()
  11. tensor转换为IPL
  12. image1 = image.data.cpu().numpy()
  13. IPLimage = numpyimg.transpose((1, 2, 0))
  14. save_img = Image.fromarray(IPLimage.astype('uint8'))
  15. '''

参考:

https://blog.csdn.net/m0_37382341/article/details/83548601

numpy.reshape
Numpy将不管是什么形状的数组,先扁平化处理成一个一维的列表,然后按照你重新定义的形状,再把这个列表截断拼成新的形状。 在这个过程中,如果你要处理的是图片矩阵的话,就会完全改变图片信息。
numpy.transpose
numpy.transpose采取轴作为输入,所以你可以改变轴,这对于张量来说很有用,也很方便。比如data.transpose(1,0,2),就表示把1位置的数换到0位置,0位置的换到1位置,2没有变。

由于测试时候使用:

  1. def transform_val(self, sample):
  2. composed_transforms = transforms.Compose([
  3. tr.FixScaleCrop(crop_size=self.args.crop_size),
  4. tr.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)),
  5. tr.ToTensor()
  6. ])
  7. return composed_transforms(sample)

应该把注释改掉:

  1. def transform_val(self, sample):
  2. composed_transforms = transforms.Compose([
  3. tr.FixScaleCrop(crop_size=self.args.crop_size),
  4. #tr.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)),
  5. tr.ToTensor()
  6. ])
  7. return composed_transforms(sample)

这样方便我们保存Image对比 

  1. import cv2
  2. target = target.cpu().numpy()
  3. image = image.data.cpu().numpy()
  4. image1 = image[0, :]
  5. target1 = target[0, :]
  6. #image1.reshape([image1.size[1],image1.size[2],image1.size[3]])
  7. #target1.reshape([image1.size[1],image1.size[2],image1.size[3]])
  8. image1 = image1.transpose(2,1,0)
  9. #target1 = target1.transpose(2,1,0)
  10. image1 = cv2.cvtColor(image1, cv2.COLOR_RGB2BGR)
  11. cv2.imwrite("./image1.jpg",image1)
  12. cv2.imwrite("./target1.jpg", target1)

我这里出现一些问题,target方向错误了,debug一下,看看载入时候有没有问题:

  1. def _make_img_gt_point_pair(self, index):
  2. coco = self.coco
  3. img_id = self.ids[index]
  4. img_metadata = coco.loadImgs(img_id)[0]
  5. path = img_metadata['file_name']
  6. _img = Image.open(os.path.join(self.img_dir, path)).convert('RGB')
  7. cocotarget = coco.loadAnns(coco.getAnnIds(imgIds=img_id))
  8. _target = Image.fromarray(self._gen_seg_mask(
  9. cocotarget, img_metadata['height'], img_metadata['width']))
  10. image1 = cv2.cvtColor(np.asarray(_img), cv2.COLOR_RGB2BGR)
  11. target1 = cv2.cvtColor(np.asarray(_target), cv2.COLOR_GRAY2BGR)
  12. cv2.imwrite("./image1.jpg", image1)
  13. cv2.imwrite("./target1.jpg", target1)
  14. return _img, _target
  1. def __getitem__(self, index):
  2. _img, _target = self._make_img_gt_point_pair(index)
  3. sample = {'image': _img, 'label': _target}
  4. if self.split == "train":
  5. return self.transform_tr(sample)
  6. elif self.split == 'val':
  7. return self.transform_val(sample)
  8. elif self.split == 'test':
  9. X = self.transform_val(sample)
  10. aa = X['image']
  11. bb = X['label']
  12. aa = aa.cpu().numpy()
  13. bb = bb.cpu().numpy()
  14. aa = aa.transpose(2, 1, 0)
  15. image1 = cv2.cvtColor(aa, cv2.COLOR_RGB2BGR)
  16. target1 = cv2.cvtColor(bb, cv2.COLOR_GRAY2BGR)
  17. cv2.imwrite("./image2.jpg", image1)
  18. cv2.imwrite("./target2.jpg", target1)
  19. return X

 原图resize后方向变了,果然。。。。。。。

原图:

因为项目中使用了一个torch函数进行预处理:

pytorch的transforms.py

 

  1. class Compose(object):
  2. """Composes several transforms together.
  3. Args:
  4. transforms (list of ``Transform`` objects): list of transforms to compose.
  5. Example:
  6. >>> transforms.Compose([
  7. >>> transforms.CenterCrop(10),
  8. >>> transforms.ToTensor(),
  9. >>> ])
  10. """
  11. def __init__(self, transforms):
  12. self.transforms = transforms
  13. def __call__(self, img):
  14. for t in self.transforms:
  15. img = t(img)
  16. return img

首先

  1. class FixScaleCrop(object):
  2. def __init__(self, crop_size):
  3. self.crop_size = crop_size
  4. def __call__(self, sample):
  5. img = sample['image']
  6. mask = sample['label']
  7. w, h = img.size
  8. if w > h:
  9. oh = self.crop_size
  10. ow = int(1.0 * w * oh / h)
  11. else:
  12. ow = self.crop_size
  13. oh = int(1.0 * h * ow / w)
  14. img = img.resize((ow, oh), Image.BILINEAR)
  15. mask = mask.resize((ow, oh), Image.NEAREST)
  16. # center crop
  17. w, h = img.size
  18. x1 = int(round((w - self.crop_size) / 2.))
  19. y1 = int(round((h - self.crop_size) / 2.))
  20. img = img.crop((x1, y1, x1 + self.crop_size, y1 + self.crop_size))
  21. mask = mask.crop((x1, y1, x1 + self.crop_size, y1 + self.crop_size))
  22. return {'image': img,
  23. 'label': mask}
  1. class FixScaleCrop(object):
  2. def __init__(self, crop_size):
  3. self.crop_size = crop_size
  4. def __call__(self, sample):
  5. img = sample['image']
  6. mask = sample['label']
  7. w, h = img.size
  8. if w > h:
  9. oh = self.crop_size
  10. ow = int(1.0 * w * oh / h)
  11. else:
  12. ow = self.crop_size
  13. oh = int(1.0 * h * ow / w)
  14. img = img.resize((ow, oh), Image.BILINEAR)
  15. mask = mask.resize((ow, oh), Image.NEAREST)
  16. # center crop
  17. w, h = img.size
  18. x1 = int(round((w - self.crop_size) / 2.))
  19. y1 = int(round((h - self.crop_size) / 2.))
  20. img = img.crop((x1, y1, x1 + self.crop_size, y1 + self.crop_size))
  21. mask = mask.crop((x1, y1, x1 + self.crop_size, y1 + self.crop_size))
  22. import cv2
  23. image1 = cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)
  24. target1 = cv2.cvtColor(np.asarray(mask), cv2.COLOR_GRAY2BGR)
  25. cv2.imwrite("./image3.jpg", image1)
  26. cv2.imwrite("./target3.jpg", target1)
  27. return {'image': img,
  28. 'label': mask}

 

程序在这里还是没问题的,结果接下来会进入:

  1. class ToTensor(object):
  2. """Convert ndarrays in sample to Tensors."""
  3. def __call__(self, sample):
  4. # swap color axis because
  5. # numpy image: H x W x C
  6. # torch image: C X H X W
  7. img = sample['image']
  8. mask = sample['label']
  9. img = np.array(img).astype(np.float32).transpose((2, 0, 1))
  10. mask = np.array(mask).astype(np.float32)
  11. img = torch.from_numpy(img).float()
  12. mask = torch.from_numpy(mask).float()
  13. return {'image': img,
  14. 'label': mask}
  1. class ToTensor(object):
  2. """Convert ndarrays in sample to Tensors."""
  3. def __call__(self, sample):
  4. # swap color axis because
  5. # numpy image: H x W x C
  6. # torch image: C X H X W
  7. img = sample['image']
  8. mask = sample['label']
  9. img = np.array(img).astype(np.float32).transpose((2, 0, 1))
  10. mask = np.array(mask).astype(np.float32)
  11. img = torch.from_numpy(img).float()
  12. mask = torch.from_numpy(mask).float()
  13. import cv2
  14. image1=img.cpu().numpy()
  15. target1=mask.cpu().numpy()
  16. image1 = image1.transpose(2, 1, 0)
  17. image1 = cv2.cvtColor(image1, cv2.COLOR_RGB2BGR)
  18. target1 = cv2.cvtColor(target1, cv2.COLOR_GRAY2BGR)
  19. cv2.imwrite("./image4.jpg", image1)
  20. cv2.imwrite("./target4.jpg", target1)
  21. return {'image': img,
  22. 'label': mask}

 这里出错了,方向不对了

如果将代码改为;

img = np.array(img).astype(np.float32).transpose((2, 1, 0))

 方向就都对了,那么作者原本为什么那样写??????

        img = np.array(img).astype(np.float32).transpose((2, 0, 1))

到底有什么用,

  1. class ToTensor(object):
  2. """Convert ndarrays in sample to Tensors."""
  3. def __call__(self, sample):
  4. # swap color axis because
  5. # numpy image: H x W x C
  6. # torch image: C X H X W
  7. img = sample['image']
  8. mask = sample['label']
  9. import cv2
  10. image1 = cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)
  11. target1 = cv2.cvtColor(np.asarray(mask), cv2.COLOR_GRAY2BGR)
  12. cv2.imwrite("./image5.jpg", image1)
  13. cv2.imwrite("./target5.jpg", target1)
  14. xxx = np.array(img).astype(np.float32)
  15. import copy
  16. xxx1 = copy.deepcopy(xxx)
  17. xxx2 = copy.deepcopy(xxx)
  18. img1 = np.array(xxx1).astype(np.float32).transpose((2, 1, 0))
  19. img2 = np.array(xxx2).astype(np.float32).transpose((2, 0, 1))
  20. img = np.array(img).astype(np.float32).transpose((2, 1, 0))
  21. mask = np.array(mask).astype(np.float32)
  22. img = torch.from_numpy(img).float()
  23. mask = torch.from_numpy(mask).float()

513*513*3---3* 513*513   

.transpose((2, 1, 0)) 

513*513*3---3* 513*513

.transpose((2, 0, 1))

 其实实验做到这里我已经明白是我错了,

原本是

513*513*3

我们通过.transpose((2, 0, 1)),正常变换,我错在test显示的时候:

  1. import cv2
  2. target = target.cpu().numpy()
  3. image = image.data.cpu().numpy()
  4. image1 = image[0, :]
  5. target1 = target[0, :]
  6. #image1.reshape([image1.size[1],image1.size[2],image1.size[3]])
  7. #target1.reshape([image1.size[1],image1.size[2],image1.size[3]])
  8. image1 = image1.transpose(1,2,0)
  9. image1 = cv2.cvtColor(image1, cv2.COLOR_RGB2BGR)
  10. cv2.imwrite("./image1.jpg",image1)
  11. cv2.imwrite("./target1.jpg", target1)

这里应该是

image1 = image1.transpose(1,2,0)

因为原本

  1. for i, sample in enumerate(self.test_loader):
  2. image, target = sample['image'], sample['label']

image为:torch.Size([1, 3, 513, 513])

target为:<class 'tuple'>: (1, 513, 513)

所以应该使用image1 = image1.transpose(1,2,0)

这下就对了

现在还有一个问题摆在面前,

我做测试时候,COCO数据集格式,自己的数据集,

图片有153张,但是最后输出只有25张pred,

找原因:

pytorch-deeplab-xception/dataloaders/datasets/coco.py

在处理coco数据之前,会生成一个test_ids_2017.pth

id对应文件,新ID与旧ID相对应,

用于知道哪些ID被保留下来,用于接下来的测试

  1. if os.path.exists(ids_file):
  2. self.ids = torch.load(ids_file)
  3. else:
  4. ids = list(self.coco.imgs.keys())
  5. self.ids = self._preprocess(ids, ids_file, self.split)
  6. self.args = args

判断条件在函数self._preprocess(ids, ids_file, self.split)

  1. def _preprocess(self, ids, ids_file, split):
  2. print("Preprocessing mask, this will take a while. " + \
  3. "But don't worry, it only run once for each split.")
  4. tbar = trange(len(ids))
  5. new_ids = []
  6. for i in tbar:
  7. img_id = ids[i]
  8. cocotarget = self.coco.loadAnns(self.coco.getAnnIds(imgIds=img_id))
  9. img_metadata = self.coco.loadImgs(img_id)[0]
  10. savemaskname=img_metadata['file_name']
  11. image = ids_file.split("annotations")[0]+'images/'+split+str(self.year) + '/' +savemaskname
  12. oriimg = cv2.imread(image)
  13. h,w,c = oriimg.shape
  14. mask = self._gen_seg_mask(cocotarget, h,
  15. w)
  16. cv2.imwrite('/home/spple/paddle/DeepGlint/deepglint-adv/pytorch-deeplab-xception/mask/'+split+'/'+savemaskname, mask)
  17. # more than 1k pixels
  18. if (mask > 0).sum() > 1000:
  19. new_ids.append(img_id)
  20. tbar.set_description('Doing: {}/{}, got {} qualified images'. \
  21. format(i, len(ids), len(new_ids)))
  22. print('Found number of qualified images: ', len(new_ids))
  23. torch.save(new_ids, ids_file)
  24. return new_ids

通过函数def _gen_seg_mask(self, target, h, w): 获取mask

  1. def _gen_seg_mask(self, target, h, w):
  2. mask = np.zeros((h, w), dtype=np.uint8)
  3. coco_mask = self.coco_mask
  4. for instance in target:
  5. rle = coco_mask.frPyObjects(instance['segmentation'], h, w)
  6. m = coco_mask.decode(rle)
  7. cat = instance['category_id']
  8. if cat in self.CAT_LIST:
  9. c = self.CAT_LIST.index(cat)
  10. else:
  11. continue
  12. if len(m.shape) < 3:
  13. mask[:, :] += (mask == 0) * (m * c)
  14. else:
  15. mask[:, :] += (mask == 0) * (((np.sum(m, axis=2)) > 0) * c).astype(np.uint8)
  16. return mask

但是这里有个问题,判断依据是mask分割像素点必须是1000以上,但是对于小图像,可能达不到,这里,我们要修改

  1. if (mask > 0).sum() > 1000:
  2. new_ids.append(img_id)

修改为:

  1. if (mask > 0).sum() > 50:
  2. new_ids.append(img_id)

还有之前的函数只是简单的保存是参考:

https://github.com/jfzhang95/pytorch-deeplab-xception/issues/122

  1. import argparse
  2. import os
  3. import numpy as np
  4. import tqdm
  5. import torch
  6. from PIL import Image
  7. from dataloaders import make_data_loader
  8. from modeling.deeplab import *
  9. from dataloaders.utils import get_pascal_labels
  10. from utils.metrics import Evaluator
  11. class Tester(object):
  12. def __init__(self, args):
  13. if not os.path.isfile(args.model):
  14. raise RuntimeError("no checkpoint found at '{}'".fromat(args.model))
  15. self.args = args
  16. self.color_map = get_pascal_labels()
  17. self.test_loader, self.ids, self.nclass = make_data_loader(args)
  18. #Define model
  19. model = DeepLab(num_classes=self.nclass,
  20. backbone=args.backbone,
  21. output_stride=args.out_stride,
  22. sync_bn=False,
  23. freeze_bn=False)
  24. self.model = model
  25. device = torch.device('cpu')
  26. checkpoint = torch.load(args.model, map_location=device)
  27. self.model.load_state_dict(checkpoint['state_dict'])
  28. self.evaluator = Evaluator(self.nclass)
  29. def save_image(self, array, id, op):
  30. text = 'gt'
  31. if op == 0:
  32. text = 'pred'
  33. file_name = str(id)+'_'+text+'.png'
  34. r = array.copy()
  35. g = array.copy()
  36. b = array.copy()
  37. for i in range(self.nclass):
  38. r[array == i] = self.color_map[i][0]
  39. g[array == i] = self.color_map[i][1]
  40. b[array == i] = self.color_map[i][2]
  41. rgb = np.dstack((r, g, b))
  42. save_img = Image.fromarray(rgb.astype('uint8'))
  43. save_img.save(self.args.save_path+os.sep+file_name)
  44. def test(self):
  45. self.model.eval()
  46. self.evaluator.reset()
  47. # tbar = tqdm(self.test_loader, desc='\r')
  48. for i, sample in enumerate(self.test_loader):
  49. image, target = sample['image'], sample['label']
  50. with torch.no_grad():
  51. output = self.model(image)
  52. pred = output.data.cpu().numpy()
  53. target = target.cpu().numpy()
  54. pred = np.argmax(pred, axis=1)
  55. self.save_image(pred[0], self.ids[i], 0)
  56. self.save_image(target[0], self.ids[i], 1)
  57. self.evaluator.add_batch(target, pred)
  58. Acc = self.evaluator.Pixel_Accuracy()
  59. Acc_class = self.evaluator.Pixel_Accuracy_Class()
  60. print('Acc:{}, Acc_class:{}'.format(Acc, Acc_class))
  61. def main():
  62. parser = argparse.ArgumentParser(description='Pytorch DeeplabV3Plus Test your data')
  63. parser.add_argument('--test', action='store_true', default=True,
  64. help='test your data')
  65. parser.add_argument('--dataset', default='pascal',
  66. help='datset format')
  67. parser.add_argument('--backbone', default='xception',
  68. help='what is your network backbone')
  69. parser.add_argument('--out_stride', type=int, default=16,
  70. help='output stride')
  71. parser.add_argument('--crop_size', type=int, default=513,
  72. help='image size')
  73. parser.add_argument('--model', type=str, default='',
  74. help='load your model')
  75. parser.add_argument('--save_path', type=str, default='',
  76. help='save your prediction data')
  77. args = parser.parse_args()
  78. if args.test:
  79. tester = Tester(args)
  80. tester.test()
  81. if __name__ == "__main__":
  82. main()

这里保存完后是:

  1. def save_image(self, array, id, op, oriimg=None, image111=None):
  2. import cv2
  3. text = 'gt'
  4. if op == 0:
  5. text = 'pred'
  6. file_name = str(id)+'_'+text+'.png'
  7. drow_ori_name = str(id)+'_'+'vis'+'.png'
  8. #513*513
  9. r = array.copy()
  10. g = array.copy()
  11. b = array.copy()
  12. if oriimg is True:
  13. image111 = image111.data.cpu().numpy()
  14. image111 = image111[0, :]
  15. image111 = image111.transpose(1,2,0)
  16. oneimg = image111
  17. for i in range(self.nclass):
  18. r[array == i] = self.color_map[i][2]
  19. g[array == i] = self.color_map[i][1]
  20. b[array == i] = self.color_map[i][0]
  21. rgb = np.dstack((r, g, b))
  22. hh,ww,_ = rgb.shape
  23. if oriimg is True:
  24. for i in range(self.nclass):
  25. if i != 0:
  26. index = np.argwhere(array == i)
  27. for key in index:
  28. oneimg[key[0]][key[1]][0] = self.color_map[i][0]
  29. oneimg[key[0]][key[1]][1] = self.color_map[i][1]
  30. oneimg[key[0]][key[1]][2] = self.color_map[i][2]
  31. oneimg = cv2.cvtColor(oneimg, cv2.COLOR_RGB2BGR)
  32. cv2.imwrite(self.args.save_path + os.sep + drow_ori_name, oneimg)

这样完全覆盖了,我们并不能看到真实样貌,应该参考mask_rcnn,透明效果:

其实就是将原始图像和预测类的颜色,不同比例结合,生成可视化图像:

  1. oneimg[key[0]][key[1]][0] = oneimg[key[0]][key[1]][0] * 0.5 + self.color_map[i][0] * 0.5
  2. oneimg[key[0]][key[1]][1] = oneimg[key[0]][key[1]][1] * 0.5 + self.color_map[i][1] * 0.5
  3. oneimg[key[0]][key[1]][2] = oneimg[key[0]][key[1]][2] * 0.5 + self.color_map[i][2] * 0.5

这里还有一个问题

我们进行测试时候显示:

  1. Acc:0.9829744103317358, Acc_class:0.7640047637800897, mIoU:0.7015250613321066
  2. /home/spple/pytorch-deeplab-xception/utils/metrics.py:14: RuntimeWarning: invalid value encountered in true_divide
  3. Acc = np.diag(self.confusion_matrix) / self.confusion_matrix.sum(axis=1)
  4. /home/spple/pytorch-deeplab-xception/utils/metrics.py:24: RuntimeWarning: invalid value encountered in true_divide
  5. np.diag(self.confusion_matrix))

原来是因为数组分母有为0的

比如:

  1. def Pixel_Accuracy_Class(self):
  2. a = np.diag(self.confusion_matrix)
  3. b = self.confusion_matrix.sum(axis=1)
  4. #Acc = np.diag(self.confusion_matrix) / self.confusion_matrix.sum(axis=1)
  5. Acc = a/b
  6. Acc = np.nanmean(Acc)
  7. return Acc

a: 

b:

Acc:

Acc = np.nanmean(Acc):

0.7640047637800897=(0.993579+0.534430)/2

 

顺便做了一个实验:

  1. import numpy as np
  2. a = np.array([[12],[6]])
  3. b = np.array([3,3])
  4. Acc_1= a/b
  5. c = np.array([[12,1],[1,6]])
  6. x2 = np.diag(c)
  7. Acc_2= x2/b
  8. x1 = np.zeros((2,)*1)
  9. x1[0]=3
  10. x1[1]=3

向量相除,如果最后只想得到向量,分子分母shape应该 是<class 'tuple'>: (2,)

a

b

Acc_1

c

x2

Acc_2

x1

test.py

  1. import argparse
  2. import os
  3. import numpy as np
  4. import tqdm
  5. import torch
  6. import time
  7. #https://github.com/jfzhang95/pytorch-deeplab-xception/issues/122
  8. from PIL import Image
  9. from dataloaders import make_data_loader
  10. from modeling.deeplab import *
  11. from dataloaders.utils import get_pascal_labels
  12. from utils.metrics import Evaluator
  13. import cv2
  14. class Tester(object):
  15. def __init__(self, args):
  16. if not os.path.isfile(args.model):
  17. raise RuntimeError("no checkpoint found at '{}'".fromat(args.model))
  18. self.args = args
  19. self.color_map = get_pascal_labels()
  20. self.test_loader, self.nclass= make_data_loader(args)
  21. #Define model
  22. model = DeepLab(num_classes=self.nclass,
  23. backbone=args.backbone,
  24. output_stride=args.out_stride,
  25. sync_bn=False,
  26. freeze_bn=False)
  27. self.model = model
  28. device = torch.device('cpu')
  29. checkpoint = torch.load(args.model, map_location=device)
  30. self.model.load_state_dict(checkpoint['state_dict'])
  31. self.evaluator = Evaluator(self.nclass)
  32. #--dataset pascal --backbone resnet --out_stride 16 --crop_size 513 --model /home/spple/paddle/DeepGlint/deepglint-adv/pytorch-deeplab-xception/checkpoint-gray/model_best.pth.tar --save_path /home/spple/paddle/DeepGlint/deepglint-adv/pytorch-deeplab-xception/prediction_gray
  33. # --dataset pascal --backbone resnet --out_stride 16 --crop_size 513 --model /home/spple/paddle/DeepGlint/deepglint-adv/pytorch-deeplab-xception/checkpoint/checkpoint.pth.tar --save_path /home/spple/paddle/DeepGlint/deepglint-adv/pytorch-deeplab-xception/prediction
  34. def save_image(self, array, id, op, oriimg=None, image111=None):
  35. import cv2
  36. text = 'gt'
  37. if op == 0:
  38. text = 'pred'
  39. file_name = str(id)+'_'+text+'.png'
  40. drow_ori_name = str(id)+'_'+'vis'+'.png'
  41. #513*513
  42. r = array.copy()
  43. g = array.copy()
  44. b = array.copy()
  45. if oriimg is True:
  46. oneimgpath = str(id) + '.jpg'
  47. from mypath import Path
  48. #JPEGImages_gray
  49. image111 = image111.data.cpu().numpy()
  50. image111 = image111[0, :]
  51. image111 = image111.transpose(1,2,0)
  52. oneimg = image111
  53. for i in range(self.nclass):
  54. r[array == i] = self.color_map[i][2]
  55. g[array == i] = self.color_map[i][1]
  56. b[array == i] = self.color_map[i][0]
  57. #513*513*3
  58. rgb = np.dstack((r, g, b))
  59. hh,ww,_ = rgb.shape
  60. #if oriimg is True:
  61. #oneimg = oneimg.resize((hh, ww), Image.ANTIALIAS)
  62. # 原图
  63. #image1 = cv2.cvtColor(oneimg, cv2.COLOR_RGB2BGR)
  64. #oneimg.save(self.args.save_path + os.sep + ori_name, quality=100)
  65. #cv2.imwrite(self.args.save_path + os.sep + ori_name, image1)
  66. #----gt ---- pred
  67. cv2.imwrite(self.args.save_path+os.sep+file_name, rgb)
  68. #save_img = Image.fromarray(rgb.astype('uint8'))
  69. # pred
  70. #save_img.save(self.args.save_path+os.sep+file_name, quality=100)
  71. #oneimg = oneimg.transpose(2, 0, 1)
  72. if oriimg is True:
  73. #oneimg = np.array(oneimg)
  74. for i in range(self.nclass):
  75. if i != 0:
  76. index = np.argwhere(array == i)
  77. for key in index:
  78. oneimg[key[0]][key[1]][0] = oneimg[key[0]][key[1]][0] * 0.5 + self.color_map[i][0] * 0.5
  79. oneimg[key[0]][key[1]][1] = oneimg[key[0]][key[1]][1] * 0.5 + self.color_map[i][1] * 0.5
  80. oneimg[key[0]][key[1]][2] = oneimg[key[0]][key[1]][2] * 0.5 + self.color_map[i][2] * 0.5
  81. #img_show[mask] = img_show[mask] * 0.5 + color_mask * 0.5
  82. #oneimg = Image.fromarray(oneimg.astype('uint8'))
  83. #可视化
  84. oneimg = cv2.cvtColor(oneimg, cv2.COLOR_RGB2BGR)
  85. #oneimg.save(self.args.save_path + os.sep + ori_name, quality=100)
  86. cv2.imwrite(self.args.save_path + os.sep + drow_ori_name, oneimg)
  87. #oneimg.save(self.args.save_path+os.sep+drow_ori_name, quality=100)
  88. def test(self):
  89. self.model.eval()
  90. self.evaluator.reset()
  91. # tbar = tqdm(self.test_loader, desc='\r')
  92. num = len(self.test_loader)
  93. for i, sample in enumerate(self.test_loader):
  94. image, target = sample['image'], sample['label']
  95. print(i,"/",num)
  96. torch.cuda.synchronize()
  97. start = time.time()
  98. with torch.no_grad():
  99. output = self.model(image)
  100. end = time.time()
  101. times = (end - start) * 1000
  102. print(times, "ms")
  103. torch.cuda.synchronize()
  104. pred = output.data.cpu().numpy()
  105. target = target.cpu().numpy()
  106. image1 = image.data.cpu().numpy()
  107. # #target1 = target.cpu().numpy()
  108. image1 = image1[0, :]
  109. target1 = target[0, :]
  110. # #image1.reshape([image1.size[1],image1.size[2],image1.size[3]])
  111. # #target1.reshape([image1.size[1],image1.size[2],image1.size[3]])
  112. image1 = image1.transpose(1,2,0)
  113. # #target1 = target1.transpose(2,1,0)
  114. # import cv2
  115. # image1 = cv2.cvtColor(image1, cv2.COLOR_RGB2BGR)
  116. # import cv2
  117. # cv2.imwrite("./image1.jpg",image1)
  118. cv2.imwrite("./target111.jpg", target1)
  119. pred = np.argmax(pred, axis=1)
  120. self.save_image(pred[0], i, 0, True, sample['ori_image'])
  121. self.save_image(target[0], i, 1, None, sample['ori_image'])
  122. self.evaluator.add_batch(target, pred)
  123. Acc = self.evaluator.Pixel_Accuracy()
  124. Acc_class = self.evaluator.Pixel_Accuracy_Class()
  125. mIoU = self.evaluator.Mean_Intersection_over_Union()
  126. print('Acc:{}, Acc_class:{}, mIoU:{}'.format(Acc, Acc_class, mIoU))
  127. def main():
  128. # import cv2
  129. # cvimg = cv2.imread("./dog.jpg")
  130. # graycvimg = cv2.cvtColor(cvimg, cv2.COLOR_BGR2GRAY)
  131. # cv2.imwrite("./dog_gray.jpg", graycvimg)
  132. # graycvimg_bgr = cv2.cvtColor(graycvimg, cv2.COLOR_GRAY2BGR)
  133. # cv2.imwrite("./dog_gray_bgr.jpg", graycvimg_bgr)
  134. parser = argparse.ArgumentParser(description='Pytorch DeeplabV3Plus Test your data')
  135. parser.add_argument('--test', action='store_true', default=True,
  136. help='test your data')
  137. parser.add_argument('--dataset', default='pascal',
  138. help='datset format')
  139. parser.add_argument('--backbone', default='xception',
  140. help='what is your network backbone')
  141. parser.add_argument('--out_stride', type=int, default=16,
  142. help='output stride')
  143. parser.add_argument('--crop_size', type=int, default=513,
  144. help='image size')
  145. parser.add_argument('--model', type=str, default='/Users/jaeminjung/develop/aidentify/MoE_ws/result/cheonan_24/model_best.pth.tar',
  146. help='load your model')
  147. parser.add_argument('--save_path', type=str, default='/Users/jaeminjung/develop/aidentify/MoE_ws/result/20191001_img',
  148. help='save your prediction data')
  149. args = parser.parse_args()
  150. if args.test:
  151. tester = Tester(args)
  152. tester.test()
  153. if __name__ == "__main__":
  154. main()

我们不测试val,直接生成test的预测图:

  1. import argparse
  2. import os
  3. import numpy as np
  4. import tqdm
  5. import torch
  6. from PIL import Image
  7. from dataloaders import make_data_loader
  8. from modeling.deeplab import *
  9. from dataloaders.utils import get_pascal_labels
  10. from utils.metrics import Evaluator
  11. class Tester(object):
  12. def __init__(self, args):
  13. if not os.path.isfile(args.model):
  14. raise RuntimeError("no checkpoint found at '{}'".fromat(args.model))
  15. self.args = args
  16. self.color_map = get_pascal_labels()
  17. self.nclass = 2
  18. # Define model
  19. model = DeepLab(num_classes=self.nclass,
  20. backbone=args.backbone,
  21. output_stride=args.out_stride,
  22. sync_bn=False,
  23. freeze_bn=False)
  24. self.model = model
  25. device = torch.device('cpu')
  26. checkpoint = torch.load(args.model, map_location=device)
  27. self.model.load_state_dict(checkpoint['state_dict'])
  28. def save_image(self, imgarray, array, id, op):
  29. text = 'gt'
  30. if op == 0:
  31. text = 'pred'
  32. file_name = str(id) + '_' + text + '.png'
  33. # r = array.copy()
  34. # g = array.copy()
  35. # b = array.copy()
  36. # for i in range(self.nclass):
  37. # r[array == i] = self.color_map[i][0]
  38. # g[array == i] = self.color_map[i][1]
  39. # b[array == i] = self.color_map[i][2]
  40. # rgb = np.dstack((r, g, b))
  41. #tensor 转换为 numpy
  42. numpyimg = imgarray.numpy()
  43. #numpy 转换为 IPL格式
  44. IPLimage = numpyimg.transpose((1, 2, 0))
  45. '''
  46. IPL转换为tensor
  47. _img = Image.open(os.path.join(self.img_dir, path)).convert('RGB')
  48. img = np.array(img).astype(np.float32).transpose((2, 0, 1))
  49. img = torch.from_numpy(img).float()
  50. img = img.cuda()
  51. tensor转换为IPL
  52. image1 = image.data.cpu().numpy()
  53. IPLimage = numpyimg.transpose((1, 2, 0))
  54. save_img = Image.fromarray(IPLimage.astype('uint8'))
  55. '''
  56. for i in range(self.nclass):
  57. if i != 0:
  58. index = np.argwhere(array == i)
  59. for key in index:
  60. IPLimage[key[0]][key[1]][0] = IPLimage[key[0]][key[1]][0] * 0.5 + self.color_map[i][0] * 0.5
  61. IPLimage[key[0]][key[1]][1] = IPLimage[key[0]][key[1]][1] * 0.5 + self.color_map[i][1] * 0.5
  62. IPLimage[key[0]][key[1]][2] = IPLimage[key[0]][key[1]][2] * 0.5 + self.color_map[i][2] * 0.5
  63. save_img = Image.fromarray(IPLimage.astype('uint8'))
  64. save_img.save(self.args.save_path + os.sep + file_name)
  65. def transform_val(self, sample):
  66. from torchvision import transforms
  67. from dataloaders import custom_transforms as tr
  68. composed_transforms = transforms.Compose([
  69. tr.FixScaleCrop(crop_size=self.args.crop_size),
  70. tr.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)),
  71. tr.ToTensor()
  72. ])
  73. return composed_transforms(sample)
  74. def test(self):
  75. self.model.eval()
  76. from PIL import Image
  77. file = open('./test_marker.txt', 'r')
  78. newpath = "/media/spple/新加卷/Dataset/data/marker_data/marker20191021/all/"
  79. text_lines = file.readlines()
  80. for i in range(len(text_lines)):
  81. namename = text_lines[i].replace("\n", "")
  82. namename = namename.replace("\t", "")
  83. imgsname = newpath + namename
  84. img = Image.open(imgsname).convert('RGB')
  85. imglabel = Image.open(imgsname).convert('P')
  86. #arrayimg = np.array(img).astype(np.float32)
  87. #transposeimg = arrayimg.transpose((2, 0, 1))
  88. sample = {'image': img, 'label': imglabel, 'ori_image': img, 'path': None}
  89. imgdist = self.transform_val(sample)
  90. image = imgdist['image']
  91. ori_image = imgdist['ori_image']
  92. image = image.unsqueeze(0)
  93. with torch.no_grad():
  94. output = self.model(image)
  95. pred = output.data.cpu().numpy()
  96. pred = np.argmax(pred, axis=1)
  97. self.save_image(ori_image, pred[0], namename.split(".jpg")[0], 0)
  98. def main():
  99. parser = argparse.ArgumentParser(description='Pytorch DeeplabV3Plus Test your data')
  100. parser.add_argument('--test', action='store_true', default=True,
  101. help='test your data')
  102. parser.add_argument('--dataset', default='pascal',
  103. help='datset format')
  104. parser.add_argument('--backbone', default='xception',
  105. help='what is your network backbone')
  106. parser.add_argument('--out_stride', type=int, default=16,
  107. help='output stride')
  108. parser.add_argument('--crop_size', type=int, default=513,
  109. help='image size')
  110. parser.add_argument('--model', type=str, default='',
  111. help='load your model')
  112. parser.add_argument('--save_path', type=str, default='',
  113. help='save your prediction data')
  114. args = parser.parse_args()
  115. if args.test:
  116. tester = Tester(args)
  117. tester.test()
  118. if __name__ == "__main__":
  119. main()

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/358102
推荐阅读
相关标签
  

闽ICP备14008679号