私はkerasの事前訓練モデルを使用しており、予測を取得しようとするとエラーが発生しました。私は、フラスコサーバに次のコードを持っている:ValueError:Tensor:(...)はこのグラフの要素ではありません
from NeuralNetwork import *
@app.route("/uploadMultipleImages", methods=["POST"])
def uploadMultipleImages():
uploaded_files = request.files.getlist("file[]")
getPredictionfunction = preTrainedModel["VGG16"]
for file in uploaded_files:
path = os.path.join(STATIC_PATH, file.filename)
result = getPredictionfunction(path)
これは私が私のNeuralNetwork.pyファイルにしたものである:
vgg16 = VGG16(weights='imagenet', include_top=True)
def getVGG16Prediction(img_path):
model = vgg16
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
pred = model.predict(x) #ERROR HERE
return sort(decode_predictions(pred, top=3)[0])
preTrainedModel["VGG16"] = getVGG16Prediction
ただし、以下のコードを実行し、すべてのエラーを作成しません。
ここでif __name__ == "__main__":
STATIC_PATH = os.getcwd()+"/static"
print(preTrainedModel["VGG16"](STATIC_PATH+"/18.jpg"))
コメントやご提案は大変ありがとうございます。ありがとうございました。
私の場合、フラスコはテンソルフローを2回ロードしていたため問題がありました。 app.runを変更したとき(debug = False)、それは動作しますが、理由はわかりません。その質問へのリンクは次のとおりです:http://stackoverflow.com/questions/42015797/is-tensorflow-loading-twice-in-pycharmあなたのケースでは同じかどうかは分かりませんが、それが助けてくれることを願っています。提案ありがとう :) – matchifang