jherng commited on
Commit
a8a6b5c
1 Parent(s): b7ca02b

Finish dataset loading script

Browse files
Files changed (1) hide show
  1. xd-violence.py +66 -20
xd-violence.py CHANGED
@@ -52,20 +52,42 @@ class XDViolence(datasets.GeneratorBasedBuilder):
52
  DEFAULT_CONFIG_NAME = "video"
53
  BUILDER_CONFIG_CLASS = XDViolenceConfig
54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
  def _info(self):
56
  if self.config.name == "rgb":
57
  features = datasets.Features(
58
  {
 
59
  "rgb_feats": datasets.Array3D(
60
  shape=(None, 10, 2048),
61
  dtype="float32", # (num_frames, num_crops, feature_dim) use 10 crops by default as of now
62
  ),
63
  "binary_target": datasets.ClassLabel(
64
- names=["non-violence", "violence"]
65
  ),
66
  "multilabel_targets": datasets.Sequence(
67
  datasets.ClassLabel(
68
  names=[
 
69
  "Fighting",
70
  "Shooting",
71
  "Riot",
@@ -86,13 +108,15 @@ class XDViolence(datasets.GeneratorBasedBuilder):
86
  else: # default = "video"
87
  features = datasets.Features(
88
  {
 
89
  "video_path": datasets.Value("string"),
90
  "binary_target": datasets.ClassLabel(
91
- names=["non-violence", "violence"]
92
  ),
93
  "multilabel_targets": datasets.Sequence(
94
  datasets.ClassLabel(
95
  names=[
 
96
  "Fighting",
97
  "Shooting",
98
  "Riot",
@@ -170,8 +194,8 @@ class XDViolence(datasets.GeneratorBasedBuilder):
170
 
171
  # Function to read annotations
172
  annotation_readers = {
173
- "train": XDViolence._read_train_list,
174
- "test": XDViolence._read_test_annotations,
175
  }
176
 
177
  return [
@@ -194,24 +218,34 @@ class XDViolence(datasets.GeneratorBasedBuilder):
194
  ]
195
 
196
  def _generate_examples(self, annotation_path, video_paths, annotation_reader):
197
- # If test, read annotations from test_annotations.txt
198
- # If train, read labels from train_list.txt
199
-
200
- # for key, video_path in enumerate(zip(video_paths, )):
201
- # pass
202
- print(annotation_reader)
203
- ann_data = annotation_reader(self, annotation_path)
204
- print(ann_data[:5])
205
- for key, video_path, annotation in enumerate(zip(video_paths, ann_data)):
206
- yield key, {"video_path": video_path}
207
-
208
- def _read_train_list(cls, path):
 
 
 
 
 
 
209
  """Reads the train_list.txt file and returns a list of video ids."""
210
- return pd.read_csv(path, header=None, sep=" ", usecols=[0], names=["video_id"])[
211
- "video_id"
212
- ].to_list()
213
 
214
- def _read_test_annotations(cls, path):
 
 
 
 
 
 
 
215
  """Reads the test_annotations.txt file and returns a list of annotations."""
216
  url_components = urllib.parse.urlparse(path)
217
  is_url = url_components.scheme == "https" or url_components.scheme == "http"
@@ -250,3 +284,15 @@ class XDViolence(datasets.GeneratorBasedBuilder):
250
  )
251
 
252
  return annotations
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  DEFAULT_CONFIG_NAME = "video"
53
  BUILDER_CONFIG_CLASS = XDViolenceConfig
54
 
55
+ CODE2LABEL = {
56
+ "A": "Normal",
57
+ "B1": "Fighting",
58
+ "B2": "Shooting",
59
+ "B4": "Riot",
60
+ "B5": "Abuse",
61
+ "B6": "Car accident",
62
+ "G": "Explosion",
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 == "rgb":
77
  features = datasets.Features(
78
  {
79
+ "video_id": datasets.Value("string"),
80
  "rgb_feats": datasets.Array3D(
81
  shape=(None, 10, 2048),
82
  dtype="float32", # (num_frames, num_crops, feature_dim) use 10 crops by default as of now
83
  ),
84
  "binary_target": datasets.ClassLabel(
85
+ names=["Non-violence", "Violence"]
86
  ),
87
  "multilabel_targets": datasets.Sequence(
88
  datasets.ClassLabel(
89
  names=[
90
+ "Normal",
91
  "Fighting",
92
  "Shooting",
93
  "Riot",
 
108
  else: # default = "video"
109
  features = datasets.Features(
110
  {
111
+ "video_id": datasets.Value("string"),
112
  "video_path": datasets.Value("string"),
113
  "binary_target": datasets.ClassLabel(
114
+ names=["Non-violence", "Violence"]
115
  ),
116
  "multilabel_targets": datasets.Sequence(
117
  datasets.ClassLabel(
118
  names=[
119
+ "Normal",
120
  "Fighting",
121
  "Shooting",
122
  "Riot",
 
194
 
195
  # Function to read annotations
196
  annotation_readers = {
197
+ "train": self._read_train_list,
198
+ "test": self._read_test_annotations,
199
  }
200
 
201
  return [
 
218
  ]
219
 
220
  def _generate_examples(self, annotation_path, video_paths, annotation_reader):
221
+ ann_data = annotation_reader(annotation_path)
222
+
223
+ for key, (video_path, annotation) in enumerate(zip(video_paths, ann_data)):
224
+ video_id = annotation["video_id"]
225
+ frame_annotations = annotation.get("frame_annotations", [])
226
+
227
+ binary, multilabel = self.extract_labels(video_id)
228
+
229
+ yield key, {
230
+ "video_id": video_id,
231
+ "video_path": video_path,
232
+ "binary_target": binary,
233
+ "multilabel_targets": multilabel,
234
+ "frame_annotations": frame_annotations,
235
+ }
236
+
237
+ @staticmethod
238
+ def _read_train_list(path):
239
  """Reads the train_list.txt file and returns a list of video ids."""
 
 
 
240
 
241
+ train_list = pd.read_csv(
242
+ path, header=None, sep=" ", usecols=[0], names=["video_id"]
243
+ )
244
+ train_list["video_id"] = train_list["video_id"].apply(lambda x: x.split("/")[1])
245
+ return train_list.to_dict("records")
246
+
247
+ @staticmethod
248
+ def _read_test_annotations(path):
249
  """Reads the test_annotations.txt file and returns a list of annotations."""
250
  url_components = urllib.parse.urlparse(path)
251
  is_url = url_components.scheme == "https" or url_components.scheme == "http"
 
284
  )
285
 
286
  return annotations
287
+
288
+ @classmethod
289
+ def extract_labels(cls, video_id):
290
+ """Extracts labels from the video id."""
291
+ codes = video_id.split("_")[-1].split("-")
292
+
293
+ binary = 1 if len(codes) > 1 else 0
294
+ multilabel = [
295
+ cls.LABEL2IDX[cls.CODE2LABEL[code]] for code in codes if code != "0"
296
+ ]
297
+
298
+ return binary, multilabel