This is how i built the example model in python:
import onnx
nodes = []
node = onnx.helper.make_node('Constant', inputs=[], outputs=['shape_indices'], value=onnx.helper.make_tensor(name='const_tensor', data_type=7, dims=[2], vals=[2,3]))
nodes.append(node)
node = onnx.helper.make_node('Constant', inputs=[], outputs=['divisor'], value=onnx.helper.make_tensor(name='const_tensor', data_type=7, dims=[], vals=[32]))
nodes.append(node)
node = onnx.helper.make_node('Shape', inputs=['input'], outputs=['input_shape'])
nodes.append(node)
node = onnx.helper.make_node('Gather', inputs=['input_shape', 'shape_indices'], outputs=['image_shape'])
node.attribute.append(onnx.helper.make_attribute("axis", 0))
nodes.append(node)
node = onnx.helper.make_node('Mod', inputs=['image_shape','divisor'], outputs=['remnant'])
nodes.append(node)
node = onnx.helper.make_node('Sub', inputs=['divisor', 'remnant'], outputs=['padding'])
nodes.append(node)
node = onnx.helper.make_node('Constant', inputs=[], outputs=['zeros'], value=onnx.helper.make_tensor(name='const_tensor', data_type=7, dims=[6], vals=[0,0,0,0,0,0]))
nodes.append(node)
node = onnx.helper.make_node('Concat', inputs=['zeros', 'padding'], outputs=['full_padding'])
node.attribute.append(onnx.helper.make_attribute("axis", 0))
nodes.append(node)
node = onnx.helper.make_node('Pad', inputs=['input', 'full_padding'], outputs=['padded_input'])
node.attribute.append(onnx.helper.make_attribute("mode", "constant"))
nodes.append(node)
node = onnx.helper.make_node('Constant', inputs=[], outputs=['weights'], value=onnx.helper.make_tensor(name='const_tensor', data_type=1, dims=[1, 3,3,3], vals=[1]*3*3*3))
nodes.append(node)
node = onnx.helper.make_node('Constant', inputs=[], outputs=['weights_flat'], value=onnx.helper.make_tensor(name='const_tensor', data_type=1, dims=[1, 1, 3,3], vals=[1]*3*3))
nodes.append(node)
node = onnx.helper.make_node('Conv', inputs=['padded_input', 'weights'], outputs=['conv_1'], name="Conv1")
node.attribute.append(onnx.helper.make_attribute("dilations", [1,1]))
node.attribute.append(onnx.helper.make_attribute("group", 1))
node.attribute.append(onnx.helper.make_attribute("kernel_shape", [3,3]))
node.attribute.append(onnx.helper.make_attribute("pads", [0,0,0,0]))
node.attribute.append(onnx.helper.make_attribute("strides", [1,1]))
nodes.append(node)
node = onnx.helper.make_node('Conv', inputs=['conv_1', 'weights_flat'], outputs=['conv_2'], name="Conv2")
node.attribute.append(onnx.helper.make_attribute("dilations", [1,1]))
node.attribute.append(onnx.helper.make_attribute("group", 1))
node.attribute.append(onnx.helper.make_attribute("pads", [0,0,0,0]))
node.attribute.append(onnx.helper.make_attribute("strides", [1,1]))
nodes.append(node)
node = onnx.helper.make_node('Conv', inputs=['conv_2', 'weights_flat'], outputs=['output_1'], name="Conv3")
node.attribute.append(onnx.helper.make_attribute("dilations", [1,1]))
node.attribute.append(onnx.helper.make_attribute("group", 1))
node.attribute.append(onnx.helper.make_attribute("pads", [0,0,0,0]))
node.attribute.append(onnx.helper.make_attribute("strides", [1,1]))
nodes.append(node)
node = onnx.helper.make_node('Conv', inputs=['conv_2', 'weights_flat'], outputs=['conv_3'], name="Conv4")
node.attribute.append(onnx.helper.make_attribute("dilations", [1,1]))
node.attribute.append(onnx.helper.make_attribute("group", 1))
node.attribute.append(onnx.helper.make_attribute("pads", [0,0,0,0]))
node.attribute.append(onnx.helper.make_attribute("strides", [1,1]))
nodes.append(node)
node = onnx.helper.make_node('ReduceMin', inputs=['conv_3'], outputs=['min'], name="K")
node.attribute.append(onnx.helper.make_attribute("axes", [1, 2,3]))
node.attribute.append(onnx.helper.make_attribute("keepdims", 0))
nodes.append(node)
node = onnx.helper.make_node('Constant', inputs=[], outputs=['one'], value=onnx.helper.make_tensor(name='const_tensor', data_type=1, dims=[], vals=[1]))
nodes.append(node)
node = onnx.helper.make_node('Add', inputs=['min','one'], outputs=['k'])
nodes.append(node)
node = onnx.helper.make_node('Cast', inputs=['k'], outputs=['k_int'])
node.attribute.append(onnx.helper.make_attribute("to", onnx.TensorProto.INT64))
nodes.append(node)
node = onnx.helper.make_node('TopK', inputs=['conv_3', 'k_int'], outputs=['output_2', '_'])
node.attribute.append(onnx.helper.make_attribute("axis", 2))
node.attribute.append(onnx.helper.make_attribute("largest", 1))
nodes.append(node)
inputs = []
inputs.append(onnx.helper.make_tensor_value_info("input", onnx.TensorProto.FLOAT, [1,3,"width","height"]))
outputs = []
outputs.append(onnx.helper.make_tensor_value_info("output_1", onnx.TensorProto.FLOAT, [1,1,"width_padded","height_padded"]))
outputs.append(onnx.helper.make_tensor_value_info("output_2", onnx.TensorProto.FLOAT, [1,1,"unknown", "height_padded"]))
graph = onnx.helper.make_graph(nodes, "graph", inputs, outputs)
model = onnx.helper.make_model(graph, producer_name="", opset_imports=[onnx.helper.make_opsetid("", 11)])
onnx.checker.check_model(model)
onnx.save(model, "test_padding_and_topk.onnx")