-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbenchmark.py
86 lines (57 loc) · 2.01 KB
/
benchmark.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from time import time
import os
from torch.utils.data.dataset import Dataset
from PIL import Image
import numpy as np
from pytorch_mmap_dataset import MMAPDataset
DATASET_ROOT_PATH = "./local_test_dir/testSample"
class DiskReadDataset(Dataset):
def __init__(self, root_path: str) -> None:
super().__init__()
self.root_path = root_path
self.images = os.listdir(root_path) * 10
def __getitem__(self, index):
image_name = self.images[index]
image = Image.open(os.path.join(self.root_path, image_name)).convert("RGB")
image = np.array(image).flatten()
return image, np.zeros(10)
def __len__(self):
return len(self.images)
def benchmark_disk_read(root_dir: str = DATASET_ROOT_PATH):
dataset = DiskReadDataset(root_dir)
start_time = time()
for image, label in zip(images, labels):
pass
duration = time() - start_time
print(f"Disk read benchmark: {duration} seconds")
def benchmark_in_memory(root_dir: str = DATASET_ROOT_PATH):
images = []
labels = []
dataset = DiskReadDataset(root_dir)
for image, label in dataset:
images.append(image)
labels.append(label)
start_time = time()
for image, label in zip(images, labels):
pass
duration = time() - start_time
print(f"In memory benchmark: {duration} seconds")
def benchmark_mmap(root_dir: str = DATASET_ROOT_PATH):
images = []
labels = []
dataset = DiskReadDataset(root_dir)
for image, label in dataset:
images.append(image)
labels.append(label)
dataset = MMAPDataset(images, labels, size=len(dataset))
start_time = time()
for image, label in zip(images, labels):
pass
duration = time() - start_time
print(f"MMAP benchmark: {duration} seconds")
def main():
benchmark_disk_read()
benchmark_in_memory()
benchmark_mmap()
if __name__ == "__main__":
main()