| import sys |
| import os |
| sys.path.append(os.path.abspath("src")) |
|
|
| import torch |
| import numpy as np |
| from sklearn.metrics import precision_recall_curve, f1_score |
| from dataset import create_dataloaders |
| from model import SEMViTAutoencoder |
| from train_eval import train_model, evaluate_anomaly |
| import json |
| from safetensors.torch import save_file |
|
|
| |
| DATA_DIR = "/content" |
| BATCH_SIZE = 8 |
| NUM_WORKERS = 2 |
| EPOCHS = 15 |
| DEVICE = torch.device("cuda") |
|
|
| def main(): |
| print(f"--- Running on {DEVICE} ---") |
| print("--- Loading Data ---") |
| |
| train_loader, test_loader = create_dataloaders(DATA_DIR, BATCH_SIZE, NUM_WORKERS) |
|
|
| print("--- Initializing Model ---") |
| model = SEMViTAutoencoder().to(DEVICE) |
|
|
| print("--- Starting Training (Normal Images Only) ---") |
| |
| train_model(model, train_loader, epochs=EPOCHS) |
|
|
| print("--- Evaluating & Finding Threshold ---") |
| model.eval() |
| errors, labels = evaluate_anomaly(model, test_loader) |
|
|
| target_labels = 1 - np.array(labels) |
|
|
| |
| precisions, recalls, thresholds = precision_recall_curve(target_labels, errors) |
| f1_scores = 2 * (precisions * recalls) / (precisions + recalls + 1e-8) |
|
|
| |
| best_idx = np.argmax(f1_scores) |
| best_threshold = thresholds[best_idx] |
|
|
| print(f"Optimal Anomaly Threshold: {best_threshold:.6f}") |
| print(f"Max F1-Score: {f1_scores[best_idx]:.4f}") |
|
|
| |
| save_file(model.state_dict(), "model.safetensors") |
|
|
| |
| config = { |
| "threshold": float(best_threshold), |
| "model_type": "ViT-Autoencoder", |
| "resolution": 512 |
| } |
|
|
| with open("config.json", "w") as f: |
| json.dump(config, f, indent=4) |
|
|
| print("✅ Pipeline Complete. Download 'model.safetensors' and 'config.json' from the file pane.") |
|
|
|
|
| if __name__ == "__main__": |
| main() |