|
1
|
|
|
#!/usr/bin/env python |
|
2
|
|
|
# -*- coding: utf-8 -*- |
|
3
|
|
|
|
|
4
|
|
|
|
|
5
|
|
|
import logging as loggers |
|
6
|
|
|
|
|
7
|
|
|
import numpy as np |
|
8
|
|
|
import theano.tensor as T |
|
9
|
|
|
from theano.tensor.nnet import conv |
|
10
|
|
|
from theano.tensor.signal import downsample |
|
11
|
|
|
|
|
12
|
|
|
from deepy.utils import build_activation, UniformInitializer |
|
13
|
|
|
from deepy.layers.layer import NeuralLayer |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
logging = loggers.getLogger(__name__) |
|
17
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
class Convolution(NeuralLayer): |
|
20
|
|
|
""" |
|
21
|
|
|
Convolution layer with max-pooling. |
|
22
|
|
|
""" |
|
23
|
|
|
|
|
24
|
|
|
def __init__(self, filter_shape, pool_size=(2, 2), |
|
25
|
|
|
reshape_input=False, border_mode="valid", flatten_output=False, |
|
26
|
|
|
disable_pooling=False, activation='linear', init=None): |
|
27
|
|
|
super(Convolution, self).__init__("convolution") |
|
28
|
|
|
self.filter_shape = filter_shape |
|
29
|
|
|
self.output_dim = filter_shape[0] |
|
30
|
|
|
self.pool_size = pool_size |
|
31
|
|
|
self.reshape_input = reshape_input |
|
32
|
|
|
self.flatten_output = flatten_output |
|
33
|
|
|
self.activation = activation |
|
34
|
|
|
self.disable_pooling = disable_pooling |
|
35
|
|
|
self.border_mode = border_mode |
|
36
|
|
|
self.initializer = init if init else self._default_initializer() |
|
37
|
|
|
|
|
38
|
|
|
def prepare(self): |
|
39
|
|
|
self._setup_params() |
|
40
|
|
|
self._setup_functions() |
|
41
|
|
|
|
|
42
|
|
|
def output(self, x): |
|
43
|
|
|
if self.reshape_input: |
|
44
|
|
|
img_width = T.cast(T.sqrt(x.shape[1]), "int32") |
|
45
|
|
|
x = x.reshape((x.shape[0], 1, img_width, img_width), ndim=4) |
|
46
|
|
|
|
|
47
|
|
|
conv_out = conv.conv2d( |
|
48
|
|
|
input=x, |
|
49
|
|
|
filters=self.W_conv, |
|
50
|
|
|
filter_shape=self.filter_shape, |
|
51
|
|
|
image_shape=None, |
|
52
|
|
|
border_mode=self.border_mode |
|
53
|
|
|
) |
|
54
|
|
|
|
|
55
|
|
|
pooled_out = downsample.max_pool_2d( |
|
56
|
|
|
input=conv_out, |
|
57
|
|
|
ds=self.pool_size, |
|
58
|
|
|
ignore_border=True |
|
59
|
|
|
) |
|
60
|
|
|
|
|
61
|
|
|
if self.disable_pooling: |
|
62
|
|
|
pooled_out = conv_out |
|
63
|
|
|
|
|
64
|
|
|
output = self._activation_func(pooled_out + self.B_conv.dimshuffle('x', 0, 'x', 'x')) |
|
65
|
|
|
|
|
66
|
|
|
if self.flatten_output: |
|
67
|
|
|
output = output.flatten(2) |
|
68
|
|
|
return output |
|
69
|
|
|
|
|
70
|
|
|
def _setup_functions(self): |
|
71
|
|
|
self._activation_func = build_activation(self.activation) |
|
72
|
|
|
|
|
73
|
|
|
def _setup_params(self): |
|
74
|
|
|
self.W_conv = self.create_weight(suffix="conv", initializer=self.initializer, shape=self.filter_shape) |
|
75
|
|
|
self.B_conv = self.create_bias(self.filter_shape[0], suffix="conv") |
|
76
|
|
|
|
|
77
|
|
|
self.register_parameters(self.W_conv, self.B_conv) |
|
78
|
|
|
|
|
79
|
|
|
def _default_initializer(self): |
|
80
|
|
|
fan_in = np.prod(self.filter_shape[1:]) |
|
81
|
|
|
fan_out = (self.filter_shape[0] * np.prod(self.filter_shape[2:]) / |
|
82
|
|
|
np.prod(self.pool_size)) |
|
83
|
|
|
weight_scale = np.sqrt(6. / (fan_in + fan_out)) |
|
84
|
|
|
return UniformInitializer(scale=weight_scale) |