Commit 86287840 authored by Ekanayake G.B.E.M.K's avatar Ekanayake G.B.E.M.K

Add more models

parent 5fe4f1fd
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
{
"cells": [
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"data_path = 'data/'\n",
"model_weights = 'disease_classification.h5'\n",
"\n",
"batch_size = 32\n",
"valid_size = 16\n",
"color_mode = 'rgb'\n",
"\n",
"width = 299\n",
"height = 299\n",
"\n",
"target_size = (width, height)\n",
"input_shape = (width, height, 3)\n",
"\n",
"zoom_range = 0.3\n",
"shear_range = 0.3\n",
"shift_range = 0.3\n",
"rotation_range = 30\n",
"\n",
"val_split = 0.2\n",
"\n",
"dense_1 = 512\n",
"dense_2 = 256\n",
"dense_3 = 64\n",
"num_classes = 5\n",
"\n",
"epochs = 100\n",
"rate = 0.2\n",
"\n",
"verbose = 1"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# Data Loader"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'\n",
"\n",
"import cv2 as cv\n",
"import numpy as np\n",
"import pandas as pd\n",
"import tensorflow as tf\n",
"from tensorflow.keras.preprocessing.image import img_to_array"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"from tensorflow.keras.activations import relu\n",
"from tensorflow.keras.models import Model, load_model\n",
"from tensorflow.keras.layers import Dense, BatchNormalization, Dropout\n",
"############################################################################################\n",
"\n",
"def preprocessing_function(img):\n",
" img = tf.keras.applications.xception.preprocess_input(img)\n",
" return img\n",
"\n",
"def image_data_generator():\n",
" train_datagen = tf.keras.preprocessing.image.ImageDataGenerator(\n",
" rotation_range = rotation_range,\n",
" shear_range = shear_range,\n",
" zoom_range = zoom_range,\n",
" width_shift_range=shift_range,\n",
" height_shift_range=shift_range,\n",
" horizontal_flip = True,\n",
" validation_split= val_split,\n",
" preprocessing_function=preprocessing_function\n",
" )\n",
" test_datagen = tf.keras.preprocessing.image.ImageDataGenerator(\n",
" preprocessing_function=preprocessing_function\n",
" )\n",
"\n",
" train_generator = train_datagen.flow_from_directory(\n",
" data_path,\n",
" target_size = target_size,\n",
" color_mode = color_mode,\n",
" batch_size = batch_size,\n",
" class_mode = 'categorical',\n",
" subset = 'training',\n",
" shuffle = True\n",
" )\n",
"\n",
" validation_generator = train_datagen.flow_from_directory(\n",
" data_path,\n",
" target_size = target_size,\n",
" color_mode = color_mode,\n",
" batch_size = valid_size,\n",
" class_mode = 'categorical',\n",
" subset = 'validation',\n",
" shuffle = True\n",
" )\n",
"\n",
" return train_generator, validation_generator"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Found 4030 images belonging to 6 classes.\n",
"Found 1004 images belonging to 6 classes.\n"
]
},
{
"data": {
"text/plain": [
"{'Caterpillars': 0,\n",
" 'DryingofLeaflets': 1,\n",
" 'Flaccidity': 2,\n",
" 'Leaflets': 3,\n",
" 'Not_Leaf': 4,\n",
" 'Yellowing leaf': 5}"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"train_generator, validation_generator = image_data_generator()\n",
"train_generator.class_indices"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"class DiseaseClassification(object):\n",
" def __init__(self):\n",
" self.train_generator = train_generator\n",
" self.validation_generator = validation_generator\n",
" self.train_step = self.train_generator.samples // batch_size\n",
" self.validation_step = self.validation_generator.samples // valid_size\n",
"\n",
" self.accuracy = tf.keras.metrics.CategoricalAccuracy()\n",
" self.recall = tf.keras.metrics.Recall()\n",
" self.precision = tf.keras.metrics.Precision()\n",
"\n",
" self.id2disease = {v:k for k, v in self.train_generator.class_indices.items()}\n",
"\n",
" def classifier(self, x):\n",
" if not self.trainable:\n",
" x = Dense(dense_1, activation='relu')(x)\n",
" x = Dense(dense_1)(x)\n",
" x = BatchNormalization()(x)\n",
" x = relu(x)\n",
" x = Dropout(rate)(x)\n",
"\n",
" x = Dense(dense_2, activation='relu')(x)\n",
" x = Dense(dense_2)(x)\n",
" x = BatchNormalization()(x)\n",
" x = relu(x)\n",
" x = Dropout(rate)(x)\n",
"\n",
" x = Dense(dense_3, activation='relu')(x)\n",
" x = Dense(dense_3)(x)\n",
" x = BatchNormalization()(x)\n",
" x = relu(x)\n",
" x = Dropout(rate)(x)\n",
" return x\n",
"\n",
" def model_conversion(self, trainable):\n",
" functional_model = tf.keras.applications.Xception(weights=\"imagenet\")\n",
" functional_model.trainable = trainable\n",
"\n",
" self.trainable = trainable\n",
"\n",
" inputs = functional_model.input\n",
"\n",
" x = functional_model.layers[-2].output\n",
" x = self.classifier(x)\n",
" outputs = Dense(num_classes, activation='softmax')(x)\n",
"\n",
" model = Model(\n",
" inputs=inputs,\n",
" outputs=outputs\n",
" )\n",
" \n",
" self.model = model\n",
" self.model.summary()\n",
"\n",
" def train(self):\n",
" callback = tf.keras.callbacks.EarlyStopping(\n",
" monitor='val_loss', \n",
" patience=5\n",
" )\n",
"\n",
" self.model.compile(\n",
" optimizer='Adam',\n",
" loss='categorical_crossentropy',\n",
" metrics=[\n",
" self.accuracy,\n",
" self.recall,\n",
" self.precision\n",
" ]\n",
" )\n",
" self.model.fit(\n",
" self.train_generator,\n",
" steps_per_epoch= self.train_step,\n",
" validation_data= self.validation_generator,\n",
" validation_steps = self.validation_step,\n",
" epochs=epochs,\n",
" verbose=verbose,\n",
" callbacks=[callback]\n",
" )\n",
"\n",
" def save_model(self):\n",
" self.model.save(model_weights)\n",
"\n",
" def load_model(self):\n",
" self.model = load_model(model_weights)\n",
" self.model.compile(\n",
" optimizer='Adam',\n",
" loss='categorical_crossentropy',\n",
" metrics=[\n",
" self.accuracy,\n",
" self.recall,\n",
" self.precision\n",
" ]\n",
" )\n",
"\n",
" # def predict(self, x):\n",
" # x = preprocessing_function(x)\n",
" # x = np.expand_dims(x, axis=0)\n",
" # P = self.model.predict(x)\n",
" # disease_id = np.argmax(P)\n",
" # disease = self.id2disease[disease_id]\n",
" # return disease\n",
" \n",
" def predict(self, x):\n",
" x = preprocessing_function(x)\n",
" x = np.expand_dims(x, axis=0)\n",
" P = self.model.predict(x)\n",
" disease_id = np.argmax(P)\n",
" disease = self.id2disease[disease_id]\n",
" confidence_score = np.max(P)\n",
" return disease, confidence_score\n",
"\n",
" def process(self):\n",
" if not os.path.exists(model_weights):\n",
" self.model_conversion(False)\n",
" self.train()\n",
" self.save_model()\n",
" \n",
" else:\n",
" self.load_model()"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"model = DiseaseClassification()\n",
"model.process()\n"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"def retrieve_treatments(disease):\n",
" treatment = pd.read_excel('Disease retrieve treatments.xlsx')\n",
" treatment = treatment[treatment['Folder name'] == disease]\n",
"\n",
" DiseaseNameSinhala = treatment['Disease name in sinhala'].values[0]\n",
" DiseaseNameEnglish = treatment['Disease name in English'].values[0]\n",
" TreatmentInSinhala = treatment['Treatment in Sinhala'].values[0]\n",
" TreatmentInEnglish = treatment['Treatment in English'].values[0]\n",
"\n",
" retreval_dict = {}\n",
" retreval_dict['DiseaseNameSinhala'] = DiseaseNameSinhala\n",
" retreval_dict['DiseaseNameEnglish'] = DiseaseNameEnglish\n",
" retreval_dict['TreatmentInSinhala'] = TreatmentInSinhala\n",
" retreval_dict['TreatmentInEnglish'] = TreatmentInEnglish\n",
"\n",
" return retreval_dict"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1/1 [==============================] - 0s 241ms/step\n",
"1/1 [==============================] - 0s 166ms/step\n",
"Leaflets\n",
"{'DiseaseNameSinhala': 'ප්ලෙසිස්පා කුරුමිණියා', 'DiseaseNameEnglish': 'Plesispa beetle', 'TreatmentInSinhala': 'පත්\\u200dරවලට සිදු වූ හානිය දැකගත හැකි වන්නේ ගොබය විවෘත වු පසුය . හානිය හා ජීවී සතුන් දක්නට ලැබේ නම් පහත සඳහන් කෘමිනාශකය ඉසිය යුතුය . කාබොසල්ෆාන් ( මාෂල් 20 එස්.සී ) මි.ලී 3 ක් ජලය ලීටරයක දිය කරන්න . LAT ගොබය ප්\\u200dරදේශය හොඳින් තෙමෙන සේ කෘමිනාශකය ඉසීම අත්\\u200dයාවශ්\\u200dයය . නැවතත් මෙම කෘමි හානිය දක්නට ලැබුනහොත් නිර්දේශයට අනුව සති 03 ට පමණ පසු නැවත වරක් කෘමිනාශක ඉසිය යුතුය .', 'TreatmentInEnglish': 'If the damage and the live stages of the pest are observed, spray one of the following insecticides at the given dosage.\\n\\uf0d8\\tCarbosulfan20 SC 3 milliliter in one Liter of water\\n\\uf0d8\\tChlorpyrifos40 EC 3 milliliterofone Liter of water\\nIt is essential to wet the bud region thoroughly while spraying, to allow contact of the insecticide with the pest. If there is a re-infestation repeat the spraying at an interval of 3 weeks as per the recommendation.'}\n"
]
}
],
"source": [
"#Load h5 model\n",
"anomModel = load_model(\"Anomaly_model.h5\")\n",
"\n",
"# Load images for the model\n",
"img_path = 'data/5.jpg'\n",
"img = cv.imread(img_path)\n",
"img = cv.resize(img, (224,224))\n",
"img = img_to_array(img)\n",
"img = img.astype('float32')/255\n",
"\n",
"#Predict\n",
"pred = anomModel.predict(img[np.newaxis,:,:,:])\n",
"pred_class = np.argmax(pred)\n",
"\n",
"if(pred_class == 0):\n",
" img = cv.imread(img_path)\n",
" img = cv.resize(img, target_size)\n",
" P,conf = model.predict(img)\n",
" print(P)\n",
" print(retrieve_treatments(P))\n",
"else:\n",
" print(\"Invalid Image\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.10.5 64-bit",
"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.4"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "fd15a0266db25af44b6d1d40b652f652a5b83da6f218108c7bbca6f31ae43fc8"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
This source diff could not be displayed because it is too large. You can view the blob instead.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment