@@ -673,3 +673,152 @@ def build_conv_batchnorm_sig_mul_model():
673673 onnx .checker .check_model (model_inferred )
674674
675675 return model_inferred
676+
677+
678+ def build_conv_act_pool_model (include_reshape_node = False ):
679+ # Define your model inputs and outputs
680+ input_names = ["input_0" ]
681+ output_names = ["output_0" ]
682+ input_shapes = [(32 , 64 , 256 , 256 )]
683+ output_shapes = [(32 , 128 , 128 , 128 )]
684+
685+ inputs = [
686+ helper .make_tensor_value_info (input_name , onnx .TensorProto .FLOAT , input_shape )
687+ for input_name , input_shape in zip (input_names , input_shapes )
688+ ]
689+ outputs = [
690+ helper .make_tensor_value_info (output_name , onnx .TensorProto .FLOAT , output_shape )
691+ for output_name , output_shape in zip (output_names , output_shapes )
692+ ]
693+
694+ # Create the ONNX graph with the nodes
695+ nodes = [
696+ helper .make_node (
697+ op_type = "Conv" ,
698+ inputs = ["input_0" , "weights_1" , "bias_1" ],
699+ outputs = ["conv1_conv/Conv2D:0" ],
700+ name = "conv1_conv/Conv2D" ,
701+ dilations = [1 , 1 ],
702+ group = 1 ,
703+ kernel_shape = [3 , 3 ],
704+ pads = [1 , 1 , 1 , 1 ],
705+ strides = [1 , 1 ],
706+ ),
707+ helper .make_node (
708+ op_type = "BatchNormalization" ,
709+ inputs = ["conv1_conv/Conv2D:0" , "bn1_scale" , "bn1_bias" , "bn1_mean" , "bn1_var" ],
710+ outputs = ["bn1_batchnorm/BatchNormalization:0" ],
711+ name = "bn1_batchnorm/BatchNormalization" ,
712+ ),
713+ helper .make_node (
714+ op_type = "Relu" ,
715+ inputs = ["bn1_batchnorm/BatchNormalization:0" ],
716+ outputs = ["relu1_relu/Relu:0" ],
717+ name = "relu1_relu/Relu" ,
718+ ),
719+ ]
720+ if include_reshape_node :
721+ nodes .append (
722+ helper .make_node (
723+ op_type = "Reshape" ,
724+ inputs = ["relu1_relu/Relu:0" , "shape_1" ],
725+ outputs = ["reshape1_reshape/Reshape:0" ],
726+ name = "reshape1_reshape/Reshape" ,
727+ ),
728+ )
729+ nodes .extend (
730+ [
731+ helper .make_node (
732+ op_type = "MaxPool" ,
733+ inputs = [
734+ "reshape1_reshape/Reshape:0" if include_reshape_node else "relu1_relu/Relu:0"
735+ ],
736+ outputs = ["maxpool1_maxpool/MaxPool2D:0" ],
737+ name = "maxpool1_maxpool/MaxPool2D" ,
738+ ceil_mode = False ,
739+ kernel_shape = [3 , 3 ],
740+ pads = [1 , 1 , 1 , 1 ],
741+ strides = [2 , 2 ],
742+ ),
743+ helper .make_node (
744+ op_type = "Conv" ,
745+ inputs = ["maxpool1_maxpool/MaxPool2D:0" , "weights_2" ],
746+ outputs = ["output_0" ],
747+ name = "conv2_conv/Conv2D" ,
748+ dilations = [1 , 1 ],
749+ group = 1 ,
750+ kernel_shape = [3 , 3 ],
751+ pads = [1 , 1 , 1 , 1 ],
752+ strides = [1 , 1 ],
753+ ),
754+ ]
755+ )
756+
757+ # Create the ONNX initializers
758+ initializers = [
759+ helper .make_tensor (
760+ name = "weights_1" ,
761+ data_type = onnx .TensorProto .FLOAT ,
762+ dims = (128 , 64 , 3 , 3 ),
763+ vals = np .random .uniform (low = 0.5 , high = 1.0 , size = 128 * 64 * 3 * 3 ),
764+ ),
765+ helper .make_tensor (
766+ name = "bias_1" ,
767+ data_type = onnx .TensorProto .FLOAT ,
768+ dims = (128 ,),
769+ vals = np .random .uniform (low = 0.5 , high = 1.0 , size = 128 ),
770+ ),
771+ helper .make_tensor (
772+ name = "bn1_scale" ,
773+ data_type = onnx .TensorProto .FLOAT ,
774+ dims = (128 ,),
775+ vals = np .random .uniform (low = 0.5 , high = 1.0 , size = 128 ),
776+ ),
777+ helper .make_tensor (
778+ name = "bn1_bias" ,
779+ data_type = onnx .TensorProto .FLOAT ,
780+ dims = (128 ,),
781+ vals = np .random .uniform (low = 0.5 , high = 1.0 , size = 128 ),
782+ ),
783+ helper .make_tensor (
784+ name = "bn1_mean" ,
785+ data_type = onnx .TensorProto .FLOAT ,
786+ dims = (128 ,),
787+ vals = np .random .uniform (low = 0.5 , high = 1.0 , size = 128 ),
788+ ),
789+ helper .make_tensor (
790+ name = "bn1_var" ,
791+ data_type = onnx .TensorProto .FLOAT ,
792+ dims = (128 ,),
793+ vals = np .random .uniform (low = 0.5 , high = 1.0 , size = 128 ),
794+ ),
795+ helper .make_tensor (
796+ name = "weights_2" ,
797+ data_type = onnx .TensorProto .FLOAT ,
798+ dims = (128 , 128 , 3 , 3 ),
799+ vals = np .random .uniform (low = 0.5 , high = 1.0 , size = 128 * 128 * 3 * 3 ),
800+ ),
801+ ]
802+ if include_reshape_node :
803+ initializers .append (
804+ helper .make_tensor (
805+ name = "shape_1" ,
806+ data_type = onnx .TensorProto .INT64 ,
807+ dims = (4 ,),
808+ vals = (32 , 128 , 256 , 256 ),
809+ ),
810+ )
811+
812+ # Create the ONNX graph with the nodes and initializers
813+ graph = helper .make_graph (nodes , "conv_act_pool" , inputs , outputs , initializer = initializers )
814+
815+ # Create the ONNX model
816+ model = helper .make_model (graph )
817+ model .opset_import [0 ].version = 13
818+ model .ir_version = 10
819+
820+ # Check the ONNX model
821+ model_inferred = onnx .shape_inference .infer_shapes (model )
822+ onnx .checker .check_model (model_inferred )
823+
824+ return model_inferred
0 commit comments