-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract_cnn_vgg16_keras.py
145 lines (122 loc) · 4.98 KB
/
extract_cnn_vgg16_keras.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
# -*- coding: utf-8 -*-
# Author: yongyuan.name
import numpy as np
#import deeplearning
from numpy import linalg as LA
from sklearn.decomposition import PCA
#from keras.applications.vgg16 import VGG16
#from keras.preprocessing import image
#from keras.applications.vgg16 import preprocess_input
from deeplearning.vgg16 import VGG16
from deeplearning.resnet50 import ResNet50
from keras.preprocessing import image
from deeplearning.imagenet_utils import preprocess_input
from keras.models import Model
class VGGNet:
def __init__(self):
# weights: 'imagenet'
# pooling: 'max' or 'avg'
# input_shape: (width, height, 3), width and height should >= 48
self.input_shape = (224, 224, 3)
self.weight = 'imagenet'
self.pooling = 'max'
base_model = VGG16(weights = self.weight, input_shape = (self.input_shape[0], self.input_shape[1], self.input_shape[2]), pooling = self.pooling, include_top = False)
self.model = Model(inputs=base_model.input, outputs=base_model.get_layer('block3_pool').output)
self.model.predict(np.zeros((1, 224, 224 , 3)))
'''
Use vgg16 model to extract features
Output normalized feature vector
'''
def extract_feat(self, img_path):
img = image.load_img(img_path, target_size=(self.input_shape[0], self.input_shape[1]))
img = image.img_to_array(img)
img = np.expand_dims(img, axis=0)
img = preprocess_input(img)
feat = self.model.predict(img)
print(np.shape(feat))
# print(feat[0])
# feat = feat.ravel()
norm_feat = feat[0]/LA.norm(feat[0])
norm_feat = norm_feat.T
# print(np.shape(norm_feat))
# norm_feat = norm_feat.reshape(256,-1)
# try:
# print(np.shape(norm_feat))
# pca = PCA(n_components=128)
# pca.fit(norm_feat)
# norm_feat = pca.transform(norm_feat)
# norm_feat = norm_feat/LA.norm(norm_feat)
# except:
# print("SVD did not converge")
# print("--------------------------------------------------------------------------------------------------------------")
# # norm_feat = norm_feat[~np.isnan(norm_feat)]
# # print(np.shape(norm_feat))
# # pca = PCA(n_components=128)
# # pca.fit(norm_feat)
# # norm_feat = pca.transform(norm_feat)
return norm_feat
def max_mask(fea):
mask = np.zeros((np.shape(fea)[1],np.shape(fea)[2]),dtype=bool)
for j in range(0, np.shape(fea)[0]):
temp = fea[j, :, :]
# print(np.shape(temp))
m = temp.max(1)
p1 = np.argmax(temp,axis=1)
p2 = np.argmax(m)
# print (p1,p2)
mask[p1[p2], p2] = 1
mask = mask[:]
return mask
class ResNet:
def __init__(self):
# weights: 'imagenet'
# pooling: 'max' or 'avg'
# input_shape: (width, height, 3), width and height should >= 48
self.input_shape = (224, 224, 3)
self.weight = 'imagenet'
self.pooling = 'max'
base_model = ResNet50(weights = self.weight, input_shape = (self.input_shape[0], self.input_shape[1], self.input_shape[2]), pooling = self.pooling, include_top = False)
self.model = Model(inputs=base_model.input, outputs=base_model.get_layer('bn3d_branch2c').output)
self.model.predict(np.zeros((1, 224, 224 , 3)))
'''
Use vgg16 model to extract features
Output normalized feature vector
'''
def extract_feat(self, img_path):
img = image.load_img(img_path, target_size=(self.input_shape[0], self.input_shape[1]))
img = image.img_to_array(img)
img = np.expand_dims(img, axis=0)
img = preprocess_input(img)
feat = self.model.predict(img)
#print(np.shape(feat))
# print(feat[0])
# feat = feat.ravel()
norm_feat = feat[0]/LA.norm(feat[0])
norm_feat = norm_feat.T
print(np.shape(norm_feat))
# norm_feat = norm_feat.reshape(256,-1)
# try:
# print(np.shape(norm_feat))
# pca = PCA(n_components=128)
# pca.fit(norm_feat)
# norm_feat = pca.transform(norm_feat)
# norm_feat = norm_feat/LA.norm(norm_feat)
# except:
# print("SVD did not converge")
# print("--------------------------------------------------------------------------------------------------------------")
# # norm_feat = norm_feat[~np.isnan(norm_feat)]
# # print(np.shape(norm_feat))
# # pca = PCA(n_components=128)
# # pca.fit(norm_feat)
# # norm_feat = pca.transform(norm_feat)
#try:
mask = max_mask(norm_feat)
# print(mask)
mask = np.tile(mask,[np.shape(norm_feat)[0],1,1])
print(np.shape(mask))
masked_fea = norm_feat[mask]
print (np.shape(masked_fea))
#norm_feat = np.reshape()
#except:
#print("mask error")
return norm_feat