-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
89 lines (83 loc) · 3.76 KB
/
model.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# -*- coding: utf-8 -*-
"""
Created on Fri Jan 27 19:13:36 2017
@author: Weidi Xie
@description:
This is the file to create the model, similar as the paper, but with batch normalization and skip layers,
make it more easier to train.
"""
from __future__ import absolute_import
from __future__ import print_function
import numpy as np
from keras.models import Sequential, Model
from keras.layers import (
Input,
Activation,
Merge,
merge,
Dropout,
Reshape,
Permute,
Dense,
UpSampling2D,
Flatten
)
from keras.optimizers import SGD, RMSprop
from keras.layers.convolutional import (
Convolution2D)
from keras.layers.pooling import (
MaxPooling2D,
AveragePooling2D
)
from keras.layers.normalization import BatchNormalization
from keras.regularizers import l2
weight_decay = 1e-4
def _conv_bn_relu(nb_filter, row, col, subsample = (1,1)):
def f(input):
conv_a = Convolution2D(nb_filter, row, col, subsample = subsample,
init = 'orthogonal', border_mode = 'same',bias = False,
W_regularizer = l2(weight_decay),
b_regularizer = l2(weight_decay))(input)
norm_a = BatchNormalization()(conv_a)
act_a = Activation(activation = 'relu')(norm_a)
conv_b = Convolution2D(nb_filter, row, col, subsample = subsample,
init = 'orthogonal', border_mode = 'same',bias = False,
W_regularizer = l2(weight_decay),
b_regularizer = l2(weight_decay))(act_a)
norm_b = BatchNormalization()(conv_b)
act_b = Activation(activation = 'relu')(norm_b)
return act_b
return f
def net_base(input, nb_filter = 64):
block1 = _conv_bn_relu(nb_filter,3,3)(input)
pool1 = MaxPooling2D(pool_size=(2,2))(block1)
# =========================================================================
block2 = _conv_bn_relu(nb_filter,3,3)(pool1)
pool2 = MaxPooling2D(pool_size=(2, 2))(block2)
# =========================================================================
block3 = _conv_bn_relu(nb_filter,3,3)(pool2)
pool3 = MaxPooling2D(pool_size=(2, 2))(block3)
# =========================================================================
block4 = _conv_bn_relu(nb_filter,3,3)(pool3)
up4 = merge([UpSampling2D(size=(2, 2))(block4), block3], mode='concat', concat_axis=-1)
# =========================================================================
block5 = _conv_bn_relu(nb_filter,3,3)(up4)
up5 = merge([UpSampling2D(size=(2, 2))(block5), block2], mode='concat', concat_axis=-1)
# =========================================================================
block6 = _conv_bn_relu(nb_filter,3,3)(up5)
up6 = merge([UpSampling2D(size=(2, 2))(block6), block1], mode='concat', concat_axis=-1)
# =========================================================================
block7 = _conv_bn_relu(nb_filter,3,3)(up6)
return block7
def buildModel (input_dim):
input_ = Input (shape = (input_dim))
# =========================================================================
act_ = net_base (input_, nb_filter = 64 )
# =========================================================================
density_pred = Convolution2D(1, 1, 1, bias = False, activation='linear',\
init='orthogonal',name='pred',border_mode='same')(act_)
# =========================================================================
model = Model (input = input_, output = density_pred)
opt = SGD(lr = 1e-2, momentum = 0.9, nesterov = True)
model.compile(optimizer = opt, loss = 'mse')
return model