forked from pooya-mohammadi/crnn-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.py
146 lines (129 loc) · 4.88 KB
/
settings.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import torch
# from torchvision import transforms
import albumentations as A
from dataclasses import dataclass
from albumentations.pytorch import ToTensorV2
from alphabets import ALPHABETS
from argparse import Namespace
@dataclass(init=True)
class BasicConfig:
img_h = 32 # the height of the input image to network
img_w = 100 # the width of the input image to network
file_name = "best"
# Modify
n_classes = 35
mean = [0.4845]
std = [0.1884]
alphabet_name = "FA_LPR"
train_directory = '/home/ai/projects/vehicle-plate-recognition-training/recognition/datasets/train'
val_directory = '/home/ai/projects/vehicle-plate-recognition-training/recognition/datasets/val'
output_dir = "output"
def update_basic(self):
self.n_classes = len(self.alphabets) + 1
@dataclass(init=True, repr=True)
class AugConfig(BasicConfig):
# train_transform = transforms.Compose([
# transforms.Grayscale(),
# transforms.Resize((BasicConfig.img_h, BasicConfig.img_w)),
# transforms.ToTensor(),
# transforms.Normalize(mean=BasicConfig.mean, std=BasicConfig.std), ]
# )
# val_transform = transforms.Compose([
# transforms.Grayscale(),
# transforms.Scale()
# transforms.Resize((BasicConfig.img_h, BasicConfig.img_w)),
# transforms.ToTensor(),
# transforms.Normalize(mean=BasicConfig.mean, std=BasicConfig.std), ]
# )
train_transform = A.Compose(
[A.Rotate(limit=10, p=0.2),
A.RandomScale(scale_limit=0.2),
A.Resize(height=BasicConfig.img_h, width=BasicConfig.img_w),
A.Normalize(BasicConfig.mean, BasicConfig.std, max_pixel_value=255.0),
A.ToGray(always_apply=True, p=1),
ToTensorV2()
])
val_transform = A.Compose(
[
A.Resize(height=BasicConfig.img_h, width=BasicConfig.img_w),
A.Normalize(BasicConfig.mean, BasicConfig.std, max_pixel_value=255.0),
A.ToGray(always_apply=True, p=1),
ToTensorV2()
])
def update_aug(self):
# self.train_transform = transforms.Compose([
# transforms.Grayscale(),
# transforms.Resize((self.img_h, self.img_w)),
# transforms.ToTensor(),
# transforms.Normalize(mean=self.mean, std=self.std), ]
# )
# self.val_transform = transforms.Compose([
# transforms.Grayscale(),
# transforms.Resize((self.img_h, self.img_w)),
# transforms.ToTensor(),
# transforms.Normalize(mean=self.mean, std=self.std), ]
# )
self.train_transform = A.Compose(
[A.Rotate(limit=10, p=0.2),
A.RandomScale(scale_limit=0.2),
A.Resize(height=self.img_h, width=self.img_w),
A.Normalize(self.mean, self.std, max_pixel_value=255.0),
A.ToGray(always_apply=True, p=1),
ToTensorV2()
])
self.val_transform = A.Compose(
[A.Resize(height=self.img_h, width=self.img_w),
A.Normalize(self.mean, self.std, max_pixel_value=255.0),
A.ToGray(always_apply=True, p=1),
ToTensorV2()
])
@dataclass(init=True)
class Config(AugConfig):
n_hidden = 256 # size of the lstm hidden state
lstm_input = 64 # size of the lstm_input feature size
n_channels = 1
device = 'cuda' if torch.cuda.is_available() else 'cpu'
lr = 0.0005
lr_patience = 10
min_lr = 5e-6
lr_reduce_factor = 0.1
batch_size = 128
epochs = 200
n_workers = 8
alphabets = ALPHABETS[BasicConfig.alphabet_name]
char2label = dict()
label2char = dict()
# Early stopping
early_stopping_patience = 30
def update_config_param(self, args):
if isinstance(args, Namespace):
variables = vars(args)
elif isinstance(args, dict):
variables = args
else:
raise ValueError()
for k, v in variables.items():
if hasattr(self, k):
setattr(self, k, v)
elif k == "visualize":
print("[INFO] Skipped visualize argument!")
else:
raise ValueError(f"value {k} is not defined in Config...")
self.update()
def update(self):
self.char2label = {char: i + 1 for i, char in enumerate(self.alphabets)}
self.label2char = {label: char for char, label in self.char2label.items()}
self.update_basic()
self.update_aug()
def __repr__(self):
variables = vars(self)
return f"{self.__class__.__name__} -> " + ", ".join(f"{k}: {v}" for k, v in variables.items())
def vars(self) -> dict:
out = dict()
for key in dir(self):
val = getattr(self, key)
if (key.startswith("__") and key.endswith("__")) or type(val).__name__ == "method":
continue
else:
out[key] = val
return out