{"id":13738044,"url":"https://github.com/yassouali/SCL","last_synced_at":"2025-05-08T15:32:20.766Z","repository":{"id":109451888,"uuid":"361062397","full_name":"yassouali/SCL","owner":"yassouali","description":":page_facing_up: Spatial Contrastive Learning for Few-Shot Classification (ECML/PKDD 2021).","archived":false,"fork":false,"pushed_at":"2022-12-19T13:51:25.000Z","size":1057,"stargazers_count":49,"open_issues_count":0,"forks_count":5,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-10T03:31:07.361Z","etag":null,"topics":["contrastive-learning","deep-learning","few-shot-learning","image-classification","self-supervised-learning"],"latest_commit_sha":null,"homepage":"https://yassouali.github.io/SCL_page/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yassouali.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,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2021-04-24T03:36:30.000Z","updated_at":"2024-10-11T11:35:25.000Z","dependencies_parsed_at":"2023-04-26T07:47:09.105Z","dependency_job_id":null,"html_url":"https://github.com/yassouali/SCL","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/yassouali%2FSCL","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yassouali%2FSCL/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yassouali%2FSCL/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yassouali%2FSCL/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yassouali","download_url":"https://codeload.github.com/yassouali/SCL/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253096346,"owners_count":21853586,"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":["contrastive-learning","deep-learning","few-shot-learning","image-classification","self-supervised-learning"],"created_at":"2024-08-03T03:02:09.680Z","updated_at":"2025-05-08T15:32:17.086Z","avatar_url":"https://github.com/yassouali.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"## Spatial Contrastive Learning for Few-Shot Classification (SCL)\n\n### [Paper](https://arxiv.org/abs/2012.13831) :page_with_curl:\n\nThis repo contains the official implementation of Spatial Contrastive Learning for Few-Shot Classification (SCL), which presents\nof a novel contrastive learning method applied to few-shot image classification in \norder to learn more general purpose embeddings, and facilitate the test-time adaptation to novel visual categories.\n\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://arxiv.org/abs/2012.13831\"\u003e\n    \u003cimg src=\"image/SCL.jpg\" width=\"550\"\u003e\n  \u003c/a\u003e\n\u003c/p\u003e\n\n### Highlights :fire:\n\n**(1) Contrastive Learning for Few-Shot Classification.** \\\nWe explore contrastive learning as an auxiliary pre-training objective to\nlearn more transferable features and facilitate the test time adaptation for few-shot \nclassification. \n\n**(2) Spatial Contrastive Learning (SCL).** \\\nWe propose a novel Spatial Contrastive (SC) loss\nthat promotes the encoding of the relevant spatial information into the learned representations,\nand further promotes class-independent discriminative patterns.\n\n**(3) Contrastive Distillation for Few-Shot Classification.** \\\nWe introduce a novel contrastive distillation objective to reduce the compactness of the features\nin the embedding space and provide additional refinement of the representations.\n\n\n### Requirements :wrench:\n\nThis repo was tested with CentOS 7.7.1908, Python 3.7.7, PyTorch 1.6.0, and CUDA 10.2. However, we expect that the provided code is compatible with older and newer version alike.\n\nThe required packages are `pytorch` and `torchvision`, together with `PIL` and `sckitlearn` for data-preprocessing and evaluation, `tqdm` for showing the training progress, and some additional modules. To setup the necessary modules, simply run:\n\n```bash\npip install -r requirements.txt\n```\n\n### Datasets :minidisc:\n\n#### Standard Few-shot Setting\n\nFor the standard few-shot experiments, we used ImageNet derivatives: miniImagetNet and tieredImageNet, in addition to CIFAR-100 derivatives: FC100 and CIFAR-FS.\nThese datasets are preprocessed by the repo of [MetaOptNet](https://github.com/kjunelee/MetaOptNet), \nrenamed and re-uploaded by [RFS](https://github.com/WangYueFt/rfs) and can be downloaded from here: [[DropBox]](https://www.dropbox.com/sh/6yd1ygtyc3yd981/AABVeEqzC08YQv4UZk7lNHvya?dl=0)\n\nAfter downloading all of the dataset, and placing them in the same folder which we refer to as `DATA_PATH`, where each\ndataset has its specific folder, eg: `DATA_PATH/FC100`. Then, during training, we can set the training argument `data_root` to `DATA_PATH`.\n\n#### Cross-domain Few-shot Setting\n\nIn cross-domain setting, we train on miniImageNet but we test on a different dataset. Specifically, we consider 4 datasets: cub, cars, places and plantae.\nAll of the datasets can be downloaded as follows:\n\n```shell\ncd dataset/download\npython download.py DATASET_NAME DATA_PATH\n```\n\nwhere `DATASET_NAME` refers to one of the 4 datasets (cub, cars, places and plantae) and `DATA_PATH` refers to\nthe path where the data will be downloaded and saved,\nwhich can be the path as the standard datasets above.\n\n### Running :hourglass:\n\nAll of the commands necessary to reproduce the results of the paper can be found in `scripts/run.sh`.\n\nIn general, to use the proposed method for few-shot classification, there is a two stage approach to follows: (1) training the model on the\nmerged meta-training set using `train_contrastive.py`, then (2) an evaluation setting, where we evaluate the pre-trained embedding model on\nthe meta-testing stage using `eval_fewshot.py`. Note that we can also apply an optional distillation step after the \nfirst pre-training step using `train_distillation.py`.\n\n#### Other Use Cases \n\nThe proposed SCL method is not specific to few-shot classification, and can also be used for\nstandard supervised or self-supervised training for image classification.\nFor instance, this can be done as follows:\n\n```python\n\nfrom losses import ContrastiveLoss\nfrom models.attention import AttentionSimilarity\n\nattention_module = AttentionSimilarity(hidden_size=128) # hidden_size depends on the encoder\ncontrast_criterion = ContrastiveLoss(temperature=10) # inverse temp is used (0.1)\n\n....\n\n# apply some augmentations\naug_inputs1, aug_inputs2 = augment(inputs) \naug_inputs = torch.cat([aug_inputs1, aug_inputs2], dim=0)\n\n# forward pass\nfeatures = encoder(aug_inputs)\n\n# supervised case\nloss_contrast = contrast_criterion(features, attention=attention_module, labels=labels)\n\n# unsupervised case\nloss_contrast = contrast_criterion(features, attention=attention_module, labels=None)\n\n....\n\n\n```\n\n### Citation :pencil:\n\nIf you find this repo useful for your research, please consider citing the paper as follows:\n\n```\n@article{ouali2020spatial,\n  title={Spatial Contrastive Learning for Few-Shot Classification},\n  author={Ouali, Yassine and Hudelot, C{\\'e}line and Tami, Myriam},\n  journal={arXiv preprint arXiv:2012.13831},\n  year={2020}\n}\n```\n\nFor any questions, please contact Yassine Ouali.\n\n#### Acknowlegements\n\n* The code structure is based on [RFS](https://github.com/WangYueFt/rfs) repo.\n* The cross-domain datasets code is based on [CrossDomainFewShot](https://github.com/hytseng0509/CrossDomainFewShot) repo.\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyassouali%2FSCL","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyassouali%2FSCL","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyassouali%2FSCL/lists"}