Download experiments/large-critic-20260928/source/objectives.py from User-2468/mini-unet-colorizer: direct link, hf CLI and curl.
- Browser
- Download file 1.01 kB
-
https://huggingface.co/User-2468/mini-unet-colorizer/resolve/915de375b4f97d217f62fedcf467e8a4e52b3094/experiments/large-critic-20260928/source/objectives.py
- Command line
-
hf download hf://User-2468/mini-unet-colorizer@915de375b4f97d217f62fedcf467e8a4e52b3094/experiments/large-critic-20260928/source/objectives.py
-
curl -L -o objectives.py https://huggingface.co/User-2468/mini-unet-colorizer/resolve/915de375b4f97d217f62fedcf467e8a4e52b3094/experiments/large-critic-20260928/source/objectives.py
1.01 kB
| import math | |
| import torch | |
| import torch.nn.functional as F | |
| def color_loss(pred,target): | |
| """Fit one coherent target; distribution matching discourages gray averages.""" | |
| pred=F.adaptive_avg_pool2d(pred,(64,64)).float();target=target.float() | |
| pixel=F.l1_loss(pred/20,target/20) | |
| p=F.avg_pool2d(pred,2).flatten(2);t=F.avg_pool2d(target,2).flatten(2) | |
| angle=torch.arange(8,device=p.device,dtype=p.dtype)*math.pi/8 | |
| directions=torch.stack([angle.cos(),angle.sin()],1) | |
| a=torch.einsum('kc,bcn->bkn',directions,p).sort(-1).values | |
| b=torch.einsum('kc,bcn->bkn',directions,t).sort(-1).values | |
| distribution=F.l1_loss(a/20,b/20) | |
| # Match boundaries in the teacher target, never minimize gradients toward zero. | |
| edge=(F.l1_loss((pred[:,:,:,1:]-pred[:,:,:,:-1])/20,(target[:,:,:,1:]-target[:,:,:,:-1])/20)+F.l1_loss((pred[:,:,1:]-pred[:,:,:-1])/20,(target[:,:,1:]-target[:,:,:-1])/20))/2 | |
| return pixel+.25*distribution+.1*edge,{'pixel':pixel,'distribution':distribution,'edge':edge} | |