无法在仅 TensorFlow CPU 版本中加载模型

Posted

技术标签:

【中文标题】无法在仅 TensorFlow CPU 版本中加载模型【英文标题】:Unable to load model in Tensorflow CPU only version 【发布时间】:2021-05-24 17:15:54 【问题描述】:

环境:

Tensorflow:2.3.0(仅限 CPU) Python:3.8.5 GPU: 0 操作系统:Ubuntu 20.04 LTS

问题陈述:

我很抱歉问了另一个新手问题,但我正在尝试使用 Tensorflow (仅限 CPU 版本) 中的 load_model() 方法加载模型。

I tensorflow/core/platform/profile_utils/cpu_utils.cc:104] CPU Frequency: 1996330000 Hz

I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x7fc360269ab0 initialized for platform Host (this does not guarantee that XLA will be used). Devices:

I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): Host, Default Version

尝试:

我尝试设置环境变量link

import os
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
import tensorflow as tf
from keras.models import load_model


model = tf.keras.models.load_model('path/to/location/model.model')

或者

import os
os.environ['CUDA_VISIBLE_DEVICES'] = ''
import tensorflow as tf
from keras.models import load_model


model = tf.keras.models.load_model('path/to/location/model.model')

注意:请检查模型是否在.model 扩展中


Q1。有没有办法检查我在.model 扩展中的模型?


编辑:

根据@kosa 回答model.summary() 给我以下输出。

Model: "model"
__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
image (InputLayer)              [(None, 45, 168, 1)] 0                                            
__________________________________________________________________________________________________
conv2d (Conv2D)                 (None, 45, 168, 16)  160         image[0][0]                      
__________________________________________________________________________________________________
max_pooling2d (MaxPooling2D)    (None, 23, 84, 16)   0           conv2d[0][0]                     
__________________________________________________________________________________________________
conv2d_1 (Conv2D)               (None, 23, 84, 32)   4640        max_pooling2d[0][0]              
__________________________________________________________________________________________________
max_pooling2d_1 (MaxPooling2D)  (None, 12, 42, 32)   0           conv2d_1[0][0]                   
__________________________________________________________________________________________________
conv2d_2 (Conv2D)               (None, 12, 42, 32)   9248        max_pooling2d_1[0][0]            
__________________________________________________________________________________________________
max_pooling2d_2 (MaxPooling2D)  (None, 6, 21, 32)    0           conv2d_2[0][0]                   
__________________________________________________________________________________________________
batch_normalization_v1 (BatchNo (None, 6, 21, 32)    128         max_pooling2d_2[0][0]            
__________________________________________________________________________________________________
flatten (Flatten)               (None, 4032)         0           batch_normalization_v1[0][0]     
__________________________________________________________________________________________________
dense (Dense)                   (None, 64)           258112      flatten[0][0]                    
__________________________________________________________________________________________________
dense_1 (Dense)                 (None, 64)           258112      flatten[0][0]                    
__________________________________________________________________________________________________
dense_2 (Dense)                 (None, 64)           258112      flatten[0][0]                    
__________________________________________________________________________________________________
dense_3 (Dense)                 (None, 64)           258112      flatten[0][0]                    
__________________________________________________________________________________________________
dense_4 (Dense)                 (None, 64)           258112      flatten[0][0]                    
__________________________________________________________________________________________________
dense_5 (Dense)                 (None, 64)           258112      flatten[0][0]                    
__________________________________________________________________________________________________
dropout (Dropout)               (None, 64)           0           dense[0][0]                      
__________________________________________________________________________________________________
dropout_1 (Dropout)             (None, 64)           0           dense_1[0][0]                    
__________________________________________________________________________________________________
dropout_2 (Dropout)             (None, 64)           0           dense_2[0][0]                    
__________________________________________________________________________________________________
dropout_3 (Dropout)             (None, 64)           0           dense_3[0][0]                    
__________________________________________________________________________________________________
dropout_4 (Dropout)             (None, 64)           0           dense_4[0][0]                    
__________________________________________________________________________________________________
dropout_5 (Dropout)             (None, 64)           0           dense_5[0][0]                    
__________________________________________________________________________________________________
char_1 (Dense)                  (None, 36)           2340        dropout[0][0]                    
__________________________________________________________________________________________________
char_2 (Dense)                  (None, 36)           2340        dropout_1[0][0]                  
__________________________________________________________________________________________________
char_3 (Dense)                  (None, 36)           2340        dropout_2[0][0]                  
__________________________________________________________________________________________________
char_4 (Dense)                  (None, 36)           2340        dropout_3[0][0]                  
__________________________________________________________________________________________________
char_5 (Dense)                  (None, 36)           2340        dropout_4[0][0]                  
__________________________________________________________________________________________________
char_6 (Dense)                  (None, 36)           2340        dropout_5[0][0]                  
==================================================================================================
Total params: 1,576,888
Trainable params: 1,576,824
Non-trainable params: 64
__________________________________________________________________________________________________
None

【问题讨论】:

是什么让你认为这没有成功? @gobrewers14,我遇到了上述错误。 既然,我是新手,很高兴知道拒绝投票的原因。这样我就可以避免这样的错误。 【参考方案1】:

可能没有任何错误。请尝试model.summary() 并检查其输出。

【讨论】:

看起来工作正常。为什么说它没有加载?

以上是关于无法在仅 TensorFlow CPU 版本中加载模型的主要内容,如果未能解决你的问题,请参考以下文章

Spyder 无法在 conda 环境中加载 tensorflow,尽管 python 可以

无法在 tensorflow 官方 resnet 模型中加载用于 eval 的图像

无法在 Keras 2.1.0(使用 Tensorflow 1.3.0)中保存的 Keras 2.4.3(使用 Tensorflow 2.3.0)中加载 Keras 模型

在不同的会话中加载 TensorFlow 模型

无法使用熊猫在 python3 中加载 csv 文件

在 Django 应用程序中加载 TensorFlow 模型的位置