-
Notifications
You must be signed in to change notification settings - Fork 2
/
spatialfeatures.py
205 lines (157 loc) · 5.78 KB
/
spatialfeatures.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
"""
Extract spatial features with different CNN's
"""
import numpy as np
from keras.models import Model
from keras.layers import Input
from keras import backend as K
from keras.applications.vgg16 import VGG16
from keras.applications import Xception ###
from keras.applications.resnet50 import ResNet50 ###
from keras.applications.densenet import DenseNet121 ##
from keras.applications.densenet import DenseNet169 ###
from keras.applications.densenet import DenseNet201 ###
from keras.applications.mobilenetv2 import MobileNetV2 #
from keras.applications.vgg16 import preprocess_input as v_preprocess
from keras.applications.densenet import preprocess_input as d_preprocess
from keras.applications.resnet50 import preprocess_input as r_preprocess
from keras.applications.xception import preprocess_input as x_preprocess
from keras.applications.mobilenetv2 import preprocess_input as m_preprocess
import tensorflow as tf
class SFE_VGG16(object):
"""
Feature extractor using VGG16
"""
def __init__(self, source):
"""Create the graph of the VGG16 model.
Args:
source: Placeholder for the input tensor.
"""
# Parse input arguments into class variables
self.input = source
# Call the create function to build the computational graph of VGG16
self.create()
def create(self):
model = VGG16(weights="imagenet", include_top=False, input_shape=(227, 227, 3))
self.output = model.predict(self.input)
class SFE_Xception(object):
"""
Feature extractor using Xception Network
"""
def __init__(self, source):
"""Create the graph of the Xception model.
Args:
source: Placeholder for the input tensor.
"""
# Parse input arguments into class variables
self.input = source
# Call the create function to build the computational graph of Xception
self.create()
def create(self):
model = Xception(
weights="imagenet", include_top=False, input_shape=(227, 227, 3)
)
self.output = model.predict(self.input)
class SFE_ResNet50(object):
"""
Feature extractor using ResNet50 Network
"""
def __init__(self, source):
"""Create the graph of the ResNet50 model.
Args:
source: Placeholder for the input tensor.
"""
# Parse input arguments into class variables
self.input = source
# Call the create function to build the computational graph of ResNet50
self.create()
def create(self):
model = ResNet50(
weights="imagenet", include_top=False, input_shape=(227, 227, 3)
)
self.output = model.predict(self.input)
class SFE_DenseNet121(object):
"""
Feature extractor using DenseNet121 Network
"""
def __init__(self, source):
"""Create the graph of the DenseNet121 model.
Args:
source: Placeholder for the input tensor.
"""
print("DenseNet121(Input) : ", source.shape)
# Parse input arguments into class variables
self.input = source
# Call the create function to build the computational graph of DenseNet121
self.create()
def create(self):
# img_batch = d_preprocess(self.input)
img_batch = Input(tensor=self.input)
# batch_features = model(img_batch)
model = DenseNet121(
weights="imagenet",
include_top=False,
input_shape=(227, 227, 3),
input_tensor=img_batch,
pooling="max",
)
feature_batch = tf.identity(model.layers[-1].output, name="feature_batch")
self.output = tf.expand_dims(feature_batch, 1, name="cnn_output")
# self.output = feature_model.predict(
# img_batch, steps=self.input.shape[0])
# self.output = model.predict(img_data, steps=self.input.shape[0])
# self.output = model.predict(img_data)
class SFE_DenseNet169(object):
"""
Feature extractor using DenseNet169 Network
"""
def __init__(self, source):
"""Create the graph of the DenseNet169 model.
Args:
source: Placeholder for the input tensor.
"""
# Parse input arguments into class variables
self.input = source
# Call the create function to build the computational graph of DenseNet169
self.create()
def create(self):
model = DenseNet169(
weights="imagenet", include_top=False, input_shape=(227, 227, 3)
)
self.output = model.predict(self.input)
class SFE_DenseNet201(object):
"""
Feature extractor using DenseNet201 Network
"""
def __init__(self, source):
"""Create the graph of the DenseNet201 model.
Args:
source: Placeholder for the input tensor.
"""
# Parse input arguments into class variables
self.input = source
# Call the create function to build the computational graph of DenseNet201
self.create()
def create(self):
model = DenseNet201(
weights="imagenet", include_top=False, input_shape=(227, 227, 3)
)
self.output = model.predict(self.input)
class SFE_MobileNetV2(object):
"""
Feature extractor using MobileNetV2 Network
"""
def __init__(self, source):
"""Create the graph of the MobileNetV2 model.
Args:
source: Placeholder for the input tensor.
"""
# Parse input arguments into class variables
self.input = source
# Call the create function to build the computational graph of MobileNetV2
self.create()
def create(self):
model = MobileNetV2(
weights="imagenet", include_top=False, input_shape=(227, 227, 3)
)
self.output = model.predict(self.input)