def _build_test_dataset(filepaths, input_shape=(256, 256, 3), norm=255, single_channel=False): """ Load a set of images into memory from file and mask the centers to use as a test set. :filepaths: list of strings pointing to image files :input_shape: dimensions of input images :norm: normalizing value for images Returns img_arr, mask """ img_arr = np.stack([ _load_img(f, norm=norm, num_channels=input_shape[2], resize=input_shape[:2]) for f in filepaths ]) mask = _make_test_mask(*input_shape) mask = np.stack([mask for _ in range(img_arr.shape[0])]) return img_arr, mask
def test_load_single_channel_img(test_single_channel_png_path): img_arr = _load_img(test_single_channel_png_path, resize=(32, 32), num_channels=1) assert isinstance(img_arr, np.ndarray) assert len(img_arr.shape) == 3 assert img_arr.shape[2] == 1
def _make_sprite(imfiles, norm=1, num_channels=3, resize=(50,50)): """ Input a 4D tensor, output a sprite image. assumes your pictures are all the same size and square """ num_sprites = len(imfiles) gridsize = np.int(np.ceil(np.sqrt(num_sprites))) sprite_arr = np.stack([ _load_img(f, norm, num_channels, resize) for f in imfiles ])#.astype(np.uint8) output = np.zeros((resize[0]*gridsize, resize[1]*gridsize, 3), dtype=np.uint8) for i in range(num_sprites): col = i // gridsize row = i % gridsize output[resize[0]*col:resize[0]*(col+1), resize[1]*row:resize[1]*(row+1),:] = sprite_arr[i,:,:,:3] img = Image.fromarray(output) img = img.resize((resize[0]*gridsize, resize[1]*gridsize)) return img
def _load_img(self, f): """ Wrapper for patchwork._util._load_img :f: string; path to file """ return _load_img(f, norm=self._norm, num_channels=self._num_channels, resize=self._imshape)
def _multiple_augplot(filepaths, aug_params=True, num_resamps=5, norm=255, num_channels=3, resize=None): """ """ num_files = len(filepaths) img = _load_img(filepaths[0], norm=norm, num_channels=num_channels, resize=resize) aug_func = augment_function(img.shape[:2], aug_params) plt.figure() for i in range(num_files): img = _load_img(filepaths[i], norm=norm, num_channels=num_channels, resize=resize) plt.subplot(num_files+1, num_resamps+1, 1+i*(num_resamps+1)) plt.imshow(img) plt.axis(False) if i == 0: plt.title("original") for j in range(num_resamps): plt.subplot(num_files+1, num_resamps+1, j+2+i*(num_resamps+1)) plt.imshow(aug_func(img).numpy()) plt.axis(False)
def _single_augplot(filepath, aug_params=True, norm=255, num_channels=3, resize=None): """ Input a path to an image and an augmentation function; sample 15 augmentations and display using matplotlib. """ img = _load_img(filepath, norm=norm, num_channels=num_channels, resize=resize) aug_func = augment_function(img.shape[:2], aug_params) plt.figure() plt.subplot(4,4,1) plt.imshow(img) plt.axis(False) plt.title("original") for i in range(2,17): plt.subplot(4,4,i) plt.imshow(aug_func(img).numpy()) plt.axis(False)
def test_load_and_stack_single_channel_img(test_single_channel_png_path): img_arr = _load_img(test_single_channel_png_path, num_channels=3) assert isinstance(img_arr, np.ndarray) assert len(img_arr.shape) == 3 assert img_arr.shape[2] == 3
def test_load_img_on_png_with_resize(test_png_path): img_arr = _load_img(test_png_path, resize=(71, 71)) assert isinstance(img_arr, np.ndarray) assert len(img_arr.shape) == 3 assert img_arr.shape == (71, 71, 3)
def test_load_img_on_jpg(test_jpg_path): img_arr = _load_img(test_jpg_path) assert isinstance(img_arr, np.ndarray) assert len(img_arr.shape) == 3 assert img_arr.shape[2] == 3
def test_load_img_on_geotif(test_geotif_path): img_arr = _load_img(test_geotif_path, num_channels=4, norm=1) assert isinstance(img_arr, np.ndarray) assert len(img_arr.shape) == 3 assert img_arr.shape[2] == 4