Commit 3bcaac39 authored by Warnasooriya M.D.S.'s avatar Warnasooriya M.D.S.

Model 01 Developed

parent 940b3a2e
......@@ -125,6 +125,131 @@
"plt.subplot(1 , 2, 2)\n",
"img = view_random_image(target_dir=\"Damage Data/Traning\" , target_class=\"Mealybug\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"Model - 01"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"🟢 Pre-process Data"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n",
"train_dir = \"Damage Data/Traning\"\n",
"test_dir = \"Damage Data/Testing\"\n",
"\n",
"#Normalize the data \n",
"train_datagen = ImageDataGenerator(rescale = 1/255.,\n",
" rotation_range = 0.2, #how much do you want to rotate an image\n",
" shear_range = 0.3,\n",
" zoom_range = 0.3, \n",
" width_shift_range=0.25,\n",
" height_shift_range=0.25,\n",
" horizontal_flip=True) \n",
"test_datagen = ImageDataGenerator(rescale = 1/255.)\n",
"#Loading the images from the directories\n",
"train_data = train_datagen.flow_from_directory(directory=train_dir,\n",
" target_size = (448, 448),\n",
" class_mode='categorical',\n",
" batch_size = 16,\n",
" shuffle=True)\n",
"\n",
"test_data = test_datagen.flow_from_directory(directory=test_dir,\n",
" target_size = (448, 448),\n",
" batch_size = 16,\n",
" class_mode = \"categorical\",\n",
" shuffle=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"height = 448\n",
"width = 448\n",
"depth = 3\n",
"n_classes = 3\n",
"\n",
"\n",
"#Create the model\n",
"model = Sequential()\n",
"inputShape = (height, width, depth)\n",
"chanDim = -1\n",
"if K.image_data_format() == \"channels_first\":\n",
" inputShape = (depth, height, width)\n",
" chanDim = 1\n",
"model.add(Conv2D(32, (3, 3), padding=\"same\",input_shape=inputShape))\n",
"model.add(Activation(\"relu\"))\n",
"model.add(BatchNormalization(axis=chanDim))\n",
"model.add(MaxPooling2D(pool_size=(3, 3)))\n",
"model.add(Dropout(0.25))\n",
"model.add(Conv2D(64, (3, 3), padding=\"same\"))\n",
"model.add(Activation(\"relu\"))\n",
"model.add(BatchNormalization(axis=chanDim))\n",
"model.add(Conv2D(64, (3, 3), padding=\"same\"))\n",
"model.add(Activation(\"relu\"))\n",
"model.add(BatchNormalization(axis=chanDim))\n",
"model.add(MaxPooling2D(pool_size=(2, 2)))\n",
"model.add(Dropout(0.25))\n",
"model.add(Conv2D(128, (3, 3), padding=\"same\"))\n",
"model.add(Activation(\"relu\"))\n",
"model.add(BatchNormalization(axis=chanDim))\n",
"model.add(Conv2D(128, (3, 3), padding=\"same\"))\n",
"model.add(Activation(\"relu\"))\n",
"model.add(BatchNormalization(axis=chanDim))\n",
"model.add(MaxPooling2D(pool_size=(2, 2)))\n",
"model.add(Dropout(0.25))\n",
"model.add(Flatten())\n",
"model.add(Dense(1024))\n",
"model.add(Activation(\"relu\"))\n",
"model.add(BatchNormalization())\n",
"model.add(Dropout(0.5))\n",
"model.add(Dense(n_classes))\n",
"model.add(Activation(\"softmax\"))\n",
"\n",
"#Compile the model\n",
"model.compile(loss = tf.keras.losses.CategoricalCrossentropy(),\n",
" optimizer = tf.keras.optimizers.Adam(),\n",
" metrics = ['accuracy'])\n",
"fle_s = '/content/saved_models/pest_damages.h5'\n",
"checkpointer = ModelCheckpoint(fle_s, monitor='loss', verbose=1, save_best_only=True,\n",
" save_weights_only=False, mode='auto', save_freq='epoch')\n",
"callback_list = [checkpointer]\n",
"\n",
"model_history = model.fit(train_data,\n",
" epochs=10,\n",
" batch_size=16,\n",
" steps_per_epoch=len(train_data),\n",
" validation_data = test_data,\n",
" validation_steps = len(test_data),\n",
" callbacks=[callback_list]\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
......
......@@ -125,6 +125,131 @@
"plt.subplot(1 , 2, 2)\n",
"img = view_random_image(target_dir=\"Damage Data/Traning\" , target_class=\"Mealybug\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"Model - 01"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"🟢 Pre-process Data"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n",
"train_dir = \"Damage Data/Traning\"\n",
"test_dir = \"Damage Data/Testing\"\n",
"\n",
"#Normalize the data \n",
"train_datagen = ImageDataGenerator(rescale = 1/255.,\n",
" rotation_range = 0.2, #how much do you want to rotate an image\n",
" shear_range = 0.3,\n",
" zoom_range = 0.3, \n",
" width_shift_range=0.25,\n",
" height_shift_range=0.25,\n",
" horizontal_flip=True) \n",
"test_datagen = ImageDataGenerator(rescale = 1/255.)\n",
"#Loading the images from the directories\n",
"train_data = train_datagen.flow_from_directory(directory=train_dir,\n",
" target_size = (448, 448),\n",
" class_mode='categorical',\n",
" batch_size = 16,\n",
" shuffle=True)\n",
"\n",
"test_data = test_datagen.flow_from_directory(directory=test_dir,\n",
" target_size = (448, 448),\n",
" batch_size = 16,\n",
" class_mode = \"categorical\",\n",
" shuffle=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"height = 448\n",
"width = 448\n",
"depth = 3\n",
"n_classes = 3\n",
"\n",
"\n",
"#Create the model\n",
"model = Sequential()\n",
"inputShape = (height, width, depth)\n",
"chanDim = -1\n",
"if K.image_data_format() == \"channels_first\":\n",
" inputShape = (depth, height, width)\n",
" chanDim = 1\n",
"model.add(Conv2D(32, (3, 3), padding=\"same\",input_shape=inputShape))\n",
"model.add(Activation(\"relu\"))\n",
"model.add(BatchNormalization(axis=chanDim))\n",
"model.add(MaxPooling2D(pool_size=(3, 3)))\n",
"model.add(Dropout(0.25))\n",
"model.add(Conv2D(64, (3, 3), padding=\"same\"))\n",
"model.add(Activation(\"relu\"))\n",
"model.add(BatchNormalization(axis=chanDim))\n",
"model.add(Conv2D(64, (3, 3), padding=\"same\"))\n",
"model.add(Activation(\"relu\"))\n",
"model.add(BatchNormalization(axis=chanDim))\n",
"model.add(MaxPooling2D(pool_size=(2, 2)))\n",
"model.add(Dropout(0.25))\n",
"model.add(Conv2D(128, (3, 3), padding=\"same\"))\n",
"model.add(Activation(\"relu\"))\n",
"model.add(BatchNormalization(axis=chanDim))\n",
"model.add(Conv2D(128, (3, 3), padding=\"same\"))\n",
"model.add(Activation(\"relu\"))\n",
"model.add(BatchNormalization(axis=chanDim))\n",
"model.add(MaxPooling2D(pool_size=(2, 2)))\n",
"model.add(Dropout(0.25))\n",
"model.add(Flatten())\n",
"model.add(Dense(1024))\n",
"model.add(Activation(\"relu\"))\n",
"model.add(BatchNormalization())\n",
"model.add(Dropout(0.5))\n",
"model.add(Dense(n_classes))\n",
"model.add(Activation(\"softmax\"))\n",
"\n",
"#Compile the model\n",
"model.compile(loss = tf.keras.losses.CategoricalCrossentropy(),\n",
" optimizer = tf.keras.optimizers.Adam(),\n",
" metrics = ['accuracy'])\n",
"fle_s = '/content/saved_models/pest_damages.h5'\n",
"checkpointer = ModelCheckpoint(fle_s, monitor='loss', verbose=1, save_best_only=True,\n",
" save_weights_only=False, mode='auto', save_freq='epoch')\n",
"callback_list = [checkpointer]\n",
"\n",
"model_history = model.fit(train_data,\n",
" epochs=10,\n",
" batch_size=16,\n",
" steps_per_epoch=len(train_data),\n",
" validation_data = test_data,\n",
" validation_steps = len(test_data),\n",
" callbacks=[callback_list]\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
......
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