SaulLu commited on
Commit
aaf60c4
1 Parent(s): dbf71fa

add working version

Browse files
Caltech-101.py CHANGED
@@ -18,9 +18,11 @@
18
  import csv
19
  import json
20
  import os
 
21
 
22
  import datasets
23
  from datasets.tasks import ImageClassification
 
24
 
25
  _CITATION = """\
26
  @article{FeiFei2004LearningGV,
@@ -43,8 +45,8 @@ _HOMEPAGE = "https://data.caltech.edu/records/20086"
43
 
44
  _LICENSE = "CC BY 4.0"
45
 
46
- _DATA_URL = "brand_new_data/caltech-101.zip"
47
- # _DATA_URL = "brand_new_data/caltech-101/101_ObjectCategories.tar.gz"
48
 
49
  _NAMES = [
50
  "accordion",
@@ -164,24 +166,21 @@ class Caltech101(datasets.GeneratorBasedBuilder):
164
  description=_DESCRIPTION,
165
  features=datasets.Features(
166
  {
167
- "img": datasets.Image(),
168
  "label": datasets.features.ClassLabel(names=_NAMES),
169
  }
170
  ),
171
- supervised_keys=("img", "label"),
172
  homepage=_HOMEPAGE,
173
  license=_LICENSE,
174
  citation=_CITATION,
175
  task_templates=ImageClassification(
176
- image_column="img", label_column="label"
177
  ),
178
  )
179
 
180
  def _split_generators(self, dl_manager):
181
- # ----- Work in progress here -----
182
  data_dir = dl_manager.download_and_extract(_DATA_URL)
183
- files = dl_manager.iter_files(data_dir)
184
- # ---------------------------------
185
  return [
186
  datasets.SplitGenerator(
187
  name=datasets.Split.TRAIN,
@@ -203,5 +202,32 @@ class Caltech101(datasets.GeneratorBasedBuilder):
203
 
204
  # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
205
  def _generate_examples(self, filepath, split):
206
- # TODO
207
- pass
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  import csv
19
  import json
20
  import os
21
+ from pathlib import Path
22
 
23
  import datasets
24
  from datasets.tasks import ImageClassification
25
+ import numpy as np
26
 
27
  _CITATION = """\
28
  @article{FeiFei2004LearningGV,
 
45
 
46
  _LICENSE = "CC BY 4.0"
47
 
48
+ # _DATA_URL = "brand_new_data/caltech-101.zip"
49
+ _DATA_URL = "brand_new_data/caltech-101/101_ObjectCategories.tar.gz"
50
 
51
  _NAMES = [
52
  "accordion",
 
166
  description=_DESCRIPTION,
167
  features=datasets.Features(
168
  {
169
+ "image": datasets.Image(),
170
  "label": datasets.features.ClassLabel(names=_NAMES),
171
  }
172
  ),
173
+ supervised_keys=("image", "label"),
174
  homepage=_HOMEPAGE,
175
  license=_LICENSE,
176
  citation=_CITATION,
177
  task_templates=ImageClassification(
178
+ image_column="image", label_column="label"
179
  ),
180
  )
181
 
182
  def _split_generators(self, dl_manager):
 
183
  data_dir = dl_manager.download_and_extract(_DATA_URL)
 
 
184
  return [
185
  datasets.SplitGenerator(
186
  name=datasets.Split.TRAIN,
 
202
 
203
  # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
204
  def _generate_examples(self, filepath, split):
205
+ # Same stratagy as the one proposed in TF datasets
206
+ is_train_split = (split == "train")
207
+ data_dir = Path(filepath) / "101_ObjectCategories"
208
+ # Sets random seed so the random partitioning of files is the same when
209
+ # called for the train and test splits.
210
+ numpy_original_state = np.random.get_state()
211
+ np.random.seed(1234)
212
+
213
+ for class_dir in data_dir.iterdir():
214
+ fnames = [image_path for image_path in class_dir.iterdir() if image_path.name.endswith(".jpg")]
215
+ assert [image_path for image_path in class_dir.iterdir() if not image_path.name.endswith(".jpg")] == []
216
+ # _TRAIN_POINTS_PER_CLASS datapoints are sampled for the train split,
217
+ # the others constitute the test split.
218
+ if _TRAIN_POINTS_PER_CLASS > len(fnames):
219
+ raise ValueError("Fewer than {} ({}) points in class {}".format(
220
+ _TRAIN_POINTS_PER_CLASS, len(fnames), class_dir.name))
221
+ train_fnames = np.random.choice(
222
+ fnames, _TRAIN_POINTS_PER_CLASS, replace=False)
223
+ test_fnames = set(fnames).difference(train_fnames)
224
+ fnames_to_emit = train_fnames if is_train_split else test_fnames
225
+
226
+ for image_file in fnames_to_emit:
227
+ record = {
228
+ "image": str(image_file),
229
+ "label": class_dir.name.lower(),
230
+ }
231
+ yield "%s/%s" % (class_dir.name.lower(), image_file), record
232
+ # Resets the seeds to their previous states.
233
+ np.random.set_state(numpy_original_state)
brand_new_data/caltech-101/101_ObjectCategories.tar.gz ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:af6ece2f339791ca20f855943d8b55dd60892c0a25105fcd631ee3d6430f9926
3
+ size 131740031
brand_new_data/caltech-101/101_ObjectCategories.tar.gz.lock ADDED
File without changes
brand_new_data/caltech-101/Annotations.tar ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:1717f4e10aa837b05956e3f4c94456527b143eec0d95e935028b30aff40663d8
3
+ size 14028800
brand_new_data/caltech-101/show_annotation.m ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ function show_annotation(imgfile, annotation_file);
2
+ %%
3
+ %% imgfile: string
4
+ %% annotation_file: string
5
+ %%
6
+ %% written by by Fei-Fei Li - November 2004
7
+ %%
8
+
9
+ IMTYPE = 'jpg';
10
+ GUIDELINE_MODE = 1;
11
+ %% Parameters
12
+ %label_abbrev = {'LE', 'RE', 'LN', 'NB', 'RN', 'LM', 'RM'};
13
+ LARGEFONT = 28;
14
+ MEDFONT = 18;
15
+ BIG_WINDOW = get(0,'ScreenSize');
16
+ SMALL_WINDOW = [100 100 512 480];
17
+
18
+ %% load the annotated data
19
+ load(annotation_file, 'box_coord', 'obj_contour');
20
+
21
+ %% Read and display image
22
+ ima = imread(imgfile);
23
+ ff=figure(1); clf; imagesc(ima); axis image; axis ij; hold on;
24
+ % black and white images
25
+ if length(size(ima))<3
26
+ colormap(gray);
27
+ end
28
+ set(ff,'Position',SMALL_WINDOW);
29
+
30
+ %% show box
31
+ box_handle = rectangle('position', [box_coord(3), box_coord(1), box_coord(4)-box_coord(3), box_coord(2)-box_coord(1)]);
32
+ set(box_handle, 'edgecolor','y', 'linewidth',5);
33
+
34
+ %% show contour
35
+ for cc = 1:size(obj_contour,2)
36
+ if cc < size(obj_contour,2)
37
+ plot([obj_contour(1,cc), obj_contour(1,cc+1)]+box_coord(3), [obj_contour(2,cc), obj_contour(2,cc+1)]+box_coord(1), 'r','linewidth',4);
38
+ else
39
+ plot([obj_contour(1,cc), obj_contour(1,1)]+box_coord(3), [obj_contour(2,cc), obj_contour(2,1)]+box_coord(1), 'r','linewidth',4);
40
+ end
41
+ end
42
+
43
+ title(imgfile);
44
+