def batch(self, batch_size, shuffle): """Returns generator of batch images. Args: batch_size (int): size of a batch. Returns: (ndarray): Images(4 dimension) of input data for Network. If including labels, return with transformed labels """ if shuffle: perm = np.random.permutation(self._data_size) else: perm = np.arange(self._data_size) batches = [ perm[i * batch_size:(i + 1) * batch_size] for i in range(int(np.ceil(self._data_size / batch_size))) ] imgfiles = [[self._data_table[p] for p in b] for b in batches] imgs = ImageLoader(imgfiles, self._color) for p, imgs in zip(batches, imgs.wait_images()): # Case: we are given both images and labels if self._data_y is not None: data_y = self._data_y[p].copy() for index, img in enumerate(imgs): label = np.array([data_y[index]], dtype=np.float32) if self._color == "GRAY": img = np.array(img, dtype=np.float32)[:, :, np.newaxis] else: img = np.array(img, dtype=np.float32) image, data_y[index] = resize(img, size=self._imsize, labels=label, num_class=len( self._class_list)) imgs[index] = image[0] if self._augmentation is not None: imgs, data_y = self._augmentation.create( np.array(imgs, dtype=np.float32), labels=data_y, num_class=len(self._class_list)) imgs = np.array(imgs, dtype=np.float32).transpose((0, 3, 1, 2)) yield imgs, data_y # Case: we are only given images else: for index, img in enumerate(imgs): if self._color == "GRAY": img = np.array(img, dtype=np.float32)[:, :, np.newaxis] else: img = np.array(img, dtype=np.float32) imgs[index] = resize(img, size=self._imsize)[0] if self._augmentation is not None: imgs = self._augmentation.create( np.array(imgs, dtype=np.float32)) imgs = np.array(imgs, dtype=np.float32).transpose((0, 3, 1, 2)) yield imgs
def batch(self, batch_size, shuffle): """ :param int batch_size: size of a batch. :return: Images(4 dimension) of input data for Network. If including labels, return with original labels :rtype: ndarray """ if shuffle: perm = np.random.permutation(self._data_size) else: perm = np.arange(self._data_size) batches = [perm[i * batch_size:(i + 1) * batch_size] for i in range(int(np.ceil(self._data_size / batch_size)))] imgfiles = [[self._data_table[p] for p in b] for b in batches] labels = [[self._data_y[p] for p in b] for b in batches] imgs = ImageLoader(imgfiles, self._color) for lbls, imgs in itertools.izip(labels, imgs.wait_images()): for index, img in enumerate(imgs): if self._color == "GRAY": img = np.array(img, dtype=np.float32)[:, :, np.newaxis] else: img = np.array(img, dtype=np.float32) imgs[index] = resize(img, size=self._imsize)[0] if self._augmentation is not None: imgs = self._augmentation.create(np.array(imgs, dtype=np.float32)) lbls = np.array(lbls) imgs = np.array(imgs, dtype=np.float32).transpose(0, 3, 1, 2) if self._data_y is None: yield imgs yield imgs, lbls
def transform(self, x, random=False, labels=None, num_class=0): """Performs a Zoom transformation of a Numpy Images. if x is a Batch, apply Zoom transform to Batch. needs zoom_rate (has to be > 1.0). If you use Random transformation, Zoom will be done randomly for values between the two limits given by the tuple or from 1 to zoom_rate. :param ndarray x: 3 or 4(batch) dimensional Image :param bool random: If True, apply random transformation. :param ndarray labels: rectangle labels(2-dimensional array). ex:) np.array([[center x, center y, x_top_left, height, 0, 0, 0, 1, 0]]) :param int num_class: number of class of datasets (for rectangle transformation) :return: Images(4 dimension) of zoom transformed. If including labels, return with transformed labels :rtype: ndarray :Example: >>> from renom.utility.image.data_augmentation.zoom import Zoom >>> from PIL import Image as im >>> import matplotlib.pyplot as plt >>> import numpy as np >>> image = im.open(image path) # ex:) "./image_folder/camera.jpg" >>> image = np.array(image).astype(np.float32) >>> zo = Zoom(zoom_rate=2) >>> zoom_image = zo.transform(image, random=True) >>> fig, axes = plt.subplots(2, 1) >>> axes[0].imshow(image/255); axes[0].set_title("Original Image") >>> axes[1].imshow(zoom_image[0] / 255); axes[1].set_title("Zoom One Image") >>> plt.show() """ zoom_images, batch_size, original_size = self.check_x_dim(x.copy()) transformed_labels = None if random: if isinstance(self.zoom_rate, tuple): zoom_rate = np.random.uniform(self.zoom_rate[0], self.zoom_rate[1], batch_size) else: zoom_rate = np.random.uniform(1.0, self.zoom_rate, batch_size) if labels is not None: transformed_labels = labels.copy() for index, (image, label, rate) in enumerate( zip(zoom_images, transformed_labels, zoom_rate)): crop_size = (int(float(original_size[0]) / rate), int(float(original_size[1]) / rate)) image, transformed_label = crop(image, size=crop_size, random=True, labels=np.array([label]), num_class=num_class) zoom_images[index], transformed_label = resize( image, size=original_size, labels=transformed_label, num_class=num_class) transformed_labels[index] = transformed_label[0] else: for index, (image, rate) in enumerate(zip(zoom_images, zoom_rate)): crop_size = (int(float(original_size[0]) / rate), int(float(original_size[1]) / rate)) image = crop(image, size=crop_size, random=True) zoom_images[index] = resize(image, size=original_size) else: crop_size = (int(float(original_size[0]) / self.zoom_rate), int(float(original_size[1]) / self.zoom_rate)) if labels is not None: zoom_images, transformed_labels = crop(zoom_images, size=crop_size, labels=labels, num_class=num_class) zoom_images, transformed_labels = resize( zoom_images, size=original_size, labels=transformed_labels, num_class=num_class) else: zoom_images = crop(zoom_images, size=crop_size) zoom_images = resize(zoom_images, size=original_size) if labels is not None: return zoom_images, transformed_labels else: return zoom_images