def transform_imglabel(self,image,label,name): size = np.array(image, dtype=np.uint8).shape # print(name) name1 = name if name1.startswith('site1'): image = image.resize((math.ceil(2*size[1]),math.ceil(2*size[0])), Image.BICUBIC) label = label.resize((math.ceil(2*size[1]),math.ceil(2*size[0])), Image.NEAREST) image = np.asarray(image, np.float32) print(image.shape) label = self.encode_segmap(np.array(label, dtype=np.uint8)) elif name1.startswith('site2'): image = image.resize((math.ceil(2*size[1]),math.ceil(2*size[0])), Image.BICUBIC) label = label.resize((math.ceil(2*size[1]),math.ceil(2*size[0])), Image.NEAREST) image = np.asarray(image, np.float32) label = self.encode_segmap(np.array(label, dtype=np.uint8)) elif name1.startswith('site4'): image = image.resize((math.ceil(1.16*size[1]),math.ceil(1.16*size[0])), Image.BICUBIC) label = label.resize((math.ceil(1.16*size[1]),math.ceil(1.16*size[0])), Image.NEAREST) image = np.asarray(image, np.float32) label = self.encode_segmap(np.array(label, dtype=np.uint8)) elif name1.startswith('site3'): image = np.array(image, dtype=np.float32) label = self.encode_segmap(np.array(label, dtype=np.uint8)) image = np.expand_dims(image, axis=2) label = np.expand_dims(label, axis=2) img_label = np.concatenate((image, label), axis=-1) img_label = CenterCrop(img_label,128) image = img_label[..., 0] label = img_label[..., 1] label = np.squeeze(label) image = np.squeeze(image) return image,label
def transform(self, image, label, name): size = np.array(image, dtype=np.uint8).shape # print(name) name1 = name # print(name) if name1.startswith('site1') or name1.startswith('site2'): image = image.resize( (math.ceil(2 * size[1]), math.ceil(2 * size[0])), Image.BICUBIC) label = label.resize( (math.ceil(2 * size[1]), math.ceil(2 * size[0])), Image.NEAREST) image = np.asarray(image, np.float32) label = self.encode_segmap(np.array(label, dtype=np.uint8)) elif name1.startswith('site4'): image = image.resize( (math.ceil(1.16 * size[1]), math.ceil(1.16 * size[0])), Image.BICUBIC) label = label.resize( (math.ceil(1.16 * size[1]), math.ceil(1.16 * size[0])), Image.NEAREST) image = np.asarray(image, np.float32) label = self.encode_segmap(np.array(label, dtype=np.uint8)) elif name1.startswith('site3'): image = np.array(image, dtype=np.float32) label = self.encode_segmap(np.array(label, dtype=np.uint8)) label = np.expand_dims(label, axis=2) img_label = np.concatenate((image, label), axis=-1) img_label = CenterCrop(img_label, self.img_size) image = img_label[..., :3] label = img_label[..., 3] label = np.squeeze(label) image = np.squeeze(image) image = image / 255.0 image = (image - np.mean(image)) / np.std(image) image = np.transpose(image, (2, 0, 1)) label = np.expand_dims(label, axis=0) image = torch.FloatTensor(image) label = torch.FloatTensor(label) return image, label
def __getitem__(self, index): datafiles = self.files[index] image = Image.open(datafiles["img"]).convert('L') name = datafiles["name"] label = Image.open(datafiles["lbl"]).convert('L') if self.set =='test': if self.resize_and_crop == "center_crop": size = np.array(label, dtype=np.uint8).shape[1] if size <= self.crop_size: image = np.asarray(image, np.float32) label = self.encode_segmap(np.array(label, dtype=np.uint8)) else: image = np.asarray(image, np.float32) label = self.encode_segmap(np.array(label, dtype=np.uint8)) image = np.expand_dims(image, axis=2) label = np.expand_dims(label, axis=2) img_label = np.concatenate((image, label), axis=-1) img_label = CenterCrop(img_label,self.crop_size) image = img_label[..., 0] label = img_label[..., 1] label = np.squeeze(label) image = np.squeeze(image) if self.set =='train': if self.resize_and_crop == "resize": image = image.resize(self.img_size, Image.BICUBIC) image = np.asarray(image, np.float32) label = label.resize(self.img_size, Image.NEAREST) label = self.encode_segmap(np.array(label, dtype=np.uint8)) elif self.resize_and_crop == "center_crop": size = np.array(label, dtype=np.uint8).shape if size[1] <= self.crop_size: image = image.resize(self.img_size, Image.BICUBIC) label = label.resize(self.img_size, Image.NEAREST) image = np.asarray(image, np.float32) label = self.encode_segmap(np.array(label, dtype=np.uint8)) else: image = np.asarray(image, np.float32) label = self.encode_segmap(np.array(label, dtype=np.uint8)) image = np.expand_dims(image, axis=2) label = np.expand_dims(label, axis=2) img_label = np.concatenate((image, label), axis=-1) img_label = CenterCrop(img_label,self.crop_size) image = img_label[..., 0] label = img_label[..., 1] label = np.squeeze(label) image = np.squeeze(image) elif self.resize_and_crop == "resize_and_center_crop": image = image.resize(self.img_size, Image.BICUBIC) image = np.asarray(image, np.float32) label = label.resize(self.img_size, Image.NEAREST) label = self.encode_segmap(np.array(label, dtype=np.uint8)) label = np.expand_dims(label, axis=2) img_label = np.concatenate((image, label), axis=-1) image, label = CenterCrop(image, label,self.crop_size) image = img_label[..., 0] label = img_label[..., 1] label = np.squeeze(label) image = np.squeeze(image) elif self.resize_and_crop == "resize_and_random_crop": image = image.resize(self.img_size, Image.BICUBIC) image = np.asarray(image, np.float32) label = label.resize(self.img_size, Image.NEAREST) label = self.encode_segmap(np.array(label, dtype=np.uint8)) label = np.expand_dims(label, axis=2) image = np.expand_dims(image, axis=2) img_label = np.concatenate((image, label), axis=-1) img_label = RandomCrop(img_label,self.crop_size) image = img_label[..., 0] label = img_label[..., 1] label = np.squeeze(label) image = np.squeeze(image) else: image = np.asarray(image, np.float32) label = self.encode_segmap(np.array(label, dtype=np.uint8)) # normalize image image = image / 255.0 image -= np.mean(image) image = image/np.std(image) label = np.expand_dims(label, axis=0) image = np.expand_dims(image, axis=0) if self.nlabel: size = label.shape lbl = np.zeros((self.n_class,label.shape[1],label.shape[2])) for i in range(self.n_class): lbl[i] = np.array((label[0] == i), dtype=np.uint8) return image.copy(), lbl.copy(), label.copy(), size, name