dpc7 commited on
Commit
8ccf492
·
1 Parent(s): ae06796

added flattenedLoss func

Browse files
Files changed (1) hide show
  1. app.py +24 -0
app.py CHANGED
@@ -26,6 +26,30 @@ import gradio as gr
26
  from fastai.vision.all import *
27
  import skimage
28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  learn = load_learner('stage2-resnet50-size256-export.pkl')
30
 
31
  labels = learn.dls.vocab
 
26
  from fastai.vision.all import *
27
  import skimage
28
 
29
+ class FlattenedLoss():
30
+ "Same as `func`, but flattens input and target."
31
+ def __init__(self, func, *args, axis:int=-1, floatify:bool=False, is_2d:bool=True, **kwargs):
32
+ self.func,self.axis,self.floatify,self.is_2d = func(*args,**kwargs),axis,floatify,is_2d
33
+ functools.update_wrapper(self, self.func)
34
+
35
+ def __repr__(self): return f"FlattenedLoss of {self.func}"
36
+ @property
37
+ def reduction(self): return self.func.reduction
38
+ @reduction.setter
39
+ def reduction(self, v): self.func.reduction = v
40
+
41
+ def __call__(self, input:Tensor, target:Tensor, **kwargs)->Rank0Tensor:
42
+ input = input.transpose(self.axis,-1).contiguous()
43
+ target = target.transpose(self.axis,-1).contiguous()
44
+ if self.floatify: target = target.float()
45
+
46
+ # Label smoothing experiment
47
+ target = (target * 0.9 + 0.05)
48
+ target[:,0] = 1
49
+
50
+ input = input.view(-1,input.shape[-1]) if self.is_2d else input.view(-1)
51
+ return self.func.__call__(input, target.view(-1), **kwargs)
52
+
53
  learn = load_learner('stage2-resnet50-size256-export.pkl')
54
 
55
  labels = learn.dls.vocab