我使用keras'1.2.2'和theano'0.9.0'后端训练了一个模型,用于二进制图像分类,然后将其另存为“ .h5”文件。当我调用它并使用'predict_classes'预测图像的类别时,我会获得令人满意的结果。我什至可以通过tensorflow后端获得正确的预测。
但是在将Keras版本升级到“ 2.2.2”之后,由于该模型未经过训练,因此预测会产生随机结果。
在搜索该问题时,我发现这很重要,因为在keras更新期间某些基本功能已更改。
要检查已保存模型的权重,我使用以下代码在keras版本“ 1.2.2”和“ 2.2.2”中加载了该代码:(例如,模型的第二层)
model_filename ='/home/PC/model'
img_width,img_height = 96,96
try:
My_Model = load_model(model_filename,img_width,img_height)
print('Successfully loaded ;)')
except (ImportError, ValueError, OSError):
print('Failed to load')
weights = My_Model.layers[1].get_weights()
print(weights)
两种型号的权重都不同
对于keras“ 1.2.2”
[[[[ 2.67727766e-02 -1.02486722e-01 -1.13446461e-02]
[-9.90765542e-02 -2.62213908e-02 1.99221164e-01]
[-6.49987534e-03 2.11890116e-01 1.66391402e-01]]
[[ 1.01199998e-02 3.27988947e-03 -5.50620724e-03]
[ 5.40793501e-03 -4.08519944e-03 -6.78007258e-04]
[-3.23061738e-03 -1.89595227e-03 1.02263000e-02]]
[[ 3.52744982e-02 -1.69398095e-02 -1.16202254e-02]
[-2.49999501e-02 -3.99940647e-02 1.98777020e-02]
[-3.26521844e-02 2.10426636e-02 6.18882850e-02]]
...
[[-2.70411950e-02 -6.46410929e-03 -1.77911445e-02]
[-1.06132298e-03 -1.92066245e-02 -1.60754975e-02]
[ 4.76289587e-03 -4.20227554e-03 1.75879453e-03]]
[[ 2.57274956e-02 -2.26275213e-02 -1.34805087e-02]
[-5.42348344e-03 -8.97927806e-02 -1.42245442e-02]
[-7.87555277e-02 -3.25850062e-02 5.93977869e-02]]
[[ 7.87358284e-02 -2.68406887e-02 -2.92847957e-02]
[-3.89512442e-02 -1.00802995e-01 2.51645613e-02]
[-7.90851712e-02 1.62124224e-02 1.33138046e-01]]]
[[[-3.53743099e-02 -5.24977744e-02 -4.41135280e-03]
[-5.73175363e-02 -4.95060943e-02 5.05086929e-02]
[-2.26958226e-02 2.10389420e-02 1.42894983e-01]]
[[ 9.88597982e-03 9.13118385e-03 3.73593764e-03]
[ 1.06699411e-02 9.40596592e-03 3.71209672e-03]
[ 8.12445208e-03 6.70108804e-03 3.40487971e-03]]
[[-7.20167905e-03 -1.13335364e-02 -1.15232135e-04]
[-5.87605173e-03 -1.61704123e-02 3.97728896e-03]
[-8.37239902e-03 -7.90210068e-03 2.41603516e-02]]
...
[[-2.66054031e-02 3.18636298e-02 4.96573895e-02]
[ 1.46111343e-02 6.95549399e-02 7.50007555e-02]
[ 2.78785601e-02 7.16475174e-02 8.15759301e-02]]
[[-4.82842401e-02 -4.99468371e-02 5.63178305e-03]
[-3.99397127e-02 -4.80442867e-02 1.58883426e-02]
[-1.97574776e-02 -2.43974980e-02 4.93397191e-02]]
[[-2.94181313e-02 -3.55718695e-02 -4.86502051e-03]
[-1.87499989e-02 -3.61651815e-02 6.27049711e-03]
[-2.46286895e-02 -2.34574061e-02 4.93806675e-02]]]
[[[-1.68556359e-03 -1.32390782e-02 -1.01792878e-02]
[-7.43501680e-03 -2.76938621e-02 -2.30944753e-02]
[ 1.81745703e-03 -1.84992775e-02 -8.04548990e-03]]
[[ 4.93210778e-02 6.36478961e-02 4.72785495e-02]
[ 6.55914098e-02 8.05444270e-02 6.32954985e-02]
[ 5.01842983e-02 6.42350838e-02 4.70451042e-02]]
[[-1.20003149e-02 -1.56359691e-02 -1.86928231e-02]
[-1.60366632e-02 -2.14026086e-02 -2.50869785e-02]
[-9.71693918e-03 -2.00182814e-02 -2.36944854e-02]]
...
[[-2.92959549e-02 -2.28327084e-02 -2.82175522e-02]
[-2.70110406e-02 -2.22128499e-02 -2.88447570e-02]
[-3.69955264e-02 -3.51521894e-02 -4.25063968e-02]]
[[-5.84619679e-03 -1.76020507e-02 -1.14910984e-02]
[-3.42250685e-03 -1.71911847e-02 -1.57482717e-02]
[ 1.91919934e-02 -7.55109172e-03 -6.50930917e-03]]
[[-2.25112378e-03 -1.84209254e-02 -1.82535443e-02]
[-1.01924269e-02 -2.84045860e-02 -3.23592126e-02]
[ 1.09742424e-02 -1.75609868e-02 -2.16843132e-02]]]
...
[[[-4.58369323e-04 -1.45500358e-02 2.46625743e-03]
[-2.18929406e-02 -2.38806177e-02 1.54738650e-02]
[-2.83873989e-04 2.17282958e-02 6.01423718e-02]]
[[-2.29836330e-02 -2.85153724e-02 -2.64862049e-02]
[-3.20399888e-02 -3.73557620e-02 -3.46410535e-02]
[-3.15360427e-02 -3.60929109e-02 -3.22993100e-02]]
[[-7.00323703e-03 -9.29493643e-03 -4.52254852e-03]
[-1.01411417e-02 -1.14088962e-02 -5.91889396e-03]
[-8.98361299e-03 -8.70704185e-03 5.47777396e-04]]
...
[[-4.81438916e-03 -1.94843276e-03 -6.82928134e-04]
[-6.04649493e-03 -4.11749631e-03 -6.68567093e-03]
[-8.64188187e-03 -9.57136974e-03 -1.05011780e-02]]
[[-5.59144933e-03 -1.44957015e-02 2.87879328e-03]
[ 3.25045366e-05 -8.99286382e-03 1.07123004e-02]
[ 1.01352436e-02 -4.71479492e-03 1.37759540e-02]]
[[ 1.31981692e-03 -3.83399660e-03 1.00931199e-02]
[ 4.93809523e-04 -7.13844784e-03 1.09553514e-02]
[-4.36017243e-03 -6.44172216e-03 2.21460108e-02]]]
[[[ 4.26713824e-02 -5.17829247e-02 -6.76089302e-02]
[-1.17408615e-02 1.85385011e-02 -1.39560746e-02]
[-5.09968363e-02 9.76121705e-03 -1.99724492e-02]]
[[-2.22006021e-03 -2.39412911e-04 -1.90242229e-03]
[-3.69457784e-03 2.68332846e-03 -6.24640437e-04]
[-5.09824464e-03 9.52110917e-04 8.79028579e-04]]
[[ 1.18437530e-02 -5.12046963e-02 -4.37613130e-02]
[ 3.98636535e-02 2.82259062e-02 -3.58597338e-02]
[-1.74475163e-02 3.85886803e-02 3.14726122e-03]]
...
[[-1.01121636e-02 -1.31027400e-02 1.89607905e-03]
[ 6.46430627e-03 1.34903826e-02 -1.42347347e-02]
[ 2.49886862e-03 1.12614958e-02 -9.83303227e-03]]
[[ 3.87255847e-02 -9.59163606e-02 -3.22180651e-02]
[ 2.04146236e-01 5.86615279e-02 -8.65866616e-02]
[ 5.25022745e-02 2.00507134e-01 2.85513420e-02]]
[[ 3.96059677e-02 -1.03656650e-01 -7.78649971e-02]
[ 1.37873694e-01 8.89248997e-02 -7.60490298e-02]
[-2.82266792e-02 1.26404390e-01 2.78986227e-02]]]
[[[ 1.28794506e-01 1.31120667e-01 9.13364366e-02]
[ 6.09769784e-02 2.21851412e-02 6.90997764e-03]
[ 6.96227793e-03 -2.55617052e-02 -2.46680304e-02]]
[[-8.78862944e-03 -8.76607560e-03 -7.25985179e-03]
[-3.50507535e-03 -2.47649150e-03 -2.27161159e-04]
[3.45510198e-03 5.24202967e-03 7.66773988e-03]]
[[ 8.38499237e-03 1.32070621e-02 1.15254000e-02]
[ 1.22498199e-02 4.18237131e-03 -4.24729718e-04]
[ 8.88461992e-03 -1.07725267e-03 -4.37496928e-04]]
...
[[-4.55849916e-02 -4.30879407e-02 -3.36049721e-02]
[-6.01397408e-03 3.48995999e-03 6.75049471e-03]
[ 1.03886742e-02 1.86380167e-02 1.75387561e-02]]
[[ 7.35910088e-02 5.95212653e-02 4.05755229e-02]
[-1.49060423e-02 -4.11915369e-02 -4.10984270e-02]
[-1.91051178e-02 -4.60702106e-02 -3.80814970e-02]]
[[ 2.01520249e-02 2.33896598e-02 2.09293440e-02]
[ 1.53205004e-02 -6.61500823e-03 -1.63844116e-02]
[ 9.37470049e-03 -2.10408997e-02 -1.69835445e-02]]]]
[-0.30960736 0.3628977 0.13367908 0.07859909 0.8951073 0.08816771
0.10718324 -0.23031554 -0.630525 0.17874952 -0.33495104 0.19398735
0.43630022 0.19260858 0.11047334 0.1057681 -1.0240598 0.05172339
0.12835366 0.22596645 -0.7150005 0.21419501 -0.6962017 0.04443557
0.1384489 0.2827491 0.35205162 0.38443682 0.4316273 -0.4835703
0.02482753 0.09119287 0.04995462 0.54926634 0.3225261 0.07108227
-0.1776137 0.3752207 0.39340425 0.17539951 -0.15969473 0.25720754
0.08045568 0.08533712 -0.094541 0.23051816 0.29588515 0.13437031
-0.3775823 0.11423616 -0.53577447 0.47629428 -0.07748792 0.29809955
0.13603118 0.16250613 0.23968525 0.22908202 -0.1955172 0.01456635
0.27697623 0.03302503 0.0587023 -0.57805544]
对于keras'2.2.2,我得到了:
[[[[ 2.67727766e-02 -3.53743099e-02 -1.68556359e-03 ... -4.58369323e-04
4.26713824e-02 1.28794506e-01]
[ 1.01199998e-02 9.88597982e-03 4.93210778e-02 ... -2.29836330e-02
-2.22006021e-03 -8.78862944e-03]
[ 3.52744982e-02 -7.20167905e-03 -1.20003149e-02 ... -7.00323703e-03
1.18437530e-02 8.38499237e-03]
...
[-2.70411950e-02 -2.66054031e-02 -2.92959549e-02 ... -4.81438916e-03
-1.01121636e-02 -4.55849916e-02]
[ 2.57274956e-02 -4.82842401e-02 -5.84619679e-03 ... -5.59144933e-03
3.87255847e-02 7.35910088e-02]
[ 7.87358284e-02 -2.94181313e-02 -2.25112378e-03 ... 1.31981692e-03
3.96059677e-02 2.01520249e-02]]
[[-1.02486722e-01 -5.24977744e-02 -1.32390782e-02 ... -1.45500358e-02
-5.17829247e-02 1.31120667e-01]
[ 3.27988947e-03 9.13118385e-03 6.36478961e-02 ... -2.85153724e-02
-2.39412911e-04 -8.76607560e-03]
[-1.69398095e-02 -1.13335364e-02 -1.56359691e-02 ... -9.29493643e-03
-5.12046963e-02 1.32070621e-02]
...
[-6.46410929e-03 3.18636298e-02 -2.28327084e-02 ... -1.94843276e-03
-1.31027400e-02 -4.30879407e-02]
[-2.26275213e-02 -4.99468371e-02 -1.76020507e-02 ... -1.44957015e-02
-9.59163606e-02 5.95212653e-02]
[-2.68406887e-02 -3.55718695e-02 -1.84209254e-02 ... -3.83399660e-03
-1.03656650e-01 2.33896598e-02]]
[[-1.13446461e-02 -4.41135280e-03 -1.01792878e-02 ... 2.46625743e-03
-6.76089302e-02 9.13364366e-02]
[-5.50620724e-03 3.73593764e-03 4.72785495e-02 ... -2.64862049e-02
-1.90242229e-03 -7.25985179e-03]
[-1.16202254e-02 -1.15232135e-04 -1.86928231e-02 ... -4.52254852e-03
-4.37613130e-02 1.15254000e-02]
...
[-1.77911445e-02 4.96573895e-02 -2.82175522e-02 ... -6.82928134e-04
1.89607905e-03 -3.36049721e-02]
[-1.34805087e-02 5.63178305e-03 -1.14910984e-02 ... 2.87879328e-03
-3.22180651e-02 4.05755229e-02]
[-2.92847957e-02 -4.86502051e-03 -1.82535443e-02 ... 1.00931199e-02
-7.78649971e-02 2.09293440e-02]]]
[[[-9.90765542e-02 -5.73175363e-02 -7.43501680e-03 ... -2.18929406e-02
-1.17408615e-02 6.09769784e-02]
[ 5.40793501e-03 1.06699411e-02 6.55914098e-02 ... -3.20399888e-02
-3.69457784e-03 -3.50507535e-03]
[-2.49999501e-02 -5.87605173e-03 -1.60366632e-02 ... -1.01411417e-02
3.98636535e-02 1.22498199e-02]
...
[-1.06132298e-03 1.46111343e-02 -2.70110406e-02 ... -6.04649493e-03
6.46430627e-03 -6.01397408e-03]
[-5.42348344e-03 -3.99397127e-02 -3.42250685e-03 ... 3.25045366e-05
2.04146236e-01 -1.49060423e-02]
[-3.89512442e-02 -1.87499989e-02 -1.01924269e-02 ... 4.93809523e-04
1.37873694e-01 1.53205004e-02]]
[[-2.62213908e-02 -4.95060943e-02 -2.76938621e-02 ... -2.38806177e-02
1.85385011e-02 2.21851412e-02]
[-4.08519944e-03 9.40596592e-03 8.05444270e-02 ... -3.73557620e-02
2.68332846e-03 -2.47649150e-03]
[-3.99940647e-02 -1.61704123e-02 -2.14026086e-02 ... -1.14088962e-02
2.82259062e-02 4.18237131e-03]
...
[-1.92066245e-02 6.95549399e-02 -2.22128499e-02 ... -4.11749631e-03
1.34903826e-02 3.48995999e-03]
[-8.97927806e-02 -4.80442867e-02 -1.71911847e-02 ... -8.99286382e-03
5.86615279e-02 -4.11915369e-02]
[-1.00802995e-01 -3.61651815e-02 -2.84045860e-02 ... -7.13844784e-03
8.89248997e-02 -6.61500823e-03]]
[[ 1.99221164e-01 5.05086929e-02 -2.30944753e-02 ... 1.54738650e-02
-1.39560746e-02 6.90997764e-03]
[-6.78007258e-04 3.71209672e-03 6.32954985e-02 ... -3.46410535e-02
-6.24640437e-04 -2.27161159e-04]
[ 1.98777020e-02 3.97728896e-03 -2.50869785e-02 ... -5.91889396e-03
-3.58597338e-02 -4.24729718e-04]
...
[-1.60754975e-02 7.50007555e-02 -2.88447570e-02 ... -6.68567093e-03
-1.42347347e-02 6.75049471e-03]
[-1.42245442e-02 1.58883426e-02 -1.57482717e-02 ... 1.07123004e-02
-8.65866616e-02 -4.10984270e-02]
[ 2.51645613e-02 6.27049711e-03 -3.23592126e-02 ... 1.09553514e-02
-7.60490298e-02 -1.63844116e-02]]]
[[[-6.49987534e-03 -2.26958226e-02 1.81745703e-03 ... -2.83873989e-04
-5.09968363e-02 6.96227793e-03]
[-3.23061738e-03 8.12445208e-03 5.01842983e-02 ... -3.15360427e-02
-5.09824464e-03 3.45510198e-03]
[-3.26521844e-02 -8.37239902e-03 -9.71693918e-03 ... -8.98361299e-03
-1.74475163e-02 8.88461992e-03]
...
[ 4.76289587e-03 2.78785601e-02 -3.69955264e-02 ... -8.64188187e-03
2.49886862e-03 1.03886742e-02]
[-7.87555277e-02 -1.97574776e-02 1.91919934e-02 ... 1.01352436e-02
5.25022745e-02 -1.91051178e-02]
[-7.90851712e-02 -2.46286895e-02 1.09742424e-02 ... -4.36017243e-03
-2.82266792e-02 9.37470049e-03]]
[[ 2.11890116e-01 2.10389420e-02 -1.84992775e-02 ... 2.17282958e-02
9.76121705e-03 -2.55617052e-02]
[-1.89595227e-03 6.70108804e-03 6.42350838e-02 ... -3.60929109e-02
9.52110917e-04 5.24202967e-03]
[ 2.10426636e-02 -7.90210068e-03 -2.00182814e-02 ... -8.70704185e-03
3.85886803e-02 -1.07725267e-03]
...
[-4.20227554e-03 7.16475174e-02 -3.51521894e-02 ... -9.57136974e-03
1.12614958e-02 1.86380167e-02]
[-3.25850062e-02 -2.43974980e-02 -7.55109172e-03 ... -4.71479492e-03
2.00507134e-01 -4.60702106e-02]
[ 1.62124224e-02 -2.34574061e-02 -1.75609868e-02 ... -6.44172216e-03
1.26404390e-01 -2.10408997e-02]]
[[ 1.66391402e-01 1.42894983e-01 -8.04548990e-03 ... 6.01423718e-02
-1.99724492e-02 -2.46680304e-02]
[ 1.02263000e-02 3.40487971e-03 4.70451042e-02 ... -3.22993100e-02
8.79028579e-04 7.66773988e-03]
[ 6.18882850e-02 2.41603516e-02 -2.36944854e-02 ... 5.47777396e-04
3.14726122e-03 -4.37496928e-04]
...
[ 1.75879453e-03 8.15759301e-02 -4.25063968e-02 ... -1.05011780e-02
-9.83303227e-03 1.75387561e-02]
[ 5.93977869e-02 4.93397191e-02 -6.50930917e-03 ... 1.37759540e-02
2.85513420e-02 -3.80814970e-02]
[ 1.33138046e-01 4.93806675e-02 -2.16843132e-02 ... 2.21460108e-02
2.78986227e-02 -1.69835445e-02]]]]
[-0.30960736 0.3628977 0.13367908 0.07859909 0.8951073 0.08816771
0.10718324 -0.23031554 -0.630525 0.17874952 -0.33495104 0.19398735
0.43630022 0.19260858 0.11047334 0.1057681 -1.0240598 0.05172339
0.12835366 0.22596645 -0.7150005 0.21419501 -0.6962017 0.04443557
0.1384489 0.2827491 0.35205162 0.38443682 0.4316273 -0.4835703
0.02482753 0.09119287 0.04995462 0.54926634 0.3225261 0.07108227
-0.1776137 0.3752207 0.39340425 0.17539951 -0.15969473 0.25720754
0.08045568 0.08533712 -0.094541 0.23051816 0.29588515 0.13437031
-0.3775823 0.11423616 -0.53577447 0.47629428 -0.07748792 0.29809955
0.13603118 0.16250613 0.23968525 0.22908202 -0.1955172 0.01456635
0.27697623 0.03302503 0.0587023 -0.57805544]
请问有人知道为什么权重值改变了吗?以及如何在keras'2.2.2'中获得正确的权重
谢谢!!