-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from rajewsky-lab/fix_imaging_preprocessing
Fix imaging preprocessing
- Loading branch information
Showing
20 changed files
with
386 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
__version__ = '0.0.2' | ||
__version__ = '0.0.11' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
from packaging import version | ||
import torch | ||
from torch import nn | ||
|
||
class PatchNCELoss(nn.Module): | ||
def __init__(self, opt): | ||
super().__init__() | ||
self.opt = opt | ||
self.cross_entropy_loss = torch.nn.CrossEntropyLoss(reduction='none') | ||
self.mask_dtype = torch.uint8 if version.parse(torch.__version__) < version.parse('1.2.0') else torch.bool | ||
|
||
def forward(self, feat_q, feat_k): | ||
num_patches = feat_q.shape[0] | ||
dim = feat_q.shape[1] | ||
feat_k = feat_k.detach() | ||
|
||
# pos logit | ||
l_pos = torch.bmm( | ||
feat_q.view(num_patches, 1, -1), feat_k.view(num_patches, -1, 1)) | ||
l_pos = l_pos.view(num_patches, 1) | ||
|
||
# neg logit | ||
|
||
# Should the negatives from the other samples of a minibatch be utilized? | ||
# In CUT and FastCUT, we found that it's best to only include negatives | ||
# from the same image. Therefore, we set | ||
# --nce_includes_all_negatives_from_minibatch as False | ||
# However, for single-image translation, the minibatch consists of | ||
# crops from the "same" high-resolution image. | ||
# Therefore, we will include the negatives from the entire minibatch. | ||
if self.opt.nce_includes_all_negatives_from_minibatch: | ||
# reshape features as if they are all negatives of minibatch of size 1. | ||
batch_dim_for_bmm = 1 | ||
else: | ||
batch_dim_for_bmm = self.opt.batch_size | ||
|
||
# reshape features to batch size | ||
feat_q = feat_q.view(batch_dim_for_bmm, -1, dim) | ||
feat_k = feat_k.view(batch_dim_for_bmm, -1, dim) | ||
npatches = feat_q.size(1) | ||
l_neg_curbatch = torch.bmm(feat_q, feat_k.transpose(2, 1)) | ||
|
||
# diagonal entries are similarity between same features, and hence meaningless. | ||
# just fill the diagonal with very small number, which is exp(-10) and almost zero | ||
diagonal = torch.eye(npatches, device=feat_q.device, dtype=self.mask_dtype)[None, :, :] | ||
l_neg_curbatch.masked_fill_(diagonal, -10.0) | ||
l_neg = l_neg_curbatch.view(-1, npatches) | ||
|
||
out = torch.cat((l_pos, l_neg), dim=1) / self.opt.nce_T | ||
|
||
loss = self.cross_entropy_loss(out, torch.zeros(out.size(0), dtype=torch.long, | ||
device=feat_q.device)) | ||
|
||
return loss |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.