-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
3 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"provenance":[],"authorship_tag":"ABX9TyOEfCfsksiRlW2gAwTV71yY"},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"},"accelerator":"GPU","gpuClass":"standard"},"cells":[{"cell_type":"markdown","source":["### Change Runtime type to **GPU**"],"metadata":{"id":"PZ3OtlanAGYr"}},{"cell_type":"code","execution_count":null,"metadata":{"id":"GsJjRNOV86Dc"},"outputs":[],"source":["#installation\n","from google.colab import drive\n","drive.mount('/content/drive')"]},{"cell_type":"markdown","source":["### Change your **Google drive path** then just click run"],"metadata":{"id":"dfbScIVdAj08"}},{"cell_type":"code","source":["# CHANGE YOUR DRIVE PATH HERE\n","%cd /content/drive/MyDrive/YOUR_FOLDER_THAT_HAVE_RAW_DATA_FOLDER\n","!pip install -r requirement.txt"],"metadata":{"id":"doT_Sj4UAhLb"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["#import\n","import os\n","import torch\n","import numpy as np\n","import torchvision\n","import matplotlib.pyplot as plt\n","from torchvision import transforms as T\n","from helper import device, ToJPG, predict, ToTensor, ToTensorShowImage, plot_predict_most_confs, plot_predict"],"metadata":{"id":"uF_sI2Dk9p4C"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["#load trained model\n","current_path = os.getcwd()\n","folder_name = current_path.split('/')[-1]\n","%cd ..\n","for file in os.listdir(folder_name) :\n"," if file.endswith('pt') :\n"," weight_path = file\n","os.chdir(folder_name)\n","class_names = [line.strip() for line in open('class_names.txt', 'r')]\n","\n","os.makedirs('predict_images', exist_ok=True)\n","trained_model = torchvision.models.efficientnet_b2().to(device)\n","trained_model.classifier = torch.nn.Sequential(\n"," torch.nn.Dropout(p=0.3, inplace=True), \n"," torch.nn.Linear(in_features=1408, \n"," out_features=len(class_names), \n"," bias=True)).to(device)\n","trained_model.load_state_dict(torch.load(f=weight_path, map_location=device))"],"metadata":{"id":"R4n_31lL9Hsf"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["### upload predict images in **predict_images** folder\n","- !!! uploaded images before run the next cell"],"metadata":{"id":"3_yo5AXu-WSx"}},{"cell_type":"code","source":["# to .jpg format\n","for image_name in os.listdir('predict_images') :\n"," image_path = os.path.join('predict_images', image_name)\n"," ToJPG(image_path)"],"metadata":{"id":"mxyblz9q-RUu"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["### predict a single image\n","- change image_path "],"metadata":{"id":"7FFp_4DEJLOm"}},{"cell_type":"code","source":["image_path = 'predict_images/YOUR_IMAGE_NAME.jpg' \n","most_like = 3 # find most like 3 classes\n","\n","pred_class, confs = predict(ToTensor(image_path), trained_model, class_names)\n","confs = confs*100\n","print(f'''\n","predict class : {pred_class}\n","confident : {np.max(confs):.2f}%\n","''')"],"metadata":{"id":"2FeGP8Bb-h56"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["plot_predict_most_confs(image_path, most_like, class_names, trained_model)"],"metadata":{"id":"qyCjedA8Eunq"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["### predict multiple images\n","- just run and see a result"],"metadata":{"id":"3SjaNoXQRXeU"}},{"cell_type":"code","source":["images_path = [os.path.join('predict_images', image_name) for image_name in os.listdir('predict_images') if not image_name.endswith('avif')]\n","size = int(len(images_path)**(1/2)) + 1\n","plt.figure(figsize=(4*size, 4*size))\n","for i, image_name in enumerate(images_path, start=1) : \n"," plt.subplot(size, size, i)\n"," image_fp = ToTensor(image_name)\n"," image_fs = ToTensorShowImage(image_name)\n"," plot_predict(image_fp, trained_model, image_fs, class_names, color='black')"],"metadata":{"id":"8LvSkzknQ0xp"},"execution_count":null,"outputs":[]}]} | ||
{"cells":[{"cell_type":"markdown","metadata":{"id":"PZ3OtlanAGYr"},"source":["### Change Runtime type to **GPU**"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"GsJjRNOV86Dc"},"outputs":[],"source":["#installation\n","from google.colab import drive\n","drive.mount('/content/drive')"]},{"cell_type":"markdown","metadata":{"id":"dfbScIVdAj08"},"source":["### Change your **Google drive path** then just click run"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"doT_Sj4UAhLb"},"outputs":[],"source":["# CHANGE YOUR DRIVE PATH HERE\n","%cd /content/drive/MyDrive/YOUR_FOLDER_THAT_HAVE_RAW_DATA_FOLDER\n","!pip install -r requirement.txt"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"uF_sI2Dk9p4C"},"outputs":[],"source":["#import\n","import os\n","import torch\n","import numpy as np\n","import torchvision\n","import matplotlib.pyplot as plt\n","from torchvision import transforms as T\n","from helper import device, ToJPG, predict, ToTensor, ToTensorShowImage, plot_predict_most_confs, plot_predict"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"R4n_31lL9Hsf"},"outputs":[],"source":["#load trained model\n","current_path = os.getcwd()\n","folder_name = current_path.split('/')[-1]\n","%cd ..\n","for file in os.listdir(folder_name) :\n"," if file.endswith('pt') :\n"," weight_path = file\n","os.chdir(folder_name)\n","class_names = [line.strip() for line in open('class_names.txt', 'r')]\n","\n","os.makedirs('predict_images', exist_ok=True)\n","trained_model = torchvision.models.efficientnet_b2().to(device)\n","trained_model.classifier = torch.nn.Sequential(\n"," torch.nn.Dropout(p=0.3, inplace=True), \n"," torch.nn.Linear(in_features=1408, \n"," out_features=len(class_names), \n"," bias=True)).to(device)\n","trained_model.load_state_dict(torch.load(f=weight_path, map_location=device))"]},{"cell_type":"markdown","metadata":{"id":"3_yo5AXu-WSx"},"source":["### upload predict images in **predict_images** folder\n","- !!! uploaded images before run the next cell"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"mxyblz9q-RUu"},"outputs":[],"source":["# to .jpg format\n","for image_name in os.listdir('predict_images') :\n"," image_path = os.path.join('predict_images', image_name)\n"," ToJPG(image_path)"]},{"cell_type":"markdown","metadata":{"id":"7FFp_4DEJLOm"},"source":["### predict a single image\n","- change image_path "]},{"cell_type":"code","execution_count":null,"metadata":{"id":"2FeGP8Bb-h56"},"outputs":[],"source":["image_path = 'predict_images/YOUR_IMAGE_NAME.jpg' \n","most_like = 3 # find most like 3 classes\n","\n","pred_class, confs = predict(ToTensor(image_path), trained_model, class_names)\n","confs = confs*100\n","print(f'''\n","predict class : {pred_class}\n","confident : {np.max(confs):.2f}%\n","''')"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"qyCjedA8Eunq"},"outputs":[],"source":["plot_predict_most_confs(image_path, most_like, class_names, trained_model)"]},{"cell_type":"markdown","metadata":{"id":"3SjaNoXQRXeU"},"source":["### predict multiple images\n","- just run and see a result"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"8LvSkzknQ0xp"},"outputs":[],"source":["images_path = [os.path.join('predict_images', image_name) for image_name in os.listdir('predict_images') if not image_name.endswith('avif')]\n","size = int(len(images_path)**(1/2)) + 1\n","plt.figure(figsize=(4*size, 4*size))\n","for i, image_name in enumerate(images_path, start=1) : \n"," plt.subplot(size, size, i)\n"," image_fp = ToTensor(image_name)\n"," image_fs = ToTensorShowImage(image_name)\n"," plot_predict(image_fp, trained_model, image_fs, class_names, color='black')"]}],"metadata":{"accelerator":"GPU","colab":{"authorship_tag":"ABX9TyOEfCfsksiRlW2gAwTV71yY","provenance":[]},"gpuClass":"standard","kernelspec":{"display_name":"Python 3.10.0 64-bit","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.10.0"},"vscode":{"interpreter":{"hash":"63e79917a05e390872358bfb73c58bc903ada01d2d04077091749088207d82cf"}}},"nbformat":4,"nbformat_minor":0} |
Large diffs are not rendered by default.
Oops, something went wrong.