尝试从 h5 文件加载保存的 Keras U-Net 模型时出现问题

问题描述 投票:0回答:1

我在公司被分配了一项任务,尝试水合为之前项目训练的模型,虽然我可以再次加载它,但我无法尝试它,我不知道为什么。

该模型遵循 U-Net 架构,这是调用

summary()
load_weights()
方法的输出。

Model: "model"
__________________________________________________________________________________________________
 Layer (type)                   Output Shape         Param #     Connected to                     
==================================================================================================
 input_1 (InputLayer)           [(None, 640, 512, 1  0           []                               
                                )]                                                                
                                                                                                  
 conv2d (Conv2D)                (None, 640, 512, 64  640         ['input_1[0][0]']                
                                )                                                                 
                                                                                                  
 conv2d_1 (Conv2D)              (None, 640, 512, 64  36928       ['conv2d[0][0]']                 
                                )                                                                 
                                                                                                  
 max_pooling2d (MaxPooling2D)   (None, 320, 256, 64  0           ['conv2d_1[0][0]']               
                                )                                                                 
                                                                                                  
 conv2d_2 (Conv2D)              (None, 320, 256, 12  73856       ['max_pooling2d[0][0]']          
                                8)                                                                
                                                                                                  
 conv2d_3 (Conv2D)              (None, 320, 256, 12  147584      ['conv2d_2[0][0]']               
                                8)                                                                
                                                                                                  
 max_pooling2d_1 (MaxPooling2D)  (None, 160, 128, 12  0          ['conv2d_3[0][0]']               
                                8)                                                                
                                                                                                  
 conv2d_4 (Conv2D)              (None, 160, 128, 25  295168      ['max_pooling2d_1[0][0]']        
                                6)                                                                
                                                                                                  
 conv2d_5 (Conv2D)              (None, 160, 128, 25  590080      ['conv2d_4[0][0]']               
                                6)                                                                
                                                                                                  
 max_pooling2d_2 (MaxPooling2D)  (None, 80, 64, 256)  0          ['conv2d_5[0][0]']               
                                                                                                  
 conv2d_6 (Conv2D)              (None, 80, 64, 512)  1180160     ['max_pooling2d_2[0][0]']        
                                                                                                  
 conv2d_7 (Conv2D)              (None, 80, 64, 512)  2359808     ['conv2d_6[0][0]']               
                                                                                                  
 max_pooling2d_3 (MaxPooling2D)  (None, 40, 32, 512)  0          ['conv2d_7[0][0]']               
                                                                                                  
 conv2d_8 (Conv2D)              (None, 40, 32, 1024  4719616     ['max_pooling2d_3[0][0]']        
                                )                                                                 
                                                                                                  
 conv2d_9 (Conv2D)              (None, 40, 32, 1024  9438208     ['conv2d_8[0][0]']               
                                )                                                                 
                                                                                                  
 up_sampling2d (UpSampling2D)   (None, 80, 64, 1024  0           ['conv2d_9[0][0]']               
                                )                                                                 
                                                                                                  
 concatenate (Concatenate)      (None, 80, 64, 1536  0           ['up_sampling2d[0][0]',          
                                )                                 'conv2d_7[0][0]']               
                                                                                                  
 conv2d_10 (Conv2D)             (None, 80, 64, 512)  7078400     ['concatenate[0][0]']            
                                                                                                  
 conv2d_11 (Conv2D)             (None, 80, 64, 512)  2359808     ['conv2d_10[0][0]']              
                                                                                                  
 up_sampling2d_1 (UpSampling2D)  (None, 160, 128, 51  0          ['conv2d_11[0][0]']              
                                2)                                                                
                                                                                                  
 concatenate_1 (Concatenate)    (None, 160, 128, 76  0           ['up_sampling2d_1[0][0]',        
                                8)                                'conv2d_5[0][0]']               
                                                                                                  
 conv2d_12 (Conv2D)             (None, 160, 128, 25  1769728     ['concatenate_1[0][0]']          
                                6)                                                                
                                                                                                  
 conv2d_13 (Conv2D)             (None, 160, 128, 25  590080      ['conv2d_12[0][0]']              
                                6)                                                                
                                                                                                  
 up_sampling2d_2 (UpSampling2D)  (None, 320, 256, 25  0          ['conv2d_13[0][0]']              
                                6)                                                                
                                                                                                  
 concatenate_2 (Concatenate)    (None, 320, 256, 38  0           ['up_sampling2d_2[0][0]',        
                                4)                                'conv2d_3[0][0]']               
                                                                                                  
 conv2d_14 (Conv2D)             (None, 320, 256, 12  442496      ['concatenate_2[0][0]']          
                                8)                                                                
                                                                                                  
 conv2d_15 (Conv2D)             (None, 320, 256, 12  147584      ['conv2d_14[0][0]']              
                                8)                                                                
                                                                                                  
 up_sampling2d_3 (UpSampling2D)  (None, 640, 512, 12  0          ['conv2d_15[0][0]']              
                                8)                                                                
                                                                                                  
 concatenate_3 (Concatenate)    (None, 640, 512, 19  0           ['up_sampling2d_3[0][0]',        
                                2)                                'conv2d_1[0][0]']               
                                                                                                  
 conv2d_16 (Conv2D)             (None, 640, 512, 64  110656      ['concatenate_3[0][0]']          
                                )                                                                 
                                                                                                  
 conv2d_17 (Conv2D)             (None, 640, 512, 64  36928       ['conv2d_16[0][0]']              
                                )                                                                 
                                                                                                  
 conv2d_18 (Conv2D)             (None, 640, 512, 1)  65          ['conv2d_17[0][0]']              
                                                                                                  
==================================================================================================
Total params: 31,377,793
Trainable params: 31,377,793
Non-trainable params: 0
__________________________________________________________________________________________________

我主要担心的是,当我将图片加载为 numpy 数组时,最终得到输入形状 (640, 512, 1),就像第一层一样,我收到以下错误。

from tensorflow.keras.preprocessing.image import load_img, img_to_array

img_size = (640,512)
color_mode = "grayscale" 
image = img_to_array(load_img(image_path, target_size=self.image_size, color_mode=self.color_mode))
image = image/255.0
print(image.shape)
#(640, 512, 1)

#unet is a wrapper class which contains the model described above
#unet.load_weights('../../models/unet_model_i_04.h5')
unet.model.predict(image)

此片段产生此错误:

ValueError: Input 0 of layer "model" is incompatible with the layer: expected shape=(None, 640, 512, 1), found shape=(32, 512, 1)

我尝试更改输入的形状,从而更改图像大小(我怀疑,基于我被授予访问权限的过时笔记本,当模型最初训练和导出时,它是使用 320x256 图像完成的,但是仅将错误更改为

ValueError: Input 0 of layer "model" is incompatible with the layer: expected shape=(None, 320, 256, 1), found shape=(32, 256, 1)
python tensorflow keras deep-learning unet-neural-network
1个回答
0
投票

正如有人在评论中建议的那样,尝试扩大输入张量的维度。该模型期望一批图像作为输入,对于一个图像,输入形状应该是

1 x H x W x 1
:

from tensorflow.keras.preprocessing.image import load_img, img_to_array

img_size = (640,512)
color_mode = "grayscale" 
image = img_to_array(load_img(image_path, target_size=imge_size, color_mode=color_mode))
image = image/255.0
image = tf.expand_dims(image, 0)
print(image.shape)
#(1, 640, 512, 1)

#unet is a wrapper class which contains the model described above
#unet.load_weights('../../models/unet_model_i_04.h5')
unet.model.predict(image)
© www.soinside.com 2019 - 2024. All rights reserved.