forked from PatrickLib/captcha_recognize
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodel.py
More file actions
180 lines (146 loc) · 5.12 KB
/
Copy pathmodel.py
File metadata and controls
180 lines (146 loc) · 5.12 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
"""
Modern CAPTCHA recognition model using TensorFlow 2.x and Keras.
"""
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from typing import Tuple, Optional
import logging
from .config import ModelConfig
logger = logging.getLogger(__name__)
class CaptchaModel(keras.Model):
"""
Modern CAPTCHA recognition model using CNN architecture.
This model is designed to recognize CAPTCHA images with multiple characters.
It uses a convolutional neural network with multiple convolutional layers
followed by fully connected layers for character classification.
"""
def __init__(self, config: ModelConfig, name: str = "captcha_model"):
super().__init__(name=name)
self.config = config
self._build_model()
def _build_model(self):
"""Build the model architecture."""
# Input layer
self.input_layer = layers.Input(shape=self.config.input_shape)
# Convolutional layers
self.conv1 = layers.Conv2D(
filters=64,
kernel_size=(3, 3),
activation='relu',
padding='same',
name='conv1'
)
self.pool1 = layers.MaxPooling2D(pool_size=(2, 2), name='pool1')
self.conv2 = layers.Conv2D(
filters=64,
kernel_size=(3, 3),
activation='relu',
padding='same',
name='conv2'
)
self.pool2 = layers.MaxPooling2D(pool_size=(2, 2), name='pool2')
self.conv3 = layers.Conv2D(
filters=64,
kernel_size=(3, 3),
activation='relu',
padding='same',
name='conv3'
)
self.pool3 = layers.MaxPooling2D(pool_size=(2, 2), name='pool3')
self.conv4 = layers.Conv2D(
filters=64,
kernel_size=(3, 3),
activation='relu',
padding='same',
name='conv4'
)
self.pool4 = layers.MaxPooling2D(pool_size=(2, 2), name='pool4')
# Flatten and dense layers
self.flatten = layers.Flatten()
self.dense1 = layers.Dense(1024, activation='relu', name='dense1')
self.dropout = layers.Dropout(0.5)
# Output layer - one dense layer per character position
self.output_layers = []
for i in range(self.config.chars_num):
output_layer = layers.Dense(
self.config.classes_num,
activation='softmax',
name=f'char_{i}_output'
)
self.output_layers.append(output_layer)
def call(self, inputs, training=None):
"""Forward pass through the model."""
x = inputs
# Convolutional layers
x = self.conv1(x)
x = self.pool1(x)
x = self.conv2(x)
x = self.pool2(x)
x = self.conv3(x)
x = self.pool3(x)
x = self.conv4(x)
x = self.pool4(x)
# Flatten and dense layers
x = self.flatten(x)
x = self.dense1(x)
x = self.dropout(x, training=training)
# Output layers for each character
outputs = []
for output_layer in self.output_layers:
char_output = output_layer(x)
outputs.append(char_output)
return outputs
def build_model(self) -> keras.Model:
"""Build and return a functional model."""
inputs = self.input_layer
outputs = self.call(inputs)
model = keras.Model(inputs=inputs, outputs=outputs, name=self.name)
return model
def create_model(config: ModelConfig) -> keras.Model:
"""
Factory function to create a CAPTCHA recognition model.
Args:
config: Model configuration
Returns:
Compiled Keras model
"""
model_instance = CaptchaModel(config)
model = model_instance.build_model()
# Compile the model
model.compile(
optimizer=keras.optimizers.Adam(learning_rate=config.learning_rate),
loss='categorical_crossentropy',
metrics=['accuracy']
)
logger.info(f"Created model with {model.count_params()} parameters")
return model
def load_model(model_path: str, config: ModelConfig) -> keras.Model:
"""
Load a pre-trained model from disk.
Args:
model_path: Path to the saved model
config: Model configuration
Returns:
Loaded Keras model
"""
try:
model = keras.models.load_model(model_path)
logger.info(f"Model loaded successfully from {model_path}")
return model
except Exception as e:
logger.error(f"Failed to load model from {model_path}: {e}")
raise
def save_model(model: keras.Model, model_path: str):
"""
Save a trained model to disk.
Args:
model: Keras model to save
model_path: Path where to save the model
"""
try:
model.save(model_path)
logger.info(f"Model saved successfully to {model_path}")
except Exception as e:
logger.error(f"Failed to save model to {model_path}: {e}")
raise