-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
266 lines (209 loc) · 9.69 KB
/
models.py
File metadata and controls
266 lines (209 loc) · 9.69 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import torch
import torch.nn as nn
from transformers import (
AutoImageProcessor,
AutoModelForImageClassification,
LayoutLMv3ForSequenceClassification,
LayoutLMv3Processor,
LayoutLMv3ImageProcessor,
LayoutLMv3TokenizerFast,
LayoutLMv3Config
)
from config import TEACHER_MODEL_NAME, STUDENT_MODEL_NAME, NUM_CLASSES, DEVICE
def load_teacher_model():
"""
Load DiT (Document Image Transformer) as teacher model
"""
print("Chargement du modèle Teacher (DiT)...")
# Load DiT processor and model
processor = AutoImageProcessor.from_pretrained(TEACHER_MODEL_NAME)
model = AutoModelForImageClassification.from_pretrained(TEACHER_MODEL_NAME)
# Set to evaluation mode and move to device
model.eval()
model.to(DEVICE)
# Freeze teacher model parameters
for param in model.parameters():
param.requires_grad = False
print(f"Teacher model loaded: {TEACHER_MODEL_NAME}")
return model, processor
def create_ultra_tiny_config():
"""
Create an ultra-lightweight LayoutLMv3 configuration with <10M parameters
Strategy:
- Hidden size: 768 -> 128 (very small)
- Num layers: 12 -> 2 (minimal depth)
- Attention heads: 12 -> 2 (minimal attention)
- Intermediate size: 3072 -> 256 (minimal FFN)
- Coordinate embedding: simplified
"""
config = LayoutLMv3Config.from_pretrained(STUDENT_MODEL_NAME)
# ULTRA-tiny modifications for <10M parameters
config.hidden_size = 64 # VERY small hidden dimension
config.num_hidden_layers = 1 # Only 1 transformer layer
config.num_attention_heads = 2 # Minimal attention heads
config.intermediate_size = 128 # Very small FFN
config.num_labels = NUM_CLASSES
# Adjust dependent parameters
config.coordinate_size = 64 # Match hidden_size
config.shape_size = 64 # Match hidden_size
# Simplify embeddings drastically
config.max_position_embeddings = 256 # Very small
config.max_2d_position_embeddings = 256 # Very small spatial positions
# Reduce vocab if possible
config.vocab_size = min(config.vocab_size, 30000) # Limit vocabulary
# Reduce vocabulary size if possible (use smaller tokenizer subset)
# Note: We'll keep original vocab to maintain compatibility
return config
# Custom class removed - using standard LayoutLMv3 with aggressive freezing instead
def load_student_model():
"""
Load ultra-lightweight student model with <10M parameters
Strategy: Use standard LayoutLMv3 with aggressive freezing for reliability
"""
print("Chargement du modèle Student Ultra-Léger (<10M paramètres)...")
# Load tokenizer
tokenizer = LayoutLMv3TokenizerFast.from_pretrained(STUDENT_MODEL_NAME)
# Simple processor
class SimpleLayoutLMv3Processor:
def __init__(self, tokenizer):
self.tokenizer = tokenizer
def __call__(self, text=None, words=None, boxes=None, return_tensors="pt", **kwargs):
if words is not None:
word_list = words
elif text is not None:
word_list = text.split() if text else ["document"]
else:
word_list = ["document"]
# Extract common parameters to avoid conflicts
padding = kwargs.pop('padding', True)
truncation = kwargs.pop('truncation', True)
max_length = kwargs.pop('max_length', 512)
if boxes is not None and len(boxes) > 0:
if len(boxes) != len(word_list):
if len(boxes) > len(word_list):
boxes = boxes[:len(word_list)]
else:
while len(boxes) < len(word_list):
boxes.append([0, 0, 0, 0])
# Clamp boxes to valid range to avoid CUDA indexing errors
if isinstance(boxes, list):
boxes = torch.tensor(boxes)
boxes = torch.clamp(boxes, 0, 999) # Safe range for LayoutLMv3
encoding = self.tokenizer(
word_list, boxes=boxes.tolist(), return_tensors=return_tensors,
padding=padding, truncation=truncation, max_length=max_length, **kwargs
)
else:
encoding = self.tokenizer(
word_list, return_tensors=return_tensors,
padding=padding, truncation=truncation, max_length=max_length, **kwargs
)
return encoding
processor = SimpleLayoutLMv3Processor(tokenizer)
# Use standard LayoutLMv3 with aggressive freezing (more reliable than custom model)
print("Using standard LayoutLMv3 with aggressive parameter freezing...")
# Load standard LayoutLMv3
model = LayoutLMv3ForSequenceClassification.from_pretrained(
STUDENT_MODEL_NAME,
num_labels=NUM_CLASSES,
ignore_mismatched_sizes=True
)
# FREEZE ALL parameters except classifier (ultra-aggressive freezing)
for name, param in model.named_parameters():
param.requires_grad = False
# Only classifier layers trainable
if 'classifier' in name:
param.requires_grad = True
# Move to device
model.to(DEVICE)
# Count parameters
total_params = sum(p.numel() for p in model.parameters())
trainable_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
print(f"Student model created: LayoutLMv3 Ultra-Frozen")
print(f"Total parameters: {total_params:,}")
print(f"Trainable parameters: {trainable_params:,} ({100*trainable_params/total_params:.3f}%)")
print(f"Model size: ~{total_params * 4 / 1024**2:.1f} MB")
# Check if we achieved our goal
if trainable_params <= 10_000_000:
print(f"✅ SUCCESS: Only {trainable_params:,} trainable parameters!")
print(f"📊 Compression: {total_params/trainable_params:.0f}x parameter reduction for training")
else:
print(f"⚠️ {trainable_params:,} trainable parameters (still over 10M)")
return model, processor
def get_model_info(model, name):
"""
Print model information
"""
total_params = sum(p.numel() for p in model.parameters())
trainable_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
print(f"\n{name} Model Info:")
print(f" Total parameters: {total_params:,}")
print(f" Trainable parameters: {trainable_params:,}")
print(f" Model size: ~{total_params * 4 / 1024 / 1024:.1f} MB")
def load_trained_student_model(model_path="student_model.pth"):
"""
Load the trained student model - same implementation as in test_student_model.py and gradio_interface.py
"""
print(f"Loading trained student model from {model_path}...")
# Load tokenizer and processor
tokenizer = LayoutLMv3TokenizerFast.from_pretrained(STUDENT_MODEL_NAME)
# Simple processor class
class SimpleLayoutLMv3Processor:
def __init__(self, tokenizer):
self.tokenizer = tokenizer
def __call__(self, text=None, words=None, boxes=None, return_tensors="pt", **kwargs):
if words is not None:
word_list = words
elif text is not None:
word_list = text.split() if text else ["document"]
else:
word_list = ["document"]
# Extract common parameters to avoid conflicts
padding = kwargs.pop('padding', True)
truncation = kwargs.pop('truncation', True)
max_length = kwargs.pop('max_length', 512)
if boxes is not None and len(boxes) > 0:
if len(boxes) != len(word_list):
if len(boxes) > len(word_list):
boxes = boxes[:len(word_list)]
else:
while len(boxes) < len(word_list):
boxes.append([0, 0, 0, 0])
# Clamp boxes to valid range
if isinstance(boxes, list):
boxes = torch.tensor(boxes)
boxes = torch.clamp(boxes, 0, 999)
encoding = self.tokenizer(
word_list, boxes=boxes.tolist(), return_tensors=return_tensors,
padding=padding, truncation=truncation, max_length=max_length, **kwargs
)
else:
encoding = self.tokenizer(
word_list, return_tensors=return_tensors,
padding=padding, truncation=truncation, max_length=max_length, **kwargs
)
return encoding
processor = SimpleLayoutLMv3Processor(tokenizer)
# Load the base model architecture
model = LayoutLMv3ForSequenceClassification.from_pretrained(
STUDENT_MODEL_NAME,
num_labels=NUM_CLASSES,
ignore_mismatched_sizes=True
)
# Load trained weights
try:
state_dict = torch.load(model_path, map_location=DEVICE)
model.load_state_dict(state_dict)
print("✅ Trained weights loaded successfully!")
except Exception as e:
print(f"❌ Error loading trained weights: {e}")
print(f"Using pre-trained weights instead...")
model.to(DEVICE)
model.eval()
return model, processor
if __name__ == "__main__":
# Test model loading
teacher_model, teacher_processor = load_teacher_model()
student_model, student_processor = load_student_model()
get_model_info(teacher_model, "Teacher (DiT)")
get_model_info(student_model, "Student (Ultra-Light)")