From 33a634ef7b65a1feddbe744cb5525c96e8396699 Mon Sep 17 00:00:00 2001 From: Kshitij Date: Sat, 22 Feb 2025 19:34:17 +0530 Subject: [PATCH] Test 3 using TonyStarkD99 large model. Works like a charm. Need to increase the class labels. --- models/test3.py | 50 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 models/test3.py diff --git a/models/test3.py b/models/test3.py new file mode 100644 index 0000000..782efd4 --- /dev/null +++ b/models/test3.py @@ -0,0 +1,50 @@ +from PIL import Image +import torch +import numpy as np +from transformers import CLIPModel, CLIPTokenizer + +# Load the model +model_name = "TonyStarkD99/CLIP-Crop_Disease-Large" +model = CLIPModel.from_pretrained(model_name) + +# Load your image +image_path = "/home/overnion/Status200/potato.png" # Replace with your image path +image = Image.open(image_path) + +# Define the class labels (text prompts) +class_labels = [ + "healthy plant", + "diseased plant", + "wilted plant", + "pest-infested plant" +] + +# Resize and normalize the image +image = image.convert("RGB") # Ensure the image is in RGB format +image = image.resize((224, 224)) # Resize to the expected input size + +# Convert the image to a tensor +image_tensor = torch.tensor(np.array(image)).permute(2, 0, 1).unsqueeze(0) # Convert to (1, C, H, W) +image_tensor = image_tensor.float() / 255.0 # Normalize to [0, 1] + +# Load the tokenizer +tokenizer = CLIPTokenizer.from_pretrained("openai/clip-vit-base-patch16") # Use a compatible tokenizer + +# Tokenize the text prompts +text_inputs = tokenizer(class_labels, padding=True, return_tensors="pt") + +# Make predictions +with torch.no_grad(): + outputs = model(pixel_values=image_tensor, input_ids=text_inputs['input_ids']) + +logits_per_image = outputs.logits_per_image # This gives the similarity scores +probs = logits_per_image.softmax(dim=1) # Convert to probabilities + +# Get the predicted class +predicted_class_idx = probs.argmax().item() +predicted_class = class_labels[predicted_class_idx] + +# Print the predicted class and probabilities +print("Predicted class:", predicted_class) +print("Probabilities:", probs.detach().numpy()) +