【対応例】ValueError: No model found in config file

【対応例】ValueError: No model found in config file

 

【問題と解決方法例 – Issue & Example Solution】ValueError: No model found in config file

 

 

FlaskのWebアプリを動かすためにKerasで作成したモデルを読み込んだ際に遭遇したエラーです。
This is the error I encountered when I loaded a model created with “Keras” to run a Flask Web app.

 

最終確認日 – Date of last confirmation

2021.2.2

 

開発環境 – Development Environment

 

 

・Visual Studio Code1 1.52.1
・macOS Big Sur 11.2
・Command Line Tools:Xcode 12.4(12D4e)
・ターミナル – Terminal:zsh
・Homebrewでインストールしたpyenvを利用 – Use pyenv installed by Homebrew
・pyenv 1.2.21
・Python 3.6.9
・Flask 1.1.2
・tensorflow 2.4.1
・Keras 2.4.3

 

 

問題 – Issue

 

 

:Flask 機械学習Webアプリは起動するが、予測結果が表示されない。
 The Flask machine learning web app starts, but the prediction results are not displayed.

 

 

出力結果 – Results

 

 

ValueError: No model found in config file.

 

【問題と解決方法例 - Issue & Example Solution】ValueError:No model found in config file エラー画面

 

 

【解決方法例 – Example Solution】

 

 

対応方法 – Method:Example Solution

:読み込ませる学習モデルを

・学習済みの「重み」

ではなく、

・学習モデルの「モデル構造」と学習済みの「重み」

に変更する。

そのため、機械学習プログラムのモデルの保存方法を

model.save_weights('weight.h5') 

ではなく、

model.save('model_and_weight.h5')

にして、

from keras.models import load_model

model = load_model('model_and_weight.h5') 

で読み込ませる。

 

【問題と解決方法例 - Issue & Example Solution】ValueError:No model found in config file 予測成功

 




 

Change the learning model to be loaded to the “model structure” of the learning model and the learned “weight” instead of the learned “weight”.

So, instead of using

model.save_weights('weight.h5') 

to save the model in your machine learning program, you can use

model.save('model_and_weight.h5')

and use

from keras.models import load_model

model = load_model('model_and_weight.h5') 

to load it.

 

 

Kerasのモデルの保存形式の違いについて – How Keras models are stored in different formats

 

 

Kerasのモデルの保存形式の違いについては、以下のページにまとめておきましたので、必要に応じて参照ください。
I have summarized the different formats for storing Keras models in the following pages, so please refer to them if necessary.

【Kerasの使い方解説】機械学習のモデル構造・学習モデルの重みの保存の違い:json・pickle・pkl・hdf5・h5ファイルとは? -【Explaining how to use Keras】Differences in storing machine learning model structure and learning model weights: What are json, pickle, pkl, hdf5, and h5 files?

慣れるまでは、混乱しやすいですね。
It’s easy to get confused until you get used to it.
 

 

by 子供プログラマー | プログラミング入門ウェブ教室

 

【ERROR Codes】- プログラミング学習エラーコード集(一覧)

 

日本人のための人工知能プログラマー入門講座(機械学習)