#!/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)