-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
29 lines (25 loc) · 729 Bytes
/
test.py
File metadata and controls
29 lines (25 loc) · 729 Bytes
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
import tensorflow as tf
import numpy as np
from PIL import Image
from tensorflow.keras.applications import ResNet50
from tensorflow.keras.applications.resnet50 import preprocess_input
# Load frozen ResNet50
base_model = ResNet50(
weights="imagenet",
include_top=False,
pooling="avg"
)
base_model.trainable = False
def extract_features(img_path):
img = Image.open(img_path).convert("RGB")
img = img.resize((224, 224))
img = np.array(img)
img = np.expand_dims(img, axis=0)
img = preprocess_input(img)
features = base_model(img, training=False)
return features.numpy().squeeze()
# Test
feat = extract_features("test.jpg")
feat_2d = feat.reshape(-1, 1)
print(feat_2d.shape)
print(feat_2d)