forked from PatrickLib/captcha_recognize
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig.py
More file actions
108 lines (89 loc) · 3.04 KB
/
config.py
File metadata and controls
108 lines (89 loc) · 3.04 KB
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
"""
Configuration settings for the CAPTCHA recognition system.
"""
import os
from dataclasses import dataclass
from typing import Optional
from pathlib import Path
@dataclass
class ModelConfig:
"""Model architecture configuration."""
image_height: int = 48
image_width: int = 128
char_set: str = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
chars_num: int = 5
@property
def classes_num(self) -> int:
"""Number of character classes."""
return len(self.char_set)
@property
def input_shape(self) -> tuple:
"""Input image shape."""
return (self.image_height, self.image_width, 1)
@dataclass
class TrainingConfig:
"""Training configuration."""
batch_size: int = 128
learning_rate: float = 1e-4
epochs: int = 100
validation_split: float = 0.2
early_stopping_patience: int = 10
checkpoint_dir: str = './checkpoints'
model_save_path: str = './models/captcha_model.h5'
@dataclass
class DataConfig:
"""Data configuration."""
record_dir: str = './data'
train_file: str = 'train.tfrecords'
valid_file: str = 'valid.tfrecords'
test_data_dir: str = './data/test_data'
@property
def train_path(self) -> Path:
"""Full path to training data."""
return Path(self.record_dir) / self.train_file
@property
def valid_path(self) -> Path:
"""Full path to validation data."""
return Path(self.record_dir) / self.valid_file
@dataclass
class Config:
"""Main configuration class."""
model: ModelConfig = None
training: TrainingConfig = None
data: DataConfig = None
def __post_init__(self):
"""Initialize default configurations if not provided."""
if self.model is None:
self.model = ModelConfig()
if self.training is None:
self.training = TrainingConfig()
if self.data is None:
self.data = DataConfig()
@classmethod
def from_env(cls) -> 'Config':
"""Create configuration from environment variables."""
return cls(
model=ModelConfig(
image_height=int(os.getenv('CAPTCHA_IMAGE_HEIGHT', 48)),
image_width=int(os.getenv('CAPTCHA_IMAGE_WIDTH', 128)),
chars_num=int(os.getenv('CAPTCHA_CHARS_NUM', 5))
),
training=TrainingConfig(
batch_size=int(os.getenv('CAPTCHA_BATCH_SIZE', 128)),
learning_rate=float(os.getenv('CAPTCHA_LEARNING_RATE', 1e-4)),
epochs=int(os.getenv('CAPTCHA_EPOCHS', 100))
),
data=DataConfig(
record_dir=os.getenv('CAPTCHA_RECORD_DIR', './data'),
test_data_dir=os.getenv('CAPTCHA_TEST_DIR', './data/test_data')
)
)
# Backward compatibility
IMAGE_HEIGHT = 48
IMAGE_WIDTH = 128
CHAR_SET = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
CLASSES_NUM = len(CHAR_SET)
CHARS_NUM = 5
RECORD_DIR = './data'
TRAIN_FILE = 'train.tfrecords'
VALID_FILE = 'valid.tfrecords'