In [1]:
import numpy as np

In [2]:
import matplotlib.pyplot as plt

In [3]:
from PIL import Image

In [4]:
from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img


Using TensorFlow backend.

In [59]:
img =  load_img('/usr/not-backed-up/temp/NYU/images/0000.png')

In [90]:
img.save('0.png')

In [26]:
npimg = img_to_array(img)

In [88]:
npimg = img_to_array(img)
aug = augment(npimg)
array_to_img(aug)


Out[88]:

In [89]:
npaug = aug.reshape((1,) + aug.shape)
datagen = ImageDataGenerator(
    channel_shift_range=10,
    horizontal_flip=True,
    )
i = 0;
nrows = 6;
ncols = 3;
fig, axes = plt.subplots(nrows, ncols,figsize=(20,20))
for batch in datagen.flow(npaug, batch_size=1,save_to_dir='./output/'):
    i += 1;
    plt.subplot(nrows,ncols,i)
    plt.imshow(array_to_img(batch[0]))
    if i == nrows*ncols:
        break
plt.show()



In [58]:
npimg = npimg.reshape((1,) + npimg.shape)

In [231]:
datagen = ImageDataGenerator(
    channel_shift_range=10,
    horizontal_flip=True,
    preprocessing_function=augment
    )

In [232]:
i = 0;
nrows = 6;
ncols = 3;
fig, axes = plt.subplots(nrows, ncols,figsize=(20,20))
for batch in datagen.flow(npimg, batch_size=1):
    i += 1;
    plt.subplot(nrows,ncols,i)
    plt.imshow(array_to_img(batch[0]))
    if i == nrows*ncols:
        break
plt.show()


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-232-095a869f2360> in <module>()
      3 ncols = 3;
      4 fig, axes = plt.subplots(nrows, ncols,figsize=(20,20))
----> 5 for batch in datagen.flow(npimg, batch_size=1):
      6     i += 1;
      7     plt.subplot(nrows,ncols,i)

/usr/not-backed-up/anaconda/lib/python3.6/site-packages/keras/preprocessing/image.py in __next__(self, *args, **kwargs)
    735 
    736     def __next__(self, *args, **kwargs):
--> 737         return self.next(*args, **kwargs)
    738 
    739 

/usr/not-backed-up/anaconda/lib/python3.6/site-packages/keras/preprocessing/image.py in next(self)
    814             x = self.image_data_generator.random_transform(x.astype(K.floatx()))
    815             x = self.image_data_generator.standardize(x)
--> 816             batch_x[i] = x
    817         if self.save_to_dir:
    818             for i in range(current_batch_size):

ValueError: could not broadcast input array from shape (240,320,3) into shape (427,561,3)

In [8]:
import scipy.ndimage as ndi

def random_rotation(x, rg, row_axis=0, col_axis=1, channel_axis=2,
                    fill_mode='nearest', cval=0., order=3):
    """Performs a random rotation of a Numpy image tensor.
    # Arguments
        x: Input tensor. Must be 3D.
        rg: Rotation range, in degrees.
        row_axis: Index of axis for rows in the input tensor.
        col_axis: Index of axis for columns in the input tensor.
        channel_axis: Index of axis for channels in the input tensor.
        fill_mode: Points outside the boundaries of the input
            are filled according to the given mode
            (one of `{'constant', 'nearest', 'reflect', 'wrap'}`).
        cval: Value used for points outside the boundaries
            of the input if `mode='constant'`.
        order: The order of the spline interpolation, default is 3. 
            The order has to be in the range 0-5.
    # Returns
        Rotated Numpy image tensor.
    """
    theta = np.pi / 180 * np.random.uniform(-rg, rg)
    rotation_matrix = np.array([[np.cos(theta), -np.sin(theta), 0],
                                [np.sin(theta), np.cos(theta), 0],
                                [0, 0, 1]])

    h, w = x.shape[row_axis], x.shape[col_axis]
    transform_matrix = transform_matrix_offset_center(rotation_matrix, h, w)
    x = apply_transform(x, transform_matrix, channel_axis, fill_mode, cval, order)
    return x

def transform_matrix_offset_center(matrix, x, y):
    o_x = float(x) / 2 + 0.5
    o_y = float(y) / 2 + 0.5
    offset_matrix = np.array([[1, 0, o_x], [0, 1, o_y], [0, 0, 1]])
    reset_matrix = np.array([[1, 0, -o_x], [0, 1, -o_y], [0, 0, 1]])
    transform_matrix = np.dot(np.dot(offset_matrix, matrix), reset_matrix)
    return transform_matrix

def apply_transform(x,
                    transform_matrix,
                    channel_axis=0,
                    fill_mode='nearest',
                    cval=0.,
                    order = 3):
    """Apply the image transformation specified by a matrix.
    # Arguments
        x: 2D numpy array, single image.
        transform_matrix: Numpy array specifying the geometric transformation.
        channel_axis: Index of axis for channels in the input tensor.
        fill_mode: Points outside the boundaries of the input
            are filled according to the given mode
            (one of `{'constant', 'nearest', 'reflect', 'wrap'}`).
        cval: Value used for points outside the boundaries
            of the input if `mode='constant'`.
        order: The order of the spline interpolation, default is 3. 
            The order has to be in the range 0-5.
    # Returns
        The transformed version of the input.
    """
    x = np.rollaxis(x, channel_axis, 0)
    final_affine_matrix = transform_matrix[:2, :2]
    final_offset = transform_matrix[:2, 2]
    channel_images = [ndi.interpolation.affine_transform(
        x_channel,
        final_affine_matrix,
        final_offset,
        order=order,
        mode=fill_mode,
        cval=cval) for x_channel in x]
    x = np.stack(channel_images, axis=0)
    x = np.rollaxis(x, 0, channel_axis + 1)
    return x

In [9]:
from math import ceil
def random_resize(npimg, minwidth=320, minheight=240, resample=Image.BICUBIC):
    """Returns a random resized copy of a Numpy image tensor.
    # Arguments
        npimg: Input tensor. Must be 3D.
        minwidth: Minimum width of resized image.
        minheight: Minimum height of resized image.
        resample: An optional resampling filter. 
            This can be one of PIL.Image.NEAREST, PIL.Image.BOX, PIL.Image.BILINEAR,
            PIL.Image.HAMMING, PIL.Image.BICUBIC or PIL.Image.LANCZOS. 
            If omitted, it is set PIL.Image.BICUBIC.
    # Returns
        Resized Numpy image tensor.
    """
    img = array_to_img(npimg)
    smallestResizeFactor = max(ceil((minwidth / img.size[0])*100) , ceil((minheight / img.size[1])*100))
    resizeFactor = 0.01 * np.random.randint(smallestResizeFactor,140) # In range of [Min_Width_Height, 1.4*original_size] 
    img = img.resize((int(img.size[0]*resizeFactor),int(img.size[1]*resizeFactor)),resample=resample)
    return img_to_array(img)

In [10]:
from keras import backend as K

def random_crop(npimg, width=320, height=240, data_format=None):
    """Returns a random cropped copy of a Numpy image tensor.
    # Arguments
        npimg: Input tensor. Must be 3D.
        width: width of crop region.
        height: height of crop region.
        data_format: Image data format. One of "channels_first" or "channels_last"
    # Returns
        Cropped Numpy image tensor.
    """
    if npimg.ndim != 3:
        raise ValueError('Expected image array to have rank 3 (single image). '
                         'Got array with shape:', npimg.shape)

    if data_format is None:
        data_format = K.image_data_format()
    if data_format not in {'channels_first', 'channels_last'}:
        raise ValueError('Invalid data_format:', data_format)

    # Original Numpy array x has format (height, width, channel)
    # or (channel, height, width)
    if data_format == 'channels_first':
        npimg = npimg.transpose(1, 2, 0)
    
    maxHeightIndex = npimg.shape[0] - height
    heightIndex = np.random.randint(0,maxHeightIndex)
    
    maxWidthIndex = npimg.shape[1] - width
    widthIndex = np.random.randint(0,maxWidthIndex)
    
    crop = npimg[heightIndex:heightIndex+height,widthIndex:widthIndex+width,:]
    
    if data_format == 'channels_first':
        crop = crop.transpose(2, 0, 1)
    
    return crop

In [277]:
fig, axes = plt.subplots(2, 3,figsize=(20,20))
seed = 5
plt.subplot(2,3,1)
plt.imshow(img)
plt.subplot(2,3,2)
plt.imshow(norm)
plt.subplot(2,3,3)
plt.imshow(mask)
np.random.seed(seed)
plt.subplot(2,3,4)
plt.imshow(array_to_img(augment(img)))
np.random.seed(seed)
plt.subplot(2,3,5)
plt.imshow(array_to_img(augment(norm)))
np.random.seed(seed)
plt.subplot(2,3,6)
plt.imshow(array_to_img(augment(mask)))
plt.show()



In [67]:
from PIL import ImageEnhance as IE

In [88]:
IE.Sharpness(img).enhance(2)


Out[88]:

In [11]:
def augment(npimg):
    return random_crop((random_rotation(random_resize(npimg),5)))

In [23]:
array_to_img(augment(npimg[0]))


Out[23]:

In [256]:
img =  load_img('/usr/not-backed-up/temp/NYU/images/0000.png')

In [257]:
norm =  load_img('/usr/not-backed-up/temp/NYU/normals/0000.png')

In [258]:
mask =  load_img('/usr/not-backed-up/temp/NYU/masks/0000.png')

In [265]:
np.random.seed(1)
array_to_img(augment(img_to_array(img)))


Out[265]:

In [266]:
np.random.seed(1)
array_to_img(augment(img_to_array(norm)))


Out[266]:

In [267]:
np.random.seed(1)
array_to_img(augment(img_to_array(mask)))


Out[267]:

In [297]:
np.linalg.norm(npimg[0,0,0,:]/255)


Out[297]:
0.77906084

In [289]:
tmp = np.array([1,2,2])

np.linalg.norm(tmp)


Out[289]:
3.0

In [290]:
tmp/3


Out[290]:
array([ 0.33333333,  0.66666667,  0.66666667])

In [292]:
np.linalg.norm(tmp/3)


Out[292]:
1.0

In [293]:
from sklearn.preprocessing import normalize

In [335]:
normalize(img_to_array(norm))


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-335-95541a949d7f> in <module>()
----> 1 normalize(img_to_array(norm))

/usr/not-backed-up/anaconda/lib/python3.6/site-packages/sklearn/preprocessing/data.py in normalize(X, norm, axis, copy, return_norm)
   1342 
   1343     X = check_array(X, sparse_format, copy=copy, warn_on_dtype=True,
-> 1344                     estimator='the normalize function', dtype=FLOAT_DTYPES)
   1345     if axis == 0:
   1346         X = X.T

/usr/not-backed-up/anaconda/lib/python3.6/site-packages/sklearn/utils/validation.py in check_array(array, accept_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, warn_on_dtype, estimator)
    403         if not allow_nd and array.ndim >= 3:
    404             raise ValueError("Found array with dim %d. %s expected <= 2."
--> 405                              % (array.ndim, estimator_name))
    406         if force_all_finite:
    407             _assert_all_finite(array)

ValueError: Found array with dim 3. the normalize function expected <= 2.

In [308]:
img_to_array(norm)[0,0,:]/np.linalg.norm(img_to_array(norm)[0,0,:])


Out[308]:
array([ 0.86701947,  0.43350974,  0.24565552], dtype=float32)

In [379]:
l2norm=np.linalg.norm(img_to_array(norm),axis=2)
array_to_img(np.round(np.divide(img_to_array(norm),l2norm.reshape(l2norm.shape+(1,)))*255))


Out[379]:

In [367]:
norm


Out[367]:

In [320]:
np123 = np.array([[[1,1,1,1],[1,1,1,1]],[[2,2,2,2],[2,2,2,2]],[[2,2,2,2],[2,2,2,2]]])

In [329]:
np.linalg.norm(np123,axis=0)


Out[329]:
array([[ 3.,  3.,  3.,  3.],
       [ 3.,  3.,  3.,  3.]])

In [324]:
np123[:,0,0]


Out[324]:
array([1, 2, 2])

In [323]:
img_to_array(norm).shape


Out[323]:
(427, 561, 3)

In [358]:
npnorm = img_to_array(norm)

if np.array_equal(np.divide(npnorm,255) , np.divide(npnorm,ones)):
    print('Ow')

In [357]:
ones = np.array([255]*427*561).reshape(427,561,1)

In [359]:
np.divide(npnorm,255)


Out[359]:
array([[[ 0.94117647,  0.47058824,  0.26666668],
        [ 0.93725491,  0.4509804 ,  0.26274511],
        [ 0.92941177,  0.43529412,  0.25490198],
        ..., 
        [ 0.627451  ,  0.33333334,  0.04313726],
        [ 0.63529414,  0.33333334,  0.04705882],
        [ 0.64705884,  0.32156864,  0.05490196]],

       [[ 0.94117647,  0.46666667,  0.26666668],
        [ 0.93725491,  0.4509804 ,  0.25882354],
        [ 0.93333334,  0.42745098,  0.26274511],
        ..., 
        [ 0.61960787,  0.33333334,  0.04313726],
        [ 0.62352943,  0.33333334,  0.04313726],
        [ 0.63529414,  0.32156864,  0.05098039]],

       [[ 0.94117647,  0.45490196,  0.26666668],
        [ 0.92941177,  0.44313726,  0.25098041],
        [ 0.93333334,  0.42352942,  0.26274511],
        ..., 
        [ 0.62352943,  0.34509805,  0.03921569],
        [ 0.63137257,  0.34509805,  0.03921569],
        [ 0.64313728,  0.33333334,  0.04705882]],

       ..., 
       [[ 0.58431375,  0.35294119,  0.96862745],
        [ 0.53725493,  0.32549021,  0.96470588],
        [ 0.45882353,  0.32549021,  0.96470588],
        ..., 
        [ 0.07058824,  0.24313726,  0.45882353],
        [ 0.08627451,  0.22352941,  0.44313726],
        [ 0.10588235,  0.19607843,  0.42745098]],

       [[ 0.54901963,  0.3764706 ,  0.98039216],
        [ 0.50196081,  0.35294119,  0.97647059],
        [ 0.43921569,  0.34509805,  0.96862745],
        ..., 
        [ 0.07450981,  0.23529412,  0.4627451 ],
        [ 0.08627451,  0.21960784,  0.45490196],
        [ 0.09803922,  0.20392157,  0.44705883]],

       [[ 0.53333336,  0.41176471,  0.98823529],
        [ 0.50196081,  0.39215687,  0.98823529],
        [ 0.44705883,  0.38431373,  0.98039216],
        ..., 
        [ 0.08235294,  0.22745098,  0.4509804 ],
        [ 0.07843138,  0.22745098,  0.45882353],
        [ 0.09019608,  0.20784314,  0.45490196]]], dtype=float32)

In [361]:
np.divide(npnorm,ones).shape


Out[361]:
(427, 561, 3)

In [368]:
normnorm = np.divide(img_to_array(norm),l2norm.reshape(l2norm.shape+(1,)))

In [376]:
np.linalg.norm(normnorm[412,514,:])


Out[376]:
1.0

In [375]:



0
1
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-375-13d4dd4a0fb5> in <module>()
----> 1 for i,j in range(2),range(3):
      2     print(i)
      3     print(j)

ValueError: too many values to unpack (expected 2)

In [378]:
round(3.54)


Out[378]:
4

In [381]:
n551 = load_img('./551AlignedNormals.png')

In [382]:
n551


Out[382]:

In [399]:
l2norm = np.linalg.norm(img_to_array(n551),axis=2)
#np.amax(np.divide(img_to_array(n551),l2norm.reshape(l2norm.shape+(1,))))
np.amax(l2norm)


Out[399]:
255.80266

In [401]:
np.linalg.norm(img_to_array(n551),axis=2)


Out[401]:
array([[ 255.00196838,  255.19599915,  255.39186096, ...,  255.1078186 ,
         254.86859131,  255.51124573],
       [ 254.32460022,  254.94509888,  254.845047  , ...,  255.55038452,
         254.86859131,  254.55255127],
       [ 255.00196838,  255.11958313,  254.87643433, ...,  255.24497986,
         255.01763916,  254.86663818],
       ..., 
       [ 254.61146545,  254.66252136,  254.98431396, ...,  254.74890137,
         254.61146545,  254.71160889],
       [ 255.47993469,  254.82739258,  255.24302673, ...,  254.62913513,
         254.92352295,  255.22735596],
       [ 255.1587677 ,  255.1078186 ,  255.2371521 , ...,  255.52104187,
         255.22735596,  254.95294189]], dtype=float32)

In [402]:
img_to_array(n551)/255


Out[402]:
array([[[ 0.78039217,  0.04705882,  0.62352943],
        [ 0.82352942,  0.        ,  0.56862748],
        [ 0.90196079,  0.12941177,  0.41568628],
        ..., 
        [ 0.4627451 ,  0.32941177,  0.82352942],
        [ 0.47843137,  0.33333334,  0.81176472],
        [ 0.49019608,  0.33333334,  0.80784315]],

       [[ 0.7764706 ,  0.05490196,  0.62352943],
        [ 0.81960785,  0.        ,  0.57254905],
        [ 0.89803922,  0.11372549,  0.42352942],
        ..., 
        [ 0.47058824,  0.33333334,  0.81960785],
        [ 0.47843137,  0.33333334,  0.81176472],
        [ 0.48627451,  0.33725491,  0.80392158]],

       [[ 0.78039217,  0.04705882,  0.62352943],
        [ 0.81176472,  0.02352941,  0.58431375],
        [ 0.87843138,  0.09803922,  0.46666667],
        ..., 
        [ 0.46666667,  0.32549021,  0.82352942],
        [ 0.48235294,  0.32941177,  0.81176472],
        [ 0.49411765,  0.32941177,  0.80392158]],

       ..., 
       [[ 0.23137255,  0.80392158,  0.54509807],
        [ 0.22745098,  0.81568629,  0.52941179],
        [ 0.25882354,  0.80784315,  0.52941179],
        ..., 
        [ 0.50980395,  0.85882354,  0.02352941],
        [ 0.52156866,  0.8509804 ,  0.02745098],
        [ 0.52156866,  0.8509804 ,  0.03921569]],

       [[ 0.24313726,  0.78823531,  0.56862748],
        [ 0.23921569,  0.80000001,  0.54901963],
        [ 0.26274511,  0.80000001,  0.5411765 ],
        ..., 
        [ 0.56470591,  0.82352942,  0.        ],
        [ 0.56078434,  0.82745099,  0.01568628],
        [ 0.56862748,  0.82352942,  0.01568628]],

       [[ 0.24705882,  0.78823531,  0.56470591],
        [ 0.23529412,  0.80784315,  0.5411765 ],
        [ 0.27058825,  0.80000001,  0.53725493],
        ..., 
        [ 0.57647061,  0.81960785,  0.00392157],
        [ 0.56862748,  0.82352942,  0.01568628],
        [ 0.57254905,  0.81960785,  0.00784314]]], dtype=float32)

In [ ]: