forked from clu0/unet.cu
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
45 lines (39 loc) · 1.41 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
import os
from typing import Dict, Any, List
import torch
import argparse
def write(tensor: torch.Tensor, handle):
if tensor.is_cuda:
handle.write(tensor.cpu().detach().numpy().astype("float32").tobytes())
else:
handle.write(tensor.detach().numpy().astype("float32").tobytes())
def str2bool(v):
"""
https://stackoverflow.com/questions/15008758/parsing-boolean-values-with-argparse
"""
if isinstance(v, bool):
return v
if v.lower() in ("yes", "true", "t", "y", "1"):
return True
elif v.lower() in ("no", "false", "f", "n", "0"):
return False
else:
raise argparse.ArgumentTypeError("boolean value expected")
def add_dict_to_argparser(parser, default_dict: Dict[str, Any]):
for k, v in default_dict.items():
v_type = type(v)
if v is None:
v_type = str
elif isinstance(v, bool):
v_type = str2bool
parser.add_argument(f"--{k}", default=v, type=v_type)
def list_image_files_recursive(data_dir: str) -> List[str]:
results = []
for entry in sorted(os.listdir(data_dir)):
full_path = os.path.join(data_dir, entry)
ext = entry.split('.')[-1]
if "." in entry and ext.lower() in ["jpg", "jpeg", "png", "gif"]:
results.append(full_path)
elif os.path.isdir(full_path):
results.extend(list_image_files_recursive(full_path))
return results