-
Notifications
You must be signed in to change notification settings - Fork 1
/
VGG16.py
32 lines (27 loc) · 945 Bytes
/
VGG16.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
from keras.preprocessing import image
from keras.applications.vgg16 import VGG16, preprocess_input
from keras.models import Model
import numpy as np
from PIL import Image
# Model Defining
def init_model():
vgg16_model = VGG16(weights="imagenet")
extract_model = Model(inputs=vgg16_model.inputs, outputs = vgg16_model.get_layer("fc1").output)
return extract_model
# Image Preprocessing, image to tensor
def image_preprocess(img):
img = img.resize((224,224)) # VGG16 size constraint
img = img.convert("RGB")
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
return x
def extract_vector(model, image_path):
print("Extracting: ", image_path)
img = Image.open(image_path)
img_tensor = image_preprocess(img)
# Features extraction
vector = model.predict(img_tensor)[0]
# Vector normalization
vector = vector / np.linalg.norm(vector)
return vector