75 lines
2.4 KiB
Python
75 lines
2.4 KiB
Python
"""
|
|
Pseudo-MOS Learning Script: No-Reference IQA Regression from Fused Full-Reference Scores
|
|
|
|
This script trains a no-reference IQA regressor using pseudo-MOS (fusion_rf scores)
|
|
generated by a full-reference fusion model.
|
|
|
|
Steps:
|
|
1. Load distorted image paths and corresponding fusion scores.
|
|
2. Extract deep features using selected encoder.
|
|
3. Train regression models to predict fusion_rf scores.
|
|
4. Evaluate using cross-validation.
|
|
5. Save predictions and comparison plots.
|
|
"""
|
|
|
|
import os
|
|
import pandas as pd
|
|
|
|
from nr_fusion.features import extract_features
|
|
from nr_fusion.models import train_regressor
|
|
from nr_fusion.evaluation import evaluate_and_plot, compute_correlation_scores
|
|
|
|
def main(encoder: str):
|
|
"""Run no-reference IQA regression using selected encoder."""
|
|
# Paths
|
|
image_dir = "images/distorted"
|
|
scores_csv = "results/ffr_metric/fusion_scores_all.csv"
|
|
output_dir = f"results/nr_metric/{encoder}"
|
|
os.makedirs(output_dir, exist_ok=True)
|
|
|
|
pred_csv = os.path.join(output_dir, "nr_predictions.csv")
|
|
|
|
# Load fusion scores
|
|
df = pd.read_csv(scores_csv)
|
|
if "fusion_rf" not in df.columns:
|
|
raise ValueError("Column 'fusion_rf' not found in CSV. Run fusion first.")
|
|
|
|
# Check if predictions already exist
|
|
if os.path.exists(pred_csv):
|
|
print("Predictions already exist. Loading...")
|
|
pred_df = pd.read_csv(pred_csv)
|
|
y = pred_df["pseudo_mos"].values
|
|
y_pred = pred_df["predicted"].values
|
|
else:
|
|
# Feature extraction
|
|
x, y, filenames = extract_features(df, image_dir)
|
|
|
|
# Train + cross-validate + predict
|
|
y_pred, _, _ = train_regressor(x, y)
|
|
|
|
# Save predictions
|
|
pred_df = pd.DataFrame({
|
|
"file_name": filenames,
|
|
"pseudo_mos": y,
|
|
"predicted": y_pred
|
|
})
|
|
pred_df.to_csv(os.path.join(output_dir, "nr_predictions.csv"), index=False)
|
|
|
|
# Plot results
|
|
evaluate_and_plot(y, y_pred, output_dir, show_trend=True)
|
|
|
|
correlation_scores = compute_correlation_scores(y, y_pred)
|
|
print("Correlation scores:", correlation_scores)
|
|
with open(os.path.join(output_dir, "correlation_scores.txt"), "w", encoding="utf-8") as f:
|
|
for k, v in correlation_scores.items():
|
|
f.write(f"{k}: {v:.4f}\n")
|
|
|
|
print(f"Pseudo-MOS training with {encoder} completed. Predictions saved.")
|
|
|
|
if __name__ == "__main__":
|
|
|
|
# "resnet"
|
|
# "facenet"
|
|
|
|
main(encoder="resnet")
|