-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
67 lines (56 loc) · 1.58 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# Importing Libraries
import numpy as np
import matplotlib.pyplot as plt
import os, json
import torch, torchvision
from pytorch_lightning.callbacks import TQDMProgressBar
import pytorch_lightning as pl
# Progress bar
class LitProgressBar(TQDMProgressBar):
def on_validation_epoch_end(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule") -> None:
print ()
return super().on_validation_epoch_end(trainer, pl_module)
def Plot_Images(
Dataset,
save_path="Plots"
):
"""
Plots video splits as images from dataset.
Args:
Dataset (torch.utils.data.Dataset): Torch Dataset.
"""
idx = np.random.randint(Dataset.__len__())
V1, V2, I = Dataset.__getitem__(idx)
plt.figure(figsize=(12, 12))
for i in range(4):
plt.imshow(V1[i].permute(1, 2, 0))
plt.axis("off")
plt.savefig(save_path+str(i)+".png")
plt.imshow(I[0].permute(1, 2, 0))
plt.savefig(save_path+str(4)+".png")
for i in range(4):
plt.imshow(V2[i].permute(1, 2, 0))
plt.axis("off")
plt.savefig(save_path+str(5+i)+".png")
def Load_Model(
Model: torch.nn.Module,
Path
):
"""
Loading weights to a PyTorch Model
Args:
Model (torch.nn.Module): PyTorch Model
Path (string): path to best checkpoint save by PyTorch Lightning trainer.
Returns:
Model (torch.nn.Module): PyTorch Model with trained weights.
"""
checkpoint = torch.load(Path)
# Model Weights
model_weights = checkpoint["state_dict"]
for key in list(model_weights):
if "lossfn" in key:
model_weights.pop(key)
else:
model_weights[key.replace("model.", "")] = model_weights.pop(key)
Model.load_state_dict(model_weights)
return Model