Teknik-Informatika-PSDKU-Ng.../backend/predict.ipynb

201 lines
5.1 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "27158e1e-4c4b-4f2d-ac70-8cc8d5bec95c",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import numpy as np\n",
"import tensorflow as tf\n",
"\n",
"from tensorflow.keras.preprocessing import image\n",
"from tensorflow.keras.models import load_model\n",
"from tensorflow.keras.applications.mobilenet_v2 import preprocess_input"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "bad0a63e-ce7b-48c3-903a-4f82d8d4d867",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Model berhasil dimuat\n"
]
}
],
"source": [
"# ==========================================\n",
"# PATH\n",
"# ==========================================\n",
"BASE_DIR = os.getcwd()\n",
"\n",
"MODEL1_PATH = os.path.join(BASE_DIR, \"model_1\", \"model1_best.h5\")\n",
"MODEL2_PATH = os.path.join(BASE_DIR, \"model_2\", \"model2_best.h5\")\n",
"\n",
"# ==========================================\n",
"# LOAD MODEL\n",
"# ==========================================\n",
"model1 = load_model(MODEL1_PATH, compile=False)\n",
"model2 = load_model(MODEL2_PATH, compile=False)\n",
"\n",
"print(\"Model berhasil dimuat\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "50be8401-d008-42f9-9faf-bdac0813fdff",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Label Model 1: ['jagung', 'non_jagung']\n",
"Label Model 2: ['hawar_daun', 'karat_daun', 'sehat']\n"
]
}
],
"source": [
"# ⚠️ CEK train_gen.class_indices SAAT TRAINING!\n",
"\n",
"LABELS_MODEL1 = [\"jagung\", \"non_jagung\"]\n",
"LABELS_MODEL2 = [\"hawar_daun\", \"karat_daun\", \"sehat\"]\n",
"\n",
"print(\"Label Model 1:\", LABELS_MODEL1)\n",
"print(\"Label Model 2:\", LABELS_MODEL2)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "16772b66-a860-4497-8b42-7300773d2931",
"metadata": {},
"outputs": [],
"source": [
"def preprocess_img(img_path):\n",
" img = image.load_img(img_path, target_size=(224,224))\n",
" img = image.img_to_array(img)\n",
" img = np.expand_dims(img, axis=0)\n",
" img = preprocess_input(img)\n",
" return img"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "66497688-5c30-4e64-9f20-46253d1d5e78",
"metadata": {},
"outputs": [],
"source": [
"def predict_image(img_path, threshold=0.6):\n",
"\n",
" if not os.path.exists(img_path):\n",
" raise FileNotFoundError(\"File tidak ditemukan\")\n",
"\n",
" img = preprocess_img(img_path)\n",
"\n",
" # =====================\n",
" # MODEL 1\n",
" # =====================\n",
" preds1 = model1.predict(img, verbose=0)[0]\n",
"\n",
" idx1 = np.argmax(preds1)\n",
" conf1 = preds1[idx1]\n",
" label1 = LABELS_MODEL1[idx1]\n",
"\n",
" print(\"\\nMODEL 1:\", label1, conf1)\n",
"\n",
" if conf1 < threshold:\n",
" return {\"status\": \"tidak yakin\"}\n",
"\n",
" if label1 != \"jagung\":\n",
" return {\"status\": \"bukan jagung\"}\n",
"\n",
" # =====================\n",
" # MODEL 2\n",
" # =====================\n",
" preds2 = model2.predict(img, verbose=0)[0]\n",
"\n",
" idx2 = np.argmax(preds2)\n",
" conf2 = preds2[idx2]\n",
" label2 = LABELS_MODEL2[idx2]\n",
"\n",
" print(\"\\nMODEL 2:\", label2, conf2)\n",
"\n",
" return {\n",
" \"status\": \"jagung\",\n",
" \"hasil\": label2,\n",
" \"confidence\": float(conf2)\n",
" }"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "0464d10c-68ec-42c5-97dc-fdd5043f4dec",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"MODEL 1: jagung 0.99942905\n",
"\n",
"MODEL 2: hawar_daun 0.9100269\n",
"\n",
"HASIL:\n",
"{'status': 'jagung', 'hasil': 'hawar_daun', 'confidence': 0.9100269079208374}\n"
]
}
],
"source": [
"# Ganti dengan path gambar kamu\n",
"test_image = os.path.join(BASE_DIR, \"test_image3.jpg\")\n",
"\n",
"result = predict_image(test_image)\n",
"\n",
"print(\"\\nHASIL:\")\n",
"print(result)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7d65869a-6c9c-49c8-a4ce-1a37dc0c5264",
"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.10.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}