Export Detectron2 model before calling model.eval()

mycul: <p>I trained a detectron2 model on some data that I plan to use, but unfortunately the team I’m working with is requiring that I use another software that they have already implemented (its a long story). Anyways now I’m having to try to convert this model into a software known as MVtec Halcon. The issue is that it gives the following error when trying to upload the onnx model of the detectron2 that I trained:</p>
<blockquote>
<p>Unhandled program exception:</p>
<p>HALCON operator error</p>
<p>while calling ‘read_dl_model’ in procedure ‘main’ line: 4.</p>
<p>Error occurred while reading an ONNX model (HALCON error code: 7801)</p>
<p>Extended error code: 5:</p>
<p>Shape dimension for graph input must be 4</p>
</blockquote>
<p>What I have come to understand from this issue is that it requires me to convert the detectron2 model into onnx before calling the model.eval() method which removes the batch_size from the input requirements for the detectron2 model.</p>
<p>I imagine this isn’t common, but I was curious to if anyone has had any experience with this and could provide any tips. I can’t seem to figure it out and I can’t seem to find anything online relating to it.</p>
<p>ADDITIONAL INFO: The batch_size that I trained on is 1 (the images are massive), so maybe there is a way I can export the model in the typical way and then just unsqueeze the input? Open to any ideas.</p>

mycul: <p>So I have been continuing to try to figure this out and arrived at a potential idea. I was able to employ the following code:</p>
<blockquote>
<p>import onnx<br>
from onnx import helper, TensorProto</p>
<h1>Load your ONNX model</h1>
<p>model = onnx.load(model_path_v1)</p>
<h1>Define the input tensor with dynamic batch size</h1>
<p>input_tensor = helper.make_tensor_value_info(‘input’, TensorProto.FLOAT, [1, 3, 1333, 348])</p>
<h1>Update the input of the model</h1>
<p>model.graph.input[0].CopyFrom(input_tensor)</p>
<h1>Iterate through all nodes and update dimensions if necessary</h1>
<p>for node in model.graph.node:<br>
for i, input_name in enumerate(node.input):<br>
if input_name == ‘x.1’:<br>
node.input[i] = ‘input’</p>
<h1>Save the modified model</h1>
<p>onnx.save(model, ‘modified_model.onnx’)</p>
</blockquote>
<p>It increases the input to contain a fixed batch_size; However, this just gives me more issues (which was kind of expected). Here is the netron of my model, hopefully that can help someone who is trying to understand my issue.</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/5/d/5d82962c4e597ee42b7b5dc1284802a50147de2b.png" data-download-href="https://discuss.pytorch.org/uploads/default/5d82962c4e597ee42b7b5dc1284802a50147de2b" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/5/d/5d82962c4e597ee42b7b5dc1284802a50147de2b_2_398x500.png" alt="image" data-base62-sha1="dlebudDUIO6kT9n5ja32asBci3V" width="398" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/5/d/5d82962c4e597ee42b7b5dc1284802a50147de2b_2_398x500.png, https://discuss.pytorch.org/uploads/default/optimized/3X/5/d/5d82962c4e597ee42b7b5dc1284802a50147de2b_2_597x750.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/5/d/5d82962c4e597ee42b7b5dc1284802a50147de2b.png 2x" data-dominant-color="F4F4F4"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">735×923 24.9 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>