Spaces:
Sleeping
Sleeping
File size: 7,949 Bytes
c20a1af |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
import os
import cv2
import glob
import torch
import math
import imageio
import numpy as np
from PIL import Image
from core.aff_utils import *
from tools.ai.augment_utils import *
from tools.ai.torch_utils import one_hot_embedding
from tools.general.xml_utils import read_xml
from tools.general.json_utils import read_json
from tools.dataset.voc_utils import get_color_map_dic
class Iterator:
def __init__(self, loader):
self.loader = loader
self.init()
def init(self):
self.iterator = iter(self.loader)
def get(self):
try:
data = next(self.iterator)
except StopIteration:
self.init()
data = next(self.iterator)
return data
class VOC_Dataset(torch.utils.data.Dataset):
def __init__(self, root_dir, domain, with_id=False, with_tags=False, with_mask=False):
self.root_dir = root_dir
self.image_dir = self.root_dir + 'JPEGImages/'
self.xml_dir = self.root_dir + 'Annotations/'
self.mask_dir = self.root_dir + 'SegmentationClass/'
self.image_id_list = [image_id.strip() for image_id in open('./data/%s.txt'%domain).readlines()]
self.with_id = with_id
self.with_tags = with_tags
self.with_mask = with_mask
def __len__(self):
return len(self.image_id_list)
def get_image(self, image_id):
image = Image.open(self.image_dir + image_id + '.jpg').convert('RGB')
return image
def get_mask(self, image_id):
mask_path = self.mask_dir + image_id + '.png'
if os.path.isfile(mask_path):
mask = Image.open(mask_path)
else:
mask = None
return mask
def get_tags(self, image_id):
_, tags = read_xml(self.xml_dir + image_id + '.xml')
return tags
def __getitem__(self, index):
image_id = self.image_id_list[index]
data_list = [self.get_image(image_id)]
if self.with_id:
data_list.append(image_id)
if self.with_tags:
data_list.append(self.get_tags(image_id))
if self.with_mask:
data_list.append(self.get_mask(image_id))
return data_list
class VOC_Dataset_For_Classification(VOC_Dataset):
def __init__(self, root_dir, domain, transform=None):
super().__init__(root_dir, domain, with_tags=True)
self.transform = transform
data = read_json('./data/VOC_2012.json')
self.class_dic = data['class_dic']
self.classes = data['classes']
def __getitem__(self, index):
image, tags = super().__getitem__(index)
if self.transform is not None:
image = self.transform(image)
label = one_hot_embedding([self.class_dic[tag] for tag in tags], self.classes)
return image, label
class VOC_Dataset_For_Segmentation(VOC_Dataset):
def __init__(self, root_dir, domain, transform=None):
super().__init__(root_dir, domain, with_mask=True)
self.transform = transform
cmap_dic, _, class_names = get_color_map_dic()
self.colors = np.asarray([cmap_dic[class_name] for class_name in class_names])
def __getitem__(self, index):
image, mask = super().__getitem__(index)
if self.transform is not None:
input_dic = {'image':image, 'mask':mask}
output_dic = self.transform(input_dic)
image = output_dic['image']
mask = output_dic['mask']
return image, mask
class VOC_Dataset_For_Evaluation(VOC_Dataset):
def __init__(self, root_dir, domain, transform=None):
super().__init__(root_dir, domain, with_id=True, with_mask=True)
self.transform = transform
cmap_dic, _, class_names = get_color_map_dic()
self.colors = np.asarray([cmap_dic[class_name] for class_name in class_names])
def __getitem__(self, index):
image, image_id, mask = super().__getitem__(index)
if self.transform is not None:
input_dic = {'image':image, 'mask':mask}
output_dic = self.transform(input_dic)
image = output_dic['image']
mask = output_dic['mask']
return image, image_id, mask
class VOC_Dataset_For_WSSS(VOC_Dataset):
def __init__(self, root_dir, domain, pred_dir, transform=None):
super().__init__(root_dir, domain, with_id=True)
self.pred_dir = pred_dir
self.transform = transform
cmap_dic, _, class_names = get_color_map_dic()
self.colors = np.asarray([cmap_dic[class_name] for class_name in class_names])
def __getitem__(self, index):
image, image_id = super().__getitem__(index)
mask = Image.open(self.pred_dir + image_id + '.png')
if self.transform is not None:
input_dic = {'image':image, 'mask':mask}
output_dic = self.transform(input_dic)
image = output_dic['image']
mask = output_dic['mask']
return image, mask
class VOC_Dataset_For_Testing_CAM(VOC_Dataset):
def __init__(self, root_dir, domain, transform=None):
super().__init__(root_dir, domain, with_tags=True, with_mask=True)
self.transform = transform
cmap_dic, _, class_names = get_color_map_dic()
self.colors = np.asarray([cmap_dic[class_name] for class_name in class_names])
data = read_json('./data/VOC_2012.json')
self.class_dic = data['class_dic']
self.classes = data['classes']
def __getitem__(self, index):
image, tags, mask = super().__getitem__(index)
if self.transform is not None:
input_dic = {'image':image, 'mask':mask}
output_dic = self.transform(input_dic)
image = output_dic['image']
mask = output_dic['mask']
label = one_hot_embedding([self.class_dic[tag] for tag in tags], self.classes)
return image, label, mask
class VOC_Dataset_For_Making_CAM(VOC_Dataset):
def __init__(self, root_dir, domain):
super().__init__(root_dir, domain, with_id=True, with_tags=True, with_mask=True)
cmap_dic, _, class_names = get_color_map_dic()
self.colors = np.asarray([cmap_dic[class_name] for class_name in class_names])
data = read_json('./data/VOC_2012.json')
self.class_names = np.asarray(class_names[1:21])
self.class_dic = data['class_dic']
self.classes = data['classes']
def __getitem__(self, index):
image, image_id, tags, mask = super().__getitem__(index)
label = one_hot_embedding([self.class_dic[tag] for tag in tags], self.classes)
return image, image_id, label, mask
class VOC_Dataset_For_Affinity(VOC_Dataset):
def __init__(self, root_dir, domain, path_index, label_dir, transform=None):
super().__init__(root_dir, domain, with_id=True)
data = read_json('./data/VOC_2012.json')
self.class_dic = data['class_dic']
self.classes = data['classes']
self.transform = transform
self.label_dir = label_dir
self.path_index = path_index
self.extract_aff_lab_func = GetAffinityLabelFromIndices(self.path_index.src_indices, self.path_index.dst_indices)
def __getitem__(self, idx):
image, image_id = super().__getitem__(idx)
label = imageio.imread(self.label_dir + image_id + '.png')
label = Image.fromarray(label)
output_dic = self.transform({'image':image, 'mask':label})
image, label = output_dic['image'], output_dic['mask']
return image, self.extract_aff_lab_func(label)
|