Spaces:
Running
Running
Update README.md
Browse files
README.md
CHANGED
@@ -10,3 +10,172 @@ sdk_version: 3.4
|
|
10 |
app_file: app.py
|
11 |
pinned: true
|
12 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
app_file: app.py
|
11 |
pinned: true
|
12 |
---
|
13 |
+
|
14 |
+
|
15 |
+
import numpy as np
|
16 |
+
import time
|
17 |
+
from pathlib import Path
|
18 |
+
|
19 |
+
# Import your PyLaia implementation
|
20 |
+
from htrflow.models.teklia.pylaia import PyLaia
|
21 |
+
from htrflow.utils.imgproc import read
|
22 |
+
|
23 |
+
NORMAL_IMAGE_PATH = "examples/images/lines/A0068699_00021_region0_line1.jpg"
|
24 |
+
|
25 |
+
|
26 |
+
def create_test_images(base_image_path, num_images=100):
|
27 |
+
"""Create test images - mix of real and synthetic variations."""
|
28 |
+
images = []
|
29 |
+
|
30 |
+
# Load the real image
|
31 |
+
real_image = read(base_image_path)
|
32 |
+
|
33 |
+
# Create test images
|
34 |
+
for i in range(num_images):
|
35 |
+
if i % 2 == 0:
|
36 |
+
# Use the real image
|
37 |
+
images.append(real_image.copy())
|
38 |
+
else:
|
39 |
+
# Create a slightly modified version (add some noise)
|
40 |
+
noisy_image = real_image.copy()
|
41 |
+
noise = np.random.normal(0, 10, real_image.shape).astype(np.uint8)
|
42 |
+
noisy_image = np.clip(noisy_image.astype(np.int16) + noise, 0, 255).astype(np.uint8)
|
43 |
+
images.append(noisy_image)
|
44 |
+
|
45 |
+
return images
|
46 |
+
|
47 |
+
|
48 |
+
def benchmark_pylaia(model_name="Teklia/pylaia-belfort", num_images=100):
|
49 |
+
"""Benchmark PyLaia with different chunk sizes."""
|
50 |
+
|
51 |
+
print(f"\n{'='*80}")
|
52 |
+
print(f"PyLaia Chunking Performance Benchmark")
|
53 |
+
print(f"{'='*80}")
|
54 |
+
print(f"Model: {model_name}")
|
55 |
+
print(f"Number of test images: {num_images}")
|
56 |
+
|
57 |
+
# Initialize model
|
58 |
+
print("\nInitializing model...")
|
59 |
+
model = PyLaia(model_name)
|
60 |
+
print(f"Device: {model.device}")
|
61 |
+
|
62 |
+
# Create test images
|
63 |
+
print(f"\nCreating {num_images} test images...")
|
64 |
+
test_images = create_test_images(NORMAL_IMAGE_PATH, num_images)
|
65 |
+
|
66 |
+
# Test different chunk sizes
|
67 |
+
chunk_sizes = [1, 5, 10, 20, 50, 100]
|
68 |
+
results = {}
|
69 |
+
|
70 |
+
print(f"\n{'='*80}")
|
71 |
+
print("Running benchmarks...")
|
72 |
+
print(f"{'='*80}")
|
73 |
+
|
74 |
+
for chunk_size in chunk_sizes:
|
75 |
+
if chunk_size > num_images:
|
76 |
+
continue
|
77 |
+
|
78 |
+
print(f"\nTesting chunk_size={chunk_size}...")
|
79 |
+
|
80 |
+
# Warm-up run
|
81 |
+
print(" Warm-up run...")
|
82 |
+
_ = model._predict(test_images[:min(5, num_images)], chunk_size=chunk_size)
|
83 |
+
|
84 |
+
# Actual timing
|
85 |
+
print(" Timing run...")
|
86 |
+
start_time = time.time()
|
87 |
+
|
88 |
+
predictions = model._predict(
|
89 |
+
test_images,
|
90 |
+
batch_size=8,
|
91 |
+
temperature=1.0,
|
92 |
+
chunk_size=chunk_size
|
93 |
+
)
|
94 |
+
|
95 |
+
end_time = time.time()
|
96 |
+
elapsed_time = end_time - start_time
|
97 |
+
|
98 |
+
results[chunk_size] = {
|
99 |
+
'time': elapsed_time,
|
100 |
+
'images_per_second': num_images / elapsed_time,
|
101 |
+
'ms_per_image': (elapsed_time / num_images) * 1000,
|
102 |
+
'predictions': predictions
|
103 |
+
}
|
104 |
+
|
105 |
+
print(f" ✓ Completed in {elapsed_time:.2f}s")
|
106 |
+
print(f" Speed: {results[chunk_size]['images_per_second']:.2f} images/second")
|
107 |
+
print(f" Time per image: {results[chunk_size]['ms_per_image']:.2f}ms")
|
108 |
+
|
109 |
+
# Print summary table
|
110 |
+
print(f"\n{'='*80}")
|
111 |
+
print("PERFORMANCE SUMMARY")
|
112 |
+
print(f"{'='*80}")
|
113 |
+
print(f"{'Chunk Size':>12} | {'Total Time':>10} | {'Images/sec':>12} | {'ms/image':>10} | {'Speedup':>10}")
|
114 |
+
print(f"{'-'*12}-+-{'-'*10}-+-{'-'*12}-+-{'-'*10}-+-{'-'*10}")
|
115 |
+
|
116 |
+
baseline_time = results[1]['time'] if 1 in results else list(results.values())[0]['time']
|
117 |
+
|
118 |
+
for chunk_size in sorted(results.keys()):
|
119 |
+
data = results[chunk_size]
|
120 |
+
speedup = baseline_time / data['time']
|
121 |
+
print(f"{chunk_size:>12} | {data['time']:>10.2f}s | {data['images_per_second']:>12.2f} | "
|
122 |
+
f"{data['ms_per_image']:>10.2f} | {speedup:>10.2f}x")
|
123 |
+
|
124 |
+
# Verify consistency
|
125 |
+
print(f"\n{'='*80}")
|
126 |
+
print("Verifying result consistency...")
|
127 |
+
baseline_texts = [r.texts[0] for r in results[1]['predictions']] if 1 in results else None
|
128 |
+
|
129 |
+
all_consistent = True
|
130 |
+
for chunk_size, data in results.items():
|
131 |
+
if baseline_texts and chunk_size != 1:
|
132 |
+
chunk_texts = [r.texts[0] for r in data['predictions']]
|
133 |
+
if chunk_texts != baseline_texts:
|
134 |
+
print(f" ✗ Results mismatch for chunk_size={chunk_size}")
|
135 |
+
all_consistent = False
|
136 |
+
|
137 |
+
if all_consistent:
|
138 |
+
print(" ✓ All chunk sizes produced identical results")
|
139 |
+
|
140 |
+
# Find optimal chunk size
|
141 |
+
optimal_chunk = min(results.keys(), key=lambda k: results[k]['time'])
|
142 |
+
optimal_speedup = baseline_time / results[optimal_chunk]['time']
|
143 |
+
|
144 |
+
print(f"\n{'='*80}")
|
145 |
+
print(f"🚀 OPTIMAL CONFIGURATION")
|
146 |
+
print(f"{'='*80}")
|
147 |
+
print(f"Chunk size: {optimal_chunk}")
|
148 |
+
print(f"Processing time: {results[optimal_chunk]['time']:.2f}s")
|
149 |
+
print(f"Speed: {results[optimal_chunk]['images_per_second']:.2f} images/second")
|
150 |
+
print(f"Speedup: {optimal_speedup:.2f}x")
|
151 |
+
|
152 |
+
return results
|
153 |
+
|
154 |
+
|
155 |
+
def quick_test():
|
156 |
+
"""Quick test with 20 images for faster results."""
|
157 |
+
print("\n" + "="*80)
|
158 |
+
print("QUICK TEST (20 images)")
|
159 |
+
print("="*80)
|
160 |
+
|
161 |
+
benchmark_pylaia(num_images=20)
|
162 |
+
|
163 |
+
|
164 |
+
def full_test():
|
165 |
+
"""Full test with 100 images."""
|
166 |
+
print("\n" + "="*80)
|
167 |
+
print("FULL TEST (100 images)")
|
168 |
+
print("="*80)
|
169 |
+
|
170 |
+
benchmark_pylaia(num_images=100)
|
171 |
+
|
172 |
+
|
173 |
+
if __name__ == "__main__":
|
174 |
+
# Run quick test first
|
175 |
+
quick_test()
|
176 |
+
|
177 |
+
# Uncomment to run full test
|
178 |
+
# full_test()
|
179 |
+
|
180 |
+
# Or run with custom parameters
|
181 |
+
# benchmark_pylaia(model_name="Teklia/pylaia-belfort", num_images=50)
|