diff --git a/README.md b/README.md index 1a1bf7c..40d49f8 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,18 @@ python generate_IN100.py \ Note: Replace `train` with `val` to generate ImageNet-100 val data as well +Corruption Benchmarks: + +``` +python generate_IN100.py \ + --source_folder /path/to/ImageNet-1K-c-(bar) data + --target_folder /path/to/ImageNet-100-c-(bar) data +``` + +This lets you create the same class IN-100-c and IN-100-c-bar dataset. Those are the corrupted version of the testset, with 19/10 different corruptions and 5 severity levels each, that have to be looped over. Obtain information towards the corruption benchmarks from: +ImagenNet-c: https://github.com/hendrycks/robustness +ImageNet-c-bar: https://github.com/facebookresearch/augmentation-corruption + ## Training ResNets on ImageNet-100 The implementation of training and validation code can be used in main_IN100.py, and run it for the usage. @@ -75,4 +87,4 @@ If you use this toolbox in your work, please cite this project. ## Acknowledgements -Part of this code is based on [HobbitLong/SupContrast](https://github.com/HobbitLong/SupContrast). \ No newline at end of file +Part of this code is based on [HobbitLong/SupContrast](https://github.com/HobbitLong/SupContrast). diff --git a/generate_IN100-c.py b/generate_IN100-c.py new file mode 100644 index 0000000..50daf7a --- /dev/null +++ b/generate_IN100-c.py @@ -0,0 +1,53 @@ +import os +import shutil +import argparse + + +def parse_option(): + parser = argparse.ArgumentParser('argument for generating ImageNet-100') + + parser.add_argument('--source_folder', type=str, + default='', help='folder of ImageNet-1K dataset') + parser.add_argument('--target_folder', type=str, + default='', help='folder of ImageNet-100 dataset') + parser.add_argument('--target_class', type=str, + default='IN100.txt', help='class file of ImageNet-100') + + opt = parser.parse_args() + + return opt + +f = [] +def generate_data(source_folder, target_folder, target_class): + + txt_data = open(target_class, "r") + for ids, txt in enumerate(txt_data): + s = str(txt.split('\n')[0]) + f.append(s) + + # Loop over corruption type folders + for corruption in os.listdir(source_folder): + corruption_path = os.path.join(source_folder, corruption) + if os.path.isdir(corruption_path): + # Loop over severity levels 1-5 + for severity in os.listdir(corruption_path): + severity_path = os.path.join(corruption_path, severity) + if os.path.isdir(severity_path): + # Create corresponding folders in target directory + target_corruption = os.path.join(target_folder, corruption) + target_severity = os.path.join(target_corruption, severity) + os.makedirs(target_severity, exist_ok=True) + + # Loop over class folders + for dirs in os.listdir(severity_path): + for tg_class in f: + if dirs == tg_class: + print(f'{dirs} is transferred from {corruption}/{severity}') + src_path = os.path.join(severity_path, dirs) + dst_path = os.path.join(target_severity, dirs) + shutil.copytree(src_path, dst_path) + + +opt = parse_option() +generate_data(opt.source_folder, opt.target_folder, opt.target_class) +