SQUEEZENET1.1
import cv2
import numpy as np
from openvino.runtime import Core, Tensor
# Function to preprocess the image
def preprocess_image(image_path):
# Load the image
image = cv2.imread(image_path)
# Resize the image to 227x227 (input size for SqueezeNet)
image_resized = cv2.resize(image, (227, 227))
# Convert from BGR to RGB
image_rgb = image_resized #cv2.cvtColor(image_resized, cv2.COLOR_BGR2RGB)
# Normalize the image
image_normalized = image_rgb.astype(np.float32)
# Change shape to (1, 3, 227, 227)
image_input = np.transpose(image_normalized, (2, 0, 1)) # Shape becomes (3, 227, 227)
image_input = np.expand_dims(image_input, axis=0) # Add batch dimension
return image_input
# Load the OpenVINO runtime
ie = Core()
# Load the model
model_xml = 'models/squeezenet1.1/FP16/squeezenet1.1.xml'
model_bin = 'models/squeezenet1.1/FP16/squeezenet1.1.bin'
model = ie.read_model(model=model_xml, weights=model_bin)
# Compile the model for the target device (e.g., CPU)
compiled_model = ie.compile_model(model, device_name='CPU')
# Create an inference request
infer_request = compiled_model.create_infer_request()
# Preprocess the input image
image_input = preprocess_image("[Dataset]_Module22_images/cat.jpeg")
# Create an OpenVINO tensor from the NumPy array
input_tensor = Tensor(image_input)
# Set the input tensor
infer_request.set_input_tensor(input_tensor)
# Perform inference
infer_request.infer()
# Get the output tensor
output_tensor = infer_request.get_output_tensor(0) # Assuming single output
output_data = output_tensor.data
# Post-process the output
predictions = np.argmax(output_data, axis=1)
print("Predicted class:", predictions)
# Load the labels
def load_labels(labels_file):
with open(labels_file, 'r') as f:
labels = [line.strip() for line in f.readlines()]
return labels
predictions.resize(1)
# Load labels and get the predicted label
labels = load_labels('models/squeezenet1.1/FP32/squeezenet1.1.labels') # Replace with the correct path to your labels file
predicted_label = labels[predictions[0]] # Assuming single image input
print("Predicted class:", predicted_label)
Comments
Post a Comment