将图片上传到经过训练的模型时出错

时间:2019-11-02 20:21:32

标签: python tensorflow machine-learning flask keras

这是上传测试图像以使用预训练模型进行预测时遇到的错误。请帮忙!

tensorflow.python.framework.errors_impl.FailedPreconditionError: Error while reading resource variable conv2d_2/bias from Container: localhost. This could mean that the variable was uninitialized. Not found: Container localhost does not exist. (Could not find resource: localhost/conv2d_2/bias)
     [[{{node conv2d_2/BiasAdd/ReadVariableOp}}]]

这就是我加载保存的模型并调用模型以对上传的图片进行预测的方式

    graph = tf.get_default_graph()
    with graph.as_default():
        # load model at very first
        model = load_model(STATIC_FOLDER + '/' + '*********.h5')


    # call model to predict an image
    def api(full_path):
        data = image.load_img(full_path, target_size=(150, 150, 3))
        data = np.expand_dims(data, axis=0)
        data = data * 1.0 / 255

        with graph.as_default():
            predicted = model.predict(data)
            return predicted

这就是我处理上传文件并在Python中对其进行预测的方式。

    @app.route('/upload', methods=['POST','GET'])
    def upload_file():

        if request.method == 'GET':
            return render_template('index.html')
       else:
            file = request.files['image']
            full_name = os.path.join(UPLOAD_FOLDER, file.filename)
            file.save(full_name)

            indices = {0: 'Counterfeit', 1: 'Authentic'}
            result = api(full_name)

            predicted_class = np.asscalar(np.argmax(result, axis=1))
            accuracy = round(result[0][predicted_class] * 100, 2)
            label = indices[predicted_class]

        return render_template('predict.html', image_file_name = file.filename, label = label, accuracy = accuracy)

    @app.route('/uploads/<filename>')
    def send_file(filename):
        return send_from_directory(UPLOAD_FOLDER, filename)

系统配置:

Python               3.7.4
tensorflow           2.0.0    
tensorflow-estimator 2.0.1 
Keras                2.3.1    
Keras-Applications   1.0.8    
Keras-Preprocessing  1.1.0 

0 个答案:

没有答案