Datasets:

ArXiv:
License:
NightPro commited on
Commit
82e3506
·
verified ·
1 Parent(s): 2a5eddf

Add gRef-CW data

Browse files
.gitattributes CHANGED
@@ -1456,3 +1456,5 @@ AgroMind/CropHarvest/data/983-kenya-non-crop_2019-02-06_2020-02-01.tif filter=lf
1456
  AgroMind/CropHarvest/data/986-kenya-non-crop_2019-02-06_2020-02-01.tif filter=lfs diff=lfs merge=lfs -text
1457
  AgroMind/CropHarvest/data/996-germany_2018-02-06_2019-02-01.tif filter=lfs diff=lfs merge=lfs -text
1458
  AgroMind/CropHarvest/data/999-tanzania-ceo.tif filter=lfs diff=lfs merge=lfs -text
 
 
 
1456
  AgroMind/CropHarvest/data/986-kenya-non-crop_2019-02-06_2020-02-01.tif filter=lfs diff=lfs merge=lfs -text
1457
  AgroMind/CropHarvest/data/996-germany_2018-02-06_2019-02-01.tif filter=lfs diff=lfs merge=lfs -text
1458
  AgroMind/CropHarvest/data/999-tanzania-ceo.tif filter=lfs diff=lfs merge=lfs -text
1459
+ gRef-CW/data/grefs(unc).json filter=lfs diff=lfs merge=lfs -text
1460
+ gRef-CW/data/instances.json filter=lfs diff=lfs merge=lfs -text
gRef-CW/Weed-VG/checkpoints/stage_one.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:40e64d35891fee8406b65ecf78ca750296734380e383023649bf43e29d30d7e1
3
+ size 918570101
gRef-CW/Weed-VG/checkpoints/stage_two.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:8113bc775e77e379eb6c57bd82c5b7cc7adbc63b3c6344bd5101fba4494cd9e9
3
+ size 997609381
gRef-CW/data/grefs(unc).json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3a07b9a27a2bd4406f7f9d68067dc0bc7f2775cde21e4795224892a1f45e01e3
3
+ size 20740212
gRef-CW/data/instances.json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:be4f0956f3c72e95ae0f13f9d72014815f52e09c7e4fc5a2a20ee8ced98348cc
3
+ size 566170007
gRef-CW/scripts/build_dataset.py ADDED
@@ -0,0 +1,195 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ build_dataset.py — reconstruct the gRef-CW image set locally.
4
+
5
+ gRef-CW ships only the ANNOTATIONS (``grefs(unc).json`` and ``instances.json``).
6
+ The underlying images are part of the CropAndWeed dataset (AIT) and, under its
7
+ non-commercial licence, **may not be redistributed**. This script therefore
8
+ obtains the images from the official CropAndWeed source and arranges them to
9
+ match the gRef-CW annotations.
10
+
11
+ Pipeline
12
+ --------
13
+ 1. (``--auto``) clone the official CropAndWeed repo and run its downloader
14
+ (``python cnw/setup.py``), or point at an existing CropAndWeed checkout
15
+ with ``--cnw-images``.
16
+ 2. index the downloaded CropAndWeed images by file name / stem.
17
+ 3. for every image referenced by ``instances.json``, link (or copy) it into
18
+ ``<data-dir>/images/``.
19
+ 4. verify that every referenced image is present; report anything missing.
20
+
21
+ Usage
22
+ -----
23
+ # Fully automatic: clone CropAndWeed, download images, then build.
24
+ python scripts/build_dataset.py --data-dir data --auto
25
+
26
+ # If you already have CropAndWeed images on disk:
27
+ python scripts/build_dataset.py --data-dir data \
28
+ --cnw-images /path/to/cropandweed-dataset/data/images
29
+
30
+ # Copy instead of symlink (e.g. across filesystems):
31
+ python scripts/build_dataset.py --data-dir data --cnw-images <dir> --copy
32
+
33
+ Notes
34
+ -----
35
+ * CropAndWeed image files are named ``ave_<session>_<id>`` / ``vwg_<session>_<id>``
36
+ (see the CropAndWeed README). Matching is done on the file name, falling back
37
+ to the stem (name without extension), so it is tolerant of ``.jpg``/``.png``.
38
+ * Stdlib only — no third-party dependencies.
39
+ * Licence details: see ``DATASHEET.md`` and ``LICENSE``.
40
+ """
41
+
42
+ from __future__ import annotations
43
+
44
+ import argparse
45
+ import json
46
+ import os
47
+ import shutil
48
+ import subprocess
49
+ import sys
50
+ from pathlib import Path
51
+
52
+ CNW_REPO = "https://github.com/cropandweed/cropandweed-dataset.git"
53
+ IMAGE_EXTS = (".jpg", ".jpeg", ".png", ".tif", ".tiff", ".bmp")
54
+
55
+
56
+ def log(msg: str) -> None:
57
+ print(f"[build_dataset] {msg}", flush=True)
58
+
59
+
60
+ def referenced_file_names(instances_json: Path) -> list[str]:
61
+ """Return the list of image file names referenced by instances.json (COCO-style)."""
62
+ with instances_json.open(encoding="utf-8") as f:
63
+ data = json.load(f)
64
+ images = data.get("images")
65
+ if not images:
66
+ sys.exit(f"ERROR: no 'images' array found in {instances_json}. "
67
+ "Confirm this is a COCO-style instances file.")
68
+ names = []
69
+ for img in images:
70
+ name = img.get("file_name") or img.get("filename") or img.get("name")
71
+ if not name:
72
+ sys.exit(f"ERROR: an entry in {instances_json} has no file_name field: {img!r}")
73
+ names.append(name)
74
+ return names
75
+
76
+
77
+ def index_images(images_dir: Path) -> dict[str, Path]:
78
+ """Map both full file name and stem -> path for every image under images_dir."""
79
+ if not images_dir.is_dir():
80
+ sys.exit(f"ERROR: CropAndWeed images directory not found: {images_dir}")
81
+ index: dict[str, Path] = {}
82
+ count = 0
83
+ for path in images_dir.rglob("*"):
84
+ if path.is_file() and path.suffix.lower() in IMAGE_EXTS:
85
+ index.setdefault(path.name, path)
86
+ index.setdefault(path.stem, path)
87
+ count += 1
88
+ log(f"indexed {count} image files under {images_dir}")
89
+ if count == 0:
90
+ sys.exit("ERROR: no image files found. Did the CropAndWeed download succeed? "
91
+ "Pass the correct --cnw-images directory.")
92
+ return index
93
+
94
+
95
+ def resolve(name: str, index: dict[str, Path]) -> Path | None:
96
+ """Find a CropAndWeed image for a referenced gRef-CW file name."""
97
+ if name in index:
98
+ return index[name]
99
+ stem = Path(name).stem
100
+ return index.get(stem)
101
+
102
+
103
+ def auto_download(workdir: Path) -> Path:
104
+ """Clone CropAndWeed and run its downloader. Returns the images directory."""
105
+ repo_dir = workdir / "cropandweed-dataset"
106
+ if not repo_dir.exists():
107
+ log(f"cloning CropAndWeed into {repo_dir} ...")
108
+ subprocess.run(["git", "clone", "--depth", "1", CNW_REPO, str(repo_dir)], check=True)
109
+ else:
110
+ log(f"reusing existing CropAndWeed checkout at {repo_dir}")
111
+
112
+ setup = repo_dir / "cnw" / "setup.py"
113
+ if not setup.exists():
114
+ sys.exit(f"ERROR: expected CropAndWeed downloader at {setup} but it is missing. "
115
+ "Check the CropAndWeed repo layout and use --cnw-images instead.")
116
+ log("running CropAndWeed downloader (python cnw/setup.py) — this fetches the images ...")
117
+ subprocess.run([sys.executable, "cnw/setup.py"], cwd=str(repo_dir), check=True)
118
+
119
+ # CropAndWeed extracts images under data/images by convention; fall back to a search.
120
+ candidate = repo_dir / "data" / "images"
121
+ if candidate.is_dir() and any(candidate.rglob("*")):
122
+ return candidate
123
+ for sub in repo_dir.rglob("images"):
124
+ if sub.is_dir() and any(p.suffix.lower() in IMAGE_EXTS for p in sub.rglob("*") if p.is_file()):
125
+ return sub
126
+ sys.exit("ERROR: CropAndWeed download finished but no images directory was found. "
127
+ "Locate it manually and re-run with --cnw-images.")
128
+
129
+
130
+ def link_or_copy(src: Path, dst: Path, copy: bool) -> None:
131
+ if dst.exists() or dst.is_symlink():
132
+ dst.unlink()
133
+ if copy:
134
+ shutil.copy2(src, dst)
135
+ else:
136
+ try:
137
+ os.symlink(os.path.relpath(src, dst.parent), dst)
138
+ except OSError:
139
+ shutil.copy2(src, dst) # symlinks unsupported (e.g. some Windows setups)
140
+
141
+
142
+ def main() -> int:
143
+ ap = argparse.ArgumentParser(description="Reconstruct the gRef-CW image set from CropAndWeed.")
144
+ ap.add_argument("--data-dir", type=Path, default=Path("data"),
145
+ help="gRef-CW data dir containing instances.json; images written to <data-dir>/images.")
146
+ ap.add_argument("--instances-json", type=Path, default=None,
147
+ help="Path to instances.json (default: <data-dir>/instances.json).")
148
+ src = ap.add_mutually_exclusive_group(required=True)
149
+ src.add_argument("--auto", action="store_true",
150
+ help="Clone CropAndWeed and run its official downloader automatically.")
151
+ src.add_argument("--cnw-images", type=Path,
152
+ help="Path to an existing CropAndWeed images directory.")
153
+ ap.add_argument("--workdir", type=Path, default=Path(".cropandweed_cache"),
154
+ help="Where to clone CropAndWeed when using --auto.")
155
+ ap.add_argument("--copy", action="store_true",
156
+ help="Copy image files instead of symlinking.")
157
+ args = ap.parse_args()
158
+
159
+ instances_json = args.instances_json or (args.data_dir / "instances.json")
160
+ if not instances_json.exists():
161
+ sys.exit(f"ERROR: {instances_json} not found. Download the gRef-CW annotations first "
162
+ "(see README 'Data and Weights').")
163
+
164
+ out_dir = args.data_dir / "images"
165
+ out_dir.mkdir(parents=True, exist_ok=True)
166
+
167
+ cnw_images = auto_download(args.workdir) if args.auto else args.cnw_images
168
+ log(f"CropAndWeed images: {cnw_images}")
169
+
170
+ index = index_images(cnw_images)
171
+ names = referenced_file_names(instances_json)
172
+ log(f"{len(names)} images referenced by {instances_json.name}")
173
+
174
+ matched, missing = 0, []
175
+ for name in names:
176
+ src_path = resolve(name, index)
177
+ if src_path is None:
178
+ missing.append(name)
179
+ continue
180
+ link_or_copy(src_path, out_dir / Path(name).name, args.copy)
181
+ matched += 1
182
+
183
+ log(f"linked/copied {matched}/{len(names)} images into {out_dir}")
184
+ if missing:
185
+ log(f"WARNING: {len(missing)} referenced images were NOT found in CropAndWeed. "
186
+ f"First few: {missing[:10]}")
187
+ log("This usually means the CropAndWeed download is incomplete or the file-name "
188
+ "convention differs — verify the source and re-run.")
189
+ return 1
190
+ log("Done — every referenced image was found. Dataset is ready.")
191
+ return 0
192
+
193
+
194
+ if __name__ == "__main__":
195
+ raise SystemExit(main())