1. Kernels & Convolutions

In [2]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
In [3]:
path_photo_a = 'snapshot/teamA.jpg'
In [7]:
from keras.applications.vgg16 import VGG16, preprocess_input, decode_predictions
from keras.models import Model
from keras.preprocessing import image
from keras.optimizers import SGD
In [8]:
photo_a = image.load_img(path_photo_a)
plt.imshow(photo_a)
plt.xticks([])
plt.yticks([])
plt.tight_layout()
plt.show()
In [9]:
photo_a = image.load_img(path_photo_a,target_size=(224,224))
plt.imshow(photo_a)
plt.xticks([])
plt.yticks([])
plt.tight_layout()
plt.show()
In [10]:
x = image.img_to_array(photo_a)
x = np.expand_dims(x, axis=0)
x.shape
Out[10]:
(1, 224, 224, 3)
In [11]:
type(x)
Out[11]:
numpy.ndarray
In [12]:
datagen = image.ImageDataGenerator(featurewise_center=False,
                                  featurewise_std_normalization=False,
                                  rotation_range=20,
                                  width_shift_range=0.2,
                                  height_shift_range=0.2,
                                  horizontal_flip=True)
datagen.fit(x)
In [13]:
fig, axes = plt.subplots(2, 8, figsize=(10, 3))
axes = np.ravel(axes)
num_augmented = 0
for x_aug in datagen.flow(x, batch_size=1):
    if num_augmented >= 16:
        break
    x_aug = 255 - x_aug
    axes[num_augmented].imshow(x_aug[0], interpolation="nearest")
    axes[num_augmented].set_xticks([])
    axes[num_augmented].set_yticks([])
    num_augmented += 1
plt.xticks([])
plt.yticks([])
plt.tight_layout()
plt.show()
In [14]:
from scipy import ndimage
from skimage import color, io
from skimage.measure import block_reduce
In [15]:
gray_photo_a = color.rgb2gray(io.imread(path_photo_a));
filtered_photo_a = block_reduce(gray_photo_a, block_size=(4, 4), func=np.max);
filtered2_photo_a = block_reduce(gray_photo_a, block_size=(8, 8), func=np.max)

plt.figure(figsize=(15, 5))

plt.subplot(131)
plt.title("original (bw) {:s}".format(str(gray_photo_a.shape)))
plt.xticks([])
plt.yticks([])
plt.imshow(gray_photo_a, cmap="gray")

plt.subplot(132)
plt.title("low resolution {:s}".format(str(filtered_photo_a.shape)))
plt.xticks([])
plt.yticks([])
plt.imshow(filtered_photo_a, cmap="gray")

plt.subplot(133)
plt.title("low resolution {:s}".format(str(filtered2_photo_a.shape)))
plt.xticks([])
plt.yticks([])
plt.imshow(filtered2_photo_a, cmap="gray")
plt.tight_layout()

plt.savefig('snapshot/reduce_resolution.jpg',bbox_inches='tight')

Basic Convolutions

In [16]:
identidy = np.array([[0, 0, 0],
                    [0, 1, 0],
                    [0, 0, 0]])
photo_identidy = ndimage.convolve(gray_photo_a, identidy, mode="constant", cval=0.0)
plt.title("identidy")
plt.imshow(photo_identidy, cmap="gray")
plt.xticks([])
plt.yticks([])
plt.savefig('snapshot/identidy.jpg',bbox_inches='tight')
plt.show()