Guest User

Quantization RMSE

a guest
Jul 21st, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.72 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. # Compare the Root Mean Square Errors between these two quantization approaches:
  4. # a) Simple truncation
  5. # b) Truncation combined with error diffusion
  6.  
  7. import random
  8. from math import sqrt
  9.  
  10. # Generate a pixel vector that has at least this length
  11. minimumNumberOfPixelsToGenerate = 500000
  12.  
  13. # Simulate a smooth gradient by appending uniform chunks of identically-valued pixels
  14. # to the pixel vector.  Each chunk has a random width comprised between 5 and 20 pixels.
  15. # A smooth gradient is simulated by enforcing that the value of these uniform chunks must
  16. # randomly differ by +1 or -1 from the preceding chunk.  As an exception, if the previous
  17. # random chunk contained zero-valued pixels, then, as we don't allow negative pixel
  18. # values, the next chunk must containe pixels with a value of one.
  19.  
  20. # We start with an empty vector
  21. pixelVector = []
  22.  
  23. numberOfGeneratedPixels = 0
  24.  
  25. previousPixelValue = random.randint( 0, 15 )
  26.  
  27. while numberOfGeneratedPixels < minimumNumberOfPixelsToGenerate:
  28.  
  29.     widthOfThisChunk = random.randint( 4, 20 )
  30.  
  31.     # The new chunk must contain values that differ from the previous chunk by -1 or +1
  32.     newPixelValue = previousPixelValue + random.choice( [ -1, 1 ] )
  33.  
  34.     if newPixelValue < 0:
  35.         # The previous pixel value must then necessarily have been zero
  36.         newPixelValue = 1
  37.  
  38.     for i in range( widthOfThisChunk ):
  39.         pixelVector.append( newPixelValue )
  40.  
  41.     previousPixelValue = newPixelValue
  42.     numberOfGeneratedPixels += widthOfThisChunk
  43.  
  44. ########### First case: simple truncation of the two lower bits by using the right shift (>>) operator
  45.  
  46. truncatedQuantizedVector = [ p >> 2 for p in pixelVector ]
  47.  
  48. accumulatedSquareError = 0
  49. for i in range( numberOfGeneratedPixels ):
  50.     error = pixelVector[ i ] - truncatedQuantizedVector[ i ] * 4
  51.     accumulatedSquareError += error * error
  52.  
  53. print( "RMSE straightforward truncation:", sqrt( float( accumulatedSquareError ) / numberOfGeneratedPixels ) )
  54.  
  55. ########### Second case: Error diffusion
  56.  
  57. errorDiffusedQuantizedVector = [ 0 for i in range( numberOfGeneratedPixels ) ]
  58.  
  59. accumulatedError = 0
  60. for i in range( numberOfGeneratedPixels ):
  61.  
  62.     quantizedPixel = pixelVector[ i ] >> 2
  63.     error = pixelVector[ i ] - quantizedPixel * 4
  64.     accumulatedError += error
  65.  
  66.     if accumulatedError > 2:
  67.         quantizedPixel += 1
  68.         accumulatedError -= 4
  69.     elif accumulatedError < -2:
  70.         quantizedPixel -= 1
  71.         accumulatedError += 4
  72.  
  73.     errorDiffusedQuantizedVector[ i ] = quantizedPixel
  74.  
  75. accumulatedSquareError = 0
  76. for i in range( numberOfGeneratedPixels ):
  77.     error = pixelVector[ i ] - errorDiffusedQuantizedVector[ i ] * 4
  78.     accumulatedSquareError += error * error
  79.  
  80. print( "RMSE with error diffusion:", sqrt( float( accumulatedSquareError ) / numberOfGeneratedPixels ) )
Advertisement
Add Comment
Please, Sign In to add comment