-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
68 lines (55 loc) · 2.59 KB
/
test.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
import keras
import glob
import os
import importlib
from tqdm import tqdm
import numpy as np
from cv2 import cv2
import validation
import utils
class TestClass:
def __init__(self, project_name, weights_name='latest'):
# call ValidationClass to get all the paths and config file
validObj = validation.ValidateClass(project_name)
self.paths = validObj.paths
self.config = validObj.config
# get model name from function arg
self.weights_name = weights_name
self.weights_path = self.get_weights_path(self.weights_name)
def get_weights_path(self, weight_name):
''' This function load the weight file (latest/custom) '''
if weight_name == 'latest':
utils.print_head("\nUsing latest weight file for testing model....\n", color ='darkcyan')
# Find latest weights
list_of_weights = glob.glob(f'{self.paths["model_weights_path"]}/*.h5') # * means all if need specific format then *.csv
latest_weight = max(list_of_weights, key=os.path.getctime)
return latest_weight
else:
utils.print_head(f"Loading weigth file {weight_name} for testing model....", color='darkcyan')
return f"{self.paths['model_weights_path']}/{weight_name}"
def load_testing_dataset(self):
''' This function loads the testing dataset '''
utils.print_head('Testing dataset loaded...', 'darkcyan')
self.test_datagen = utils.load_test_dataset(self.paths['test_dataset_path'], self.config)
def load_model(self):
utils.print_head('Model loaded for Testing...', 'darkcyan')
self.model = keras.models.load_model(self.weights_path)
self.classes = np.load(f"{self.paths['class_file_path']}/{self.paths['class_file_name']}")
def evaluate_mode(self):
# load model
self.load_model()
# load testing dataset
self.load_testing_dataset()
steps = self.test_datagen.samples // self.config.batch_size
if(steps < 1):
steps = self.test_datagen.samples
utils.print_head('Evaluate model on testing dataset..', 'darkcyan')
best_scores = self.model.evaluate_generator(self.test_datagen, steps=steps)
utils.print_head(f"Best Weight's Accuracy: {utils.font_bold(round((best_scores[1]) * 100, 2))}%", color='darkcyan')
def test(name, weights_name='latest'):
# call test class
testObj = TestClass(name, weights_name)
# load test dataset
testObj.load_testing_dataset()
# evaluate the model
testObj.evaluate_mode()