Spaces:
Sleeping
Sleeping
File size: 3,929 Bytes
9935a4c 856d9db 9935a4c 856d9db 9935a4c 856d9db 9935a4c 856d9db 9935a4c 856d9db 9935a4c 856d9db 9935a4c 856d9db 9935a4c 856d9db 9935a4c 856d9db 9935a4c 856d9db 9935a4c 856d9db 9935a4c 856d9db 9935a4c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "a3278dc9-0d83-4a37-aece-e46ac416988f",
"metadata": {},
"outputs": [],
"source": [
"#| default_exp app"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d6810835-d62a-4f94-a52e-0e0cd163fb98",
"metadata": {},
"outputs": [],
"source": [
"#| export\n",
"from fastai.vision.all import *\n",
"import gradio as gr\n",
"title = \"FastAI - Big Cats Classifier\"\n",
"description = \"Classify big cats using all Resnet models available pre-trained in FastAI\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6092ad61-d5cd-40f7-b2d2-20a77b0c8b0f",
"metadata": {},
"outputs": [],
"source": [
"#| export\n",
"learners = {\n",
" \"resnet-18\" : 'models/resnet18-model.pkl',\n",
" \"resnet-34\" : 'models/resnet34-model.pkl',\n",
" \"resnet-50\" : 'models/resnet50-model.pkl',\n",
" \"resnet-101\": 'models/resnet101-model.pkl',\n",
" \"resnet-152\": 'models/resnet152-model.pkl'\n",
"}\n",
"models = list(learners.keys())\n",
"\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "632cbc1b-73b5-4992-8956-d4ae40f6b80b",
"metadata": {},
"outputs": [],
"source": [
"#| export\n",
" \n",
"def classify_image(img, model_file=\"resnet-101\"):\n",
" learn = load_learner(learners[model_file])\n",
" pred,idx,probs = learn.predict(img)\n",
" print(pred, idx, probs)\n",
" return dict(zip(learn.dls.vocab, map(float, probs)))\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9b5f1cc6-5173-475a-9365-0cab11db2d03",
"metadata": {},
"outputs": [],
"source": [
"example_images = [ 'cheetah.jpg', 'jaguar.jpg', 'tiger.jpg', 'cougar.jpg', 'lion.jpg', 'african leopard.jpg', 'clouded leopard.jpg', 'snow leopard.jpg' ]\n",
"\n",
"for c in example_images:\n",
" im = PILImage.create(c)\n",
" result = classify_image(im)\n",
" print(result)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a48e7483-c04b-4048-a1ae-34a8c7986a57",
"metadata": {},
"outputs": [],
"source": [
"#| export\n",
"image = gr.inputs.Image(size=(128,128))\n",
"model = gr.inputs.Dropdown(choices=models)\n",
"label = gr.outputs.Label()\n",
"example_images = [ 'cheetah.jpg', 'jaguar.jpg', 'tiger.jpg', 'cougar.jpg', 'lion.jpg', 'african leopard.jpg', 'clouded leopard.jpg', 'snow leopard.jpg' ]\n",
"example_models = [] #list(learners.values())\n",
"intf = gr.Interface(fn=classify_image, inputs=image, outputs=label, examples=example_images, title=title, description=description )\n",
"if __name__ == \"__main__\":\n",
" intf.launch(debug=True, inline=False)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cab071f9-7c3b-4b35-a0d1-3687731ffce5",
"metadata": {},
"outputs": [],
"source": [
"import nbdev\n",
"nbdev.export.nb_export('app.ipynb', './')\n",
"print('Export successful')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c7e6ddfb-9919-4a35-aac7-674d6fc5fd96",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "e56bc359-81c7-4e70-a84a-5f81a0713cd3",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"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.8.2"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|