Download dataset_split_generator.py from GrainsPolito/BBBicycles: direct link, hf CLI and curl.
- Browser
- Download file 17.9 kB
-
https://huggingface.co/datasets/GrainsPolito/BBBicycles/resolve/main/dataset_split_generator.py
- Command line
-
hf download hf://datasets/GrainsPolito/BBBicycles/dataset_split_generator.py
-
curl -L -o dataset_split_generator.py https://huggingface.co/datasets/GrainsPolito/BBBicycles/resolve/main/dataset_split_generator.py
17.9 kB
| from os import listdir, walk | |
| from os.path import isfile, isdir, join, splitext, exists, getmtime | |
| from random import seed, randint, choice | |
| import re | |
| import json | |
| import datetime | |
| import argparse | |
| parser = argparse.ArgumentParser(description='Generate BBBicycles split.') | |
| parser.add_argument('-p', '--path', type=str, required=True, | |
| help='directory containing the ID folders') | |
| args = parser.parse_args() | |
| path = args.path | |
| random_seed = seed(1337) | |
| train_val_bike_type_split = 10 | |
| img_regex = re.compile('(^img.\d*[.]png$)') | |
| dir_regex = re.compile('(^\w+_)') | |
| train = open("bike_train.txt", "w") | |
| query_v = open("bike_query_val.txt", "w") | |
| galley_v = open("bike_gallery_val.txt", "w") | |
| query_t = open("bike_query_test.txt", "w") | |
| galley_t = open("bike_gallery_test.txt", "w") | |
| num_ids = 0 | |
| num_imgs = 0 | |
| num_damaged_imgs = 0 | |
| num_broken_imgs = 0 | |
| num_bent_imgs = 0 | |
| num_missingpart_imgs = 0 | |
| nums_missingpart_imgs = [0 for i in range(5)] | |
| models_dist = {} | |
| num_train_ids = 0 | |
| num_train_imgs = 0 | |
| num_damaged_train_imgs = 0 | |
| num_broken_train_imgs = 0 | |
| num_bent_train_imgs = 0 | |
| num_missingpart_train_imgs = 0 | |
| nums_missingpart_train_imgs = [0 for i in range(5)] | |
| models_dist_train = {} | |
| num_val_ids = 0 | |
| num_val_imgs = 0 | |
| num_damaged_val_imgs = 0 | |
| num_broken_val_imgs = 0 | |
| num_bent_val_imgs = 0 | |
| num_missingpart_val_imgs = 0 | |
| nums_missingpart_val_imgs = [0 for i in range(5)] | |
| models_dist_val = {} | |
| num_test_ids = 0 | |
| num_test_imgs = 0 | |
| num_damaged_test_imgs = 0 | |
| num_broken_test_imgs = 0 | |
| num_bent_test_imgs = 0 | |
| num_missingpart_test_imgs = 0 | |
| nums_missingpart_test_imgs = [0 for i in range(5)] | |
| models_dist_test = {} | |
| for id, bike in enumerate(listdir(path)): | |
| if isdir(join(path, bike)) and dir_regex.match(bike) and exists(join(path, bike, "before")) and len(listdir(join(path, bike, "before"))) != 0 and exists(join(path, bike, "after")) and len(listdir(join(path, bike, "after"))) != 0 and exists(join(path, bike, "fixed_data.json")): | |
| #aggiungere: prendere json dell'identità per estrarre info | |
| json_fixed = open(join(path, bike, 'fixed_data.json'),) | |
| data_fixed = json.load(json_fixed) | |
| if str(data_fixed['Bike Type']) not in models_dist: | |
| models_dist[str(data_fixed['Bike Type'])] = {str(data_fixed['Model']): 1} | |
| elif str(data_fixed['Model']) not in models_dist[str(data_fixed['Bike Type'])]: | |
| models_dist[str(data_fixed['Bike Type'])][str(data_fixed['Model'])] = 1 | |
| else: | |
| models_dist[str(data_fixed['Bike Type'])][str(data_fixed['Model'])] = models_dist[str(data_fixed['Bike Type'])][str(data_fixed['Model'])] + 1 | |
| c=0 | |
| num_ids = num_ids + 1 | |
| for type in ["before", "after"]: | |
| for file in listdir(join(path, bike, type)): | |
| if img_regex.match(file): | |
| if exists(join(path, bike, type, splitext(file)[0] + '_variable.json')): | |
| json_var = open(join(path, bike, type, splitext(file)[0] + '_variable.json'),) | |
| data = json.load(json_var) | |
| dmgid = 0 if type == "before" else int(data["Damage Type"]) | |
| missid = "00000" if type == "before" else str(data["Removed Parts"]) | |
| json_var.close() | |
| if dmgid != 0: | |
| num_damaged_imgs = num_damaged_imgs + 1 | |
| if dmgid == 2 or dmgid == 3: | |
| num_broken_imgs = num_broken_imgs + 1 | |
| if dmgid == 1 or dmgid == 3: | |
| num_bent_imgs = num_bent_imgs + 1 | |
| if missid != "00000" : | |
| num_missingpart_imgs = num_missingpart_imgs + 1 | |
| for i in range(5): | |
| if missid[i] == "1": | |
| nums_missingpart_imgs[i] = nums_missingpart_imgs[i] + 1 | |
| num_imgs = num_imgs + 1 | |
| c=c+1 | |
| else: | |
| print(bike) | |
| if c != 14: | |
| print(bike) | |
| if str(data_fixed['Model']) in ['mfactory ', 'ghost', 'oldbike', 'rondo', 'verdona']: | |
| #Train | |
| if str(data_fixed['Bike Type']) not in models_dist_train: | |
| models_dist_train[str(data_fixed['Bike Type'])] = {str(data_fixed['Model']): 1} | |
| elif str(data_fixed['Model']) not in models_dist_train[str(data_fixed['Bike Type'])]: | |
| models_dist_train[str(data_fixed['Bike Type'])][str(data_fixed['Model'])] = 1 | |
| else: | |
| models_dist_train[str(data_fixed['Bike Type'])][str(data_fixed['Model'])] = models_dist_train[str(data_fixed['Bike Type'])][str(data_fixed['Model'])] + 1 | |
| num_train_ids = num_train_ids + 1 | |
| for type in ["before", "after"]: | |
| for file in listdir(join(path, bike, type)): | |
| if img_regex.match(file) and exists(join(path, bike, type, splitext(file)[0] + '_variable.json')): | |
| img_path = join(bike, type, file) | |
| json_var = open(join(path, bike, type, splitext(file)[0] + '_variable.json'),) | |
| data = json.load(json_var) | |
| camid = int(data["Focal Length"]) | |
| viewid = int(data["Viewing Side"]) | |
| dmgid = 0 if type == "before" else int(data["Damage Type"]) | |
| missid = "00000" if type == "before" else str(data["Removed Parts"]) | |
| train.write("{} {} {} {} {} {}\n".format(img_path, id, camid, viewid, dmgid, missid)) | |
| json_var.close() | |
| if dmgid != 0: | |
| num_damaged_train_imgs = num_damaged_train_imgs + 1 | |
| if dmgid == 2 or dmgid == 3: | |
| num_broken_train_imgs = num_broken_train_imgs + 1 | |
| if dmgid == 1 or dmgid == 3: | |
| num_bent_train_imgs = num_bent_train_imgs + 1 | |
| if missid != "00000" : | |
| num_missingpart_train_imgs = num_missingpart_train_imgs + 1 | |
| for i in range(5): | |
| if missid[i] == "1": | |
| nums_missingpart_train_imgs[i] = nums_missingpart_train_imgs[i] + 1 | |
| num_train_imgs = num_train_imgs + 1 | |
| else: | |
| if str(data_fixed['Model']) not in ['mirage', 'gbike', 'enduro']: | |
| if str(data_fixed['Model']) not in ['becane', 'btwin', 'croad'] and randint(0, 100) > train_val_bike_type_split: | |
| if str(data_fixed['Bike Type']) not in models_dist_train: | |
| models_dist_train[str(data_fixed['Bike Type'])] = {str(data_fixed['Model']): 1} | |
| elif str(data_fixed['Model']) not in models_dist_train[str(data_fixed['Bike Type'])]: | |
| models_dist_train[str(data_fixed['Bike Type'])][str(data_fixed['Model'])] = 1 | |
| else: | |
| models_dist_train[str(data_fixed['Bike Type'])][str(data_fixed['Model'])] = models_dist_train[str(data_fixed['Bike Type'])][str(data_fixed['Model'])] + 1 | |
| num_train_ids = num_train_ids + 1 | |
| for type in ["before", "after"]: | |
| for file in listdir(join(path, bike, type)): | |
| if img_regex.match(file) and exists(join(path, bike, type, splitext(file)[0] + '_variable.json')): | |
| img_path = join(bike, type, file) | |
| json_var = open(join(path, bike, type, splitext(file)[0] + '_variable.json'),) | |
| data = json.load(json_var) | |
| camid = int(data["Focal Length"]) | |
| viewid = int(data["Viewing Side"]) | |
| dmgid = 0 if type == "before" else int(data["Damage Type"]) | |
| missid = "00000" if type == "before" else str(data["Removed Parts"]) | |
| train.write("{} {} {} {} {} {}\n".format(img_path, id, camid, viewid, dmgid, missid)) | |
| json_var.close() | |
| if dmgid != 0: | |
| num_damaged_train_imgs = num_damaged_train_imgs + 1 | |
| if dmgid == 2 or dmgid == 3: | |
| num_broken_train_imgs = num_broken_train_imgs + 1 | |
| if dmgid == 1 or dmgid == 3: | |
| num_bent_train_imgs = num_bent_train_imgs + 1 | |
| if missid != "00000" : | |
| num_missingpart_train_imgs = num_missingpart_train_imgs + 1 | |
| for i in range(5): | |
| if missid[i] == "1": | |
| nums_missingpart_train_imgs[i] = nums_missingpart_train_imgs[i] + 1 | |
| num_train_imgs = num_train_imgs + 1 | |
| else: | |
| #Val | |
| if str(data_fixed['Bike Type']) not in models_dist_val: | |
| models_dist_val[str(data_fixed['Bike Type'])] = {str(data_fixed['Model']): 1} | |
| elif str(data_fixed['Model']) not in models_dist_val[str(data_fixed['Bike Type'])]: | |
| models_dist_val[str(data_fixed['Bike Type'])][str(data_fixed['Model'])] = 1 | |
| else: | |
| models_dist_val[str(data_fixed['Bike Type'])][str(data_fixed['Model'])] = models_dist_val[str(data_fixed['Bike Type'])][str(data_fixed['Model'])] + 1 | |
| num_val_ids = num_val_ids + 1 | |
| for type in ["before", "after"]: | |
| files = [f for f in listdir(join(path, bike, type)) if img_regex.match(f) and exists(join(path, bike, type, splitext(f)[0] + '_variable.json'))] | |
| file = choice(files) | |
| img_path = join(bike, type, file) | |
| json_var = open(join(path, bike, type, splitext(file)[0] + '_variable.json'),) | |
| data = json.load(json_var) | |
| camid = int(data["Focal Length"]) | |
| viewid = int(data["Viewing Side"]) | |
| dmgid = 0 if type == "before" else int(data["Damage Type"]) | |
| missid = "00000" if type == "before" else str(data["Removed Parts"]) | |
| if type == "before" : | |
| galley_v.write("{} {} {} {} {} {}\n".format(img_path, id, camid, viewid, dmgid, missid)) | |
| else: | |
| query_v.write("{} {} {} {} {} {}\n".format(img_path, id, camid, viewid, dmgid, missid)) | |
| json_var.close() | |
| if dmgid != 0: | |
| num_damaged_val_imgs = num_damaged_val_imgs + 1 | |
| if dmgid == 2 or dmgid == 3: | |
| num_broken_val_imgs = num_broken_val_imgs + 1 | |
| if dmgid == 1 or dmgid == 3: | |
| num_bent_val_imgs = num_bent_val_imgs + 1 | |
| if missid != "00000" : | |
| num_missingpart_val_imgs = num_missingpart_val_imgs + 1 | |
| for i in range(5): | |
| if missid[i] == "1": | |
| nums_missingpart_val_imgs[i] = nums_missingpart_val_imgs[i] + 1 | |
| num_val_imgs = num_val_imgs + 1 | |
| else: | |
| #Test | |
| if str(data_fixed['Bike Type']) not in models_dist_test: | |
| models_dist_test[str(data_fixed['Bike Type'])] = {str(data_fixed['Model']): 1} | |
| elif str(data_fixed['Model']) not in models_dist_test[str(data_fixed['Bike Type'])]: | |
| models_dist_test[str(data_fixed['Bike Type'])][str(data_fixed['Model'])] = 1 | |
| else: | |
| models_dist_test[str(data_fixed['Bike Type'])][str(data_fixed['Model'])] = models_dist_test[str(data_fixed['Bike Type'])][str(data_fixed['Model'])] + 1 | |
| num_test_ids = num_test_ids + 1 | |
| for type in ["before", "after"]: | |
| files = [f for f in listdir(join(path, bike, type)) if img_regex.match(f) and exists(join(path, bike, type, splitext(f)[0] + '_variable.json'))] | |
| if not files: | |
| print(join(path, bike, type)) | |
| file = choice(files) | |
| img_path = join(bike, type, file) | |
| json_var = open(join(path, bike, type, splitext(file)[0] + '_variable.json'),) | |
| data = json.load(json_var) | |
| camid = int(data["Focal Length"]) | |
| viewid = int(data["Viewing Side"]) | |
| dmgid = 0 if type == "before" else int(data["Damage Type"]) | |
| missid = "00000" if type == "before" else str(data["Removed Parts"]) | |
| if type == "before" : | |
| galley_t.write("{} {} {} {} {} {}\n".format(img_path, id, camid, viewid, dmgid, missid)) | |
| else: | |
| query_t.write("{} {} {} {} {} {}\n".format(img_path, id, camid, viewid, dmgid, missid)) | |
| json_var.close() | |
| if dmgid != 0: | |
| num_damaged_test_imgs = num_damaged_test_imgs + 1 | |
| if dmgid == 2 or dmgid == 3: | |
| num_broken_test_imgs = num_broken_test_imgs + 1 | |
| if dmgid == 1 or dmgid == 3: | |
| num_bent_test_imgs = num_bent_test_imgs + 1 | |
| if missid != "00000" : | |
| num_missingpart_test_imgs = num_missingpart_test_imgs + 1 | |
| for i in range(5): | |
| if missid[i] == "1": | |
| nums_missingpart_test_imgs[i] = nums_missingpart_test_imgs[i] + 1 | |
| num_test_imgs = num_test_imgs + 1 | |
| json_fixed.close() | |
| else: | |
| print(bike) | |
| train.close() | |
| query_v.close() | |
| galley_v.close() | |
| query_t.close() | |
| galley_t.close() | |
| data = {} | |
| data["General"] = [] | |
| data["General"].append({ | |
| 'Num IDs': num_ids, | |
| 'Num Bike types': len(models_dist.keys()), | |
| 'Num Models': sum(len(models_dist[k].keys()) for k in models_dist.keys()), | |
| 'Num images': num_imgs, | |
| 'Num bent images': num_bent_imgs, | |
| 'Num broken images': num_broken_imgs, | |
| 'Num damaged images': num_damaged_imgs, | |
| 'Num images with missing parts': num_missingpart_imgs, | |
| 'Num images with missing Front Wheel': nums_missingpart_imgs[0], | |
| 'Num images with missing Rear Wheel': nums_missingpart_imgs[1], | |
| 'Num images with missing Seat': nums_missingpart_imgs[2], | |
| 'Num images with missing Handlebar': nums_missingpart_imgs[3], | |
| 'Num images with missing Pedals': nums_missingpart_imgs[4] | |
| }) | |
| data["Train"] = [] | |
| data["Train"].append({ | |
| 'Num IDs': num_train_ids, | |
| 'Num Bike types': len(models_dist_train.keys()), | |
| 'Num Models': sum(len(models_dist_train[k].keys()) for k in models_dist_train.keys()), | |
| 'Num images': num_train_imgs, | |
| 'Num bent images': num_bent_train_imgs, | |
| 'Num broken images': num_broken_train_imgs, | |
| 'Num damaged images': num_damaged_train_imgs, | |
| 'Num images with missing parts': num_missingpart_train_imgs, | |
| 'Num images with missing Front Wheel': nums_missingpart_train_imgs[0], | |
| 'Num images with missing Rear Wheel': nums_missingpart_train_imgs[1], | |
| 'Num images with missing Seat': nums_missingpart_train_imgs[2], | |
| 'Num images with missing Handlebar': nums_missingpart_train_imgs[3], | |
| 'Num images with missing Pedals': nums_missingpart_train_imgs[4] | |
| }) | |
| data["Validation"] = [] | |
| data["Validation"].append({ | |
| 'Num IDs': num_val_ids, | |
| 'Num Bike types': len(models_dist_val.keys()), | |
| 'Num Models': sum(len(models_dist_val[k].keys()) for k in models_dist_val.keys()), | |
| 'Num images': num_val_imgs, | |
| 'Num bent images': num_bent_val_imgs, | |
| 'Num broken images': num_broken_val_imgs, | |
| 'Num damaged images': num_damaged_val_imgs, | |
| 'Num images with missing parts': num_missingpart_val_imgs, | |
| 'Num images with missing Front Wheel': nums_missingpart_val_imgs[0], | |
| 'Num images with missing Rear Wheel': nums_missingpart_val_imgs[1], | |
| 'Num images with missing Seat': nums_missingpart_val_imgs[2], | |
| 'Num images with missing Handlebar': nums_missingpart_val_imgs[3], | |
| 'Num images with missing Pedals': nums_missingpart_val_imgs[4] | |
| }) | |
| data["Test"] = [] | |
| data["Test"].append({ | |
| 'Num IDs': num_test_ids, | |
| 'Num Bike types': len(models_dist_test.keys()), | |
| 'Num Models': sum(len(models_dist_test[k].keys()) for k in models_dist_test.keys()), | |
| 'Num images': num_test_imgs, | |
| 'Num bent images': num_bent_test_imgs, | |
| 'Num broken images': num_broken_test_imgs, | |
| 'Num damaged images': num_damaged_test_imgs, | |
| 'Num images with missing parts': num_missingpart_test_imgs, | |
| 'Num images with missing Front Wheel': nums_missingpart_test_imgs[0], | |
| 'Num images with missing Rear Wheel': nums_missingpart_test_imgs[1], | |
| 'Num images with missing Seat': nums_missingpart_test_imgs[2], | |
| 'Num images with missing Handlebar': nums_missingpart_test_imgs[3], | |
| 'Num images with missing Pedals': nums_missingpart_test_imgs[4] | |
| }) | |
| with open('bike_current_split_stats.json', 'w', encoding='utf-8') as f: | |
| json.dump(data, f, ensure_ascii=False, indent=4) | |