Upload 20 files
Browse files- .gitattributes +3 -0
- Notebook/InceptionV3_Dogs_and_Cats_Classification.ipynb +0 -0
- Results/InceptionV3_Dogs_and_Cats_Classification.mp4 +3 -0
- app.py +7 -0
- app/__init__.py +0 -0
- app/__pycache__/gradio_app.cpython-310.pyc +0 -0
- app/__pycache__/gradio_app.cpython-314.pyc +0 -0
- app/__pycache__/main.cpython-313.pyc +0 -0
- app/__pycache__/model.cpython-310.pyc +0 -0
- app/__pycache__/model.cpython-313.pyc +0 -0
- app/__pycache__/model.cpython-314.pyc +0 -0
- app/gradio_app.py +87 -0
- app/main.py +26 -0
- app/model.py +42 -0
- examples/cat1.jpg +0 -0
- examples/cat2.jpg +3 -0
- examples/cat3.jpg +0 -0
- examples/dog1.jpg +0 -0
- examples/dog2.jpg +3 -0
- requirements.txt +7 -0
- saved_model/InceptionV3_Dogs_and_Cats_Classification.h5 +3 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,6 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
examples/cat2.jpg filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
examples/dog2.jpg filter=lfs diff=lfs merge=lfs -text
|
| 38 |
+
Results/InceptionV3_Dogs_and_Cats_Classification.mp4 filter=lfs diff=lfs merge=lfs -text
|
Notebook/InceptionV3_Dogs_and_Cats_Classification.ipynb
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
Results/InceptionV3_Dogs_and_Cats_Classification.mp4
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:40c4b5378681b9aea4108a5ee5db31cb026b110d6c1509cd9406b65cb8f845eb
|
| 3 |
+
size 5820619
|
app.py
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from app.gradio_app import demo
|
| 3 |
+
|
| 4 |
+
if __name__ == "__main__":
|
| 5 |
+
port = int(os.environ.get("PORT", 7860))
|
| 6 |
+
print(f"Launching Gradio demo on 0.0.0.0:{port}")
|
| 7 |
+
demo.launch(server_name="0.0.0.0", server_port=port, share=False, show_error=True)
|
app/__init__.py
ADDED
|
File without changes
|
app/__pycache__/gradio_app.cpython-310.pyc
ADDED
|
Binary file (2.3 kB). View file
|
|
|
app/__pycache__/gradio_app.cpython-314.pyc
ADDED
|
Binary file (3.7 kB). View file
|
|
|
app/__pycache__/main.cpython-313.pyc
ADDED
|
Binary file (1.58 kB). View file
|
|
|
app/__pycache__/model.cpython-310.pyc
ADDED
|
Binary file (1.05 kB). View file
|
|
|
app/__pycache__/model.cpython-313.pyc
ADDED
|
Binary file (1.76 kB). View file
|
|
|
app/__pycache__/model.cpython-314.pyc
ADDED
|
Binary file (2.01 kB). View file
|
|
|
app/gradio_app.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from PIL import Image
|
| 3 |
+
from .model import predict
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
model_path = os.path.join(
|
| 7 |
+
os.path.dirname(os.path.dirname(__file__)),
|
| 8 |
+
"saved_model",
|
| 9 |
+
"InceptionV3_Dogs_and_Cats_Classification.h5"
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
def classify_image(image):
|
| 13 |
+
if image is None:
|
| 14 |
+
return None, {"error": "Please upload an image"}
|
| 15 |
+
|
| 16 |
+
try:
|
| 17 |
+
label, confidence, probs = predict(image)
|
| 18 |
+
results = {
|
| 19 |
+
"Predicted Class": label,
|
| 20 |
+
"Confidence": f"{confidence * 100:.2f}%",
|
| 21 |
+
"Cat Probability": f"{probs['Cat'] * 100:.2f}%",
|
| 22 |
+
"Dog Probability": f"{probs['Dog'] * 100:.2f}%"
|
| 23 |
+
}
|
| 24 |
+
return image, results
|
| 25 |
+
|
| 26 |
+
except Exception as e:
|
| 27 |
+
return image, {"error": f"Classification failed: {str(e)}"}
|
| 28 |
+
|
| 29 |
+
with gr.Blocks(title="Cats vs Dogs Classifier", theme=gr.themes.Soft()) as demo:
|
| 30 |
+
gr.Markdown(
|
| 31 |
+
"""
|
| 32 |
+
# Cats vs Dogs Classifier
|
| 33 |
+
|
| 34 |
+
Upload an image of a cat or dog, and the InceptionV3 model will classify it!
|
| 35 |
+
|
| 36 |
+
**Model:** InceptionV3 (Transfer Learning)
|
| 37 |
+
**Classes:** Cat | Dog
|
| 38 |
+
**Image Size:** 256x256 pixels
|
| 39 |
+
|
| 40 |
+
**NOTE:**
|
| 41 |
+
- You can upload an image from your device, just press "X" icon and start uploading.
|
| 42 |
+
"""
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
with gr.Row():
|
| 46 |
+
with gr.Column():
|
| 47 |
+
gr.Markdown("### Upload Image")
|
| 48 |
+
image_input = gr.Image(
|
| 49 |
+
type="pil",
|
| 50 |
+
label="Upload Image",
|
| 51 |
+
sources=["upload", "webcam"],
|
| 52 |
+
interactive=True
|
| 53 |
+
)
|
| 54 |
+
with gr.Column():
|
| 55 |
+
gr.Markdown("### Prediction Results")
|
| 56 |
+
output = gr.JSON(label="Classification Results")
|
| 57 |
+
|
| 58 |
+
submit_btn = gr.Button("Classify Image", variant="primary", scale=1)
|
| 59 |
+
submit_btn.click(
|
| 60 |
+
fn=classify_image,
|
| 61 |
+
inputs=image_input,
|
| 62 |
+
outputs=[image_input, output]
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
gr.Markdown("### Examples")
|
| 66 |
+
gr.Examples(
|
| 67 |
+
examples=[
|
| 68 |
+
["examples/cat1.jpg"],
|
| 69 |
+
["examples/cat2.jpg"],
|
| 70 |
+
["examples/cat3.jpg"],
|
| 71 |
+
["examples/dog1.jpg"],
|
| 72 |
+
["examples/dog2.jpg"]
|
| 73 |
+
],
|
| 74 |
+
inputs=image_input,
|
| 75 |
+
outputs=[image_input, output],
|
| 76 |
+
fn=classify_image,
|
| 77 |
+
run_on_click=True,
|
| 78 |
+
label="Example Images (Click to run)"
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
if __name__ == "__main__":
|
| 82 |
+
demo.launch(
|
| 83 |
+
server_name="0.0.0.0",
|
| 84 |
+
server_port=7860,
|
| 85 |
+
share=False,
|
| 86 |
+
show_error=True
|
| 87 |
+
)
|
app/main.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, File
|
| 2 |
+
from fastapi.responses import JSONResponse
|
| 3 |
+
from app.model import predict
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import io
|
| 6 |
+
|
| 7 |
+
app = FastAPI(title="Cats and Dogs Image Classifier")
|
| 8 |
+
|
| 9 |
+
@app.post("/predict")
|
| 10 |
+
async def predict_image(file: UploadFile = File(...)):
|
| 11 |
+
try:
|
| 12 |
+
# Read image from uploaded file
|
| 13 |
+
contents = await file.read()
|
| 14 |
+
img = Image.open(io.BytesIO(contents))
|
| 15 |
+
|
| 16 |
+
# Run prediction
|
| 17 |
+
label, confidence, probs = predict(img)
|
| 18 |
+
|
| 19 |
+
return JSONResponse(content={
|
| 20 |
+
"predicted_label": label,
|
| 21 |
+
"confidence": round(confidence, 3),
|
| 22 |
+
"probabilities": {k: round(v, 3) for k, v in probs.items()}
|
| 23 |
+
})
|
| 24 |
+
|
| 25 |
+
except Exception as e:
|
| 26 |
+
return JSONResponse(content={"error": str(e)}, status_code=500)
|
app/model.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import tensorflow as tf
|
| 2 |
+
import numpy as np
|
| 3 |
+
from PIL import Image
|
| 4 |
+
|
| 5 |
+
# Load your trained CNN model
|
| 6 |
+
model = tf.keras.models.load_model(
|
| 7 |
+
"saved_model/InceptionV3_Dogs_and_Cats_Classification.h5",
|
| 8 |
+
compile=False
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
# Same label order you used when training (from LabelEncoder)
|
| 12 |
+
CLASS_NAMES = ["Cat", "Dog"]
|
| 13 |
+
|
| 14 |
+
def preprocess_image(img: Image.Image, target_size=(256, 256)):
|
| 15 |
+
|
| 16 |
+
img = img.convert("RGB") # ensure 3 channels
|
| 17 |
+
img = img.resize(target_size)
|
| 18 |
+
img = np.array(img).astype("float32") / 255.0 # normalize
|
| 19 |
+
img = np.expand_dims(img, axis=0) # (1, 256, 256, 3)
|
| 20 |
+
return img
|
| 21 |
+
|
| 22 |
+
def predict(img: Image.Image):
|
| 23 |
+
# Apply preprocessing
|
| 24 |
+
input_tensor = preprocess_image(img) # (1, 256, 256, 3)
|
| 25 |
+
|
| 26 |
+
# Model prediction (sigmoid output)
|
| 27 |
+
prob = float(model.predict(input_tensor)[0][0]) # probability of class 1 (Dog) or class 0 (Cat)
|
| 28 |
+
|
| 29 |
+
# Determine label based on 0.5 threshold
|
| 30 |
+
if prob >= 0.5:
|
| 31 |
+
label = CLASS_NAMES[1] # "Dog"
|
| 32 |
+
else:
|
| 33 |
+
label = CLASS_NAMES[0] # "Cat"
|
| 34 |
+
|
| 35 |
+
# Confidence and probability dictionary
|
| 36 |
+
confidence = prob if label == CLASS_NAMES[1] else 1 - prob
|
| 37 |
+
prob_dict = {
|
| 38 |
+
CLASS_NAMES[0]: 1 - prob,
|
| 39 |
+
CLASS_NAMES[1]: prob
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
return label, confidence, prob_dict
|
examples/cat1.jpg
ADDED
|
examples/cat2.jpg
ADDED
|
Git LFS Details
|
examples/cat3.jpg
ADDED
|
examples/dog1.jpg
ADDED
|
examples/dog2.jpg
ADDED
|
Git LFS Details
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
tensorflow
|
| 4 |
+
numpy
|
| 5 |
+
python-multipart
|
| 6 |
+
pillow
|
| 7 |
+
gradio
|
saved_model/InceptionV3_Dogs_and_Cats_Classification.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:78e103158acf66458a00ed9c90203be54e907f27adc2ee9fa9fdc059e944e2e8
|
| 3 |
+
size 142060784
|