vf_train.c
This file
Path
stb_image_resize_test/vf_train.c
999 lines · 27.8 KiB · C
Last changed by
Merge branch 'master' into workingSean Barrettbefore adoption6e9f34d
History of this fileTake it
1#include <stdio.h>2#include <stdlib.h>3#include <string.h>45#define stop() __debugbreak()6#include <windows.h>7#define int64 __int6489#pragma warning(disable:4127)1011#define STBIR__WEIGHT_TABLES 12#define STBIR_PROFILE13#define STB_IMAGE_RESIZE_IMPLEMENTATION14#include "stb_image_resize2.h" 1516static int * file_read( char const * filename )17{18 size_t s;19 int * m;20 FILE * f = fopen( filename, "rb" );21 if ( f == 0 ) return 0;22 23 fseek( f, 0, SEEK_END);24 s = ftell( f );25 fseek( f, 0, SEEK_SET);26 m = malloc( s + 4 );27 m[0] = (int)s;28 fread( m+1, 1, s, f);29 fclose(f);3031 return( m );32}3334typedef struct fileinfo35{36 int * timings;37 int timing_count;38 int dimensionx, dimensiony;39 int numtypes;40 int * types;41 int * effective;42 int cpu;43 int simd;44 int numinputrects;45 int * inputrects;46 int outputscalex, outputscaley;47 int milliseconds;48 int64 cycles;49 double scale_time;50 int bitmapx, bitmapy;51 char const * filename;52} fileinfo;5354int numfileinfo;55fileinfo fi[256];56unsigned char * bitmap;57int bitmapw, bitmaph, bitmapp;5859static int use_timing_file( char const * filename, int index )60{61 int * base = file_read( filename );62 int * file = base;6364 if ( base == 0 ) return 0;6566 ++file; // skip file image size;67 if ( *file++ != 'VFT1' ) return 0;68 fi[index].cpu = *file++;69 fi[index].simd = *file++;70 fi[index].dimensionx = *file++;71 fi[index].dimensiony = *file++;72 fi[index].numtypes = *file++;73 fi[index].types = file; file += fi[index].numtypes;74 fi[index].effective = file; file += fi[index].numtypes;75 fi[index].numinputrects = *file++;76 fi[index].inputrects = file; file += fi[index].numinputrects * 2;77 fi[index].outputscalex = *file++;78 fi[index].outputscaley = *file++;79 fi[index].milliseconds = *file++;80 fi[index].cycles = ((int64*)file)[0]; file += 2;81 fi[index].filename = filename;8283 fi[index].timings = file;84 fi[index].timing_count = (int) ( ( base[0] - ( ((char*)file - (char*)base - sizeof(int) ) ) ) / (sizeof(int)*2) );8586 fi[index].scale_time = (double)fi[index].milliseconds / (double)fi[index].cycles;8788 return 1;89}9091static int vert_first( float weights_table[STBIR_RESIZE_CLASSIFICATIONS][4], int ox, int oy, int ix, int iy, int filter, STBIR__V_FIRST_INFO * v_info )92{93 float h_scale=(float)ox/(float)(ix);94 float v_scale=(float)oy/(float)(iy);95 stbir__support_callback * support = stbir__builtin_supports[filter];96 int vertical_filter_width = stbir__get_filter_pixel_width(support,v_scale,0);97 int vertical_gather = ( v_scale >= ( 1.0f - stbir__small_float ) ) || ( vertical_filter_width <= STBIR_FORCE_GATHER_FILTER_SCANLINES_AMOUNT );9899 return stbir__should_do_vertical_first( weights_table, stbir__get_filter_pixel_width(support,h_scale,0), h_scale, ox, vertical_filter_width, v_scale, oy, vertical_gather, v_info );100} 101102#define STB_IMAGE_WRITE_IMPLEMENTATION103#include "stb_image_write.h"104105static void alloc_bitmap()106{107 int findex;108 int x = 0, y = 0;109 int w = 0, h = 0;110111 for( findex = 0 ; findex < numfileinfo ; findex++ )112 {113 int nx, ny;114 int thisw, thish;115116 thisw = ( fi[findex].dimensionx * fi[findex].numtypes ) + ( fi[findex].numtypes - 1 );117 thish = ( fi[findex].dimensiony * fi[findex].numinputrects ) + ( fi[findex].numinputrects - 1 );118119 for(;;)120 {121 nx = x + ((x)?4:0) + thisw;122 ny = y + ((y)?4:0) + thish;123 if ( ( nx <= 3600 ) || ( x == 0 ) )124 { 125 fi[findex].bitmapx = x + ((x)?4:0);126 fi[findex].bitmapy = y + ((y)?4:0);127 x = nx;128 if ( x > w ) w = x;129 if ( ny > h ) h = ny;130 break;131 }132 else133 {134 x = 0;135 y = h;136 }137 }138 }139140 w = (w+3) & ~3;141 bitmapw = w;142 bitmaph = h;143 bitmapp = w * 3; // RGB144 bitmap = malloc( bitmapp * bitmaph );145146 memset( bitmap, 0, bitmapp * bitmaph );147}148149static void build_bitmap( float weights[STBIR_RESIZE_CLASSIFICATIONS][4], int do_channel_count_index, int findex )150{151 static int colors[STBIR_RESIZE_CLASSIFICATIONS];152 STBIR__V_FIRST_INFO v_info = {0};153154 int * ts;155 int ir;156 unsigned char * bitm = bitmap + ( fi[findex].bitmapx*3 ) + ( fi[findex].bitmapy*bitmapp) ;157158 for( ir = 0; ir < STBIR_RESIZE_CLASSIFICATIONS ; ir++ ) colors[ ir ] = 127*ir/STBIR_RESIZE_CLASSIFICATIONS+128;159160 ts = fi[findex].timings;161162 for( ir = 0 ; ir < fi[findex].numinputrects ; ir++ )163 {164 int ix, iy, chanind;165 ix = fi[findex].inputrects[ir*2];166 iy = fi[findex].inputrects[ir*2+1];167168 for( chanind = 0 ; chanind < fi[findex].numtypes ; chanind++ )169 {170 int ofs, h, hh;171172 // just do the type that we're on173 if ( chanind != do_channel_count_index )174 {175 ts += 2 * fi[findex].dimensionx * fi[findex].dimensiony;176 continue;177 }178179 // bitmap offset180 ofs=chanind*(fi[findex].dimensionx+1)*3+ir*(fi[findex].dimensiony+1)*bitmapp;181182 h = 1;183 for( hh = 0 ; hh < fi[findex].dimensiony; hh++ )184 {185 int ww, w = 1;186 for( ww = 0 ; ww < fi[findex].dimensionx; ww++ )187 {188 int good, v_first, VF, HF;189190 VF = ts[0];191 HF = ts[1];192193 v_first = vert_first( weights, w, h, ix, iy, STBIR_FILTER_MITCHELL, &v_info );194195 good = ( ((HF<=VF) && (!v_first)) || ((VF<=HF) && (v_first)));196197 if ( good )198 {199 bitm[ofs+2] = 0;200 bitm[ofs+1] = (unsigned char)colors[v_info.v_resize_classification];201 }202 else203 {204 double r;205206 if ( HF < VF )207 r = (double)(VF-HF)/(double)HF;208 else209 r = (double)(HF-VF)/(double)VF;210 211 if ( r > 0.4f) r = 0.4;212 r *= 1.0f/0.4f; 213214 bitm[ofs+2] = (char)(255.0f*r);215 bitm[ofs+1] = (char)(((float)colors[v_info.v_resize_classification])*(1.0f-r));216 }217 bitm[ofs] = 0;218219 ofs += 3;220 ts += 2;221 w += fi[findex].outputscalex;222 }223 ofs += bitmapp - fi[findex].dimensionx*3;224 h += fi[findex].outputscaley;225 }226 }227 }228}229230static void build_comp_bitmap( float weights[STBIR_RESIZE_CLASSIFICATIONS][4], int do_channel_count_index )231{232 int * ts0;233 int * ts1;234 int ir;235 unsigned char * bitm = bitmap + ( fi[0].bitmapx*3 ) + ( fi[0].bitmapy*bitmapp) ;236237 ts0 = fi[0].timings;238 ts1 = fi[1].timings;239240 for( ir = 0 ; ir < fi[0].numinputrects ; ir++ )241 {242 int ix, iy, chanind;243 ix = fi[0].inputrects[ir*2];244 iy = fi[0].inputrects[ir*2+1];245246 for( chanind = 0 ; chanind < fi[0].numtypes ; chanind++ )247 {248 int ofs, h, hh;249250 // just do the type that we're on251 if ( chanind != do_channel_count_index )252 {253 ts0 += 2 * fi[0].dimensionx * fi[0].dimensiony;254 ts1 += 2 * fi[0].dimensionx * fi[0].dimensiony;255 continue;256 }257258 // bitmap offset259 ofs=chanind*(fi[0].dimensionx+1)*3+ir*(fi[0].dimensiony+1)*bitmapp;260261 h = 1;262 for( hh = 0 ; hh < fi[0].dimensiony; hh++ )263 {264 int ww, w = 1;265 for( ww = 0 ; ww < fi[0].dimensionx; ww++ )266 {267 int v_first, time0, time1;268269 v_first = vert_first( weights, w, h, ix, iy, STBIR_FILTER_MITCHELL, 0 );270271 time0 = ( v_first ) ? ts0[0] : ts0[1];272 time1 = ( v_first ) ? ts1[0] : ts1[1];273274 if ( time0 < time1 )275 {276 double r = (double)(time1-time0)/(double)time0;277 if ( r > 0.4f) r = 0.4;278 r *= 1.0f/0.4f; 279 bitm[ofs+2] = 0;280 bitm[ofs+1] = (char)(255.0f*r);281 bitm[ofs] = (char)(64.0f*(1.0f-r));282 }283 else284 {285 double r = (double)(time0-time1)/(double)time1;286 if ( r > 0.4f) r = 0.4;287 r *= 1.0f/0.4f; 288 bitm[ofs+2] = (char)(255.0f*r);289 bitm[ofs+1] = 0;290 bitm[ofs] = (char)(64.0f*(1.0f-r));291 }292 ofs += 3;293 ts0 += 2;294 ts1 += 2;295 w += fi[0].outputscalex;296 }297 ofs += bitmapp - fi[0].dimensionx*3;298 h += fi[0].outputscaley;299 }300 }301 }302}303304static void write_bitmap()305{306 stbi_write_png( "results.png", bitmapp / 3, bitmaph, 3|STB_IMAGE_BGR, bitmap, bitmapp );307}308309310static void calc_errors( float weights_table[STBIR_RESIZE_CLASSIFICATIONS][4], int * curtot, double * curerr, int do_channel_count_index )311{312 int th, findex;313 STBIR__V_FIRST_INFO v_info = {0};314315 for(th=0;th<STBIR_RESIZE_CLASSIFICATIONS;th++)316 {317 curerr[th]=0;318 curtot[th]=0;319 }320321 for( findex = 0 ; findex < numfileinfo ; findex++ )322 {323 int * ts;324 int ir;325 ts = fi[findex].timings;326327 for( ir = 0 ; ir < fi[findex].numinputrects ; ir++ )328 {329 int ix, iy, chanind;330 ix = fi[findex].inputrects[ir*2];331 iy = fi[findex].inputrects[ir*2+1];332333 for( chanind = 0 ; chanind < fi[findex].numtypes ; chanind++ )334 {335 int h, hh;336337 // just do the type that we're on338 if ( chanind != do_channel_count_index )339 {340 ts += 2 * fi[findex].dimensionx * fi[findex].dimensiony;341 continue;342 }343344 h = 1;345 for( hh = 0 ; hh < fi[findex].dimensiony; hh++ )346 {347 int ww, w = 1;348 for( ww = 0 ; ww < fi[findex].dimensionx; ww++ )349 {350 int good, v_first, VF, HF;351352 VF = ts[0];353 HF = ts[1];354355 v_first = vert_first( weights_table, w, h, ix, iy, STBIR_FILTER_MITCHELL, &v_info );356357 good = ( ((HF<=VF) && (!v_first)) || ((VF<=HF) && (v_first)));358359 if ( !good )360 {361 double diff;362 if ( VF < HF )363 diff = ((double)HF-(double)VF) * fi[findex].scale_time;364 else365 diff = ((double)VF-(double)HF) * fi[findex].scale_time;366367 curtot[v_info.v_resize_classification] += 1;368 curerr[v_info.v_resize_classification] += diff;369 }370371 ts += 2;372 w += fi[findex].outputscalex;373 }374 h += fi[findex].outputscaley;375 }376 }377 }378 }379}380381#define TRIESPERWEIGHT 32382#define MAXRANGE ((TRIESPERWEIGHT+1) * (TRIESPERWEIGHT+1) * (TRIESPERWEIGHT+1) * (TRIESPERWEIGHT+1) - 1)383384static void expand_to_floats( float * weights, int range )385{386 weights[0] = (float)( range % (TRIESPERWEIGHT+1) ) / (float)TRIESPERWEIGHT;387 weights[1] = (float)( range/(TRIESPERWEIGHT+1) % (TRIESPERWEIGHT+1) ) / (float)TRIESPERWEIGHT;388 weights[2] = (float)( range/(TRIESPERWEIGHT+1)/(TRIESPERWEIGHT+1) % (TRIESPERWEIGHT+1) ) / (float)TRIESPERWEIGHT;389 weights[3] = (float)( range/(TRIESPERWEIGHT+1)/(TRIESPERWEIGHT+1)/(TRIESPERWEIGHT+1) % (TRIESPERWEIGHT+1) ) / (float)TRIESPERWEIGHT;390}391392static char const * expand_to_string( int range )393{394 static char str[128];395 int w0,w1,w2,w3;396 w0 = range % (TRIESPERWEIGHT+1);397 w1 = range/(TRIESPERWEIGHT+1) % (TRIESPERWEIGHT+1);398 w2 = range/(TRIESPERWEIGHT+1)/(TRIESPERWEIGHT+1) % (TRIESPERWEIGHT+1);399 w3 = range/(TRIESPERWEIGHT+1)/(TRIESPERWEIGHT+1)/(TRIESPERWEIGHT+1) % (TRIESPERWEIGHT+1);400 sprintf( str, "[ %2d/%d %2d/%d %2d/%d %2d/%d ]",w0,TRIESPERWEIGHT,w1,TRIESPERWEIGHT,w2,TRIESPERWEIGHT,w3,TRIESPERWEIGHT );401 return str;402}403404static void print_weights( float weights[STBIR_RESIZE_CLASSIFICATIONS][4], int channel_count_index, int * tots, double * errs )405{406 int th;407 printf("ChInd: %d Weights:\n",channel_count_index);408 for(th=0;th<STBIR_RESIZE_CLASSIFICATIONS;th++)409 {410 float * w = weights[th];411 printf(" %d: [%1.5f %1.5f %1.5f %1.5f] (%d %.4f)\n",th, w[0], w[1], w[2], w[3], tots[th], errs[th] );412 }413 printf("\n");414}415416static int windowranges[ 16 ];417static int windowstatus = 0;418static DWORD trainstart = 0;419420static void opt_channel( float best_output_weights[STBIR_RESIZE_CLASSIFICATIONS][4], int channel_count_index )421{422 int newbest = 0;423 float weights[STBIR_RESIZE_CLASSIFICATIONS][4] = {0};424 double besterr[STBIR_RESIZE_CLASSIFICATIONS];425 int besttot[STBIR_RESIZE_CLASSIFICATIONS];426 int best[STBIR_RESIZE_CLASSIFICATIONS]={0};427428 double curerr[STBIR_RESIZE_CLASSIFICATIONS];429 int curtot[STBIR_RESIZE_CLASSIFICATIONS];430 int th, range;431 DWORD lasttick = 0;432433 for(th=0;th<STBIR_RESIZE_CLASSIFICATIONS;th++) 434 {435 besterr[th]=1000000000000.0;436 besttot[th]=0x7fffffff;437 }438439 newbest = 0;440441 // try the whole range 442 range = MAXRANGE;443 do444 {445 for(th=0;th<STBIR_RESIZE_CLASSIFICATIONS;th++)446 expand_to_floats( weights[th], range );447448 calc_errors( weights, curtot, curerr, channel_count_index );449450 for(th=0;th<STBIR_RESIZE_CLASSIFICATIONS;th++)451 {452 if ( curerr[th] < besterr[th] )453 {454 besterr[th] = curerr[th];455 besttot[th] = curtot[th];456 best[th] = range;457 expand_to_floats( best_output_weights[th], best[th] );458 newbest = 1;459 }460 }461462 {463 DWORD t = GetTickCount();464 if ( range == 0 )465 goto do_bitmap;466467 if ( newbest )468 {469 if ( ( GetTickCount() - lasttick ) > 200 )470 {471 int findex;472 473 do_bitmap:474 lasttick = t;475 newbest = 0;476477 for( findex = 0 ; findex < numfileinfo ; findex++ )478 build_bitmap( best_output_weights, channel_count_index, findex );479480 lasttick = GetTickCount();481 }482 }483 }484 485 windowranges[ channel_count_index ] = range;486487 // advance all the weights and loop488 --range;489 } while( ( range >= 0 ) && ( !windowstatus ) );490491 // if we hit here, then we tried all weights for this opt, so save them 492}493494static void print_struct( float weight[5][STBIR_RESIZE_CLASSIFICATIONS][4], char const * name )495{496 printf("\n\nstatic float %s[5][STBIR_RESIZE_CLASSIFICATIONS][4]=\n{", name );497 {498 int i;499 for(i=0;i<5;i++) 500 { 501 int th;502 for(th=0;th<STBIR_RESIZE_CLASSIFICATIONS;th++)503 {504 int j;505 printf("\n "); 506 for(j=0;j<4;j++) 507 printf("%1.5ff, ", weight[i][th][j] ); 508 }509 printf("\n");510 }511 printf("\n};\n");512 }513}514515static float retrain_weights[5][STBIR_RESIZE_CLASSIFICATIONS][4];516517static DWORD __stdcall retrain_shim( LPVOID p )518{519 int chanind = (int) (size_t)p;520 opt_channel( retrain_weights[chanind], chanind );521 return 0;522}523524static char const * gettime( int ms )525{526 static char time[32];527 if (ms > 60000)528 sprintf( time, "%dm %ds",ms/60000, (ms/1000)%60 );529 else 530 sprintf( time, "%ds",ms/1000 );531 return time;532}533534static BITMAPINFOHEADER bmiHeader;535static DWORD extrawindoww, extrawindowh;536static HINSTANCE instance;537static int curzoom = 1;538539static LRESULT WINAPI WindowProc( HWND window,540 UINT message,541 WPARAM wparam,542 LPARAM lparam )543{544 switch( message )545 {546 case WM_CHAR:547 if ( wparam != 27 )548 break;549 // falls through550551 case WM_CLOSE:552 {553 int i;554 int max = 0;555 556 for( i = 0 ; i < fi[0].numtypes ; i++ )557 if( windowranges[i] > max ) max = windowranges[i];558 559 if ( ( max == 0 ) || ( MessageBox( window, "Cancel before training is finished?", "Vertical First Training", MB_OKCANCEL|MB_ICONSTOP ) == IDOK ) )560 {561 for( i = 0 ; i < fi[0].numtypes ; i++ )562 if( windowranges[i] > max ) max = windowranges[i];563 if ( max )564 windowstatus = 1;565 DestroyWindow( window );566 }567 }568 return 0;569 570 case WM_PAINT:571 {572 PAINTSTRUCT ps;573 HDC dc;574575 dc = BeginPaint( window, &ps );576 StretchDIBits( dc, 577 0, 0, bitmapw*curzoom, bitmaph*curzoom,578 0, 0, bitmapw, bitmaph,579 bitmap, (BITMAPINFO*)&bmiHeader, DIB_RGB_COLORS, SRCCOPY );580581 PatBlt( dc, bitmapw*curzoom, 0, 4096, 4096, WHITENESS );582 PatBlt( dc, 0, bitmaph*curzoom, 4096, 4096, WHITENESS );583584 SetTextColor( dc, RGB(0,0,0) );585 SetBkColor( dc, RGB(255,255,255) );586 SetBkMode( dc, OPAQUE );587588 {589 int i, l = 0, max = 0;590 char buf[1024];591 RECT rc;592 POINT p;593594 for( i = 0 ; i < fi[0].numtypes ; i++ )595 {596 l += sprintf( buf + l, "channels: %d %s\n", fi[0].effective[i], windowranges[i] ? expand_to_string( windowranges[i] ) : "Done." );597 if ( windowranges[i] > max ) max = windowranges[i];598 }599600 rc.left = 32; rc.top = bitmaph*curzoom+10;601 rc.right = 512; rc.bottom = rc.top + 512;602 DrawText( dc, buf, -1, &rc, DT_TOP );603604 l = 0;605 if ( max == 0 )606 {607 static DWORD traindone = 0;608 if ( traindone == 0 ) traindone = GetTickCount();609 l = sprintf( buf, "Finished in %s.", gettime( traindone - trainstart ) );610 }611 else if ( max != MAXRANGE )612 l = sprintf( buf, "Done in %s...", gettime( (int) ( ( ( (int64)max * ( (int64)GetTickCount() - (int64)trainstart ) ) ) / (int64) ( MAXRANGE - max ) ) ) );613614 GetCursorPos( &p );615 ScreenToClient( window, &p );616617 if ( ( p.x >= 0 ) && ( p.y >= 0 ) && ( p.x < (bitmapw*curzoom) ) && ( p.y < (bitmaph*curzoom) ) )618 {619 int findex;620 int x, y, w, h, sx, sy, ix, iy, ox, oy;621 int ir, chanind;622 int * ts;623 char badstr[64];624 STBIR__V_FIRST_INFO v_info={0};625626 p.x /= curzoom;627 p.y /= curzoom;628629 for( findex = 0 ; findex < numfileinfo ; findex++ )630 {631 x = fi[findex].bitmapx;632 y = fi[findex].bitmapy;633 w = x + ( fi[findex].dimensionx + 1 ) * fi[findex].numtypes;634 h = y + ( fi[findex].dimensiony + 1 ) * fi[findex].numinputrects;635636 if ( ( p.x >= x ) && ( p.y >= y ) && ( p.x < w ) && ( p.y < h ) )637 goto found;638 }639 goto nope;640 641 found:642 643 ir = ( p.y - y ) / ( fi[findex].dimensiony + 1 );644 sy = ( p.y - y ) % ( fi[findex].dimensiony + 1 );645 if ( sy >= fi[findex].dimensiony ) goto nope;646647 chanind = ( p.x - x ) / ( fi[findex].dimensionx + 1 );648 sx = ( p.x - x ) % ( fi[findex].dimensionx + 1 );649 if ( sx >= fi[findex].dimensionx ) goto nope;650651 ix = fi[findex].inputrects[ir*2];652 iy = fi[findex].inputrects[ir*2+1];653654 ts = fi[findex].timings + ( ( fi[findex].dimensionx * fi[findex].dimensiony * fi[findex].numtypes * ir ) + ( fi[findex].dimensionx * fi[findex].dimensiony * chanind ) + ( fi[findex].dimensionx * sy ) + sx ) * 2;655656 ox = 1+fi[findex].outputscalex*sx;657 oy = 1+fi[findex].outputscaley*sy;658659 if ( windowstatus != 2 )660 {661 int VF, HF, v_first, good;662 VF = ts[0];663 HF = ts[1];664665 v_first = vert_first( retrain_weights[chanind], ox, oy, ix, iy, STBIR_FILTER_MITCHELL, &v_info );666667 good = ( ((HF<=VF) && (!v_first)) || ((VF<=HF) && (v_first)));668669 if ( good )670 badstr[0] = 0;671 else672 {673 double r;674675 if ( HF < VF )676 r = (double)(VF-HF)/(double)HF;677 else678 r = (double)(HF-VF)/(double)VF;679 sprintf( badstr, " %.1f%% off", r*100 );680 }681 sprintf( buf + l, "\n\n%s\nCh: %d Resize: %dx%d to %dx%d\nV: %d H: %d Order: %c (%s%s)\nClass: %d Scale: %.2f %s", fi[findex].filename,fi[findex].effective[chanind], ix,iy,ox,oy, VF, HF, v_first?'V':'H', good?"Good":"Wrong", badstr, v_info.v_resize_classification, (double)oy/(double)iy, v_info.is_gather ? "Gather" : "Scatter" );682 }683 else684 {685 int v_first, time0, time1;686 float (* weights)[4] = stbir__compute_weights[chanind];687 int * ts1;688 char b0[32], b1[32];689690 ts1 = fi[1].timings + ( ts - fi[0].timings );691692 v_first = vert_first( weights, ox, oy, ix, iy, STBIR_FILTER_MITCHELL, &v_info );693694 time0 = ( v_first ) ? ts[0] : ts[1];695 time1 = ( v_first ) ? ts1[0] : ts1[1];696 697 b0[0] = b1[0] = 0;698 if ( time0 < time1 )699 sprintf( b0," (%.f%% better)", ((double)time1-(double)time0)*100.0f/(double)time0);700 else701 sprintf( b1," (%.f%% better)", ((double)time0-(double)time1)*100.0f/(double)time1);702703 sprintf( buf + l, "\n\n0: %s\n1: %s\nCh: %d Resize: %dx%d to %dx%d\nClass: %d Scale: %.2f %s\nTime0: %d%s\nTime1: %d%s", fi[0].filename, fi[1].filename, fi[0].effective[chanind], ix,iy,ox,oy, v_info.v_resize_classification, (double)oy/(double)iy, v_info.is_gather ? "Gather" : "Scatter", time0, b0, time1, b1 );704 }705 }706 nope:707708 rc.left = 32+320; rc.right = 512+320; 709 SetTextColor( dc, RGB(0,0,128) );710 DrawText( dc, buf, -1, &rc, DT_TOP );711712 }713 EndPaint( window, &ps );714 return 0;715 }716717 case WM_TIMER:718 InvalidateRect( window, 0, 0 );719 return 0;720721 case WM_DESTROY:722 PostQuitMessage( 0 );723 return 0;724 }725 726727 return DefWindowProc( window, message, wparam, lparam );728}729730static void SetHighDPI(void)731{732 typedef HRESULT WINAPI setdpitype(int v);733 HMODULE h=LoadLibrary("Shcore.dll");734 if (h)735 {736 setdpitype * sd = (setdpitype*)GetProcAddress(h,"SetProcessDpiAwareness");737 if (sd )738 sd(1);739 }740} 741742static void draw_window()743{744 WNDCLASS wc;745 HWND w;746 MSG msg;747 748 instance = GetModuleHandle(NULL);749750 wc.style = 0;751 wc.lpfnWndProc = WindowProc;752 wc.cbClsExtra = 0;753 wc.cbWndExtra = 0;754 wc.hInstance = instance;755 wc.hIcon = 0;756 wc.hCursor = LoadCursor(NULL, IDC_ARROW);757 wc.hbrBackground = 0;758 wc.lpszMenuName = 0;759 wc.lpszClassName = "WHTrain";760761 if ( !RegisterClass( &wc ) )762 exit(1);763764 SetHighDPI();765766 bmiHeader.biSize = sizeof(BITMAPINFOHEADER);767 bmiHeader.biWidth = bitmapp/3;768 bmiHeader.biHeight = -bitmaph;769 bmiHeader.biPlanes = 1;770 bmiHeader.biBitCount = 24;771 bmiHeader.biCompression = BI_RGB;772773 w = CreateWindow( "WHTrain",774 "Vertical First Training",775 WS_CAPTION | WS_POPUP| WS_CLIPCHILDREN |776 WS_SYSMENU | WS_MINIMIZEBOX | WS_SIZEBOX,777 CW_USEDEFAULT,CW_USEDEFAULT,778 CW_USEDEFAULT,CW_USEDEFAULT,779 0, 0, instance, 0 );780781 {782 RECT r, c;783 GetWindowRect( w, &r );784 GetClientRect( w, &c );785 extrawindoww = ( r.right - r.left ) - ( c.right - c.left );786 extrawindowh = ( r.bottom - r.top ) - ( c.bottom - c.top );787 SetWindowPos( w, 0, 0, 0, bitmapw * curzoom + extrawindoww, bitmaph * curzoom + extrawindowh + 164, SWP_NOMOVE );788 }789790 ShowWindow( w, SW_SHOWNORMAL );791 SetTimer( w, 1, 250, 0 );792793 {794 BOOL ret;795 while( ( ret = GetMessage( &msg, w, 0, 0 ) ) != 0 )796 { 797 if ( ret == -1 )798 break;799 TranslateMessage( &msg ); 800 DispatchMessage( &msg ); 801 }802 }803}804805static void retrain()806{807 HANDLE threads[ 16 ];808 int chanind;809810 trainstart = GetTickCount();811 for( chanind = 0 ; chanind < fi[0].numtypes ; chanind++ )812 threads[ chanind ] = CreateThread( 0, 2048*1024, retrain_shim, (LPVOID)(size_t)chanind, 0, 0 );813814 draw_window();815816 for( chanind = 0 ; chanind < fi[0].numtypes ; chanind++ )817 {818 WaitForSingleObject( threads[ chanind ], INFINITE );819 CloseHandle( threads[ chanind ] );820 }821822 write_bitmap();823824 print_struct( retrain_weights, "retained_weights" );825 if ( windowstatus ) printf( "CANCELLED!\n" );826}827828static void info()829{830 int findex;831832 // display info about each input file833 for( findex = 0 ; findex < numfileinfo ; findex++ )834 {835 int i, h,m,s;836 if ( findex ) printf( "\n" );837 printf( "Timing file: %s\n", fi[findex].filename );838 printf( "CPU type: %d %s\n", fi[findex].cpu, fi[findex].simd?(fi[findex].simd==2?"SIMD8":"SIMD4"):"Scalar" );839 h = fi[findex].milliseconds/3600000;840 m = (fi[findex].milliseconds-h*3600000)/60000;841 s = (fi[findex].milliseconds-h*3600000-m*60000)/1000;842 printf( "Total time in test: %dh %dm %ds Cycles/sec: %.f\n", h,m,s, 1000.0/fi[findex].scale_time );843 printf( "Each tile of samples is %dx%d, and is scaled by %dx%d.\n", fi[findex].dimensionx,fi[findex].dimensiony, fi[findex].outputscalex,fi[findex].outputscaley );844 printf( "So the x coords are: " );845 for( i=0; i < fi[findex].dimensionx ; i++ ) printf( "%d ",1+i*fi[findex].outputscalex );846 printf( "\n" );847 printf( "And the y coords are: " );848 for( i=0; i < fi[findex].dimensiony ; i++ ) printf( "%d ",1+i*fi[findex].outputscaley );849 printf( "\n" );850 printf( "There are %d channel counts and they are: ", fi[findex].numtypes );851 for( i=0; i < fi[findex].numtypes ; i++ ) printf( "%d ",fi[findex].effective[i] );852 printf( "\n" );853 printf( "There are %d input rect sizes and they are: ", fi[findex].numinputrects );854 for( i=0; i < fi[findex].numtypes ; i++ ) printf( "%dx%d ",fi[findex].inputrects[i*2],fi[findex].inputrects[i*2+1] );855 printf( "\n" );856 }857}858859static void current( int do_win, int do_bitmap )860{861 int i, findex;862863 trainstart = GetTickCount();864865 // clear progress866 memset( windowranges, 0, sizeof( windowranges ) );867 // copy in appropriate weights868 memcpy( retrain_weights, stbir__compute_weights, sizeof( retrain_weights ) );869870 // build and print current errors and build current bitmap871 for( i = 0 ; i < fi[0].numtypes ; i++ )872 {873 double curerr[STBIR_RESIZE_CLASSIFICATIONS];874 int curtot[STBIR_RESIZE_CLASSIFICATIONS];875 float (* weights)[4] = retrain_weights[i];876 877 calc_errors( weights, curtot, curerr, i );878 if ( !do_bitmap )879 print_weights( weights, i, curtot, curerr );880881 for( findex = 0 ; findex < numfileinfo ; findex++ )882 build_bitmap( weights, i, findex );883 }884885 if ( do_win )886 draw_window();887888 if ( do_bitmap )889 write_bitmap();890}891892static void compare()893{894 int i;895896 trainstart = GetTickCount();897 windowstatus = 2; // comp mode898899 // clear progress900 memset( windowranges, 0, sizeof( windowranges ) );901902 if ( ( fi[0].numtypes != fi[1].numtypes ) || ( fi[0].numinputrects != fi[1].numinputrects ) ||903 ( fi[0].dimensionx != fi[1].dimensionx ) || ( fi[0].dimensiony != fi[1].dimensiony ) || 904 ( fi[0].outputscalex != fi[1].outputscalex ) || ( fi[0].outputscaley != fi[1].outputscaley ) )905 {906 err:907 printf( "Timing files don't match.\n" );908 exit(5);909 }910911 for( i=0; i < fi[0].numtypes ; i++ )912 {913 if ( fi[0].effective[i] != fi[1].effective[i] ) goto err;914 if ( fi[0].inputrects[i*2] != fi[1].inputrects[i*2] ) goto err;915 if ( fi[0].inputrects[i*2+1] != fi[1].inputrects[i*2+1] ) goto err;916 }917 918 alloc_bitmap( 1 );919 920 for( i = 0 ; i < fi[0].numtypes ; i++ )921 {922 float (* weights)[4] = stbir__compute_weights[i];923 build_comp_bitmap( weights, i );924 }925926 draw_window();927}928929static void load_files( char ** args, int count )930{931 int i;932933 if ( count == 0 )934 {935 printf( "No timing files listed!" );936 exit(3);937 }938939 for ( i = 0 ; i < count ; i++ )940 {941 if ( !use_timing_file( args[i], i ) )942 {943 printf( "Bad timing file %s\n", args[i] );944 exit(2);945 }946 }947 numfileinfo = count;948} 949950int main( int argc, char ** argv )951{952 int check;953 if ( argc < 3 )954 {955 err:956 printf( "vf_train retrain [timing_filenames....] - recalcs weights for all the files on the command line.\n");957 printf( "vf_train info [timing_filenames....] - shows info about each timing file.\n");958 printf( "vf_train check [timing_filenames...] - show results for the current weights for all files listed.\n");959 printf( "vf_train compare <timing file1> <timing file2> - compare two timing files (must only be two files and same resolution).\n");960 printf( "vf_train bitmap [timing_filenames...] - write out results.png, comparing against the current weights for all files listed.\n");961 exit(1);962 }963 964 check = ( strcmp( argv[1], "check" ) == 0 );965 if ( ( check ) || ( strcmp( argv[1], "bitmap" ) == 0 ) )966 {967 load_files( argv + 2, argc - 2 );968 alloc_bitmap( numfileinfo );969 current( check, !check );970 }971 else if ( strcmp( argv[1], "info" ) == 0 ) 972 {973 load_files( argv + 2, argc - 2 );974 info();975 }976 else if ( strcmp( argv[1], "compare" ) == 0 ) 977 {978 if ( argc != 4 )979 {980 printf( "You must specify two files to compare.\n" );981 exit(4);982 }983984 load_files( argv + 2, argc - 2 );985 compare();986 }987 else if ( strcmp( argv[1], "retrain" ) == 0 ) 988 {989 load_files( argv + 2, argc - 2 );990 alloc_bitmap( numfileinfo );991 retrain(); 992 }993 else994 {995 goto err;996 }997998 return 0;999}