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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": null,
|
| 6 |
+
"metadata": {},
|
| 7 |
+
"outputs": [],
|
| 8 |
+
"source": [
|
| 9 |
+
"Id=[]\n",
|
| 10 |
+
"import numpy as np # linear algebra\n",
|
| 11 |
+
"import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n",
|
| 12 |
+
"import tensorflow as tf\n",
|
| 13 |
+
"import matplotlib.pyplot as plt\n",
|
| 14 |
+
"from PIL import Image\n",
|
| 15 |
+
"import os\n",
|
| 16 |
+
"IMAGE_SIZE=256\n",
|
| 17 |
+
"BATCH_SIZE=32\n",
|
| 18 |
+
"CHANNELS=3\n",
|
| 19 |
+
"PATH_NAME='../../../poultry/test_data/img'\n",
|
| 20 |
+
"classes = [ \"Coccidiosis\" , \"Healthy poultry\" , \"New castle diseases\" , \"Salmonella\" , ]\n",
|
| 21 |
+
"\n",
|
| 22 |
+
"def load_test(path_name):\n",
|
| 23 |
+
" \n",
|
| 24 |
+
" for dirname, _, filenames in os.walk(path_name):\n",
|
| 25 |
+
" for filename in filenames:\n",
|
| 26 |
+
" Id.append(os.path.join(dirname, filename))\n",
|
| 27 |
+
" Id[:5]\n",
|
| 28 |
+
" \n",
|
| 29 |
+
" test=pd.DataFrame()\n",
|
| 30 |
+
" test=test.assign(filename=Id)\n",
|
| 31 |
+
" test.head()\n",
|
| 32 |
+
" return test\n",
|
| 33 |
+
"def predict_a(path_name='../../../poultry/test_data/img'):\n",
|
| 34 |
+
" PATH_NAME=path_name\n",
|
| 35 |
+
" test = load_test(PATH_NAME)\n",
|
| 36 |
+
"\n",
|
| 37 |
+
" model = tf.saved_model.load('../EfficientNet-model')\n",
|
| 38 |
+
"\n",
|
| 39 |
+
" \n",
|
| 40 |
+
" # variables to use\n",
|
| 41 |
+
" result = []\n",
|
| 42 |
+
" for i in test.filename:\n",
|
| 43 |
+
" img = Image.open(i).convert('RGB')\n",
|
| 44 |
+
" # Updated to use LANCZOS for resizing\n",
|
| 45 |
+
" img = img.resize((300, 300 * img.size[1] // img.size[0]), Image.Resampling.LANCZOS)\n",
|
| 46 |
+
" inp_numpy = np.array(img)[None]\n",
|
| 47 |
+
" inp = tf.constant(inp_numpy, dtype='float32')\n",
|
| 48 |
+
" class_scores = model(inp)[0].numpy()\n",
|
| 49 |
+
" result.append(classes[class_scores.argmax()])\n",
|
| 50 |
+
" print(result[:5])\n"
|
| 51 |
+
]
|
| 52 |
+
},
|
| 53 |
+
{
|
| 54 |
+
"cell_type": "code",
|
| 55 |
+
"execution_count": null,
|
| 56 |
+
"metadata": {},
|
| 57 |
+
"outputs": [],
|
| 58 |
+
"source": [
|
| 59 |
+
"import matplotlib.pyplot as plt\n",
|
| 60 |
+
"from PIL import Image\n",
|
| 61 |
+
"import numpy as np\n",
|
| 62 |
+
"import tensorflow as tf\n",
|
| 63 |
+
"\n",
|
| 64 |
+
"def predict(path_name):\n",
|
| 65 |
+
" PATH_NAME=path_name\n",
|
| 66 |
+
" # Assuming model, classes, and test are already defined\n",
|
| 67 |
+
" result = []\n",
|
| 68 |
+
" test= load_test(path_name)\n",
|
| 69 |
+
" # Number of rows and columns in the grid\n",
|
| 70 |
+
" num_cols = 3\n",
|
| 71 |
+
" num_rows = (len(test.filename) + num_cols - 1) // num_cols\n",
|
| 72 |
+
"\n",
|
| 73 |
+
" # Create a figure with the appropriate size\n",
|
| 74 |
+
" plt.figure(figsize=(15, 5 * num_rows))\n",
|
| 75 |
+
"\n",
|
| 76 |
+
" for index, i in enumerate(test.filename):\n",
|
| 77 |
+
" img = Image.open(i).convert('RGB')\n",
|
| 78 |
+
" img = img.resize((300, 300 * img.size[1] // img.size[0]), Image.Resampling.LANCZOS)\n",
|
| 79 |
+
" inp_numpy = np.array(img)[None]\n",
|
| 80 |
+
" inp = tf.constant(inp_numpy, dtype='float32')\n",
|
| 81 |
+
" \n",
|
| 82 |
+
" # Get model predictions\n",
|
| 83 |
+
" model = tf.saved_model.load('../EfficientNet-model')\n",
|
| 84 |
+
" class_scores = model(inp)[0].numpy()\n",
|
| 85 |
+
" predicted_class_index = class_scores.argmax()\n",
|
| 86 |
+
" classes = [ \"Coccidiosis\" , \"Healthy poultry\" , \"New castle diseases\" , \"Salmonella\" , ]\n",
|
| 87 |
+
" predicted_class = classes[predicted_class_index]\n",
|
| 88 |
+
" confidence = class_scores[predicted_class_index]\n",
|
| 89 |
+
"\n",
|
| 90 |
+
" # Append result\n",
|
| 91 |
+
" result.append(predicted_class)\n",
|
| 92 |
+
" \n",
|
| 93 |
+
" # Plotting the image in the grid\n",
|
| 94 |
+
" plt.subplot(num_rows, num_cols, index + 1)\n",
|
| 95 |
+
" plt.imshow(img)\n",
|
| 96 |
+
" plt.title(f\"{predicted_class}\\nConfidence: {confidence:.2f}\\nFilename: {i}\")\n",
|
| 97 |
+
" plt.axis('off')\n",
|
| 98 |
+
"\n",
|
| 99 |
+
" # Show the grid\n",
|
| 100 |
+
" plt.tight_layout()\n",
|
| 101 |
+
" plt.show()\n"
|
| 102 |
+
]
|
| 103 |
+
},
|
| 104 |
+
{
|
| 105 |
+
"cell_type": "code",
|
| 106 |
+
"execution_count": null,
|
| 107 |
+
"metadata": {},
|
| 108 |
+
"outputs": [],
|
| 109 |
+
"source": [
|
| 110 |
+
"import matplotlib.pyplot as plt\n",
|
| 111 |
+
"from PIL import Image\n",
|
| 112 |
+
"import numpy as np\n",
|
| 113 |
+
"import tensorflow as tf\n",
|
| 114 |
+
"def predict_c(path_name):\n",
|
| 115 |
+
"# Assuming model, classes, and test are already defined\n",
|
| 116 |
+
" result = []\n",
|
| 117 |
+
"\n",
|
| 118 |
+
" # Dictionary to store the results: {filename: (predicted_class, confidence_score)}\n",
|
| 119 |
+
" prediction_dict = {}\n",
|
| 120 |
+
" test = load_test(path_name)\n",
|
| 121 |
+
" for filename in test.filename:\n",
|
| 122 |
+
" img = Image.open(filename).convert('RGB')\n",
|
| 123 |
+
" img = img.resize((300, 300 * img.size[1] // img.size[0]), Image.Resampling.LANCZOS)\n",
|
| 124 |
+
" inp_numpy = np.array(img)[None]\n",
|
| 125 |
+
" inp = tf.constant(inp_numpy, dtype='float32')\n",
|
| 126 |
+
" \n",
|
| 127 |
+
" # Get model predictions\n",
|
| 128 |
+
" model = tf.saved_model.load('../EfficientNet-model')\n",
|
| 129 |
+
" class_scores = model(inp)[0].numpy()\n",
|
| 130 |
+
" predicted_class_index = class_scores.argmax()\n",
|
| 131 |
+
" predicted_class = classes[predicted_class_index]\n",
|
| 132 |
+
" confidence = class_scores[predicted_class_index]\n",
|
| 133 |
+
"\n",
|
| 134 |
+
" # Store result in the dictionary\n",
|
| 135 |
+
" prediction_dict[filename] = (predicted_class, confidence)\n",
|
| 136 |
+
"\n",
|
| 137 |
+
" # Number of rows and columns in the grid\n",
|
| 138 |
+
" num_cols = 3\n",
|
| 139 |
+
" num_rows = (len(test.filename) + num_cols - 1) // num_cols\n",
|
| 140 |
+
"\n",
|
| 141 |
+
" # Create a figure with the appropriate size\n",
|
| 142 |
+
" plt.figure(figsize=(15, 5 * num_rows))\n",
|
| 143 |
+
"\n",
|
| 144 |
+
" for index, filename in enumerate(test.filename):\n",
|
| 145 |
+
" img = Image.open(filename).convert('RGB')\n",
|
| 146 |
+
" # Retrieve the prediction and confidence for the current filename\n",
|
| 147 |
+
" predicted_class, confidence = prediction_dict[filename]\n",
|
| 148 |
+
" \n",
|
| 149 |
+
" # Plotting the image in the grid\n",
|
| 150 |
+
" plt.subplot(num_rows, num_cols, index + 1)\n",
|
| 151 |
+
" plt.imshow(img)\n",
|
| 152 |
+
" plt.title(f\"{predicted_class}\\nConfidence: {confidence:.2f}\\nFilename: {filename}\")\n",
|
| 153 |
+
" plt.axis('off')\n",
|
| 154 |
+
"\n",
|
| 155 |
+
" # Show the grid\n",
|
| 156 |
+
" plt.tight_layout()\n",
|
| 157 |
+
" plt.show()\n"
|
| 158 |
+
]
|
| 159 |
+
}
|
| 160 |
+
],
|
| 161 |
+
"metadata": {
|
| 162 |
+
"kernelspec": {
|
| 163 |
+
"display_name": "env",
|
| 164 |
+
"language": "python",
|
| 165 |
+
"name": "env"
|
| 166 |
+
},
|
| 167 |
+
"language_info": {
|
| 168 |
+
"codemirror_mode": {
|
| 169 |
+
"name": "ipython",
|
| 170 |
+
"version": 3
|
| 171 |
+
},
|
| 172 |
+
"file_extension": ".py",
|
| 173 |
+
"mimetype": "text/x-python",
|
| 174 |
+
"name": "python",
|
| 175 |
+
"nbconvert_exporter": "python",
|
| 176 |
+
"pygments_lexer": "ipython3",
|
| 177 |
+
"version": "3.12.2"
|
| 178 |
+
}
|
| 179 |
+
},
|
| 180 |
+
"nbformat": 4,
|
| 181 |
+
"nbformat_minor": 2
|
| 182 |
+
}
|