View Single Post
Old 10th September 2018, 01:26   #9  |  Link
zorr
Registered User
 
Join Date: Mar 2018
Posts: 447
Augmented Script (part 2/2)

Code:
# per frame logging (ssim, time)
delimiter = "; "
resultFile = "D:\optimizer\perFrame.txt"	# output out1="ssim: MAX(float)" out2="time: MIN(time) ms" file="D:\optimizer\perFrame.txt"
WriteFile(resultFile, "current_frame", "delimiter", "ssim", "delimiter", "avstimer")
The current frame number, per frame ssim and runtime values are written to a file, separated by ";". The comment starting "# output" is another important input for the optimizer. Here we define the output values of the script in a special format. The first output is defined with out1="...", the second with out2="..." and so on. The format is "<name>: <goal>(<type>) [<unit>]". <name> is the name of the output, this is used in the visualization. <goal> is either MIN or MAX (or a lowercase variation) and tells whether we want to minimize or maximize this value. <type> can be float, int or time. Time is a float but it will be rounded to 10 milliseconds (this is the default and can be changed). The reason for the rounding is that the time measurement itself is not very accurate and a very small difference in the timing is not very important anyway. For example if we have to scripts with the same quality but the other is one millisecond faster that probably isn't a reason to consider it better (and we can't be sure which one is really faster). <unit> is an optional name for the unit. It will be used in the visualization if it's defined. All of this can also be defined in another, more verbose format. For example we could define out1="ssim: MAX(float)" with "name1=ssim, goal1=MAX, type1=float". In this case I didn't use quotes, they are not necessary if the values don't contain space characters. The rounding can be defined with roundx="...", for example "round2=10".

The "file=" -definition is the only compulsory output parameter, the others have default values. The optimizer will change the file name where the results are written for every new script that it creates. For that reason it needs to know what part of the text needs to be replaced with a new file name. We just need to repeat the same file name which is used as the resultFile's value.

Code:
# write "stop" at the last frame to tell the optimizer that the script has finished
frame_count = FrameCount()
WriteFileIf(resultFile, "current_frame == frame_count-1", """ "stop " """, "ssim_total", append=true)
When the script is finished it writes a line starting with "stop". This is needed to tell the optimizer that the script is finished processing and the result is ready for consumption. The optimizer is polling the file every 0.1 seconds until this line is found. I was hoping to use a more sane technique but as of now this is how it works. The polling interval is not too extreme so it doesn't use much CPU or I/O but also frequent enough so that we can get the results rapidly. Maybe the polling interval should be customizable. The code above also writes the total ssim value but does NOT write a total runtime. If the runtime were written also the optimizer would read the result using these values from the last line. If the last line doesn't contain enough values to read (in this case two would be needed, one for each output) it reads all the lines and sums the values there. In some cases the script might calculate the final result using a different logic than simple addition, it can then write the result as the last line. In that case the individual lines are not even needed so the first WriteFile() could be removed.

Code:
# return original and reconstructed frame side by side for comparison
#return StackHorizontal(orig_yv12, inter_yv12)

# NOTE: must return last or FrameEvaluate will not run
return last
The script must return "last" in order to work properly (FrameEvaluate is not called otherwise). If you're debugging the script and returning for example a comparison frame with original and reconstructed frame just remember to change it back to "last" when you're done.

The results-file will look like this:

Code:
0; 0.754317; 164.351318
1; 0.859464; 92.895966
2; 0.805696; 67.377174
3; 0.744211; 64.517632
4; 0.684871; 60.627941
5; 0.821181; 63.919125
6; 0.919067; 57.346405
7; 0.842346; 57.677219
8; 0.833121; 65.297905
9; 0.787272; 59.674370
stop 8.051547
All right, that's enough for today. Tomorrow I will cover how to run the optimizer and then add the download package.
zorr is offline   Reply With Quote