图像的不同与角膜的conv2D卷积 [英] The different with the image convolve with the conv2D of keras

查看:150
本文介绍了图像的不同与角膜的conv2D卷积的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

1.first, the similar solution will be: using a high pass filter in the image processing, and generate a new image, and use it in the model. ---this is have to use the image processing, which is cost of time.

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

2.I want to set the a layer of Conv2D , which is also able to high pass the image. with a custom filter( as a intializer). the basic is that the filter and the conv2D is both using convolution rules.

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

but the results are different from the first solution.

#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的结果并不相同.

The problem is : using the image processing is able to produce a proper high passed image, but using the Conv2D is not the same result.

我假设两个结果应该相同或相似,但事实并非如此……

I am assuming two results should be the same or similar, the it turns out not...

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

Why, and it there any problem of my thoughts?

推荐答案

对于不完整的答案,我们深表歉意,但是我得到了部分解决的办法和一些解释.这是代码:

Apologies for the incomplete answer, but I've got something that partially works, and some explanation. Here's the code:

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)

以下是需要注意的事项:

Here are the things to note:

  • 这是一幅图像,像素是字节,任何大于字节的值都可能是 会被截断,并且可能会被不正确地截断(请注意,我不得不 在内核上删除"/12".这就是为什么我添加了"+ = 256" 部分.
  • 您不能假设填充"区域会完全相同.一世 不知道keras和opencv用于填充的值是什么,但是它 似乎不是相同的值.您的输出图像只能是 与[3,3]相同(即,每边3个像素的边框 不同).
  • 在使用内核之前先对其进行检查.它被四舍五入为-1和0 在我的系统上.大概使用整数算术.添加线 "kernel = kernel/12"对内核给出了更正确的结果,但是 卷积函数内的舍入似乎使事情搞砸了 再次,所以我将其保留为不带"/12"
  • 由于四舍五入,Relu搞砸了 (低于0的值表示喀拉斯语未正确截断为 激活功能将未签名的字节过滤掉了.)
  • It's an image, pixels are bytes, anything bigger than a byte may be truncated and may be truncated incorrectly (note that I've had to remove your "/12" on the kernel. That's why I've added the "+=256" section.
  • You can't assume that the "padded" areas will come out identical. I don't know what values keras and opencv use for padding, but it doesn't seem to be the same values. Your output images should only be identical from [3,3] (i.e. a border of 3 pixels on all sides might differ).
  • Check your kernel before you use it. It was being rounded to -1 and 0 on my system. Presumably using integer arithmetic. Adding the line "kernel=kernel/12" gave more correct results for the kernel, but the rounding within the convolution function seemed to screw things up again, so I've left it without the "/12"
  • The Relu was screwing things up, again because of the rounding (anything below zero that keras wasn't correctly truncating to unsigned byte was being filtered out by the activation function).

这篇关于图像的不同与角膜的conv2D卷积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆