-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyFCN.py
73 lines (61 loc) · 2.86 KB
/
MyFCN.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import chainer
from chainer import Variable
import chainer.links as L
import chainer.functions as F
import numpy as np
import math
import cv2
from chainer.links.caffe import CaffeFunction
import chainerrl
from chainerrl.agents import a3c
class DilatedConvBlock(chainer.Chain):
def __init__(self, d_factor, weight):
super(DilatedConvBlock, self).__init__(
diconv=L.DilatedConvolution2D( in_channels=64, out_channels=64, ksize=3, stride=1, pad=d_factor, dilate=d_factor, nobias=False, initialW=weight),
#bn=L.BatchNormalization(64)
)
self.train = True
def __call__(self, x):
h = F.relu(self.diconv(x))
#h = F.relu(self.bn(self.diconv(x)))
return h
class MyFcn(chainer.Chain, a3c.A3CModel):
def __init__(self, n_actions):
w = chainer.initializers.HeNormal()
# net = CaffeFunction('/home/puwenbo/puwenbo/PycharmProjects/pixelRL-master/initial_weight/zhang_cvpr17_denoise_15_gray.caffemodel')
super(MyFcn, self).__init__(
conv1=L.Convolution2D( 1, 64, 3, stride=1, pad=1, nobias=False, initialW=w),
diconv2=DilatedConvBlock(2, w),
diconv3=DilatedConvBlock(3, w),
diconv4=DilatedConvBlock(4, w),
diconv5_pi=DilatedConvBlock(3, w),
diconv6_pi=DilatedConvBlock(2, w),
conv7_Wz=L.Convolution2D( 64, 64, 3, stride=1, pad=1, nobias=True, initialW=w),
conv7_Uz=L.Convolution2D( 64, 64, 3, stride=1, pad=1, nobias=True, initialW=w),
conv7_Wr=L.Convolution2D( 64, 64, 3, stride=1, pad=1, nobias=True, initialW=w),
conv7_Ur=L.Convolution2D( 64, 64, 3, stride=1, pad=1, nobias=True, initialW=w),
conv7_W=L.Convolution2D( 64, 64, 3, stride=1, pad=1, nobias=True, initialW=w),
conv7_U=L.Convolution2D( 64, 64, 3, stride=1, pad=1, nobias=True, initialW=w),
conv8_pi=chainerrl.policies.SoftmaxPolicy(L.Convolution2D( 64, n_actions, 3, stride=1, pad=1, nobias=False, initialW=w)),
diconv5_V=DilatedConvBlock(3, w),
diconv6_V=DilatedConvBlock(2, w),
conv7_V=L.Convolution2D( 64, 1, 3, stride=1, pad=1, nobias=False, initialW=w),
)
self.train = True
def pi_and_v(self, x):
h = F.relu(self.conv1(x[:,0:1,:,:]))
h = self.diconv2(h)
h = self.diconv3(h)
h = self.diconv4(h)
h_pi = self.diconv5_pi(h)
x_t = self.diconv6_pi(h_pi)
h_t1 = x[:,-64:,:,:]
z_t = F.sigmoid(self.conv7_Wz(x_t)+self.conv7_Uz(h_t1))
r_t = F.sigmoid(self.conv7_Wr(x_t)+self.conv7_Ur(h_t1))
h_tilde_t = F.tanh(self.conv7_W(x_t)+self.conv7_U(r_t*h_t1))
h_t = (1-z_t)*h_t1+z_t*h_tilde_t
pout = self.conv8_pi(h_t)
h_V = self.diconv5_V(h)
h_V = self.diconv6_V(h_V)
vout = self.conv7_V(h_V)
return pout, vout, h_t