Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ This repo covers an reference implementation for the following papers in PyTorch
ImageNet model (small batch size with the trick of the momentum encoder) is released [here](https://www.dropbox.com/s/l4a69ececk4spdt/supcon.pth?dl=0).

## Loss Function
The loss function [`SupConLoss`](https://github.com/HobbitLong/SupContrast/blob/master/losses.py#L11) in `losses.py` takes `features` (L2 normalized) and `labels` as input, and return the loss. If `labels` is `None` or not passed to the it, it degenerates to SimCLR.
The loss function [`SupConLoss`](https://github.com/HobbitLong/SupContrast/blob/master/losses.py#L11) in `losses.py` takes `features` (L2 normalized) and `labels` as input, and return the loss. If `labels` is `None` or not passed to the it, it degenerates to SimCLR. To use SimCLR loss, temperature and base_temperature must be set to the same value.

Usage:
```python
from losses import SupConLoss

# define loss with a temperature `temp`
criterion = SupConLoss(temperature=temp)
criterionSupContrast = SupConLoss(temperature=temp)
criterionSimCLR = SupConLoss(temperature=temp, base_temperature=temp)

# features: [bsz, n_views, f_dim]
# `n_views` is the number of crops from each image
Expand All @@ -29,9 +30,9 @@ features = ...
labels = ...

# SupContrast
loss = criterion(features, labels)
loss = criterionSupContrast(features, labels)
# or SimCLR
loss = criterion(features)
loss = criterionSimCLR(features)
...
```

Expand Down