def __getitem__(self, index): img_path = self.img_paths[index] img = np.array(Image.open(img_path)) if self.split == 'test': # Resize (Scale & Pad & Crop) if self.net_type == 'unet': img = minmax_normalize(img) img = meanstd_normalize(img, mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) else: img = minmax_normalize(img, norm_range=(-1, 1)) if self.resizer: resized = self.resizer(image=img) img = resized['image'] img = img.transpose(2, 0, 1) img = torch.FloatTensor(img) return img else: lbl_path = self.lbl_paths[index] lbl = np.array(Image.open(lbl_path)) # [y][x][rgb] numpy lbl = self.encode_one_hot(lbl) #index = [] #lbl = self.encode_mask(lbl) # ImageAugment (RandomBrightness, AddNoise...) if self.image_augmenter: augmented = self.image_augmenter(image=img) # Apply img = augmented['image'] # Resize (Scale & Pad & Crop) if self.net_type == 'unet': img = minmax_normalize(img) img = meanstd_normalize(img, mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) else: img = minmax_normalize(img, norm_range=(-1, 1)) if self.resizer: resized = self.resizer(image=img, mask=lbl) # Apply_to_mask img, lbl = resized['image'], resized['mask'] # AffineAugment (Horizontal Flip, Rotate...) if self.affine_augmenter: augmented = self.affine_augmenter(image=img, mask=lbl) # Apply_to_mask img, lbl = augmented['image'], augmented['mask'] #for (c, r), value in np.ndenumerate(lbl): # if value not in index: # index.append(value) #print("Lables without duplicates : ", index) # --> [0, 1, 2] or [0, 1] or [0, 2] or [0] if self.debug: print(lbl_path) print(np.unique(lbl)) else: img = img.transpose(2, 0, 1) img = torch.FloatTensor(img) lbl = torch.LongTensor(lbl) #print("Lbl in __get_item_ is", lbl.size()) return img, lbl
def __getitem__(self, index): img_path = self.img_paths[index] img = np.array(Image.open(img_path)) if self.split == 'test': # Resize (Scale & Pad & Crop) if self.net_type == 'unet': img = minmax_normalize(img) img = meanstd_normalize(img, mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) else: img = minmax_normalize(img, norm_range=(-1, 1)) if self.resizer: resized = self.resizer(image=img) img = resized['image'] img = img.transpose(2, 0, 1) img = torch.FloatTensor(img) return img else: lbl_path = self.lbl_paths[index] lbl = np.array(Image.open(lbl_path)) index = [] lbl = self.encode_mask(lbl) # ImageAugment (RandomBrightness, AddNoise...) if self.image_augmenter: augmented = self.image_augmenter(image=img) # Apply img = augmented['image'] # Resize (Scale & Pad & Crop) if self.net_type == 'unet': img = minmax_normalize(img) img = meanstd_normalize(img, mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) else: img = minmax_normalize(img, norm_range=(-1, 1)) if self.resizer: resized = self.resizer(image=img, mask=lbl) # Apply_to_mask img, lbl = resized['image'], resized['mask'] # AffineAugment (Horizontal Flip, Rotate...) if self.affine_augmenter: augmented = self.affine_augmenter(image=img, mask=lbl) # Apply_to_mask img, lbl = augmented['image'], augmented['mask'] if self.debug: print(lbl_path) print(np.unique(lbl)) else: img = img.transpose(2, 0, 1) img = torch.FloatTensor(img) lbl = torch.LongTensor(lbl) #print("Lbl in __get_item_ is", lbl.size()) return img, lbl
def __getitem__(self, index): img_path = self.img_paths[index] img = np.array(Image.open(img_path)) if True: # (shiyu) always load labels to locate the pixels belonging to the null class # if self.split == 'test': # Resize (Scale & Pad & Crop) if self.net_type == 'unet': img = minmax_normalize(img) img = meanstd_normalize(img, mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) else: img = minmax_normalize(img, norm_range=(-1, 1)) if self.resizer: resized = self.resizer(image=img) img = resized['image'] img = img.transpose(2, 0, 1) img = torch.FloatTensor(img) return {'img': img, 'img_path': str(img_path)} else: lbl_path = self.lbl_paths[index] lbl = np.array(Image.open(lbl_path)) lbl = self.encode_mask(lbl) # ImageAugment (RandomBrightness, AddNoise...) if self.image_augmenter: augmented = self.image_augmenter(image=img) img = augmented['image'] # Resize (Scale & Pad & Crop) if self.net_type == 'unet': img = minmax_normalize(img) img = meanstd_normalize(img, mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) else: img = minmax_normalize(img, norm_range=(-1, 1)) if self.resizer: resized = self.resizer(image=img, mask=lbl) img, lbl = resized['image'], resized['mask'] # AffineAugment (Horizontal Flip, Rotate...) if self.affine_augmenter: augmented = self.affine_augmenter(image=img, mask=lbl) img, lbl = augmented['image'], augmented['mask'] if self.debug: print(lbl_path) print(np.unique(lbl)) else: img = img.transpose(2, 0, 1) img = torch.FloatTensor(img) lbl = torch.LongTensor(lbl) return {'img': img, 'lbl': lbl, 'img_path': str(img_path)}
def __getitem__(self, index): img_path = self.img_paths[index] img = np.array(Image.open(img_path)) if self.split == 'test': # Resize (Scale & Pad & Crop) if self.net_type == 'unet': img = minmax_normalize(img) img = meanstd_normalize(img, mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) else: img = minmax_normalize(img, norm_range=(-1, 1)) if self.resizer: resized = self.resizer(image=img) img = resized['image'] img = img.transpose(2, 0, 1) img = torch.FloatTensor(img) return img, img_path.stem else: # lbl_path = self.lbl_paths[index] # lbl = np.array(Image.open(lbl_path)) # lbl = self.encode_mask(lbl) # ImageAugment (RandomBrightness, AddNoise...) if self.image_augmenter: augmented = self.image_augmenter(image=img) img = augmented['image'] # Resize (Scale & Pad & Crop) if self.net_type == 'unet': img = minmax_normalize(img) img = meanstd_normalize(img, mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) else: img = minmax_normalize(img, norm_range=(-1, 1)) if self.resizer: resized = self.resizer(image=img) img = resized['image'] # AffineAugment (Horizontal Flip, Rotate...) if self.affine_augmenter: augmented = self.affine_augmenter(image=img) img = augmented['image'] if self.debug: print() else: img = img.transpose(2, 0, 1) img = torch.FloatTensor(img) return img, img_path.stem
def infer_image_by_path( self, image_path='/home/ubuntu/data/Segmentation/pytorch-segmentation/test1.jpg', output_name='single_test_output', display=False): """ Opens image from fs and passes it through the loaded network, then displays and saves the result. :param output_name: Output image name :param display: Display images in windows or not :param image_path: Path of input images :return: null """ if not self.dataset == 'deepglobe': print( '[ERROR] Inference script only available for the Deepglobe dataset.' ) exit(-1) print('[Tester] [Single test] Opening image ' + image_path + '...') # Open and prepare image input_img = Image.open(image_path) if display: input_img.show() custom_img = np.array(input_img) custom_img = minmax_normalize(custom_img, norm_range=(-1, 1)) custom_img = custom_img.transpose(2, 0, 1) custom_img = torch.FloatTensor([custom_img]) print('[Tester] [Single test] Inferring image...') self.model.eval().to(self.device) with torch.no_grad(): # Send to GPU, infer and collect custom_img = custom_img.to(self.device) #preds = self.model.tta(custom_img, net_type='deeplab') preds = self.model.tta(custom_img, net_type='unet') preds = preds.argmax(dim=1) preds_np = preds.detach().cpu().numpy() print('[Tester] [Single test] Processing result...') good_preds = preds_np[0] good_mask = Image.fromarray(good_preds.astype('uint8'), 'P') # Transform mask to set good indexes and palette good_mask = DeepGlobeDataset.index_to_palette(good_mask) if display: good_mask.show() good_mask.save(output_name + '_prediction.png') overlay = Tester.make_overlay(good_mask, input_img, 100) if display: overlay.show() overlay.save(output_name + '_overlay.png') print('[Tester] [Single test] Done.')
def __getitem__(self, index): img_path = self.img_paths[index] img = Image.open(img_path) img = img.resize(self.resize_size) img = np.array(img) img = minmax_normalize(img, norm_range=(-1, 1)) img = img.transpose(2, 0, 1) img = torch.FloatTensor(img) name = os.path.splitext(os.path.basename(img_path))[0] return img, name
matplotlib.use('Agg') import matplotlib.pyplot as plt from utils.custum_aug import Rotate affine_augmenter = albu.Compose([albu.HorizontalFlip(p=.5), # Rotate(5, p=.5) ]) # image_augmenter = albu.Compose([albu.GaussNoise(p=.5), # albu.RandomBrightnessContrast(p=.5)]) image_augmenter = None dataset = CityscapesDataset(split='train', net_type='deeplab', ignore_index=19, debug=True, affine_augmenter=affine_augmenter, image_augmenter=image_augmenter) dataloader = DataLoader(dataset, batch_size=8, shuffle=True) print(len(dataset)) for i, batched in enumerate(dataloader): images, labels = batched if i == 0: fig, axes = plt.subplots(8, 2, figsize=(20, 48)) plt.tight_layout() for j in range(8): axes[j][0].imshow(minmax_normalize(images[j], norm_range=(0, 1), orig_range=(-1, 1))) axes[j][1].imshow(labels[j]) axes[j][0].set_xticks([]) axes[j][0].set_yticks([]) axes[j][1].set_xticks([]) axes[j][1].set_yticks([]) plt.savefig('dataset/cityscapes.png') plt.close() break
with open(str(Path(input_path) / 'ImageSets' / 'Segmentation' / 'val.txt')) as f: filenames = f.readlines() target_files = list( map(lambda x: Path(input_path) / 'JPEGImages' / (x.strip() + '.jpg'), filenames)) areas = [] for img_file in target_files: print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())), f'处理图片{img_file.name}') img = Image.open(img_file) width, height = img.size img = img.resize(target_size[::-1]) img = np.array(img) if net_type == 'unet': img = minmax_normalize(img) img = meanstd_normalize(img, mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) else: img = minmax_normalize(img, norm_range=(-1, 1)) img = np.transpose(img, (2, 0, 1)) img = np.expand_dims(img, axis=0) img = torch.FloatTensor(img) with torch.no_grad(): t = time.time() pred_np = predict(img) times.append(time.time() - t) pred_np = np.array(Image.fromarray(pred_np[0]).resize((width, height))) # pred_np = cv2.morphologyEx(pred_np, cv2.MORPH_OPEN, kernel, iterations=1)
while cap.isOpened(): ret, frame = cap.read() if ret: #cv2.imshow("frame", frame) with torch.no_grad(): # no backward pass, meaning no need to learn # anything, but only prediction required # Resizing image into 256x256, hence accelerate throughput resized = resizer(image=frame) frame = resized['image'] #print(frame) # Transformation of an OpenCV image into a PyTorch tensor img = np.array(frame) img = minmax_normalize(img, norm_range=(-1, 1)) # network internal weights range from [-1, 1] img = img.transpose(2, 0, 1) # The transpose part # OpenCV images (hegight, width, channels) # PyTorch tensor (channels, height, width) # in this case (256, 256, 3) -> (3, 256, 256) img = [img] # Specify the batch size # For test, one sample in a batch # (3, 256, 256) -> (1, 3, 256, 256) image = torch.FloatTensor(img) # tensor of float data type image = image.to(device) # let gpu calculate afterwards # Inference time measure prev = datetime.datetime.now() # Prediction using DNN (deeplabv3 plus) pred = model.tta(image, net_type='deeplab') # forward pass (network predction)
images = np.concatenate(images_list) labels = np.concatenate(labels_list) preds = np.concatenate(preds_list) # Ignore index ignore_pixel = labels == 255 preds[ignore_pixel] = 0 labels[ignore_pixel] = 0 # Plot fig, axes = plt.subplots(4, 3, figsize=(12, 10)) plt.tight_layout() axes[0, 0].set_title('input image') axes[0, 1].set_title('prediction') axes[0, 2].set_title('ground truth') for ax, img, lbl, pred in zip(axes, images, labels, preds): ax[0].imshow(minmax_normalize(img, norm_range=(0, 1), orig_range=(-1, 1))) ax[1].imshow(pred) ax[2].imshow(lbl) ax[0].set_xticks([]) ax[0].set_yticks([]) ax[1].set_xticks([]) ax[1].set_yticks([]) ax[2].set_xticks([]) ax[2].set_yticks([]) plt.savefig('eval.png') plt.close()
axes[0, 0].set_title('input image') axes[0, 1].set_title('prediction') axes[0, 2].set_title('ground truth') for ax, img, lbl, pred in zip(axes, images[start:start + num], labels[start:start + num], preds[start:start + num]): if net_type == 'unet': mean = np.asarray([0.485, 0.456, 0.406]) std = np.asarray([0.229, 0.224, 0.225]) img = img * std + mean img = np.clip(img, 0., 1.) else: img = minmax_normalize(img, norm_range=(0, 1), orig_range=(-1, 1)) ax[0].imshow(img) ax[1].imshow(pred) ax[2].imshow(lbl) ax[0].set_xticks([]) ax[0].set_yticks([]) ax[1].set_xticks([]) ax[1].set_yticks([]) ax[2].set_xticks([]) ax[2].set_yticks([]) (log_dir / 'eval_vis').mkdir(exist_ok=True, parents=True) plt.savefig( str(log_dir / 'eval_vis' / f'{i_epoch:04d}_{valid_iou:.4f}_{valid_fp}{"_best" if best_metrics == valid_iou else ""}_{i:03d}.png'
debug=True, affine_augmenter=affine_augmenter, image_augmenter=image_augmenter, target_size=(1280, 1280)) dataloader = DataLoader(dataset, batch_size=8, shuffle=True) palette = np.array([ [0, 0, 0], # black [255, 0, 0], # red [0, 0, 255], # blue [255, 255, 0] ]) # yellow for i, batched in enumerate(dataloader): images, labels = batched if i == 0: fig, axes = plt.subplots(8, 2, figsize=(20, 48)) plt.tight_layout() for j in range(8): axes[j][0].imshow( minmax_normalize(images[j], norm_range=(0, 1), orig_range=(-1, 1))) axes[j][1].imshow(palette[labels[j]]) axes[j][0].set_xticks([]) axes[j][0].set_yticks([]) axes[j][1].set_xticks([]) axes[j][1].set_yticks([]) plt.savefig('dataset/bdd100k.png') plt.close() break
def infer_image_by_name(self, image_name='255876', output_name='single_test_output', display=True): """ Opens image from fs and passes it through the loaded network, then displays and saves the result. :param output_name: Output image name :param display: Display images in windows or not :param image_path: Path of input images :return: null """ if not self.dataset == 'deepglobe': print( '[ERROR] Inference script only available for the Deepglobe dataset.' ) exit(-1) print('[Tester] [Single test] Opening image ' + image_name + '...') # Open and prepare image input_img = Image.open( '/home/ubuntu/data/Segmentation/pytorch-segmentation/data/deepglobe_as_pascalvoc/VOCdevkit/VOC2012/JPEGImages/' + image_name + '.jpg') label = Image.open( '/home/ubuntu/data/Segmentation/pytorch-segmentation/data/deepglobe_as_pascalvoc/VOCdevkit/VOC2012/SegmentationClass/' + image_name + '.png') label_raw = copy.deepcopy(label) overlay_ground_truth = Tester.make_overlay(label_raw, input_img, 100) label = label.convert('P', palette=Image.WEB) if display: input_img.show(title='Input raw image') label.show(title='Ground truth') overlay_ground_truth.show(title='Overlay_ground_truth') custom_img = np.array(input_img) custom_img = minmax_normalize(custom_img, norm_range=(-1, 1)) custom_img = custom_img.transpose(2, 0, 1) custom_img = torch.FloatTensor([custom_img]) print('[Tester] [Single test] Inferring image...') self.model.eval() with torch.no_grad(): # Send to GPU, infer and collect custom_img = custom_img.to(self.device) preds = self.model.tta(custom_img, net_type='deeplab') preds = preds.argmax(dim=1) preds_np = preds.detach().cpu().numpy() print('[Tester] [Single test] Processing result...') good_preds = preds_np[0] good_mask = Image.fromarray(good_preds.astype('uint8'), 'P') # Transform mask to set good indexes and palette good_mask = DeepGlobeDataset.index_to_palette(good_mask) overlay = Tester.make_overlay(good_mask, input_img, 100) if display: good_mask.show(title='Prediction') overlay.show(title='Overlay') good_mask.save(output_name + '_prediction.png') overlay.save(output_name + '_overlay.png') overlay_ground_truth.save(output_name + '_overlay_truth.png') print('[Tester] [Single test] Done.')
images = np.concatenate(images_list) labels = np.concatenate(labels_list) preds = np.concatenate(preds_list) # Ignore index ignore_pixel = labels == 255 preds[ignore_pixel] = 0 labels[ignore_pixel] = 0 # Plot fig, axes = plt.subplots(4, 3, figsize=(12, 10)) plt.tight_layout() axes[0, 0].set_title('input image') axes[0, 1].set_title('prediction') axes[0, 2].set_title('ground truth') for ax, img, lbl, pred in zip(axes, images, labels, preds): ax[0].imshow(minmax_normalize(img)) ax[1].imshow(pred) ax[2].imshow(lbl) ax[0].set_xticks([]) ax[0].set_yticks([]) ax[1].set_xticks([]) ax[1].set_yticks([]) ax[2].set_xticks([]) ax[2].set_yticks([]) plt.savefig('eval.png') plt.close()
def make_demo_image(self): """ Picks 4 images from dataset randomly and creates image with raw, inferred and label pictures. :return: null """ images_list = [] labels_list = [] preds_list = [] print('[Tester] [Demo] Gathering images and inferring...') self.model.eval() with torch.no_grad(): for batched in self.valid_loader: images, labels = batched images_np = images.numpy().transpose(0, 2, 3, 1) labels_np = labels.numpy() images, labels = images.to(self.device), labels.to(self.device) preds = self.model.tta(images, net_type='deeplab') preds = preds.argmax(dim=1) preds_np = preds.detach().cpu().numpy() images_list.append(images_np) labels_list.append(labels_np) preds_list.append(preds_np) if len(images_list) == 4: break print('[Tester] [Demo] Processing results...') images = np.concatenate(images_list) labels = np.concatenate(labels_list) preds = np.concatenate(preds_list) # Ignore index ignore_pixel = labels == 255 preds[ignore_pixel] = 0 labels[ignore_pixel] = 0 # Plot fig, axes = plt.subplots(4, 3, figsize=(12, 10)) plt.tight_layout() axes[0, 0].set_title('input image') axes[0, 1].set_title('prediction') axes[0, 2].set_title('ground truth') for ax, img, lbl, pred in zip(axes, images, labels, preds): ax[0].imshow( minmax_normalize(img, norm_range=(0, 1), orig_range=(-1, 1))) ax[1].imshow(pred) ax[2].imshow(lbl) ax[0].set_xticks([]) ax[0].set_yticks([]) ax[1].set_xticks([]) ax[1].set_yticks([]) ax[2].set_xticks([]) ax[2].set_yticks([]) plt.savefig('eval.png') plt.show() plt.close()
f'处理图片{img_file.name}') try: img = Image.open(img_file) sub_imgs = [] width, height = img.size parts = 2 sub_width, sub_height = width // parts, height // parts for i in range(parts): for j in range(parts): sub_img = img.crop( (sub_width * i, sub_height * j, sub_width * (i + 1), sub_height * (j + 1))) sub_img = sub_img.resize(target_size[::-1]) sub_img = np.array(sub_img) if net_type == 'unet': sub_img = minmax_normalize(sub_img) sub_img = meanstd_normalize(sub_img, mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) else: sub_img = minmax_normalize(sub_img, norm_range=(-1, 1)) sub_img = np.transpose(sub_img, (2, 0, 1)) sub_imgs.append(sub_img) sub_imgs = np.stack(sub_imgs) sub_imgs = torch.FloatTensor(sub_imgs) with torch.no_grad(): preds_np = predict(sub_imgs) result = np.ndarray((sub_height * parts, sub_width * parts), dtype=np.uint8) for i in range(parts): for j in range(parts):
def __getitem__(self, index): img_path = self.img_paths[index] # For debug # print('[LOADER] Got image ' + str(img_path)) img = np.array(Image.open(img_path)) if self.split == 'test': # Resize (Scale & Pad & Crop) if self.net_type == 'unet': img = minmax_normalize(img) img = meanstd_normalize(img, mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) else: img = minmax_normalize(img, norm_range=(-1, 1)) if self.resizer: resized = self.resizer(image=img) img = resized['image'] img = img.transpose(2, 0, 1) img = torch.FloatTensor(img) return img else: lbl_path = self.lbl_paths[index] inter_img = Image.open(lbl_path) # Convert the RGB images to the P mode in PIL # inter_img = inter_img.convert('P', palette=Image.ADAPTIVE, colors=256) C'EST DE LA MERDE inter_img = inter_img.convert('P', palette=Image.WEB) self.palette_to_indexes(inter_img) lbl = np.array(inter_img) lbl[lbl == 255] = 0 # ImageAugment (RandomBrightness, AddNoise...) if self.image_augmenter: augmented = self.image_augmenter(image=img) img = augmented['image'] # Resize (Scale & Pad & Crop) if self.net_type == 'unet': img = minmax_normalize(img) img = meanstd_normalize(img, mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) else: img = minmax_normalize(img, norm_range=(-1, 1)) if self.resizer: resized = self.resizer(image=img, mask=lbl) img, lbl = resized['image'], resized['mask'] # AffineAugment (Horizontal Flip, Rotate...) if self.affine_augmenter: augmented = self.affine_augmenter(image=img, mask=lbl) img, lbl = augmented['image'], augmented['mask'] if self.debug: print(lbl_path) print(lbl.shape) print(np.unique(lbl)) else: img = img.transpose(2, 0, 1) img = torch.FloatTensor(img) lbl = torch.LongTensor(lbl) return img, lbl