You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

ade20k.py 2.3 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. """
  2. Copyright 2020 Tianshu AI Platform. All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. =============================================================
  13. """
  14. import collections
  15. import torch
  16. import torchvision
  17. import numpy as np
  18. from PIL import Image
  19. import os
  20. from torchvision.datasets import VisionDataset
  21. from .utils import colormap
  22. class ADE20K(VisionDataset):
  23. cmap = colormap()
  24. def __init__(
  25. self,
  26. root,
  27. split="training",
  28. transform=None,
  29. target_transform=None,
  30. transforms=None,
  31. ):
  32. super( ADE20K, self ).__init__( root=root, transforms=transforms, transform=transform, target_transform=target_transform )
  33. assert split in ['training', 'validation'], "split should be \'training\' or \'validation\'"
  34. self.root = os.path.expanduser(root)
  35. self.split = split
  36. self.num_classes = 150
  37. img_list = []
  38. lbl_list = []
  39. img_dir = os.path.join( self.root, 'images', self.split )
  40. lbl_dir = os.path.join( self.root, 'annotations', self.split )
  41. for img_name in os.listdir( img_dir ):
  42. img_list.append( os.path.join( img_dir, img_name ) )
  43. lbl_list.append( os.path.join( lbl_dir, img_name[:-3]+'png') )
  44. self.img_list = img_list
  45. self.lbl_list = lbl_list
  46. def __len__(self):
  47. return len(self.img_list)
  48. def __getitem__(self, index):
  49. img = Image.open( self.img_list[index] )
  50. lbl = Image.open( self.lbl_list[index] )
  51. if self.transforms:
  52. img, lbl = self.transforms(img, lbl)
  53. lbl = np.array(lbl, dtype='uint8')-1 # 1-150 => 0-149 + 255
  54. return img, lbl
  55. @classmethod
  56. def decode_seg_to_color(cls, mask):
  57. """decode semantic mask to RGB image"""
  58. return cls.cmap[mask+1]

一站式算法开发平台、高性能分布式深度学习框架、先进算法模型库、视觉模型炼知平台、数据可视化分析平台等一系列平台及工具,在模型高效分布式训练、数据处理和可视分析、模型炼知和轻量化等技术上形成独特优势,目前已在产学研等各领域近千家单位及个人提供AI应用赋能

Contributors (1)