-
Notifications
You must be signed in to change notification settings - Fork 3
/
additive_lesion_hgru.py
136 lines (126 loc) · 4.16 KB
/
additive_lesion_hgru.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env python
import os
import tensorflow as tf
from layers.feedforward import conv
from layers.feedforward import normalization
from layers.feedforward import pooling
from layers.recurrent import hgru
from config import Config
from ops import model_tools
from argparse import ArgumentParser
def experiment_params():
"""Parameters for the experiment."""
exp = {}
exp['lr'] = 1e-3
exp['loss_function'] = 'cce'
exp['optimizer'] = 'nadam'
exp['dataset'] = 'curv_contour_length_14_full'
exp['data_augmentations'] = [
[
'grayscale',
# 'left_right',
# 'up_down',
'uint8_rescale',
'singleton',
'resize',
# 'per_image_standardization',
'zero_one'
]]
exp['val_augmentations'] = exp['data_augmentations']
exp['batch_size'] = 32 # Train/val batch size.
exp['epochs'] = 2
exp['model_name'] = __file__.split('.')[0]
exp['exp_name'] = '%s_%s' % (
exp['model_name'].split(os.path.sep)[-1],
exp['dataset'])
# exp['clip_gradients'] = 7.
exp['save_weights'] = True
exp['validation_iters'] = 1000
exp['num_validation_evals'] = 50
exp['shuffle_val'] = True # Shuffle val data.
exp['shuffle_train'] = True
return exp
def build_model(data_tensor, reuse, training):
"""Create the hgru from Learning long-range..."""
with tf.variable_scope('cnn', reuse=reuse):
with tf.variable_scope('input', reuse=reuse):
conv_aux = {
'pretrained': os.path.join(
'weights',
'gabors_for_contours_7.npy'),
'pretrained_key': 's1',
'nonlinearity': 'square'
}
x = conv.conv_layer(
bottom=data_tensor,
name='gabor_input',
stride=[1, 1, 1, 1],
padding='SAME',
trainable=training,
use_bias=True,
aux=conv_aux)
layer_hgru = hgru.hGRU(
'hgru_1',
x_shape=x.get_shape().as_list(),
timesteps=8,
h_ext=15,
strides=[1, 1, 1, 1],
padding='SAME',
aux={
'lesion_mu': True,
'lesion_kappa': True},
train=training)
h2 = layer_hgru.build(x)
h2 = normalization.batch(
bottom=h2,
# renorm=True,
reuse=reuse,
name='hgru_bn',
training=training)
with tf.variable_scope('readout_1', reuse=reuse):
activity = conv.conv_layer(
bottom=h2,
name='pre_readout_conv',
num_filters=2,
kernel_size=1,
trainable=training,
use_bias=False)
pool_aux = {'pool_type': 'max'}
activity = pooling.global_pool(
bottom=activity,
name='pre_readout_pool',
aux=pool_aux)
activity = normalization.batch(
bottom=activity,
# renorm=True,
reuse=reuse,
name='readout_1_bn',
training=training)
with tf.variable_scope('readout_2', reuse=reuse):
activity = tf.layers.flatten(
activity,
name='flat_readout')
activity = tf.layers.dense(
inputs=activity,
units=2)
return activity, h2
def main(placeholders=False, gpu_device='/gpu:0', cpu_device='/cpu:0'):
"""Run an experiment with hGRUs."""
config = Config()
params = experiment_params()
model_tools.model_builder(
params=params,
config=config,
model_spec=build_model,
placeholders=placeholders,
gpu_device=gpu_device,
cpu_device=cpu_device)
if __name__ == '__main__':
parser = ArgumentParser()
parser.add_argument(
'--placeholders',
dest='placeholders',
action='store_true',
help='Use placeholder data loading.')
args = parser.parse_args()
main(**vars(args))