ultralytics / hub

Ultralytics HUB tutorials and support
https://hub.ultralytics.com
GNU Affero General Public License v3.0
138 stars 14 forks source link

Shared Inference API Example Request Python & cURL code not working correctly #855

Closed curtinmjc closed 1 month ago

curtinmjc commented 1 month ago

Search before asking

HUB Component

Inference

Bug

3 issues listed below: Below information is based on v8n Classify model created 2024.09.27 using a custom dataset uploaded 2024.09.26

  1. Python example code (below with edit for path of my test file) -- code runs successfully; however, JSON results values (class, confidence, name) are always the same regardless of the file sent:

import json import requests

Run inference on an image

url = "https://predict.ultralytics.com" headers = {"x-api-key": "d3fc123d26aaf85f89ab6064363146ec52d5a0b619"} data = {"model": "https://hub.ultralytics.com/models/uBRAwxJRTTWrihq4Mxw3", "imgsz": 640, "conf": 0.25, "iou": 0.45} with open("/Users/curtinmjc/AI/model_analysis/created_jpg/IMG_3220copy.jpg", "rb") as f: response = requests.post(url, headers=headers, data=data, files={"file": f})

Check for successful response

response.raise_for_status()

Print inference results

print(json.dumps(response.json(), indent=2))


JSON response to Python example code -- "results" class, confidence, name always same for each different file sent:

{ "images": [ { "results": [ { "class": 8, "confidence": 0.45183, "name": "CM_Cones_Sundaes_Ice_Cream_Social_PP_03" } ], "shape": [ 1080, 810 ], "speed": { "inference": 34.58214, "postprocess": 0.04768, "preprocess": 17.8256 } } ], "metadata": { "functionTimeAlive": 21506.304, "functionTimeCall": 0.09, "imageCount": 1, "model": "https://hub.ultralytics.com/models/uBRAwxJRTTWrihq4Mxw3", "version": { "python": "3.11.10", "torch": "2.4.1+cpu", "torchvision": "0.19.1+cpu", "ultralytics": "8.2.103" } } }


  1. cURL example code (below) -- code never runs successfully

curl -X POST "https://predict.ultralytics.com" \ -H "Content-Type: multipart/form-data" \ -H "x-api-key: d3fc123d26aaf85f89ab6064363146ec52d5a0b619" \ -F "model=https://hub.ultralytics.com/models/uBRAwxJRTTWrihq4Mxw3" \ -F "imgsz=640" \ -F "conf=0.25" \ -F "iou=0.45" \ -F "file=/Users/curtinmjc/AI/model_analysis/created_jpg/IMG_3220copy.jpg"


cURL response

{ "error": "Internal Server Error", "help": "For detailed usage instructions, visit https://docs.ultralytics.com/hub/inference-api. For bug reports or questions, visit https://github.com/ultralytics/hub/issues.", "message": "An unexpected error occurred while processing your request: '\u001b[31m\u001b[1mfile\u001b[0m' is not a valid YOLO argument. Similar arguments are i.e. ['profile=False'].\n\n Arguments received: ['yolo', '--bind', ':8080', '--timeout', '0', 'main:app']. Ultralytics 'yolo' commands use the following syntax:\n\n yolo TASK MODE ARGS\n\n Where TASK (optional) is one of {'detect', 'obb', 'pose', 'classify', 'segment'}\n MODE (required) is one of {'predict', 'track', 'val', 'train', 'benchmark', 'export'}\n ARGS (optional) are any number of custom 'arg=value' pairs like 'imgsz=320' that override defaults.\n See all ARGS at https://docs.ultralytics.com/usage/cfg or with 'yolo cfg'\n\n 1. Train a detection model for 10 epochs with an initial learning_rate of 0.01\n yolo train data=coco8.yaml model=yolov8n.pt epochs=10 lr0=0.01\n\n 2. Predict a YouTube video using a pretrained segmentation model at image size 320:\n yolo predict model=yolov8n-seg.pt source='https://youtu.be/LNwODJXcvt4' imgsz=320\n\n 3. Val a pretrained detection model at batch-size 1 and image size 640:\n yolo val model=yolov8n.pt data=coco8.yaml batch=1 imgsz=640\n\n 4. Export a YOLOv8n classification model to ONNX format at image size 224 by 128 (no TASK required)\n yolo export model=yolov8n-cls.pt format=onnx imgsz=224,128\n\n 5. Explore your datasets using semantic search and SQL with a simple GUI powered by Ultralytics Explorer API\n yolo explorer data=data.yaml model=yolov8n.pt\n \n 6. Streamlit real-time webcam inference GUI\n yolo streamlit-predict\n \n 7. Run special commands:\n yolo help\n yolo checks\n yolo version\n yolo settings\n yolo copy-cfg\n yolo cfg\n\n Docs: https://docs.ultralytics.com\n Community: https://community.ultralytics.com\n GitHub: https://github.com/ultralytics/ultralytics\n " }


  1. Example cURL in "https://docs.ultralytics.com/hub/inference-api" (below) is different than the cURL example code in HUB model's Deploy tab (what I list in #2 above) -- it's missing: -H "Content-Type: multipart/form-data" \

curl -X POST "https://predict.ultralytics.com" \ -H "x-api-key: API_KEY" \ -F "model=https://hub.ultralytics.com/models/MODEL_ID" \ -F "file=@/path/to/image.jpg" \ -F "imgsz=640" \ -F "conf=0.25" \ -F "iou=0.45"

Environment

Minimal Reproducible Example

Ran Python code in Visual Studio Code Ran cURL in iTerm2 terminal

Additional

No response

UltralyticsAssistant commented 1 month ago

👋 Hello @curtinmjc, thank you for reaching out about the issues with the Inference API example code on the Ultralytics HUB 🚀!

To assist in resolving your problem, could you please provide a Minimum Reproducible Example of both your Python and cURL-based issues? This will greatly help us diagnose the problems:

  1. Python Example: Ensure the data and headers are correctly formatted and that the file paths and URLs are accessible. Any additional logs or screenshots would be helpful in understanding why the JSON outputs remain constant.

  2. cURL Example: There may be differences causing the "Internal Server Error." Please verify that all fields and headers match the API's expected format. Double-check that the file path is properly formatted in your cURL command (e.g., -F "file=@/path/to/image.jpg").

Please check out these helpful resources:

An Ultralytics engineer will review your issue and assist further soon. Thanks for your patience! 😊

curtinmjc commented 1 month ago

Thank you for the quick response.

My answers to your comments:

  1. cURL Example: You are correct in pointing me to the file parameter... I had not included the '@' between 'file=' and '/path/to/image.jpg'. After changing this both cURL and Postman worked without error. However, their results were the same as with Python code... JSON outputs remained constant.

  2. Python Example (constant JSON outputs): I verified that file paths and URLs were accessible. And after cURL and Postman delivered the same constant results I still think there is an issue. But I did further testing and discovered that when I use the old Python url & data code format I got expected (non constant) results. The different formats are listed below. Using the older format with cURL and Postman gave me results the same as with the Python old formatted code.

New format: url = "https://predict.ultralytics.com" data = {"model": " https://hub.ultralytics.com/models/uBRAwxJRTTWrihq4Mxw3", "imgsz": 640, "conf": 0.25, "iou": 0.45}

Old format: url = "https://api.ultralytics.com/v1/predict/uBRAwxJRTTWrihq4Mxw3" data = {"imgsz": 640, "conf": 0.25, "iou": 0.45}

I am attaching a screenshot of my terminal showing four cURL cmds: (a) the first two are with the new format and show the same, incorrect results: {"class":8,"confidence":0.45183,"name":"CM_Cones_Sundaes_Ice_Cream_Social_PP_03"} (b) the second two are with the old format showing different, correct results: {"class":5,"confidence":0.52154,"name":"CM_Camp_Left_Life_At_The_Lake_PP_04"} and {"class":0,"confidence":0.99355,"name":"CM_Animals_Life_At_The_Lake_PP_01"}

In both cases I used the same two image files (IMG_3220copy.jpg and IMG_3217 copy.jpg).

On Sun, Sep 29, 2024 at 3:37 PM Ultralytics Assistant < @.***> wrote:

👋 Hello @curtinmjc https://github.com/curtinmjc, thank you for reaching out about the issues with the Inference API example code on the Ultralytics HUB https://github.com/ultralytics/hub 🚀!

To assist in resolving your problem, could you please provide a Minimum Reproducible Example https://docs.ultralytics.com/help/minimum_reproducible_example/ of both your Python and cURL-based issues? This will greatly help us diagnose the problems:

1.

Python Example: Ensure the data and headers are correctly formatted and that the file paths and URLs are accessible. Any additional logs or screenshots would be helpful in understanding why the JSON outputs remain constant. 2.

cURL Example: There may be differences causing the "Internal Server Error." Please verify that all fields and headers match the API's expected format. Double-check that the file path is properly formatted in your cURL command (e.g., -F "file=@/path/to/image.jpg").

Please check out these helpful resources:

An Ultralytics engineer will review your issue and assist further soon. Thanks for your patience! 😊

— Reply to this email directly, view it on GitHub https://github.com/ultralytics/hub/issues/855#issuecomment-2381576860, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABMSSD5DAUFIWXTBBABIOD3ZZBJGZAVCNFSM6AAAAABPB7PUF2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGOBRGU3TMOBWGA . You are receiving this because you were mentioned.Message ID: @.***>

sergiuwaxmann commented 1 month ago

Can you try again? The example should work fine now.

curtinmjc commented 1 month ago

Yes, it's working fine now. Thanks much for the quick turnaround.

On Mon, Sep 30, 2024 at 5:00 PM Sergiu Waxmann @.***> wrote:

Can you try again? The example should work fine now.

— Reply to this email directly, view it on GitHub https://github.com/ultralytics/hub/issues/855#issuecomment-2384135759, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABMSSD3AKERNGXZQDOH3HGLZZG3W3AVCNFSM6AAAAABPB7PUF2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGOBUGEZTKNZVHE . You are receiving this because you were mentioned.Message ID: @.***>

sergiuwaxmann commented 1 month ago

Awesome! Thank you for reporting this issue!