{"id":13737857,"url":"https://github.com/boschresearch/rince","last_synced_at":"2025-05-08T15:31:52.591Z","repository":{"id":45204298,"uuid":"452829886","full_name":"boschresearch/rince","owner":"boschresearch","description":"This is the code accompanying the AAAI 2022 paper \"Ranking Info Noise Contrastive Estimation: Boosting Contrastive Learning via Ranked Positives\" https://arxiv.org/abs/2201.11736 . The method allows you to use additional ranking information for representation learning.","archived":true,"fork":false,"pushed_at":"2022-08-12T12:12:10.000Z","size":14403,"stargazers_count":24,"open_issues_count":0,"forks_count":4,"subscribers_count":7,"default_branch":"cifar","last_synced_at":"2024-11-15T06:32:38.420Z","etag":null,"topics":["classification","contrastive-learning","out-of-distribution-detection","paper-resource","representation-learning","self-supervised-learning","video-representation-learning"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"agpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/boschresearch.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-01-27T20:14:50.000Z","updated_at":"2024-07-10T06:25:36.000Z","dependencies_parsed_at":"2022-09-10T23:02:37.050Z","dependency_job_id":null,"html_url":"https://github.com/boschresearch/rince","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/boschresearch%2Frince","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/boschresearch%2Frince/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/boschresearch%2Frince/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/boschresearch%2Frince/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/boschresearch","download_url":"https://codeload.github.com/boschresearch/rince/tar.gz/refs/heads/cifar","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253096167,"owners_count":21853549,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["classification","contrastive-learning","out-of-distribution-detection","paper-resource","representation-learning","self-supervised-learning","video-representation-learning"],"created_at":"2024-08-03T03:02:03.513Z","updated_at":"2025-05-08T15:31:52.576Z","avatar_url":"https://github.com/boschresearch.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"# Ranking Info Noise Contrastive Estimation: Boosting Contrastive Learning via Ranked Positives\r\n\r\nOfficial PyTorch implementation of the AAAI 2022 paper [Ranking Info Noise Contrastive Estimation: Boosting Contrastive Learning via Ranked Positives](https://arxiv.org/abs/2201.11736). The code allows the users to reproduce and extend the results reported in the study.\r\n\r\n# Overview\r\n\r\nThis repository provides the code for RINCE, a contrastive loss that can exploit ranking information, such that same samples are very close and similar samples close in the embedding space (see Figure below).\r\n\r\n![RINCE teaser](figures/RINCE_teaser.png)\r\n\r\n# Talks\r\n\u003cdiv align=\"center\"\u003e\r\n\u003ctable border=\"0\"\u003e\r\n \u003ctr\u003e\r\n\u003ctd\u003e A short talk can be found here:\u003c/td\u003e\r\n\u003ctd\u003e The full talk is available here:\u003c/td\u003e\r\n \u003c/tr\u003e\r\n\u003ctr\u003e\r\n\u003ctd\u003e\u003ca href=\"https://www.youtube.com/watch?v=JYhN9Uw1-Ww\"\u003e\u003cimg src=\"https://img.youtube.com/vi/JYhN9Uw1-Ww/0.jpg\" alt=\"Click to see the short talk\" width=\"90%\"\u003e\u003c/a\u003e\u003c/td\u003e\r\n\r\n\u003ctd\u003e\u003ca href=\"https://www.youtube.com/watch?v=RV3CAvmsCSc\"\u003e\u003cimg src=\"https://img.youtube.com/vi/RV3CAvmsCSc/0.jpg\" alt=\"Click to see the full talk\" width=\"90%\"\u003e\u003c/a\u003e\u003c/td\u003e\r\n\r\n \u003c/tr\u003e\r\n\u003c/table\u003e\r\n\u003c/div\u003e\r\n\r\n## Setup\r\n\r\nClone this repository.\r\n\r\n```buildoutcfg\r\ngit clone https://github.com/boschresearch/rince.git\r\ncd rince\r\n```\r\n\r\nInstall minimal set of requirements\r\n```\r\nconda env create --file requirements.yml python=3.7\r\nconda activate rince\r\n```\r\n\r\n## Datasets\r\n\r\nCifar should download automatically on demand.\r\nDownload TinyImageNet and split both the training and val set into folders with inliers and outliers. The class names for both sets can be found in\r\n```\r\n./datasets/tiny_imagenet_inliers.txt\r\n./datasets/tiny_imagenet_outliers.txt\r\n```\r\ncreate a symlink to your data directories in the folder ./data\r\n\r\n## Training\r\n\r\n### Rince\r\n\r\nTo train RINCE out-in on Cifar100 run\r\n\r\n```\r\npython main_supcon.py --batch_size 512 --num_workers 8 --print_freq 100 --data_folder ./data --dataset cifar100 --cosine --learning_rate 0.5 --min_tau 0.1 --max_tau 0.6 --similarity_threshold 0.5 --n_sim_classes 5 --use_supercategories True --use_same_and_similar_class True --mixed_out_in_log True --exp_name test_RINCE_out_in\r\n```\r\n\r\nfor RINCE in run:\r\n\r\n```\r\npython main_supcon.py --batch_size 512 --num_workers 8 --print_freq 100 --data_folder ./data --dataset cifar100 --cosine --learning_rate 0.5 --min_tau 0.1 --max_tau 0.6 --similarity_threshold 0.5 --n_sim_classes 5 --use_supercategories True --use_same_and_similar_class True --exp_name test_RINCE_in\r\n```\r\n\r\nfor RINCE out\r\n\r\n```\r\npython main_supcon.py --batch_size 512 --num_workers 8 --print_freq 100 --data_folder ./data --dataset cifar100 --cosine --learning_rate 0.5 --min_tau 0.1 --max_tau 0.6 --similarity_threshold 0.5 --n_sim_classes 5 --use_supercategories True --use_same_and_similar_class True --do_sum_in_log False --exp_name test_RINCE_out\r\n```\r\n\r\n### SCL\r\n\r\nSCL in\r\n```\r\npython main_supcon.py --batch_size 512 --num_workers 8 --print_freq 100 --data_folder ./data --dataset cifar100 --cosine --learning_rate 0.5 --min_tau 0.1 --max_tau 0.1 --similarity_threshold 0.0 --one_loss_per_rank False --n_sim_classes 1 --do_sum_in_log True --exp_name test_SCL_in\r\n```\r\n\r\nSCL out\r\n```\r\npython main_supcon.py --batch_size 512 --num_workers 8 --print_freq 100 --data_folder ./data --dataset cifar100 --cosine --learning_rate 0.5 --min_tau 0.1 --max_tau 0.1 --similarity_threshold 0.0 --one_loss_per_rank False --n_sim_classes 1 --do_sum_in_log False --exp_name test_SCL_out\r\n```\r\n\r\n\r\n### Cross-Entropy\r\n\r\n```\r\npython main_ce.py --batch_size 512 --num_workers 8 --print_freq 100 --data_folder ./data --dataset cifar100 --cosine --learning_rate 0.8 --exp_name cifar100_cross_entropy\r\n```\r\n\r\n## Testing the Model\r\n\r\n\r\nSet ```--ckpt ``` to the last checkpoint of the model.\r\n\r\nTo evaluate RINCE and SCL run for linear probe, retrieval on cifar 100 labels, retrieval on cifar 100 coarse labels, and OOD on cifar10 and TinyImageNet the respective script:\r\n\r\n```\r\npython main_linear.py --batch_size 512 --learning_rate 5 --dataset cifar100 --data_folder ./data --model resnet50 --model_type contrastive --ckpt $path2chkpt/last.pth\r\npython retrieval.py --dataset cifar100 --data_folder ./data --labelset fine --model resnet50 --model_type contrastive --ckpt $path2chkpt/last.pth\r\npython retrieval.py --dataset cifar100 --data_folder ./data --labelset coarse --model resnet50 --model_type contrastive --ckpt $path2chkpt/last.pth\r\npython out_of_dist_detection.py --dataset_outliers cifar10 --data_folder_outliers ./data --dataset cifar100 --data_folder ./data --size 32 --model resnet50 --model_type contrastive --ckpt $path2chkpt/last.pth\r\npython out_of_dist_detection.py --dataset_outliers tiny_imagenet --data_folder_outliers ./data --dataset cifar100 --data_folder ./data --size 32 --model resnet50 --model_type contrastive --ckpt $path2chkpt/last.pth\r\n```\r\n\r\nTo evaluate cross-entropy run:\r\n\r\n```\r\npython main_linear.py --batch_size 512 --learning_rate 5 --dataset cifar100 --data_folder ./data --model resnet50 --model_type cross_entropy --ckpt $path2chkpt/last.pth\r\npython retrieval_old.py --dataset cifar100 --data_folder ./data --labelset fine --model resnet50 --model_type cross_entropy --ckpt $path2chkpt/last.pth\r\npython retrieval_old.py --dataset cifar100 --data_folder ./data --labelset coarse --model resnet50 --model_type cross_entropy --ckpt $path2chkpt/last.pth\r\npython out_of_dist_detection.py --dataset_outliers cifar10 --data_folder_outliers ./data --dataset cifar100 --data_folder ./data --size 32 --model resnet50 --model_type cross_entropy --ckpt $path2chkpt/last.pth\r\npython out_of_dist_detection.py --dataset_outliers tiny_imagenet --data_folder_outliers ./data --dataset cifar100 --data_folder ./data --size 32 --model resnet50 --model_type cross_entropy --ckpt $path2chkpt/last.pth\r\n```\r\n\r\n## Training and Testing ImageNet-100\r\n\r\nCheck out the ImageNet branch of this repository.\r\n\r\n## Pretrained Models\r\n\r\nPretrained models can be found in [trained_models](trained_models).\r\n\r\n## Citation\r\nIf this code is useful in your research we would kindly ask you to cite our paper.\r\n```\r\n@article{rince2022AAAI,\r\n    title={Ranking Info Noise Contrastive Estimation: Boosting Contrastive Learning via Ranked Positives},\r\n    author={Hoffmann, David T and Behrmann, Nadine and Gall, Juergen and Brox, Thomas and Noroozi, Mehdi},\r\n    journal={arXiv preprint arXiv:2201.11736},\r\n    year={2022}\r\n}\r\n```\r\n\r\n##  License\r\n\r\nThis project is open-sourced under the AGPL-3.0 license. See the [License](LICENSE) file for details.\r\n\r\nFor a list of other open source components included in this project, see the file [3rd-party-licenses.txt](3rd-party-licenses.txt).\r\n\r\n## Purpose of the project\r\nThis software is a research prototype, solely developed for and published as\r\npart of the publication cited above. It will neither be\r\nmaintained nor monitored in any way.\r\n\r\n## Contact\r\nPlease feel free to open an issue or contact us personally if you have questions, need help, or need explanations.\r\nWrite to one of the following email addresses, and maybe put the other in the cc:\r\n\r\ndavid.hoffmann2@de.bosch.com\r\n\r\nnadine.behrmann@de.bosch.com\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fboschresearch%2Frince","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fboschresearch%2Frince","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fboschresearch%2Frince/lists"}