def random_flip(src, px=0, py=0, copy=False): """Randomly flip image along horizontal and vertical with probabilities. Parameters ---------- src : mxnet.nd.NDArray Input image with HWC format. px : float Horizontal flip probability [0, 1]. py : float Vertical flip probability [0, 1]. copy : bool If `True`, return a copy of input Returns ------- mxnet.nd.NDArray Augmented image. tuple Tuple of (flip_x, flip_y), records of whether flips are applied. """ flip_y = np.random.choice([False, True], p=[1-py, py]) flip_x = np.random.choice([False, True], p=[1-px, px]) if flip_y: src = nd.flip(src, axis=0) if flip_x: src = nd.flip(src, axis=1) if copy: src = src.copy() return src, (flip_x, flip_y)
def random_flip(src, px=0, py=0, copy=False): """Randomly flip image along horizontal and vertical with probabilities. Parameters ---------- src : mxnet.nd.NDArray Input image with HWC format. px : float Horizontal flip probability [0, 1]. py : float Vertical flip probability [0, 1]. copy : bool If `True`, return a copy of input Returns ------- mxnet.nd.NDArray Augmented image. tuple Tuple of (flip_x, flip_y), records of whether flips are applied. """ flip_y = np.random.choice([False, True], p=[1 - py, py]) flip_x = np.random.choice([False, True], p=[1 - px, px]) if flip_y: src = nd.flip(src, axis=0) if flip_x: src = nd.flip(src, axis=1) if copy: src = src.copy() return src, (flip_x, flip_y)
def random_flip(src, px=0, py=0, copy=False): flip_y = np.random.choice([False, True], p=[1 - py, py]) flip_x = np.random.choice([False, True], p=[1 - px, px]) if flip_y: src = nd.flip(src, axis=0) if flip_x: src = nd.flip(src, axis=1) if copy: src = src.copy() return src, (flip_x, flip_y)
def validate(val_data, val_dataset, net, ctx): if isinstance(ctx, mx.Context): ctx = [ctx] val_metric.reset() from tqdm import tqdm for batch in tqdm(val_data): data, scale, center, score, imgid = val_batch_fn(batch, ctx) outputs = [net(X) for X in data] if opt.flip_test: data_flip = [nd.flip(X, axis=3) for X in data] outputs_flip = [net(X) for X in data_flip] outputs_flipback = [flip_heatmap(o, val_dataset.joint_pairs, shift=True) for o in outputs_flip] outputs = [(o + o_flip)/2 for o, o_flip in zip(outputs, outputs_flipback)] if len(outputs) > 1: outputs_stack = nd.concat(*[o.as_in_context(mx.cpu()) for o in outputs], dim=0) else: outputs_stack = outputs[0].as_in_context(mx.cpu()) preds, maxvals = get_final_preds(outputs_stack, center.asnumpy(), scale.asnumpy()) val_metric.update(preds, maxvals, score, imgid) res = val_metric.get() return
def transform(data, label): data = nd.transpose(data.astype(np.float32), (2, 0, 1)) / 255 label = label.astype(np.float32) if flip: if np.random.uniform() < 0.5: data = nd.flip(data, axis=2) return data, label
def validate(val_data, val_dataset, net, ctx): if isinstance(ctx, mx.Context): ctx = [ctx] val_metric.reset() from tqdm import tqdm for batch in tqdm(val_data): # data, scale, center, score, imgid = val_batch_fn(batch, ctx) data, scale_box, score, imgid = val_batch_fn(batch, ctx) outputs = [net(X) for X in data] if opt.flip_test: data_flip = [nd.flip(X, axis=3) for X in data] outputs_flip = [net(X) for X in data_flip] outputs_flipback = [flip_heatmap(o, val_dataset.joint_pairs, shift=True) for o in outputs_flip] outputs = [(o + o_flip)/2 for o, o_flip in zip(outputs, outputs_flipback)] if len(outputs) > 1: outputs_stack = nd.concat(*[o.as_in_context(mx.cpu()) for o in outputs], dim=0) else: outputs_stack = outputs[0].as_in_context(mx.cpu()) # preds, maxvals = get_final_preds(outputs_stack, center.asnumpy(), scale.asnumpy()) preds, maxvals = heatmap_to_coord_alpha_pose(outputs_stack, scale_box) # print(preds, maxvals, scale_box) # print(preds, maxvals) # raise val_metric.update(preds, maxvals, score, imgid) res = val_metric.get() return
def transform(data, target_wd, target_ht, is_train, box): """Crop and normnalize an image nd array.""" if box is not None: x, y, w, h = box data = data[y:min(y + h, data.shape[0]), x:min(x + w, data.shape[1])] # Resize to target_wd * target_ht. data = mx.image.imresize(data, target_wd, target_ht) # Normalize in the same way as the pre-trained model. data = data.astype(np.float32) / 255.0 data = (data - mx.nd.array([0.485, 0.456, 0.406])) / mx.nd.array( [0.229, 0.224, 0.225]) if is_train: if random.random() < 0.5: data = nd.flip(data, axis=1) data, _ = mx.image.random_crop(data, (224, 224)) else: data, _ = mx.image.center_crop(data, (224, 224)) # Transpose from (target_wd, target_ht, 3) # to (3, target_wd, target_ht). data = nd.transpose(data, (2, 0, 1)) # If image is greyscale, repeat 3 times to get RGB image. if data.shape[0] == 1: data = nd.tile(data, (3, 1, 1)) return data.reshape((1, ) + data.shape)
def transform_test_flip(data, isf=False): flip_data = nd.flip(data, axis=1) if isf: data = nd.transpose(data, (2, 0, 1)).astype('float32') flip_data = nd.transpose(flip_data, (2, 0, 1)).astype('float32') return data, flip_data return transform_test(data), transform_test(flip_data)
def transform(data, target_wd, target_ht, is_train, box): """Crop and normnalize an image nd array.""" if box is not None: x, y, w, h = box data = data[y:min(y+h, data.shape[0]), x:min(x+w, data.shape[1])] # Resize to target_wd * target_ht. data = mx.image.imresize(data, target_wd, target_ht) # Normalize in the same way as the pre-trained model. data = data.astype(np.float32) / 255.0 data = (data - mx.nd.array([0.485, 0.456, 0.406])) / mx.nd.array([0.229, 0.224, 0.225]) if is_train: if random.random() < 0.5: data = nd.flip(data, axis=1) data, _ = mx.image.random_crop(data, (224, 224)) else: data, _ = mx.image.center_crop(data, (224, 224)) # Transpose from (target_wd, target_ht, 3) # to (3, target_wd, target_ht). data = nd.transpose(data, (2, 0, 1)) # If image is greyscale, repeat 3 times to get RGB image. if data.shape[0] == 1: data = nd.tile(data, (3, 1, 1)) return data.reshape((1,) + data.shape)
def random_augmentation(x, y): for i in range(x.shape[0]): seed = np.random.randint(low=0, high=2, size=3, dtype=int) y_probs = y[i, :(9 * 9 + 1)].reshape((9, 9)) if seed[0]: x[i] = nd.transpose(x[i], (0, 2, 1)) y_probs[i] = nd.transpose(y_probs[i]) if seed[1]: x[i] = nd.flip(x[i], 1) y_probs[i] = nd.flip(y_probs[i], 1) if seed[2]: x[i] = nd.flip(x[i], 2) y_probs[i] = nd.flip(y_probs[i], 2) y_probs = y[i, :-1].flatten() y[i] = np.concatenate((y_probs, y[i, (9 * 9 + 1)]), axis=0) print(x, y) return x, y
def _random_flip(img, label, p=0.5): if nd.random.uniform(low=0, high=1) > p: h, w, _ = img.shape img = nd.flip(img, axis=2) xmin = label[:, 0].copy() xmax = label[:, 2].copy() label[:, 0] = w - xmax label[:, 2] = w - xmin return img, label
def ten_crop(src, size): """Crop 10 regions from an array. This is performed same as: http://chainercv.readthedocs.io/en/stable/reference/transforms.html#ten-crop This method crops 10 regions. All regions will be in shape :obj`size`. These regions consist of 1 center crop and 4 corner crops and horizontal flips of them. The crops are ordered in this order. * center crop * top-left crop * bottom-left crop * top-right crop * bottom-right crop * center crop (flipped horizontally) * top-left crop (flipped horizontally) * bottom-left crop (flipped horizontally) * top-right crop (flipped horizontally) * bottom-right crop (flipped horizontally) Parameters ---------- src : mxnet.nd.NDArray Input image. size : tuple Tuple of length 2, as (width, height) of the cropped areas. Returns ------- mxnet.nd.NDArray The cropped images with shape (10, size[1], size[0], C) """ h, w, _ = src.shape ow, oh = size if h < oh or w < ow: raise ValueError( "Cannot crop area {} from image with size ({}, {})".format( str(size), h, w)) center = src[(h - oh) // 2:(h + oh) // 2, (w - ow) // 2:(w + ow) // 2, :] tl = src[0:oh, 0:ow, :] bl = src[h - oh:h, 0:ow, :] tr = src[0:oh, w - ow:w, :] br = src[h - oh:h, w - ow:w, :] crops = nd.stack(*[center, tl, bl, tr, br], axis=0) crops = nd.concat(*[crops, nd.flip(crops, axis=2)], dim=0) return crops
def forward(self, clips): h, w, _ = clips.shape oh, ow = self.size if h < oh or w < ow: raise ValueError("Cannot crop area {} from image with size \ ({}, {})".format(str(self.size), h, w)) center = clips[(h - oh) // 2:(h + oh) // 2, (w - ow) // 2:(w + ow) // 2, :] tl = clips[0:oh, 0:ow, :] bl = clips[h - oh:h, 0:ow, :] tr = clips[0:oh, w - ow:w, :] br = clips[h - oh:h, w - ow:w, :] crops = nd.concat(*[center, tl, bl, tr, br], dim=2) crops = nd.concat(*[crops, nd.flip(crops, axis=1)], dim=2) return crops
def ten_crop(src, size): """Crop 10 regions from an array. This is performed same as: http://chainercv.readthedocs.io/en/stable/reference/transforms.html#ten-crop This method crops 10 regions. All regions will be in shape :obj`size`. These regions consist of 1 center crop and 4 corner crops and horizontal flips of them. The crops are ordered in this order. * center crop * top-left crop * bottom-left crop * top-right crop * bottom-right crop * center crop (flipped horizontally) * top-left crop (flipped horizontally) * bottom-left crop (flipped horizontally) * top-right crop (flipped horizontally) * bottom-right crop (flipped horizontally) Parameters ---------- src : mxnet.nd.NDArray Input image. size : tuple Tuple of length 2, as (width, height) of the cropped areas. Returns ------- mxnet.nd.NDArray The cropped images with shape (10, size[1], size[0], C) """ h, w, _ = src.shape ow, oh = size if h < oh or w < ow: raise ValueError( "Cannot crop area {} from image with size ({}, {})".format(str(size), h, w)) center = src[(h - oh) // 2:(h + oh) // 2, (w - ow) // 2:(w + ow) // 2, :] tl = src[0:oh, 0:ow, :] bl = src[h - oh:h, 0:ow, :] tr = src[0:oh, w - ow:w, :] br = src[h - oh:h, w - ow:w, :] crops = nd.stack(*[center, tl, bl, tr, br], axis=0) crops = nd.concat(*[crops, nd.flip(crops, axis=2)], dim=0) return crops
def validate(val_data, val_dataset, net, ctx): if isinstance(ctx, mx.Context): ctx = [ctx] val_metric.reset() from tqdm import tqdm for batch in tqdm(val_data): data, scale, center, score, imgid = val_batch_fn(batch, ctx) outputs = [net(X) for X in data] if opt.flip_test: data_flip = [nd.flip(X, axis=3) for X in data] outputs_flip = [net(X) for X in data_flip] outputs_flipback = [ flip_heatmap(o, val_dataset.joint_pairs, shift=True) for o in outputs_flip ] outputs = [(o + o_flip) / 2 for o, o_flip in zip(outputs, outputs_flipback)] if opt.dsnt: outputs = [net_dsnt(X)[0] for X in outputs] if len(outputs) > 1: outputs_stack = nd.concat( *[o.as_in_context(mx.cpu()) for o in outputs], dim=0) else: outputs_stack = outputs[0].as_in_context(mx.cpu()) if opt.dsnt: preds = (outputs_stack - 0.5) * scale.expand_dims( axis=1) + center.expand_dims(axis=1) maxvals = nd.ones(preds.shape[0:2] + (1, )) else: preds, maxvals = get_final_preds(outputs_stack, center.asnumpy(), scale.asnumpy()) val_metric.update(preds, maxvals, score, imgid) metric_name, metric_score = val_metric.get() print("Inference Completed! %s = %.4f" % (metric_name, metric_score)) return
def validate(val_data, val_dataset, net, ctx, opt): if isinstance(ctx, mx.Context): ctx = [ctx] val_metric = COCOKeyPointsMetric(val_dataset, 'coco_keypoints', in_vis_thresh=0) for batch in tqdm(val_data, dynamic_ncols=True): # data, scale, center, score, imgid = val_batch_fn(batch, ctx) data, scale_box, score, imgid = val_batch_fn(batch, ctx) outputs = [net(X) for X in data] if opt.flip_test: data_flip = [nd.flip(X, axis=3) for X in data] outputs_flip = [net(X) for X in data_flip] outputs_flipback = [ flip_heatmap(o, val_dataset.joint_pairs, shift=True) for o in outputs_flip ] outputs = [(o + o_flip) / 2 for o, o_flip in zip(outputs, outputs_flipback)] if len(outputs) > 1: outputs_stack = nd.concat( *[o.as_in_context(mx.cpu()) for o in outputs], dim=0) else: outputs_stack = outputs[0].as_in_context(mx.cpu()) # preds, maxvals = get_final_preds(outputs_stack, center.asnumpy(), scale.asnumpy()) preds, maxvals = heatmap_to_coord_alpha_pose(outputs_stack, scale_box) val_metric.update(preds, maxvals, score, imgid) nullwriter = NullWriter() oldstdout = sys.stdout sys.stdout = nullwriter try: res = val_metric.get() finally: sys.stdout = oldstdout return res
def forward(self, inpt): fwd = self._lstm_fwd(inpt) bwd_inpt = nd.flip(inpt, 0) bwd = self._lstm_bwd(bwd_inpt) bwd = nd.flip(bwd, 0) return nd.concat(fwd, bwd, dim=2)
def test_flip(): b = create_2d_tensor(rows=LARGE_X, columns=SMALL_Y) t = nd.flip(b, axis=0) assert t.shape == (LARGE_X, SMALL_Y) assert np.sum(t[-1, :].asnumpy() == 0) == b.shape[1]
def fliplr(img): '''flip horizontal''' img_flip = nd.flip(img, axis=3) return img_flip
def forward(self, clips): if random.random() < 0.5: clips = nd.flip(clips, axis=1) return clips