How Text-to-Image Models Handle Spatial Relations, Negation, and On-Image Text

    Complex text-to-image prompts often combine multiple objects, attributes, spatial relations, and negated conditions, and many failures come from how those constraints are injected into the generation process. This article explains a planning-plus-rendering workflow, a structured intermediate representation, coordinate-based checks for spatial relations, practical handling of negation, on-image text rendering and verification, and a reproducible evaluation framework. It also covers engineering deployment practices and current limitations.

    1. Why Complex Instructions Are Often Rendered Incorrectly

    Text-to-image models usually receive instructions that contain multiple objects, attributes, and relations at the same time. Consider this prompt: “A girl in a red raincoat sits on the left side of a blue bench, and on the right side there is a dog that is not eating an apple.” This sentence contains at least four types of constraints:

    • Objects: girl, bench, dog
    • Attribute binding: the raincoat is red, and the bench is blue
    • Spatial relations: the girl is on the left of the bench, and the dog is on the right
    • Negation: the dog has not bitten an apple

    There are three common failure modes. The first is attribute leakage, where red ends up on the dog or the bench. The second is relation misplacement, where the left-right order is reversed. The third is negation failure, where the negated object still appears in the image.

    These errors originate from the way conditions are injected. Diffusion models typically inject text features into the denoising process through cross-attention. The correspondence between text tokens and image regions is learned implicitly and lacks explicit structural constraints. When the word “apple” appears in the text, the model is likely to generate an apple in the image because it has difficulty applying the word “not” precisely to a specific region.

    2. Plan First, Render Second: A Hybrid Generation Approach

    One practical engineering path is to split generation into two stages:

    1. Planning stage: a language model interprets the instruction and outputs a structured scene description, including an entity list, position boxes, attributes, and negation constraints.
    2. Rendering stage: an image generator uses this description as a condition for drawing.

    The following is an illustrative intermediate representation:

    1. {
    2. "canvas": {"width": 1024, "height": 1024, "background": "street at dusk"},
    3. "entities": [
    4. {"id": "girl", "label": "girl", "attributes": ["red raincoat"], "bbox": [0.08, 0.30, 0.45, 0.95]},
    5. {"id": "bench", "label": "bench", "attributes": ["blue"], "bbox": [0.10, 0.55, 0.50, 0.80]},
    6. {"id": "dog", "label": "dog", "attributes": ["not biting an apple"], "bbox": [0.58, 0.45, 0.92, 0.95]}
    7. ],
    8. "relations": [
    9. {"subject": "girl", "relation": "left_of", "object": "dog"}
    10. ],
    11. "negative_constraints": ["No bitten apple appears in the image"]
    12. }

    The advantage of this approach is that constraints become checkable. Before the planning result enters rendering, logical validation can be performed, for example to check whether the left-right relation is consistent with the position boxes. The cost is an additional inference step, which increases overall latency. Errors in the planning stage also propagate directly to the rendering stage, so validation and retry mechanisms must be designed for planning results.

    3. Handling Spatial Relations and Negation Constraints

    3.1 Convert Relative Positions into Checkable Coordinates

    Words such as “left,” “right,” “above,” and “contains” can be mapped to normalized coordinates. The following function uses the horizontal center point of boxes to determine left-right relations:

    1. def check_left_of(a_box, b_box):
    2. """Check whether entity a is to the left of entity b by comparing horizontal box centers."""
    3. a_center_x = (a_box[0] + a_box[2]) / 2
    4. b_center_x = (b_box[0] + b_box[2]) / 2
    5. return a_center_x < b_center_x

    The center-point method is a simple heuristic. When two boxes overlap heavily, its judgment may be unstable. In real projects, overlap area or minimum distance can be added as auxiliary conditions.

    3.2 Expressing Negation Constraints

    Negation is the hardest part to handle. “Not eating an apple” often corresponds in training corpora to samples where “dog and apple co-occur,” and the model has difficulty learning the meaning of negation from such data. A more reliable approach has three parts:

    • Rewrite negation as a positive state, for example “the apple in front of the dog is intact.”
    • Remove the negated object from the entity list during planning and explicitly exclude it in the negative prompt.
    • After rendering, use a detection model to confirm whether the negated object exists; only pass if it does not.

    3.3 Quantity and Attribute Binding

    Quantity words such as “three cats” and attribute binding such as “the red raincoat belongs only to the girl” also require structured representation. Attaching attributes to specific entities instead of leaving them in a free-text paragraph can significantly reduce the probability of attribute leakage.

    4. On-Image Text Rendering: Treat Text as Symbols

    Traditional image models learn the visual shapes of strokes, and text appears to them as something close to texture. The result is strings that look like text but cannot be read.

    Common improvement directions include:

    • Add large numbers of images containing text to training data, paired with accurate text annotations.
    • Use a character-sensitive text encoder so the model receives character-level information in the conditional input.
    • During planning, output text that needs to be rendered as a separate field and specify its position and font size, for example "text": "Happy Mid-Autumn Festival".
    • After rendering, compare the text recognition result with the target string.
    1. def verify_text(rendered_image, expected_text, ocr_fn):
    2. recognized = ocr_fn(rendered_image)
    3. return recognized.strip() == expected_text

    In practice, short text, large font sizes, and scenes with fewer characters usually achieve higher accuracy. Long paragraphs, small font sizes, and decorative fonts remain difficult. Before launch, validate each piece of copy actually used by the business.

    5. Build a Reproducible Evaluation System

    Selecting images by eye cannot reliably compare different approaches. It is advisable to construct a test set covering the following five capabilities and report pass rates by category:

    Capability Example Instruction Judgment Method
    Spatial relations left, right, above, below, contains Compare detected box positions
    Quantity three cats Object detection count
    Attribute binding red raincoat belongs only to the girl Color region analysis plus manual review
    Negation no apple appears in the image Check whether the detected target exists
    Text title text is exactly correct Text recognition and string comparison

    The following is illustrative code for the evaluation flow:

    1. def evaluate(cases, generate_fn, judge_fns):
    2. results = {}
    3. for case in cases:
    4. image = generate_fn(case["prompt"])
    5. for name, judge in judge_fns.items():
    6. results.setdefault(name, []).append(judge(image, case))
    7. return {name: sum(v) / len(v) for name, v in results.items()}

    Reports should also include the number of samples in each category. When the sample size is small, a single overall pass rate can easily hide obvious defects in a particular capability.

    6. Engineering Deployment Considerations

    • Timeouts and fallback: when the planning stage fails, fall back to a simplified prompt for rendering and log the failure reason.
    • Result caching: planning results for identical instructions can be cached and reused to reduce repeated inference.
    • Content moderation: rendered results need content safety review, and text in the image should also be checked for sensitive words.
    • Version records: record model versions and parameters separately for the planning and rendering stages to make it easier to locate the source of problems.
    • Cost control: a lighter model can be used for planning, and resolution and sampling steps can be capped for rendering.

    7. Limitations and Caveats

    • Planning does not solve all problems. When there are many entities and the layout is crowded, position boxes may still conflict.
    • Text rendering support varies by font, layout, and language, so it must be tested separately for each target language.
    • Copyright, likeness rights, and commercial licensing for generated content require separate evaluation; model outputs cannot be assumed to be compliant by default.
    • A model’s description of its own output cannot be used as validation. Key constraints should be checked by independent detection, recognition, or rule modules.

    In summary, reliable implementation of complex instructions depends on the combination of structured planning, explicit constraints, and independent verification. Simply scaling a single model is unlikely to solve relation, negation, and text problems at the same time.