-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathVGG16_sequential.py
More file actions
202 lines (168 loc) · 7.69 KB
/
VGG16_sequential.py
File metadata and controls
202 lines (168 loc) · 7.69 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
194
195
196
197
198
199
200
201
'''VGG16 model for Keras.
# Reference:
- [Very Deep Convolutional Networks for Large-Scale Image Recognition](https://arxiv.org/abs/1409.1556)
'''
# from __future__ import print_function
# from __future__ import absolute_import
import warnings
import numpy as np
from keras.models import Sequential
from keras.layers import Flatten, Dense, Dropout, activations
from keras.layers import Convolution2D, MaxPooling2D, ZeroPadding2D
from keras.utils.layer_utils import convert_all_kernels_in_model
from keras.utils.data_utils import get_file
from keras.layers.normalization import BatchNormalization
from keras import backend as K
import socket
import os.path
WEIGHTS_PATH = '/pathTOWeights/vgg16_weights_init.h5'
# TH_WEIGHTS_PATH_DEEP_GLLIM = 'path/to/your_th_weights'
# TF_WEIGHTS_PATH_DEEP_GLLIM = 'path/to/your_tf_weights'
# TH_WEIGHTS_PATH_DEEP_GLLIM_PCA_BN = '/services/scratch/perception/dataBiwi/Deep_Gllim_pose86407_K2_weights.hdf5'
def VGG16(weights='imagenet'):
'''Instantiate the VGG16 architecture,
optionally loading weights pre-trained
on ImageNet. Note that when using TensorFlow,
for best performance you should set
`image_dim_ordering="tf"` in your Keras config
at ~/.keras/keras.json.
The model and the weights are compatible with both
TensorFlow and Theano. The dimension ordering
convention used by the model is the one
specified in your Keras config file.
# Arguments
include_top: whether to include the 3 fully-connected
layers at the top of the network.
weights: one of `deep_gllim` (fine tunned weights)
or "imagenet" (pre-training on ImageNet).
# Returns
A Keras model instance.
'''
# if weights not in {'imagenet', 'deep_gllim'}:
# raise ValueError('The `weights` argument should be either '
# '`imagenet` (pre-training on ImageNet)'
# 'or `deep_gllim` (fine tunned weights).')
# Determine proper input shape
if K.image_dim_ordering() == 'th':
INPUT_SHAPE = (3, 224, 224)
else:
INPUT_SHAPE = (224, 224, 3)
model = Sequential()
model.add(ZeroPadding2D((1,1),input_shape=INPUT_SHAPE))
model.add(Convolution2D(64, 3, 3, activation='relu', trainable=False))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(64, 3, 3, activation='relu', trainable=False))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(128, 3, 3, activation='relu', trainable=False))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(128, 3, 3, activation='relu', trainable=False))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(256, 3, 3, activation='relu', trainable=False))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(256, 3, 3, activation='relu', trainable=False))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(256, 3, 3, activation='relu', trainable=False))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu', trainable=False))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu', trainable=False))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu', trainable=False))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu', trainable=False))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu', trainable=False))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu', trainable=False))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(Flatten())
model.add(Dense(4096, activation='relu', trainable=True))
model.add(Dropout(0.5))
model.add(Dense(4096, activation='relu', trainable=True))
model.add(Dropout(0.5))
model.add(Dense(1000, activation='softmax', trainable=True))
# load weights
if weights == 'imagenet':
if K.image_dim_ordering() == 'th':
print "LOAD: " + WEIGHTS_PATH
weights_path = WEIGHTS_PATH
model.load_weights(weights_path)
model.pop() # remove softmax layer
model.pop() # remove dropout
if K.backend() == 'tensorflow':
warnings.warn('You are using the TensorFlow backend, yet you '
'are using the Theano '
'image dimension ordering convention '
'(`image_dim_ordering="th"`). '
'For best performance, set '
'`image_dim_ordering="tf"` in '
'your Keras config '
'at ~/.keras/keras.json.')
convert_all_kernels_in_model(model)
else:
weights_path = get_file('vgg16_weights_tf_dim_ordering_tf_kernels.h5',
TF_WEIGHTS_PATH,
cache_subdir='models')
model.load_weights(weights_path)
model.pop() # remove softmax layer
model.pop() # remove dropout
if K.backend() == 'theano':
convert_all_kernels_in_model(model)
elif weights == 'deep_gllim':
if K.image_dim_ordering() == 'th':
weights_path = TH_WEIGHTS_PATH_DEEP_GLLIM
model.load_weights(weights_path)
model.pop() # remove softmax layer
model.pop() # remove dropout
if K.backend() == 'tensorflow':
warnings.warn('You are using the TensorFlow backend, yet you '
'are using the Theano '
'image dimension ordering convention '
'(`image_dim_ordering="th"`). '
'For best performance, set '
'`image_dim_ordering="tf"` in '
'your Keras config '
'at ~/.keras/keras.json.')
convert_all_kernels_in_model(model)
else:
weights_path = TF_WEIGHTS_PATH_DEEP_GLLIM
model.load_weights(weights_path)
model.pop() # remove softmax layer
model.pop() # remove dropout
if K.backend() == 'theano':
convert_all_kernels_in_model(model)
elif weights == 'deep_gllim_PCA_BN':
model.pop() # remove softmax layer
model.pop() # remove dropout
model.add(Dense(512, activation='linear', trainable=False))
model.add(BatchNormalization())
weights_path = TH_WEIGHTS_PATH_DEEP_GLLIM_PCA_BN
model.load_weights(weights_path)
model.pop() # remove BN
return model
def extract_features_generator(network, generator, size):
'''Extract VGG features from a generator'''
print("Extracting features :")
features = network.predict_generator(generator, val_samples=size)
return features
def extract_features(network, x):
'''Extract VGG features from a generator'''
print("Extracting features :")
features = network.predict(x, batch_size=64)
return features
def extract_XY_generator(network, generator, size):
'''Extract VGG features and data targets from a generator'''
i=0
X=[]
Y=[]
for x,y in generator:
X.extend(network.predict_on_batch(x))
Y.extend(y)
i+=len(y)
if i>=size:
break
return np.asarray(X), np.asarray(Y)