forked from kuangliu/pytorch-cifar
-
Notifications
You must be signed in to change notification settings - Fork 1
/
speed.py
202 lines (164 loc) · 5.9 KB
/
speed.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import os
import time
import torch
import random
import shutil
import cProfile
import torch.optim
from glob import glob
import multiprocessing
import torch.utils.data
import torch.nn.parallel
from functools import wraps
import torch.utils.data.distributed
from alisuretool.Tools import Tools
import torch.utils.data.distributed
import torchvision.models as models
from line_profiler import LineProfiler
import torchvision.datasets as datasets
import torchvision.transforms as transforms
"""
https://www.cnblogs.com/king-lps/p/10936374.html
https://blog.csdn.net/winycg/article/details/92443146
"""
class InputTrain(object):
def __init__(self, data_root, batch_size, num_workers, queue_max_size):
self.data_root = data_root
self.batch_size = batch_size
self.queue_max_size = queue_max_size
self.queue = multiprocessing.Queue(maxsize=self.queue_max_size)
self.train_dataset = self.data()
self.num_workers = num_workers
self.data_len = len(self.train_dataset.imgs)
self.num_per_worker = self.data_len // self.num_workers
self.split_data = []
self.process = []
pass
def data(self):
# train_dir = os.path.join(self.data_root, 'train')
train_dir = os.path.join(self.data_root, 'val_new')
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
train_dataset = datasets.ImageFolder(train_dir, transforms.Compose([
transforms.RandomResizedCrop(224), transforms.RandomHorizontalFlip(), transforms.ToTensor(), normalize]))
return train_dataset
def init(self):
img_index = list(range(self.data_len))
random.shuffle(img_index)
self.split_data = [img_index[i * self.num_per_worker:
(i + 1) * self.num_per_worker] for i in range(self.num_workers)]
pass
def start_queue(self):
self.process = [multiprocessing.Process(
target=self._queue_put, args=(self.queue, self.split_data[i])) for i in range(self.num_workers)]
[t.start() for t in self.process]
pass
def __getitem__(self, item):
images, labels = self.queue.get()
return images, labels
def _queue_put(self, queue, index_list):
batch_num = len(index_list) // self.batch_size
for i in range(batch_num):
images, labels = self._get_batch_data(index_list[i * self.batch_size: (i + 1) * self.batch_size])
queue.put([images, labels])
pass
pass
def _get_batch_data(self, indexes):
assert len(indexes) == self.batch_size
images, labels = [], []
for i in indexes:
sample, target = self.train_dataset.__getitem__(i)
images.append(sample)
labels.append(target)
pass
return torch.stack(images, dim=0), torch.tensor(labels, dtype=torch.long)
pass
class Runner(object):
def __init__(self, train_loader):
self.model = torch.nn.DataParallel(models.__dict__["resnet18"](pretrained=False)).cuda()
self.train_loader = train_loader
pass
def train(self):
self.model.train()
time_sum = 0
end = time.time()
self.train_loader.init()
self.train_loader.start_queue()
for i, (_images, _target) in enumerate(self.train_loader):
use_time = time.time() - end
time_sum += use_time
end = time.time()
Tools.print("i={} use={:4f} avg={:4f} sum={:4f}".format(i, use_time, time_sum / (i + 1), time_sum))
pass
pass
@staticmethod
def demo():
input_train = InputTrain(data_root="/home/z840/data/DATASET/ILSVRC2015/Data/CLS-LOC",
batch_size=256, num_workers=30, queue_max_size=100)
Runner(train_loader=input_train).train()
pass
pass
def copy_data():
replace1 = "/data/DATASET/ILSVRC2015/Data/CLS-LOC"
replace2 = "/media/z840/ALISURE/data/DATASET/ILSVRC2015/Data/CLS-LOC"
image_file_list = glob("{}/train/*/*.JPEG".format(replace1))
for index, image_file in enumerate(image_file_list):
if index % 10000 == 0:
print("{} {}".format(index, len(image_file_list)))
image_file2 = image_file.replace(replace1, replace2)
if not os.path.exists(image_file2):
print(image_file2)
shutil.copy(image_file, image_file2)
pass
pass
pass
class TimeStat(object):
@staticmethod
def func_time(f):
@wraps(f)
def wrapper(*args, **kwargs):
start = time.time()
result = f(*args, **kwargs)
end = time.time()
print(f.__name__, 'took', end - start, 'seconds')
return result
return wrapper
@staticmethod
def func_profile(f):
@wraps(f)
def wrapper(*args, **kwargs):
profile = cProfile.Profile()
try:
profile.enable()
result = f(*args, **kwargs)
profile.disable()
return result
finally:
profile.print_stats(sort='time')
return wrapper
@staticmethod
def func_line_time(follow=[]):
def decorate(func):
@wraps(func)
def profiled_func(*args, **kwargs):
profiler = LineProfiler()
try:
profiler.add_function(func)
for f in follow:
profiler.add_function(f)
profiler.enable_by_count()
return func(*args, **kwargs)
finally:
profiler.print_stats()
pass
return profiled_func
return decorate
pass
@TimeStat.func_line_time()
def first():
for x in range(1000000):
if x % 100000 == 0:
print(x)
pass
if __name__ == '__main__':
first()
pass