卷積的理解

2021-08-15 19:31:45 字數 3926 閱讀 5340

最近大火人工智慧,深度學習,各種提到卷積神經網路。不搞懂卷積,好像學習不下去了。

於是就去看各種文章,以下是三篇解釋的比較不錯的,附上鏈結。

我對卷積的理解

最容易理解的對卷積(convolution)的解釋

理解深度學習中的卷積

這裡做一下總結吧。

離散的卷積公式:g(

n)=∑

+∞i=

−∞f(

i)h(

n−i)

g(n)=∑i=−∞+∞f(i)h(n−i)

就假設h(x

)h(x)

是某種藥的藥效,第一天100%,第二天80%,第三天60%,第x天……醬紫。 f(

x)f(x)

是小明同學(小明好慘..)第x天吃的藥量。 g(

x)g(x)

是第x天,小明體內的藥效。則 g

……符合上面的公式。其實,h(

n−i)

h(n−i)

可以認為是h(

i)h(i)

先翻轉h(−

i)h(−i)

, 再右移nn

得到的。

s所以,卷積的過程,可以認為是:

(反轉),移動,乘積,求和**

在計算機領域,咱還是比較關心離散的公式: f[

x,y]

∗g[x

,y]=

∑∞n1

=−∞∑

∞n2=

−∞f[

n1,n

2]⋅g

[x−n

1,y−

n2]f[x,y]∗g[x,y]=∑n1=−∞∞∑n2=−∞∞f[n1,n2]⋅g[x−n1,y−n2]

二維卷積就是一維卷積的擴充套件,原理差不多。核心還是(反轉),移動,乘積,求和。這裡二維的反轉就是將卷積核沿反對角線翻轉。比如

s直接上個python**,最為直觀:

#參考**:

#skimage沒安裝的話,可以執行:pip install scikit-image

from skimage import io, color

import matplotlib.pyplot as plt

import numpy as np

from skimage import exposure

import pylab

defconvolve2d

(image, kernel):

# this function which takes an image and a kernel

# and returns the convolution of them

# args:

# image: a numpy array of size [image_height, image_width].

# kernel: a numpy array of size [kernel_height, kernel_width].

# returns:

# a numpy array of size [image_height, image_width] (convolution output).

print('kernel = ')

print(kernel)

kernel = np.flipud(np.fliplr(kernel)) # flip the kernel

print('after flip, now kernel = ')

print(kernel)

output = np.zeros_like(image) # convolution output

# add zero padding to the input image

#左右上下要擴充乙個畫素, 否則邊沿的畫素,如(0,0)點,沒法計算

image_padded = np.zeros((image.shape[0] + 2, image.shape[1] + 2))

image_padded[1:-1, 1:-1] = image

for x in range(image.shape[1]): # loop over every pixel of the image

for y in range(image.shape[0]):

# element-wise multiplication of the kernel and the image

output[y,x]=(kernel*image_padded[y:y+3,x:x+3]).sum()

return output

img = io.imread('image.png') # load the image

img = color.rgb2gray(img) # convert the image to grayscale (1 channel)

image_equalized = exposure.equalize_adapthist(img/np.max(np.abs(img)), clip_limit=0.03)

plt.imshow(image_equalized, cmap=plt.cm.gray)

plt.axis('off')

plt.show()

# convolve the sharpen kernel and the image

kernel = np.array([[0,-1,0],[-1,5,-1],[0,-1,0]])

image_sharpen = convolve2d(img,kernel)

print('\n first 5 columns and rows of the image_sharpen matrix: \n', image_sharpen[:5,:5]*255)

# plot the filtered image

plt.imshow(image_sharpen, cmap=plt.cm.gray)

plt.axis('off')

plt.show()

其中,以下**就是將卷積核矩陣翻**

kernel = np.flipud(np.fliplr(kernel))
以下**,就是將卷積核與影象的某塊3x3矩陣 乘積+求和:

output[y,x]=(kernel*image_padded[y:y+3,x:x+3]).sum()
執行結果

卷積前:

卷積後:

卷積的理解

卷積,看著好煩人的東西啊。直接上來公式 好氣啊,什麼意思啊,一頭霧水。換種方式。假設你挨一拳f t 而這一拳的響應為h t 這樣後果疼痛就是fh。這很合理。你想啊,你一秒鐘挨一拳,那麼最新的一拳和舊的一拳都有疼痛的後果。這個痛是有多痛呢?你現在的拳,和之前拳的殘留。這樣求總痛怎麼求呢?依此疊加。注意...

卷積的理解

訊號處理中的乙個重要運算是卷積.初學卷積的時候,往往是在連續的情形,兩個函式f x g x 的卷積,是 f u g x u du 當然,證明卷積的一些性質並不困難,比如交換,結合等等,但是對於卷積運算的來處,初學者就不甚了了。其實,從離散的情形看卷積,或許更加清楚,對於兩個序列f n g n 一般可...

卷積的理解

卷積的理解 1.卷積的定義 對這個表示式,積分變數是 f 是輸入函式,g x 是系統響應。設橫座標取x是某個點,其表示的實際含義是求x左邊無窮距離到x右邊無窮距離輸入函式乘以對應權重的積分。離散化形式 相當於對連續函式取乙個個離散的點。例子 試想小明有一段時間每天都要去輸液,輸的藥會在身體裡殘留直至...