-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
59 lines (45 loc) · 1.53 KB
/
Copy pathutils.py
File metadata and controls
59 lines (45 loc) · 1.53 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
import os
import cv2
import yaml
import glob
import torch
import os.path as osp
import torchvision.transforms as transforms
from torch.utils.data import Dataset
from PIL import Image
def load_yaml(config_file):
config_obj = {}
if osp.exists(config_file):
with open(config_file, 'r') as f:
config_obj = yaml.safe_load(f)
return config_obj
class CompCarsDataset(Dataset):
def __init__(self, root, transforms_, mode='train'):
self.transform = transforms.Compose(transforms_)
self.files = glob.glob(f'{root}/cars_test/*.jpg') + glob.glob(f'{root}/cars_train/*.jpg')
if self.__len__() == 0:
raise FileNotFoundError('Dataset loading error')
def __getitem__(self, index):
filepath = self.files[index % len(self.files)]
img = self.transform(Image.open(filepath).convert('RGB'))
# filename = filepath.split('/')[-1]
return img
def __len__(self):
return len(self.files)
def create_dirs(model_dir):
if not osp.exists(model_dir):
os.makedirs(model_dir)
def save_model(filename, epoch, gen, disc=None):
dd = {
'gen': gen.state_dict(),
'epoch': epoch
}
if disc is not None:
dd['disc'] = disc.state_dict()
torch.save(dd, filename)
def load(filename, gen, map_location=None):
if map_location is None:
map_location = torch.device('cpu')
dd = torch.load(filename, map_location=map_location)
gen.load_state_dict(dd['gen'])
print(f'last trained epoch was {dd["epoch"]}')