tensorflow/tensorrt

Unable to save gradient functions when exporting a _DefinedFunction when using converter.save('model')

tanpengshi opened this issue · 1 comments

I have trained a keras model with transfer learning from Resnet and here are my training codes:

pre_trained_model = ResNet50(input_shape=(150,150,3),
                                include_top=False,
                                weights="imagenet")

for layer in pre_trained_model.layers[:-5]:
    layer.trainable=False
model = tf.keras.models.Sequential([
    pre_trained_model,
    GlobalAveragePooling2D(),    
    Dense(512,activation="swish"),
    Dropout(0.7),
    Dense(256,activation="swish"),
    Dropout(0.5),
    Dense(128,activation="swish"),
    Dropout(0.3), 
    Dense(32,activation="tanh"),
    Dropout(0.2), 
    Dense(1, activation='sigmoid')
])

model.compile(optimizer=RMSprop(learning_rate=1e-4),
              loss="binary_crossentropy",
              metrics=['accuracy'])

After that I save the model and to TensorRT conversion:

model.save('my_model')

params = tf.experimental.tensorrt.ConversionParams(
    precision_mode='FP16',
)

converter = tf.experimental.tensorrt.Converter(
    input_saved_model_dir='my_model', conversion_params=params
)

converter.convert()
converter.save('my_quantized_model')

And I received a ValueError:
_ValueError: Unable to save gradient functions when exporting a DefinedFunction (generally created through graph freezing utils or through V1 graph importers). Please save with options=tf.SaveOptions(experimental_custom_gradients=False)

This error does not occur if I do not add keras transfer learning model to my Sequential backbone. Help will be much appreciated! :)

I have solved my own problem:

Basically TensorRT cannot do the conversion when I used 'swish' as activation function. When I used other activation functions like 'relu', it works perfectly!