Spaces:
Runtime error
Runtime error
Commit ·
f1c6cd4
0
Parent(s):
gradio classifier
Browse files- .gitignore +2 -0
- README.md +0 -0
- app.py +67 -0
- ml/data/train_11009.jpg +0 -0
- ml/data/train_11716.jpg +0 -0
- ml/data/train_11890.jpg +0 -0
- ml/data/train_3321.jpg +0 -0
- notebooks/__init__.py +0 -0
- notebooks/classification_inference.ipynb +173 -0
- requirements.txt +2 -0
.gitignore
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
.venv
|
| 2 |
+
__pycache__
|
README.md
ADDED
|
File without changes
|
app.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
import torch
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import torchvision.transforms as T
|
| 6 |
+
|
| 7 |
+
import gradio as gr
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
# Current path
|
| 11 |
+
current_path = os.getcwd()
|
| 12 |
+
|
| 13 |
+
# Define the model path
|
| 14 |
+
model_path = os.path.join(current_path, "ml", "models", "model.pt")
|
| 15 |
+
|
| 16 |
+
# Determine the device to use (GPU if available, otherwise CPU)
|
| 17 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 18 |
+
|
| 19 |
+
# Load model
|
| 20 |
+
model = torch.jit.load(model_path)
|
| 21 |
+
model.eval() # Set to evaluation mode
|
| 22 |
+
|
| 23 |
+
# Depending on the device, load the model
|
| 24 |
+
model = model.to(device)
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
# Define the transformation
|
| 28 |
+
transform = T.Compose(
|
| 29 |
+
[
|
| 30 |
+
T.Resize(224),
|
| 31 |
+
T.CenterCrop(224),
|
| 32 |
+
T.ToTensor(), # Converts to [C, H, W] with values in [0, 1]
|
| 33 |
+
T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), # ImageNet mean # ImageNet std
|
| 34 |
+
]
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def cls_helper(label):
|
| 39 |
+
if label == 0:
|
| 40 |
+
return "Clear sky"
|
| 41 |
+
elif label == 1:
|
| 42 |
+
return "Cloudy"
|
| 43 |
+
elif label == 2:
|
| 44 |
+
return "Haze"
|
| 45 |
+
else:
|
| 46 |
+
return "Unknown"
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def predict(image: Image.Image):
|
| 50 |
+
img = image.convert("RGB")
|
| 51 |
+
tensor = transform(img).unsqueeze(0) # [1, 3, 224, 224]
|
| 52 |
+
|
| 53 |
+
with torch.no_grad():
|
| 54 |
+
output = model(tensor)
|
| 55 |
+
pred_idx = torch.argmax(output, dim=1).item()
|
| 56 |
+
pred_class = cls_helper(pred_idx)
|
| 57 |
+
|
| 58 |
+
return pred_class
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
interface = gr.Interface(
|
| 62 |
+
fn=predict,
|
| 63 |
+
inputs=gr.Image(type="pil"),
|
| 64 |
+
outputs=["text"],
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
interface.launch()
|
ml/data/train_11009.jpg
ADDED
|
ml/data/train_11716.jpg
ADDED
|
ml/data/train_11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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": 1,
|
| 6 |
+
"id": "875564f0",
|
| 7 |
+
"metadata": {},
|
| 8 |
+
"outputs": [],
|
| 9 |
+
"source": [
|
| 10 |
+
"import os\n",
|
| 11 |
+
"\n",
|
| 12 |
+
"import torch\n",
|
| 13 |
+
"from PIL import Image\n",
|
| 14 |
+
"import torchvision.transforms as T"
|
| 15 |
+
]
|
| 16 |
+
},
|
| 17 |
+
{
|
| 18 |
+
"cell_type": "code",
|
| 19 |
+
"execution_count": 2,
|
| 20 |
+
"id": "33891e0b",
|
| 21 |
+
"metadata": {},
|
| 22 |
+
"outputs": [],
|
| 23 |
+
"source": [
|
| 24 |
+
"# Current path\n",
|
| 25 |
+
"current_path = os.getcwd()\n",
|
| 26 |
+
"\n",
|
| 27 |
+
"# Define the model path\n",
|
| 28 |
+
"model_path = os.path.join(current_path, '..', 'ml', 'models', 'model.pt')"
|
| 29 |
+
]
|
| 30 |
+
},
|
| 31 |
+
{
|
| 32 |
+
"cell_type": "code",
|
| 33 |
+
"execution_count": 3,
|
| 34 |
+
"id": "0085c439",
|
| 35 |
+
"metadata": {},
|
| 36 |
+
"outputs": [],
|
| 37 |
+
"source": [
|
| 38 |
+
"# Determine the device to use (GPU if available, otherwise CPU)\n",
|
| 39 |
+
"device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n",
|
| 40 |
+
"\n",
|
| 41 |
+
"# Load model\n",
|
| 42 |
+
"model = torch.jit.load(model_path)\n",
|
| 43 |
+
"model.eval() # Set to evaluation mode\n",
|
| 44 |
+
"\n",
|
| 45 |
+
"# Depending on the device, load the model\n",
|
| 46 |
+
"model = model.to(device)"
|
| 47 |
+
]
|
| 48 |
+
},
|
| 49 |
+
{
|
| 50 |
+
"cell_type": "code",
|
| 51 |
+
"execution_count": 10,
|
| 52 |
+
"id": "e3620e37",
|
| 53 |
+
"metadata": {},
|
| 54 |
+
"outputs": [],
|
| 55 |
+
"source": [
|
| 56 |
+
"# Define data path\n",
|
| 57 |
+
"data_path = os.path.join(current_path, '..', 'ml', 'data')\n",
|
| 58 |
+
"\n",
|
| 59 |
+
"# Define the image path\n",
|
| 60 |
+
"img_path = os.path.join(data_path, \"train_11716.jpg\")"
|
| 61 |
+
]
|
| 62 |
+
},
|
| 63 |
+
{
|
| 64 |
+
"cell_type": "code",
|
| 65 |
+
"execution_count": 11,
|
| 66 |
+
"id": "e8375f5d",
|
| 67 |
+
"metadata": {},
|
| 68 |
+
"outputs": [],
|
| 69 |
+
"source": [
|
| 70 |
+
"# Load and preprocess the image\n",
|
| 71 |
+
"image = Image.open(img_path).convert(\"RGB\")\n",
|
| 72 |
+
"\n",
|
| 73 |
+
"# Define the transformation\n",
|
| 74 |
+
"transform = T.Compose(\n",
|
| 75 |
+
" [\n",
|
| 76 |
+
" T.Resize(224),\n",
|
| 77 |
+
" T.CenterCrop(224),\n",
|
| 78 |
+
" T.ToTensor(), # Converts to [C, H, W] with values in [0, 1]\n",
|
| 79 |
+
" T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), # ImageNet mean # ImageNet std\n",
|
| 80 |
+
" ]\n",
|
| 81 |
+
")\n",
|
| 82 |
+
"\n",
|
| 83 |
+
"# Prepare the input tensor\n",
|
| 84 |
+
"input_tensor = transform(image).unsqueeze(0) # Add batch dim → [1, 3, 224, 224]"
|
| 85 |
+
]
|
| 86 |
+
},
|
| 87 |
+
{
|
| 88 |
+
"cell_type": "code",
|
| 89 |
+
"execution_count": 18,
|
| 90 |
+
"id": "0b3a589f",
|
| 91 |
+
"metadata": {},
|
| 92 |
+
"outputs": [
|
| 93 |
+
{
|
| 94 |
+
"name": "stdout",
|
| 95 |
+
"output_type": "stream",
|
| 96 |
+
"text": [
|
| 97 |
+
"Predicted class index: 1\n"
|
| 98 |
+
]
|
| 99 |
+
}
|
| 100 |
+
],
|
| 101 |
+
"source": [
|
| 102 |
+
"with torch.no_grad():\n",
|
| 103 |
+
" output = model(input_tensor)\n",
|
| 104 |
+
" pred = torch.argmax(output, dim=1)\n",
|
| 105 |
+
"\n",
|
| 106 |
+
"pred_class = pred.item()\n",
|
| 107 |
+
"\n",
|
| 108 |
+
"print(f\"Predicted class index: {pred.item()}\")"
|
| 109 |
+
]
|
| 110 |
+
},
|
| 111 |
+
{
|
| 112 |
+
"cell_type": "code",
|
| 113 |
+
"execution_count": 23,
|
| 114 |
+
"id": "9d676ebf",
|
| 115 |
+
"metadata": {},
|
| 116 |
+
"outputs": [],
|
| 117 |
+
"source": [
|
| 118 |
+
"def show_image(image: Image.Image, pred_cls: int) -> None:\n",
|
| 119 |
+
" \"\"\"Display an image.\"\"\"\n",
|
| 120 |
+
" if pred_cls == 0:\n",
|
| 121 |
+
" title = \"Clear sky\"\n",
|
| 122 |
+
" elif pred_cls == 1:\n",
|
| 123 |
+
" title = \"Cloudy\"\n",
|
| 124 |
+
" elif pred_cls == 2:\n",
|
| 125 |
+
" title = \"Haze\"\n",
|
| 126 |
+
" else:\n",
|
| 127 |
+
" title = \"Unknown\"\n",
|
| 128 |
+
"\n",
|
| 129 |
+
" image.show(title=title)\n",
|
| 130 |
+
" print(title)"
|
| 131 |
+
]
|
| 132 |
+
},
|
| 133 |
+
{
|
| 134 |
+
"cell_type": "code",
|
| 135 |
+
"execution_count": 24,
|
| 136 |
+
"id": "d0935f27",
|
| 137 |
+
"metadata": {},
|
| 138 |
+
"outputs": [
|
| 139 |
+
{
|
| 140 |
+
"name": "stdout",
|
| 141 |
+
"output_type": "stream",
|
| 142 |
+
"text": [
|
| 143 |
+
"Cloudy\n"
|
| 144 |
+
]
|
| 145 |
+
}
|
| 146 |
+
],
|
| 147 |
+
"source": [
|
| 148 |
+
"show_image(image, pred_class)"
|
| 149 |
+
]
|
| 150 |
+
}
|
| 151 |
+
],
|
| 152 |
+
"metadata": {
|
| 153 |
+
"kernelspec": {
|
| 154 |
+
"display_name": ".venv",
|
| 155 |
+
"language": "python",
|
| 156 |
+
"name": "python3"
|
| 157 |
+
},
|
| 158 |
+
"language_info": {
|
| 159 |
+
"codemirror_mode": {
|
| 160 |
+
"name": "ipython",
|
| 161 |
+
"version": 3
|
| 162 |
+
},
|
| 163 |
+
"file_extension": ".py",
|
| 164 |
+
"mimetype": "text/x-python",
|
| 165 |
+
"name": "python",
|
| 166 |
+
"nbconvert_exporter": "python",
|
| 167 |
+
"pygments_lexer": "ipython3",
|
| 168 |
+
"version": "3.13.5"
|
| 169 |
+
}
|
| 170 |
+
},
|
| 171 |
+
"nbformat": 4,
|
| 172 |
+
"nbformat_minor": 5
|
| 173 |
+
}
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastai
|
| 2 |
+
gradio
|