-
Notifications
You must be signed in to change notification settings - Fork 0
/
LoadUCF101Data.py
131 lines (98 loc) · 3.55 KB
/
LoadUCF101Data.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
import torchvision.transforms as transforms
from torch.utils.data import Dataset, DataLoader
import torch
import numpy as np
import os
from PIL import Image
import torchvision.transforms._transforms_video as v_transform
import torchvision.io as io
import config
classInd = []
with open('classInd.txt', 'r') as f:
all_Class_and_Ind = f.readlines()
for line in all_Class_and_Ind:
idx = line[:-1].split(' ')[0]
className = line[:-1].split(' ')[1]
classInd.append(className)
TrainVideoNameList = []
with open('trainlist01.txt', 'r') as f:
all_Class_and_Ind = f.readlines()
for line in all_Class_and_Ind:
video_name = line.split(' ')[0]
video_name = video_name.split('/')[1]
TrainVideoNameList.append(video_name)
TestVideoNameList = []
with open('testlist01.txt', 'r') as f:
all_Class_and_Ind = f.readlines()
for line in all_Class_and_Ind:
video_name = line[:-1].split(' ')[0]
video_name = video_name.split('/')[1]
TestVideoNameList.append(video_name)
class UCF101Data(Dataset): # define a class named MNIST
# read all pictures' filename
def __init__(self, UCF_root, isTrain, transform=None):
# root: Dataset's filepath
# classInd: dictionary (1 -> ApplyEyeMakeup)
self.RGBvideoData = []
self.transform = transform
for i in range(0, 101):
RGB_class_path = UCF_root + classInd[i]
# only load train/test data using TrainVideoNameList/TestVideoNameList
if isTrain:
TrainOrTest_VideoNameList = list(set(os.listdir(RGB_class_path)).intersection(set(TrainVideoNameList)))
else:
TrainOrTest_VideoNameList = list(set(os.listdir(RGB_class_path)).intersection(set(TestVideoNameList)))
for video_dir in os.listdir(RGB_class_path):
if video_dir in TrainOrTest_VideoNameList:
signel_RGB_video_path = RGB_class_path + '/' + video_dir
# (signel_RGB_video_path, label)
self.RGBvideoData.append((signel_RGB_video_path, i))
self.len = len(self.RGBvideoData)
# Get a sample from the dataset & Return an image and it's label
def __getitem__(self, index):
signel_RGB_video_path, label = self.RGBvideoData[index]
vframes, _, _ = io.read_video(signel_RGB_video_path)
ran_i = np.random.randint(len(vframes))
# open the RGB frame
RGB_frame = vframes[ran_i]
RGB_frame = RGB_frame.numpy()
# May use transform function to transform samples
if self.transform is not None:
RGB_frame = self.transform(RGB_frame)
return RGB_frame, label
# get the length of dataset
def __len__(self):
return self.len
# define the transformation
# PIL images -> torch tensors [0, 1]
transform = transforms.Compose([
transforms.ToPILImage(),
transforms.RandomResizedCrop(224),
transforms.ToTensor(),
])
# load the UCF101 training dataset
trainset = UCF101Data(
UCF_root=config.UCF101_original_Dataset_root,
isTrain=True,
transform=transform
)
# divide the dataset into batches
trainset_loader = DataLoader(
trainset,
batch_size=config.AR_TRAIN_BATCH_SIZE,
shuffle=True,
num_workers=8
)
# load the UCF101 testing dataset
testset = UCF101Data(
UCF_root=config.UCF101_original_Dataset_root,
isTrain=False,
transform=transform
)
# divide the dataset into batches
testset_loader = DataLoader(
testset,
batch_size=config.AR_TEST_BATCH_SIZE,
shuffle=False,
num_workers=8
)