ensemble model part one added

parent adf10677
...@@ -460,6 +460,69 @@ ...@@ -460,6 +460,69 @@
"source": [ "source": [
"efficientNet_model_top.save('/content/drive/MyDrive/RP_SmartFarmer/Sandhini Gamage - Weed identification /private/saved_models/Weed_top_view2.h5')" "efficientNet_model_top.save('/content/drive/MyDrive/RP_SmartFarmer/Sandhini Gamage - Weed identification /private/saved_models/Weed_top_view2.h5')"
] ]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"🟩 03 -Combining Two Models - Ensemble Approach"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#Getting a copy of the saved model to colab\n",
"import shutil \n",
"\n",
"shutil.copy('/content/drive/MyDrive/RP_SmartFarmer/Sandhini Gamage - Weed identification /private/saved_models/Weed_top_view2.h5' , '/content')\n",
"shutil.copy('/content/drive/MyDrive/RP_SmartFarmer/Sandhini Gamage - Weed identification /private/saved_models/Weed_side_view2.h5', '/content')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def load_all_models():\n",
" all_models = []\n",
" model_names = ['Weed_top_view2.h5', 'Weed_side_view2.h5']\n",
" for model_name in model_names:\n",
" filename = os.path.join('/content', model_name)\n",
" model = tf.keras.models.load_model(filename , custom_objects={'KerasLayer':hub.KerasLayer})\n",
" all_models.append(model)\n",
" print('loaded:', filename)\n",
" return all_models"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"models = load_all_models()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from tensorflow.keras.models import Model , load_model\n",
"from tensorflow.keras.layers import Input, Average\n",
"\n",
"model_input = Input(shape = (224, 224, 3))\n",
"model_outputs = [model(model_input) for model in models]\n",
"ensemble_output = Average()(model_outputs)\n",
"ensemble_model = Model(inputs = model_input , outputs = ensemble_output , name = 'ensemble')"
]
} }
], ],
"metadata": { "metadata": {
......
...@@ -460,6 +460,69 @@ ...@@ -460,6 +460,69 @@
"source": [ "source": [
"efficientNet_model_top.save('/content/drive/MyDrive/RP_SmartFarmer/Sandhini Gamage - Weed identification /private/saved_models/Weed_top_view2.h5')" "efficientNet_model_top.save('/content/drive/MyDrive/RP_SmartFarmer/Sandhini Gamage - Weed identification /private/saved_models/Weed_top_view2.h5')"
] ]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"🟩 03 -Combining Two Models - Ensemble Approach"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#Getting a copy of the saved model to colab\n",
"import shutil \n",
"\n",
"shutil.copy('/content/drive/MyDrive/RP_SmartFarmer/Sandhini Gamage - Weed identification /private/saved_models/Weed_top_view2.h5' , '/content')\n",
"shutil.copy('/content/drive/MyDrive/RP_SmartFarmer/Sandhini Gamage - Weed identification /private/saved_models/Weed_side_view2.h5', '/content')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def load_all_models():\n",
" all_models = []\n",
" model_names = ['Weed_top_view2.h5', 'Weed_side_view2.h5']\n",
" for model_name in model_names:\n",
" filename = os.path.join('/content', model_name)\n",
" model = tf.keras.models.load_model(filename , custom_objects={'KerasLayer':hub.KerasLayer})\n",
" all_models.append(model)\n",
" print('loaded:', filename)\n",
" return all_models"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"models = load_all_models()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from tensorflow.keras.models import Model , load_model\n",
"from tensorflow.keras.layers import Input, Average\n",
"\n",
"model_input = Input(shape = (224, 224, 3))\n",
"model_outputs = [model(model_input) for model in models]\n",
"ensemble_output = Average()(model_outputs)\n",
"ensemble_model = Model(inputs = model_input , outputs = ensemble_output , name = 'ensemble')"
]
} }
], ],
"metadata": { "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