Release a CPU fold locator with ONNX, trainable weights, CLI and a frozen 17-book comparison
90af208 verified Download training/simulate.py from foliokestrel/folio-fold: direct link, hf CLI and curl.
- Browser
- Download file 3.63 kB
-
https://huggingface.co/foliokestrel/folio-fold/resolve/main/training/simulate.py
- Command line
-
hf download hf://foliokestrel/folio-fold/training/simulate.py
-
curl -L -o simulate.py https://huggingface.co/foliokestrel/folio-fold/resolve/main/training/simulate.py
3.63 kB
| """Procedural grayscale images of two facing pages, with known visible fold geometry.""" | |
| import torch | |
| import torch.nn.functional as F | |
| def render(bank, count, side=160): | |
| device=bank.device | |
| shape=(count,1,1) | |
| def rand(lo=0.,hi=1.): | |
| return torch.rand(shape,device=device)*(hi-lo)+lo | |
| def normal(): | |
| return torch.randn(shape,device=device) | |
| def side_values(lo,hi): | |
| return torch.where(left,rand(lo,hi),rand(lo,hi)) | |
| coords=(torch.arange(side,device=device)+.5)/side | |
| x,y=coords.view(1,1,side),coords.view(1,side,1) | |
| center=rand(.22,.78) | |
| slope=rand(-.68,.68)*rand(.12,1.0) | |
| gutter=center+slope*(y-.5) | |
| distance=x-gutter | |
| left=distance<0 | |
| # Different camera poses can leave one or both outer page edges outside the frame. | |
| scale=rand(.24,.82) | |
| half=scale*side_values(.75,1.25)*(1+side_values(-.38,.38)*(y-.5)) | |
| u=abs(distance)/half | |
| top=rand(-.34,.32) | |
| length=rand(.53,1.36) | |
| bend=u.clamp(0,1.3)**side_values(.65,1.65) | |
| top_edge=top+bend*side_values(-.21,.24) | |
| bottom_edge=top+length+bend*side_values(-.21,.24) | |
| v=(y-top_edge)/(bottom_edge-top_edge).clamp_min(.15) | |
| in_page=(u<1)&(v>=0)&(v<=1) | |
| paper=rand(.50,.97)+side_values(-.13,.1)+side_values(-.23,.15)*u.clamp_max(1) | |
| paper=paper+rand(-.15,.15)*(v-.5) | |
| def texture(nx,ny): | |
| field=torch.randn((count,1,ny,nx),device=device) | |
| return F.interpolate(field,(side,side),mode='bilinear',align_corners=False)[:,0] | |
| paper=paper+texture(8,8)*rand(0,.035)+texture(32,32)*rand(0,.027) | |
| # Independent magnifications and wrapping generate multiple columns and text scales. | |
| ids=torch.randint(len(bank),(count*2,),device=device) | |
| tiles=bank[ids].float().unsqueeze(1)/255. | |
| gx=(distance/half*side_values(.7,4.2)+side_values(0.,1.))%1 | |
| gy=(v*side_values(.8,6.5)+side_values(-.045,.045)*u**1.6+side_values(0,1))%1 | |
| grid=torch.stack([gx*2-1,gy*2-1],-1) | |
| sampled=F.grid_sample(tiles,grid.repeat(2,1,1,1),mode='bilinear',padding_mode='border',align_corners=False)[:,0] | |
| ink=1-torch.where(left,sampled[:count],sampled[count:]) | |
| content=((u>side_values(.015,.21))&(u<side_values(.75,.99))& | |
| (v>side_values(.01,.20))&(v<side_values(.76,.99))) | |
| # Blank pages, margins inside columns, diagrams and printed tables provide distractors. | |
| col_phase=u*side_values(2.,6.)+side_values(0.,1.) | |
| column=(col_phase%1.) > side_values(.0,.19) | |
| ink=ink*content*column*side_values(.20,.84)*side_values(0.,1.).gt(.10) | |
| rules=(col_phase%1. < side_values(.012,.044)) | ((v*side_values(9,33))%1.<.04) | |
| ink=ink+rules*content*side_values(0,.65)*side_values(0,1).gt(.65) | |
| plate=(u>side_values(.12,.38))&(u<side_values(.52,.93))&(v>side_values(.08,.30))&(v<side_values(.53,.87)) | |
| ink=ink+plate*side_values(0,1).gt(.80)*(texture(12,13)*.19+.24) | |
| shadow=side_values(-.06,.35)*torch.exp(-abs(distance)/side_values(.006,.11)) | |
| sharp=rand(-.14,.43)*torch.exp(-abs(distance)/rand(.001,.011)) | |
| background=rand(.04,.89)+texture(4,6)*rand(0,.13)+texture(16,23)*rand(0,.08) | |
| cover=(u<1+side_values(.008,.13))&(v>-side_values(.01,.08))&(v<1+side_values(.01,.12)) | |
| background=torch.where(cover,rand(.05,.59)+texture(10,10)*.05,background) | |
| image=torch.where(in_page,paper-ink-shadow-sharp,background) | |
| image=image+torch.randn_like(image)*rand(.003,.055) | |
| image=image.clamp(0,1).unsqueeze(1) | |
| soft=F.avg_pool2d(image,3,stride=1,padding=1,count_include_pad=False) | |
| blur=rand(0,.75).unsqueeze(1) | |
| image=image*(1-blur)+soft*blur | |
| return image,center.reshape(count),slope.reshape(count),top.reshape(count),length.reshape(count) | |