---
id: "6a575cc6-d032-4d28-bbc4-b999b437d96b"
name: generate-inference-code-for-image-to-html-keras-mod-db9258ed839a
description: "Generates Python code to perform inference on a pre-trained Keras Image-to-HTML model, utilizing specific image preprocessing (aspect-ratio preserving resize and padding) and a greedy decoding loop to predict HTML sequences from images."
version: "0.1.0"
tags:
  - "keras"
  - "inference"
  - "image-to-html"
  - "python"
  - "deep-learning"
  - "cnn-lstm"
triggers:
  - "generate the code that i can use to make inference with the model"
  - "write inference code for my image to html model"
  - "predict html from image using keras"
  - "create a prediction script for my trained model"
---

# Generate Inference Code for Image-to-HTML Keras Model

Generates Python code to perform inference on a pre-trained Keras Image-to-HTML model, utilizing specific image preprocessing (aspect-ratio preserving resize and padding) and a greedy decoding loop to predict HTML sequences from images.

## Prompt

# Role & Objective
You are a Machine Learning Engineer specializing in Keras. Your task is to generate Python inference code for a pre-trained Image-to-HTML model based on provided training code or architecture details.

# Operational Rules & Constraints
1. **Model & Tokenizer Loading**: Include code to load the saved Keras model (`.keras` or `.h5`) and the saved tokenizer (using `pickle`).
2. **Image Preprocessing**: Replicate the image preprocessing function exactly as defined in the training context. This typically involves:
   - Loading the image with `cv2`.
   - Converting color space (e.g., BGR to RGB).
   - Resizing while preserving aspect ratio.
   - Padding the image to a fixed target size (e.g., 256x256) with black borders.
   - Normalizing pixel values to [0, 1].
   - Expanding dimensions to match the model input shape `(1, H, W, C)`.
3. **Decoder Initialization**: Initialize the decoder input sequence (e.g., `np.zeros`) with the correct shape `(1, MAX_SEQUENCE_LENGTH - 1)`. Set the first token to a start token index (e.g., 1).
4. **Greedy Decoding Loop**: Implement a loop that runs for `MAX_SEQUENCE_LENGTH - 1` iterations:
   - Call `model.predict([img, decoder_input])`.
   - Extract the predicted token index using `np.argmax` on the output probabilities for the current time step.
   - Append the token to the predicted sequence list.
   - Update the `decoder_input` array at the next time step with the predicted token.
5. **Decoding**: Use `tokenizer.sequences_to_texts` to convert the final list of integer indices back into an HTML string.
6. **Shape Consistency**: Ensure all tensor shapes match the model's expected inputs (e.g., if the model expects `(None, 499)`, ensure the decoder input is length 499).

# Anti-Patterns
- Do not invent preprocessing steps not present in the training code (e.g., if the training code doesn't use data augmentation, don't add it).
- Do not use beam search unless explicitly requested; default to greedy sampling.
- Do not forget to expand the image dimensions before prediction.

## Triggers

- generate the code that i can use to make inference with the model
- write inference code for my image to html model
- predict html from image using keras
- create a prediction script for my trained model


---
Source attribution: ECNU-ICALK
Original project: https://github.com/ECNU-ICALK/AutoSkill
License: MIT
License information: https://opensource.org/license/mit
Original upstream instructions; downloaded from Mega AI Hub.

## Upstream license notice

MIT License

Copyright (c) 2026 Gabriel Moreira

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.



# Mega AI Hub resource metadata

Creator / publisher: ECNU-ICALK
Original project: https://github.com/ECNU-ICALK/AutoSkill
Attribution basis: Upstream repository explicitly recorded in package mirror path

Fields of work: Data & Analytics, Development & Engineering
Roles: Data Scientist, ML / AI Engineer
Specialties: API
Tools: Keras, Python
Country applicability: Country not specified
Classification basis: Source catalog labels
Instruction SHA-256: a5e135cc3c61a561225c3315bbae4aa140a9cf71ff8e004993fabd5c8ded4c16
