MCPcopy Create free account
hub / github.com/Modulus-Labs/RockyBot / eval_model

Function eval_model

pytorch-model/classification_eval.py:38–102  ·  view source on GitHub ↗

Evaluates model over validation set.

(model, val_dataloader, criterion, args)

Source from the content-addressed store, hash-verified

36
37
38def eval_model(model, val_dataloader, criterion, args):
39 """
40 Evaluates model over validation set.
41 """
42 print("\n" + ("-" * 30) + " Evaluating model! " + ("-" * 30))
43 total_loss = 0
44 total_correct = 0
45 total_examples = 0
46
47 avg_loss = None
48 avg_acc = None
49
50 model.eval()
51
52 # --- Set up confusion matrix ---
53 val_dataset = val_dataloader.dataset
54 confusion_matrix = torch.zeros(val_dataset.get_output_dim(), val_dataset.get_output_dim())
55
56 with torch.no_grad():
57 for idx, (x, y) in tqdm(enumerate(val_dataloader), total=len(val_dataloader)):
58
59 # --- Move to GPU ---
60 # x = x.cuda(constants.GPU, non_blocking=True)
61 # y = y.cuda(constants.GPU, non_blocking=True)
62
63 # --- Compute logits ---
64 logits = model(x)
65 loss = criterion(logits, y)
66
67 # --- Bookkeeping ---
68 _, predicted = torch.max(logits.data, 1)
69 total_correct += (predicted == y).sum().item()
70
71 total_loss += loss.item()
72 total_examples += y.shape[0]
73 # total_examples += y.shape[0] * y.shape[1]
74
75 avg_loss = total_loss / total_examples
76 avg_acc = total_correct / total_examples
77
78 # --- Updates confusion matrix ---
79 update_confusion_matrix(confusion_matrix, y, predicted)
80
81 # if idx % args.print_every_eval_minibatch == 0:
82 # tqdm.write(f"Val minibatch number: {idx} | Avg loss: {avg_loss} | Avg acc: {avg_acc}")
83
84 # --- Invert labels to idx ---
85 temp_idx_to_labels = [None] * len(val_dataset.labels_to_idx)
86 for k, v in val_dataset.labels_to_idx.items():
87 temp_idx_to_labels[v] = k
88 idx_to_labels = list()
89 for idx in range(len(temp_idx_to_labels) - 1):
90 bucket_low = temp_idx_to_labels[idx]
91 bucket_high = temp_idx_to_labels[idx + 1]
92 idx_to_labels.append(f"{bucket_low}_{bucket_high}")
93
94 # --- Generate confusion matrix image ---
95 viz_save_dir = constants.get_viz_dir(args.dataset, args.model_type, args.model_name)

Callers 1

mainFunction · 0.70

Calls 2

update_confusion_matrixFunction · 0.85
get_output_dimMethod · 0.45

Tested by

no test coverage detected