Yusuf Rahman (k22040245) commited on
Commit
f3ee60d
·
unverified ·
2 Parent(s): d4a4907 3f67469

Merge pull request #8 from K23064919/test-model

Browse files
dataPrep/data_preparation.py CHANGED
@@ -45,8 +45,9 @@ if torch.cuda.is_available():
45
 
46
 
47
  # ----- ClearML Setup -----
 
48
  task = Task.init(
49
- project_name='Small Group Project',
50
  task_name='Data Preparation',
51
  task_type=Task.TaskTypes.data_processing
52
  )
 
45
 
46
 
47
  # ----- ClearML Setup -----
48
+ project_name = "Small Group Project"
49
  task = Task.init(
50
+ project_name=f'{project_name}/Data Preparation',
51
  task_name='Data Preparation',
52
  task_type=Task.TaskTypes.data_processing
53
  )
testingModel/helpers/evaluation.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from torch.nn import CrossEntropyLoss
3
+
4
+
5
+ """
6
+ Evaluates a trained model on a dataloader that returns batches like:
7
+ batch["image"] -> Tensor [B, 3, 256, 256]
8
+ batch["label"] -> Tensor [B]
9
+
10
+ Returns dict:
11
+ { "accuracy": float, "loss": float }
12
+ """
13
+ def make_predictions(model, dataloader, device):
14
+
15
+ model.eval()
16
+ criterion = CrossEntropyLoss()
17
+
18
+ total_loss = 0
19
+ total_correct = 0
20
+ total_samples = 0
21
+
22
+ with torch.no_grad():
23
+ for batch in dataloader:
24
+
25
+ # Move tensors to device
26
+ images = batch["image"].to(device)
27
+ labels = batch["label"].to(device).long()
28
+
29
+ # Forward pass
30
+ outputs = model(images)
31
+ loss = criterion(outputs, labels)
32
+
33
+ total_loss += loss.item() * images.size(0)
34
+ total_correct += (outputs.argmax(dim=1) == labels).sum().item()
35
+ total_samples += labels.size(0)
36
+
37
+ accuracy = total_correct / total_samples
38
+ avg_loss = total_loss / total_samples
39
+
40
+ return {
41
+ "accuracy": accuracy,
42
+ "loss": avg_loss,
43
+ }
testingModel/run_testing.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from clearml import Task
2
+ from dataPrep.helpers.clearml_data import extract_latest_data_task
3
+
4
+ import torch
5
+ from models.modelOne import modelOne
6
+ from testingModel.helpers.evaluation import make_predictions
7
+
8
+
9
+ # -------------- Load Data --------------
10
+ project_name = "Small Group Project"
11
+ subset_loaders, full_loaders, data_prep_metadata = extract_latest_data_task(project_name=project_name)
12
+
13
+
14
+ # -------- ClearML Testing Task Setup --------
15
+ testing_task = Task.init(
16
+ project_name=f"{project_name}/Model Testing",
17
+ task_name="Model Testing",
18
+ task_type=Task.TaskTypes.testing,
19
+ reuse_last_task_id=False,
20
+ )
21
+
22
+ # Reference the data prep task used
23
+ testing_logger = testing_task.get_logger()
24
+ testing_task.connect(data_prep_metadata, name="data_prep_metadata_READONLY")
25
+
26
+ CLEARML_TRAINING_ID = "5bac154a885b4acbaa07d8588027bb27"
27
+
28
+ # Testing parameters - Modify these when experimenting
29
+ testing_config = {
30
+ "model_train_id": CLEARML_TRAINING_ID,
31
+ "num_classes": 39,
32
+ "model_path": "best_model.pt",
33
+ }
34
+ testing_task.connect(testing_config)
35
+
36
+ # Load the model weights from ClearML training task
37
+ training_task = Task.get_task(task_id=testing_config["model_train_id"])
38
+ model_artifact = training_task.artifacts.get("best_model")
39
+ model_path = model_artifact.get_local_copy()
40
+
41
+ # Reference training metadata
42
+ training_hyperparams = training_task.get_parameters_as_dict()
43
+ testing_task.connect(training_hyperparams['General'], name="training_metadata_READONLY")
44
+
45
+
46
+ # -------- Rebuild the ML model --------
47
+ model = modelOne()
48
+ state_dict = torch.load(model_path, map_location="cpu") # Load to CPU first
49
+ model.load_state_dict(state_dict)
50
+ model.eval() # set dropout & batch norm layers to eval mode
51
+
52
+ # Move model to GPU if available
53
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
54
+ model.to(device)
55
+
56
+
57
+ # -------------------- Test model on test set --------------------
58
+ testing_logger.report_text("Starting evaluation on TEST SUBSET...\n")
59
+ test_subset = subset_loaders['test']
60
+
61
+ subset_results = make_predictions(model, test_subset, device)
62
+
63
+
64
+ # Accuracy & Loss logging
65
+ testing_logger.report_single_value(name="Test Subset Accuracy", value=subset_results["accuracy"])
66
+ testing_logger.report_single_value(name="Test Subset Loss", value=subset_results["loss"])
67
+
68
+
69
+ # --------- Complete -----------------
70
+ print("\n------ Testing Complete ------")
71
+ testing_logger.report_text(
72
+ f"TEST SUBSET RESULTS:\n"
73
+ f"Loss: {subset_results['loss']:.4f}\n"
74
+ f"Accuracy: {subset_results['accuracy']:.4f}\n"
75
+ )
76
+ testing_task.close()
trainingModel/{Training.py → helpers/Training.py} RENAMED
File without changes
trainingModel/run_training.py CHANGED
@@ -4,7 +4,7 @@ from dataPrep.helpers.clearml_data import extract_latest_data_task
4
 
5
  import torch
6
  from models.modelOne import modelOne
7
- from trainingModel.Training import train_model
8
 
9
 
10
  # -------------- Load Data --------------
@@ -14,7 +14,7 @@ subset_loaders, full_loaders, data_prep_metadata = extract_latest_data_task(proj
14
 
15
  # -------- ClearML Training Task Setup --------
16
  training_task = Task.init(
17
- project_name="Small Group Project",
18
  task_name="Model Training",
19
  reuse_last_task_id=False,
20
  )
@@ -26,7 +26,7 @@ training_task.connect(data_prep_metadata, name="data_prep_metadata_READONLY")
26
  # Training parameters - Modify these to experiment
27
  training_config = {
28
  "num_classes": 39,
29
- "n_epochs": 10,
30
  "learning_rate": 1e-3,
31
  "optimizer": "adam",
32
  "save_path": "best_model.pt",
@@ -37,7 +37,12 @@ training_task.connect(training_config)
37
  # -------- Build the ML model --------
38
  model = modelOne(noOfClasses=training_config["num_classes"])
39
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 
40
 
 
 
 
 
41
 
42
  # ------- Train the model (on subset for now) -------
43
 
 
4
 
5
  import torch
6
  from models.modelOne import modelOne
7
+ from trainingModel.helpers.Training import train_model
8
 
9
 
10
  # -------------- Load Data --------------
 
14
 
15
  # -------- ClearML Training Task Setup --------
16
  training_task = Task.init(
17
+ project_name=f"{project_name}/Model Training",
18
  task_name="Model Training",
19
  reuse_last_task_id=False,
20
  )
 
26
  # Training parameters - Modify these to experiment
27
  training_config = {
28
  "num_classes": 39,
29
+ "n_epochs": 1,
30
  "learning_rate": 1e-3,
31
  "optimizer": "adam",
32
  "save_path": "best_model.pt",
 
37
  # -------- Build the ML model --------
38
  model = modelOne(noOfClasses=training_config["num_classes"])
39
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
40
+ model.to(device)
41
 
42
+ # Print device info
43
+ print(f"\n**Using device: {device}**\n")
44
+ if device.type == 'cuda':
45
+ print(f"GPU Name: {torch.cuda.get_device_name(0)}")
46
 
47
  # ------- Train the model (on subset for now) -------
48