Spaces:
Sleeping
Sleeping
Add example images
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
Files changed (50)
- Dockerfile+5
- __pycache__/model_loader.cpython-39.pycAdded
- app.py+76-25
- confusion_matrix_examples.zipAdded+3
- confusion_matrix_examples/FN/Stable Diffusion_0001 (6).jpgAdded
- confusion_matrix_examples/FN/Stable Diffusion_0003 (3).jpgAdded
- confusion_matrix_examples/FN/Stable Diffusion_0004 (2).jpgAdded
- confusion_matrix_examples/FN/Stable Diffusion_0013 (10).jpgAdded
- confusion_matrix_examples/FN/Stable Diffusion_0013.jpgAdded
- confusion_matrix_examples/FN/Stable Diffusion_0017 (10).jpgAdded
- confusion_matrix_examples/FN/Stable Diffusion_0019 (10).jpgAdded
- confusion_matrix_examples/FN/Stable Diffusion_0019 (9).jpgAdded
- confusion_matrix_examples/FN/Stable Diffusion_0027 (7).jpgAdded
- confusion_matrix_examples/FN/Stable Diffusion_0030.jpgAdded
- confusion_matrix_examples/FP/Stable Diffusion_101 (3).jpgAdded
- confusion_matrix_examples/FP/Stable Diffusion_102 (10).jpgAdded
- confusion_matrix_examples/FP/Stable Diffusion_102 (9).jpgAdded
- confusion_matrix_examples/FP/Stable Diffusion_112 (3).jpgAdded
- confusion_matrix_examples/FP/Stable Diffusion_115 (4).jpgAdded
- confusion_matrix_examples/FP/Stable Diffusion_117 (3).jpgAdded
- confusion_matrix_examples/FP/Stable Diffusion_117 (9).jpgAdded
- confusion_matrix_examples/FP/Stable Diffusion_118 (4).jpgAdded
- confusion_matrix_examples/FP/Stable Diffusion_124 (3).jpgAdded
- confusion_matrix_examples/FP/Stable Diffusion_125 (8).jpgAdded
- confusion_matrix_examples/TN/Stable Diffusion_0 (10).jpgAdded
- confusion_matrix_examples/TN/Stable Diffusion_0 (2).jpgAdded
- confusion_matrix_examples/TN/Stable Diffusion_0 (3).jpgAdded
- confusion_matrix_examples/TN/Stable Diffusion_0 (4).jpgAdded
- confusion_matrix_examples/TN/Stable Diffusion_0 (5).jpgAdded
- confusion_matrix_examples/TN/Stable Diffusion_0 (6).jpgAdded
- confusion_matrix_examples/TN/Stable Diffusion_0 (7).jpgAdded
- confusion_matrix_examples/TN/Stable Diffusion_0 (8).jpgAdded
- confusion_matrix_examples/TN/Stable Diffusion_0 (9).jpgAdded
- confusion_matrix_examples/TN/Stable Diffusion_0.jpgAdded
- confusion_matrix_examples/TP/Stable Diffusion_0000 (10).jpgAdded
- confusion_matrix_examples/TP/Stable Diffusion_0000 (2).jpgAdded
- confusion_matrix_examples/TP/Stable Diffusion_0000 (3).jpgAdded
- confusion_matrix_examples/TP/Stable Diffusion_0000 (4).jpgAdded
- confusion_matrix_examples/TP/Stable Diffusion_0000 (5).jpgAdded
- confusion_matrix_examples/TP/Stable Diffusion_0000 (6).jpgAdded
- confusion_matrix_examples/TP/Stable Diffusion_0000 (7).jpgAdded
- confusion_matrix_examples/TP/Stable Diffusion_0000 (8).jpgAdded
- confusion_matrix_examples/TP/Stable Diffusion_0000 (9).jpgAdded
- confusion_matrix_examples/TP/Stable Diffusion_0000.jpgAdded
- confusion_matrix_examples/collection_summary.txtAdded+19
- images/FN.jpgAdded
- images/FP.jpgAdded
- images/TN.jpgAdded
- images/TP.jpgAdded
- requirements.txt+1
24 unmodified lines25262728293024 unmodified lines# Hugging Face sets $PORT; Gunicorn will bind to it.ENV PORT=7860ENV MODEL_PATH=models/alexnext_vsf_bext.pthENV FLASK_DEBUG=0# Single worker (GPU inference), thread worker for simplicity24 unmodified lines252627282930313233343524 unmodified lines# Hugging Face sets $PORT; Gunicorn will bind to it.ENV PORT=7860ENV MODEL_PATH=models/alexnext_vsf_bext.pthENV CONFUSion_PATH=images/TP.jpgENV TP_PATH=images/TP.jpgENV TN_PATH=images/TN.jpgENV FN_PATH=images/FN.jpgENV FP_PATH=images/FP.jpgENV FLASK_DEBUG=0# Single worker (GPU inference), thread worker for simplicity
12345678910112 unmodified lines14151617181920212223244 unmodified lines293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576import osfrom typing import Anyfrom flask import Flask, jsonify, request, send_from_directoryfrom PIL import Imageimport torchimport torch.nn.functional as Ffrom dotenv import load_dotenvfrom model_loader import load_alexnet_model, preprocess_imageload_dotenv(override=True)2 unmodified linesHOST = "0.0.0.0"MODEL_PATH = os.getenv("MODEL_PATH", "models/alexnext_vsf_bext.pth")# Single worker is safest for GPU inferencetorch.set_num_threads(1)# Create app and static hostingapp = Flask(__name__, static_folder="static", static_url_path="")# Device selectionDEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")4 unmodified lines@app.get("/")def root() -> Any:# serve your frontendreturn send_from_directory(app.static_folder, "index.html")@app.get("/health")def health() -> Any:return jsonify({"status": "ok", "device": str(DEVICE)})def load_image(file_stream):return Image.open(file_stream).convert("RGB")@app.post("/predict_AlexNet")def predict_alexnet() -> Any:if "image" not in request.files:return jsonify({"error": "Missing file field 'image'."}), 400file = request.files["image"]if not file:return jsonify({"error": "Empty file."}), 400try:img = load_image(file.stream)input_tensor = preprocess_image(img).to(DEVICE)with torch.no_grad():output = model(input_tensor)probabilities = F.softmax(output[0], dim=0).detach().cpu()pred_prob, pred_idx = torch.max(probabilities, dim=0)predicted_class = classes[int(pred_idx)]result = {"class": predicted_class,"confidence": float(pred_prob),"probabilities": {cls: float(prob) for cls, prob in zip(classes, probabilities.tolist())},}return jsonify(result)except Exception as e:return jsonify({"error": f"Failed to process image: {e}"}), 400if __name__ == "__main__":debug = bool(int(os.getenv("FLASK_DEBUG", "0")))app.run(host=HOST, port=PORT, debug=debug)1234567891011122 unmodified lines151617181920212223242526272829303132333435363738394 unmodified lines4445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127import osfrom typing import Any, Dictfrom flask import Flask, jsonify, request, send_from_directory, abortfrom PIL import Imageimport torchimport torch.nn.functional as Ffrom dotenv import load_dotenvfrom model_loader import load_alexnet_model, preprocess_imagefrom flask_cors import CORSload_dotenv(override=True)2 unmodified linesHOST = "0.0.0.0"MODEL_PATH = os.getenv("MODEL_PATH", "models/alexnext_vsf_bext.pth")# Preset image paths via ENVTP_PATH = os.getenv("TP_PATH", "images/TP.jpg")TN_PATH = os.getenv("TN_PATH", "images/TN.jpg")FN_PATH = os.getenv("FN_PATH", "images/FN.jpg")FP_PATH = os.getenv("FP_PATH", "images/FP.jpg")PRESET_MAP: Dict[str, str] = {"TP": TP_PATH,"TN": TN_PATH,"FN": FN_PATH,"FP": FP_PATH,}# Single worker is safest for GPU inferencetorch.set_num_threads(1)# Create app and static hostingapp = Flask(__name__, static_folder="static", static_url_path="")CORS(app)# Device selectionDEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")4 unmodified lines@app.get("/")def root() -> Any:return send_from_directory(app.static_folder, "index.html")@app.get("/health")def health() -> Any:return jsonify({"status": "ok", "device": str(DEVICE)})def load_image(file_stream_or_path):if isinstance(file_stream_or_path, str):return Image.open(file_stream_or_path).convert("RGB")return Image.open(file_stream_or_path).convert("RGB")def run_inference(img: Image.Image) -> Dict[str, Any]:input_tensor = preprocess_image(img).to(DEVICE)with torch.no_grad():output = model(input_tensor)probabilities = F.softmax(output[0], dim=0).detach().cpu()pred_prob, pred_idx = torch.max(probabilities, dim=0)predicted_class = classes[int(pred_idx)]return {"class": predicted_class,"confidence": float(pred_prob),"probabilities": {cls: float(prob) for cls, prob in zip(classes, probabilities.tolist())},}# --- Existing upload classification ---@app.post("/predict_AlexNet")def predict_alexnet() -> Any:if "image" not in request.files:return jsonify({"error": "Missing file field 'image'."}), 400file = request.files["image"]if not file:return jsonify({"error": "Empty file."}), 400try:img = load_image(file.stream)result = run_inference(img)return jsonify(result)except Exception as e:return jsonify({"error": f"Failed to process image: {e}"}), 400# --- NEW: classify a preset image ---@app.post("/predict_preset")def predict_preset() -> Any:try:payload = request.get_json(force=True, silent=False)except Exception:payload = Noneif not payload or "preset" not in payload:return jsonify({"error": "Missing JSON field 'preset' (TP|TN|FN|FP)."}), 400key = str(payload["preset"]).upper()if key not in PRESET_MAP:return jsonify({"error": f"Invalid preset '{key}'. Use one of: TP, TN, FN, FP."}), 400path = PRESET_MAP[key]if not os.path.exists(path):return jsonify({"error": f"Preset image not found on server: {path}"}), 404try:img = load_image(path)result = run_inference(img)result.update({"preset": key, "path": path})return jsonify(result)except Exception as e:return jsonify({"error": f"Failed to process preset image: {e}"}), 400# --- NEW: serve preset thumbnails safely ---@app.get("/preset_image/<label>")def preset_image(label: str):key = str(label).upper()if key not in PRESET_MAP:abort(404)path = PRESET_MAP[key]if not os.path.exists(path):abort(404)directory, filename = os.path.split(os.path.abspath(path))# Let Flask serve the actual file bytesreturn send_from_directory(directory, filename)if __name__ == "__main__":debug = bool(int(os.getenv("FLASK_DEBUG", "0")))app.run(host=HOST, port=PORT, debug=debug)
123version https://git-lfs.github.com/spec/v1oid sha256:75665f0fda9ce346e3d4ef1a4ed3d774b4a2581394ae784829d27be1df1cb7b6size 4236912345678910111213141516171819Confusion Matrix Example Collection Summary======================================================================Model: /work/cssema416/202610/22/Gurinder/alexnet_vsf_best.pth
Datasets Searched: Stable Diffusion: - /work/cssema416/202610/22/test - /work/cssema416/202610/22/train Midjourney: - /work/cssema416/202610/22/Midjourney_Exp2/test - /work/cssema416/202610/22/Midjourney_Exp2/train DALLE: - /work/cssema416/202610/22/dalle
Results:True Positives (TP): 10/10True Negatives (TN): 10/10False Positives (FP): 10/10False Negatives (FN): 10/101 unmodified line2341 unmodified linepillow>=10.0.0gunicorn>=21.2.0python-dotenv>=1.0.01 unmodified line23451 unmodified linepillow>=10.0.0gunicorn>=21.2.0python-dotenv>=1.0.0Flask-Cors>=4.0.0
Dockerfile+5
__pycache__/model_loader.cpython-39.pycAdded
app.py+76-25
confusion_matrix_examples.zipAdded+3
confusion_matrix_examples/FN/Stable Diffusion_0001 (6).jpgAdded
.jpg)
confusion_matrix_examples/FN/Stable Diffusion_0003 (3).jpgAdded
.jpg)
confusion_matrix_examples/FN/Stable Diffusion_0004 (2).jpgAdded
.jpg)
confusion_matrix_examples/FN/Stable Diffusion_0013 (10).jpgAdded
.jpg)
confusion_matrix_examples/FN/Stable Diffusion_0013.jpgAdded

confusion_matrix_examples/FN/Stable Diffusion_0017 (10).jpgAdded
.jpg)
confusion_matrix_examples/FN/Stable Diffusion_0019 (10).jpgAdded
.jpg)
confusion_matrix_examples/FN/Stable Diffusion_0019 (9).jpgAdded
.jpg)
confusion_matrix_examples/FN/Stable Diffusion_0027 (7).jpgAdded
.jpg)
confusion_matrix_examples/FN/Stable Diffusion_0030.jpgAdded

confusion_matrix_examples/FP/Stable Diffusion_101 (3).jpgAdded
.jpg)
confusion_matrix_examples/FP/Stable Diffusion_102 (10).jpgAdded
.jpg)
confusion_matrix_examples/FP/Stable Diffusion_102 (9).jpgAdded
.jpg)
confusion_matrix_examples/FP/Stable Diffusion_112 (3).jpgAdded
.jpg)
confusion_matrix_examples/FP/Stable Diffusion_115 (4).jpgAdded
.jpg)
confusion_matrix_examples/FP/Stable Diffusion_117 (3).jpgAdded
.jpg)
confusion_matrix_examples/FP/Stable Diffusion_117 (9).jpgAdded
.jpg)
confusion_matrix_examples/FP/Stable Diffusion_118 (4).jpgAdded
.jpg)
confusion_matrix_examples/FP/Stable Diffusion_124 (3).jpgAdded
.jpg)
confusion_matrix_examples/FP/Stable Diffusion_125 (8).jpgAdded
.jpg)
confusion_matrix_examples/TN/Stable Diffusion_0 (10).jpgAdded
.jpg)
confusion_matrix_examples/TN/Stable Diffusion_0 (2).jpgAdded
.jpg)
confusion_matrix_examples/TN/Stable Diffusion_0 (3).jpgAdded
.jpg)
confusion_matrix_examples/TN/Stable Diffusion_0 (4).jpgAdded
.jpg)
confusion_matrix_examples/TN/Stable Diffusion_0 (5).jpgAdded
.jpg)
confusion_matrix_examples/TN/Stable Diffusion_0 (6).jpgAdded
.jpg)
confusion_matrix_examples/TN/Stable Diffusion_0 (7).jpgAdded
.jpg)
confusion_matrix_examples/TN/Stable Diffusion_0 (8).jpgAdded
.jpg)
confusion_matrix_examples/TN/Stable Diffusion_0 (9).jpgAdded
.jpg)
confusion_matrix_examples/TN/Stable Diffusion_0.jpgAdded

confusion_matrix_examples/TP/Stable Diffusion_0000 (10).jpgAdded
.jpg)
confusion_matrix_examples/TP/Stable Diffusion_0000 (2).jpgAdded
.jpg)
confusion_matrix_examples/TP/Stable Diffusion_0000 (3).jpgAdded
.jpg)
confusion_matrix_examples/TP/Stable Diffusion_0000 (4).jpgAdded
.jpg)
confusion_matrix_examples/TP/Stable Diffusion_0000 (5).jpgAdded
.jpg)
confusion_matrix_examples/TP/Stable Diffusion_0000 (6).jpgAdded
.jpg)
confusion_matrix_examples/TP/Stable Diffusion_0000 (7).jpgAdded
.jpg)
confusion_matrix_examples/TP/Stable Diffusion_0000 (8).jpgAdded
.jpg)
confusion_matrix_examples/TP/Stable Diffusion_0000 (9).jpgAdded
.jpg)
confusion_matrix_examples/TP/Stable Diffusion_0000.jpgAdded

confusion_matrix_examples/collection_summary.txtAdded+19
images/FN.jpgAdded

images/FP.jpgAdded

images/TN.jpgAdded

images/TP.jpgAdded

requirements.txt+1