Dean commited on
Commit
5ad6755
1 Parent(s): 3410172

Changes to almost all of the pipeline:

Browse files

- Added a different type of preprocessing which multiplies the data values by a constant – giving a human readable label, which is reversible for the calculation of metrics.
- Created an eval and metric calculation stage that logs test metrics
- Added eval stage to dvc.yaml and added metrics
- Added logs folder with logged metrics and params

TODO:
- Add params.yaml and const.py with relevant parameter and constants to reduce room for human error
- Train the model for longer and see if results improve.

dvc.yaml CHANGED
@@ -15,6 +15,9 @@ stages:
15
  - src/data/processed/train
16
  outs:
17
  - src/models/
 
 
 
18
  eval:
19
  cmd: python3 src/code/eval.py src/data/processed/test
20
  deps:
@@ -22,4 +25,7 @@ stages:
22
  - src/models/model.pth
23
  - src/data/processed/test
24
  outs:
25
- - src/eval/
 
 
 
 
15
  - src/data/processed/train
16
  outs:
17
  - src/models/
18
+ metrics:
19
+ - logs/train_metrics.csv:
20
+ cache: false
21
  eval:
22
  cmd: python3 src/code/eval.py src/data/processed/test
23
  deps:
 
25
  - src/models/model.pth
26
  - src/data/processed/test
27
  outs:
28
+ - src/eval/
29
+ metrics:
30
+ - logs/test_metrics.csv:
31
+ cache: false
src/code/eval.py CHANGED
@@ -1,36 +1,8 @@
1
  import sys
2
- from fastai.vision.all import unet_learner, Path, resnet34, MSELossFlat, get_files, L
3
- import torch
4
  from src.code.custom_data_loading import create_data
5
- from dagshub.fastai import DAGsHubLogger
6
-
7
-
8
- def compute_errors(targ, pred):
9
- thresh = torch.max((targ / pred), (pred / targ)).numpy()
10
- a1 = (thresh < 1.25).mean()
11
- a2 = (thresh < 1.25 ** 2).mean()
12
- a3 = (thresh < 1.25 ** 3).mean()
13
-
14
- abs_rel = (torch.abs(targ - pred) / targ).mean().item()
15
- sq_rel = torch.mean(((targ - pred).pow(2)) / targ).item()
16
-
17
- rmse = torch.sqrt((targ - pred).pow(2).mean()).item()
18
-
19
- rmse_log = torch.sqrt((torch.log(1 + targ) - torch.log(1 + pred)).pow(2).mean()).item()
20
-
21
- err = torch.log(1 + pred) - torch.log(1 + targ)
22
- silog = torch.sqrt(torch.mean(err.pow(2)) - torch.mean(err).pow(2)).item() * 100
23
-
24
- log_10 = (torch.abs(torch.log10(1 + targ) - torch.log10(1 + pred))).mean().item()
25
- return dict(a1=a1,
26
- a2=a2,
27
- a3=a3,
28
- abs_rel=abs_rel,
29
- sq_rel=sq_rel,
30
- rmse=rmse,
31
- rmse_log=rmse_log,
32
- silog=silog,
33
- log_10=log_10)
34
 
35
 
36
  if __name__ == "__main__":
@@ -51,5 +23,21 @@ if __name__ == "__main__":
51
  path='src/',
52
  model_dir='models')
53
  learner = learner.load('model')
54
- predictions, targets = learner.get_preds(dl=test_dl)
55
- print(compute_errors(targets, predictions))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import sys
2
+ from fastai.vision.all import unet_learner, Path, resnet34, MSELossFlat, get_files, L, tuplify
 
3
  from src.code.custom_data_loading import create_data
4
+ from src.code.eval_metric_calculation import compute_eval_metrics
5
+ from dagshub import dagshub_logger
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
 
8
  if __name__ == "__main__":
 
23
  path='src/',
24
  model_dir='models')
25
  learner = learner.load('model')
26
+ inputs, predictions, targets, decoded = learner.get_preds(dl=test_dl,
27
+ with_input=True,
28
+ with_decoded=True)
29
+ # FastAI magic to retrieve image values
30
+ inputs = (inputs,)
31
+ decoded_predictions = learner.dls.decode(inputs + tuplify(decoded))[1]
32
+ decoded_targets = learner.dls.decode(inputs + tuplify(targets))[1]
33
+
34
+ metrics = compute_eval_metrics(decoded_targets.numpy(), decoded_predictions.numpy())
35
+
36
+ with dagshub_logger(
37
+ metrics_path="logs/test_metrics.csv",
38
+ should_log_hparams=False
39
+ ) as logger:
40
+ # Metric logging
41
+ logger.log_metrics(metrics)
42
+
43
+ print("Evaluation Done!")
src/code/eval_metric_calculation.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+
3
+
4
+ def compute_errors(target, prediction):
5
+ thresh = np.maximum((target / prediction), (prediction / target))
6
+ a1 = (thresh < 1.25).mean()
7
+ a2 = (thresh < 1.25 ** 2).mean()
8
+ a3 = (thresh < 1.25 ** 3).mean()
9
+
10
+ abs_rel = np.mean(np.abs(target - prediction) / target)
11
+ sq_rel = np.mean(((target - prediction) ** 2) / target)
12
+
13
+ rmse = (target - prediction) ** 2
14
+ rmse = np.sqrt(rmse.mean())
15
+
16
+ rmse_log = (np.log(target) - np.log(prediction)) ** 2
17
+ rmse_log = np.sqrt(rmse_log.mean())
18
+
19
+ err = np.log(prediction) - np.log(target)
20
+ silog = np.sqrt(np.mean(err ** 2) - np.mean(err) ** 2) * 100
21
+
22
+ log_10 = (np.abs(np.log10(target) - np.log10(prediction))).mean()
23
+
24
+ return a1, a2, a3, abs_rel, sq_rel, rmse, rmse_log, silog, log_10
25
+
26
+
27
+ def compute_eval_metrics(targets, predictions):
28
+ targets = targets / 25.0
29
+ predictions = predictions / 25.0
30
+
31
+ min_depth_eval = 1e-3
32
+ max_depth_eval = 10
33
+
34
+ num_samples = predictions.shape[0]
35
+
36
+ a1 = np.zeros(num_samples, np.float32)
37
+ a2 = np.zeros(num_samples, np.float32)
38
+ a3 = np.zeros(num_samples, np.float32)
39
+ abs_rel = np.zeros(num_samples, np.float32)
40
+ sq_rel = np.zeros(num_samples, np.float32)
41
+ rmse = np.zeros(num_samples, np.float32)
42
+ rmse_log = np.zeros(num_samples, np.float32)
43
+ silog = np.zeros(num_samples, np.float32)
44
+ log10 = np.zeros(num_samples, np.float32)
45
+
46
+ for i in range(num_samples):
47
+ target_depth = targets[i]
48
+ prediction_depth = predictions[i]
49
+
50
+ prediction_depth[prediction_depth < min_depth_eval] = min_depth_eval
51
+ prediction_depth[prediction_depth > max_depth_eval] = max_depth_eval
52
+ prediction_depth[np.isinf(prediction_depth)] = max_depth_eval
53
+
54
+ target_depth[np.isinf(target_depth)] = 0
55
+ target_depth[np.isnan(target_depth)] = 0
56
+
57
+ valid_mask = np.logical_and(target_depth > min_depth_eval, target_depth < max_depth_eval)
58
+
59
+ a1[i], a2[i], a3[i], abs_rel[i], sq_rel[i], rmse[i], rmse_log[i], silog[i], log10[i] = \
60
+ compute_errors(target_depth[valid_mask], prediction_depth[valid_mask])
61
+
62
+ print("{:>7}, {:>7}, {:>7}, {:>7}, {:>7}, {:>7}, {:>7}, {:>7}, {:>7}".format(
63
+ 'd1', 'd2', 'd3', 'AbsRel', 'SqRel', 'RMSE', 'RMSElog', 'SILog', 'log10'))
64
+ print("{:7.3f}, {:7.3f}, {:7.3f}, {:7.3f}, {:7.3f}, {:7.3f}, {:7.3f}, {:7.3f}, {:7.3f}".format(
65
+ a1.mean(), a2.mean(), a3.mean(),
66
+ abs_rel.mean(), sq_rel.mean(), rmse.mean(), rmse_log.mean(), silog.mean(), log10.mean()))
67
+
68
+ return dict(a1=a1.mean(), a2=a2.mean(), a3=a3.mean(),
69
+ abs_rel=abs_rel.mean(), sq_rel=sq_rel.mean(),
70
+ rmse=rmse.mean(), rmse_log=rmse_log.mean(),
71
+ log10=log10.mean(), silog=silog.mean())
src/code/make_dataset.py CHANGED
@@ -65,8 +65,9 @@ def convert_image(index, depth_map, img, output_folder):
65
  """
66
 
67
  # Normalize the depth image
68
- normalized_depth = cv2.normalize(depth_map, None, 0, 255, cv2.NORM_MINMAX)
69
- cv2.imwrite("%s/%05d_depth.png" % (output_folder, index), normalized_depth)
 
70
 
71
  # Adding black frame to original image
72
  img = img[:, :, ::-1] # Flipping the image from RGB to BGR for opencv
 
65
  """
66
 
67
  # Normalize the depth image
68
+ # normalized_depth = cv2.normalize(depth_map, None, 0, 255, cv2.NORM_MINMAX)
69
+ img_depth = depth_map * 25.0
70
+ cv2.imwrite("%s/%05d_depth.png" % (output_folder, index), img_depth)
71
 
72
  # Adding black frame to original image
73
  img = img[:, :, ::-1] # Flipping the image from RGB to BGR for opencv
src/code/training.py CHANGED
@@ -25,8 +25,8 @@ if __name__ == "__main__":
25
  path='src/',
26
  model_dir='models',
27
  cbs=DAGsHubLogger(
28
- metrics_path="train_metrics.csv",
29
- hparams_path="train_params.yml"
30
  ))
31
 
32
  print("Training model...")
 
25
  path='src/',
26
  model_dir='models',
27
  cbs=DAGsHubLogger(
28
+ metrics_path="logs/train_metrics.csv",
29
+ hparams_path="logs/train_params.yml"
30
  ))
31
 
32
  print("Training model...")