-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModelLibrary.py
More file actions
194 lines (152 loc) · 7.39 KB
/
ModelLibrary.py
File metadata and controls
194 lines (152 loc) · 7.39 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
__author__ = 'Peter HIRT'
# Utils used with tensorflow implemetation
import tensorflow as tf
import numpy as np
import os, sys
from collections import OrderedDict
import logging
import datetime
from six.moves import xrange
from scipy.io import loadmat
from tf_unet import util
from tf_unet.layers import (weight_variable, weight_variable_devonc, bias_variable,
conv2d, deconv2d, max_pool, crop_and_concat, pixel_wise_softmax_2,
cross_entropy)
#
##################################################################################################
# UNET model
# origin tf_unet
# programmable depth
def get_image_summary(img, idx=0):
"""
Make an image summary for 4d tensor image with index idx
"""
V = tf.slice(img, (0, 0, 0, idx), (1, -1, -1, 1))
V -= tf.reduce_min(V)
V /= tf.reduce_max(V)
V *= 255
img_w = tf.shape(img)[1]
img_h = tf.shape(img)[2]
V = tf.reshape(V, tf.stack((img_w, img_h, 1)))
V = tf.transpose(V, (2, 0, 1))
V = tf.reshape(V, tf.stack((-1, img_w, img_h, 1)))
return V
def create_conv_net(x, keep_prob, channels, n_class, layers=3, features_root=16, summaries=False, filter_size=3, pool_size=2 ):
"""
Creates a new convolutional unet for the given parametrization.
:param x: input tensor, shape [?,nx,ny,channels]
:param keep_prob: dropout probability tensor
:param channels: number of channels in the input image
:param n_class: number of output labels
:param layers: number of layers in the net
:param features_root: number of features in the first layer
:param filter_size: size of the convolution filter
:param pool_size: size of the max pooling operation
:param summaries: Flag if summaries should be created
"""
logging.info("Layers {layers}, features {features}, filter size {filter_size}x{filter_size}, pool size: {pool_size}x{pool_size}".format(layers=layers,
features=features_root,
filter_size=filter_size,
pool_size=pool_size))
# Placeholder for the input image
nx = tf.shape(x)[1]
ny = tf.shape(x)[2]
x_image = tf.reshape(x, tf.stack([-1,nx,ny,channels]))
in_node = x_image
batch_size = tf.shape(x_image)[0]
weights = []
biases = []
convs = []
pools = OrderedDict()
deconv = OrderedDict()
dw_h_convs = OrderedDict()
up_h_convs = OrderedDict()
in_size = 1000
size = in_size
counter = 0
# down layers
for layer in range(0, layers):
counter = counter + 1
features = 2**layer*features_root
stddev = np.sqrt(2 / (filter_size**2 * features))
if layer == 0:
w1 = weight_variable([filter_size, filter_size, channels, features], stddev)
else:
w1 = weight_variable([filter_size, filter_size, features//2, features], stddev)
w2 = weight_variable([filter_size, filter_size, features, features], stddev)
b1 = bias_variable([features])
b2 = bias_variable([features])
conv1 = conv2d(in_node, w1, keep_prob)
logging.info("conv{layer}, features {features}, filter size {filter_size}x{filter_size}, activation: relu"
.format(layer=counter, features=features, filter_size=filter_size))
tmp_h_conv = tf.nn.relu(conv1 + b1)
conv2 = conv2d(tmp_h_conv, w2, keep_prob)
logging.info("conv{layer}, features {features}, filter size {filter_size}x{filter_size}, activation: relu"
.format(layer=counter, features=features, filter_size=filter_size))
dw_h_convs[layer] = tf.nn.relu(conv2 + b2)
weights.append((w1, w2))
biases.append((b1, b2))
convs.append((conv1, conv2))
size -= 4
if layer < layers-1:
pools[layer] = max_pool(dw_h_convs[layer], pool_size)
logging.info("pool{layer}, pool size: {pool_size}x{pool_size}"
.format(layer=counter, pool_size=pool_size))
in_node = pools[layer]
size /= 2
in_node = dw_h_convs[layers-1]
# up layers
for layer in range(layers-2, -1, -1):
counter = counter + 1
features = 2**(layer+1)*features_root
stddev = np.sqrt(2 / (filter_size**2 * features))
wd = weight_variable_devonc([pool_size, pool_size, features//2, features], stddev)
bd = bias_variable([features//2])
h_deconv = tf.nn.relu(deconv2d(in_node, wd, pool_size) + bd)
h_deconv_concat = crop_and_concat(dw_h_convs[layer], h_deconv)
deconv[layer] = h_deconv_concat
logging.info("up{layer}, pool size: {pool_size}x{pool_size}".format(layer=counter, pool_size=pool_size))
w1 = weight_variable([filter_size, filter_size, features, features//2], stddev)
w2 = weight_variable([filter_size, filter_size, features//2, features//2], stddev)
b1 = bias_variable([features//2])
b2 = bias_variable([features//2])
conv1 = conv2d(h_deconv_concat, w1, keep_prob)
logging.info("conv{layer}, features {features}, filter size {filter_size}x{filter_size}, activation: relu".format(layer=counter, features=features, filter_size=filter_size))
h_conv = tf.nn.relu(conv1 + b1)
conv2 = conv2d(h_conv, w2, keep_prob)
logging.info("conv{layer}, features {features}, filter size {filter_size}x{filter_size}, activation: relu".format(layer=counter, features=features, filter_size=filter_size))
in_node = tf.nn.relu(conv2 + b2)
up_h_convs[layer] = in_node
weights.append((w1, w2))
biases.append((b1, b2))
convs.append((conv1, conv2))
size *= 2
size -= 4
# Output Map
counter = counter + 1
weight = weight_variable([1, 1, features_root, n_class], stddev)
bias = bias_variable([n_class])
conv = conv2d(in_node, weight, tf.constant(1.0))
logging.info("conv{layer}, n_class: {n_class}, filter size 1x1, activation: relu".format(layer=counter, n_class=n_class))
output_map = tf.nn.relu(conv + bias)
up_h_convs["out"] = output_map
if summaries:
for i, (c1, c2) in enumerate(convs):
tf.summary.image('summary_conv_%02d_01'%i, get_image_summary(c1))
tf.summary.image('summary_conv_%02d_02'%i, get_image_summary(c2))
for k in pools.keys():
tf.summary.image('summary_pool_%02d'%k, get_image_summary(pools[k]))
for k in deconv.keys():
tf.summary.image('summary_deconv_concat_%02d'%k, get_image_summary(deconv[k]))
for k in dw_h_convs.keys():
tf.summary.histogram("dw_convolution_%02d"%k + '/activations', dw_h_convs[k])
for k in up_h_convs.keys():
tf.summary.histogram("up_convolution_%s"%k + '/activations', up_h_convs[k])
variables = []
for w1,w2 in weights:
variables.append(w1)
variables.append(w2)
for b1,b2 in biases:
variables.append(b1)
variables.append(b2)
return output_map, variables, int(in_size - size)