def draw_row(self, title, images, subtitles): title_cell = np.zeros( (self.cell_height, self.title_cell_width, 3), dtype=np.uint8) + 255 title_cell = ia.draw_text(title_cell, x=2, y=12, text=title, color=[0, 0, 0], size=16) image_cells = [] for image, subtitle in zip(images, subtitles): image_cell = np.zeros( (self.cell_height, self.cell_width, 3), dtype=np.uint8) + 255 image_cell[0:image.shape[0], 0:image.shape[1], :] = image image_cell = ia.draw_text(image_cell, x=2, y=image.shape[0] + 2, text=subtitle, color=[0, 0, 0], size=11) image_cells.append(image_cell) row = np.hstack([title_cell] + image_cells) return row
def draw_row(self, title, images, subtitles): title_cell = np.zeros((self.cell_height, self.title_cell_width, 3), dtype=np.uint8) + 255 title_cell = ia.draw_text(title_cell, x=2, y=12, text=title, color=[0, 0, 0], size=16) image_cells = [] for image, subtitle in zip(images, subtitles): image_cell = np.zeros((self.cell_height, self.cell_width, 3), dtype=np.uint8) + 255 image_cell[0:image.shape[0], 0:image.shape[1], :] = image image_cell = ia.draw_text(image_cell, x=2, y=image.shape[0]+2, text=subtitle, color=[0, 0, 0], size=11) image_cells.append(image_cell) row = np.hstack([title_cell] + image_cells) return row
def main(): image = data.astronaut() print("image shape:", image.shape) print("Press any key or wait %d ms to proceed to the next image." % (TIME_PER_STEP, )) children_all = [("hflip", iaa.Fliplr(1)), ("add", iaa.Add(50)), ("dropout", iaa.Dropout(0.2)), ("affine", iaa.Affine(rotate=35))] channels_all = [None, 0, [], [0], [0, 1], [1, 2], [0, 1, 2]] cv2.namedWindow("aug", cv2.WINDOW_NORMAL) cv2.imshow("aug", image[..., ::-1]) cv2.waitKey(TIME_PER_STEP) for children_title, children in children_all: for channels in channels_all: aug = iaa.WithChannels(channels=channels, children=children) img_aug = aug.augment_image(image) print("dtype", img_aug.dtype, "averages", np.average(img_aug, axis=tuple(range(0, img_aug.ndim - 1)))) #print("dtype", img_aug.dtype, "averages", img_aug.mean(axis=range(1, img_aug.ndim))) title = "children=%s | channels=%s" % (children_title, channels) img_aug = ia.draw_text(img_aug, x=5, y=5, text=title) cv2.imshow("aug", img_aug[..., ::-1]) # here with rgb2bgr cv2.waitKey(TIME_PER_STEP)
def load_images(n_batches=10, sleep=0.0, draw_text=True): batch_size = 4 astronaut = data.astronaut() astronaut = ia.imresize_single_image(astronaut, (64, 64)) kps = ia.KeypointsOnImage([ia.Keypoint(x=15, y=25)], shape=astronaut.shape) counter = 0 for i in range(n_batches): if draw_text: batch_images = [] batch_kps = [] for b in range(batch_size): astronaut_text = ia.draw_text(astronaut, x=0, y=0, text="%d" % (counter,), color=[0, 255, 0], size=16) batch_images.append(astronaut_text) batch_kps.append(kps) counter += 1 batch = ia.Batch( images=np.array(batch_images, dtype=np.uint8), keypoints=batch_kps ) else: if i == 0: batch_images = np.array([np.copy(astronaut) for _ in range(batch_size)], dtype=np.uint8) batch = ia.Batch( images=np.copy(batch_images), keypoints=[kps.deepcopy() for _ in range(batch_size)] ) yield batch if sleep > 0: time.sleep(sleep)
def main(): image = data.astronaut() image = ia.imresize_single_image(image, (64, 64)) print("image shape:", image.shape) print("Press any key or wait %d ms to proceed to the next image." % (TIME_PER_STEP, )) k = [1, 3, 5, 7, (3, 3), (1, 11)] cv2.namedWindow("aug", cv2.WINDOW_NORMAL) cv2.resizeWindow("aug", 64 * NB_AUGS_PER_IMAGE, 64) #cv2.imshow("aug", image[..., ::-1]) #cv2.waitKey(TIME_PER_STEP) for ki in k: aug = iaa.MedianBlur(k=ki) img_aug = [aug.augment_image(image) for _ in range(NB_AUGS_PER_IMAGE)] img_aug = np.hstack(img_aug) print("dtype", img_aug.dtype, "averages", np.average(img_aug, axis=tuple(range(0, img_aug.ndim - 1)))) #print("dtype", img_aug.dtype, "averages", img_aug.mean(axis=range(1, img_aug.ndim))) title = "k=%s" % (str(ki), ) img_aug = ia.draw_text(img_aug, x=5, y=5, text=title) cv2.imshow("aug", img_aug[..., ::-1]) # here with rgb2bgr cv2.waitKey(TIME_PER_STEP)
def draw_bar(frac, key, h=30, w=20, margin_right=15): bar = np.zeros((h, 1), dtype=np.uint8) + 32 bar[0:int(h * frac) + 1] = 255 bar = np.flipud(bar) bar = np.tile(bar[:, :, np.newaxis], (1, w, 3)) bar = np.pad(bar, ((20, 30), (0, margin_right), (0, 0)), mode="constant", constant_values=0) textx = 5 if frac * 100 >= 10: textx = textx - 3 elif frac * 100 >= 100: textx = textx - 6 bar = ia.draw_text(bar, x=textx, y=2, text="%.0f%%" % (frac * 100, ), size=8, color=[255, 255, 255]) keyimg = draw_key(key) util.draw_image(bar, x=(w // 2) - keyimg.shape[1] // 2, y=bar.shape[0] - keyimg.shape[0] - 8, other_img=keyimg, copy=False) return bar
def load_images(n_batches=10, sleep=0.0, draw_text=True): batch_size = 4 astronaut = data.astronaut() astronaut = ia.imresize_single_image(astronaut, (64, 64)) kps = ia.KeypointsOnImage([ia.Keypoint(x=15, y=25)], shape=astronaut.shape) counter = 0 for i in range(n_batches): if draw_text: batch_images = [] batch_kps = [] for b in range(batch_size): astronaut_text = ia.draw_text(astronaut, x=0, y=0, text="%d" % (counter, ), color=[0, 255, 0], size=16) batch_images.append(astronaut_text) batch_kps.append(kps) counter += 1 batch = ia.Batch(images=np.array(batch_images, dtype=np.uint8), keypoints=batch_kps) else: if i == 0: batch_images = np.array( [np.copy(astronaut) for _ in range(batch_size)], dtype=np.uint8) batch = ia.Batch( images=np.copy(batch_images), keypoints=[kps.deepcopy() for _ in range(batch_size)]) yield batch if sleep > 0: time.sleep(sleep)
def generate_imgcorruptlike(): ia.seed(1) image = ia.quokka((128, 128)) images_aug = [] augnames = [ "Original", "GaussianNoise", "ShotNoise", "ImpulseNoise", "SpeckleNoise", "GaussianBlur", "GlassBlur", "DefocusBlur", "MotionBlur", "ZoomBlur", "Fog", "Frost", "Snow", "Spatter", "Contrast", "Brightness", "Saturate", "JpegCompression", "Pixelate", "ElasticTransform" ] for augname in augnames: if augname == "Original": image_aug = np.copy(image) else: aug = getattr(iaa.imgcorruptlike, augname) image_aug = aug(severity=3)(image=image) image_aug = iaa.pad(image_aug, top=30, cval=255) image_aug = ia.draw_text(image_aug, y=6, x=2, text=augname, color=(0, 0, 0), size=15) images_aug.append(image_aug) _save("imgcorruptlike.jpg", ia.draw_grid(images_aug, cols=5, rows=4))
def main(): image = data.astronaut() cv2.namedWindow("aug", cv2.WINDOW_NORMAL) cv2.imshow("aug", image) cv2.waitKey(TIME_PER_STEP) # for value in cycle(np.arange(-255, 255, VAL_PER_STEP)): for value in np.arange(-255, 255, VAL_PER_STEP): aug = iaa.AddToHueAndSaturation(value=value) img_aug = aug.augment_image(image) img_aug = iaa.pad(img_aug, bottom=40) img_aug = ia.draw_text(img_aug, x=0, y=img_aug.shape[0] - 38, text="value=%d" % (value, ), size=30) cv2.imshow("aug", img_aug) cv2.waitKey(TIME_PER_STEP) images_aug = iaa.AddToHueAndSaturation( value=(-255, 255), per_channel=True).augment_images([image] * 64) ia.imshow(ia.draw_grid(images_aug)) image = ia.quokka_square((128, 128)) images_aug = [] images_aug.extend(iaa.AddToHue().augment_images([image] * 10)) images_aug.extend(iaa.AddToSaturation().augment_images([image] * 10)) ia.imshow(ia.draw_grid(images_aug, rows=2))
def main(): image = data.astronaut() image = ia.imresize_single_image(image, (64, 64)) print("image shape:", image.shape) print("Press any key or wait %d ms to proceed to the next image." % (TIME_PER_STEP,)) k = [ 1, 3, 5, 7, (3, 3), (1, 11) ] cv2.namedWindow("aug", cv2.WINDOW_NORMAL) cv2.resizeWindow("aug", 64*NB_AUGS_PER_IMAGE, 64) for ki in k: aug = iaa.MedianBlur(k=ki) img_aug = [aug.augment_image(image) for _ in range(NB_AUGS_PER_IMAGE)] img_aug = np.hstack(img_aug) print("dtype", img_aug.dtype, "averages", np.average(img_aug, axis=tuple(range(0, img_aug.ndim-1)))) title = "k=%s" % (str(ki),) img_aug = ia.draw_text(img_aug, x=5, y=5, text=title) cv2.imshow("aug", img_aug[..., ::-1]) # here with rgb2bgr cv2.waitKey(TIME_PER_STEP)
def draw_box(image, bbs): """ 绘制图片以及对应的bounding box Args: img: numpy array boxes: BoundingBoxesOnImage对象 """ image *= 255.0 for bound_box in bbs.bounding_boxes: x_center = bound_box.center_x y_center = bound_box.center_y _class = CLASS[bound_box.label] image = bound_box.draw_on_image(image, color=Colors_to_map[_class], alpha=0.7, thickness=2, raise_if_out_of_image=True) image = ia.draw_text(image, y=y_center, x=x_center - 20, color=Colors_to_map[_class], text=_class) plt.imshow(image) plt.title("Iamge size >>> {}".format(image.shape)) plt.axis('off') plt.xticks([]) plt.yticks([]) plt.show()
def main(): image = data.astronaut()[..., ::-1] # rgb2bgr print(image.shape) cv2.namedWindow("aug", cv2.WINDOW_NORMAL) cv2.imshow("aug", image) cv2.waitKey(TIME_PER_STEP) for n_segments in cycle(reversed(np.arange(1, 200, SEGMENTS_PER_STEP))): aug = iaa.Superpixels(p_replace=0.75, n_segments=n_segments) time_start = time.time() img_aug = aug.augment_image(image) print("augmented %d in %.4fs" % (n_segments, time.time() - time_start)) img_aug = ia.draw_text(img_aug, x=5, y=5, text="%d" % (n_segments, )) cv2.imshow("aug", img_aug) cv2.waitKey(TIME_PER_STEP)
def main(): image = data.astronaut()[...,::-1] # rgb2bgr print(image.shape) cv2.namedWindow("aug", cv2.WINDOW_NORMAL) cv2.imshow("aug", image) cv2.waitKey(TIME_PER_STEP) for n_segments in cycle(reversed(np.arange(1, 200, SEGMENTS_PER_STEP))): aug = iaa.Superpixels(p_replace=0.75, n_segments=n_segments) time_start = time.time() img_aug = aug.augment_image(image) print("augmented %d in %.4fs" % (n_segments, time.time() - time_start)) img_aug = ia.draw_text(img_aug, x=5, y=5, text="%d" % (n_segments,)) cv2.imshow("aug", img_aug) cv2.waitKey(TIME_PER_STEP)
def main(): image = data.astronaut() image = ia.imresize_single_image(image, (128, 128)) print("image shape:", image.shape) print("Press any key or wait %d ms to proceed to the next image." % (TIME_PER_STEP, )) configs = [ (1, 75, 75), (3, 75, 75), (5, 75, 75), (10, 75, 75), (10, 25, 25), (10, 250, 150), (15, 75, 75), (15, 150, 150), (15, 250, 150), (20, 75, 75), (40, 150, 150), ((1, 5), 75, 75), (5, (10, 250), 75), (5, 75, (10, 250)), (5, (10, 250), (10, 250)), (10, (10, 250), (10, 250)), ] cv2.namedWindow("aug", cv2.WINDOW_NORMAL) cv2.resizeWindow("aug", 128 * NB_AUGS_PER_IMAGE, 128) for (d, sigma_color, sigma_space) in configs: aug = iaa.BilateralBlur(d=d, sigma_color=sigma_color, sigma_space=sigma_space) img_aug = [aug.augment_image(image) for _ in range(NB_AUGS_PER_IMAGE)] img_aug = np.hstack(img_aug) print("dtype", img_aug.dtype, "averages", np.average(img_aug, axis=tuple(range(0, img_aug.ndim - 1)))) title = "d=%s, sc=%s, ss=%s" % (str(d), str(sigma_color), str(sigma_space)) img_aug = ia.draw_text(img_aug, x=5, y=5, text=title) cv2.imshow("aug", img_aug[..., ::-1]) # here with rgb2bgr cv2.waitKey(TIME_PER_STEP)
def main(): image = data.astronaut() cv2.namedWindow("aug", cv2.WINDOW_NORMAL) cv2.imshow("aug", image) cv2.waitKey(TIME_PER_STEP) # for value in cycle(np.arange(-255, 255, VAL_PER_STEP)): for value in np.arange(-255, 255, VAL_PER_STEP): aug = iaa.AddToHueAndSaturation(value=value) img_aug = aug.augment_image(image) img_aug = ia.pad(img_aug, bottom=40) img_aug = ia.draw_text(img_aug, x=0, y=img_aug.shape[0]-38, text="value=%d" % (value,), size=30) cv2.imshow("aug", img_aug) cv2.waitKey(TIME_PER_STEP) images_aug = iaa.AddToHueAndSaturation(value=(-255, 255), per_channel=True).augment_images([image] * 64) ia.imshow(ia.draw_grid(images_aug))
def main(): image = data.astronaut() image = ia.imresize_single_image(image, (128, 128)) print("image shape:", image.shape) print("Press any key or wait %d ms to proceed to the next image." % (TIME_PER_STEP,)) configs = [ (1, 75, 75), (3, 75, 75), (5, 75, 75), (10, 75, 75), (10, 25, 25), (10, 250, 150), (15, 75, 75), (15, 150, 150), (15, 250, 150), (20, 75, 75), (40, 150, 150), ((1, 5), 75, 75), (5, (10, 250), 75), (5, 75, (10, 250)), (5, (10, 250), (10, 250)), (10, (10, 250), (10, 250)), ] cv2.namedWindow("aug", cv2.WINDOW_NORMAL) cv2.resizeWindow("aug", 128*NB_AUGS_PER_IMAGE, 128) for (d, sigma_color, sigma_space) in configs: aug = iaa.BilateralBlur(d=d, sigma_color=sigma_color, sigma_space=sigma_space) img_aug = [aug.augment_image(image) for _ in range(NB_AUGS_PER_IMAGE)] img_aug = np.hstack(img_aug) print("dtype", img_aug.dtype, "averages", np.average(img_aug, axis=tuple(range(0, img_aug.ndim-1)))) title = "d=%s, sc=%s, ss=%s" % (str(d), str(sigma_color), str(sigma_space)) img_aug = ia.draw_text(img_aug, x=5, y=5, text=title) cv2.imshow("aug", img_aug[..., ::-1]) # here with rgb2bgr cv2.waitKey(TIME_PER_STEP)
def load_images(): batch_size = 4 astronaut = data.astronaut() astronaut = ia.imresize_single_image(astronaut, (64, 64)) kps = ia.KeypointsOnImage([ia.Keypoint(x=15, y=25)], shape=astronaut.shape) counter = 0 for i in range(10): batch_images = [] batch_kps = [] for b in range(batch_size): astronaut_text = ia.draw_text(astronaut, x=0, y=0, text="%d" % (counter, ), color=[0, 255, 0], size=16) batch_images.append(astronaut_text) batch_kps.append(kps) counter += 1 batch = ia.Batch(images=np.array(batch_images, dtype=np.uint8), keypoints=batch_kps) yield batch
def main(): image = data.astronaut() print("image shape:", image.shape) print("Press any key or wait %d ms to proceed to the next image." % (TIME_PER_STEP,)) children_all = [ ("hflip", iaa.Fliplr(1)), ("add", iaa.Add(50)), ("dropout", iaa.Dropout(0.2)), ("affine", iaa.Affine(rotate=35)) ] channels_all = [ None, 0, [], [0], [0, 1], [1, 2], [0, 1, 2] ] cv2.namedWindow("aug", cv2.WINDOW_NORMAL) cv2.imshow("aug", image[..., ::-1]) cv2.waitKey(TIME_PER_STEP) for children_title, children in children_all: for channels in channels_all: aug = iaa.WithChannels(channels=channels, children=children) img_aug = aug.augment_image(image) print("dtype", img_aug.dtype, "averages", np.average(img_aug, axis=tuple(range(0, img_aug.ndim-1)))) #print("dtype", img_aug.dtype, "averages", img_aug.mean(axis=range(1, img_aug.ndim))) title = "children=%s | channels=%s" % (children_title, channels) img_aug = ia.draw_text(img_aug, x=5, y=5, text=title) cv2.imshow("aug", img_aug[..., ::-1]) # here with rgb2bgr cv2.waitKey(TIME_PER_STEP)
def chapter_examples_bounding_boxes_iou(): import numpy as np import imgaug as ia from imgaug.augmentables.bbs import BoundingBox ia.seed(1) # Define image with two bounding boxes. image = ia.quokka(size=(256, 256)) bb1 = BoundingBox(x1=50, x2=100, y1=25, y2=75) bb2 = BoundingBox(x1=75, x2=125, y1=50, y2=100) # Compute intersection, union and IoU value # Intersection and union are both bounding boxes. They are here # decreased/increased in size purely for better visualization. bb_inters = bb1.intersection(bb2).extend(all_sides=-1) bb_union = bb1.union(bb2).extend(all_sides=2) iou = bb1.iou(bb2) # Draw bounding boxes, intersection, union and IoU value on image. image_bbs = np.copy(image) image_bbs = bb1.draw_on_image(image_bbs, size=2, color=[0, 255, 0]) image_bbs = bb2.draw_on_image(image_bbs, size=2, color=[0, 255, 0]) image_bbs = bb_inters.draw_on_image(image_bbs, size=2, color=[255, 0, 0]) image_bbs = bb_union.draw_on_image(image_bbs, size=2, color=[0, 0, 255]) image_bbs = ia.draw_text(image_bbs, text="IoU=%.2f" % (iou, ), x=bb_union.x2 + 10, y=bb_union.y1 + bb_union.height // 2, color=[255, 255, 255], size=13) # ------------ save("examples_bounding_boxes", "iou.jpg", grid([image_bbs], cols=1, rows=1), quality=90)
def generate_video_image(batch_idx, examples, model): """Generate frames for a video of the training progress. Each frame contains N examples shown in a grid. Each example shows the input image and the main heatmap predicted by the model.""" start_time = time.time() #print("A", time.time() - start_time) model.eval() # fw through network inputs, outputs_gt = examples_to_batch(examples, iaa.Noop()) inputs_torch = torch.from_numpy(inputs) inputs_torch = Variable(inputs_torch, volatile=True) if GPU >= 0: inputs_torch = inputs_torch.cuda(GPU) outputs_pred_torch = model(inputs_torch) #print("B", time.time() - start_time) outputs_pred = outputs_pred_torch.cpu().data.numpy() inputs = (inputs * 255).astype(np.uint8).transpose(0, 2, 3, 1) #print("C", time.time() - start_time) heatmaps = [] for i in range(inputs.shape[0]): hm_drawn = draw_heatmap(inputs[i], np.squeeze(outputs_pred[i][0]), alpha=0.5) heatmaps.append(hm_drawn) #print("D", time.time() - start_time) grid = ia.draw_grid(heatmaps, cols=11, rows=6).astype(np.uint8) #grid_rs = misc.imresize(grid, (720-32, 1280-32)) # pad by 42 for the text and to get the image to 720p aspect ratio grid_pad = np.pad(grid, ((0, 42), (0, 0), (0, 0)), mode="constant") grid_pad_text = ia.draw_text(grid_pad, x=grid_pad.shape[1] - 220, y=grid_pad.shape[0] - 35, text="Batch %05d" % (batch_idx, ), color=[255, 255, 255]) #print("E", time.time() - start_time) return grid_pad_text
def main(): image = data.astronaut() image = ia.imresize_single_image(image, (64, 64)) keypoints_on_image = ia.KeypointsOnImage([ia.Keypoint(x=10, y=10, vis=None, label=None)], shape=image.shape) images_arr = np.array([image for _ in range(NB_AUGS_PER_IMAGE)]) images_list = [image for _ in range(NB_AUGS_PER_IMAGE)] keypoints_on_images = [keypoints_on_image.deepcopy() for _ in range(NB_AUGS_PER_IMAGE)] print("image shape:", image.shape) print("Press ENTER or wait %d ms to proceed to the next image." % (TIME_PER_STEP,)) children = [ iaa.CoarseDropout(p=0.5, size_percent=0.05), iaa.AdditiveGaussianNoise(scale=0.1*255) ] n = [ None, 0, 1, len(children), len(children)+1, (1, 1), (1, len(children)), (1, len(children)+1), (1, None) ] cv2.namedWindow("aug", cv2.WINDOW_NORMAL) cv2.resizeWindow("aug", 64*NB_AUGS_PER_IMAGE, (2+4+4)*64) rows = [] for ni in n: aug = iaa.SomeOf(ni, children, random_order=False) rows.append(aug.augment_images(images_arr)) grid = to_grid(rows, None) cv2.imshow("aug", grid[..., ::-1]) # here with rgb2bgr cv2.waitKey(TIME_PER_STEP) for ni in n: print("------------------------") print("-- %s" % (str(ni),)) print("------------------------") aug = iaa.SomeOf(ni, children, random_order=False) aug_ro = iaa.SomeOf(ni, children, random_order=True) aug_det = aug.to_deterministic() aug_ro_det = aug_ro.to_deterministic() aug_kps = [] aug_kps.extend([aug_det.augment_keypoints(keypoints_on_images)] * 4) aug_kps.extend([aug_ro_det.augment_keypoints(keypoints_on_images)] * 4) aug_rows = [] aug_rows.append(images_arr) aug_rows.append(images_list) aug_rows.append(aug_det.augment_images(images_arr)) aug_rows.append(aug_det.augment_images(images_arr)) aug_rows.append(aug_det.augment_images(images_list)) aug_rows.append(aug_det.augment_images(images_list)) aug_rows.append(aug_ro_det.augment_images(images_arr)) aug_rows.append(aug_ro_det.augment_images(images_arr)) aug_rows.append(aug_ro_det.augment_images(images_list)) aug_rows.append(aug_ro_det.augment_images(images_list)) grid = to_grid(aug_rows, aug_kps) title = "n=%s" % (str(ni),) grid = ia.draw_text(grid, x=5, y=5, text=title) cv2.imshow("aug", grid[..., ::-1]) # here with rgb2bgr cv2.waitKey(TIME_PER_STEP)
def chapter_alpha_masks_frequency(): # ----------------------------------------- # example 1 (basic) # ----------------------------------------- import imgaug as ia from imgaug import augmenters as iaa from imgaug import parameters as iap ia.seed(1) # Example batch of images. # The array has shape (8, 64, 64, 3) and dtype uint8. images = np.array([ia.quokka(size=(128, 128)) for _ in range(8)], dtype=np.uint8) seq = iaa.BlendAlphaFrequencyNoise( foreground=iaa.Multiply(iap.Choice([0.5, 1.5]), per_channel=True)) images_aug = seq(images=images) # ------------ save("alpha", "alpha_frequency_example_basic.jpg", grid(images_aug, cols=4, rows=2)) # ----------------------------------------- # example 1 (per_channel) # ----------------------------------------- import imgaug as ia from imgaug import augmenters as iaa ia.seed(1) # Example batch of images. # The array has shape (8, 128, 128, 3) and dtype uint8. images = np.array([ia.quokka(size=(128, 128)) for _ in range(8)], dtype=np.uint8) seq = iaa.BlendAlphaFrequencyNoise(foreground=iaa.EdgeDetect(1.0), per_channel=True) images_aug = seq(images=images) # ------------ save("alpha", "alpha_frequency_example_per_channel.jpg", grid(images_aug, cols=4, rows=2)) # ----------------------------------------- # noise masks # ----------------------------------------- import imgaug as ia from imgaug import augmenters as iaa from imgaug import parameters as iap seed = 1 ia.seed(seed) seq = iaa.BlendAlphaFrequencyNoise( foreground=iaa.Multiply(iap.Choice([0.5, 1.5]), per_channel=True)) masks = [ seq.factor.draw_samples((64, 64), random_state=ia.new_random_state(seed + 1 + i)) for i in range(16) ] masks = [np.tile(mask[:, :, np.newaxis], (1, 1, 3)) for mask in masks] masks = [(mask * 255).astype(np.uint8) for mask in masks] # ------------ save("alpha", "alpha_frequency_noise_masks.jpg", grid(masks, cols=8, rows=2)) # ----------------------------------------- # noise masks, varying exponent # ----------------------------------------- import imgaug as ia from imgaug import augmenters as iaa from imgaug import parameters as iap seed = 1 ia.seed(seed) masks = [] nb_rows = 4 exponents = np.linspace(-4.0, 4.0, 16) for i, exponent in enumerate(exponents): seq = iaa.BlendAlphaFrequencyNoise(exponent=exponent, foreground=iaa.Multiply( iap.Choice([0.5, 1.5]), per_channel=True), size_px_max=32, upscale_method="linear", iterations=1, sigmoid=False) group = [] for row in range(nb_rows): mask = seq.factor.draw_samples( (64, 64), random_state=ia.new_random_state(seed + 1 + i * 10 + row)) mask = np.tile(mask[:, :, np.newaxis], (1, 1, 3)) mask = (mask * 255).astype(np.uint8) if row == nb_rows - 1: mask = np.pad(mask, ((0, 20), (0, 0), (0, 0)), mode="constant", constant_values=255) mask = ia.draw_text(mask, y=64 + 2, x=6, text="%.2f" % (exponent, ), size=10, color=[0, 0, 0]) group.append(mask) masks.append(np.vstack(group)) # ------------ save("alpha", "alpha_frequency_noise_masks_exponents.jpg", grid(masks, cols=16, rows=1)) # ----------------------------------------- # noise masks, upscale=nearest # ----------------------------------------- import imgaug as ia from imgaug import augmenters as iaa from imgaug import parameters as iap seed = 1 ia.seed(seed) seq = iaa.BlendAlphaFrequencyNoise(foreground=iaa.Multiply( iap.Choice([0.5, 1.5]), per_channel=True), upscale_method="nearest") masks = [ seq.factor.draw_samples((64, 64), random_state=ia.new_random_state(seed + 1 + i)) for i in range(16) ] masks = [np.tile(mask[:, :, np.newaxis], (1, 1, 3)) for mask in masks] masks = [(mask * 255).astype(np.uint8) for mask in masks] # ------------ save("alpha", "alpha_frequency_noise_masks_nearest.jpg", grid(masks, cols=8, rows=2)) # ----------------------------------------- # noise masks linear # ----------------------------------------- import imgaug as ia from imgaug import augmenters as iaa from imgaug import parameters as iap seed = 1 ia.seed(seed) seq = iaa.BlendAlphaFrequencyNoise(foreground=iaa.Multiply( iap.Choice([0.5, 1.5]), per_channel=True), upscale_method="linear") masks = [ seq.factor.draw_samples((64, 64), random_state=ia.new_random_state(seed + 1 + i)) for i in range(16) ] masks = [np.tile(mask[:, :, np.newaxis], (1, 1, 3)) for mask in masks] masks = [(mask * 255).astype(np.uint8) for mask in masks] # ------------ save("alpha", "alpha_frequency_noise_masks_linear.jpg", grid(masks, cols=8, rows=2))
def create_bb_img(input_lst_path, input_img_root_path, output_img_path, class_list=[]): """ 画像データにlstファイルに基づくバウンディングボックスを加工した画像をつうる """ import random import copy import os from os import path import shutil from PIL import Image import numpy as np import imgaug as ia from tqdm import tqdm_notebook as tqdm # 出力先をリセット if path.isdir(output_img_path): shutil.rmtree(output_img_path) os.makedirs(output_img_path) with open(input_lst_path) as lst_f: for line in tqdm(lst_f.readlines()): line = line.strip() if not line: continue #lst形式のデータを読み取って、変数に入れる origin_img_index, header_size, label_width, label_data, img_path = __read_lst( line) img_path = path.join(input_img_root_path, img_path) # 画像を読み込む origin_img = Image.open(img_path).convert('RGB') img_height = origin_img.height img_width = origin_img.width max_edge = max(img_height, img_width) # 画像を変換する target_img = np.array(origin_img) # バウンディングボックスを生成 bbs = [] for bb_index in range(len(label_data) // label_width): bb = ia.BoundingBox( x1=float(label_data[bb_index * label_width + 1]) * img_width, y1=float(label_data[bb_index * label_width + 2]) * img_height, x2=float(label_data[bb_index * label_width + 3]) * img_width, y2=float(label_data[bb_index * label_width + 4]) * img_height) class_val = int(label_data[bb_index * label_width]) assert 0 <= class_val and class_val < len( class_list), 'classの値が不正です。 : ' + str(class_val) class_name = class_list[class_val] if class_list[ class_val] else str(class_val) target_img = ia.draw_text(target_img, bb.y1, bb.x1, class_name) bbs.append(bb) bbs_on_img = ia.BoundingBoxesOnImage(bbs, shape=target_img.shape) after_bb_img = bbs_on_img.draw_on_image(target_img) output_img_name = path.basename(img_path) Image.fromarray(after_bb_img).save( path.join(output_img_path, output_img_name))
def chapter_alpha_masks_iterative(): # ----------------------------------------- # IterativeNoiseAggregator varying number of iterations # ----------------------------------------- import imgaug as ia from imgaug import parameters as iap seed = 1 ia.seed(seed) masks = [] iterations_all = [1, 2, 3, 4] for iterations in iterations_all: noise = iap.IterativeNoiseAggregator(other_param=iap.FrequencyNoise( exponent=(-4.0, 4.0), upscale_method=["linear", "nearest"]), iterations=iterations, aggregation_method="max") row = [ noise.draw_samples((64, 64), random_state=ia.new_random_state(seed + 1 + i)) for i in range(16) ] row = np.hstack(row) row = np.tile(row[:, :, np.newaxis], (1, 1, 3)) row = (row * 255).astype(np.uint8) row = np.pad(row, ((0, 0), (50, 0), (0, 0)), mode="constant", constant_values=255) row = ia.draw_text(row, y=24, x=2, text="%d iter." % (iterations, ), size=14, color=[0, 0, 0]) masks.append(row) # ------------ save("alpha", "iterative_vary_iterations.jpg", grid(masks, cols=1, rows=len(iterations_all))) # ----------------------------------------- # IterativeNoiseAggregator varying methods # ----------------------------------------- import imgaug as ia from imgaug import parameters as iap seed = 1 ia.seed(seed) iterations_all = [1, 2, 3, 4, 5, 6] methods = ["min", "avg", "max"] cell_idx = 0 rows = [] for method_idx, method in enumerate(methods): row = [] for iterations in iterations_all: noise = iap.IterativeNoiseAggregator( other_param=iap.FrequencyNoise( exponent=-2.0, size_px_max=32, upscale_method=["linear", "nearest"]), iterations=iterations, aggregation_method=method) cell = noise.draw_samples( (64, 64), random_state=ia.new_random_state(seed + 1 + method_idx)) cell = np.tile(cell[:, :, np.newaxis], (1, 1, 3)) cell = (cell * 255).astype(np.uint8) if iterations == 1: cell = np.pad(cell, ((0, 0), (40, 0), (0, 0)), mode="constant", constant_values=255) cell = ia.draw_text(cell, y=27, x=2, text="%s" % (method, ), size=14, color=[0, 0, 0]) if method_idx == 0: cell = np.pad(cell, ((20, 0), (0, 0), (0, 0)), mode="constant", constant_values=255) cell = ia.draw_text(cell, y=0, x=12 + 40 * (iterations == 1), text="%d iter." % (iterations, ), size=14, color=[0, 0, 0]) cell = np.pad(cell, ((0, 1), (0, 1), (0, 0)), mode="constant", constant_values=255) row.append(cell) cell_idx += 1 rows.append(np.hstack(row)) gridarr = np.vstack(rows) # ------------ save("alpha", "iterative_vary_methods.jpg", gridarr)
def chapter_alpha_masks_sigmoid(): # ----------------------------------------- # Sigmoid varying on/off # ----------------------------------------- import imgaug as ia from imgaug import parameters as iap seed = 1 ia.seed(seed) masks = [] for activated in [False, True]: noise = iap.Sigmoid.create_for_noise(other_param=iap.FrequencyNoise( exponent=(-4.0, 4.0), upscale_method="linear"), activated=activated) row = [ noise.draw_samples((64, 64), random_state=ia.new_random_state(seed + 1 + i)) for i in range(16) ] row = np.hstack(row) row = np.tile(row[:, :, np.newaxis], (1, 1, 3)) row = (row * 255).astype(np.uint8) row = np.pad(row, ((0, 0), (90, 0), (0, 0)), mode="constant", constant_values=255) row = ia.draw_text(row, y=17, x=2, text="activated=\n%s" % (activated, ), size=14, color=[0, 0, 0]) masks.append(row) # ------------ save("alpha", "sigmoid_vary_activated.jpg", grid(masks, cols=1, rows=2)) # ----------------------------------------- # Sigmoid varying on/off # ----------------------------------------- import imgaug as ia from imgaug import parameters as iap seed = 1 ia.seed(seed) masks = [] nb_rows = 3 class ConstantNoise(iap.StochasticParameter): def __init__(self, noise, seed): super(ConstantNoise, self).__init__() self.noise = noise self.seed = seed def _draw_samples(self, size, random_state): return self.noise.draw_samples(size, random_state=ia.new_random_state( self.seed)) for rowidx in range(nb_rows): row = [] for tidx, threshold in enumerate(np.linspace(-10.0, 10.0, 10)): noise = iap.Sigmoid.create_for_noise(other_param=ConstantNoise( iap.FrequencyNoise(exponent=(-4.0, 4.0), upscale_method="linear"), seed=seed + 100 + rowidx), activated=True, threshold=threshold) cell = noise.draw_samples( (64, 64), random_state=ia.new_random_state(seed + tidx)) cell = np.tile(cell[:, :, np.newaxis], (1, 1, 3)) cell = (cell * 255).astype(np.uint8) if rowidx == 0: cell = np.pad(cell, ((20, 0), (0, 0), (0, 0)), mode="constant", constant_values=255) cell = ia.draw_text(cell, y=2, x=15, text="%.1f" % (threshold, ), size=14, color=[0, 0, 0]) row.append(cell) row = np.hstack(row) masks.append(row) gridarr = np.vstack(masks) # ------------ save("alpha", "sigmoid_vary_threshold.jpg", gridarr)
def draw(self, height, width): image = np.full((height, width, 3), 255, dtype=np.uint8) image = ia.draw_text(image, 0, 0, self.text, color=(0, 0, 0), size=12) return image
def main(): image = data.astronaut() image = ia.imresize_single_image(image, (64, 64)) keypoints_on_image = ia.KeypointsOnImage([ia.Keypoint(x=10, y=10)], shape=image.shape) images_arr = np.array([image for _ in range(NB_AUGS_PER_IMAGE)]) images_list = [image for _ in range(NB_AUGS_PER_IMAGE)] keypoints_on_images = [keypoints_on_image.deepcopy() for _ in range(NB_AUGS_PER_IMAGE)] print("image shape:", image.shape) print("Press ENTER or wait %d ms to proceed to the next image." % (TIME_PER_STEP,)) children = [ iaa.CoarseDropout(p=0.5, size_percent=0.05), iaa.AdditiveGaussianNoise(scale=0.1*255) ] n = [ None, 0, 1, len(children), len(children)+1, (1, 1), (1, len(children)), (1, len(children)+1), (1, None) ] cv2.namedWindow("aug", cv2.WINDOW_NORMAL) cv2.resizeWindow("aug", 64*NB_AUGS_PER_IMAGE, (2+4+4)*64) rows = [] for ni in n: aug = iaa.SomeOf(ni, children, random_order=False) rows.append(aug.augment_images(images_arr)) grid = to_grid(rows, None) cv2.imshow("aug", grid[..., ::-1]) # here with rgb2bgr cv2.waitKey(TIME_PER_STEP) for ni in n: print("------------------------") print("-- %s" % (str(ni),)) print("------------------------") aug = iaa.SomeOf(ni, children, random_order=False) aug_ro = iaa.SomeOf(ni, children, random_order=True) aug_det = aug.to_deterministic() aug_ro_det = aug_ro.to_deterministic() aug_kps = [] aug_kps.extend([aug_det.augment_keypoints(keypoints_on_images)] * 4) aug_kps.extend([aug_ro_det.augment_keypoints(keypoints_on_images)] * 4) aug_rows = [] aug_rows.append(images_arr) aug_rows.append(images_list) aug_rows.append(aug_det.augment_images(images_arr)) aug_rows.append(aug_det.augment_images(images_arr)) aug_rows.append(aug_det.augment_images(images_list)) aug_rows.append(aug_det.augment_images(images_list)) aug_rows.append(aug_ro_det.augment_images(images_arr)) aug_rows.append(aug_ro_det.augment_images(images_arr)) aug_rows.append(aug_ro_det.augment_images(images_list)) aug_rows.append(aug_ro_det.augment_images(images_list)) grid = to_grid(aug_rows, aug_kps) title = "n=%s" % (str(ni),) grid = ia.draw_text(grid, x=5, y=5, text=title) cv2.imshow("aug", grid[..., ::-1]) # here with rgb2bgr cv2.waitKey(TIME_PER_STEP)
def main(): # Configure imgaug ia.seed(1) ######################################################### # Parse arguments ######################################################### parser = argparse.ArgumentParser( description= 'Applies a set of augmentations to every image in the input directory.' ) parser.add_argument('--input_directory', '-i', type=str, help='e.g. "./downloads/"') parser.add_argument('--output_directory', '-o', type=str, help='e.g. "./downloads/"') parser.add_argument( '--single_threaded', '-s', action='store_true', help='Process images in one thread instead of multithreading') parser.add_argument('--preview_only', '-p', action='store_true', help='Show previews instead of writing to disk') parser.add_argument( '--skip_originals', action='store_true', help= 'Prevent original images from being copied into the destination folder' ) args = parser.parse_args() input_directory = args.input_directory output_directory = args.output_directory user_requested_preview_only = args.preview_only single_threaded = args.single_threaded skip_originals = args.skip_originals ######################################################### # Process all data files in the input directory, either # copying them over or queueing them to be augmented in # the next step. ######################################################### # Load YOLO region class names from file class_names = [] with open(os.path.join(input_directory, "class.names")) as class_file: class_names = [ line.rstrip() for line in class_file if line.rstrip() != "" ] if not user_requested_preview_only: # Copy MyAugments.py to the output directory copyfile(os.path.join(os.getcwd(), "MyAugments.py"), os.path.join(output_directory, "MyAugments.py")) # Copy the YOLO region class names file to the output directory copyfile(os.path.join(input_directory, "class.names"), os.path.join(output_directory, "class.names")) augment_files = [] filenames = os.listdir(input_directory) filenames.sort() for filename in filenames: # Work only on YOLO .txt files. if not filename.endswith(".txt"): continue base_filename = os.path.splitext(filename)[0] data_path = os.path.join(input_directory, base_filename + ".txt") image_path = os.path.join(input_directory, base_filename + ".jpg") augment_files.append(DataPair(data_path, image_path)) if not skip_originals and not user_requested_preview_only: # Copy the original data file to the output directory. copyfile(os.path.join(input_directory, base_filename + ".txt"), os.path.join(output_directory, base_filename + ".txt")) # Copy the original image to the output directory. copyfile(os.path.join(input_directory, base_filename + ".jpg"), os.path.join(output_directory, base_filename + ".jpg")) ######################################################### # From the list of data/image pairs to augment, create # batches for imgaug to process. ######################################################### batch = [] batches = [] MAX_BATCH_SIZE = 10 if user_requested_preview_only else 16 for i, item in enumerate(augment_files): # Load image into memory image = imageio.imread(item.image_path) # Get imgaug representation of bounding boxes image_data = ImageData.from_yolo_data(item.data_path, class_names) bbs = image_data.to_imgaug(image.shape) batch.append((image, bbs, item)) # If we're at the max batch size or the end of the file list, # finalize the batch and add it to the batch list. if len(batch) == MAX_BATCH_SIZE or i == len(augment_files) - 1: images, bounding_boxes, data = list(zip(*batch)) batches.append( UnnormalizedBatch(images=images, bounding_boxes=bounding_boxes, data=data)) batch.clear() ######################################################### # Apply each operation in MyAugments.py to each image ######################################################### should_multithread = not single_threaded and not user_requested_preview_only ops = get_augmentation_operations() total_ops_per_image = sum([op.num_repetitions for op in ops]) input_image_count = sum([len(b.data) for b in batches]) generated_image_count = total_ops_per_image * input_image_count print(f"{generated_image_count} new images will be created.") progress_bar = tqdm(total=generated_image_count) for op in ops: for i in range(op.num_repetitions): # Produce augmentations for batches_aug in op.operation.augment_batches( batches, background=should_multithread): if user_requested_preview_only: # Preview output one batch at a time. # Blocks execution until the window is closed. # Closing a window will cause the next batch to appear. # Close the Python instance in the dock to stop execution. images_with_labels = [ bb.draw_on_image(image) for image, bb in zip(batches_aug.images_aug, batches_aug.bounding_boxes_aug) ] grid_image = ia.draw_grid(images_with_labels, cols=None, rows=None) title = f"{op.name}\nRep {i}\n" # title += ", ".join([item.image_filename for item in batches_aug.data]) # Draw image filenames grid_image = ia.draw_text(grid_image, 8, 8, title, color=(255, 0, 0), size=50) ia.imshow(grid_image, backend='matplotlib') continue for image, bbs, data in zip(batches_aug.images_aug, batches_aug.bounding_boxes_aug, batches_aug.data): # Write image and matching data file to output folder # Determine base name for image and matching data file image_filename_no_extension, image_extension = os.path.splitext( Path(data.image_path).name) base_filename = "" if op.num_repetitions == 1: base_filename = f"{image_filename_no_extension}_{op.name}" else: base_filename = f"{image_filename_no_extension}_{op.name}_rep{i}" # Write image to output folder output_image_path = os.path.join( output_directory, f"{base_filename}{image_extension}") imageio.imwrite(output_image_path, image) # Write modified imgaug bounding boxes as YOLO format in output folder image_height, image_width, _ = image.shape output_data = ImageData.from_imagaug( image_width, image_height, bbs) output_data.write_yolo(data.data_path, class_names) # Update progress bar progress_bar.update(1) progress_bar.close()