【问题标题】:The different with the image convolve with the conv2D of keras与图像的不同与keras的conv2D卷积
【发布时间】:2019-07-02 04:24:04
【问题描述】:

目标是尝试在第一层模型上使用自定义权重来充分填充高通滤波器的功能---使第一层模型与图像的高通滤波器相同。

1.首先,类似的解决方案是:在图像处理中使用高通滤波器,并生成新图像,并在模型中使用。 ---这是必须使用图像处理,这是时间成本。

2.我想设置Conv2D的a层,也可以高通图像。使用自定义过滤器(作为初始化器)。基本是filter和conv2D都使用卷积规则。

但结果与第一个解决方案不同。

#The image processing code:
    kernel55 = np.array([[-1, 2, -2, 2, -1], 
                         [2, -6, 8, -6, 2], 
                         [-2, 8, -12, 8, -2], 
                         [2,-6, 8, -6, 2],
                         [-1, 2, -2, 2, -1]])/12
        # load the image, pre-process it, and store it in the data list
        image = cv2.imread('1.pgm',-1)
        image = ndimage.convolve(image, kernel55)
        print(image)

#the first layer of the Model:

    def kernel_init(shape):
        kernel = np.zeros(shape)
        kernel[:,:,0,0] = np.array([[-1, 2, -2, 2, -1], 
                             [2, -6, 8, -6, 2], 
                             [-2, 8, -12, 8, -2], 
                             [2,-6, 8, -6, 2],
                             [-1, 2, -2, 2, -1]])/12
        return kernel
    #Build Keras model
    model = Sequential()
    model.add(Conv2D(1, [5,5], kernel_initializer=kernel_init, 
                     input_shape=(256,256,1), padding="same",activation='relu'))
    model.build()

test_im=cv2.imread('1.pgm',-1)  # define a test image
test_im=np.expand_dims(np.expand_dims(np.array(test_im),2),0)
out = model.predict(test_im)

问题是: 使用图像处理能够产生适当的高通图像,但使用 Conv2D 是不一样的结果。

我假设两个结果应该相同或相似,但事实证明不是......

为什么,我的想法有什么问题吗?

【问题讨论】:

  • 你在for循环中做什么?因为我注意到imagePath 未被使用

标签: python image-processing keras convolution


【解决方案1】:

为不完整的答案道歉,但我有一些部分有效的东西,以及一些解释。代码如下:

import cv2
import numpy as np
import scipy.ndimage as ndimage
from keras.models import Sequential
from keras.layers import Dense, Activation, Conv2D

#The image processing code:
#the first layer of the Model:

def kernel_init(shape):
    kernel = np.zeros(shape)
    kernel[:,:,0,0] = np.array([[-1, 2, -2, 2, -1],
                         [2, -6, 8, -6, 2],
                         [-2, 8, -12, 8, -2],
                         [2,-6, 8, -6, 2],
                         [-1, 2, -2, 2, -1]])
    #kernel = kernel/12
    #print("Here is the kernel")
    #print(kernel)
    #print("That was the kernel")
    return kernel

def main():
    print("starting")
    kernel55 = np.array([[-1, 2, -2, 2, -1],
                         [2, -6, 8, -6, 2],
                         [-2, 8, -12, 8, -2],
                         [2,-6, 8, -6, 2],
                         [-1, 2, -2, 2, -1]])
    # load the image, pre-process it, and store it in the data list
    image = cv2.imread('tiger.bmp',-1)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    myimage = cv2.resize(gray,(256,256))
    myimage = myimage
    print("The image")
    #print(myimage)
    print("That was the image")
    segment = myimage[0:10, 0:10]
    print(segment)

    imgOut = ndimage.convolve(myimage, kernel55)
    #imgOut = imgOut/12
    print(imgOut.shape)
    cv2.imwrite('zzconv.png', imgOut)

    #print(imgOut)
    segment = imgOut[0:10, 0:10]
    print(segment)

    #Build Keras model
    print("And the Keras stuff")
    model = Sequential()
    model.add(Conv2D(1, [5,5], kernel_initializer=kernel_init, input_shape=(256,256,1), padding="same"))
    model.build()

    test_im=myimage
    test_im = test_im.reshape((1, 256, 256, 1))
    print(test_im.shape)
    imgOut2 = model.predict(test_im)
    imgOut2 = imgOut2.reshape(256, 256)
    print(imgOut2.shape)
    #imgOut2 = imgOut2 / 12
    imgOut2[imgOut2 < 0] += 256

    cv2.imwrite('zzconv2.png', imgOut2)

    #print(imgOut2)
    segment = imgOut2[0:10, 0:10]
    print(segment)

以下是需要注意的事项:

  • 这是一个图像,像素是字节,任何大于字节的都可能是 被截断并且可能被错误地截断(请注意,我必须 删除内核上的“/ 12”。这就是我添加“+=256”的原因 部分。
  • 您不能假设“填充”区域会完全一样。我 不知道 keras 和 opencv 使用什么值来填充,但它 似乎不是相同的值。您的输出图像应该只是 与 [3,3] 相同(即所有边上的 3 个像素的边框可能 不同)。
  • 在使用之前检查您的内核。它被四舍五入到 -1 和 0 在我的系统上。大概使用整数算术。添加行 "kernel=kernel/12" 为内核提供了更正确的结果,但是 卷积函数内的舍入似乎把事情搞砸了 再次,所以我没有“/ 12”
  • Relu 把事情搞砸了,同样是因为四舍五入 (任何低于零的 keras 没有正确截断到 无符号字节被激活函数过滤掉了)。

【讨论】:

  • 非常感谢。我的主要目的是尝试制作与图像处理相同的输出..我会调查一下..非常感谢。
  • 我仔细运行程序后,结果是:期望outsize中的两行和两列,值是一样的。我认为局外人不同,只是因为它处理边缘的方式有很大不同,但是使用 kernel/12 是标准程序,我不知道如果不除以 12 会不会引起一些问题
  • @earlfordeep 我的背景是 C 语言,所以类型是固有的。我通常与 Python 的推断类型作斗争。我怀疑将所有内容设置为浮点数(或其他允许小数位的类型),然后最后转换为字节将允许您处理除以 12(我怀疑这就是 opencv/cv2 处理它的方式)。您可以尝试在图像周围添加(黑色或 0)边框并使用默认(无填充)而不是“相同”。
猜你喜欢
  • 1970-01-01
  • 2018-07-15
  • 1970-01-01
  • 2019-04-10
  • 2011-12-17
  • 2019-08-28
  • 1970-01-01
  • 1970-01-01
  • 2018-12-13
相关资源
最近更新 更多