{ "cells": [ { "cell_type": "markdown", "id": "df084527", "metadata": {}, "source": [ "# Transformers image processing examples\n", "Transformers provides APIs and tools to easily download and train state-of-the-art pretrained models. \n", "Credits: Huggingface documentation" ] }, { "cell_type": "markdown", "id": "b642b2ed", "metadata": {}, "source": [ "## Image classification" ] }, { "cell_type": "code", "execution_count": 1, "id": "be6c9453", "metadata": {}, "outputs": [], "source": [ "from transformers import pipeline\n" ] }, { "cell_type": "code", "execution_count": 2, "id": "7fb58fb8", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "275e0faa56b9408da0ac037321f4c8e8", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Downloading (…)lve/main/config.json: 0%| | 0.00/69.7k [00:00" ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from IPython.display import Image\n", "Image(url=image_url)" ] }, { "cell_type": "code", "execution_count": 5, "id": "6cfc0312", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[{'score': 0.4335, 'label': 'lynx, catamount'},\n", " {'score': 0.0348,\n", " 'label': 'cougar, puma, catamount, mountain lion, painter, panther, Felis concolor'},\n", " {'score': 0.0324, 'label': 'snow leopard, ounce, Panthera uncia'},\n", " {'score': 0.0239, 'label': 'Egyptian cat'},\n", " {'score': 0.0229, 'label': 'tiger cat'}]" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "preds = vision_classifier(image_url)\n", "preds = [{\"score\": round(pred[\"score\"], 4), \"label\": pred[\"label\"]} for pred in preds]\n", "preds" ] }, { "cell_type": "code", "execution_count": null, "id": "c507a767", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "@webio": { "lastCommId": null, "lastKernelId": null }, "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.9" } }, "nbformat": 4, "nbformat_minor": 5 }