NGA xView 2018 Dataset Auto-Download (#3775)
* update clip_coords for numpy * uncomment * cleanup * Add autosplits * fix * cleanup
This commit is contained in:
1 parent
ffb6e11050
commit
f89941711c
3 files changed
+125
-15
No files matched your search
+11
-10
@@ -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):
|
||||
|
||||
+13
-5
@@ -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):
|
||||
|
||||
Reference in new issue
Block a user