- Practical Convolutional Neural Networks
- Mohit Sewak Md. Rezaul Karim Pradeep Pujari
- 209字
- 2021-06-24 18:58:53
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.
推薦閱讀
- 數據庫基礎教程(SQL Server平臺)
- Visual Studio 2015 Cookbook(Second Edition)
- Oracle RAC 11g實戰指南
- 新型數據庫系統:原理、架構與實踐
- 數據庫應用基礎教程(Visual FoxPro 9.0)
- 3D計算機視覺:原理、算法及應用
- 跟老男孩學Linux運維:MySQL入門與提高實踐
- 數據庫設計與應用(SQL Server 2014)(第二版)
- TextMate How-to
- 大數據數學基礎(Python語言描述)
- 云工作時代:科技進化必將帶來的新工作方式
- 數字孿生
- Hive性能調優實戰
- 工業大數據工程:系統、方法與實踐
- MySQL技術內幕:InnoDB存儲引擎(第2版)