【问题标题】:Using the .h5 file is not giving the expected output?使用 .h5 文件没有给出预期的输出?
【发布时间】:2020-07-19 20:07:59
【问题描述】:

我目前正在使用 Flask 来结合后端和前端表单图像分类。我还使用 .h5 文件来预测输出。输出不同,完全错误。输出应该是预测概率。代码如下:

def upload():
    if request.method == 'POST':
        # Get the file from post request
        f = request.files['file']

        # Save the file to ./uploads
        basepath = os.path.dirname(__file__)
        file_path = os.path.join(
            basepath, 'uploads', secure_filename(f.filename))
        f.save(file_path)

        MODEL_ARCHITECTURE = 'model_adam_01.json'
        MODEL_WEIGHTS = 'model_50_eopchs_adam_01.h5'

        json_file = open(MODEL_ARCHITECTURE)
        loaded_model_json = json_file.read()
        json_file.close()
        model = model_from_json(loaded_model_json)
        model.load_weights(MODEL_WEIGHTS)
        model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
        prediction = model_predict(file_path, model)
        print("I think that is ")
        print(prediction)
        # print('I think that is {}.'.format(predicted_class.lower()))
        return str(prediction)

下面是model_predict函数,我在其中传递了图像路径和模型

def model_predict(img_path, model):
    '''
        Args:
            -- img_path : an URL path where a given image is stored.
            -- model : a given Keras CNN model.
    '''

    IMG = image.load_img(img_path)
    print(type(IMG))

    IMG_ = np.asarray(IMG)
    print(type(IMG_))

    print(IMG_.shape)

    IMG_ = prepare(IMG_)
    print(IMG_.shape)
    #print(model)

    prediction = model.predict(IMG_)
    print(prediction.shape)

    return str(prediction)

以下是我得到的输出:

I think that is
[[0.]]

为什么会出现这个问题?我正在使用 keras 2.3.1 和 tesorflow 1.15.2

【问题讨论】:

    标签: python tensorflow machine-learning image-processing keras


    【解决方案1】:

    在进行预测时,您必须应用与训练模型之前对训练数据所做的相同的预处理步骤。我认为这应该是问题而不是代码。

    【讨论】:

    • 感谢您的回答。我使用了与训练模型相同的预处理步骤,但仍然提供相同的输出。@Galo Castillo
    猜你喜欢
    • 2018-09-18
    • 2019-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-22
    • 2019-07-16
    相关资源
    最近更新 更多