-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_classification.py
44 lines (35 loc) · 1.35 KB
/
image_classification.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# Build Semantic Segmentation
import torch
import torchvision.transforms as transforms
import torchvision.models as models
from PIL import Image
# Load the pre-trained ResNet-50 model trained on ImageNet
model = models.resnet50(pretrained=True) # replace with face mask detection, gesture detection
# Set the model to evaluation mode
model.eval()
# Define the transformations for preprocessing the input image
preprocess = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])
# Load and preprocess the input image
input_image = Image.open("input_image.jpg")
input_tensor = preprocess(input_image)
input_batch = input_tensor.unsqueeze(0) # Add a batch dimension
# Check if GPU is available and move the input batch to the GPU if available
if torch.cuda.is_available():
input_batch = input_batch.to('cuda')
model.to('cuda')
# Perform inference
with torch.no_grad():
output = model(input_batch)
# Load the labels for ImageNet classes
with open("imagenet_classes.txt") as f:
labels = [line.strip() for line in f.readlines()]
# Get the predicted class index
predicted_index = torch.argmax(output[0]).item()
predicted_label = labels[predicted_index]
# Print the predicted label
print("Predicted label:", predicted_label)