|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +import torch |
| 5 | +from os import path |
| 6 | +from scipy.misc import imread, imresize |
| 7 | +from scipy.io import loadmat |
| 8 | + |
| 9 | + |
| 10 | +class Rescale(object): |
| 11 | + """Rescale the image in a sample to a given size. |
| 12 | +
|
| 13 | + Args: |
| 14 | + output_size (int or tuple): Desired output size. If tuple, output is |
| 15 | + matched to output_size. If int, smaller of image edges is matched |
| 16 | + to output_size keeping aspect ratio the same. |
| 17 | + """ |
| 18 | + |
| 19 | + def __init__(self, output_size): |
| 20 | + assert isinstance(output_size, (int, tuple)) |
| 21 | + self.output_size = output_size |
| 22 | + |
| 23 | + def __call__(self, image, labels): |
| 24 | + assert image.shape[:2] == labels.shape |
| 25 | + |
| 26 | + h, w = image.shape[:2] |
| 27 | + if isinstance(self.output_size, int): |
| 28 | + if h > w: |
| 29 | + new_h, new_w = self.output_size * h / w, self.output_size |
| 30 | + else: |
| 31 | + new_h, new_w = self.output_size, self.output_size * w / h |
| 32 | + else: |
| 33 | + new_h, new_w = self.output_size |
| 34 | + |
| 35 | + new_h, new_w = int(new_h), int(new_w) |
| 36 | + |
| 37 | + img = imresize(image, (new_h, new_w)) |
| 38 | + lbls = imresize(labels, (new_h, new_w), interp="nearest") |
| 39 | + |
| 40 | + return (img, lbls) |
| 41 | + |
| 42 | + |
| 43 | +class RandomCrop(object): |
| 44 | + """Crop randomly the image in a sample. |
| 45 | +
|
| 46 | + Args: |
| 47 | + output_size (tuple or int): Desired output size. If int, square crop |
| 48 | + is made. |
| 49 | + """ |
| 50 | + |
| 51 | + def __init__(self, output_size): |
| 52 | + assert isinstance(output_size, (int, tuple)) |
| 53 | + if isinstance(output_size, int): |
| 54 | + self.output_size = (output_size, output_size) |
| 55 | + else: |
| 56 | + assert len(output_size) == 2 |
| 57 | + self.output_size = output_size |
| 58 | + |
| 59 | + def __call__(self, image, labels): |
| 60 | + assert image.shape[:2] == labels.shape |
| 61 | + |
| 62 | + h, w = image.shape[:2] |
| 63 | + new_h, new_w = self.output_size |
| 64 | + |
| 65 | + top = np.random.randint(0, h - new_h) |
| 66 | + left = np.random.randint(0, w - new_w) |
| 67 | + |
| 68 | + image = image[top: top + new_h, |
| 69 | + left: left + new_w] |
| 70 | + |
| 71 | + labels = labels[top: top + new_h, |
| 72 | + left: left + new_w] |
| 73 | + |
| 74 | + return (image, labels) |
| 75 | + |
| 76 | + |
| 77 | +class ToTensor(object): |
| 78 | + """Convert ndarrays in sample to Tensors.""" |
| 79 | + |
| 80 | + def __call__(self, image, labels): |
| 81 | + assert image.shape[:2] == labels.shape |
| 82 | + |
| 83 | + # swap color axis because |
| 84 | + # numpy image: H x W x C |
| 85 | + # torch image: C X H X W |
| 86 | + image = image.transpose((2, 0, 1)) |
| 87 | + return (torch.from_numpy(image), |
| 88 | + torch.from_numpy(labels)) |
| 89 | + |
| 90 | + |
| 91 | +def loadCOCO(dataset_folder): |
| 92 | + resc = Rescale(650) |
| 93 | + crop = RandomCrop(640) |
| 94 | + |
| 95 | + namespath = path.join(dataset_folder, "imageLists/train.txt") |
| 96 | + names = np.loadtxt(namespath, dtype=str, delimiter="\n") |
| 97 | + |
| 98 | + images = [] |
| 99 | + labels = [] |
| 100 | + for imgName in names: |
| 101 | + im = imread(path.join(dataset_folder, "images/"+imgName+".jpg"), mode="RGB") |
| 102 | + mat = loadmat(path.join(dataset_folder, "annotations/"+imgName+".mat")) |
| 103 | + lbl = mat["S"] |
| 104 | + |
| 105 | + im, lbl = resc(im, lbl) |
| 106 | + im, lbl = crop(im, lbl) |
| 107 | + images.append(im) |
| 108 | + labels.append(lbl) |
| 109 | + |
| 110 | + images = np.array(images, dtype='float32') |
| 111 | + images /= 255.0 # Span 0 ~ 1 |
| 112 | + images = (images*2) - 1 # Span -1 ~ 1 |
| 113 | + |
| 114 | + return (images, np.array(labels)) |
| 115 | + |
| 116 | + |
| 117 | +if __name__ == '__main__': |
| 118 | + DATASET_FOLDER = "/home/toni/Data/ssegmentation/COCO" |
| 119 | + loadCOCO(DATASET_FOLDER) |
0 commit comments