{"slug": "a-hands-on-coding-tutorial-on-qualcomm-ai-hub-models-for-classification-object", "title": "A Hands-On Coding Tutorial on Qualcomm AI Hub Models for Classification, Object Detection, and Hardware-Aware Deployment", "summary": "Qualcomm AI Hub provides an end-to-end workflow for deploying machine learning models on Qualcomm devices, as demonstrated in a new tutorial that walks through MobileNet-V2 classification and YOLOv7 object detection. The tutorial covers model discovery, local PyTorch inference with input shape conversion, and optional cloud-based compilation and profiling on real Qualcomm hardware using an API token. This hands-on guide enables developers to bridge the gap between PyTorch model development and hardware-aware deployment on Qualcomm platforms.", "body_md": "In this tutorial, we work through an end-to-end** **workflow for[ Qualcomm AI Hub Models](https://github.com/qualcomm/ai-hub-models). We start by setting up the required package, discovering the available model collection, and loading MobileNet-V2 for local PyTorch inference. We also handle an important input-shape issue by converting NHWC image tensors into the NCHW format expected by the model. From there, we run inference on both the model’s built-in sample input and a real image, inspect top predictions, execute the official Qualcomm AI Hub CLI demo, and extend the workflow with a YOLOv7 object detection example. Also, we include an optional cloud-device section where we compile, profile, and run the model on a real Qualcomm device when an API token is available.\n\n``` python\nimport subprocess, sys, os, glob, textwrap, traceback\nimport numpy as np, torch\nfrom PIL import Image\nimport matplotlib.pyplot as plt\ndef pip_install(*pkgs):\n   subprocess.run([sys.executable, \"-m\", \"pip\", \"install\", \"-q\", *pkgs], check=True)\npip_install(\"qai_hub_models\")\nOUT_DIR = \"/content/qaihm_out\"; os.makedirs(OUT_DIR, exist_ok=True)\ntorch.set_grad_enabled(False)\ndef to_nchw(value):\n   arr = value[0] if isinstance(value, (list, tuple)) else value\n   t = torch.from_numpy(np.asarray(arr, dtype=np.float32))\n   if t.ndim == 3:\n       t = t.unsqueeze(0)\n   if t.ndim == 4 and t.shape[1] != 3 and t.shape[-1] == 3:\n       t = t.permute(0, 3, 1, 2).contiguous()\n   return t\n```\n\nWe begin by importing libraries and setting up a helper function to install packages directly inside Colab. We install qai_hub_models, create an output directory, and disable gradient tracking since we only need inference. We also define the to_nchw() function to convert any input image tensor to the channel-first format expected by the model.\n\n``` python\nimport pkgutil, qai_hub_models.models as _m\nmodel_ids = sorted(n for _, n, p in pkgutil.iter_modules(_m.__path__)\n                  if p and not n.startswith(\"_\"))\nprint(f\">>> {len(model_ids)} models available. First 40:\\n\")\nprint(textwrap.fill(\", \".join(model_ids[:40]), 100), \"\\n\")\nfrom qai_hub_models.models.mobilenet_v2 import Model as MobileNetV2\nmodel = MobileNetV2.from_pretrained().eval()\nspec = model.get_input_spec()\ninput_name = list(spec.keys())[0]\nprint(\">>> Input:\", input_name, spec[input_name].shape, spec[input_name].dtype)\nfrom torchvision.models import MobileNet_V2_Weights\nIMAGENET_CLASSES = MobileNet_V2_Weights.IMAGENET1K_V1.meta[\"categories\"]\ndef top5(logits):\n   if logits.ndim == 1: logits = logits.unsqueeze(0)\n   probs = torch.softmax(logits, dim=1)[0]\n   conf, idx = probs.topk(5)\n   return [(IMAGENET_CLASSES[i], float(c)) for c, i in zip(conf, idx)]\n```\n\nWe discover the available Qualcomm AI Hub model packages and print the first set of model IDs to understand what is accessible. We then load the pretrained MobileNet-V2 model, read its input specification, and identify the correct input name. We also prepare the ImageNet class labels and define a top5() function to convert model logits into readable top-5 predictions.\n\n```\nsample = model.sample_inputs()\nx = to_nchw(sample[input_name])\nprint(\">>> fed tensor shape:\", tuple(x.shape))\nprint(\"\\n>>> Top-5 for the built-in sample input:\")\nfor label, conf in top5(model(x)):\n   print(f\"    {conf:6.2%}  {label}\")\nfrom torchvision import transforms\npreprocess = transforms.Compose([\n   transforms.Resize(256), transforms.CenterCrop(224), transforms.ToTensor(),\n])\nimg = None\ntry:\n   import urllib.request\n   p = os.path.join(OUT_DIR, \"input.jpg\")\n   urllib.request.urlretrieve(\n       \"https://raw.githubusercontent.com/pytorch/hub/master/images/dog.jpg\", p)\n   img = Image.open(p).convert(\"RGB\")\nexcept Exception as e:\n   print(\">>> photo download skipped:\", e)\nif img is not None:\n   preds = top5(model(preprocess(img).unsqueeze(0)))\n   print(\"\\n>>> Top-5 for the downloaded photo:\")\n   for label, conf in preds: print(f\"    {conf:6.2%}  {label}\")\n   plt.figure(figsize=(5,5)); plt.imshow(img); plt.axis(\"off\")\n   plt.title(f\"{preds[0][0]}  ({preds[0][1]:.1%})\"); plt.show()\n```\n\nWe first run inference using the model’s built-in sample input and use to_nchw() to fix the tensor shape before passing it to MobileNet-V2. We then download a real image, preprocess it using standard resizing, cropping, and tensor conversion steps, and run another prediction. We finally display the image with the top predicted label to visually connect the model output to the input photo.\n\n``` python\ndef run_demo(module, extra=None, timeout=900):\n   cmd = [sys.executable, \"-m\", module, \"--eval-mode\", \"fp\",\n          \"--output-dir\", OUT_DIR] + (extra or [])\n   print(f\"\\n>>> {' '.join(cmd)}\")\n   try:\n       r = subprocess.run(cmd, capture_output=True, text=True, timeout=timeout)\n       print(\"\\n\".join((r.stdout + r.stderr).strip().splitlines()[-25:]))\n   except Exception as e:\n       print(\">>> demo skipped:\", e)\nrun_demo(\"qai_hub_models.models.mobilenet_v2.demo\")\ntry:\n   pip_install(\"qai_hub_models[yolov7]\")\n   run_demo(\"qai_hub_models.models.yolov7.demo\")\n   imgs = sorted(glob.glob(OUT_DIR + \"/*.png\") + glob.glob(OUT_DIR + \"/*.jpg\"),\n                 key=os.path.getmtime)\n   if imgs:\n       plt.figure(figsize=(9,9)); plt.imshow(Image.open(imgs[-1]).convert(\"RGB\"))\n       plt.axis(\"off\"); plt.title(\"YOLOv7 detections\"); plt.show()\n   else:\n       print(\">>> no output image found (results may have printed instead).\")\nexcept Exception:\n   print(\">>> YOLOv7 section skipped:\\n\", traceback.format_exc())\n```\n\nWe define a reusable run_demo() function that executes official Qualcomm AI Hub model demos from the command line. We use it to run the MobileNet-V2 demo and then install the YOLOv7 extras for object detection. We run the YOLOv7 demo, search for the generated output image, and visualize the detections if an image is created.\n\n``` python\ntry:\n   import qai_hub as hub\n   devices = hub.get_devices()\n   print(f\"\\n>>> Authenticated. {len(devices)} cloud devices available.\")\n   device = hub.Device(\"Samsung Galaxy S24 (Family)\")\n   sample = model.sample_inputs()\n   nchw = to_nchw(sample[input_name])\n   traced = torch.jit.trace(model, [nchw])\n   cloud_inputs = {input_name: [nchw.numpy()]}\n   cj = hub.submit_compile_job(model=traced, device=device,\n                               input_specs=model.get_input_spec(),\n                               options=\"--target_runtime tflite\")\n   target = cj.get_target_model(); print(\">>> compiled:\", cj.url)\n   pj = hub.submit_profile_job(model=target, device=device); print(\">>> profiling:\", pj.url)\n   ij = hub.submit_inference_job(model=target, device=device, inputs=cloud_inputs)\n   out = ij.download_output_data()\n   dev_logits = torch.from_numpy(np.asarray(list(out.values())[0][0]))\n   print(\">>> Top-5 from the REAL device:\")\n   for label, conf in top5(dev_logits): print(f\"    {conf:6.2%}  {label}\")\n   target.download(os.path.join(OUT_DIR, \"mobilenet_v2.tflite\"))\n   print(\">>> saved compiled .tflite to\", OUT_DIR)\nexcept Exception as e:\n   print(\"\\n>>> Cloud (on-device) section skipped — no API token configured.\")\n   print(\"    Get one at workbench.aihub.qualcomm.com, then:\")\n   print(\"    !qai-hub configure --api_token YOUR_TOKEN\")\n   print(\"    detail:\", (str(e).splitlines() or [type(e).__name__])[0])\nprint(\"\\n>>> Tutorial complete. Outputs in:\", OUT_DIR)\n```\n\nWe include an optional Qualcomm AI Hub cloud workflow that runs only when an API token is configured. We retrieve available cloud devices, trace the PyTorch model, compile it for TFLite, profile it on a Qualcomm device, and submit an inference job. We then download the device output, print the top predictions, save the compiled TFLite model, and finish by showing where all tutorial outputs are stored.\n\nIn conclusion, we have a complete practical workflow for using Qualcomm AI Hub Models inside Colab. We learned how to load pretrained models, prepare inputs correctly, run local inference, visualize classification and detection results, and use the official demos as reproducible reference points. We also saw how the same model can move beyond local PyTorch execution into Qualcomm’s cloud-device pipeline for compilation, profiling, and real-device inference. It provides a path from simple experimentation to hardware-aware deployment with Qualcomm AI Hub.\n\nCheck out the ** Full Codes with Notebook here. **Also, feel free to follow us on\n\n**and don’t forget to join our**[Twitter](https://x.com/intent/follow?screen_name=marktechpost)\n\n**and Subscribe to**\n\n[150k+ ML SubReddit](https://www.reddit.com/r/machinelearningnews/)**. Wait! are you on telegram?**\n\n[our Newsletter](https://www.aidevsignals.com/)\n\n[now you can join us on telegram as well.](https://t.me/machinelearningresearchnews)Need to partner with us for promoting your GitHub Repo OR Hugging Face Page OR Product Release OR Webinar etc.? [Connect with us](https://forms.gle/wbash1wF6efRj8G58)\n\nSana Hassan, a consulting intern at Marktechpost and dual-degree student at IIT Madras, is passionate about applying technology and AI to address real-world challenges. With a keen interest in solving practical problems, he brings a fresh perspective to the intersection of AI and real-life solutions.\n\n- Sana Hassan\n- Sana Hassan\n- Sana Hassan\n- Sana Hassan", "url": "https://wpnews.pro/news/a-hands-on-coding-tutorial-on-qualcomm-ai-hub-models-for-classification-object", "canonical_source": "https://www.marktechpost.com/2026/06/05/a-hands-on-coding-tutorial-on-qualcomm-ai-hub-models-for-classification-object-detection-and-hardware-aware-deployment/", "published_at": "2026-06-05 22:32:51+00:00", "updated_at": "2026-06-05 23:02:40.856136+00:00", "lang": "en", "topics": ["machine-learning", "computer-vision", "neural-networks", "ai-tools", "ai-infrastructure"], "entities": ["Qualcomm", "Qualcomm AI Hub", "PyTorch", "MobileNet-V2", "YOLOv7"], "alternates": {"html": "https://wpnews.pro/news/a-hands-on-coding-tutorial-on-qualcomm-ai-hub-models-for-classification-object", "markdown": "https://wpnews.pro/news/a-hands-on-coding-tutorial-on-qualcomm-ai-hub-models-for-classification-object.md", "text": "https://wpnews.pro/news/a-hands-on-coding-tutorial-on-qualcomm-ai-hub-models-for-classification-object.txt", "jsonld": "https://wpnews.pro/news/a-hands-on-coding-tutorial-on-qualcomm-ai-hub-models-for-classification-object.jsonld"}}