Manasa B Rao
commited on
Add files via upload
Browse files
app.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import tensorflow
|
3 |
+
|
4 |
+
from tensorflow.keras.preprocessing import image
|
5 |
+
from tensorflow.keras.layers import GlobalMaxPooling2D
|
6 |
+
from tensorflow.keras.applications.resnet50 import ResNet50,preprocess_input
|
7 |
+
from numpy.linalg import norm
|
8 |
+
import os
|
9 |
+
from tqdm import tqdm
|
10 |
+
import pickle
|
11 |
+
|
12 |
+
model = ResNet50(weights="imagenet", include_top=False,input_shape=(224,224,3))
|
13 |
+
model.trainable=False
|
14 |
+
|
15 |
+
model1 = tensorflow.keras.Sequential([
|
16 |
+
model,
|
17 |
+
GlobalMaxPooling2D()
|
18 |
+
])
|
19 |
+
|
20 |
+
def extract_features(img_path,model):
|
21 |
+
img=image.load_img(img_path,target_size = (224,224))
|
22 |
+
image_array = image.img_to_array(img)
|
23 |
+
expanded_image_array = np.expand_dims(image_array,axis=0)
|
24 |
+
processed_image = preprocess_input(expanded_image_array)
|
25 |
+
result = model.predict(processed_image).flatten()
|
26 |
+
normalized_result=result/norm(result)
|
27 |
+
return normalized_result
|
28 |
+
|
29 |
+
filenames =[]
|
30 |
+
|
31 |
+
for file in os.listdir('images'):
|
32 |
+
filenames.append(os.path.join('images',file))
|
33 |
+
|
34 |
+
feature_list = []
|
35 |
+
|
36 |
+
for i in tqdm(filenames):
|
37 |
+
feature_list.append(extract_features(i,model1))
|
38 |
+
|
39 |
+
print(np.array(feature_list).shape)
|
40 |
+
|
41 |
+
pickle.dump(feature_list,open('embeddings.pkl','wb'))
|
42 |
+
pickle.dump(filenames,open('filenames.pkl','wb'))
|