def __tile(self, img_path, mask_path, number_of_tiles=12): img = MultiImage(img_path)[-1] mask = MultiImage(mask_path)[-1] shape = img.shape pad0, pad1 = (self.__patch_size - shape[0] % self.__patch_size) % self.__patch_size, ( self.__patch_size - shape[1] % self.__patch_size) % self.__patch_size img = np.pad(img, [[pad0 // 2, pad0 - pad0 // 2], [pad1 // 2, pad1 - pad1 // 2], [0, 0]], constant_values=255) mask = np.pad(mask, [[pad0 // 2, pad0 - pad0 // 2], [pad1 // 2, pad1 - pad1 // 2], [0, 0]], constant_values=0) img = img.reshape(img.shape[0] // self.__patch_size, self.__patch_size, img.shape[1] // self.__patch_size, self.__patch_size, 3) img = img.transpose(0, 2, 1, 3, 4).reshape(-1, self.__patch_size, self.__patch_size, 3) mask = mask.reshape(mask.shape[0] // self.__patch_size, self.__patch_size, mask.shape[1] // self.__patch_size, self.__patch_size, 3) mask = mask.transpose(0, 2, 1, 3, 4).reshape(-1, self.__patch_size, self.__patch_size, 3) if len(img) < number_of_tiles: mask = np.pad( mask, [[0, number_of_tiles - len(img)], [0, 0], [0, 0], [0, 0]], constant_values=0) img = np.pad( img, [[0, number_of_tiles - len(img)], [0, 0], [0, 0], [0, 0]], constant_values=255) idxs = np.argsort(img.reshape(img.shape[0], -1).sum(-1))[:number_of_tiles] img = img[idxs] mask = mask[idxs] return img, mask