daobalaoer / image_deeplearning

0 stars 0 forks source link

image #1

Open daobalaoer opened 3 years ago

daobalaoer commented 3 years ago

Idea, Concept, Realization, Tool

daobalaoer commented 3 years ago

Convolutions是一种矩阵之间的计算(对应元素相乘,再求和)。 image+kernel(filter) 有各种常见实用的filter:例如模糊/锐化/边缘检测/Sobel,这些filter是小矩阵。这样与image进行对于的矩阵操作,就会提取image中对应的信息。 当然,也可以用CNN来自动寻找这些filter。

daobalaoer commented 3 years ago

一些概念容易被乱叫,虽然不影响,但是还是要搞得清楚一点。 对于一个image,你想识别这个image是不是猫或者狗,你需要通过一种工具来从这里面提取信息,这个工具就是Local receptige field,这是一个小矩阵(可能是33,55等等)。这个矩阵中的每个元素值,即参数值(w,b),叫做kernel/filter。

滑动产生了几个概念:weight share,Feature map,hidden neurons。 使用这一个Local receptive field 在image滑动一遍,一直都用同样的一组参数,这就是weight share的概念。为什么要用一样的参数,优点是能够极大的减少网络尺寸(你可以是这计算一些Dense 和 Conv对比一下就知道了)。 用

daobalaoer commented 3 years ago

问题

image-->cat/dog?

思路

image--feature-->cat/dog? 通过Local receptige field在image中滑动,来提取某一个feature(例如是不是一个edge)。

概念

[Local receptige field]

  1. kernel/filter:即小矩阵(33,55...),矩阵里的元素即参数(w),外加一个偏置b。即对于一个kernel,有w+b个参数。(有LRF想出来具体的概念,没有用圆来提取,而是用了矩阵)

[滑动]

  1. weight share:一个kernel在image滑动一遍,均使用同样的参数值与对于的LRF进行卷积计算。优点是极大的减少网络参数(你可以是这计算一些Dense 和 Conv对比一下就知道了)。
  2. Feature map:用一个kernel在image滑动一遍,相当于在一个image的所以地方都在检测有没有这个特征。所以说滑动的过程就是Feature map。
  3. hidden neurons:lernel与LRF进行卷积/加上b/再放入函数中,最终的到的值,就是对应的下一层hidden neurons的值。
daobalaoer commented 3 years ago

卷积计算

#convolutions.py
# importk pkg
from skimage.exposure import rescale_intensity
import cv2
import numpy as np
import argparse
def convolve(image,kernel):
    (iH,iW) = image.shape[:2]
    (kH,kW) = kernel.shape[:2]
    #pad,makeborder
    pad = (kW-1)//2
    image = cv2.copyMakeBorder(image,pad,pad,pad,pad,cv2.BORDER_REFLECT)
    output = np.zeros((iH,iW),dtype='float32')
    for y in np.arange(pad,iH+pad):
        for x in np.arange(pad,iW+pad):
            roi = image[y-pad:y+pad+1,x-pad:x+pad+1]
            k = (roi*kernel).sum()
            output[y-pad,x-pad] = k
    output = rescale_intensity(output,in_range=(0,255))
    output = (output*255).astype('uint8')
    return output

# argument parse
ap = argparse.ArgumentParser()
ap.add_argument('-i','--image',required=True,help='path to the input image')
args = vars(ap.parse_args())

#构造不同的kernel
#模糊
smallBlur = np.ones((7,7),dtype='float')*(1.0/(7*7))
largeBlur = np.ones((21,21),dtype='float')*(1.0/(21*21))
#锐化
sharpen = np.array((
    [0,-1,0],
    [-1,5,-1],
    [0,-1,0]
),dtype='int')
#检测边缘detect edge-like
laplacian = np.array((
    [0,1,0],
    [1,-4,1],
    [0,1,0]    
),dtype='int')
#Sobel x-axis kernel
sobelX = np.array((
    [-1,0,1],
    [-2,0,2],
    [-1,0,1]
),dtype='int')
sobelY = np.array((
    [-1,-2,-1],
    [0,0,0],
    [1,2,1]
),dtype='int')
kernelBank = (
    ('small_blur',smallBlur),
    ('large_blur',largeBlur),
    ('sharpen',sharpen),
    ('laplacian',laplacian),
    ('sobel_x',sobelX),
    ('sobel_y',sobelY))
# BGR to GRAY
image = cv2.imread(args['image'])
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
for (kernelName,kernel) in kernelBank:
    print ('[INFO] applying {} kernel' .format(kernelName))
    convoleOutput = convolve(gray,kernel)
    opencvOutput = cv2.filter2D(gray,-1,kernel)
    cv2.imshow('original',gray)
    cv2.imshow('{} - convole' .format(kernelName),convoleOutput)
    cv2.imshow('{} - opencv' .format(kernelName),opencvOutput)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
'''
cd  /convolution/
/anaconda3/bin/python convolutions.py --image 328605603.jpg
'''

参考来源:www.pyimagesearch.com