experiments/overlap/augmentations/color.py [133:180]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        levels = 14-levels

        return {'levels' : levels}

    def transform(self, image, levels):

        index = 0
        color_map = [int(255 * i / (levels -1)) for i in range(levels)]
        div = [int(levels*i / 256) for i in range(256)]
        out = np.zeros_like(image)
        image_copy = image.copy()
        m = np.array([[0,0,0],[0,0,7],[3,5,1]])
        

        for y in range(self.im_size):
            reverse = ((y % 1) == 1)
            if reverse:
                index = y*self.im_size + self.im_size - 1
                direction = -1
            else:
                index = y*self.im_size
                direction = 1

            for x in range(self.im_size):
                curr_val = image_copy[index//self.im_size, index%self.im_size,:]

                new_val = np.array([color_map[div[c]] for c in curr_val])
                out[index//self.im_size, index%self.im_size,:] = new_val

                e = curr_val - new_val

                for i in [-1,0,1]:
                    iy = y+i
                    if iy > 0 and iy < self.im_size:
                        for j in [-1,0,1]:
                            jx = x+j
                            if jx > 0 and jx < self.im_size:
                                if reverse:
                                    w = m[(i+1),-j+1]
                                else:
                                    w = m[(i+1),j+1]
                                if w != 0:
                                    k = index - j if reverse else index + j
                                    curr_val = image_copy[k//self.im_size, k%self.im_size,:].astype(np.float32)
                                    curr_val = np.clip(curr_val + e * w/np.sum(m),0,255).astype(np.uint8)
                                    image_copy[k//self.im_size,k%self.im_size,:] = curr_val
                index += direction
        return np.clip(out, 0, 255).astype(np.uint8)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



imagenet_c_bar/corrupt.py [548:595]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        levels = 14-levels

        return {'levels' : levels}

    def transform(self, image, levels):

        index = 0
        color_map = [int(255 * i / (levels -1)) for i in range(levels)]
        div = [int(levels*i / 256) for i in range(256)]
        out = np.zeros_like(image)
        image_copy = image.copy()
        m = np.array([[0,0,0],[0,0,7],[3,5,1]])
        

        for y in range(self.im_size):
            reverse = ((y % 1) == 1)
            if reverse:
                index = y*self.im_size + self.im_size - 1
                direction = -1
            else:
                index = y*self.im_size
                direction = 1

            for x in range(self.im_size):
                curr_val = image_copy[index//self.im_size, index%self.im_size,:]

                new_val = np.array([color_map[div[c]] for c in curr_val])
                out[index//self.im_size, index%self.im_size,:] = new_val

                e = curr_val - new_val

                for i in [-1,0,1]:
                    iy = y+i
                    if iy > 0 and iy < self.im_size:
                        for j in [-1,0,1]:
                            jx = x+j
                            if jx > 0 and jx < self.im_size:
                                if reverse:
                                    w = m[(i+1),-j+1]
                                else:
                                    w = m[(i+1),j+1]
                                if w != 0:
                                    k = index - j if reverse else index + j
                                    curr_val = image_copy[k//self.im_size, k%self.im_size,:].astype(np.float32)
                                    curr_val = np.clip(curr_val + e * w/np.sum(m),0,255).astype(np.uint8)
                                    image_copy[k//self.im_size,k%self.im_size,:] = curr_val
                index += direction
        return np.clip(out, 0, 255).astype(np.uint8)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



