forked from OlafenwaMoses/ImageAI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_custom_detection_training.py
83 lines (66 loc) · 2.39 KB
/
test_custom_detection_training.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
72
73
74
75
76
77
78
79
80
81
82
83
import os, sys
import shutil
import pytest
from os.path import dirname
sys.path.insert(1, os.path.join(dirname(dirname(os.path.abspath(__file__)))))
from imageai.Detection.Custom import DetectionModelTrainer
test_folder = dirname(os.path.abspath(__file__))
detection_dataset = os.path.join(
test_folder,
"data-datasets",
"number-plate"
)
pretrained_models_folder = os.path.join(
test_folder,
"data-models"
)
def delete_cache(dirs: list):
for dir in dirs:
if os.path.isdir(dir):
shutil.rmtree(dir)
@pytest.mark.parametrize(
"transfer_learning",
[
(os.path.join(
pretrained_models_folder,
"yolov3.pt"
)),
(None),
]
)
def test_yolov3_training(transfer_learning):
json_dir = os.path.join(detection_dataset, "json")
json_file = os.path.join(json_dir, "number-plate_yolov3_detection_config.json")
models_dir = os.path.join(detection_dataset, "models")
delete_cache([json_dir, models_dir])
trainer = DetectionModelTrainer()
trainer.setModelTypeAsYOLOv3()
trainer.setDataDirectory(data_directory=detection_dataset)
trainer.setTrainConfig(object_names_array=["number-plate"], batch_size=2, num_experiments=2, train_from_pretrained_model=transfer_learning)
trainer.trainModel()
assert os.path.isfile(json_file)
assert len([file for file in os.listdir(models_dir) if file.endswith(".pt")]) > 0
delete_cache([json_dir, models_dir])
@pytest.mark.parametrize(
"transfer_learning",
[
(os.path.join(
pretrained_models_folder,
"tiny-yolov3.pt"
)),
(None),
]
)
def test_tiny_yolov3_training(transfer_learning):
json_dir = os.path.join(detection_dataset, "json")
json_file = os.path.join(json_dir, "number-plate_tiny-yolov3_detection_config.json")
models_dir = os.path.join(detection_dataset, "models")
delete_cache([json_dir, models_dir])
trainer = DetectionModelTrainer()
trainer.setModelTypeAsTinyYOLOv3()
trainer.setDataDirectory(data_directory=detection_dataset)
trainer.setTrainConfig(object_names_array=["number-plate"], batch_size=2, num_experiments=2, train_from_pretrained_model=transfer_learning)
trainer.trainModel()
assert os.path.isfile(json_file)
assert len([file for file in os.listdir(models_dir) if file.endswith(".pt")]) > 0
delete_cache([json_dir, models_dir])