From 8dedd2610f33dc1d7849f518ed5dc1cf1054441b Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Fri, 25 Jun 2021 20:36:15 +0200 Subject: [PATCH 1/6] update clip_coords for numpy --- data/xView.yaml | 98 ++++++++++++++++++++++++++++++++++++++++++++++++ utils/general.py | 18 ++++++--- 2 files changed, 111 insertions(+), 5 deletions(-) create mode 100644 data/xView.yaml diff --git a/data/xView.yaml b/data/xView.yaml new file mode 100644 index 000000000000..f8165b155892 --- /dev/null +++ b/data/xView.yaml @@ -0,0 +1,98 @@ +# xView 2018 dataset http://xviewdataset.org +# Train command: python train.py --data xView.yaml +# Default dataset location is next to YOLOv5: +# /parent +# /datasets/xView +# /yolov5 + + +# 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, ..] +path: ../datasets/xView # dataset root dir +train: images/train # train images (relative to 'path') 847 images +val: images/val # train images (relative to 'path') 282 images + +# Classes +nc: 60 # number of classes +names: [ 'Fixed-wing Aircraft', 'Small Aircraft', 'Cargo Plane', 'Helicopter', 'Passenger Vehicle', 'Small Car', 'Bus', + 'Pickup Truck', 'Utility Truck', 'Truck', 'Cargo Truck', 'Truck w/Box', 'Truck Tractor', 'Trailer', + 'Truck w/Flatbed', 'Truck w/Liquid', 'Crane Truck', 'Railway Vehicle', 'Passenger Car', 'Cargo Car', + 'Flat Car', 'Tank car', 'Locomotive', 'Maritime Vessel', 'Motorboat', 'Sailboat', 'Tugboat', 'Barge', + 'Fishing Vessel', 'Ferry', 'Yacht', 'Container Ship', 'Oil Tanker', 'Engineering Vehicle', 'Tower crane', + 'Container Crane', 'Reach Stacker', 'Straddle Carrier', 'Mobile Crane', 'Dump Truck', 'Haul Truck', + 'Scraper/Tractor', 'Front loader/Bulldozer', 'Excavator', 'Cement Mixer', 'Ground Grader', 'Hut/Tent', 'Shed', + 'Building', 'Aircraft Hangar', 'Damaged Building', 'Facility', 'Construction Site', 'Vehicle Lot', 'Helipad', + 'Storage Tank', 'Shipping container lot', 'Shipping Container', 'Pylon', 'Tower' ] # class names + + +# Download script/URL (optional) +download: | + import json + import os + from pathlib import Path + + import numpy as np + from PIL import Image + from tqdm import tqdm + + from utils.general import xyxy2xywhn + + + def convert_labels(fname=Path('xView/xView_train.geojson')): + # Convert xView geoJSON labels to YOLO format + path = fname.parent + with open(fname) as f: + print(f'Loading {fname}...') + data = json.load(f) + + # Make dirs + labels = Path(path / 'labels' / 'train') + os.system(f'rm -rf {labels}') + labels.mkdir(parents=True, exist_ok=True) + + # xView classes 11-94 to 0-59 + 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, + 12, 13, 14, 15, -1, -1, 16, 17, 18, 19, 20, 21, 22, -1, 23, 24, 25, -1, 26, 27, -1, 28, -1, 29, + 30, 31, 32, 33, 34, 35, 36, 37, -1, 38, 39, 40, 41, 42, 43, 44, 45, -1, -1, -1, -1, 46, 47, 48, + 49, -1, 50, 51, -1, 52, -1, -1, -1, 53, 54, -1, 55, -1, -1, 56, -1, 57, -1, 58, 59] + + shapes = {} + for feature in tqdm(data['features'], desc=f'Converting {fname}'): + p = feature['properties'] + if p['bounds_imcoords']: + id = p['image_id'] + file = path / 'train_images' / id + if file.exists(): # 1395.tif missing + try: + box = np.array([int(num) for num in p['bounds_imcoords'].split(",")]) + assert box.shape[0] == 4, f'incorrect box shape {box.shape[0]}' + cls = p['type_id'] + cls = xview_class2index[int(cls)] # xView class to 0-60 + assert 59 >= cls >= 0, f'incorrect class index {cls}' + + # Write YOLO label + if id not in shapes: + shapes[id] = Image.open(file).size + box = xyxy2xywhn(box[None].astype(np.float), w=shapes[id][0], h=shapes[id][1], + clip=True) # to YOLO format + with open((labels / id).with_suffix('.txt'), 'a') as f: + f.write(f"{cls} {' '.join(f'{x:.6f}' for x in box[0])}\n") # write label.txt + except Exception as e: + print(f'WARNING: skipping one label for {file}: {e}') + + + # Download + dir = Path(yaml['path']) # dataset root dir + # urls = ['https://d307kc0mrhucc3.cloudfront.net/train_labels.zip', # train labels + # 'https://d307kc0mrhucc3.cloudfront.net/train_images.zip', # 15G, 847 train images + # 'https://d307kc0mrhucc3.cloudfront.net/val_images.zip'] # 5G, 282 val images (no labels) + # download(urls, dir=dir, delete=False) + + # Convert labels + convert_labels(dir / 'xView_train.geojson') + + # Move images + # images = Path(dir / 'images') + # images.mkdir(parents=True, exist_ok=True) + # Path(dir / 'train_images').rename(dir / 'images' / 'train') + # Path(dir / 'val_images').rename(dir / 'images' / 'val') + diff --git a/utils/general.py b/utils/general.py index 6a5b42f374e6..83eb95744678 100755 --- a/utils/general.py +++ b/utils/general.py @@ -393,8 +393,10 @@ def xywhn2xyxy(x, w=640, h=640, padw=0, padh=0): return y -def xyxy2xywhn(x, w=640, h=640): +def xyxy2xywhn(x, w=640, h=640, clip=False): # Convert nx4 boxes from [x1, y1, x2, y2] to [x, y, w, h] normalized where xy1=top-left, xy2=bottom-right + if clip: + clip_coords(x, (h, w)) # warning: inplace clip y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x) y[:, 0] = ((x[:, 0] + x[:, 2]) / 2) / w # x center y[:, 1] = ((x[:, 1] + x[:, 3]) / 2) / h # y center @@ -455,10 +457,16 @@ def scale_coords(img1_shape, coords, img0_shape, ratio_pad=None): def clip_coords(boxes, img_shape): # Clip bounding xyxy bounding boxes to image shape (height, width) - boxes[:, 0].clamp_(0, img_shape[1]) # x1 - boxes[:, 1].clamp_(0, img_shape[0]) # y1 - boxes[:, 2].clamp_(0, img_shape[1]) # x2 - boxes[:, 3].clamp_(0, img_shape[0]) # y2 + if isinstance(boxes, torch.Tensor): + boxes[:, 0].clamp_(0, img_shape[1]) # x1 + boxes[:, 1].clamp_(0, img_shape[0]) # y1 + boxes[:, 2].clamp_(0, img_shape[1]) # x2 + boxes[:, 3].clamp_(0, img_shape[0]) # y2 + else: # np.array + boxes[:, 0].clip(0, img_shape[1], out=boxes[:, 0]) # x1 + boxes[:, 1].clip(0, img_shape[0], out=boxes[:, 1]) # y1 + boxes[:, 2].clip(0, img_shape[1], out=boxes[:, 2]) # x2 + boxes[:, 3].clip(0, img_shape[0], out=boxes[:, 3]) # y2 def bbox_iou(box1, box2, x1y1x2y2=True, GIoU=False, DIoU=False, CIoU=False, eps=1e-7): From e100ffd43886f52b288fd4cb7f79f161cfeb924b Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Fri, 25 Jun 2021 20:40:49 +0200 Subject: [PATCH 2/6] uncomment --- data/xView.yaml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/data/xView.yaml b/data/xView.yaml index f8165b155892..3c133c0a1e39 100644 --- a/data/xView.yaml +++ b/data/xView.yaml @@ -91,8 +91,8 @@ download: | convert_labels(dir / 'xView_train.geojson') # Move images - # images = Path(dir / 'images') - # images.mkdir(parents=True, exist_ok=True) - # Path(dir / 'train_images').rename(dir / 'images' / 'train') - # Path(dir / 'val_images').rename(dir / 'images' / 'val') + images = Path(dir / 'images') + images.mkdir(parents=True, exist_ok=True) + Path(dir / 'train_images').rename(dir / 'images' / 'train') + Path(dir / 'val_images').rename(dir / 'images' / 'val') From b5f064e6db7d9520e66d8faf4114bc021a3589c5 Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Fri, 25 Jun 2021 20:53:06 +0200 Subject: [PATCH 3/6] cleanup --- data/xView.yaml | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/data/xView.yaml b/data/xView.yaml index 3c133c0a1e39..1c3d78be2bdf 100644 --- a/data/xView.yaml +++ b/data/xView.yaml @@ -24,7 +24,7 @@ names: [ 'Fixed-wing Aircraft', 'Small Aircraft', 'Cargo Plane', 'Helicopter', ' 'Storage Tank', 'Shipping container lot', 'Shipping Container', 'Pylon', 'Tower' ] # class names -# Download script/URL (optional) +# Download script/URL (optional) --------------------------------------------------------------------------------------- download: | import json import os @@ -34,7 +34,7 @@ download: | from PIL import Image from tqdm import tqdm - from utils.general import xyxy2xywhn + from utils.general import download, xyxy2xywhn def convert_labels(fname=Path('xView/xView_train.geojson')): @@ -51,9 +51,9 @@ download: | # xView classes 11-94 to 0-59 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, - 12, 13, 14, 15, -1, -1, 16, 17, 18, 19, 20, 21, 22, -1, 23, 24, 25, -1, 26, 27, -1, 28, -1, 29, - 30, 31, 32, 33, 34, 35, 36, 37, -1, 38, 39, 40, 41, 42, 43, 44, 45, -1, -1, -1, -1, 46, 47, 48, - 49, -1, 50, 51, -1, 52, -1, -1, -1, 53, 54, -1, 55, -1, -1, 56, -1, 57, -1, 58, 59] + 12, 13, 14, 15, -1, -1, 16, 17, 18, 19, 20, 21, 22, -1, 23, 24, 25, -1, 26, 27, -1, 28, -1, + 29, 30, 31, 32, 33, 34, 35, 36, 37, -1, 38, 39, 40, 41, 42, 43, 44, 45, -1, -1, -1, -1, 46, + 47, 48, 49, -1, 50, 51, -1, 52, -1, -1, -1, 53, 54, -1, 55, -1, -1, 56, -1, 57, -1, 58, 59] shapes = {} for feature in tqdm(data['features'], desc=f'Converting {fname}'): @@ -72,8 +72,7 @@ download: | # Write YOLO label if id not in shapes: shapes[id] = Image.open(file).size - box = xyxy2xywhn(box[None].astype(np.float), w=shapes[id][0], h=shapes[id][1], - clip=True) # to YOLO format + box = xyxy2xywhn(box[None].astype(np.float), w=shapes[id][0], h=shapes[id][1], clip=True) with open((labels / id).with_suffix('.txt'), 'a') as f: f.write(f"{cls} {' '.join(f'{x:.6f}' for x in box[0])}\n") # write label.txt except Exception as e: @@ -95,4 +94,3 @@ download: | images.mkdir(parents=True, exist_ok=True) Path(dir / 'train_images').rename(dir / 'images' / 'train') Path(dir / 'val_images').rename(dir / 'images' / 'val') - From c65d48ce638964693f4285c4b7aa21eceb3788d6 Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Fri, 25 Jun 2021 23:52:36 +0200 Subject: [PATCH 4/6] Add autosplits --- data/xView.yaml | 14 ++++++++++---- utils/datasets.py | 21 +++++++++++---------- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/data/xView.yaml b/data/xView.yaml index 1c3d78be2bdf..2c259d1ba94f 100644 --- a/data/xView.yaml +++ b/data/xView.yaml @@ -1,4 +1,5 @@ -# xView 2018 dataset http://xviewdataset.org +# xView 2018 dataset https://challenge.xviewdataset.org +# ----> NOTE: DOWNLOAD DATA MANUALLY from URL above and unzip to /datasets/xView before running train command below # Train command: python train.py --data xView.yaml # Default dataset location is next to YOLOv5: # /parent @@ -8,8 +9,8 @@ # 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, ..] path: ../datasets/xView # dataset root dir -train: images/train # train images (relative to 'path') 847 images -val: images/val # train images (relative to 'path') 282 images +train: images/autosplit_train.txt # train images (relative to 'path') 90% of 847 images +val: images/autosplit_val.txt # train images (relative to 'path') 10% of 847 images # Classes nc: 60 # number of classes @@ -34,6 +35,7 @@ download: | from PIL import Image from tqdm import tqdm + from utils.datasets import autosplit from utils.general import download, xyxy2xywhn @@ -79,7 +81,7 @@ download: | print(f'WARNING: skipping one label for {file}: {e}') - # Download + # Download manually from https://challenge.xviewdataset.org dir = Path(yaml['path']) # dataset root dir # urls = ['https://d307kc0mrhucc3.cloudfront.net/train_labels.zip', # train labels # 'https://d307kc0mrhucc3.cloudfront.net/train_images.zip', # 15G, 847 train images @@ -94,3 +96,7 @@ download: | images.mkdir(parents=True, exist_ok=True) Path(dir / 'train_images').rename(dir / 'images' / 'train') Path(dir / 'val_images').rename(dir / 'images' / 'val') + + # Split + autosplit(dir / 'images' / 'train') + ` \ No newline at end of file diff --git a/utils/datasets.py b/utils/datasets.py index eac0c7834308..4658dc524be0 100755 --- a/utils/datasets.py +++ b/utils/datasets.py @@ -985,7 +985,7 @@ def create_folder(path='./new'): os.makedirs(path) # make new output folder -def flatten_recursive(path='../coco128'): +def flatten_recursive(path='../datasets/coco128'): # Flatten a recursive directory by bringing all files to top level new_path = Path(path + '_flat') create_folder(new_path) @@ -993,7 +993,7 @@ def flatten_recursive(path='../coco128'): shutil.copyfile(file, new_path / Path(file).name) -def extract_boxes(path='../coco128/'): # from utils.datasets import *; extract_boxes('../coco128') +def extract_boxes(path='../datasets/coco128'): # from utils.datasets import *; extract_boxes() # Convert detection dataset into classification dataset, with one directory per class path = Path(path) # images dir @@ -1028,27 +1028,28 @@ def extract_boxes(path='../coco128/'): # from utils.datasets import *; extract_ assert cv2.imwrite(str(f), im[b[1]:b[3], b[0]:b[2]]), f'box failure in {f}' -def autosplit(path='../coco128', weights=(0.9, 0.1, 0.0), annotated_only=False): +def autosplit(path='../datasets/coco128/images', weights=(0.9, 0.1, 0.0), annotated_only=False): """ Autosplit a dataset into train/val/test splits and save path/autosplit_*.txt files - Usage: from utils.datasets import *; autosplit('../coco128') + Usage: from utils.datasets import *; autosplit() Arguments - path: Path to images directory - weights: Train, val, test weights (list) - annotated_only: Only use images with an annotated txt file + path: Path to images directory + weights: Train, val, test weights (list, tuple) + annotated_only: Only use images with an annotated txt file """ path = Path(path) # images dir files = sum([list(path.rglob(f"*.{img_ext}")) for img_ext in img_formats], []) # image files only n = len(files) # number of files + random.seed(0) # for reproducibility indices = random.choices([0, 1, 2], weights=weights, k=n) # assign each image to a split txt = ['autosplit_train.txt', 'autosplit_val.txt', 'autosplit_test.txt'] # 3 txt files - [(path / x).unlink() for x in txt if (path / x).exists()] # remove existing + [(path.parent / x).unlink(missing_ok=True) for x in txt] # remove existing print(f'Autosplitting images from {path}' + ', using *.txt labeled images only' * annotated_only) for i, img in tqdm(zip(indices, files), total=n): if not annotated_only or Path(img2label_paths([str(img)])[0]).exists(): # check label - with open(path / txt[i], 'a') as f: - f.write(str(img) + '\n') # add image to txt file + with open(path.parent / txt[i], 'a') as f: + f.write('./' + img.relative_to(path.parent).as_posix() + '\n') # add image to txt file def verify_image_label(args): From db2194a5485187ebda66d3d9d184109330c5260a Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Fri, 25 Jun 2021 23:54:40 +0200 Subject: [PATCH 5/6] fix --- data/xView.yaml | 1 - 1 file changed, 1 deletion(-) diff --git a/data/xView.yaml b/data/xView.yaml index 2c259d1ba94f..4e2be2686dbb 100644 --- a/data/xView.yaml +++ b/data/xView.yaml @@ -99,4 +99,3 @@ download: | # Split autosplit(dir / 'images' / 'train') - ` \ No newline at end of file From fa7b1f843f2f31856dd8ada7a008050eccb3d778 Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Sat, 26 Jun 2021 00:28:48 +0200 Subject: [PATCH 6/6] cleanup --- data/xView.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/data/xView.yaml b/data/xView.yaml index 4e2be2686dbb..5212193a0bf0 100644 --- a/data/xView.yaml +++ b/data/xView.yaml @@ -9,8 +9,8 @@ # 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, ..] path: ../datasets/xView # dataset root dir -train: images/autosplit_train.txt # train images (relative to 'path') 90% of 847 images -val: images/autosplit_val.txt # train images (relative to 'path') 10% of 847 images +train: images/autosplit_train.txt # train images (relative to 'path') 90% of 847 train images +val: images/autosplit_val.txt # train images (relative to 'path') 10% of 847 train images # Classes nc: 60 # number of classes