zoukankan      html  css  js  c++  java
  • theano中对图像进行convolution 运算

    (1) 定义计算过程中需要的symbolic expression

     1 """
     2 定义相关的symbolic experssion
     3 """
     4 # convolution layer的输入,根据theano,它应该是一个4d tensor
     5 input = T.tensor4(name='input')
     6 # 共享权值W,它的shape为2,3,9,9
     7 w_shp = (2,3,9,9);w_bound = numpy.sqrt(3*9*9)
     8 W = theano.shared(numpy.asarray(rng.uniform(low= -1.0/w_bound, high = 1.0/w_bound,size=w_shp),dtype=input.dtype),name='W')
     9 # 利用卷积核W对input进行卷积运算
    10 conv_out = conv.conv2d(input,W)
    11 # 偏执向量b
    12 b_shp = (2,)  # b是一个只有1个元素2的tuple
    13 b = theano.shared(numpy.asarray(rng.uniform(low= -.5, high = .5,size=b_shp),dtype=input.dtype),name='b')
    14 # 计算sigmoid函数
    15 output = T.nnet.sigmoid(conv_out+b.dimshuffle('x',0,'x','x'))
    16 # 输入输出function
    17 f = theano.function([input],output)

    (2)利用真实数据计算

     1 """
     2 开始使用具体数值
     3 """
     4 # 读入图像
     5 img = Image.open('3wolfmoon.jpg', mode='r')
     6 # 将输入图像存入在array中
     7 img = numpy.array(img,dtype='float64')/256
     8 # 对输入图像进行reshape
     9 img_=img.transpose(2,0,1).reshape(1,3,639,516)
    10 # 利用convolution kernel对输入图像进行卷积运算
    11 filtered_img=f(img_)

    (3)绘制需要显示的图像

     1 """
     2 绘制图像
     3 """
     4 # 显示原始图像
     5 pylab.subplot(1,3,1);pylab.axis('off');pylab.imshow(img);pylab.gray()
     6 # 显示filter后的图像的channel1
     7 pylab.subplot(1,3,2);pylab.axis('off');pylab.imshow(filtered_img[0,0,:,:])
     8 # 显示filter后的图像的channel2
     9 pylab.subplot(1,3,3);pylab.axis('off');pylab.imshow(filtered_img[0,1,:,:])
    10 # 显示
    11 pylab.show()

    整个代码段

     1 # -*- coding: utf-8 -*-
     2 
     3 # 导入相关的模块
     4 import theano
     5 from theano import tensor as T
     6 from theano.tensor.nnet import conv
     7 import numpy
     8 import pylab
     9 from PIL import Image
    10 
    11 
    12 # 产生随机数的种子
    13 rng = numpy.random.RandomState(23455)
    14 
    15 """
    16 定义相关的symbolic experssion
    17 """
    18 # convolution layer的输入,根据theano,它应该是一个4d tensor
    19 input = T.tensor4(name='input')
    20 # 共享权值W,它的shape为2,3,9,9
    21 w_shp = (2,3,9,9);w_bound = numpy.sqrt(3*9*9)
    22 W = theano.shared(numpy.asarray(rng.uniform(low= -1.0/w_bound, high = 1.0/w_bound,size=w_shp),dtype=input.dtype),name='W')
    23 # 利用卷积核W对input进行卷积运算
    24 conv_out = conv.conv2d(input,W)
    25 # 偏执向量b
    26 b_shp = (2,)  # b是一个只有1个元素2的tuple
    27 b = theano.shared(numpy.asarray(rng.uniform(low= -.5, high = .5,size=b_shp),dtype=input.dtype),name='b')
    28 # 计算sigmoid函数
    29 output = T.nnet.sigmoid(conv_out+b.dimshuffle('x',0,'x','x'))
    30 # 输入输出function
    31 f = theano.function([input],output)
    32 
    33 """
    34 开始使用具体数值
    35 """
    36 # 读入图像
    37 img = Image.open('3wolfmoon.jpg', mode='r')
    38 # 将输入图像存入在array中
    39 img = numpy.array(img,dtype='float64')/256
    40 # 对输入图像进行reshape
    41 img_=img.transpose(2,0,1).reshape(1,3,639,516)
    42 # 利用convolution kernel对输入图像进行卷积运算
    43 filtered_img=f(img_)
    44 
    45 """
    46 绘制图像
    47 """
    48 # 显示原始图像
    49 pylab.subplot(1,3,1);pylab.axis('off');pylab.imshow(img);pylab.gray()
    50 # 显示filter后的图像的channel1
    51 pylab.subplot(1,3,2);pylab.axis('off');pylab.imshow(filtered_img[0,0,:,:])
    52 # 显示filter后的图像的channel2
    53 pylab.subplot(1,3,3);pylab.axis('off');pylab.imshow(filtered_img[0,1,:,:])
    54 # 显示
    55 pylab.show()
    View Code
  • 相关阅读:
    首页调单个产品分类的推荐产品,最新产品和热卖商品
    ecshop模板<! TemplateBeginEditable name="左上角主区域" >用法
    复制DataTable数据到新DataTable
    定时任务时间与当前时间比较的方法
    批量删除文件夹下包含指定字段的文件
    SQL 字符串去除空格函数
    Javascript的IE和Firefox(火狐)兼容性的常用例子
    查询表某列的加权平均值
    Jquery实现页面定时跳转
    Date.parse Firefox返回Nan的解决办法
  • 原文地址:https://www.cnblogs.com/lutingting/p/5184539.html
Copyright © 2011-2022 走看看