-
Notifications
You must be signed in to change notification settings - Fork 0
/
dnn.py
169 lines (97 loc) · 3.68 KB
/
dnn.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
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
# -*- coding: utf-8 -*-
'''ResNet50 model for Keras.
# Reference:
- [Deep Residual Learning for Image Recognition](https://arxiv.org/abs/1512.03385)
Adapted from code contributed by BigMoyan.
'''
import numpy as np
import warnings, json
import keras
from keras.layers import merge, Input
from keras.layers import Dense, Activation, Flatten
from keras.layers import Convolution2D, MaxPooling2D, ZeroPadding2D, AveragePooling2D, UpSampling2D, Cropping2D
from keras.layers import BatchNormalization
from keras.models import Model, load_model
from keras.preprocessing import image
import keras.backend as K
from keras.utils.layer_utils import convert_all_kernels_in_model
from keras.utils.data_utils import get_file
from keras.applications.imagenet_utils import preprocess_input
from external.resnet50 import ResNet50
from external.vgg19 import VGG19
import matplotlib.pyplot as plt
import IPython, tempfile, shutil, os
""" Weights utils """
import h5py
def str_shape(x):
return 'x'.join(map(str, x.shape))
def clear_session_except_model(model):
model.save("cache.h5")
K.clear_session()
model = load_model("cache.h5")
os.remove("cache.h5")
return model
""" DNN FCN utils """
def constrain(l1, inputs):
model = Model(inputs, l1)
w1 = model.layers[-1].output_shape[2]
i1 = model.layers[0].batch_input_shape[2]
if w1 == i1:
return l1
s1 = (i1 - w1)//2
#print ("Inputs", w1, i1)
w1 = Cropping2D(cropping=((-s1, s1 + w1 - i1), (-s1, s1 + w1 - i1)))(l1)
return w1
def fully_convolutional(model):
inputs = model.input_tensor_fcn
arr = model.tensor_hooks_fcn
desired_output_shape = model.input_shape
for i in range(0, len(arr)):
cur_shape = Model(inputs, arr[i]).output_shape
scale_factor = desired_output_shape[-2]/cur_shape[-2]
arr[i] = UpSampling2D((scale_factor, scale_factor)) (arr[i])
#arr[i] = constrain(arr[i], inputs)
x = merge(arr, mode='concat', concat_axis=3)
return Model(inputs, x)
def resize(model, input_shape=(None, None, 3)):
json = model.get_config()
json['layers'][0]['config']['batch_input_shape'] = (None, ) + input_shape
model2 = Model.from_config(json)
model2.set_weights(model.get_weights())
return model2
def dilation_map(model):
json = model.get_config()
dilation = {}
for layer in json['layers']:
name = layer['name']
print (name)
if layer['class_name'] == 'InputLayer':
dilation[name] = 1
continue
prev = layer['inbound_nodes'][0][0][0]
print (prev)
dilation[name] = dilation[prev]
if layer['class_name'] in ['Convolution2D']:
layer['config']['dilation_rate'] = (dilation[prev], dilation[prev])
if layer['class_name'] in ['MaxPooling2D', 'Conv2D', 'Convolution2D', 'AveragePooling2D']:
mul = layer['config']['strides'][0]
dilation[name] = mul*dilation[prev]
layer['config']['strides'] = (1, 1)
layer['config']['padding'] = 'same'
model2 = Model.from_config(json)
model2.set_weights(model.get_weights())
return model2
if __name__ == '__main__':
model = VGG19(weights=None, input_shape=(64*7, 64*7, 3), classes=2, num_filters=16, pooling='avg')
model2 = dilation_map(model)
IPython.embed()
model2 = fully_convolutional(model)
model2.summary()
img_path = 'external/Africa.jpg'
img = image.load_img(img_path, target_size=(64*7, 64*7))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
print('Input image shape:', x.shape)
preds = model2.predict(x)
IPython.embed()