forked from noagarcia/keras_rmac
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rmac.py
98 lines (71 loc) · 2.61 KB
/
rmac.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
from __future__ import division
from __future__ import print_function
from keras.layers import Lambda, Dense, TimeDistributed, Input
from keras.models import Model
from keras.preprocessing import image
import keras.backend as K
from vgg16 import VGG16
from RoiPooling import RoiPooling
from get_regions import rmac_regions, get_size_vgg_feat_map
import scipy.io
import numpy as np
import utils
def addition(x):
sum = K.sum(x, axis=1)
return sum
def weighting(input):
x = input[0]
w = input[1]
w = K.repeat_elements(w, 512, axis=-1)
out = x * w
return out
def rmac(input_shape, num_rois):
# Load VGG16
vgg16_model = VGG16(utils.DATA_DIR + utils.WEIGHTS_FILE, input_shape)
# Regions as input
in_roi = Input(shape=(num_rois, 4), name='input_roi')
# ROI pooling
x = RoiPooling([1], num_rois)([vgg16_model.layers[-5].output, in_roi])
# Normalization
x = Lambda(lambda x: K.l2_normalize(x, axis=2), name='norm1')(x)
# PCA
x = TimeDistributed(Dense(512, name='pca',
kernel_initializer='identity',
bias_initializer='zeros'))(x)
# Normalization
x = Lambda(lambda x: K.l2_normalize(x, axis=2), name='pca_norm')(x)
# Addition
rmac = Lambda(addition, output_shape=(512,), name='rmac')(x)
# # Normalization
rmac_norm = Lambda(lambda x: K.l2_normalize(x, axis=1), name='rmac_norm')(rmac)
# Define model
model = Model([vgg16_model.input, in_roi], rmac_norm)
# Load PCA weights
mat = scipy.io.loadmat(utils.DATA_DIR + utils.PCA_FILE)
b = np.squeeze(mat['bias'], axis=1)
w = np.transpose(mat['weights'])
model.layers[-4].set_weights([w, b])
return model
if __name__ == "__main__":
# Load sample image
file = utils.DATA_DIR + 'sample.jpg'
img = image.load_img(file)
# Resize
scale = utils.IMG_SIZE / max(img.size)
new_size = (int(np.ceil(scale * img.size[0])), int(np.ceil(scale * img.size[1])))
print('Original size: %s, Resized image: %s' %(str(img.size), str(new_size)))
img = img.resize(new_size)
# Mean substraction
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = utils.preprocess_image(x)
# Load RMAC model
Wmap, Hmap = get_size_vgg_feat_map(x.shape[3], x.shape[2])
regions = rmac_regions(Wmap, Hmap, 3)
print('Loading RMAC model...')
model = rmac((x.shape[1], x.shape[2], x.shape[3]), len(regions))
# Compute RMAC vector
print('Extracting RMAC from image...')
RMAC = model.predict([x, np.expand_dims(regions, axis=0)])
print('RMAC size: %s' % RMAC.shape[1])
print('Done!')