Noob Image Classification Training Play

cookie: <p>Hi, I followed a tutorial on how to train a dataset for Image Classification:</p>
<p>[link removed]</p><aside class="onebox githubblob" data-onebox-src="https://github.com/mano3-1/Mr-AI/blob/main/Pytorch%20Zero%20to%20Hero/pytorch-zero-to-hero-image-classficiation.ipynb">
  <header class="source">

      <a href="https://github.com/mano3-1/Mr-AI/blob/main/Pytorch%20Zero%20to%20Hero/pytorch-zero-to-hero-image-classficiation.ipynb" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>

  <article class="onebox-body">
    <h4><a href="https://github.com/mano3-1/Mr-AI/blob/main/Pytorch%20Zero%20to%20Hero/pytorch-zero-to-hero-image-classficiation.ipynb" target="_blank" rel="noopener nofollow ugc">mano3-1/Mr-AI/blob/main/Pytorch Zero to Hero/pytorch-zero-to-hero-image-classficiation.ipynb</a></h4>


      <pre><code class="lang-ipynb">{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install timm","metadata":{"execution":{"iopub.status.busy":"2022-08-25T20:34:09.290416Z","iopub.execute_input":"2022-08-25T20:34:09.291139Z","iopub.status.idle":"2022-08-25T20:34:23.476805Z","shell.execute_reply.started":"2022-08-25T20:34:09.291008Z","shell.execute_reply":"2022-08-25T20:34:23.475582Z"},"trusted":true},"execution_count":1,"outputs":[]},{"cell_type":"code","source":"import torch\nfrom torch import nn\nfrom torch.nn import functional as F\nfrom torch.optim import Adam\nfrom torch.utils.data import Dataset, DataLoader\n\nimport timm\n\nimport numpy as np\nfrom tqdm.notebook import tqdm\nimport glob\nimport os\nimport matplotlib.pyplot as plt\nimport cv2","metadata":{"execution":{"iopub.status.busy":"2022-08-25T20:34:23.479959Z","iopub.execute_input":"2022-08-25T20:34:23.481142Z","iopub.status.idle":"2022-08-25T20:34:26.247842Z","shell.execute_reply.started":"2022-08-25T20:34:23.481097Z","shell.execute_reply":"2022-08-25T20:34:26.246785Z"},"trusted":true},"execution_count":2,"outputs":[]},{"cell_type":"code","source":"train_dir = \"../input/10-monkey-species/training/training/\"\nvalid_dir = \"../input/10-monkey-species/validation/validation/\"\n\ntrain_image_dirs = glob.glob(train_dir + \"/*/*.*\")\nvalid_image_dirs = glob.glob(valid_dir + \"/*/*.*\")\n\n\nunique_labels = os.listdir(train_dir)\nprint(unique_labels)","metadata":{"execution":{"iopub.status.busy":"2022-08-25T20:34:26.249475Z","iopub.execute_input":"2022-08-25T20:34:26.250177Z","iopub.status.idle":"2022-08-25T20:34:26.416493Z","shell.execute_reply.started":"2022-08-25T20:34:26.250138Z","shell.execute_reply":"2022-08-25T20:34:26.415320Z"},"trusted":true},"execution_count":3,"outputs":[]},{"cell_type":"code","source":"DEVICE = \"cuda\" if torch.cuda.is_available() else \"cpu\"","metadata":{"execution":{"iopub.status.busy":"2022-08-25T20:34:26.421592Z","iopub.execute_input":"2022-08-25T20:34:26.422929Z","iopub.status.idle":"2022-08-25T20:34:26.497789Z","shell.execute_reply.started":"2022-08-25T20:34:26.422896Z","shell.execute_reply":"2022-08-25T20:34:26.496439Z"},"trusted":true},"execution_count":4,"outputs":[]},{"cell_type":"code","source":"print(DEVICE)","metadata":{"execution":{"iopub.status.busy":"2022-08-25T20:34:26.501486Z","iopub.execute_input":"2022-08-25T20:34:26.504767Z","iopub.status.idle":"2022-08-25T20:34:26.510382Z","shell.execute_reply.started":"2022-08-25T20:34:26.504731Z","shell.execute_reply":"2022-08-25T20:34:26.509124Z"},"trusted":true},"execution_count":5,"outputs":[]},{"cell_type":"code","source":"print(train_image_dirs)","metadata":{"execution":{"iopub.status.busy":"2022-08-25T20:34:26.511922Z","iopub.execute_input":"2022-08-25T20:34:26.512617Z","iopub.status.idle":"2022-08-25T20:34:26.529494Z","shell.execute_reply.started":"2022-08-25T20:34:26.512578Z","shell.execute_reply":"2022-08-25T20:34:26.528250Z"},"trusted":true},"execution_count":6,"outputs":[]},{"cell_type":"markdown","source":"# Dataset Class \nexpects 3 functions:\n* init function : takes all necessary inputs\n* len function :  returns length of the dataset\n* getitem function : returns input and label that can be fed to neural network","metadata":{}},{"cell_type":"code","source":"class BIRD_SPECIES(Dataset):\n    def __init__(self, train_dirs, labels):\n        #get all necessary inputs like train directories and labels\n        self.train_dirs = train_dirs\n        self.labels = labels\n        self.labels = np.asarray(self.labels)\n\n        \n    def __len__(self,):\n        #return len of the dataset\n        return len(self.train_dirs)\n    \n    def get_one_hot_encoding(self, cat):\n        one_hot = np.asarray(cat == self.labels)\n        return one_hot\n        \n    def __getitem__(self, idx):\n        #get data for one id value..pytorch will handle the batching for you!\n        train_img_dir = self.train_dirs[idx]\n        label = train_img_dir.split(\"/\")[-2]\n        \n        #print(train_img_dir)\n        img = cv2.imread(train_img_dir)\n        img = cv2.resize(img, (256, 256))\n        one_hot = self.get_one_hot_encoding(label)\n        \n        img = img.transpose((2, 0, 1))\n        img = torch.tensor(img, dtype = torch.float)\n        one_hot = torch.tensor(one_hot, dtype = torch.float)\n        return img/255.0, one_hot\n    \n    \ntrain_dataset = BIRD_SPECIES(train_image_dirs, unique_labels)\nvalid_dataset = BIRD_SPECIES(valid_image_dirs, unique_labels)\nimg, label = train_dataset.__getitem__(1000)\nimg = img.numpy().transpose(1,2,0)\nplt.imshow(img)\nprint(label)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-25T20:34:26.530572Z","iopub.execute</code></pre>



  This file has been truncated. <a href="https://github.com/mano3-1/Mr-AI/blob/main/Pytorch%20Zero%20to%20Hero/pytorch-zero-to-hero-image-classficiation.ipynb" target="_blank" rel="noopener nofollow ugc">show original</a>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<p>My problem is a usage example…</p>
<p>I amended the following, [link removed] to get the following code:</p>
<pre data-code-wrap="import"><code class="lang-plaintext">from PIL import Image
import numpy as np
import time

from torch import nn
from torch.nn import functional as F

#import glob
import os

import timm

#	GEN LABELS
train_dir = "/home/ai/datasets/kaggle.com/ten_monkey_species/dataset_10_monkey_species/training/training/"
unique_labels = os.listdir(train_dir)
print(unique_labels)




class MonkeySpeciesClassifier(nn.Module):
	def __init__(self, num_classes):
		#define necessary layers
		super().__init__()
		self.num_classes = num_classes      
		self.model = timm.create_model(model_name = "resnet34", pretrained = True)
		self.model.fc = nn.Linear(self.model.fc.in_features, out_features = num_classes)

		
	def forward(self,X):
		#define forward pass here
		return F.softmax(self.model(X), dim=-1)

#model = MonkeySpeciesClassifier(len(unique_labels)).to(DEVICE)
#print(model(torch.zeros((1, 3, 256, 256)).to(DEVICE)).shape)
model = MonkeySpeciesClassifier(len(unique_labels))
print(model(torch.zeros((1, 3, 256, 256))).shape)



# Function to load the model
def load_model(model_path):
	#model = torch.load(model_path)
	#model = torch.load(model_path, encoding='ascii')
	model.load_state_dict(torch.load(model_path))
	if model:
		model.eval()  # Set the model to evaluation mode
	else:
		print("Error loading model: ", model_path)
	return model

# Function to preprocess the image before feeding it to the model
def preprocess_image(image_path):
	img = Image.open(image_path).convert("RGB")
	img = img.resize((224, 224))  # Resize to the model's expected input size
	img = np.array(img).astype(np.float32) / 255.0  # Convert to a NumPy array and normalize
	img = np.transpose(img, (2, 0, 1))  # Transpose the image to (channels, height, width)
	img = np.expand_dims(img, axis=0)  # Add a batch dimension
	return torch.tensor(img)

# Load the model
model_path = "best.pt"
model = load_model(model_path)

print("Loading image")
# Load the image and preprocess it
image_path = "/home/ai/datasets/kaggle.com/ten_monkey_species/dataset_10_monkey_species/validation/validation/n4/n404.jpg"

print("Processing image")
input_image = preprocess_image(image_path)

# Run the model
output = model(input_image)
print(output)
</code></pre>
<p>I’m not sure if I’m getting the wrong answer or no answer tbh… but the result is as follows, I tried to decode as I’ve seen, but as I say I’m still a noob, so any help / examples or even links to training and then using would be great please</p>
<pre><code class="lang-auto">tensor([[2.6537e-05, 6.2218e-06, 8.1168e-05, 7.7195e-05, 9.9924e-01, 1.3025e-04,
         1.2812e-04, 4.9623e-05, 2.0674e-04, 4.9618e-05]],
       grad_fn=&lt;SoftmaxBackward0&gt;)
</code></pre>
<p>Thanks</p>

ptrblck: <p>I’m unsure where exactly you are stuck, but in case you want to interpret the output of your model you would interpret it as a tensor containing probabilities for each class given the input image. In your case the model predicts with a high probability the sample belongs to <code>class4</code>. Let me know if I misunderstood your question.</p>

cookie: <p>Hi, sorry was a stressful night, but oh! oops!</p>
<p>Many thanks <img src="https://discuss.pytorch.org/images/emoji/apple/laughing.png?v=12" title=":laughing:" class="emoji" alt=":laughing:" loading="lazy" width="20" height="20"></p>