python 汇集和解放演示

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 汇集和解放演示相关的知识,希望对你有一定的参考价值。

#!/usr/bin/env python
# -*- coding: utf-8 -*-


from chainer import Variable
import cv2
import numpy as np

import chainer.functions as F

KSIZE = (2, 2)


def im_to_data(im):
    # height, width, channels -> c, h, w
    data = im.copy()
    data = np.swapaxes(np.swapaxes(data, 0, 2), 1, 2)
    data = data.astype(np.float32)
    data /= 255.
    return data


def data_to_im(data):
    # c, h, w -> h, w, c
    im = data.copy()
    im = np.swapaxes(np.swapaxes(im, 0, 2), 0, 1)
    im *= 255.
    im = im.astype(np.uint8)
    return im


im = cv2.imread('lena.jpg')
print(im.shape)
print(im.min(), im.mean(), im.max())
cv2.imshow('original', im)
cv2.waitKey(0)
imgs = [im]
x_data = np.array([im_to_data(im) for im in imgs])
print(x_data.shape)
print(x_data.min(), x_data.mean(), x_data.max())
x = Variable(x_data)


# pooling
y = F.max_pooling_2d(x, ksize=KSIZE)

for data in y.data:
    out_im = data_to_im(data)
    cv2.imshow('pooled', out_im)
    cv2.waitKey(0)


def unpooling_2d(x, ksize):
    kh, kw = ksize

    for i in xrange(kh-1):
        x.data = x.data.repeat(2, axis=2)
    for j in xrange(kw-1):
        x.data = x.data.repeat(2, axis=3)
    return x

imgs = [out_im]
y_data = np.array([im_to_data(im) for im in imgs])
y = Variable(y_data)

# unpooling
z = unpooling_2d(y, ksize=KSIZE)

for data in z.data:
    out_im = data_to_im(data)
    cv2.imshow('unpooled', out_im)
    cv2.waitKey(0)

以上是关于python 汇集和解放演示的主要内容,如果未能解决你的问题,请参考以下文章

中软国际首届嘉年华晚会召开 “解放号”勿忘初心再起航

Python字符串操作精彩汇集

干货 | 解放双手,用Python实现自动发送邮件

Python-基础知识汇集

Python的入门知识汇集

Python的入门知识汇集