StatisticDean:

Image


I am experiencing the same exact memory growing problem, however I am not using fastai library. If I read the image as a class attribute, and pass it onto class functions, e.g. _pad_img and _patch_extraction, the memory grows linearly with the iterations.
This is how I make my Dataset, which is next used by the Python DataLoader:
class MyDataset(Dataset):
        def __init__(self, patch_size, subdivisions, image_file):
            self.patch_size = patch_size
            self.subdivisions = subdivisions
            self.image = self._pad_img(cv2.cvtColor(cv2.imread(image_file), cv2.COLOR_BGR2RGB))
            self.coords = self._extract_patches(self.image)

        def _pad_img(self, img):
            aug = int(round(self.patch_size * (1 - 1.0 / self.subdivisions)))
            padding = ((aug, aug), (aug, aug), (0, 0))
            img_padded = np.pad(img, pad_width=padding, mode='reflect')
            return img_padded

        def _extract_patches(self, img):
            step = int(self.patch_size / self.subdivisions)
            row_range = range(0, img.shape[0] - self.patch_size + 1, step)
            col_range = range(0, img.shape[1] - self.patch_size + 1, step)
            coords = []
            for row in row_range:
                for col in col_range:
                    left = col
                    upper = row
                    right = col + self.patch_size
                    lower = row + self.patch_size
                    coords += [(left, upper, right, lower)]
            return coords

        def __len__(self):
            return len(self.coords)

        def __getitem__(self, idx):
            box = self.coords[idx]
            image = self.image[box[1]:box[3], box[0]:box[2]]
            return image

I have tried reading the image in _extract_patches. There is no memory problem that way, but the code becomes extremely slow.
Any ideas on how this can be fixed is appreciated.