-
Notifications
You must be signed in to change notification settings - Fork 0
/
DatasetLoader.py
139 lines (97 loc) · 3.9 KB
/
DatasetLoader.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
#!/usr/bin/python
#-*- coding: utf-8 -*-
import torch
import numpy
import random
import pdb
import os
import threading
import time
from queue import Queue
from dataLoader import *
class DatasetLoader(object):
def __init__(self, dataset_file_name, nPerEpoch, nBatchSize, maxFrames, nDataLoaderThread, maxQueueSize=10, evalmode=False, **kwargs):
self.dataset_file_name = dataset_file_name;
self.nPerEpoch = nPerEpoch;
self.nWorkers = nDataLoaderThread;
self.nMaxFrames = maxFrames;
self.batch_size = nBatchSize;
self.maxQueueSize = maxQueueSize;
self.data_list = [];
self.data_epoch = [];
self.nFiles = 0;
self.evalmode = evalmode;
self.dataLoaders = [];
with open(dataset_file_name) as listfile:
while True:
line = listfile.readline();
if not line:
break;
data = line.split();
if len(data) == 4:
if abs(int(data[3])) - abs(int(data[2])) >= maxFrames+4:
self.data_list.append(data)
else:
print('%s is too short'%(data[0]))
else:
raise;
### Initialize Workers...
self.datasetQueue = Queue(self.maxQueueSize);
print('Evalmode %s - %d clips'%(self.evalmode,len(self.data_list)))
def dataLoaderThread(self, nThreadIndex):
index = nThreadIndex*self.batch_size;
if(index >= self.nFiles):
return;
while(True):
if(self.datasetQueue.full() == True):
time.sleep(1.0);
continue;
feat_a = []
feat_i = []
for filename in self.data_epoch[index:index+self.batch_size]:
offset = int(filename[2])
vidlength = int(filename[3])
firststart = 2-min(offset,0)
laststart = vidlength-max(offset,0)-(self.nMaxFrames+2)
# if self.evalmode:
startidx = random.randint(firststart,laststart)
feat_a.append(loadWAV(filename[1], max_frames=self.nMaxFrames*4, start_frame=startidx*4))
feat_i.append(get_frames(filename[0], max_frames=self.nMaxFrames, start_frame=startidx+offset-1))
data_im = torch.cat(feat_i,dim=0)
data_aud = torch.cat(feat_a,dim=0)
self.datasetQueue.put([data_im, data_aud]);
index += self.batch_size*self.nWorkers;
if(index+self.batch_size > self.nFiles):
break;
def __iter__(self):
## Shuffle one more
random.shuffle(self.data_list)
self.data_epoch = self.data_list[:min(self.nPerEpoch,len(self.data_list))]
self.nFiles = len(self.data_epoch)
### Make and Execute Threads...
for index in range(0, self.nWorkers):
self.dataLoaders.append(threading.Thread(target = self.dataLoaderThread, args = [index]));
self.dataLoaders[-1].start();
return self;
def __next__(self):
while(True):
isFinished = True;
if(self.datasetQueue.empty() == False):
return self.datasetQueue.get();
for index in range(0, self.nWorkers):
if(self.dataLoaders[index].is_alive() == True):
isFinished = False;
break;
if(isFinished == False):
time.sleep(1.0);
continue;
for index in range(0, self.nWorkers):
self.dataLoaders[index].join();
self.dataLoaders = [];
raise StopIteration;
def __call__(self):
pass;
def getDatasetName(self):
return self.dataset_file_name;
def qsize(self):
return self.datasetQueue.qsize();