jinmang2 commited on
Commit
e569f93
·
1 Parent(s): a939106

Update ucf_crime.py

Browse files
Files changed (1) hide show
  1. ucf_crime.py +71 -26
ucf_crime.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import datasets
2
 
3
 
@@ -57,21 +58,21 @@ _DATA_URLS = {
57
  }
58
  _SPLIT_PATH = "UCF_Crimes-Train-Test-Split/"
59
  _SPLIT_FILES = {
60
- "action_recognition": {
61
- "train": [
62
- _SPLIT_PATH + f"Action_Recognition_splits/train_{i:03}.txt"
63
  for i in range(1, 5)
64
- ],
65
- "test": [
66
- _SPLIT_PATH + f"Action_Recognition_splits/test_{i:03}.txt"
67
  for i in range(1, 5)
68
- ],
69
  },
70
  "anomaly_detection": {
71
  "train": [_SPLIT_PATH + "Anomaly_Detection_splits/Anomaly_Train.txt"],
72
  "test": [_SPLIT_PATH + "Anomaly_Detection_splits/Anomaly_Test.txt"],
73
  },
74
- "event_recognition": {
75
  "train": [_SPLIT_PATH + "Temporal_Anomaly_Annotation_for_Testing_Videos.txt"],
76
  },
77
  }
@@ -100,8 +101,7 @@ class UCFCrime(datasets.GeneratorBasedBuilder):
100
  BUILDER_CONFIGS = [
101
  UCFCrimeConfig(name="all"),
102
  UCFCrimeConfig(name="anomaly"),
103
- UCFCrimeConfig(name="action"),
104
- UCFCrimeConfig(name="event"),
105
  UCFCrimeConfig(name="test"), # For speed-loading
106
  ]
107
 
@@ -116,21 +116,66 @@ class UCFCrime(datasets.GeneratorBasedBuilder):
116
 
117
  def _split_generators(self, dl_manager):
118
  """Return SplitGenerators."""
119
- data_url = self.config.data_urls
120
- if self.config.name == "test":
121
- data_url = data_url["test"]
122
- data_file = dl_manager.download(data_url)
123
- return [
124
- datasets.SplitGenerator(
125
- name=datasets.Split.TRAIN,
126
- # These kwargs will be passed to _generate_examples
127
- gen_kwargs={"data_file": dl_manager.iter_archive(data_file)},
128
- ),
129
- ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
130
 
131
- def _generate_examples(self, data_file: str):
132
  """Yields example."""
133
- for path, file in data_file:
134
- if path.endswith(".mp4"):
135
- print(file.read())
136
- yield None, {"video_path": data_file}
 
 
 
 
 
 
 
 
 
1
+ import os
2
  import datasets
3
 
4
 
 
58
  }
59
  _SPLIT_PATH = "UCF_Crimes-Train-Test-Split/"
60
  _SPLIT_FILES = {
61
+ "event_recognition": {
62
+ "train": {
63
+ f"event{i}": _SPLIT_PATH + f"Action_Recognition_splits/train_{i:03}.txt"
64
  for i in range(1, 5)
65
+ },
66
+ "test": {
67
+ f"event{i}": _SPLIT_PATH + f"Action_Recognition_splits/test_{i:03}.txt"
68
  for i in range(1, 5)
69
+ },
70
  },
71
  "anomaly_detection": {
72
  "train": [_SPLIT_PATH + "Anomaly_Detection_splits/Anomaly_Train.txt"],
73
  "test": [_SPLIT_PATH + "Anomaly_Detection_splits/Anomaly_Test.txt"],
74
  },
75
+ "temporal_anomaly": {
76
  "train": [_SPLIT_PATH + "Temporal_Anomaly_Annotation_for_Testing_Videos.txt"],
77
  },
78
  }
 
101
  BUILDER_CONFIGS = [
102
  UCFCrimeConfig(name="all"),
103
  UCFCrimeConfig(name="anomaly"),
104
+ *[UCFCrimeConfig(name=f"event{i}") for i in range(1, 5)],
 
105
  UCFCrimeConfig(name="test"), # For speed-loading
106
  ]
107
 
 
116
 
117
  def _split_generators(self, dl_manager):
118
  """Return SplitGenerators."""
119
+ data_urls = self.config.data_urls
120
+ if self.config.name != "test":
121
+ data_urls.pop("test")
122
+ if "event" in self.config.name:
123
+ data_urls.pop("normal-train-part1")
124
+ data_urls.pop("normal-train-part2")
125
+ data_urls.pop("normal-test")
126
+ split_url = _SPLIT_FILES["event_recognition"]
127
+ elif "anomaly" == self.config.name:
128
+ data_urls.pop("normal-event-recognition")
129
+ split_url = _SPLIT_FILES["anomaly_detection"]
130
+ data_url = data_urls
131
+ else:
132
+ data_url = data_urls["test"]
133
+
134
+ path = dl_manager.download(data_url)
135
+ if split_url is not None:
136
+ split_path = dl_manager.download(split_url)
137
+ if self.config.name in ("all", "test"):
138
+ return [
139
+ datasets.SplitGenerator(
140
+ name=datasets.Split.TRAIN,
141
+ # These kwargs will be passed to _generate_examples
142
+ gen_kwargs={
143
+ "extracted_path": dl_manager.extract(path),
144
+ "split_path": None,
145
+ },
146
+ ),
147
+ ]
148
+ elif self.config.name in ("anomaly", *[f"event{i}" for i in range(1, 5)]):
149
+ return [
150
+ datasets.SplitGenerator(
151
+ name=datasets.Split.TRAIN,
152
+ # These kwargs will be passed to _generate_examples
153
+ gen_kwargs={
154
+ "extracted_path": dl_manager.extract(path),
155
+ "split_path": split_path["train"][self.config.name],
156
+ },
157
+ ),
158
+ datasets.SplitGenerator(
159
+ name=datasets.Split.TEST,
160
+ # These kwargs will be passed to _generate_examples
161
+ gen_kwargs={
162
+ "extracted_path": dl_manager.extract(path),
163
+ "split_path": split_path["test"][self.config.name],
164
+ },
165
+ ),
166
+ ]
167
 
168
+ def _generate_examples(self, extracted_path: str, split_path: dict):
169
  """Yields example."""
170
+ idx = 0
171
+ split_files = []
172
+ with open(split_path, "r") as f:
173
+ for line in f.readlines():
174
+ split_files.append(line.strip().split("/")[-1])
175
+ for root, _, files in os.walk(extracted_path, topdown=False):
176
+ relative_dir_path = root.replace(os.path.abspath(extracted_path) + os.sep, "")
177
+ for name in files:
178
+ relative_file_path = os.path.join(relative_dir_path, name)
179
+ if relative_file_path.endswith(".mp4") and name in split_files:
180
+ yield idx, {"video_path": relative_file_path}
181
+ idx += 1