【问题标题】:utf-8 decode error when trying to feed an image into a model尝试将图像输入模型时出现 utf-8 解码错误
【发布时间】:2021-11-13 18:14:20
【问题描述】:

我已成功创建了一个网页,该网页获取图像文件并将其传递给我构建的 API。唯一的问题是,一旦我将该图像从 tensorflow 提供给 preprocessing.image.load_img,我就会收到此错误:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

这里是 API:

from starlette.responses import RedirectResponse
from fastapi import FastAPI, File, UploadFile
from tensorflow.keras import preprocessing
from fastapi.staticfiles import StaticFiles
from keras.models import load_model
import numpy as np
import uvicorn

app = FastAPI()
app.mount("/Templates", StaticFiles(directory="Templates"), name="Templates")

model_dir = 'F:\\Saved-Models\\Dog-Cat-Models\\First_Generation_dog_cat_optuna.h5'
model = load_model(model_dir)


@app.get('/')
async def index():
    return RedirectResponse(url="/Templates/index.html")


@app.post('/prediction_page')
async def prediction_form(dogcat_img: UploadFile = File(...)):
    dogcat_img_bytes = dogcat_img.file.read()

    pp_dogcat_image = preprocessing.image.load_img(dogcat_img_bytes, target_size=(150, 150))
    pp_dogcat_image_arr = preprocessing.image.img_to_array(pp_dogcat_image)
    input_arr = np.array([pp_dogcat_image_arr])
    prediction = np.argmax(model.predict(input_arr), axis=-1)

    print(prediction)


if __name__ == '__main__':
    uvicorn.run(app, host='localhost', port=8000)

【问题讨论】:

    标签: keras byte tensor fastapi starlette


    【解决方案1】:

    没有完整的traceback 例外,可能很难提供帮助,但查看文档,tf.keras.utils.load_img 需要图像文件的路径(不是原始图像数据)。

    您可以尝试这样的事情(底层library 做了类似的事情):

    --- orig.py 2021-09-20 10:47:22.465636386 +0100
    +++ new.py  2021-09-20 10:48:50.760734720 +0100
    @@ -6,6 +6,8 @@
     import numpy as np
     import uvicorn
     
    +from PIL import Image
    +
     app = FastAPI()
     app.mount("/Templates", StaticFiles(directory="Templates"), name="Templates")
     
    @@ -20,9 +22,10 @@
     
     @app.post('/prediction_page')
     async def prediction_form(dogcat_img: UploadFile = File(...)):
    -    dogcat_img_bytes = dogcat_img.file.read()
    +    # dogcat_img_bytes = dogcat_img.file.read()
     
    -    pp_dogcat_image = preprocessing.image.load_img(dogcat_img_bytes, target_size=(150, 150))
    +    # pp_dogcat_image = preprocessing.image.load_img(dogcat_img_bytes, target_size=(150, 150))
    +    pp_dogcat_image = Image.open(dogcat_img.file).resize((150, 150), Image.NEAREST).convert("RGB") 
         pp_dogcat_image_arr = preprocessing.image.img_to_array(pp_dogcat_image)
         input_arr = np.array([pp_dogcat_image_arr])
         prediction = np.argmax(model.predict(input_arr), axis=-1)
    

    顺便说一句,您可能还想考虑使用BackgroundTasks 进行图像处理,否则单个长时间运行的 (async) 请求将阻塞其他请求。

    【讨论】:

    • 哇,好用,谢谢!习惯了使用tensorflow处理方式,心胸狭隘不敢尝试PIL版本。我也会调查后台任务。
    猜你喜欢
    • 2017-11-08
    • 2015-09-03
    • 2016-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-08
    • 2019-08-21
    • 1970-01-01
    相关资源
    最近更新 更多