Download No description has been provided for this image

הרצאה 11 - CNN

Setup

In [ ]:
## Importing packages
import os # A build in package for interacting with the OS. For example to create a folder.
import numpy as np  # Numerical package (mainly multi-dimensional arrays and linear algebra)
import pandas as pd  # A package for working with data frames
import matplotlib.pyplot as plt  # A plotting package
import imageio  # A package to read and write image (is used here to save gif images)
import tabulate  # A package from pretty printing tables
from graphviz import Digraph  # A package for plothing graphs (of nodes and edges)

## Setup matplotlib to output figures into the notebook
## - To make the figures interactive (zoomable, tooltip, etc.) use ""%matplotlib notebook" instead
%matplotlib inline

## Setting some nice matplotlib defaults
plt.rcParams['figure.figsize'] = (4.5, 4.5)  # Set default plot's sizes
plt.rcParams['figure.dpi'] = 120  # Set default plot's dpi (increase fonts' size)
plt.rcParams['axes.grid'] = True  # Show grid by default in figures

## Auxiliary function for prining equations, pandas tables and images in cells output
from IPython.core.display import display, HTML, Latex, Markdown

## Create output folder
if not os.path.isdir('./output'):
    os.mkdir('./output')
In [ ]:
img = imageio.imread('./assets/face.jpg')

fig = plt.figure(figsize=(3,3))
ax = fig.add_axes([0, 0, 1, 1])
ax.axis('off')
ax.imshow(img)
Out[ ]:
<matplotlib.image.AxesImage at 0x7f9884a3b278>
No description has been provided for this image
In [ ]:
from skimage import color
img_gray = color.rgb2gray(img)

fig = plt.figure(figsize=(3,3))
ax = fig.add_axes([0, 0, 1, 1])
ax.axis('off')
ax.imshow(img_gray, cmap='gray')
Out[ ]:
<matplotlib.image.AxesImage at 0x7f9884a13470>
No description has been provided for this image
In [ ]:
from scipy import signal
In [ ]:
h1 = np.array([[-1, 2, -1], [-1, 2, -1], [-1, 2, -1]])
h2 = np.array([[-1, -1, -1], [2, 2, 2], [-1, -1, -1]])
h3 = np.array([[-0.2, 0.25, 0.25], [-0.2, 0.25, 0.25], [-0.2, -0.2, -0.2]])
h4 = np.array([[1, -2, 1], [-2, 4, -2], [1, -2, 1]])

ch1 = np.maximum(signal.convolve2d(img_gray, h1, mode='valid'), 0)
ch2 = np.maximum(signal.convolve2d(img_gray, h2, mode='valid'), 0)
ch3 = np.maximum(signal.convolve2d(img_gray, h3, mode='valid'), 0)
ch4 = np.maximum(signal.convolve2d(img_gray, h4, mode='valid'), 0)

fig = plt.figure(figsize=(3,3))
ax = fig.add_axes([0, 0, 1, 1])
ax.axis('off')
ax.imshow(ch1, cmap='gray')
fig.savefig('./output/face_vertical.png', dpi=240)

fig = plt.figure(figsize=(3,3))
ax = fig.add_axes([0, 0, 1, 1])
ax.axis('off')
ax.imshow(ch2, cmap='gray')
fig.savefig('./output/face_horizontal.png', dpi=240)

fig = plt.figure(figsize=(3,3))
ax = fig.add_axes([0, 0, 1, 1])
ax.axis('off')
ax.imshow(ch3, cmap='gray')
fig.savefig('./output/face_corner.png', dpi=240)

fig = plt.figure(figsize=(3,3))
ax = fig.add_axes([0, 0, 1, 1])
ax.axis('off')
ax.imshow(ch4, cmap='gray')
fig.savefig('./output/face_dot.png', dpi=240)
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
In [ ]:
h = np.array([[0, 1,- 1], [0, 1, -1], [0, 1, -1]])
ch2 = np.maximum(signal.convolve2d(img_gray, h, mode='valid'), 0)

fig = plt.figure(figsize=(3,3))
ax = fig.add_axes([0, 0, 1, 1])
ax.axis('off')
ax.imshow(ch2, cmap='gray')
Out[ ]:
<matplotlib.image.AxesImage at 0x7f9882673eb8>
No description has been provided for this image
In [ ]: