{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "42e4027f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/home/jovyan/shared/Chima/ml_project/repos/MYSMP\n" ] } ], "source": [ "%cd \"../../MYSMP\"" ] }, { "cell_type": "code", "execution_count": 2, "id": "ef6ea33e", "metadata": {}, "outputs": [], "source": [ "# %pip install -r requirements.txt" ] }, { "cell_type": "code", "execution_count": 3, "id": "30812aed", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/home/jovyan/shared/Chima/ml_project/repos/MYSMP/semantic-segmentation\n" ] } ], "source": [ "%cd \"../MYSMP/semantic-segmentation\"" ] }, { "cell_type": "code", "execution_count": 5, "id": "aaa4a036", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/home/jovyan/shared/Chima/ml_project/repos/MYSMP/semantic-segmentation\n" ] } ], "source": [ "!pwd" ] }, { "cell_type": "code", "execution_count": 6, "id": "099239c4", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Loading pretrained model...\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "/srv/conda/envs/notebook/lib/python3.8/site-packages/segmentation_models_pytorch/base/modules.py:116: UserWarning: Implicit dimension choice for softmax has been deprecated. Change the call to include dim=X as an argument.\n", " return self.activation(x)\n" ] } ], "source": [ "# Initialize the model\n", "from SemanticModel.model_core import SegmentationModel\n", "\n", "model = SegmentationModel(\n", " classes=['bg', 'cacao', 'matarraton', 'abarco'],\n", " architecture='unet',\n", " encoder='timm-regnety_120',\n", " weights='../data/model_outputs-unet[timm-regnety_120]-01-23-2025_075803/best_model.pth'\n", ")\n", "\n", "# Initialize prediction pipeline\n", "from SemanticModel.prediction import PredictionPipeline\n", "\n", "predictor = PredictionPipeline(model)\n", "output_dir='../predictions'\n", "image_path= '../data/Images/2019-Mission2-odm_1_42.jpg'\n", "\n", "# Make prediction\n", "prediction = predictor.predict_single_image(image_path=image_path,output_dir=output_dir)" ] }, { "cell_type": "code", "execution_count": 7, "id": "e0ae4c21", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Predictions saved to: path/to/folderofImages/predictions\n" ] }, { "data": { "text/plain": [ "'path/to/folderofImages/predictions'" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Directory of images\n", "image_path= 'path/to/folderofImages'\n", "predictor.predict_directory(image_path)" ] }, { "cell_type": "code", "execution_count": 8, "id": "fc4f2a48", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Loading raster...\n", "Processed 6/6 tiles\n", "Prediction saved to: ../predictions/prediction.tif\n" ] }, { "data": { "text/plain": [ "(array([[[0, 0, 0],\n", " [0, 0, 0],\n", " [0, 0, 0],\n", " ...,\n", " [0, 0, 0],\n", " [0, 0, 0],\n", " [0, 0, 0]],\n", " \n", " [[0, 0, 0],\n", " [0, 0, 0],\n", " [0, 0, 0],\n", " ...,\n", " [0, 0, 0],\n", " [0, 0, 0],\n", " [0, 0, 0]],\n", " \n", " [[0, 0, 0],\n", " [0, 0, 0],\n", " [0, 0, 0],\n", " ...,\n", " [0, 0, 0],\n", " [0, 0, 0],\n", " [0, 0, 0]],\n", " \n", " ...,\n", " \n", " [[0, 0, 0],\n", " [0, 0, 0],\n", " [0, 0, 0],\n", " ...,\n", " [0, 0, 0],\n", " [0, 0, 0],\n", " [0, 0, 0]],\n", " \n", " [[0, 0, 0],\n", " [0, 0, 0],\n", " [0, 0, 0],\n", " ...,\n", " [0, 0, 0],\n", " [0, 0, 0],\n", " [0, 0, 0]],\n", " \n", " [[0, 0, 0],\n", " [0, 0, 0],\n", " [0, 0, 0],\n", " ...,\n", " [0, 0, 0],\n", " [0, 0, 0],\n", " [0, 0, 0]]], dtype=uint8),\n", " {'driver': 'GTiff', 'dtype': 'uint8', 'nodata': None, 'width': 2365, 'height': 1797, 'count': 3, 'crs': CRS.from_epsg(32618), 'transform': Affine(0.03564594364277113, 0.0, 740295.5186183113,\n", " 0.0, -0.03564594364276106, 485117.0212715292), 'tiled': False, 'interleave': 'pixel'})" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Large raster\n", "output_path='../predictions/prediction.tif'\n", "raster_path = '../data/2021-Mission7_clipped_2.tif'\n", "predictor.predict_raster(raster_path, tile_size=1024,output_path=output_path,format='color')" ] }, { "cell_type": "code", "execution_count": null, "id": "8e4ab5a5", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "AgLab - Python 3", "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.8.13" } }, "nbformat": 4, "nbformat_minor": 5 }