V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
fendouai_com
V2EX  ›  Python

tensorflow CNN 卷积神经网络中的卷积层和池化层的代码和效果图

  •  1
     
  •   fendouai_com · 2017-07-28 10:39:22 +08:00 · 2241 次点击
    这是一个创建于 2457 天前的主题,其中的信息可能已经有所发展或是发生改变。

    因为很多 demo 都比较复杂,为了让 demo 比较简单,专门抽出这两个函数,写的 demo。

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    import matplotlib.pyplot as plt
    import tensorflow as tf
    from PIL import Image
    import numpy
    
    img = Image.open('szu.jpg')
    img_ndarray = numpy.asarray(img, dtype='float32')
    print(img_ndarray.shape)
    img_ndarray=img_ndarray[:,:,0]
    plt.figure()
    plt.subplot(221)
    plt.imshow(img_ndarray)
    
    w=[[-1.0,-1.0,-1.0],
       [-1.0,9.0,-1.0],
       [-1.0,-1.0,-1.0]]
    
    with tf.Session() as sess:
        img_ndarray=tf.reshape(img_ndarray,[1,183,276,1])
        w=tf.reshape(w,[3,3,1,1])
        img_cov=tf.nn.conv2d(img_ndarray, w, strides=[1, 1, 1, 1], padding='SAME')
        image_data=sess.run(img_cov)
        print(image_data.shape)
        plt.subplot(222)
        plt.imshow(image_data[0,:,:,0])
    
        img_pool=tf.nn.max_pool(img_ndarray, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1],
                       padding='SAME')
        image_data = sess.run(img_pool)
        plt.subplot(223)
        plt.imshow(image_data[0, :, :, 0])
        plt.subplot(224)
        img_pool = tf.nn.max_pool(img_ndarray, ksize=[1, 4, 4, 1], strides=[1, 4, 4, 1],
                                  padding='SAME')
        image_data = sess.run(img_pool)
        plt.imshow(image_data[0, :, :, 0])
        plt.show()
    

    卷积和池化效果图:

    更多资源教程: http://www.tensorflownews.com

    1 条回复    2017-07-28 10:56:10 +08:00
    pming1
        1
    pming1  
       2017-07-28 10:56:10 +08:00
    不明觉厉
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2862 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 13:48 · PVG 21:48 · LAX 06:48 · JFK 09:48
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.