来自其他网站的一些更令人困惑的示例,评估为
由函数 cnn 总结的过程。
函数 rounded 可能相当于命令 round。
'''
Convolution , using
scipy.signals.fftconvolve.
As this routine has various options, I shall check the results
using information from various sources.
'''
import numpy as np
import scipy.signal as sg
def rounded(xv):
sv = np.sign(xv)
xv = xv*1000
xv = np.abs(xv)
xv = xv + 500
xv = xv.astype(int)
xv = xv*sv
xv = xv/1000
xv = xv.astype(int)
return xv
def cnn(vol,volf):
vol=np.transpose(vol)
volf=np.transpose(volf)
volg=sg.fftconvolve(vol,volf, mode = 'valid')
volg = rounded(volg)
#volg=np.transpose(volg)
return volg
# ------------------------------------------------------------------
# 10 10 10 0 0 0
# 10 10 10 0 0 0
# 10 10 10 0 0 0
# 10 10 10 0 0 0
# 10 10 10 0 0 0
# 10 10 10 0 0 0
# 1 0 -1
# 1 0 -1
# 1 0 -1
# 0 30 30 0
# 0 30 30 0
# 0 30 30 0
# 0 30 30 0
#
vol=[[10,10,10,10,10,10],[10,10,10,10,10,10],[10,10,10,10,10,10],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0]]
vol=np.transpose(vol)
print(" ")
print(vol)
print(' ')
volf=[[-1,-1,-1],[0,0,0],[1,1,1]]
volf=np.transpose(volf)
print(volf)
print(' ')
# mode = full valid same
volg=sg.fftconvolve(vol,volf, mode = 'valid')
#volg=np.transpose(volg) # usually not required.
volg = rounded(volg)
print(volg)
# With slightly altered kernel, the result is true.
# should be
#[[0,30,30,0],[0,30,30,0],[0,30,30,0],[0,30,30,0]]
# original volf =[[-1,-1,-1],[0,0,0],[1,1,1]] , prior to adjustment or translate.
# https://www.analyticsvidhya.com/blog/2018/12/guide-convolutional-neural-network-cnn/
# From same website .
# 3 0 1 2 7 4
# 1 5 8 9 3 1
# 2 7 2 5 1 3
# 0 1 3 1 7 8
# 4 2 1 6 2 8
# 2 4 5 2 3 9
# 1 0 -1
# 1 0 -1
# 1 0 -1
# result
# -5 -4 0 8
# -10 -2 2 3
# 0 -2 -4 -7
# -3 -2 -3 -16
vol=[[3,1,2,0,4,2],[0,5,7,1,2,4],[1,8,2,3,1,5],[2,9,5,1,6,2],[7,3,1,7,2,3],[4,1,3,8,8,9]]
vol=np.transpose(vol)
print(" ....................... ")
print(" ")
print(vol)
print(' ')
volf=[[-1,-1,-1],[0,0,0],[1,1,1]]
volf=np.transpose(volf)
print(volf)
print(' ')
# mode = full valid same
volg=sg.fftconvolve(vol,volf, mode = 'valid')
volg = rounded(volg)
print(volg)
# The result is true, however the kernel is different than at the website.
# quit()
# ---------------------------------------------------------------------------
# another example , from .
# https://insightsimaging.springeropen.com/articles/10.1007/s13244-018-0639-9
# 1 2 1 0 2
# 2 0 0 1 0
# 1 0 2 1 0
# 0 1 0 2 1
# 0 2 1 0 2
# 1 0 1
# 0 1 0
# 1 0 1
# result.
# 1 4 2 0 3
# 4 5 3 6 1
# 2 2 6 2 3
# 2 5 3 7 2
# 1 2 4 1 4
vol=[[0,0,0,0,0,0,0],[0,1,2,1,0,0,0],[0,2,0,0,1,2,0],[0,1,0,2,0,1,0],[0,0,1,1,2,0,0],[0,2,0,0,1,2,0],[0,0,0,0,0,0,0]]
vol=np.transpose(vol)
print(" ")
print(vol)
print(' ')
volf=[[1,0,1],[0,1,0],[1,0,1]]
volf=np.transpose(volf)
print(volf)
print(' ')
volg = sg.fftconvolve(vol,volf, mode = 'valid')
volg = rounded(volg)
print(volg)
print(" ..........<< >>.......... ")
# Same result after applying rounded .
#quit()
# without padding .
# 1 2 1 0 2
# 2 0 0 1 0
# 1 0 2 1 0
# 0 1 0 2 1
# 0 2 1 0 2
# 1 0 1
# 0 1 0
# 1 0 1
# result
# 5 3 6
# 2 6 2
# 5 3 7
vol=[[1,2,1,0,0],[2,0,0,1,2],[1,0,2,0,1],[0,1,1,2,0],[2,0,0,1,2]]
vol=np.transpose(vol)
print(" ")
print(vol)
print(' ')
volf=[[1,0,1],[0,1,0],[1,0,1]]
volf=np.transpose(volf)
print(volf)
print(' ')
volg = sg.fftconvolve(vol,volf, mode = 'valid')
volg = rounded(volg)
print(volg)
# Same result after applying rounded .
#quit()
# ----------------------------- valid ----------------------------------
# Trying another example, from
# https://medium.com/analytics-vidhya/convolutional-neural-networks-cnn-explained-step-by-step-69137a54e5e7
# 1 1 1 0 0
# 0 1 1 1 0
# 0 0 1 1 1
# 0 0 1 1 0
# 0 1 1 0 0
# 1 0 1
# 0 1 0
# 1 0 1
# 4 3 4
# 2 4 3
# 2 3 4
vol=[[1,1,1,0,0],[0,1,1,1,0],[0,0,1,1,1],[0,0,1,1,0],[0,1,1,0,0]]
vol=np.transpose(vol)
print(" ")
print(vol)
print(' ')
volf=[[1,0,1],[0,1,0],[1,0,1]]
volf=np.transpose(volf)
print(volf)
print(' ')
volg = sg.fftconvolve(vol,volf, mode = 'valid')
volg=np.transpose(volg) # Notice syntax for this procedure .
volg = rounded(volg)
print(volg)
# This is correct, however having to take the transpose of volg is
# unexpected and differs from most of the other examples.
#quit()
# ....................................................
# Yet another example, using previous method .
# https://www.deeplearningwizard.com/deep_learning/practical_pytorch/pytorch_convolutional_neuralnetwork/
# 0 0 0 0 0 0 0
# 0 2 2 0 0 0 0
# 0 0 0 2 2 2 0
# 0 2 0 0 2 0 0
# 0 0 0 0 2 0 0
# 0 0 2 2 0 0 0
# 0 0 0 0 0 0 0
# 1 1 1
# 0 0 0
# 0 0 0
# 0 0 0 0 0
# 4 4 2 0 0
# 0 2 4 6 4
# 2 2 2 2 2
# 0 0 2 2 2
vol=[[0,0,0,0,0,0,0],[0,2,0,2,0,0,0],[0,2,0,0,0,2,0],[0,0,2,0,0,2,0],[0,0,2,2,2,0,0],[0,0,2,0,0,0,0],[0,0,0,0,0,0,0]]
vol=np.transpose(vol)
print(" ")
print(vol)
print(' ')
volf=[[0,0,1],[0,0,1],[0,0,1]]
volf=np.transpose(volf)
print(volf)
print(' ')
volg = sg.fftconvolve(vol,volf, mode = 'valid')
volg = rounded(volg)
print(volg)
# this is correct, however volf is reversed.
#quit()
# .....................................................
# https://victorzhou.com/blog/intro-to-cnns-part-1/
# 0 50 0 29
# 0 80 31 2
# 33 90 0 75
# 0 9 0 95
# -1 0 1
# -2 0 2
# -1 0 1
# 29 -192
# -35 -22
vol=[[0,0,33,0],[50,80,90,9],[0,31,0,0],[29,2,75,95]]
vol=np.transpose(vol)
print(" ...............<<<<< ")
print(vol)
print(' ')
volf=[[1,2,1],[0,0,0],[-1,-2,-1]]
volf=np.transpose(volf)
print(volf)
print(' ')
volg = sg.fftconvolve(vol,volf, mode = 'valid')
volg = rounded(volg)
print(volg.astype(int))
# this is correct, however volf is top row is reversed with bottom row.
# --------------------------------------------------------
#
# To summarize, the function cnn is mostly correct for the examples
# that I found upon the web, when one enters the data values in the
# format shown .
#