{
"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, ?B/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "116534ad956e439199abad118e13f5b4",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Downloading pytorch_model.bin: 0%| | 0.00/346M [00:00, ?B/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "bbf4d3ccfbf44c55ae9b59722cc2799d",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Downloading (…)rocessor_config.json: 0%| | 0.00/160 [00:00, ?B/s]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Create the image classification pipeline with GPU\n",
"vision_classifier = pipeline(model=\"google/vit-base-patch16-224\", device=0) # Specify the GPU device index"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "9129e663",
"metadata": {},
"outputs": [],
"source": [
"image_url=\"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg\""
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "982a5b7f",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"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
}