官术网_书友最值得收藏!

Code for visualizing an image 

Let's take a look at how an image can be visualized with the following code:

#import all required lib
import matplotlib.pyplot as plt

%matplotlib inline
import numpy as np
from skimage.io import imread
from skimage.transform import resize
# Load a color image in grayscale
image = imread('sample_digit.png',as_grey=True)
image = resize(image,(28,28),mode='reflect')
print('This image is: ',type(image),
'with dimensions:', image.shape)

plt.imshow(image,cmap='gray')

We obtain the following image as a result:

def visualize_input(img, ax):

ax.imshow(img, cmap='gray')
width, height = img.shape
thresh = img.max()/2.5
for x in range(width):
for y in range(height):
ax.annotate(str(round(img[x][y],2)), xy=(y,x),
horizontalalignment='center',
verticalalignment='center',
color='white' if img[x][y]<thresh else 'black')

fig = plt.figure(figsize = (12,12))
ax = fig.add_subplot(111)
visualize_input(image, ax)

The following result is obtained:

In the previous chapter, we used an MLP-based approach to recognize images. There are two issues with that approach:

  • It increases the number of parameters
  • It only accepts vectors as input, that is, flattening a matrix to a vector

This means we must find a new way to process images, in which 2D information is not completely lost. CNNs address this issue. Furthermore, CNNs accept matrices as input. Convolutional layers preserve spatial structures. First, we define a convolution window, also called a filter, or kernel; then slide this over the image.

主站蜘蛛池模板: 拜泉县| 汉源县| 金堂县| 长垣县| 玉树县| 孟津县| 平原县| 阜新市| 莱州市| 镇沅| 通江县| 河津市| 通江县| 曲阳县| 延津县| 吐鲁番市| 天峨县| 新巴尔虎右旗| 深圳市| 阳新县| 涿鹿县| 耒阳市| 同德县| 维西| 定州市| 化德县| 闽侯县| 五大连池市| 美姑县| 金坛市| 兴安县| 红安县| 禹城市| 龙游县| 台安县| 怀安县| 自贡市| 长葛市| 兴隆县| 日土县| 红安县|