-
Notifications
You must be signed in to change notification settings - Fork 2
/
loop_read.py
145 lines (127 loc) · 4.16 KB
/
loop_read.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
# Copyright 2022 CRS4 (http://www.crs4.it/)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# cassandra reader
from cassandra_reader import get_cassandra_reader
# dali
from nvidia.dali.pipeline import pipeline_def
from nvidia.dali.plugin.base_iterator import LastBatchPolicy
from nvidia.dali.plugin.pytorch import DALIGenericIterator
import nvidia.dali.fn as fn
import nvidia.dali.types as types
# some preconfigured operators
from fn_shortcuts import (
fn_decode,
fn_normalize,
fn_image_random_crop,
fn_resize,
fn_crop_normalize,
)
# varia
from clize import run
from tqdm import trange, tqdm
import pickle
import math
# supporting torchrun
import os
global_rank = int(os.getenv("RANK", default=0))
local_rank = int(os.getenv("LOCAL_RANK", default=0))
world_size = int(os.getenv("WORLD_SIZE", default=1))
def read_data(
split_fn,
*,
use_index=0,
use_gpu=False,
epochs=10,
):
"""Read images from DB or filesystem, in a tight loop
:param use_gpu: enable output to GPU (default: False)
"""
if use_gpu:
device_id = local_rank
else:
device_id = types.CPU_ONLY_DEVICE_ID
data = pickle.load(open(split_fn, "rb"))
data_table = data["data_table"]
row_keys = data["row_keys"]
split = data["split"]
source_uuids = row_keys[split[use_index]]
source_uuids = list(source_uuids)
bs = 128
chosen_reader = get_cassandra_reader(
data_table,
prefetch_buffers=4,
io_threads=8,
name="Reader",
comm_threads=1,
copy_threads=4,
ooo=True,
slow_start=4,
source_uuids=source_uuids,
shard_id=global_rank,
num_shards=world_size,
)
# create dali pipeline
@pipeline_def(
batch_size=bs,
num_threads=4,
device_id=device_id,
prefetch_queue_depth=2,
#########################
# py_start_method="spawn",
# enable_memory_stats=True,
)
def get_dali_pipeline():
images, labels = chosen_reader
####################################################################
# - decode, resize and crop, must use GPU (e.g., --use-gpu)
# images = fn_image_random_crop(images)
# images = fn_resize(images)
# images = fn_crop_normalize(images)
####################################################################
if device_id != types.CPU_ONLY_DEVICE_ID:
images = images.gpu()
labels = labels.gpu()
return images, labels
pl = get_dali_pipeline()
pl.build()
########################################################################
# DALI iterator
########################################################################
# produce images
shard_size = math.ceil(pl.epoch_size()['Reader'] / world_size)
steps = math.ceil(shard_size / bs)
# consume uuids to get images from DB
for _ in range(epochs):
# read data for current epoch
for _ in trange(steps):
pl.run()
pl.reset()
########################################################################
# alternatively: use pytorch iterator
# (note: decode of images must be enabled)
########################################################################
# ddl = DALIGenericIterator(
# [pl],
# ["data", "label"],
# reader_name="Reader",
# last_batch_policy=LastBatchPolicy.PARTIAL #FILL, PARTIAL, DROP
# )
# for _ in range(epochs):
# # consume data
# for data in tqdm(ddl):
# x, y = data[0]["data"], data[0]["label"]
# ddl.reset() # rewind data loader
# parse arguments
if __name__ == "__main__":
run(read_data)