我为保存的 AI 模型创建了一个端点 我的模特检查了眼睛的照片来告诉我眼睛是否患有任何疾病 例如 [“白内障”、“糖尿病”、“青光眼”、“正常”] 我的脚本是
import requests
from PIL import Image
import numpy as np
import tensorflow.lite as tflite
model_path = 'efficientnetb3-EyeDisease-96.22.tflite'
interpreter = tflite.Interpreter(model_path)
interpreter.allocate_tensors()
image_path='1145_right.jpeg'
def preprocess_image(image_path):
# Load and preprocess the image
image = Image.open(image_path).convert('RGB')
image = image.resize((224, 224))
image_array = np.array(image) / 255.0
image_array = np.expand_dims(image_array, axis=0)
return image_array.astype(np.float32)
def perform_inference(image_array):
# Perform inference
input_tensor = interpreter.tensor(interpreter.get_input_details()[0]['index'])
input_tensor()[0] = image_array
interpreter.invoke()
output_tensor = interpreter.tensor(interpreter.get_output_details()[0]['index'])
predictions = output_tensor()
return predictions.tolist()
if __name__ == '__main__':
data = ['cataract', 'diabetic', 'glaucoma', 'normal']
image_array = preprocess_image(image_path)
predictions = perform_inference(image_array)
result = predictions
index = np.argmax(result)
print('Inference Result:', data[index])
每次测试模型的结果都返回白内障
ai模型> python x.py 2024-01-18 14:35:50.276823:我tensorflow/core/util/port.cc:113] oneDNN 自定义操作已开启。由于不同计算顺序的浮点舍入误差,您可能会看到略有不同的数值结果。要关闭它们,请设置环境变量
。 2024-01-18 14:35:52.465150:我tensorflow/core/util/port.cc:113] oneDNN 自定义操作已开启。由于不同计算顺序的浮点舍入误差,您可能会看到略有不同的数值结果。要关闭它们,请设置环境变量TF_ENABLE_ONEDNN_OPTS=0
。 信息:为 CPU 创建了 TensorFlow Lite XNNPACK 委托。 警告:尝试使用仅支持静态大小张量的委托和具有动态大小张量的图(张量#299 是动态大小张量)。推理结果:cataractTF_ENABLE_ONEDNN_OPTS=0
模型工作正常,但结果错误,我需要帮助才能正确回答我的照片
固定代码是
import torch
import requests
from torchvision import transforms
from PIL import Image
import io
model_path = "public\Aimodel\model_scripted.pt"
model = torch.jit.load(model_path)
image_url = 'https://pbs.twimg.com/media/GEEQErLWAAA4GYJ?format=jpg&name=small'
model.eval()
# Download the image from the URL
response = requests.get(image_url)
image_data = response.content
# Open the image using PIL
image = Image.open(io.BytesIO(image_data))
t = transforms.Compose(
[
transforms.Resize((256, 256)),
transforms.ToTensor(),
]
)
image = t(image).unsqueeze(0)
pred_idx = model(image).argmax().item()
classes = ["Cataract", "Diabetic", "Glaucoma", "Normal"]
prediction = classes[pred_idx]
print(f"Prediction: {prediction}")