|
a |
|
b/data/xView.yaml |
|
|
1 |
# YOLOv5 🚀 by Ultralytics, AGPL-3.0 license |
|
|
2 |
# DIUx xView 2018 Challenge https://challenge.xviewdataset.org by U.S. National Geospatial-Intelligence Agency (NGA) |
|
|
3 |
# -------- DOWNLOAD DATA MANUALLY and jar xf val_images.zip to 'datasets/xView' before running train command! -------- |
|
|
4 |
# Example usage: python train.py --data xView.yaml |
|
|
5 |
# parent |
|
|
6 |
# ├── yolov5 |
|
|
7 |
# └── datasets |
|
|
8 |
# └── xView ← downloads here (20.7 GB) |
|
|
9 |
|
|
|
10 |
|
|
|
11 |
# Train/val/test sets as 1) dir: path/to/imgs, 2) file: path/to/imgs.txt, or 3) list: [path/to/imgs1, path/to/imgs2, ..] |
|
|
12 |
path: ../datasets/xView # dataset root dir |
|
|
13 |
train: images/autosplit_train.txt # train images (relative to 'path') 90% of 847 train images |
|
|
14 |
val: images/autosplit_val.txt # train images (relative to 'path') 10% of 847 train images |
|
|
15 |
|
|
|
16 |
# Classes |
|
|
17 |
names: |
|
|
18 |
0: Fixed-wing Aircraft |
|
|
19 |
1: Small Aircraft |
|
|
20 |
2: Cargo Plane |
|
|
21 |
3: Helicopter |
|
|
22 |
4: Passenger Vehicle |
|
|
23 |
5: Small Car |
|
|
24 |
6: Bus |
|
|
25 |
7: Pickup Truck |
|
|
26 |
8: Utility Truck |
|
|
27 |
9: Truck |
|
|
28 |
10: Cargo Truck |
|
|
29 |
11: Truck w/Box |
|
|
30 |
12: Truck Tractor |
|
|
31 |
13: Trailer |
|
|
32 |
14: Truck w/Flatbed |
|
|
33 |
15: Truck w/Liquid |
|
|
34 |
16: Crane Truck |
|
|
35 |
17: Railway Vehicle |
|
|
36 |
18: Passenger Car |
|
|
37 |
19: Cargo Car |
|
|
38 |
20: Flat Car |
|
|
39 |
21: Tank car |
|
|
40 |
22: Locomotive |
|
|
41 |
23: Maritime Vessel |
|
|
42 |
24: Motorboat |
|
|
43 |
25: Sailboat |
|
|
44 |
26: Tugboat |
|
|
45 |
27: Barge |
|
|
46 |
28: Fishing Vessel |
|
|
47 |
29: Ferry |
|
|
48 |
30: Yacht |
|
|
49 |
31: Container Ship |
|
|
50 |
32: Oil Tanker |
|
|
51 |
33: Engineering Vehicle |
|
|
52 |
34: Tower crane |
|
|
53 |
35: Container Crane |
|
|
54 |
36: Reach Stacker |
|
|
55 |
37: Straddle Carrier |
|
|
56 |
38: Mobile Crane |
|
|
57 |
39: Dump Truck |
|
|
58 |
40: Haul Truck |
|
|
59 |
41: Scraper/Tractor |
|
|
60 |
42: Front loader/Bulldozer |
|
|
61 |
43: Excavator |
|
|
62 |
44: Cement Mixer |
|
|
63 |
45: Ground Grader |
|
|
64 |
46: Hut/Tent |
|
|
65 |
47: Shed |
|
|
66 |
48: Building |
|
|
67 |
49: Aircraft Hangar |
|
|
68 |
50: Damaged Building |
|
|
69 |
51: Facility |
|
|
70 |
52: Construction Site |
|
|
71 |
53: Vehicle Lot |
|
|
72 |
54: Helipad |
|
|
73 |
55: Storage Tank |
|
|
74 |
56: Shipping container lot |
|
|
75 |
57: Shipping Container |
|
|
76 |
58: Pylon |
|
|
77 |
59: Tower |
|
|
78 |
|
|
|
79 |
|
|
|
80 |
# Download script/URL (optional) --------------------------------------------------------------------------------------- |
|
|
81 |
download: | |
|
|
82 |
import json |
|
|
83 |
import os |
|
|
84 |
from pathlib import Path |
|
|
85 |
|
|
|
86 |
import numpy as np |
|
|
87 |
from PIL import Image |
|
|
88 |
from tqdm import tqdm |
|
|
89 |
|
|
|
90 |
from utils.dataloaders import autosplit |
|
|
91 |
from utils.general import download, xyxy2xywhn |
|
|
92 |
|
|
|
93 |
|
|
|
94 |
def convert_labels(fname=Path('xView/xView_train.geojson')): |
|
|
95 |
# Convert xView geoJSON labels to YOLO format |
|
|
96 |
path = fname.parent |
|
|
97 |
with open(fname) as f: |
|
|
98 |
print(f'Loading {fname}...') |
|
|
99 |
data = json.load(f) |
|
|
100 |
|
|
|
101 |
# Make dirs |
|
|
102 |
labels = Path(path / 'labels' / 'train') |
|
|
103 |
os.system(f'rm -rf {labels}') |
|
|
104 |
labels.mkdir(parents=True, exist_ok=True) |
|
|
105 |
|
|
|
106 |
# xView classes 11-94 to 0-59 |
|
|
107 |
xview_class2index = [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, -1, 3, -1, 4, 5, 6, 7, 8, -1, 9, 10, 11, |
|
|
108 |
12, 13, 14, 15, -1, -1, 16, 17, 18, 19, 20, 21, 22, -1, 23, 24, 25, -1, 26, 27, -1, 28, -1, |
|
|
109 |
29, 30, 31, 32, 33, 34, 35, 36, 37, -1, 38, 39, 40, 41, 42, 43, 44, 45, -1, -1, -1, -1, 46, |
|
|
110 |
47, 48, 49, -1, 50, 51, -1, 52, -1, -1, -1, 53, 54, -1, 55, -1, -1, 56, -1, 57, -1, 58, 59] |
|
|
111 |
|
|
|
112 |
shapes = {} |
|
|
113 |
for feature in tqdm(data['features'], desc=f'Converting {fname}'): |
|
|
114 |
p = feature['properties'] |
|
|
115 |
if p['bounds_imcoords']: |
|
|
116 |
id = p['image_id'] |
|
|
117 |
file = path / 'train_images' / id |
|
|
118 |
if file.exists(): # 1395.tif missing |
|
|
119 |
try: |
|
|
120 |
box = np.array([int(num) for num in p['bounds_imcoords'].split(",")]) |
|
|
121 |
assert box.shape[0] == 4, f'incorrect box shape {box.shape[0]}' |
|
|
122 |
cls = p['type_id'] |
|
|
123 |
cls = xview_class2index[int(cls)] # xView class to 0-60 |
|
|
124 |
assert 59 >= cls >= 0, f'incorrect class index {cls}' |
|
|
125 |
|
|
|
126 |
# Write YOLO label |
|
|
127 |
if id not in shapes: |
|
|
128 |
shapes[id] = Image.open(file).size |
|
|
129 |
box = xyxy2xywhn(box[None].astype(np.float), w=shapes[id][0], h=shapes[id][1], clip=True) |
|
|
130 |
with open((labels / id).with_suffix('.txt'), 'a') as f: |
|
|
131 |
f.write(f"{cls} {' '.join(f'{x:.6f}' for x in box[0])}\n") # write label.txt |
|
|
132 |
except Exception as e: |
|
|
133 |
print(f'WARNING: skipping one label for {file}: {e}') |
|
|
134 |
|
|
|
135 |
|
|
|
136 |
# Download manually from https://challenge.xviewdataset.org |
|
|
137 |
dir = Path(yaml['path']) # dataset root dir |
|
|
138 |
# urls = ['https://d307kc0mrhucc3.cloudfront.net/train_labels.zip', # train labels |
|
|
139 |
# 'https://d307kc0mrhucc3.cloudfront.net/train_images.zip', # 15G, 847 train images |
|
|
140 |
# 'https://d307kc0mrhucc3.cloudfront.net/val_images.zip'] # 5G, 282 val images (no labels) |
|
|
141 |
# download(urls, dir=dir, delete=False) |
|
|
142 |
|
|
|
143 |
# Convert labels |
|
|
144 |
convert_labels(dir / 'xView_train.geojson') |
|
|
145 |
|
|
|
146 |
# Move images |
|
|
147 |
images = Path(dir / 'images') |
|
|
148 |
images.mkdir(parents=True, exist_ok=True) |
|
|
149 |
Path(dir / 'train_images').rename(dir / 'images' / 'train') |
|
|
150 |
Path(dir / 'val_images').rename(dir / 'images' / 'val') |
|
|
151 |
|
|
|
152 |
# Split |
|
|
153 |
autosplit(dir / 'images' / 'train') |