def test_random_shift_forward_backward(seed, inshape, shifts, border_mode, constant_value, ctx, func_name): from nbla_test_utils import function_tester rng = np.random.RandomState(seed) inputs = [rng.randn(*inshape).astype(np.float32)] i = nn.Variable(inputs[0].shape, need_grad=True) i.d = inputs[0] # NNabla forward with nn.context_scope(ctx), nn.auto_forward(): o = F.random_shift(i, shifts, border_mode, constant_value, 0, seed) result_shifts = (0, 0, 0) max_correl = 0 for shift_amount in itertools.product(*map( tuple, map(lambda x: range(*x), [(-2, 3) for _ in range(len(inshape))]))): r = scipy_shift(inputs[0], shift_amount, mode=border_mode, cval=constant_value) correl_and_p = pearsonr(o.d.flatten(), r.flatten()) if correl_and_p[0] > max_correl: result_shifts = shift_amount max_correl = correl_and_p[0] ref = scipy_shift(inputs[0], result_shifts, mode=border_mode, cval=constant_value) if shifts is None: shifts = (0, ) * len(inputs[0].shape) for result, shift_range in zip(result_shifts, shifts): assert abs(result) <= shift_range assert_allclose(o.d, ref) assert o.parent.name == func_name # Skipping Backward check g = np.random.randn(*i.shape) i.g = g o_grad = np.random.randn(*o.shape) o.g = o_grad o.parent.backward([i], [o]) ref_grad = i.g.copy() - g # Check accum=False with NaN gradient i.g = np.float32('nan') o.parent.backward([i], [o], [False]) assert not np.any(np.isnan(i.g)) # Check if accum option works i.g[...] = 1 o.g = o_grad o.parent.backward([i], [o], [False]) assert_allclose(i.g, ref_grad, atol=1e-6) # Check if need_grad works i.g[...] = 0 i.need_grad = False o_grad = rng.randn(*i.shape).astype(i.data.dtype) o.backward(o_grad) assert np.all(i.g == 0)
def vectorizer(x, maxh=256, test=False, output_hidden=False): """ Building discriminator network which maps a (B, 1, 28, 28) input to a (B, 100). """ # Define shortcut functions def bn(xx): # Batch normalization return PF.batch_normalization(xx, batch_stat=not test) def downsample2(xx, c): return PF.convolution(xx, c, (3, 3), pad=(1, 1), stride=(2, 2), with_bias=False) assert maxh / 8 > 0 with nn.parameter_scope("dis"): # (1, 28, 28) --> (32, 16, 16) if not test: x_ = F.image_augmentation(x, min_scale=0.9, max_scale=1.08) x2 = F.random_shift(x_, (2, 2)) with nn.parameter_scope("conv1"): c1 = F.elu( bn( PF.convolution(x2, maxh / 8, (3, 3), pad=(3, 3), stride=(2, 2), with_bias=False))) else: with nn.parameter_scope("conv1"): c1 = F.elu( bn( PF.convolution(x, maxh / 8, (3, 3), pad=(3, 3), stride=(2, 2), with_bias=False))) # (32, 16, 16) --> (64, 8, 8) with nn.parameter_scope("conv2"): c2 = F.elu(bn(downsample2(c1, maxh / 4))) # (64, 8, 8) --> (128, 4, 4) with nn.parameter_scope("conv3"): c3 = F.elu(bn(downsample2(c2, maxh / 2))) # (128, 4, 4) --> (256, 4, 4) with nn.parameter_scope("conv4"): c4 = bn( PF.convolution(c3, maxh, (3, 3), pad=(1, 1), with_bias=False)) # (256, 4, 4) --> (1,) with nn.parameter_scope("fc1"): #print "c4fdafafa",c4.shape #f = PF.affine(c4, 100) f = PF.convolution(c4, 1000, (4, 4), pad=(0, 0), with_bias=False) if output_hidden: return f, [c1, c2, c3, c4] return f
def test_random_shift_forward_backward(seed, inshape, shifts, border_mode, ctx, func_name): from nbla_test_utils import function_tester rng = np.random.RandomState(seed) inputs = [rng.randn(*inshape).astype(np.float32)] i = nn.Variable(inputs[0].shape, need_grad=True) i.d = inputs[0] # NNabla forward with nn.context_scope(ctx), nn.auto_forward(): o = F.random_shift(i, shifts, border_mode, 0, seed) result_shifts = (0, 0, 0) max_correl = 0 for shift_amount in itertools.product(*map(tuple, map(lambda x: range(*x), [(-2, 3) for _ in range(len(inshape))]))): r = scipy_shift(inputs[0], shift_amount, mode=border_mode) correl_and_p = pearsonr(o.d.flatten(), r.flatten()) if correl_and_p[0] > max_correl: result_shifts = shift_amount max_correl = correl_and_p[0] ref = scipy_shift(inputs[0], result_shifts, mode=border_mode) if shifts is None: shifts = (0,) * len(inputs[0].shape) for result, shift_range in zip(result_shifts, shifts): assert abs(result) <= shift_range assert np.allclose(o.d, ref) assert o.parent.name == func_name # Skipping Backward check g = np.random.randn(*i.shape) i.g = g o_grad = np.random.randn(*o.shape) o.g = o_grad o.parent.backward([i], [o]) ref_grad = i.g.copy() - g # Check accum=False with NaN gradient i.g = np.float32('nan') o.parent.backward([i], [o], [False]) assert not np.any(np.isnan(i.g)) # Check if accum option works i.g[...] = 1 o.g = o_grad o.parent.backward([i], [o], [False]) assert np.allclose(i.g, ref_grad, atol=1e-6) # Check if need_grad works i.g[...] = 0 i.need_grad = False o_grad = rng.randn(*i.shape).astype(i.data.dtype) o.backward(o_grad) assert np.all(i.g == 0)
def augmentation(h, test, aug): ''' Data augmentation by randomly shifting up to 2 pixels. Args: h (nnabla.Variable): Shape [B, C, H, W] test (bool): Only valid if aug is None, and the augmentation is applied if test=False. aug (None or bool): Whether the augmentation is applied or not. Returns: nnabla.Variable: Shape [B, C, H, W] ''' if aug is None: aug = not test if aug: h = F.random_shift(h, (0, 0, 2, 2), seed=0) return h
def augment(batch, aug_list, p_aug=1.0): if isinstance(p_aug, float): p_aug = nn.Variable.from_numpy_array(p_aug * np.ones((1,))) if "flip" in aug_list: rnd = F.rand(shape=[batch.shape[0], ]) batch_aug = F.random_flip(batch, axes=(2, 3)) batch = F.where( F.greater(F.tile(p_aug, batch.shape[0]), rnd), batch_aug, batch) if "lrflip" in aug_list: rnd = F.rand(shape=[batch.shape[0], ]) batch_aug = F.random_flip(batch, axes=(3,)) batch = F.where( F.greater(F.tile(p_aug, batch.shape[0]), rnd), batch_aug, batch) if "translation" in aug_list and batch.shape[2] >= 8: rnd = F.rand(shape=[batch.shape[0], ]) # Currently nnabla does not support random_shift with border_mode="noise" mask = np.ones((1, 3, batch.shape[2], batch.shape[3])) mask[:, :, :, 0] = 0 mask[:, :, :, -1] = 0 mask[:, :, 0, :] = 0 mask[:, :, -1, :] = 0 batch_int = F.concatenate( batch, nn.Variable().from_numpy_array(mask), axis=0) batch_int_aug = F.random_shift(batch_int, shifts=( batch.shape[2]//8, batch.shape[3]//8), border_mode="nearest") batch_aug = F.slice(batch_int_aug, start=( 0, 0, 0, 0), stop=batch.shape) mask_var = F.slice(batch_int_aug, start=( batch.shape[0], 0, 0, 0), stop=batch_int_aug.shape) batch_aug = batch_aug * F.broadcast(mask_var, batch_aug.shape) batch = F.where( F.greater(F.tile(p_aug, batch.shape[0]), rnd), batch_aug, batch) if "color" in aug_list: rnd = F.rand(shape=[batch.shape[0], ]) rnd_contrast = 1.0 + 0.5 * \ (2.0 * F.rand(shape=[batch.shape[0], 1, 1, 1] ) - 1.0) # from 0.5 to 1.5 rnd_brightness = 0.5 * \ (2.0 * F.rand(shape=[batch.shape[0], 1, 1, 1] ) - 1.0) # from -0.5 to 0.5 rnd_saturation = 2.0 * \ F.rand(shape=[batch.shape[0], 1, 1, 1]) # from 0.0 to 2.0 # Brightness batch_aug = batch + rnd_brightness # Saturation mean_s = F.mean(batch_aug, axis=1, keepdims=True) batch_aug = rnd_saturation * (batch_aug - mean_s) + mean_s # Contrast mean_c = F.mean(batch_aug, axis=(1, 2, 3), keepdims=True) batch_aug = rnd_contrast * (batch_aug - mean_c) + mean_c batch = F.where( F.greater(F.tile(p_aug, batch.shape[0]), rnd), batch_aug, batch) if "cutout" in aug_list and batch.shape[2] >= 16: batch = F.random_erase(batch, prob=p_aug.d[0], replacements=(0.0, 0.0)) return batch
def single_image_augment(image): image = F.image_augmentation(image, contrast=1.0, angle=0.0, flip_lr=True) image = F.random_shift(image, shifts=(4, 4), border_mode="reflect") return image