tensorflow-tf.nn.conv2d卷积运算(3)

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Tue Oct  2 13:23:27 2018

@author: myhaspl
@email:[email protected]
tf.nn.conv2d
"""

import tensorflow as tf

g=tf.Graph()

with g.as_default():
    x=tf.constant([
               [
                    [[1.],[2.],[11.]],
                    [[3.],[4.],[22.]],
                    [[5.],[6.],[33.]]
               ],
               [
                    [[10.],[20.],[44.]],
                    [[30.],[40.],[55.]],
                    [[50.],[60.],[66.]]                    
               ]
            ])#2*3*3*1
    kernel=tf.constant(
            [
                   [[[2.]],[[3.]]]
            ]
            )#2*2*1
    y=tf.nn.conv2d(x,kernel,strides=[1,1,1,1],padding="VALID")

with tf.Session(graph=g) as sess:
    print x.get_shape()
    print kernel.get_shape()
    print sess.run(y)    
    print y.get_shape()

padding="VALID"
X的最后一列不会参与计算。

(2, 3, 3, 1)
(1, 2, 1, 1)
[[[[  8.]
   [ 37.]]

  [[ 18.]
   [ 74.]]

  [[ 28.]
   [111.]]]

 [[[ 80.]
   [172.]]

  [[180.]
   [245.]]

  [[280.]
   [318.]]]]
(2, 3, 2, 1)

strides指定跳过数

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Tue Oct  2 13:23:27 2018

@author: myhaspl
@email:[email protected]
tf.nn.conv2d
"""

import tensorflow as tf

g=tf.Graph()

with g.as_default():
    x=tf.constant([
               [
                    [[1.],[2.],[3.]]
               ],
               [
                    [[10.],[20.],[30.]]                 
               ]
            ])#2*1*1*3
    kernel=tf.constant(
            [
                   [[[2.]]]                 
            ]
            )#1*1*3
    y=tf.nn.conv2d(x,kernel,strides=[1,1,2,1],padding="SAME")

with tf.Session(graph=g) as sess:
    print x.get_shape()
    print kernel.get_shape()
    print sess.run(y)    
    print y.get_shape()
(2, 1, 3, 1)
(1, 1, 1, 1)
[[[[ 2.]
   [ 6.]]]

 [[[20.]
   [60.]]]]
(2, 1, 2, 1)

图像模糊化的卷积

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Tue Oct  2 13:23:27 2018

@author: myhaspl
@email:[email protected]
tf.nn.conv2d处理图像
"""

import tensorflow as tf
from PIL import Image    
import numpy as np

g=tf.Graph()

with g.as_default():

    def getImageData(fileNameList):
        imageData=[]
        for fn in fileNameList:        
            testImage = Image.open(fn).convert('L')   
            testImage.show() 
            imageData.append(np.array(testImage)[:,:,None])
        return np.array(imageData,dtype=np.float32)

    imageFn=("xlh.png",)
    imageData=getImageData(imageFn)
    testData=tf.constant(imageData)
    kernel=tf.constant(
            [
                   [[[1/9.]],[[1/9.]],[[1/9.]]],
                   [[[1/9.]],[[1/9.]],[[1/9.]]], 
                   [[[1/9.]],[[1/9.]],[[1/9.]]]
            ]
            )#3*3*1*1
    y=tf.cast(tf.nn.conv2d(testData,kernel,strides=[1,1,1,1],padding="SAME"), dtype=tf.int32)
    init_op = tf.global_variables_initializer()
with tf.Session(graph=g) as sess:
    print testData.get_shape()
    print kernel.get_shape()
    resultData=sess.run(y)[0]
    resultData=resultData.reshape(resultData.shape[0],resultData.shape[1])
    resulImage=Image.fromarray(np.uint8(resultData),mode='L')   
    resulImage.show()
    print y.get_shape()

运动模糊

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Tue Oct  2 13:23:27 2018

@author: myhaspl
@email:[email protected]
tf.nn.conv2d处理图像
运动模糊
"""

import tensorflow as tf
from PIL import Image    
import numpy as np

g=tf.Graph()

with g.as_default():

    def getImageData(fileNameList):
        imageData=[]
        for fn in fileNameList:        
            testImage = Image.open(fn).convert('L')   
            testImage.show() 
            imageData.append(np.array(testImage)[:,:,None])
        return np.array(imageData,dtype=np.float32)

    imageFn=("dog.png",)
    imageData=getImageData(imageFn)
    testData=tf.constant(imageData)
    kernel=tf.constant(np.array(
            [
                   [[[1.]],[[0.]],[[0.]]],
                   [[[0.]],[[1.]],[[0.]]], 
                   [[[0.]],[[0.]],[[1.]]]
            ])/3.
            ,dtype=tf.float32)#3*3*1*1
    y=tf.cast(tf.nn.conv2d(testData,kernel,strides=[1,1,1,1],padding="SAME"), dtype=tf.int32)
    init_op = tf.global_variables_initializer()
with tf.Session(graph=g) as sess:
    print testData.get_shape()
    print kernel.get_shape()
    resultData=sess.run(y)[0]
    resultData=resultData.reshape(resultData.shape[0],resultData.shape[1])
    resulImage=Image.fromarray(np.uint8(resultData),mode='L')   
    resulImage.show()
    print y.get_shape()

猜你喜欢

转载自blog.51cto.com/13959448/2340816