Update xd-violence.py
Browse files- xd-violence.py +163 -151
xd-violence.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import urllib.parse
|
2 |
|
3 |
import datasets
|
|
|
4 |
import pandas as pd
|
5 |
import requests
|
6 |
|
@@ -41,43 +42,38 @@ class XDViolence(datasets.GeneratorBasedBuilder):
|
|
41 |
BUILDER_CONFIGS = [
|
42 |
XDViolenceConfig(
|
43 |
name="video",
|
44 |
-
description="Video dataset",
|
45 |
),
|
46 |
XDViolenceConfig(
|
47 |
-
name="
|
48 |
-
description="RGB
|
49 |
),
|
|
|
|
|
|
|
|
|
|
|
50 |
]
|
51 |
|
52 |
DEFAULT_CONFIG_NAME = "video"
|
53 |
BUILDER_CONFIG_CLASS = XDViolenceConfig
|
54 |
|
55 |
-
|
56 |
-
"A":
|
57 |
-
"B1":
|
58 |
-
"B2":
|
59 |
-
"B4":
|
60 |
-
"B5":
|
61 |
-
"B6":
|
62 |
-
"G":
|
63 |
-
}
|
64 |
-
|
65 |
-
LABEL2IDX = {
|
66 |
-
"Normal": 0,
|
67 |
-
"Fighting": 1,
|
68 |
-
"Shooting": 2,
|
69 |
-
"Riot": 3,
|
70 |
-
"Abuse": 4,
|
71 |
-
"Car accident": 5,
|
72 |
-
"Explosion": 6,
|
73 |
}
|
74 |
|
75 |
def _info(self):
|
76 |
-
if self.config.name == "
|
77 |
features = datasets.Features(
|
78 |
{
|
79 |
"id": datasets.Value("string"),
|
80 |
-
"
|
81 |
shape=(None, 5, 2048),
|
82 |
dtype="float32", # (num_frames, num_crops, feature_dim) use 5 crops by default as of now
|
83 |
),
|
@@ -144,161 +140,177 @@ class XDViolence(datasets.GeneratorBasedBuilder):
|
|
144 |
)
|
145 |
|
146 |
def _split_generators(self, dl_manager):
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
163 |
)
|
164 |
|
|
|
165 |
# Download videos
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
sep=" ",
|
171 |
-
usecols=[0],
|
172 |
-
names=["id"],
|
173 |
-
)["id"]
|
174 |
-
.apply(
|
175 |
-
lambda x: urllib.parse.quote(
|
176 |
-
urllib.parse.urljoin(_URL, f"video/{x.split('.mp4')[0]}.mp4"),
|
177 |
-
safe=":/",
|
178 |
)
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
)["id"]
|
188 |
-
.apply(
|
189 |
-
lambda x: urllib.parse.quote(
|
190 |
-
urllib.parse.urljoin(_URL, f"video/{x.split('.mp4')[0]}.mp4"),
|
191 |
-
safe=":/",
|
192 |
)
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
video_paths = {
|
198 |
-
"train": dl_manager.download(video_urls["train"]),
|
199 |
-
"test": dl_manager.download(video_urls["test"]),
|
200 |
-
}
|
201 |
-
|
202 |
-
return [
|
203 |
-
datasets.SplitGenerator(
|
204 |
-
name=datasets.Split.TRAIN,
|
205 |
-
gen_kwargs={
|
206 |
-
"list_path": list_paths["train"],
|
207 |
-
"frame_annotation_path": None,
|
208 |
-
"video_paths": video_paths["train"],
|
209 |
-
},
|
210 |
-
),
|
211 |
-
datasets.SplitGenerator(
|
212 |
-
name=datasets.Split.TEST,
|
213 |
-
gen_kwargs={
|
214 |
-
"list_path": list_paths["test"],
|
215 |
-
"frame_annotation_path": annotation_path,
|
216 |
-
"video_paths": video_paths["test"],
|
217 |
-
},
|
218 |
-
),
|
219 |
-
]
|
220 |
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
226 |
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
232 |
|
233 |
yield (
|
234 |
key,
|
235 |
{
|
236 |
-
"id":
|
237 |
-
"
|
238 |
"binary_target": binary,
|
239 |
"multilabel_target": multilabel,
|
240 |
"frame_annotations": frame_annotations,
|
241 |
},
|
242 |
)
|
243 |
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
)
|
249 |
-
file_list["id"] = file_list["id"].apply(
|
250 |
-
lambda x: x.split("/")[1].split(".mp4")[0]
|
251 |
-
)
|
252 |
-
file_list["binary_target"], file_list["multilabel_target"] = zip(
|
253 |
-
*file_list["id"].apply(XDViolence._extract_labels)
|
254 |
-
)
|
255 |
-
|
256 |
-
if frame_annotation_path: # test set
|
257 |
-
id2frame_annotation = {}
|
258 |
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
|
|
|
|
|
|
|
|
|
|
264 |
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
frame_annotation = [
|
269 |
-
{"start": parts[start_idx], "end": parts[start_idx + 1]}
|
270 |
-
for start_idx in range(1, len(parts), 2)
|
271 |
-
]
|
272 |
|
273 |
-
|
|
|
|
|
274 |
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
|
|
|
|
|
|
279 |
|
280 |
-
|
281 |
-
frame_annotation = [
|
282 |
-
{"start": parts[start_idx], "end": parts[start_idx + 1]}
|
283 |
-
for start_idx in range(1, len(parts), 2)
|
284 |
-
]
|
285 |
|
286 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
287 |
|
288 |
-
|
289 |
-
lambda x: id2frame_annotation[x] if x in id2frame_annotation else []
|
290 |
-
)
|
291 |
|
292 |
-
return
|
293 |
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
codes = video_id.split("_")[-1].split(".mp4")[0].split("-")
|
298 |
|
299 |
binary = 1 if len(codes) > 1 else 0
|
300 |
-
|
301 |
-
|
302 |
-
]
|
303 |
|
304 |
return binary, multilabel
|
|
|
1 |
import urllib.parse
|
2 |
|
3 |
import datasets
|
4 |
+
import numpy as np
|
5 |
import pandas as pd
|
6 |
import requests
|
7 |
|
|
|
42 |
BUILDER_CONFIGS = [
|
43 |
XDViolenceConfig(
|
44 |
name="video",
|
45 |
+
description="Video dataset.",
|
46 |
),
|
47 |
XDViolenceConfig(
|
48 |
+
name="i3d_rgb",
|
49 |
+
description="RGB features of the dataset extracted with pretrained I3D ResNet50 model.",
|
50 |
),
|
51 |
+
# TODO: Add swin_rgb features
|
52 |
+
# XDViolenceConfig(
|
53 |
+
# name="swin_rgb",
|
54 |
+
# description="RGB features of the dataset extracted with pretrained Video Swin Transformer model.",
|
55 |
+
# ),
|
56 |
]
|
57 |
|
58 |
DEFAULT_CONFIG_NAME = "video"
|
59 |
BUILDER_CONFIG_CLASS = XDViolenceConfig
|
60 |
|
61 |
+
CODE2IDX = {
|
62 |
+
"A": 0, # Normal
|
63 |
+
"B1": 1, # Fighting
|
64 |
+
"B2": 2, # Shooting
|
65 |
+
"B4": 3, # Riot
|
66 |
+
"B5": 4, # Abuse
|
67 |
+
"B6": 5, # Car accident
|
68 |
+
"G": 6, # Explosion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
}
|
70 |
|
71 |
def _info(self):
|
72 |
+
if self.config.name == "i3d_rgb":
|
73 |
features = datasets.Features(
|
74 |
{
|
75 |
"id": datasets.Value("string"),
|
76 |
+
"feature": datasets.Array3D(
|
77 |
shape=(None, 5, 2048),
|
78 |
dtype="float32", # (num_frames, num_crops, feature_dim) use 5 crops by default as of now
|
79 |
),
|
|
|
140 |
)
|
141 |
|
142 |
def _split_generators(self, dl_manager):
|
143 |
+
# Download train list
|
144 |
+
train_list_path = dl_manager.download_and_extract(
|
145 |
+
urllib.parse.urljoin(_URL, "train_list.txt")
|
146 |
+
)
|
147 |
+
train_list = (
|
148 |
+
pd.read_csv(
|
149 |
+
train_list_path, header=None, sep=" ", usecols=[0], names=["id"]
|
150 |
+
)["id"]
|
151 |
+
.apply(lambda x: x.rstrip(".mp4"))
|
152 |
+
.tolist()
|
153 |
+
)
|
154 |
+
train_ids = [
|
155 |
+
x.split("/")[1] for x in train_list
|
156 |
+
] # remove subfolder prefix, e.g., "1-1004"
|
157 |
+
|
158 |
+
# Download test list
|
159 |
+
test_list_path = dl_manager.download_and_extract(
|
160 |
+
urllib.parse.urljoin(_URL, "test_list.txt")
|
161 |
+
)
|
162 |
+
test_list = (
|
163 |
+
pd.read_csv(
|
164 |
+
test_list_path, header=None, sep=" ", usecols=[0], names=["id"]
|
165 |
+
)["id"]
|
166 |
+
.apply(lambda x: x.rstrip(".mp4"))
|
167 |
+
.tolist()
|
168 |
+
)
|
169 |
+
test_ids = [x.split("/")[1] for x in test_list]
|
170 |
+
|
171 |
+
# Download test annotation file
|
172 |
+
test_annotations_path = dl_manager.download_and_extract(
|
173 |
+
urllib.parse.urljoin(_URL, "test_annotations.txt")
|
174 |
+
)
|
175 |
+
|
176 |
+
if self.config.name == "i3d_rgb":
|
177 |
+
# Download features
|
178 |
+
train_paths = dl_manager.download_and_extract(
|
179 |
+
[
|
180 |
+
urllib.parse.quote(
|
181 |
+
urllib.parse.urljoin(_URL, f"i3d_rgb/{x}.npy"), safe=":/"
|
182 |
+
)
|
183 |
+
for x in train_list
|
184 |
+
]
|
185 |
+
)
|
186 |
+
|
187 |
+
test_paths = dl_manager.download_and_extract(
|
188 |
+
[
|
189 |
+
urllib.parse.quote(
|
190 |
+
urllib.parse.urljoin(_URL, f"i3d_rgb/{x}.npy"), safe=":/"
|
191 |
+
)
|
192 |
+
for x in test_list
|
193 |
+
]
|
194 |
)
|
195 |
|
196 |
+
else:
|
197 |
# Download videos
|
198 |
+
train_paths = dl_manager.download_and_extract(
|
199 |
+
[
|
200 |
+
urllib.parse.quote(
|
201 |
+
urllib.parse.urljoin(_URL, f"video/{x}.mp4"), safe=":/"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
202 |
)
|
203 |
+
for x in train_list
|
204 |
+
]
|
205 |
+
)
|
206 |
+
|
207 |
+
test_paths = dl_manager.download_and_extract(
|
208 |
+
[
|
209 |
+
urllib.parse.quote(
|
210 |
+
urllib.parse.urljoin(_URL, f"video/{x}.mp4"), safe=":/"
|
|
|
|
|
|
|
|
|
|
|
211 |
)
|
212 |
+
for x in test_list
|
213 |
+
]
|
214 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
215 |
|
216 |
+
return [
|
217 |
+
datasets.SplitGenerator(
|
218 |
+
name=datasets.Split.TRAIN,
|
219 |
+
gen_kwargs={
|
220 |
+
"ids": train_ids,
|
221 |
+
"paths": train_paths,
|
222 |
+
"annotations_path": None,
|
223 |
+
},
|
224 |
+
),
|
225 |
+
datasets.SplitGenerator(
|
226 |
+
name=datasets.Split.TEST,
|
227 |
+
gen_kwargs={
|
228 |
+
"ids": test_ids,
|
229 |
+
"paths": test_paths,
|
230 |
+
"annotations_path": test_annotations_path,
|
231 |
+
},
|
232 |
+
),
|
233 |
+
]
|
234 |
|
235 |
+
def _generate_examples(self, ids, paths, annotations_path):
|
236 |
+
frame_annots_mapper = (
|
237 |
+
self._read_frame_annotations(annotations_path)
|
238 |
+
if annotations_path
|
239 |
+
else dict()
|
240 |
+
)
|
241 |
+
labels = [self._extract_labels(f_id) for f_id in ids] # Extract labels
|
242 |
+
|
243 |
+
if self.config.name == "i3d_rgb":
|
244 |
+
for key, (f_id, f_path, f_label) in enumerate(zip(ids, paths, labels)):
|
245 |
+
binary, multilabel = f_label
|
246 |
+
frame_annotations = frame_annots_mapper.get(f_id, [])
|
247 |
+
feature = np.load(f_path)
|
248 |
|
249 |
yield (
|
250 |
key,
|
251 |
{
|
252 |
+
"id": f_id,
|
253 |
+
"feature": feature,
|
254 |
"binary_target": binary,
|
255 |
"multilabel_target": multilabel,
|
256 |
"frame_annotations": frame_annotations,
|
257 |
},
|
258 |
)
|
259 |
|
260 |
+
else:
|
261 |
+
for key, (f_id, f_path, f_label) in enumerate(zip(ids, paths, labels)):
|
262 |
+
binary, multilabel = f_label
|
263 |
+
frame_annotations = frame_annots_mapper.get(f_id, [])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
264 |
|
265 |
+
yield (
|
266 |
+
key,
|
267 |
+
{
|
268 |
+
"id": f_id,
|
269 |
+
"path": f_path,
|
270 |
+
"binary_target": binary,
|
271 |
+
"multilabel_target": multilabel,
|
272 |
+
"frame_annotations": frame_annotations,
|
273 |
+
},
|
274 |
+
)
|
275 |
|
276 |
+
def _read_frame_annotations(self, path):
|
277 |
+
mapper = {}
|
278 |
+
is_url = urllib.parse.urlparse(path).scheme in ("http", "https")
|
|
|
|
|
|
|
|
|
279 |
|
280 |
+
if is_url:
|
281 |
+
with requests.get(path, stream=True) as r:
|
282 |
+
r.raise_for_status()
|
283 |
|
284 |
+
for line in r.iter_lines():
|
285 |
+
parts = line.decode("utf-8").strip().split(" ")
|
286 |
+
f_id = parts[0].rstrip(".mp4")
|
287 |
+
frame_annotations = [
|
288 |
+
{"start": parts[start_idx], "end": parts[start_idx + 1]}
|
289 |
+
for start_idx in range(1, len(parts), 2)
|
290 |
+
]
|
291 |
|
292 |
+
mapper[f_id] = frame_annotations
|
|
|
|
|
|
|
|
|
293 |
|
294 |
+
else:
|
295 |
+
with open(path, "r") as f:
|
296 |
+
for line in f:
|
297 |
+
parts = line.strip().split(" ")
|
298 |
+
f_id = parts[0].rstrip(".mp4")
|
299 |
+
frame_annotations = [
|
300 |
+
{"start": parts[start_idx], "end": parts[start_idx + 1]}
|
301 |
+
for start_idx in range(1, len(parts), 2)
|
302 |
+
]
|
303 |
|
304 |
+
mapper[f_id] = frame_annotations
|
|
|
|
|
305 |
|
306 |
+
return mapper
|
307 |
|
308 |
+
def _extract_labels(self, f_id):
|
309 |
+
"""Extracts labels from a given file id."""
|
310 |
+
codes = f_id.split("_")[-1].split("-")
|
|
|
311 |
|
312 |
binary = 1 if len(codes) > 1 else 0
|
313 |
+
|
314 |
+
multilabel = [self.CODE2IDX[code] for code in codes if code != "0"]
|
|
|
315 |
|
316 |
return binary, multilabel
|