DETR table detection to ONNX
Author: emigomezCreated Nov 10, 2022Updated Mar 11, 2024
I want to transform the table detection model from detr to onnx. Some models available in HF are either "nielsr/detr-table-detection" or "microsoft/table-transformer-detection".
I try both and with the first one is with the one I'm more close to obtaining the final result so... First, I was able to obtain an ONXX model doing:
!python -m transformers.onnx --model=nielsr/detr-table-detection onnx/
ramework not requested. Using torch to export to ONNX.
Some weights of the model checkpoint at nielsr/detr-table-detection were not used when initializing DetrModel: ['bbox_predictor.layers.2.bias', 'bbox_predictor.layers.1.weight', 'bbox_predictor.layers.0.weight', 'bbox_predictor.layers.1.bias', 'bbox_predictor.layers.0.bias', 'model.encoder.layernorm.weight', 'bbox_predictor.layers.2.weight', 'model.encoder.layernorm.bias', 'class_labels_classifier.bias', 'class_labels_classifier.weight']
- This IS expected if you are initializing DetrModel from the checkpoint of a model trained on another task or with another architecture (e.g. initializing a BertForSequenceClassification model from a BertForPreTraining model).
- This IS NOT expected if you are initializing DetrModel from the checkpoint of a model that you expect to be exactly identical (initializing a BertForSequenceClassification model from a BertForSequenceClassification model).
Could not find image processor class in the image processor config or the model config. Loading based on pattern matching with the model's feature extractor configuration.
Using framework PyTorch: 1.12.1+cu113
/usr/local/lib/python3.7/dist-packages/transformers/models/detr/modeling_detr.py:560: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
if attn_weights.size() != (batch_size * self.num_heads, target_len, source_len):
/usr/local/lib/python3.7/dist-packages/transformers/models/detr/modeling_detr.py:567: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
if attention_mask.size() != (batch_size, 1, target_len, source_len):
/usr/local/lib/python3.7/dist-packages/transformers/models/detr/modeling_detr.py:591: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
if attn_output.size() != (batch_size * self.num_heads, target_len, self.head_dim):
Validating ONNX model...
-[✓] ONNX model output names match reference model ({'last_hidden_state'})
- Validating ONNX Model output "last_hidden_state":
-[✓] (3, 15, 256) matches (3, 15, 256)
-[✓] all values close (atol: 1e-05)
All good, model saved at: onnx/model.onnxAfter that, I'm trying to do the inference with that ONNX model, and I manage to do it but i don't know how to understand the result:
IMAGE_PATH = "1.png"
SCALE = (800,800)
image = Image.open(IMAGE_PATH).convert("RGB")
image = image.resize(SCALE)
feature_extractor = AutoFeatureExtractor.from_pretrained("nielsr/detr-table-detection")
session = InferenceSession("onnx/detr_td.onnx")
# ONNX Runtime expects NumPy arrays as input
inputs = feature_extractor(image, return_tensors="np")
onnx_outputs = session.run(output_names=["last_hidden_state"], input_feed=dict(inputs))These are some logs regarding the result obtained:
print("input name", session.get_inputs()[0].name)
print("input shape", session.get_inputs()[0].shape)
print("input type", session.get_inputs()[0].type)
print("output name", session.get_outputs()[0].name)
print("output shape", session.get_outputs()[0].shape)
print("output type", session.get_outputs()[0].type)
output_names = [_.name for _ in session.get_outputs()]
output_shapes = [_.shape for _ in onnx_outputs]
print("\noutput_names: ", output_names)
print("output_shapes: ", output_shapes)
print("output shape: ", np.array(onnx_outputs).shape)
result = onnx_outputs[0]
print("output shape [0]: ", result.shape)
result = result[0]
print("output shape [0][0]: ", result.shape)
print(result[1])
----------------------------------------------------------------------------
input name pixel_values
input shape ['batch', 'num_channels', 'height', 'width']
input type tensor(float)
output name last_hidden_state
output shape ['batch', 'sequence', 'Addlast_hidden_state_dim_2']
output type tensor(float)
output_names: ['last_hidden_state', 'key_value_states']
output_shapes: [(1, 15, 256)]
output shape: (1, 1, 15, 256)
output shape [0]: (1, 15, 256)
output shape [0][0]: (15, 256)Does any know how to manage this result? it should be a bounding box of the table detected if there are.
Thanks in advance!
Source: facebookresearch/detr