def load_unlabeled_data(tar, data_dir='dataset/'): from torchvision import transforms folder_tar = data_dir + tar + '/images' transform = { 'weak': transforms.Compose([ transforms.Resize([224, 224]), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]), 'strong': transforms.Compose([ transforms.Resize([256, 256]), transforms.RandomCrop(224), transforms.RandomHorizontalFlip(), RandAugment( ), # Paper: RandAugment: Practical data augmentation with no separate search transforms.ToTensor(), # Cutout(size=16), # (https://github.com/uoguelph-mlrg/Cutout/blob/master/util/cutout.py) transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) } unlabeled_weak_data = datasets.ImageFolder(root=folder_tar, transform=transform['weak']) unlabeled_strong_data = datasets.ImageFolder(root=folder_tar, transform=transform['strong']) return unlabeled_weak_data, unlabeled_strong_data
def remove_background(input_image): # Disable ssl verification (This was used to download the trained dataset) if (not os.environ.get('PYTHONHTTPSVERIFY', '') and getattr(ssl, '_create_unverified_context', None)): ssl._create_default_https_context = ssl._create_unverified_context # Defining the convolutional network with a pretrained dataset that will be used to detect the person in the image fully_convolutional_network = models.segmentation.fcn_resnet101( pretrained=True).eval() # Preprocessing the image and making it become a tensor so it can be used in the convolutional network image_to_tensor_transform = cvtransforms.Compose([ cvtransforms.ToTensor(), cvtransforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) input_tensor = image_to_tensor_transform(input_image).unsqueeze(0) # Passing the input structure through the net to get the ouput model with the person classified output_model = fully_convolutional_network(input_tensor)['out'] # Transforming the tensor into n labeled image labeled_image = torch.argmax(output_model.squeeze(), dim=0).detach().cpu().numpy() # Generating the segmentation mask to remove the background mask = segmentation_mask(labeled_image) # Removing the background leaving only the person result_image = cv2.bitwise_and(input_image, input_image, mask=mask.astype(np.uint8)) return result_image
def get_normalization_layer(mean: list, std: list, num_images: int = 1, mode: str = '2d'): """Get Z-scoring layer from config If RGB frames are stacked into tensor N, num_rgb*3, H, W, we need to repeat the mean and std num_rgb times """ # if mode == '2d': mean = mean.copy() * num_images std = std.copy() * num_images return transforms.Normalize(mean=mean, std=std)
def get_image_from_url(url=''): img_transforms = opencv_transform.Compose([ opencv_transform.Resize(int(math.ceil(160 / 0.875)), interpolation=cv2.INTER_LINEAR), opencv_transform.CenterCrop(160), opencv_transform.ToTensor(), opencv_transform.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), ]) # download the image from web if uri is valid. if (validators.url(url)): img = Image.open(requests.get(url, stream=True).raw) else: img = Image.open(url) img = img_transforms(img) return img
def load_data(src, tar, data_dir='dataset', use_cv2=False): folder_src = os.path.join(os.path.join(data_dir, src), 'images') folder_tar = os.path.join(os.path.join(data_dir, tar), 'images') if use_cv2: import cv2 from opencv_transforms import transforms def loader_opencv(path: str) -> np.ndarray: return cv2.imread(path) transform = { 'train': transforms.Compose([ transforms.Resize((256, 256), interpolation=cv2.INTER_LINEAR), transforms.RandomCrop(224), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]), 'test': transforms.Compose([ transforms.Resize((224, 224), interpolation=cv2.INTER_LINEAR), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) } source_data = datasets.ImageFolder(root=folder_src, transform=transform['train'], loader=loader_opencv) # source_data_loader = torch.utils.data.DataLoader(source_data, batch_size=batch_size, shuffle=True, **kwargs, drop_last = True) target_train_data = datasets.ImageFolder(root=folder_tar, transform=transform['train'], loader=loader_opencv) # target_train_loader = torch.utils.data.DataLoader(target_train_data, batch_size=batch_size, shuffle=True, **kwargs, drop_last = True) target_test_data = datasets.ImageFolder(root=folder_tar, transform=transform['test'], loader=loader_opencv) # target_test_loader = torch.utils.data.DataLoader(target_test_data, batch_size=batch_size, shuffle=True, **kwargs, drop_last = False) else: from torchvision import transforms transform = { 'train': transforms.Compose([ transforms.Resize((256, 256)), transforms.RandomCrop(224), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]), 'test': transforms.Compose([ transforms.Resize((224, 224)), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) } source_data = datasets.ImageFolder(root=folder_src, transform=transform['train']) # source_data_loader = torch.utils.data.DataLoader(source_data, batch_size=batch_size, shuffle=True, **kwargs, drop_last = True) target_train_data = datasets.ImageFolder(root=folder_tar, transform=transform['train']) # target_train_loader = torch.utils.data.DataLoader(target_train_data, batch_size=batch_size, shuffle=True, **kwargs, drop_last = True) target_test_data = datasets.ImageFolder(root=folder_tar, transform=transform['test']) # target_test_loader = torch.utils.data.DataLoader(target_test_data, batch_size=batch_size, shuffle=True, **kwargs, drop_last = False) return source_data, target_train_data, target_test_data
# Disable ssl verification (This was used to download the trained dataset) if (not os.environ.get('PYTHONHTTPSVERIFY', '') and getattr(ssl, '_create_unverified_context', None)): ssl._create_default_https_context = ssl._create_unverified_context # Defining the convolutional network with a pretrained dataset that will be used to detect the person in the image fully_convolutional_network = models.segmentation.fcn_resnet101( pretrained=True).eval() input_image = cv2.imread('test_images/input.jpg') # Preprocessing the image and making it become a tensor so it can be used in the convolutional network image_to_tensor_transform = cvtransforms.Compose([ cvtransforms.ToTensor(), cvtransforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) input_tensor = image_to_tensor_transform(input_image).unsqueeze(0) # Passing the input structure through the net to get the ouput model with the person classified output_model = fully_convolutional_network(input_tensor)['out'] # Transforming the tensor into n labeled image labeled_image = torch.argmax(output_model.squeeze(), dim=0).detach().cpu().numpy() # Generating the segmentation mask to remove the background mask = segmentation_mask(labeled_image) # Removing the background leaving only the person