-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.py
33 lines (25 loc) · 959 Bytes
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import torch
import shutil
import numpy as np
import random
from torch import nn
import torch.nn.functional as F
def save_checkpoint(state, is_best, task_id, filename='checkpoint.pth'):
torch.save(state, './' + str(task_id) + '/' + filename)
if is_best:
shutil.copyfile('./' + str(task_id) + '/' + filename, './' + str(task_id) + '/' + 'model_best.pth')
def setup_seed(seed):
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed) # if you are using multi-GPU.
np.random.seed(seed)
random.seed(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
class CrossEntropyLoss2d(nn.Module):
def __init__(self):
super(CrossEntropyLoss2d, self).__init__()
self.nll_loss = nn.NLLLoss(weight=None, reduction='mean')
def forward(self, inputs):
# pdb.set_trace()
return self.nll_loss(F.log_softmax(inputs, dim=1))