简体   繁体   中英

tensorflow lite conversion changes model weights

I have a model saved in .pb file and it works fine, but when I convert it to tflite model with command tflite_convert or python api, the result is wrong. And I find that the weights have been changed after conversion. The weights of first convolution layer in .pb file are as follows:

shape - 
(3, 3, 1, 8)
Value - 
[[[[-0.09953183  0.11656161  0.1101007  -0.02618909 -0.21355744
    -0.05877252  0.11881053 -0.17588891]]

  [[-0.16565287  0.16550814  0.02200373  0.0987333   0.0194475
    -0.12387082 -0.06090429 -0.19122925]]

  [[-0.19570269  0.11854213 -0.14988026 -0.01476914  0.12554781
    -0.1324673  -0.04035608 -0.05299769]]]


 [[[ 0.08548407 -0.09644134  0.24321978  0.15008359 -0.2591259
     0.2421266   0.02051029 -0.05138292]]

  [[ 0.04847065 -0.22357103 -0.00074622  0.19842042  0.00228794
     0.13352048 -0.24048899 -0.00679056]]

  [[-0.01857976 -0.09324262 -0.19632849  0.02247559  0.18489467
    -0.07365554 -0.39479995  0.0622104 ]]]


 [[[ 0.13633308  0.04041797  0.10581032 -0.13119537  0.01122213
     0.15191257  0.03097369  0.07342041]]

  [[ 0.16241515 -0.04534301 -0.06334146 -0.19276966 -0.03890191
     0.08520683 -0.0117504   0.14705475]]

  [[ 0.07332639 -0.00533756 -0.06285968 -0.12631118  0.09094885
    -0.09658462 -0.04983746  0.13325559]]]]

And The weights of first convolution layer in .tflite file are as follows:

shape - 
(1, 3, 3, 8)
Value - 
[[[[ -31.412575     42.14294      36.269154     -8.724394
     -67.77634     -12.249788     43.18692     -76.762474  ]
   [ -52.280594     59.839592      7.248423     32.89111
       6.1720166   -25.818043    -22.138346    -83.4574    ]
   [ -61.764416     42.858997    -49.373257     -4.9200554
      39.844883    -27.609783    -14.669194    -23.129562  ]]

  [[  26.979053    -34.86844      80.12097      49.997475
     -82.23831      50.46576       7.4553676   -22.424837  ]
   [  15.297499    -80.832275     -0.24581672   66.09997
       0.7261198    27.829294    -87.41631      -2.963576  ]
   [  -5.8638325   -33.71194     -64.67414       7.487314
      58.679688    -15.351814   -143.50742      27.15023   ]]

  [[  43.02717      14.613146     34.855824    -43.705227
       3.561546     31.662704     11.25875      32.04257   ]
   [  51.258755    -16.393799    -20.865818    -64.21752
     -12.346229     17.759417     -4.2712016    64.1785    ]
   [  23.14205      -1.929798    -20.70711     -42.07815
      28.864273    -20.130857    -18.115618     58.15619   ]]]]

It seems there are some relations.

the tensorflow version is 1.12.

The command is

tflite_convert --output_file=graph_net_half.tflite --graph_def_file=graph_net_half.pb  --input_arrays=input_image --output_arrays=output_landmark 

there is another similar question with no answer: tflite weights

It's expect that the weights will be changed when running tflite_convert for several reasons:

  • TensorFlow Conv2D uses HWIO weights (filter_height, filter_weight, input_channels, output_channels). TensorFlow Lite Conv2D uses IHWO weights for optimization reasons. The axis of weights must be reordered.
  • tflite_convert does optimizations like constant folding, which will also change the weights.

Seeing different weights doesn't mean the conversion is wrong.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM