-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
128 lines (116 loc) · 4.07 KB
/
train.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
from fastai.vision.all import ImageDataLoaders, vision_learner, \
models, accuracy, aug_transforms
from pathlib import Path
import argparse
from zipfile import ZipFile
import subprocess
import os
import sys
import warnings
import logging
import torch
import gc
torch.cuda.empty_cache()
gc.collect()
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - [%(filename)s:%(lineno)d] \
in %(funcName)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
logger = logging.getLogger(__name__)
warnings.filterwarnings("ignore")
def train_model(dataset_train_path: Path) -> str:
dataset_name = dataset_train_path.name
try:
logger.info(f'Loading {dataset_name} dataset')
data = ImageDataLoaders.from_folder(
dataset_train_path,
valid_pct=0.2,
size=224,
num_workers=4,
bs=4,
batch_tfms=aug_transforms(mult=2)
)
logger.info('Loading dataset done')
logger.info('Training model')
learn = vision_learner(data, models.vgg19_bn, metrics=accuracy)
learn.fit(2)
logger.info('Success')
except Exception as e:
logger.error('Error while training model')
raise e
try:
logger.info('Saving model')
learn.path = Path('data', 'models', dataset_name)
if not os.path.isdir(learn.path):
os.makedirs(Path(learn.path))
version = 1
while os.path.isfile(
Path(
learn.path,
f'{dataset_name}_vgg19_v{version}.pkl')
):
version += 1
model_to_save = f'{dataset_name}_vgg19_v{version}.pkl'
learn.export(Path(model_to_save))
logger.info('Success')
return Path(model_to_save)
except Exception as e:
logger.error('Error while saving model')
raise e
def zip_model(
dataset_path: Path,
model_to_save: Path
) -> None:
try:
model_path = Path('data', 'models', dataset_path, model_to_save)
with ZipFile(model_path.with_suffix('.zip'), 'w') as zipObj:
logger.info(f'Zipping {model_to_save}')
zipObj.write(model_path, model_to_save.name)
logger.info(f'Zipping {dataset_path}')
for folderName, subfolders, filenames in os.walk(dataset_path):
for filename in filenames:
filePath = os.path.join(folderName, filename)
zipObj.write(filePath)
logger.info('Success')
logger.info(
f'Moving {model_to_save.with_suffix(".zip")} \
to zipped_files folder')
zipped_files_path = Path('data', 'zipped_files')
if not os.path.isdir(zipped_files_path):
os.makedirs(zipped_files_path)
os.rename(Path(model_path.with_suffix('.zip')),
Path(
zipped_files_path,
f'{model_to_save.with_suffix(".zip")}')
)
logger.info('Success')
except Exception as e:
logger.error('Error while zipping model')
raise e
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
'path',
type=str,
default=Path('data/images/'),
help='Path to image dataset to train the model on'
)
args = parser.parse_args()
try:
if not os.path.isdir(args.path):
raise NotADirectoryError('Path is not a directory.')
if not len(os.listdir(args.path)) > 0 or \
not os.path.isdir(Path(args.path, os.listdir(args.path)[0])):
raise FileNotFoundError('Subdirectories not found.')
if not len(list(Path(args.path).glob('**/*.JPG'))) > 0:
raise FileNotFoundError('No image files found.')
logger.info('Augmenting images')
subprocess.run(['python3', 'Augmentation.py', args.path])
logger.info('Image augmentation done')
model = train_model(Path('augmented_directory'))
zip_model(Path('augmented_directory'), model)
except Exception as e:
logger.error(e)
sys.exit(1)