如何在Google Colab中使用Intel OpenVINO Toolkit及转换模型至IR格式
Hey there! Let's tackle your needs together—setting up Intel OpenVINO Toolkit on Google Colab, and converting your cloud-stored model to IR format since your local CPU doesn't support AVX instructions. Here's a straightforward, step-by-step guide:
Step 1: Install OpenVINO Toolkit in Google Colab
Colab's cloud runtime uses CPUs that support AVX, so we don't have to worry about your local hardware limitations here. Let's get the toolkit installed:
- First, confirm your Colab runtime uses Python 3.7+ (it usually does by default, but double-check with):
!python --version - Install the toolkit via pip. Include the optional extensions if you need support for specific model formats like ONNX or TensorFlow:
!pip install openvino-dev[onnx,tensorflow2] - Verify the installation worked by checking the Model Optimizer (our conversion tool) version:
!mo --version
Step 2: Mount Your Cloud Drive to Access the Model
Since your model lives in cloud storage, we'll mount it to Colab to access the files directly. I'll assume you're using Google Drive (the most common with Colab):
- Run this snippet and follow the authorization prompt to link your drive:
from google.colab import drive drive.mount('/content/drive') - Confirm your model is accessible by listing the contents of its folder:
!ls /content/drive/MyDrive/[your-model-folder-path]
Step 3: Convert Your Model to IR Format
The Model Optimizer (mo) converts pre-trained models to OpenVINO's IR format. The exact command depends on your model type—here are common examples:
For TensorFlow SavedModel
!mo --saved_model_dir /content/drive/MyDrive/path/to/your/saved_model --output_dir /content/drive/MyDrive/path/to/save/ir_model
For ONNX Model
!mo --input_model /content/drive/MyDrive/path/to/your/model.onnx --output_dir /content/drive/MyDrive/path/to/save/ir_model
For PyTorch Model (via ONNX)
First export your PyTorch model to ONNX, then use the ONNX conversion command above:
import torch # Import your model class (adjust this to match your code) from your_model_module import YourModel # Load and prepare the model model = YourModel() model.load_state_dict(torch.load('/content/drive/MyDrive/path/to/your/model.pth')) model.eval() # Export to ONNX (adjust dummy input shape to match your model's input) dummy_input = torch.randn(1, 3, 224, 224) torch.onnx.export(model, dummy_input, '/content/drive/MyDrive/path/to/exported_model.onnx', opset_version=11)
Quick Tips:
- Replace all paths with your actual cloud drive paths.
- Add the
--input_shapeflag if your model uses dynamic input shapes (e.g.,--input_shape [1,3,224,224]). - If you hit errors, check OpenVINO's supported model list—most popular pre-trained models (ResNet, BERT, etc.) work seamlessly.
Step 4: Verify the Converted IR Model
Once conversion is done, test the model to make sure it works:
from openvino.runtime import Core # Load and compile the IR model ie = Core() model = ie.read_model(model='/content/drive/MyDrive/path/to/save/ir_model/model.xml') compiled_model = ie.compile_model(model=model, device_name='CPU') # Run a quick inference test input_layer = compiled_model.input(0) output_layer = compiled_model.output(0) import numpy as np sample_input = np.random.randn(*input_layer.shape) result = compiled_model([sample_input])[output_layer] print("Inference successful! Output shape:", result.shape)
内容的提问来源于stack exchange,提问作者user11585758




