You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Total Number of Parameterstotal_params=sum(p.numel() forpinmodel.parameters())
# Total Number of Trainable Parameterstotal_params=sum(p.numel() forpinmodel.parameters() ifp.requires_grad)
ImageFolder with Folder Paths
classImageFolderWithPaths(datasets.ImageFolder):
# override the __getitem__ method. this is the method dataloader callsdef__getitem__(self, index):
# this is what ImageFolder normally returns original_tuple=super(ImageFolderWithPaths, self).__getitem__(index)
# the image file pathpath=self.imgs[index][0]
# make a new tuple that includes original and the pathtuple_with_path= (*original_tuple, path)
returntuple_with_path
importpdbclassForkedPdb(pdb.Pdb):
"""A Pdb subclass that may be used from a forked multiprocessing child Source: https://stackoverflow.com/a/23654936 To use: ForkedPdb().set_trace() """definteraction(self, *args, **kwargs):
_stdin=sys.stdintry:
sys.stdin=open('/dev/stdin')
pdb.Pdb.interaction(self, *args, **kwargs)
finally:
sys.stdin=_stdin
######################################################################################################## Helper Function Only# https://discuss.pytorch.org/t/convert-int-into-one-hot-format/507/4# Converts [0,1,0,1] to [[1,0], [0,1], [1,0], [0,1]]#######################################################################################################defto_onehot(y, num_classes=2):
y=y.view(-1, 1)
batch_size=y.shape[0]
y_onehot=torch.FloatTensor(batch_size, num_classes).to(y.device)
y_onehot.zero_()
y_onehot.scatter_(1, y, 1)
returny_onehot######################################################################################################### FP_loss: Reduces false positives by lowering the logits that do not correspond to the correct label# # Example: # Let y = label, y_oh = one-hot representation of y, and y_hat = logits# # For a Datapoint with y = [0] or y_oh [1, 0], and y_hat = [0.7, 0.6]# # A False positive happens if y_hat predicts [1], so to reduce the chance # of predicting a False Positive, FP_loss aims to minimize the 2nd value of y_hat# # Similarly, for a datapoint with y = [1] or y_oh [0, 1], and y_hat = [0.7, 0.6]# FP_loss aims to reduce 0.7 so that after some time, 0.6 will be bigger than the 1st of y_hat#######################################################################################################defFP_loss(logits, y):
softmax_out=torch.softmax(logits, -1)
max_softmax, _=torch.max(softmax_out, dim=-1)
softmax_normalised=softmax_out/max_softmax.view(-1,1)
y_reversed=1-yy_onehot_reversed=to_onehot(y_reversed)
fp_=torch.sum(softmax_normalised*y_onehot_reversed, dim=1)
fp_loss=torch.mean(fp_)
returnfp_loss###################################################################### # Sample Usecase #######################################################################criterion=nn.CrossEntropyLoss()
classification_loss=critraion(out, y) # Original Lossfp_loss=FP_loss(out, y) # FP Loss RegularizerBETA=0.5# Hyperparameter weight for FP loss, may use bigger weightstotal_loss=classification_loss+BETA*fp_loss# Combine lossestotal_loss.backward()
optimizer.step()