This tutorial uses torch, numpy and torchvision models as examples to demonstrate how to use SOL. These can be installed via:
pip install torch torchvision numpy
If you want to use the Tensorflow or ONNX examples instead they can be installed accordingly:
pip install tensorflow
pip install onnx
SOL does not provide an interface to create models from scratch. So you have to create them in an existing framework before you import them into SOL. There are multiple ways to do so. You can create your models by hand, load them from a model zoo or load your own saved models. The way in which a model was created is not important to SOL. It can read any valid model regardless of its creation process. Here are a few examples to get you started:
In PyTorch you can create a model by hand with torch.nn.Module
, you can load a
pretrained model from a model zoo or one you saved earlier.
Here is a simple example of creating or loading a model in torch:
import torch
import torch.nn as nn
import torchvision.models as models
class SimpleNN(nn.Module):
def __init__(self):
super(SimpleNN, self).__init__()
self.fc1 = nn.Linear(10, 50)
self.fc2 = nn.Linear(50, 1)
self.relu = nn.ReLU()
def forward(self, x):
x = self.relu(self.fc1(x))
x = self.fc2(x)
return x
# Create an instance of the model
model = SimpleNN()
Alternatively you can load a pretrained model, e.g. from torchvision:
import torch
import torchvision.models as models
model = models.resnet18(pretrained=True)
The model can also be created in Tensorflow in a similar way.
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
model = keras.Sequential([
layers.Dense(64, activation='relu', input_shape=(10,)),
layers.Dense(32, activation='relu'),
layers.Dense(1, activation='sigmoid')
])
# Compile the model
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
Alternatively you can load a pretrained model, e.g. from keras.applications:
import tensorflow as tf
model = tf.keras.applications.MobileNetV2(weights='imagenet')
In ONNX you can load a pretrained model, e.g. from onnx.hub:
from onnx import hub
model = hub.load("resnet50")