-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
67 lines (51 loc) · 1.92 KB
/
utils.py
File metadata and controls
67 lines (51 loc) · 1.92 KB
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
import six
import torch
from PIL import Image
from torchvision import models
from torch_hub import TorchHub
class Utils:
def setup_test():
x = torch.rand(5, 3)
print(x)
if torch.cuda.is_available():
print("CUDA is available!")
def show_available_models():
print(dir(models))
def load_pretrained_weights(model_path: str, weights_only=True):
if (not model_path):
print("model_path is None, (invalid input)")
return
return torch.load(model_path, weights_only= weights_only)
def get_image(file_name: str, mustShow = False) -> Image.ImageFile:
if (not file_name):
print("file_name is None, (invalid input)")
return
img = Image.open(file_name)
print(img)
if img.mode != 'RGB':
img = img.convert('RGB')
if mustShow: img.show()
return img
def compare_models(model_data, model: torch.nn.Module):
model_keys = model_data.keys()
netG_keys = model.state_dict().keys()
assert model_keys == netG_keys, "model data keys and model keys are different."
def get_device() -> str:
return 'cuda' if torch.cuda.is_available() else 'cpu'
def load_pickle(file_path: str):
if (not file_path):
print("file_path is None, (invalid input)")
return
try:
with open(file_path, 'rb') as f:
return six.moves.cPickle.load(f, encoding='latin1')
except Exception as e:
print(f"An error occurred while loading the data: {e}")
def get_model_from_hub(hub: TorchHub):
if (not hub):
print("hub is None, (invalid input)")
return
return hub.get_model()
def get_shape(tensor: torch.Tensor) -> torch.Size:
print(tensor.shape)
return tensor.shape