Created
April 3, 2023 15:50
-
-
Save aurotripathy/5f71230ca241f45e1b961ec7d98369f6 to your computer and use it in GitHub Desktop.
example of a model that does not fit.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import logging | |
import os | |
from furiosa import runtime | |
from furiosa.runtime import session | |
import numpy as np | |
LOGLEVEL = os.environ.get('FURIOSA_LOG_LEVEL', 'INFO').upper() | |
logging.basicConfig(level=LOGLEVEL) | |
quantized_model_path = './model_quantized.dfg' | |
def run_example(): | |
runtime.__full_version__ | |
sess = session.create(str(quantized_model_path)) | |
sess.print_summary() | |
# Print the first input tensor shape and dimensions | |
input_tensor = sess.inputs()[0] | |
print(input_tensor) | |
# Generate the random input tensor according to the input shape | |
input = np.random.randint(0, 255, input_tensor.shape).astype("float32") | |
# Run the inference | |
outputs = sess.run(input) | |
print("== Output ==") | |
print(outputs) | |
print(outputs[0].numpy()) | |
if __name__ == "__main__": | |
run_example() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
ChatGPT prompt: | |
write a PyTorch class with 512 x 512 x 3 input image that builds a 100 layer network of repeated conv layers | |
""" | |
import torch | |
import torch.nn as nn | |
from torchsummary import summary | |
import onnx | |
import onnxruntime | |
class ConvNet(nn.Module): | |
def __init__(self): | |
super(ConvNet, self).__init__() | |
self.in_channels = 3 | |
self.out_channels = 256 | |
self.kernel_size = 3 | |
self.num_layers = 150 | |
self.conv_layers = nn.ModuleList() | |
self.conv_layers.append(nn.Conv2d(self.in_channels, | |
self.out_channels, | |
self.kernel_size)) | |
for i in range(self.num_layers - 1): | |
self.conv_layers.append(nn.Conv2d(self.out_channels, | |
self.out_channels, | |
self.kernel_size, padding=1)) | |
self.activation = nn.ReLU(inplace=True) | |
def forward(self, x): | |
for conv_layer in self.conv_layers: | |
x = conv_layer(x) | |
x = self.activation(x) | |
# Flatten the output for the fully connected layer | |
x = x.view(x.size(0), -1) | |
return x | |
def convert_to_onnx(model, input_shape): | |
# Generate a random input tensor of the specified shape | |
x = torch.randn(input_shape) | |
model.eval() | |
# Export the PyTorch model to ONNX format | |
dummy_input = torch.randn(input_shape) | |
onnx_model = torch.onnx.export(model, dummy_input, | |
"model.onnx", opset_version=12) | |
model = ConvNet() | |
summary(model, (3, 224, 224)) | |
convert_to_onnx(model, (1, 3, 224, 224)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
Needs the imagnet 'val' folder with the structure retained | |
and a few images for the calibration functions to work | |
""" | |
import sys | |
import onnx | |
import torch | |
import torchvision | |
from torchvision import transforms | |
import tqdm | |
from furiosa.optimizer import optimize_model | |
from furiosa.quantizer import quantize, Calibrator, CalibrationMethod | |
def create_quantized_dfg(): | |
model = onnx.load_model("model.onnx") | |
preprocess = transforms.Compose( | |
[transforms.ToTensor(), | |
transforms.Resize((224,224)), | |
transforms.Normalize(mean=(0.1307,), std=(0.3081,))] | |
) | |
calibration_data = torchvision.datasets.ImageNet('.', | |
split='val', | |
transform=preprocess) | |
calibration_dataloader = torch.utils.data.DataLoader(calibration_data, | |
batch_size=1, | |
shuffle=True) | |
model = optimize_model(model) | |
calibrator = Calibrator(model, CalibrationMethod.MIN_MAX) | |
for calibration_data, _ in tqdm.tqdm(calibration_dataloader, | |
desc="Calibration", | |
unit="images", | |
mininterval=0.5): | |
calibrator.collect_data([[calibration_data.numpy()]]) | |
ranges = calibrator.compute_range() | |
model_quantized = quantize(model, ranges) | |
with open("model_quantized.dfg", "wb") as f: | |
f.write(bytes(model_quantized)) | |
if __name__ == "__main__": | |
sys.exit(create_quantized_dfg()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment