Upload 20 files
Browse filesFiles changed (21)
- .gitattributes+3
- Notebook/InceptionV3_Dogs_and_Cats_Classification.ipynbAdded
- Results/InceptionV3_Dogs_and_Cats_Classification.mp4Added+3
- app.pyAdded+7
- app/__init__.pyAdded
- app/__pycache__/gradio_app.cpython-310.pycAdded
- app/__pycache__/gradio_app.cpython-314.pycAdded
- app/__pycache__/main.cpython-313.pycAdded
- app/__pycache__/model.cpython-310.pycAdded
- app/__pycache__/model.cpython-313.pycAdded
- app/__pycache__/model.cpython-314.pycAdded
- app/gradio_app.pyAdded+87
- app/main.pyAdded+26
- app/model.pyAdded+42
- examples/cat1.jpgAdded
- examples/cat2.jpgAdded
- examples/cat3.jpgAdded
- examples/dog1.jpgAdded
- examples/dog2.jpgAdded
- requirements.txtAdded+7
- saved_model/InceptionV3_Dogs_and_Cats_Classification.h5Added+3
32 unmodified lines33343532 unmodified lines*.zip filter=lfs diff=lfs merge=lfs -text*.zst filter=lfs diff=lfs merge=lfs -text*tfevents* filter=lfs diff=lfs merge=lfs -text32 unmodified lines33343536373832 unmodified lines*.zip filter=lfs diff=lfs merge=lfs -text*.zst filter=lfs diff=lfs merge=lfs -text*tfevents* filter=lfs diff=lfs merge=lfs -textexamples/cat2.jpg filter=lfs diff=lfs merge=lfs -textexamples/dog2.jpg filter=lfs diff=lfs merge=lfs -textResults/InceptionV3_Dogs_and_Cats_Classification.mp4 filter=lfs diff=lfs merge=lfs -text
123version https://git-lfs.github.com/spec/v1oid sha256:40c4b5378681b9aea4108a5ee5db31cb026b110d6c1509cd9406b65cb8f845ebsize 58206191234567import os
from app.gradio_app import demo
if __name__ == "__main__":
port = int(os.environ.get("PORT", 7860))
print(f"Launching Gradio demo on 0.0.0.0:{port}")
demo.launch(server_name="0.0.0.0", server_port=port, share=False, show_error=True)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687import gradio as gr
from PIL import Image
from .model import predict
import os
model_path = os.path.join(
os.path.dirname(os.path.dirname(__file__)),
"saved_model",
"InceptionV3_Dogs_and_Cats_Classification.h5"
)
def classify_image(image):
if image is None:
return None, {"error": "Please upload an image"}
try:
label, confidence, probs = predict(image)
results = {
"Predicted Class": label,
"Confidence": f"{confidence * 100:.2f}%",
"Cat Probability": f"{probs['Cat'] * 100:.2f}%",
"Dog Probability": f"{probs['Dog'] * 100:.2f}%"
}
return image, results
except Exception as e:
return image, {"error": f"Classification failed: {str(e)}"}
with gr.Blocks(title="Cats vs Dogs Classifier", theme=gr.themes.Soft()) as demo:
gr.Markdown(
"""
# Cats vs Dogs Classifier
Upload an image of a cat or dog, and the InceptionV3 model will classify it!
**Model:** InceptionV3 (Transfer Learning)
**Classes:** Cat | Dog
**Image Size:** 256x256 pixels
**NOTE:**
- You can upload an image from your device, just press "X" icon and start uploading.
"""
)
with gr.Row():
with gr.Column():
gr.Markdown("### Upload Image")
image_input = gr.Image(
type="pil",
label="Upload Image",
sources=["upload", "webcam"],
interactive=True
)
with gr.Column():
gr.Markdown("### Prediction Results")
output = gr.JSON(label="Classification Results")
submit_btn = gr.Button("Classify Image", variant="primary", scale=1)
submit_btn.click(
fn=classify_image,
inputs=image_input,
outputs=[image_input, output]
)
gr.Markdown("### Examples")
gr.Examples(
examples=[
["examples/cat1.jpg"],
["examples/cat2.jpg"],
["examples/cat3.jpg"],
["examples/dog1.jpg"],
["examples/dog2.jpg"]
],
inputs=image_input,
outputs=[image_input, output],
fn=classify_image,
run_on_click=True,
label="Example Images (Click to run)"
)
if __name__ == "__main__":
demo.launch(
server_name="0.0.0.0",
server_port=7860,
share=False,
show_error=True
)1234567891011121314151617181920212223242526from fastapi import FastAPI, UploadFile, File
from fastapi.responses import JSONResponse
from app.model import predict
from PIL import Image
import io
app = FastAPI(title="Cats and Dogs Image Classifier")
@app.post("/predict")
async def predict_image(file: UploadFile = File(...)):
try:
# Read image from uploaded file
contents = await file.read()
img = Image.open(io.BytesIO(contents))
# Run prediction
label, confidence, probs = predict(img)
return JSONResponse(content={
"predicted_label": label,
"confidence": round(confidence, 3),
"probabilities": {k: round(v, 3) for k, v in probs.items()}
})
except Exception as e:
return JSONResponse(content={"error": str(e)}, status_code=500)123456789101112131415161718192021222324252627282930313233343536373839404142import tensorflow as tf
import numpy as np
from PIL import Image
# Load your trained CNN model
model = tf.keras.models.load_model(
"saved_model/InceptionV3_Dogs_and_Cats_Classification.h5",
compile=False
)
# Same label order you used when training (from LabelEncoder)
CLASS_NAMES = ["Cat", "Dog"]
def preprocess_image(img: Image.Image, target_size=(256, 256)):
img = img.convert("RGB") # ensure 3 channels
img = img.resize(target_size)
img = np.array(img).astype("float32") / 255.0 # normalize
img = np.expand_dims(img, axis=0) # (1, 256, 256, 3)
return img
def predict(img: Image.Image):
# Apply preprocessing
input_tensor = preprocess_image(img) # (1, 256, 256, 3)
# Model prediction (sigmoid output)
prob = float(model.predict(input_tensor)[0][0]) # probability of class 1 (Dog) or class 0 (Cat)
# Determine label based on 0.5 threshold
if prob >= 0.5:
label = CLASS_NAMES[1] # "Dog"
else:
label = CLASS_NAMES[0] # "Cat"
# Confidence and probability dictionary
confidence = prob if label == CLASS_NAMES[1] else 1 - prob
prob_dict = {
CLASS_NAMES[0]: 1 - prob,
CLASS_NAMES[1]: prob
}
return label, confidence, prob_dict1234567fastapi
uvicorn
tensorflow
numpy
python-multipart
pillow
gradio123version https://git-lfs.github.com/spec/v1oid sha256:78e103158acf66458a00ed9c90203be54e907f27adc2ee9fa9fdc059e944e2e8size 142060784.gitattributes+3
Notebook/InceptionV3_Dogs_and_Cats_Classification.ipynbAdded
Results/InceptionV3_Dogs_and_Cats_Classification.mp4Added+3
app.pyAdded+7
app/__init__.pyAdded
app/__pycache__/gradio_app.cpython-310.pycAdded
app/__pycache__/gradio_app.cpython-314.pycAdded
app/__pycache__/main.cpython-313.pycAdded
app/__pycache__/model.cpython-310.pycAdded
app/__pycache__/model.cpython-313.pycAdded
app/__pycache__/model.cpython-314.pycAdded
app/gradio_app.pyAdded+87
app/main.pyAdded+26
app/model.pyAdded+42
examples/cat1.jpgAdded

examples/cat2.jpgAdded

Large File Pointer Details
- SHA256: 44da7e85545825aa4a61222dfb61ba2e8f4e42e36009bf0b4d356fb65207ae31
- Pointer size: 131 Bytes
- Size of remote file: 179 kB
examples/cat3.jpgAdded

examples/dog1.jpgAdded

examples/dog2.jpgAdded

Large File Pointer Details
- SHA256: 1b358721ad06eefa76e06b48b5d6548e7b2cac8342630287ed63e563ecb8646d
- Pointer size: 131 Bytes
- Size of remote file: 179 kB
requirements.txtAdded+7
saved_model/InceptionV3_Dogs_and_Cats_Classification.h5Added+3