负载模型在tensorflow 2.9.1中失败

tquggr8v  于 2022-12-30  发布在  其他
关注(0)|答案(1)|浏览(150)

我尝试加载tensorflow 2.9.1中实现的模型的权重,但失败了
我使用model.save_weights("./saved_model/model")保存了模型
并使用加载模型

model = DepthEstimationModel()
model.load_weights(os.path.join("saved_model", "model"))
model.compile(optimizer, loss=cross_entropy)
model.built = True
model.summary()

它表明

Model: "depth_estimation_model"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 downscale_block (DownscaleB  multiple                 0 (unused)
 lock)                                                           
                                                                 
 downscale_block_1 (Downscal  multiple                 0 (unused)
 eBlock)                                                         
                                                                 
 downscale_block_2 (Downscal  multiple                 0 (unused)
 eBlock)                                                         
                                                                 
 downscale_block_3 (Downscal  multiple                 0 (unused)
 eBlock)                                                         
                                                                 
 bottle_neck_block (BottleNe  multiple                 0 (unused)
 ckBlock)                                                        
                                                                 
 upscale_block (UpscaleBlock  multiple                 0 (unused)
 )                                                               
                                                                 
 upscale_block_1 (UpscaleBlo  multiple                 0 (unused)
 ck)                                                             
                                                                 
 upscale_block_2 (UpscaleBlo  multiple                 0 (unused)
 ck)                                                             
                                                                 
 upscale_block_3 (UpscaleBlo  multiple                 0 (unused)
 ck)                                                             
                                                                 
 conv2d_18 (Conv2D)          multiple                  0 (unused)
                                                                 
=================================================================
Total params: 2
Trainable params: 0
Non-trainable params: 2

这表明参数未正确加载。
如果删除model.built = True,则输出:

This model has not yet been built. Build the model first by calling `build()` or by calling the model on a batch of data.

参考:Tensorflow 2.0 ValueError while Loading weights from .h5 file

pbossiut

pbossiut1#

在加载权重之前,你必须编译你的模型,然后你必须加载权重。

model.compile(optimizer, loss=cross_entropy)
model.load_weights(os.path.join("saved_model", "model"))

那你就不会面对那个错误了,谢谢。

相关问题