(sample, is_training)
return img
augmentation
augmentation
augmentation
def augmentation(sample, is_training):
    """
        augmentation
    """
    image_array = sample.reshape(3, image_size, image_size)
    rgb_array = np.transpose(image_array, (1, 2, 0))
    img = Image.fromarray(rgb_array, 'RGB')

    if is_training:
        # pad and crop
        img = ImageOps.expand(img, (4, 4, 4, 4), fill=0)  # pad to 40 * 40 * 3
        left_top = np.random.randint(9, size=2)  # rand 0 - 8
        img = img.crop((left_top[0], left_top[1], left_top[0] + image_size,
                        left_top[1] + image_size))

        if FLAGS.random_flip_left_right:
            if np.random.randint(2):
                img = img.transpose(Image.FLIP_LEFT_RIGHT)
        if FLAGS.random_flip_up_down:
            if np.random.randint(2):
                img = img.transpose(Image.FLIP_TOP_BOTTOM)
        if FLAGS.random_brightness:
            delta = np.random.uniform(-0.3, 0.3) + 1.
            img = ImageEnhance.Brightness(img).enhance(delta)

    img = np.array(img).astype(np.float32)

    # per_image_standardization
    img_float = img / 255.0
    num_pixels = img_float.size
    img_mean = img_float.mean()
    img_std = img_float.std()
    scale = np.maximum(np.sqrt(num_pixels), img_std)
    img = (img_float - img_mean) / scale

    img = np.transpose(img, (2, 0, 1))
    return img
def augmentation ( sample , is_training ) : image_array = sample . reshape ( 3 , image_size , image_size ) rgb_array = np . transpose ( image_array , ( 1 , 2 , 0 ) ) img = Image . fromarray ( rgb_array , 'RGB' ) if is_training : # pad and crop img = ImageOps . expand ( img , ( 4 , 4 , 4 , 4 ) , fill = 0 ) # pad to 40 * 40 * 3 left_top = np . random . randint ( 9 , size = 2 ) # rand 0 - 8 img = img . crop ( ( left_top [ 0 ] , left_top [ 1 ] , left_top [ 0 ] + image_size , left_top [ 1 ] + image_size ) ) if FLAGS . random_flip_left_right : if np . random . randint ( 2 ) : img = img . transpose ( Image . FLIP_LEFT_RIGHT ) if FLAGS . random_flip_up_down : if np . random . randint ( 2 ) : img = img . transpose ( Image . FLIP_TOP_BOTTOM ) if FLAGS . random_brightness : delta = np . random . uniform ( - 0.3 , 0.3 ) + 1. img = ImageEnhance . Brightness ( img ) . enhance ( delta ) img = np . array ( img ) . astype ( np . float32 ) # per_image_standardization img_float = img / 255.0 num_pixels = img_float . size img_mean = img_float . mean ( ) img_std = img_float . std ( ) scale = np . maximum ( np . sqrt ( num_pixels ) , img_std ) img = ( img_float - img_mean ) / scale img = np . transpose ( img , ( 2 , 0 , 1 ) ) return img