[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
This commit is contained in:
parent
7b188294e6
commit
fbb98c5c5c
47 changed files with 2646 additions and 2646 deletions
|
|
@ -41,14 +41,14 @@ class OCRModelEvaluator:
|
|||
Evaluate a model on an OCR dataset.
|
||||
"""
|
||||
# Create output directory if it doesn't exist
|
||||
os.makedirs(output_dir, exist_ok = True)
|
||||
os.makedirs(output_dir, exist_ok=True)
|
||||
|
||||
# Initialize results storage
|
||||
results = []
|
||||
|
||||
# Process each sample in the dataset
|
||||
for i, sample in enumerate(
|
||||
tqdm(dataset, desc = "Evaluating OCR performance", disable = not verbose)
|
||||
tqdm(dataset, desc="Evaluating OCR performance", disable=not verbose)
|
||||
):
|
||||
try:
|
||||
# Extract components from sample
|
||||
|
|
@ -187,7 +187,7 @@ class OCRModelEvaluator:
|
|||
|
||||
# Preparation for inference using Qwen's specific processing
|
||||
text = processor.apply_chat_template(
|
||||
input_messages, tokenize = False, add_generation_prompt = True
|
||||
input_messages, tokenize=False, add_generation_prompt=True
|
||||
)
|
||||
|
||||
# Process vision info (images/videos) from messages
|
||||
|
|
@ -195,11 +195,11 @@ class OCRModelEvaluator:
|
|||
|
||||
# Create model inputs
|
||||
inputs = processor(
|
||||
text = [text],
|
||||
images = image_inputs,
|
||||
videos = video_inputs,
|
||||
padding = True,
|
||||
return_tensors = "pt",
|
||||
text=[text],
|
||||
images=image_inputs,
|
||||
videos=video_inputs,
|
||||
padding=True,
|
||||
return_tensors="pt",
|
||||
)
|
||||
inputs = inputs.to(model.device)
|
||||
|
||||
|
|
@ -207,10 +207,10 @@ class OCRModelEvaluator:
|
|||
with torch.no_grad():
|
||||
generated_ids = model.generate(
|
||||
**inputs,
|
||||
max_new_tokens = max_new_tokens,
|
||||
temperature = temperature,
|
||||
min_p = min_p,
|
||||
use_cache = True,
|
||||
max_new_tokens=max_new_tokens,
|
||||
temperature=temperature,
|
||||
min_p=min_p,
|
||||
use_cache=True,
|
||||
)
|
||||
|
||||
# Extract only the generated part (not the input)
|
||||
|
|
@ -222,8 +222,8 @@ class OCRModelEvaluator:
|
|||
# Decode the generated text
|
||||
generated_response = processor.batch_decode(
|
||||
generated_ids_trimmed,
|
||||
skip_special_tokens = True,
|
||||
clean_up_tokenization_spaces = False,
|
||||
skip_special_tokens=True,
|
||||
clean_up_tokenization_spaces=False,
|
||||
)[0]
|
||||
|
||||
return generated_response
|
||||
|
|
@ -240,7 +240,7 @@ class OCRModelEvaluator:
|
|||
):
|
||||
"""Save individual sample result to file."""
|
||||
output_file = os.path.join(output_dir, f"sample_{sample_idx}.txt")
|
||||
with open(output_file, "w", encoding = "utf-8") as f:
|
||||
with open(output_file, "w", encoding="utf-8") as f:
|
||||
f.write(f"Sample {sample_idx}\n")
|
||||
f.write(f"Question: {question}\n\n")
|
||||
f.write(f"Model output:\n{generated_response.strip()}\n\n")
|
||||
|
|
@ -268,7 +268,7 @@ class OCRModelEvaluator:
|
|||
f.write(f"Average CER: {avg_cer:.4f}\n")
|
||||
|
||||
# Save detailed results
|
||||
df.to_csv(os.path.join(output_dir, "detailed_results.csv"), index = False)
|
||||
df.to_csv(os.path.join(output_dir, "detailed_results.csv"), index=False)
|
||||
|
||||
if verbose:
|
||||
print("\nResults Summary:")
|
||||
|
|
@ -310,12 +310,12 @@ class OCRModelEvaluator:
|
|||
|
||||
# Display the comparison table
|
||||
print("\nComparison Table (sorted by WER):")
|
||||
print(comparison_df.to_string(index = False))
|
||||
print(comparison_df.to_string(index=False))
|
||||
|
||||
# Save the comparison table
|
||||
if save_csv:
|
||||
comparison_file = "model_comparison_results.csv"
|
||||
comparison_df.to_csv(comparison_file, index = False)
|
||||
comparison_df.to_csv(comparison_file, index=False)
|
||||
print(f"\nComparison table saved to {comparison_file}")
|
||||
|
||||
# Generate a bar chart visualization
|
||||
|
|
@ -326,23 +326,23 @@ class OCRModelEvaluator:
|
|||
|
||||
def _create_comparison_plot(self, comparison_df: pd.DataFrame):
|
||||
"""Create and save comparison plot."""
|
||||
plt.figure(figsize = (12, 6))
|
||||
plt.figure(figsize=(12, 6))
|
||||
|
||||
# Plot WER
|
||||
plt.subplot(1, 2, 1)
|
||||
plt.bar(comparison_df["Model"], comparison_df["WER"], color = "skyblue")
|
||||
plt.bar(comparison_df["Model"], comparison_df["WER"], color="skyblue")
|
||||
plt.title("Word Error Rate Comparison")
|
||||
plt.ylabel("WER (lower is better)")
|
||||
plt.ylim(bottom = 0)
|
||||
plt.xticks(rotation = 45, ha = "right")
|
||||
plt.ylim(bottom=0)
|
||||
plt.xticks(rotation=45, ha="right")
|
||||
|
||||
# Plot CER
|
||||
plt.subplot(1, 2, 2)
|
||||
plt.bar(comparison_df["Model"], comparison_df["CER"], color = "lightgreen")
|
||||
plt.bar(comparison_df["Model"], comparison_df["CER"], color="lightgreen")
|
||||
plt.title("Character Error Rate Comparison")
|
||||
plt.ylabel("CER (lower is better)")
|
||||
plt.ylim(bottom = 0)
|
||||
plt.xticks(rotation = 45, ha = "right")
|
||||
plt.ylim(bottom=0)
|
||||
plt.xticks(rotation=45, ha="right")
|
||||
|
||||
plt.tight_layout()
|
||||
plt.savefig("ocr_model_comparison.png")
|
||||
|
|
@ -360,7 +360,7 @@ class OCRModelEvaluator:
|
|||
|
||||
|
||||
def evaluate_ocr_model(
|
||||
model, processor, dataset, output_dir = "ocr_evaluation_results", **kwargs
|
||||
model, processor, dataset, output_dir="ocr_evaluation_results", **kwargs
|
||||
):
|
||||
"""
|
||||
Convenience function that maintains backward compatibility with the original function.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue