| import mlx.core as mx |
| import numpy as np |
| from PIL import Image |
| from mlx_googlenet import GoogLeNet |
| |
| |
|
|
| def preprocess_image(image_path: str, target_size=(224, 224)): |
| """ |
| Loads and preprocesses an image for MLX models. |
| Resizes, normalizes, and converts to HWC MLX array. |
| """ |
| mean = np.array([0.485, 0.456, 0.406]) |
| std = np.array([0.229, 0.224, 0.225]) |
|
|
| image = Image.open(image_path).convert("RGB") |
| image = image.resize(target_size) |
| image = np.array(image, dtype=np.float32) / 255.0 |
|
|
| |
| image = (image - mean) / std |
|
|
| |
| image = mx.array(image[np.newaxis, ...]) |
| return image |
|
|
| def main(): |
| |
| |
| |
| |
| input_image_path = "dummy_input.png" |
|
|
| |
| try: |
| input_image = preprocess_image(input_image_path) |
| print(f"Preprocessed image shape: {input_image.shape}") |
| except FileNotFoundError: |
| print(f"Error: Input image '{input_image_path}' not found.") |
| print("Please create a dummy_input.png or replace the path with an existing image.") |
| return |
|
|
| |
| print("Loading GoogleNet model...") |
| model = GoogLeNet() |
| try: |
| model.load_npz("googlenet_mlx.npz") |
| print("GoogleNet weights loaded successfully.") |
| except FileNotFoundError: |
| print("Error: googlenet_mlx.npz not found.") |
| print("Ensure 'googlenet_mlx.npz' is in the same directory as this script.") |
| return |
|
|
| |
| print("Performing inference...") |
| |
| activations = model(input_image) |
| print("Inference complete.") |
|
|
| |
| print("\nGoogleNet Activations (Layer Names and Shapes):") |
| for layer_name, output_tensor in activations.items(): |
| print(f" {layer_name}: {output_tensor.shape}") |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|