Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
POSTECH-CVLab
GitHub Repository: POSTECH-CVLab/PyTorch-StudioGAN
Path: blob/master/src/utils/hdf5.py
809 views
1
"""
2
this code is borrowed from https://github.com/ajbrock/BigGAN-PyTorch
3
4
MIT License
5
6
Copyright (c) 2019 Andy Brock
7
Permission is hereby granted, free of charge, to any person obtaining a copy
8
of this software and associated documentation files (the "Software"), to deal
9
in the Software without restriction, including without limitation the rights
10
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
copies of the Software, and to permit persons to whom the Software is
12
furnished to do so, subject to the following conditions:
13
The above copyright notice and this permission notice shall be included in all
14
copies or substantial portions of the Software.
15
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
SOFTWARE.
22
"""
23
24
from os.path import dirname, exists, join, isfile
25
import os
26
27
from torch.utils.data import DataLoader
28
from tqdm import tqdm
29
import numpy as np
30
import h5py as h5
31
32
from data_util import Dataset_
33
34
35
def make_hdf5(name, img_size, crop_long_edge, resize_size, data_dir, resizer, DATA, RUN):
36
if resize_size is not None:
37
file_name = "{dataset_name}_{size}_{resizer}_train.hdf5".format(dataset_name=name, size=img_size, resizer=resizer)
38
else:
39
file_name = "{dataset_name}_{size}_train.hdf5".format(dataset_name=name, size=img_size)
40
file_path = join(data_dir, file_name)
41
hdf5_dir = dirname(file_path)
42
if not exists(hdf5_dir):
43
os.makedirs(hdf5_dir)
44
45
if os.path.isfile(file_path):
46
print("{file_name} exist!\nThe file are located in the {file_path}.".format(file_name=file_name,
47
file_path=file_path))
48
else:
49
dataset = Dataset_(data_name=DATA.name,
50
data_dir=RUN.data_dir,
51
train=True,
52
crop_long_edge=crop_long_edge,
53
resize_size=resize_size,
54
resizer=resizer,
55
random_flip=False,
56
normalize=False,
57
hdf5_path=None,
58
load_data_in_memory=False)
59
60
dataloader = DataLoader(dataset,
61
batch_size=500,
62
shuffle=False,
63
pin_memory=False,
64
num_workers=RUN.num_workers,
65
drop_last=False)
66
67
print("Start to load {name} into an HDF5 file with chunk size 500.".format(name=name))
68
for i, (x, y) in enumerate(tqdm(dataloader)):
69
x = np.transpose(x.numpy(), (0, 2, 3, 1))
70
y = y.numpy()
71
if i == 0:
72
with h5.File(file_path, "w") as f:
73
print("Produce dataset of len {num_dataset}".format(num_dataset=len(dataset)))
74
imgs_dset = f.create_dataset("imgs",
75
x.shape,
76
dtype="uint8",
77
maxshape=(len(dataset), img_size, img_size, 3),
78
chunks=(500, img_size, img_size, 3),
79
compression=False)
80
print("Image chunks chosen as {chunk}".format(chunk=str(imgs_dset.chunks)))
81
imgs_dset[...] = x
82
83
labels_dset = f.create_dataset("labels",
84
y.shape,
85
dtype="int64",
86
maxshape=(len(dataloader.dataset), ),
87
chunks=(500, ),
88
compression=False)
89
print("Label chunks chosen as {chunk}".format(chunk=str(labels_dset.chunks)))
90
labels_dset[...] = y
91
else:
92
with h5.File(file_path, "a") as f:
93
f["imgs"].resize(f["imgs"].shape[0] + x.shape[0], axis=0)
94
f["imgs"][-x.shape[0]:] = x
95
f["labels"].resize(f["labels"].shape[0] + y.shape[0], axis=0)
96
f["labels"][-y.shape[0]:] = y
97
return file_path, False, None
98
99