-
Notifications
You must be signed in to change notification settings - Fork 7
Fix llm looping on complex graphics #1180
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -68,33 +68,30 @@ def normalize_bbox(bbox, width, height): | |
| ] | ||
|
|
||
|
|
||
| def process_objects(objects, threshold): | ||
| def process_objects(qwen_output, width, height, threshold): | ||
| """ | ||
| Process detected objects by filtering, transforming, and enriching them. | ||
| Transform Qwen object detection output to IMAGE schema format. | ||
|
|
||
| - Filters objects by confidence threshold | ||
| - Transforms from Qwen format (bbox_2d, label) to IMAGE format | ||
| - Normalizes bounding boxes to [0,1] range | ||
| - Assigns confidence threshold to all objects | ||
| - Normalizes labels (replaces underscores with spaces) | ||
| - Renumbers IDs sequentially | ||
| - Calculates geometric properties (area, centroid) | ||
| - Filters objects by confidence threshold | ||
|
|
||
| Args: | ||
| objects (list): List of detected objects with confidence scores | ||
| qwen_output (list): Qwen detection output with bbox_2d and label | ||
| width (int): Image width for normalization | ||
| height (int): Image height for normalization | ||
| threshold (float): Minimum confidence score (0-1) | ||
|
|
||
| Returns: | ||
| list: Processed objects with computed properties | ||
| """ | ||
| processed = [] | ||
| for obj in objects: | ||
| if obj.get("confidence", 0) >= threshold: | ||
| obj['type'] = obj['type'].replace('_', ' ') | ||
| processed.append(obj) | ||
|
|
||
| # Renumber IDs sequentially after filtering | ||
| for idx, obj in enumerate(processed): | ||
| obj['ID'] = idx | ||
|
|
||
| x1, y1, x2, y2 = obj["dimensions"] | ||
| for idx, item in enumerate(qwen_output): | ||
| # Normalize bounding box | ||
| x1, y1, x2, y2 = normalize_bbox(item["bbox_2d"], width, height) | ||
|
|
||
| # Calculate area (width * height) | ||
| area = (x2 - x1) * (y2 - y1) | ||
|
|
@@ -103,13 +100,20 @@ def process_objects(objects, threshold): | |
| centroid_x = (x1 + x2) / 2 | ||
| centroid_y = (y1 + y2) / 2 | ||
|
|
||
| # Create object entry according to schema | ||
| obj["area"] = area | ||
| obj["centroid"] = [centroid_x, centroid_y] | ||
| # Create object entry according to IMAGE schema | ||
| obj = { | ||
| "ID": idx, | ||
| "type": item["label"].replace('_', ' '), | ||
| "dimensions": [x1, y1, x2, y2], | ||
| "confidence": threshold, | ||
| "area": area, | ||
| "centroid": [centroid_x, centroid_y] | ||
| } | ||
|
|
||
| processed.append(obj) | ||
|
|
||
| logging.debug( | ||
| f"Processed {len(objects)} objects to {len(processed)} " | ||
| f"objects with confidence >= {threshold}" | ||
| f"Processed {len(qwen_output)} objects from Qwen output" | ||
| ) | ||
| return processed | ||
|
|
||
|
|
@@ -155,35 +159,42 @@ def detect_objects(): | |
| if error: | ||
| return jsonify(error), error["code"] | ||
|
|
||
| stop_tokens = [ | ||
| "<|im_end|>", # Qwen's end token | ||
| "<|endoftext|>", # Alternative end token | ||
| "\n\n\n", # Triple newline | ||
| "```", # Code block end | ||
| ] | ||
|
|
||
| try: | ||
| # Get object info | ||
| object_json = llm_client.chat_completion( | ||
| qwen_output = llm_client.chat_completion( | ||
| prompt=OBJECT_DETECTION_PROMPT, | ||
| image_base64=base64_image, | ||
| json_schema=BBOX_RESPONSE_SCHEMA, | ||
| temperature=0.0, | ||
| parse_json=True | ||
| temperature=0.5, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. curious why the move back to non-zero temperature?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is one of the theories on why this is happening: same problem reported on GitHub. I am not 100% sure it's affecting the performance in our specific case (too many variables: model, quantization, engine...), but I'm willing to try since it doesn't seem to affect the accuracy of outputs. |
||
| parse_json=True, | ||
| stop=stop_tokens | ||
| ) | ||
|
|
||
| if object_json is None or len(object_json.get("objects", [])) == 0: | ||
| logging.debug(f"Qwen output received: {qwen_output}") | ||
|
|
||
| if qwen_output is None or len(qwen_output) == 0: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No content (204) isn't really an error, if there is legit nothing to extract. Should ideally distinguish between an actual error (something went wrong and nothing to report) vs. everything worked, but there were no objects to extract. |
||
| logging.error("Failed to extract objects from the graphic.") | ||
| return jsonify({"error": "No objects extracted"}), 204 | ||
|
|
||
| # Normalize bounding boxes | ||
| # Transform Qwen format to IMAGE schema format | ||
| width, height = pil_image.size | ||
| for obj in object_json["objects"]: | ||
| # Normalize bounding boxes | ||
| obj["dimensions"] = normalize_bbox( | ||
| obj["dimensions"], width, height | ||
| ) | ||
|
|
||
| # Filter objects by confidence threshold, add area and centroid, | ||
| # remove underscores from labels, and renumber IDs | ||
| object_json["objects"] = process_objects( | ||
| object_json["objects"], | ||
| processed_objects = process_objects( | ||
| qwen_output, | ||
| width, | ||
| height, | ||
| CONF_THRESHOLD | ||
| ) | ||
|
|
||
| # Wrap in "objects" for schema compliance | ||
| object_json = {"objects": processed_objects} | ||
|
|
||
| logging.pii(f"Normalized output: {object_json}") | ||
|
|
||
| # Data schema validation | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,45 +1,23 @@ | ||
| { | ||
| "$schema": "http://json-schema.org/draft-07/schema", | ||
| "type": "object", | ||
| "type": "array", | ||
| "title": "Object Detection Data", | ||
| "description": "Detected object data with bounding boxes.", | ||
| "definitions": { | ||
| "object": { | ||
| "type": "object", | ||
| "title": "BoundingBoxItem", | ||
| "properties": { | ||
| "ID": { | ||
| "description": "A number identifying this object in the set.", | ||
| "type": "integer" | ||
| }, | ||
| "type": { | ||
| "description": "The type of object detected (e.g., 'person', 'car').", | ||
| "type": "string" | ||
| }, | ||
| "dimensions": { | ||
| "description": "Bounding box coordinates of this object [x1, y1, x2, y2].", | ||
| "type": "array", | ||
| "items": { "type": "number" }, | ||
| "minItems": 4, | ||
| "maxItems": 4, | ||
| "additionalItems": false | ||
| }, | ||
| "confidence": { | ||
| "description": "Confidence in the correctness of this object's data (0-1).", | ||
| "type": "number", | ||
| "minimum": 0, | ||
| "maximum": 1 | ||
| } | ||
| "description": "Detected object data with bounding boxes in Qwen format.", | ||
| "items": { | ||
| "type": "object", | ||
| "properties": { | ||
| "bbox_2d": { | ||
| "description": "Bounding box coordinates [x1, y1, x2, y2].", | ||
| "type": "array", | ||
| "items": { "type": "number" }, | ||
| "minItems": 4, | ||
| "maxItems": 4 | ||
| }, | ||
| "required": ["ID", "type", "dimensions", "confidence"] | ||
| } | ||
| }, | ||
| "properties": { | ||
| "objects": { | ||
| "description": "The set of detected objects in the image.", | ||
| "type": "array", | ||
| "items": { "$ref": "#/definitions/object" } | ||
| } | ||
| }, | ||
| "required": ["objects"] | ||
| "label": { | ||
| "description": "The type of object detected (e.g., 'person', 'car').", | ||
| "type": "string" | ||
| } | ||
| }, | ||
| "required": ["bbox_2d", "label"] | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in pixels?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In pixels, yes. Thank you!