This repository was archived by the owner on Jan 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinteractive_classifier.py
More file actions
59 lines (46 loc) · 1.7 KB
/
interactive_classifier.py
File metadata and controls
59 lines (46 loc) · 1.7 KB
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
import sys
import os
import tensorflow as tf
from utils import *
from tensorflow.keras.preprocessing import image
from tensorflow.keras.callbacks import Callback
IMG_SIZE = (224,224)
IMG_SHAPE = IMG_SIZE + (3,)
base_model = tf.keras.applications.MobileNetV2(
input_shape=IMG_SHAPE,
include_top=False,
weights='imagenet'
)
preprocess_input = tf.keras.applications.mobilenet_v2.preprocess_input
global_average_layer = tf.keras.layers.GlobalAveragePooling2D()
prediction_layer = tf.keras.layers.Dense(1, activation='sigmoid')
inputs = tf.keras.Input(shape=IMG_SHAPE)
x = preprocess_input(inputs)
x = base_model(x, training=False)
x = global_average_layer(x)
outputs = prediction_layer(x)
model = tf.keras.Model(inputs, outputs)
model.load_weights("results/imagenet/trained_model/epoch_20.weights.h5")
def classify_image(filename):
img = image.load_img(filename, target_size=IMG_SIZE)
img_array = image.img_to_array(img)
img_array = tf.expand_dims(img_array, 0)
prediction = model.predict(img_array)
confidence = prediction[0][0]
return confidence
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: python classify.py <image_filename>")
sys.exit(1)
filenames = sys.argv[1:]
predicted_yale = 0
predicted_non_yale = 0
for filename in filenames:
confidence = classify_image(filename)
predicted_class = "Yale" if confidence > 0.90 else "Not Yale"
print(f"{filename} :: Predicted class: {predicted_class} (confidence: {confidence})")
if predicted_class == "Yale":
predicted_yale += 1
else:
predicted_non_yale += 1
print(f"{predicted_yale} Yale, {predicted_non_yale} Non-Yale")