Using deeplearning model for inference

Hi there i am stuck with this error whiile to use the model i exported to do inference
i do not want to build the data loader again because i just want to host the model with out references to the training data
UnboundLocalError: cannot access local variable ‘res.dls’ where it is not associated with a value

Can you provide a bit more code? It’ll be much easier to see what’s going on by seeing the code.

Thank you
i trained the model on kaggle and exported it with leaner.expor()
this is the code to intialise the model

``
# src/score.py


import json, base64
from fastai.vision.all import load_learner, PILImage
from pathlib import Path

# Global variable for the learner
learn = None

def init():
 
    Load the FastAI model into memory.
    Called once on app startup.
    
    global learn
    model_path = model_path = Path(__file__).parent.parent / "model" / "model_conn.pkl"
    print(f"Model located at: {model_path}")
    learn = load_learner(model_path)
    print(f"Model loaded from {model_path}")

def run(input_json: str) -> str:
 
    Run inference on a single image.
    Args:
        input_json: JSON string with {"image": "<base64-encoded image>"}
    Returns:
        JSON string with prediction result
    """
    try:
        global learn
        if learn is None:
            raise Exception("Model not initialized. Call init() first.")


        # Parse input JSON
        data = json.loads(input_json)
        if "image" not in data:
            raise ValueError("Missing 'image' key in input JSON")

        # Decode base64 image
        img_bytes = base64.b64decode(data["image"])
        img = PILImage.create(img_bytes)

        # Make prediction
        pred_class, pred_idx, outputs = learn.predict(img)

        # Prepare JSON result
        result = {
            "pred_class": str(pred_class),
            "pred_idx": int(pred_idx),
            "outputs": [float(x) for x in outputs]
        }

        return json.dumps(result)

    except Exception as e:
        # Return error as JSON string
        return json.dumps({"error": str(e)})`

when i run my container (severing the model via an endoint )
I get this error

INFO:plant-classifier:Loading model... /usr/local/lib/python3.11/site-packages/fastai/learner.py:455: UserWarning: load_learner uses Python’s insecure pickle module, which can execute malicious arbitrary code when loading. Only load files you trust.
If you only need to load model weights and optimizer state, use the safe Learner.load instead.
warn("load_learneruses Python's insecure pickle module, which can execute malicious arbitrary code when loading. Only load files you trust.\nIf you only need to load model weights and optimizer state, use the safeLearner.load instead.") Traceback (most recent call last): File "/usr/local/bin/uvicorn", line 7, in <module> sys.exit(main()) /app/model/model_conn.pkl ^^^^^^ File "/usr/local/lib/python3.11/site-packages/click/core.py", line 1442, in __call__ return self.main(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/click/core.py", line 1363, in main rv = self.invoke(ctx) ^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/click/core.py", line 1226, in invoke return ctx.invoke(self.callback, **ctx.params) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/click/core.py", line 794, in invoke return callback(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/uvicorn/main.py", line 413, in main run( File "/usr/local/lib/python3.11/site-packages/uvicorn/main.py", line 580, in run server.run() File "/usr/local/lib/python3.11/site-packages/uvicorn/server.py", line 67, in run return asyncio.run(self.serve(sockets=sockets)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/asyncio/runners.py", line 190, in run return runner.run(main) ^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/asyncio/runners.py", line 118, in run return self._loop.run_until_complete(task) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/asyncio/base_events.py", line 654, in run_until_complete return future.result() ^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/uvicorn/server.py", line 71, in serve await self._serve(sockets) File "/usr/local/lib/python3.11/site-packages/uvicorn/server.py", line 78, in _serve config.load() File "/usr/local/lib/python3.11/site-packages/uvicorn/config.py", line 436, in load self.loaded_app = import_from_string(self.app) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/uvicorn/importer.py", line 19, in import_from_string module = importlib.import_module(module_str) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/importlib/__init__.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "<frozen importlib._bootstrap>", line 1204, in _gcd_import File "<frozen importlib._bootstrap>", line 1176, in _find_and_load File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 690, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 940, in exec_module File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed File "/app/app.py", line 16, in <module> score_init() File "/app/src/score.py", line 17, in init learn = load_learner(model_path) ^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/fastai/learner.py", line 465, in load_learner res.dls.cpu() ^^^ UnboundLocalError: cannot access local variable 'res' where it is not associated with a value

I just want one model file I can host in production without having to rerence the training data and the data loaders

for more context this is the app.py file
`

# app.py
from fastapi import FastAPI, UploadFile, File, HTTPException
import json, base64, logging, asyncio

from src.score import init as score_init, run as score_run

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("plant-classifier")

# Initialize FastAPI app
app = FastAPI(title="Fastai Plant Classifier")

# Load the model once on startup
logger.info("Loading model...")
score_init()
logger.info("Model loaded successfully!")

# Maximum file size in bytes (e.g., 5MB)
MAX_UPLOAD_SIZE = 5 * 1024 * 1024


@app.post("/")
def defaults():
    return "Loaded Succefully"


@app.post("/predict")
async def predict(file: UploadFile = File(...)):
    try:
        # Check file size
        contents = await file.read()
        if len(contents) > MAX_UPLOAD_SIZE:
            raise HTTPException(status_code=413, detail="File too large")

        # Convert image to Base64 payload
        payload = {"image": base64.b64encode(contents).decode("utf-8")}

        # Run inference in a thread to avoid blocking the event loop
        loop = asyncio.get_running_loop()
        result_json = await loop.run_in_executor(None, score_run, json.dumps(payload))

        # Parse and return result
        return json.loads(result_json)

    except HTTPException as he:
        raise he
    except Exception as e:
        # Log the error internally
        logger.error(f"Prediction failed: {e}", exc_info=True)
        raise HTTPException(status_code=500, detail="Internal server error")
`

@Benonking
I had faced a similar error a few weeks back, but that was because the model needed my custom loss function loaded before it was loaded. If you don’t mind, can you share your training code? I think there’s something similar going on there. Also, if you search UnboundLocalError on this forum, there are a bunch of other people who have faced this in different contexts.

Thanks for response
I have searched for the relevant issue in the forum but nothing really solves my issue
Here is my training code
i get the data
`


#get the data
data_pth = '/kaggle/input/zazi-finals/data/'
#create a data block
dblock = DataBlock(
    blocks=(ImageBlock, CategoryBlock),
    get_items=get_image_files,
    splitter=RandomSplitter(valid_pct=0.2, seed=42),
    get_y = parent_label,
    item_tfms=Resize(224),
    batch_tfms=aug_transforms(mult=2)
)
dls = dblock.dataloaders(data_pth)
# then train
convernex_learner  = vision_learner(dls, 'convnext_tiny_in22k', metrics=accuracy)
lr = 0.001
convernex_learner.fine_tune(6, lr)
# export the model
convernex_learner.export('model_conn_d.pkl')
`
2 Likes

Thanks for the quick response