SOL’s TensorFlow integration supports to translate tf.Function
, tf.Module
,
Keras and tf.saved_model
models into SOL models. If your tf.saved_model
has
multiple signatures, you need to select the preferred one using
sol.optimize(my_saved_model.signatures['my_signature'])
. By default SOL uses
the tf.saved_model.__call__
function.
import tensorflow as tf
import sol
import tensorflow.keras as keras
def AlexNet(input_shape=(224, 224, 3), format="channels_last"):
inputs = keras.Input(shape=(input_shape))
x = inputs
x = keras.layers.Conv2D (input_shape=input_shape, filters=64, kernel_size=(11,11), strides=(4,4), padding='same', activation='relu', data_format=format)(x)
x = keras.layers.MaxPooling2D (pool_size=3, strides=2, padding='valid', data_format=format)(x)
x = keras.layers.Conv2D (filters=192, kernel_size=5, strides=1, padding='same', activation='relu', data_format=format)(x)
x = keras.layers.MaxPooling2D (pool_size=3, strides=2, padding="valid", data_format=format)(x)
x = keras.layers.Conv2D (filters=384, kernel_size=3, strides=1, padding="same", activation='relu', data_format=format)(x)
x = keras.layers.Conv2D (filters=256, kernel_size=3, strides=1, padding="same", activation='relu', data_format=format)(x)
x = keras.layers.Conv2D (filters=256, kernel_size=3, strides=1, padding="same", activation='relu', data_format=format)(x)
x = keras.layers.MaxPooling2D (pool_size=3, strides=2, padding="valid", data_format=format)(x)
x = keras.layers.Flatten (data_format=format)(x)
x = keras.layers.Dropout (rate=0.5)(x)
x = keras.layers.Dense (4096, input_shape=(256*6*6,), activation='relu')(x)
x = keras.layers.Dropout (rate=0.5)(x)
x = keras.layers.Dense (4096, activation="relu")(x)
x = keras.layers.Dense (1000)(x)
return keras.models.Model (inputs=inputs, outputs=x)
@tf.function(input_signature=[tf.TensorSpec([None, 224, 224, 3], dtype=tf.float32)])
def tf_function(input):
return ...
class TFModule(tf.Module):
def init(self):
super().__init__()
self.var = tf.Variable(...)
@tf.function(input_signature=[tf.TensorSpec([None, 224, 224, 3], dtype=tf.float32)])
def __call__(self, input):
return ...
with tf.device('/CPU:0'):
sol_model = sol.optimize(AlexNet(), batch_size=1)
# or
sol_model = sol.optimize(tf_function)
# or
sol_model = sol.optimize(TFModule())
# or
sol_model = sol.optimize(tf.saved_model.load("/path/to/saved/model"))
# Inference
output = sol_model(inputs)
# Training for Keras Models
sol_model.compile(...)
sol_model.fit(inputs, targets)
# Training for tf.Function and tf.Module
# TODO:
Since SOL v0.5.3 we integrated SOL tighter into Keras’s Model.compile(...)
function. It’s still experimental and might not work in all situations.
import sol.tensorflow # required to enable the modifications to Keras
model = init_your_model()
model.compile(optimizer, loss, sol_compile=True) # use sol_vdims=[...] to modify the sol.optimize(..., vdims=sol_vdims) attribute.
model(input_data) # runs using SOL
What are the best configurations for using SOL with TensorFlow? | |
---|---|
If you are using SOL with TensorFlow on X86 you should set the following env vars:
|
How can I define that the model input shall return a gradient? | |
---|---|
By default all inputs get no gradients assigned. If you want to override this behavior, use
All input’s whose name is within the set will return a gradient. |
How can I override the model input shapes? | |
---|---|
By default all inputs use the input shapes defined in the model. If you want to override this behavior, use
Be aware that your overwritten shapes need to be valid in terms of the model, otherwise compilation will fail. |
How can I update/downgrade to another TensorFlow version? | |
---|---|
Before switching version, please have a look at the compatibility list if your TensorFlow
version is supported by SOL. If yes then you can just use |
How do I store/load a Tensorflow Keras model? | |
---|---|
SOL model's cannot be stored directly. For storing/loading a SOL Keras model, use
More information on loading/storing the weights can be found here |
Which activations/recurrent_activations are supported by RNN layers? | |
---|---|
SOL currently supports |
Please refer to https://www.tensorflow.org/api/stable for how these functions are used. This documentation only contains which layers, functions and tensor functionality is currently implemented within SOL.