experiments/overlap/augmentations/distortion.py [263:310]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        radius = max(0.05 * self.im_size, 3)

        return {'seed' : seed, 'density' : density, 'eta': eta, 'radius' : radius}

    def transform(self, image, density, eta, radius, seed):
        
        def warp_kernel(point, center, a, b, eta):
            dx = point[0] - center[0]
            dy = point[1] - center[1]
            x2 = dx**2
            y2 = dy**2
            a2 = a**2
            b2 = b**2
            if (y2 >= (b2 - b2*x2/a2)):
                return point

            r = 1.0 / eta
            z = np.sqrt((1.0 - x2/a2 - y2/b2) * (a*b))
            z2 = z**2

            x_angle = np.arccos(dx / np.sqrt(x2+z2))
            angle_1 = np.pi/2 - x_angle
            angle_2 = np.arcsin(np.sin(angle_1)*r)
            angle_2 = np.pi/2 - x_angle - angle_2
            out_x = point[0] - np.tan(angle_2)*z
            #print(np.tan(angle_2)*z)

            y_angle = np.arccos(dy / np.sqrt(y2+z2))
            angle_1 = np.pi/2 - y_angle
            angle_2 = np.arcsin(np.sin(angle_1)*r)
            angle_2 = np.pi/2 - y_angle - angle_2
            out_y = point[1] - np.tan(angle_2)*z

            return np.array([out_x, out_y])

        random_state = np.random.RandomState(seed=seed)
        num = int(density * self.im_size**2)

        out = image.copy().astype(np.float32)
        for i in range(num):
            center = random_state.uniform(low=0, high=self.im_size, size=2)
            l = max(np.floor(center[1]-radius).astype(np.int), 0)
            r = min(np.ceil(center[1]+radius).astype(np.int), self.im_size)
            u = max(np.floor(center[0]-radius).astype(np.int), 0)
            d = min(np.ceil(center[0]+radius).astype(np.int), self.im_size)
            out[u:d,l:r,:] = np.array([[bilinear_interpolation(out, warp_kernel(np.array([y,x]), center, radius, radius, eta)) for x in np.arange(l,r)] for y in np.arange(u,d)])

        return np.clip(out, 0, 255).astype(np.uint8)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



imagenet_c_bar/corrupt.py [862:908]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        radius = max(0.05 * self.im_size, 3)

        return {'seed' : seed, 'density' : density, 'eta': eta, 'radius' : radius}

    def transform(self, image, density, eta, radius, seed):
        
        def warp_kernel(point, center, a, b, eta):
            dx = point[0] - center[0]
            dy = point[1] - center[1]
            x2 = dx**2
            y2 = dy**2
            a2 = a**2
            b2 = b**2
            if (y2 >= (b2 - b2*x2/a2)):
                return point

            r = 1.0 / eta
            z = np.sqrt((1.0 - x2/a2 - y2/b2) * (a*b))
            z2 = z**2

            x_angle = np.arccos(dx / np.sqrt(x2+z2))
            angle_1 = np.pi/2 - x_angle
            angle_2 = np.arcsin(np.sin(angle_1)*r)
            angle_2 = np.pi/2 - x_angle - angle_2
            out_x = point[0] - np.tan(angle_2)*z

            y_angle = np.arccos(dy / np.sqrt(y2+z2))
            angle_1 = np.pi/2 - y_angle
            angle_2 = np.arcsin(np.sin(angle_1)*r)
            angle_2 = np.pi/2 - y_angle - angle_2
            out_y = point[1] - np.tan(angle_2)*z

            return np.array([out_x, out_y])

        random_state = np.random.RandomState(seed=seed)
        num = int(density * self.im_size**2)

        out = image.copy().astype(np.float32)
        for i in range(num):
            center = random_state.uniform(low=0, high=self.im_size, size=2)
            l = max(np.floor(center[1]-radius).astype(np.int), 0)
            r = min(np.ceil(center[1]+radius).astype(np.int), self.im_size)
            u = max(np.floor(center[0]-radius).astype(np.int), 0)
            d = min(np.ceil(center[0]+radius).astype(np.int), self.im_size)
            out[u:d,l:r,:] = np.array([[bilinear_interpolation(out, warp_kernel(np.array([y,x]), center, radius, radius, eta)) for x in np.arange(l,r)] for y in np.arange(u,d)])

        return np.clip(out, 0, 255).astype(np.uint8)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



