-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
40 lines (30 loc) · 1005 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
34
35
36
37
38
39
40
import torch
import torch.nn as nn
import cv2
from torchvision import transforms
from torchvision import models
from torch.utils.checkpoint import checkpoint, checkpoint_sequential
class Reshape(nn.Module):
def __init__(self, shape=None):
super(Reshape, self).__init__()
self.shape = shape
def forward(self, x):
if self.shape is None:
x = x.view(x.size(0), -1)
else:
x = x.view(x.size(0), *self.shape)
return x
def num_params(model):
return sum(x.numel() for x in model.parameters())
class FakeFn(nn.Module):
def __init__(self, fn=lambda x: x):
super(FakeFn, self).__init__()
self.fn = fn
def forward(self, *x):
return self.fn(*x)
class DummyLayer(nn.Module):
def __init__(self):
super().__init__()
self.dummy = nn.Parameter(torch.ones(1, dtype=torch.float32))
def forward(self,x):
return x + self.dummy - self.dummy