{"id":24211770,"url":"https://github.com/richardaecn/class-balanced-loss","last_synced_at":"2025-10-20T07:02:04.767Z","repository":{"id":47131071,"uuid":"162182943","full_name":"richardaecn/class-balanced-loss","owner":"richardaecn","description":"Class-Balanced Loss Based on Effective Number of Samples. CVPR 2019","archived":false,"fork":false,"pushed_at":"2021-08-29T01:51:44.000Z","size":596,"stargazers_count":609,"open_issues_count":18,"forks_count":69,"subscribers_count":19,"default_branch":"master","last_synced_at":"2025-03-29T09:11:40.223Z","etag":null,"topics":["cloud-tpu","computer-vision","cvpr","cvpr2019","deep-learning","fine-grained","fine-grained-classification","fine-grained-visual-categorization","imagenet","inaturalist","tensorflow"],"latest_commit_sha":null,"homepage":"","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/richardaecn.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":"2018-12-17T19:58:12.000Z","updated_at":"2025-03-10T11:14:04.000Z","dependencies_parsed_at":"2022-08-24T17:51:12.841Z","dependency_job_id":null,"html_url":"https://github.com/richardaecn/class-balanced-loss","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/richardaecn%2Fclass-balanced-loss","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/richardaecn%2Fclass-balanced-loss/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/richardaecn%2Fclass-balanced-loss/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/richardaecn%2Fclass-balanced-loss/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/richardaecn","download_url":"https://codeload.github.com/richardaecn/class-balanced-loss/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247318745,"owners_count":20919484,"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":["cloud-tpu","computer-vision","cvpr","cvpr2019","deep-learning","fine-grained","fine-grained-classification","fine-grained-visual-categorization","imagenet","inaturalist","tensorflow"],"created_at":"2025-01-14T02:36:48.027Z","updated_at":"2025-10-20T07:02:04.710Z","avatar_url":"https://github.com/richardaecn.png","language":"Python","funding_links":[],"categories":["Appendix: Object Detection for Natural Scene"],"sub_categories":["Papers"],"readme":"# Class-Balanced Loss Based on Effective Number of Samples\n\nTensorflow code for the paper:\n\n[*Class-Balanced Loss Based on Effective Number of Samples*](https://arxiv.org/abs/1901.05555)\\\n[Yin Cui](https://ycui.me/), Menglin Jia, [Tsung-Yi Lin](https://vision.cornell.edu/se3/people/tsung-yi-lin/), [Yang Song](https://ai.google/research/people/author38270), [Serge Belongie](http://blogs.cornell.edu/techfaculty/serge-belongie/)\n\n\n## Dependencies:\n+ Python (3.6)\n+ Tensorflow (1.14)\n\n\n## Datasets:\n+ Long-Tailed [CIFAR](https://www.cs.toronto.edu/~kriz/cifar.html).\nWe provide [a download link](https://drive.google.com/file/d/1NY3lWYRfsTWfsjFPxJUlPumy-WFeD7zK/) that includes all the data used in our paper in .tfrecords format. The data was converted and generated by `src/generate_cifar_tfrecords.py` (original CIFAR) and `src/generate_cifar_tfrecords_im.py` (long-tailed CIFAR).\n\n\n## Effective Number of Samples:\nFor a visualization of the data and effective number of samples, please take a look at `data.ipynb`.\n\n\n## Key Implementation Details:\n+ [Weights for class-balanced loss](https://github.com/richardaecn/class-balanced-loss/blob/master/src/cifar_main.py#L425-L430)\n+ [Focal loss](https://github.com/richardaecn/class-balanced-loss/blob/master/src/cifar_main.py#L226-L266)\n+ [Assigning weights to different loss](https://github.com/richardaecn/class-balanced-loss/blob/master/src/cifar_main.py#L325-L354)\n+ [Initialization of the last layer](https://github.com/richardaecn/class-balanced-loss/blob/master/src/model_base.py#L196)\n\n\n## Training and Evaluation:\nWe provide 3 .sh scripts for training and evaluation.\n+ On original CIFAR dataset:\n```\n./cifar_trainval.sh\n```\n+ On long-tailed CIFAR dataset (the hyperparameter `IM_FACTOR` is the inverse of \"Imbalance Factor\" in the paper):\n```\n./cifar_im_trainval.sh\n```\n+ On long-tailed CIFAR dataset using the proposed class-balanced loss (set non-zero `BETA`):\n```\n./cifar_im_trainval_cb.sh\n```\n\n+ Run Tensorboard for visualization:\n```\ntensorboard --logdir=./results --port=6006\n```\n\n+ The figure below are the results of running ```./cifar_im_trainval.sh``` and ```./cifar_im_trainval_cb.sh```:\n\u003cp float=\"left\"\u003e\n    \u003cimg src=\"assets/results.png\" height=\"512\"/\u003e\n\u003c/p\u003e\n\n\n## Training with TPU:\nWe train networks on iNaturalist and ImageNet datasets using [Google's Cloud TPU](https://cloud.google.com/tpu/). The code for this section is in `tpu/`. Our code is based on the official implementation of [Training ResNet on Cloud TPU](https://cloud.google.com/tpu/docs/tutorials/resnet) and forked from https://github.com/tensorflow/tpu.\n\n\nData Preparation:\n+ Download datasets (except images) from [this link](https://drive.google.com/file/d/1ZHhMFJxsgXItJYKiM_VJ0lznj8XClgWF/) and unzip it under `tpu/`. The unzipped directory `tpu/raw_data/` contains the training and validation splits. For raw images, please download from the following links and put them into the corresponding folders in `tpu/raw_data/`:\n  + [iNaturalist 2017](https://github.com/visipedia/inat_comp/tree/master/2017)\n  + [iNaturalist 2018](https://github.com/visipedia/inat_comp)\n  + [ImageNet (ILSVRC2012)](http://www.image-net.org/challenges/LSVRC/2012/)\n\n+ Convert datasets into .tfrecords format and upload to Google Cloud Storage (gcs) using `tpu/tools/datasets/dataset_to_gcs.py`:\n```\npython dataset_to_gcs.py \\\n  --project=$PROJECT \\\n  --gcs_output_path=$GCS_DATA_DIR \\\n  --local_scratch_dir=$LOCAL_TFRECORD_DIR \\\n  --raw_data_dir=$LOCAL_RAWDATA_DIR\n```\n\nThe following 3 .sh scripts in `tpu/` can be used to train and evaluate models on iNaturalist and ImageNet using Cloud TPU. For more details on how to use Cloud TPU, please refer to [Training ResNet on Cloud TPU](https://cloud.google.com/tpu/docs/tutorials/resnet).\n\nNote that the [image mean and standard deviation](https://github.com/richardaecn/class-balanced-loss/blob/master/tpu/models/official/resnet/resnet_main.py#L232-L238) and [input size](https://github.com/richardaecn/class-balanced-loss/blob/master/tpu/models/official/resnet/resnet_preprocessing.py#L22-L27) need to be updated accordingly.\n\n+ On ImageNet (ILSVRC 2012):\n```\n./run_ILSVRC2012.sh\n```\n+ On iNaturalist 2017:\n```\n./run_inat2017.sh\n```\n+ On iNaturalist 2018:\n```\n./run_inat2018.sh\n```\n\n+ The pre-trained models, including all logs viewable on tensorboard, can be downloaded from the following links:\n\n| Dataset          | Network   | Loss                      | Input Size | Download Link |\n|------------------|-----------|---------------------------|------------|---------------|\n| ILSVRC 2012      | ResNet-50 | Class-Balanced Focal Loss | 224        | [link](https://drive.google.com/file/d/1SmLv1-D1143Cma4Y5bDxHUfXjOI_0Yvr/) |\n| iNaturalist 2018 | ResNet-50 | Class-Balanced Focal Loss | 224        | [link](https://drive.google.com/file/d/1aNyYCfAUXP4up_hvOhAKQkBevc2e8lr8/) |\n\n\n## Citation\nIf you find our work helpful in your research, please cite it as:\n```latex\n@inproceedings{cui2019classbalancedloss,\n  title={Class-Balanced Loss Based on Effective Number of Samples},\n  author={Cui, Yin and Jia, Menglin and Lin, Tsung-Yi and Song, Yang and Belongie, Serge},\n  booktitle={CVPR},\n  year={2019}\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frichardaecn%2Fclass-balanced-loss","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frichardaecn%2Fclass-balanced-loss","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frichardaecn%2Fclass-balanced-loss/lists"}