-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClassify_validation_images.py
71 lines (59 loc) · 2.91 KB
/
Classify_validation_images.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
#!/usr/bin/python
from argparse import ArgumentParser
import keras
from keras.models import model_from_json
import numpy as np
from model import *
from keras.preprocessing import image as kerasImage
import logging
from datetime import datetime
from keras.utils.generic_utils import CustomObjectScope
# models = ['ResNet50', 'DenseNet121', 'DenseNet169', 'DenseNet201', 'InceptionV3', 'InceptionResNetV2']
models = ['MobileNet', 'MobileNetV2']
directories = {"data/sample/valid/cats", "data/sample/valid/dogs"}
DEFAULT_DATE_TIME_FORMAT = "%Y%m%d-%H%M%S.%s"
classification_time_file = "./classificationTime.txt"
classification_results = "./classificationresults.txt"
header2 = ["Model Name", "Image Name", "Actual Class", "Classification Time", "Class", "Probability"]
append_row_to_csv(classification_results, header2)
logging.basicConfig(level=logging.DEBUG, format='[%(asctime)s] %(levelname)s - %(message)s', )
# format= '[%(asctime)s] {%(pathname)s:%(lineno)d} %(levelname)s - %(message)s',
logger = logging.getLogger(__name__)
def load_model(model_name: str):
with open('model_' + model_name + '_architecture.json', 'r') as f:
net_models = model_from_json(f.read())
net_models.load_weights('model-' + model_name + '-final.h5')
return net_models
for model in models:
if model.startswith("Mobile"):
logger.info('Loaded model' + model)
with CustomObjectScope({'relu6': keras.layers.ReLU(6.), 'DepthwiseConv2D':
keras.layers.DepthwiseConv2D}):
net_model = load_model(model)
else:
with open('model_' + model + '_architecture.json', 'r') as f:
net_model = model_from_json(f.read())
logger.info('Loaded model %s', model)
net_model.load_weights('model-' + model + '-final.h5')
logger.info('Loaded weights')
for d in directories:
directory = os.fsencode(d)
for file in os.listdir(directory):
filename = os.fsdecode(file)
t_start = datetime.now()
# preprocess input
raw_image = kerasImage.load_img(directory.decode('utf-8') + "/" + filename, target_size=IMAGE_SIZE)
x = kerasImage.img_to_array(raw_image)
x = np.expand_dims(x, axis=0)
post_processed_input_images = np.vstack([x])
output_probability = net_model.predict(post_processed_input_images)
output_classes = output_probability.argmax(axis=-1)
t_end = datetime.now()
difference_in_seconds = get_difference_in_seconds(t_start, t_end)
actual_type = filename.split(".")[0]
for idx, output_class in enumerate(output_classes):
row2 = []
row2.append(', '.join(
[model, filename, actual_type, str(difference_in_seconds), CLASS_LABEL[output_class],
str(output_probability[idx][output_class])]))
append_row_to_csv(classification_results, row2)