@@ -87,9 +87,9 @@ nvinfer1::IHostMemory* buildEngineYolo26Det(nvinfer1::IBuilder* builder, nvinfer
8787 C3K2 (network, weightMap, *block7->getOutput (0 ), get_width (1024 , gw, max_channels),
8888 get_width (1024 , gw, max_channels), get_depth (2 , gd), true , true , false , 0.5 , " model.8" );
8989
90- nvinfer1::IElementWiseLayer* block9 = SPPF (network, weightMap, *block8-> getOutput ( 0 ),
91- get_width ( 1024 , gw, max_channels ), get_width (1024 , gw, max_channels), 5 ,
92- true , " model.9" ); // TODO: VERIFY THIS BLOCK FOR OTHER YOLO26 MODELS
90+ nvinfer1::IElementWiseLayer* block9 =
91+ SPPF (network, weightMap, *block8-> getOutput ( 0 ), get_width (1024 , gw, max_channels),
92+ get_width ( 1024 , gw, max_channels), 5 , true , " model.9" );
9393
9494 nvinfer1::IElementWiseLayer* block10 =
9595 C2PSA (network, weightMap, *block9->getOutput (0 ), get_width (1024 , gw, max_channels),
@@ -869,4 +869,112 @@ nvinfer1::IHostMemory* buildEngineYolo26Obb(nvinfer1::IBuilder* builder, nvinfer
869869 free ((void *)(mem.second .values ));
870870 }
871871 return serialized_model;
872- }
872+ }
873+
874+ nvinfer1::IHostMemory* buildEngineYolo26Cls (nvinfer1::IBuilder* builder, nvinfer1::IBuilderConfig* config,
875+ nvinfer1::DataType dt, const std::string& wts_path, float & gd, float & gw,
876+ int & max_channels, std::string& type) {
877+ std::map<std::string, nvinfer1::Weights> weightMap = loadWeights (wts_path);
878+
879+ nvinfer1::INetworkDefinition* network = builder->createNetworkV2 (
880+ 1U << static_cast <uint32_t >(nvinfer1::NetworkDefinitionCreationFlag::kEXPLICIT_BATCH ));
881+
882+ /* ******************************************************************************************************
883+ ****************************************** YOLO26 INPUT **********************************************
884+ *******************************************************************************************************/
885+
886+ nvinfer1::ITensor* data =
887+ network->addInput (kInputTensorName , dt, nvinfer1::Dims4{kBatchSize , 3 , kClsInputH , kClsInputW });
888+ assert (data);
889+
890+ /* ******************************************************************************************************
891+ ***************************************** YOLO26 BACKBONE ********************************************
892+ *******************************************************************************************************/
893+
894+ nvinfer1::IElementWiseLayer* block0 =
895+ convBnSiLU (network, weightMap, *data, get_width (64 , gw, max_channels), {3 , 3 }, 2 , " model.0" );
896+
897+ nvinfer1::IElementWiseLayer* block1 = convBnSiLU (network, weightMap, *block0->getOutput (0 ),
898+ get_width (128 , gw, max_channels), {3 , 3 }, 2 , " model.1" );
899+
900+ bool c3k = false ;
901+ if (type == " m" || type == " l" || type == " x" ) {
902+ c3k = true ;
903+ }
904+
905+ nvinfer1::IElementWiseLayer* conv2 =
906+ C3K2 (network, weightMap, *block1->getOutput (0 ), get_width (128 , gw, max_channels),
907+ get_width (256 , gw, max_channels), get_depth (2 , gd), c3k, true , false , 0.25 , " model.2" );
908+
909+ nvinfer1::IElementWiseLayer* block3 = convBnSiLU (network, weightMap, *conv2->getOutput (0 ),
910+ get_width (256 , gw, max_channels), {3 , 3 }, 2 , " model.3" );
911+
912+ nvinfer1::IElementWiseLayer* block4 =
913+ C3K2 (network, weightMap, *block3->getOutput (0 ), get_width (256 , gw, max_channels),
914+ get_width (512 , gw, max_channels), get_depth (2 , gd), c3k, true , false , 0.25 , " model.4" );
915+
916+ nvinfer1::IElementWiseLayer* block5 = convBnSiLU (network, weightMap, *block4->getOutput (0 ),
917+ get_width (512 , gw, max_channels), {3 , 3 }, 2 , " model.5" );
918+
919+ nvinfer1::IElementWiseLayer* block6 =
920+ C3K2 (network, weightMap, *block5->getOutput (0 ), get_width (512 , gw, max_channels),
921+ get_width (512 , gw, max_channels), get_depth (2 , gd), true , true , false , 0.5 , " model.6" );
922+
923+ nvinfer1::IElementWiseLayer* block7 = convBnSiLU (network, weightMap, *block6->getOutput (0 ),
924+ get_width (1024 , gw, max_channels), {3 , 3 }, 2 , " model.7" );
925+
926+ nvinfer1::IElementWiseLayer* block8 =
927+ C3K2 (network, weightMap, *block7->getOutput (0 ), get_width (1024 , gw, max_channels),
928+ get_width (1024 , gw, max_channels), get_depth (2 , gd), true , true , false , 0.5 , " model.8" );
929+
930+ nvinfer1::IElementWiseLayer* block9 =
931+ C2PSA (network, weightMap, *block8->getOutput (0 ), get_width (1024 , gw, max_channels),
932+ get_width (1024 , gw, max_channels), get_depth (2 , gd), 0.5 , " model.9" );
933+
934+ // ///////////////////////////////////////////////////
935+
936+ nvinfer1::IElementWiseLayer* block10_convbn =
937+ convBnSiLU (network, weightMap, *block9->getOutput (0 ), 1280 , {1 , 1 }, 1 , " model.10.conv" );
938+ nvinfer1::Dims dims =
939+ block10_convbn->getOutput (0 )->getDimensions (); // Obtain the dimensions of the output of conv_class
940+ assert (dims.nbDims == 4 );
941+ nvinfer1::IPoolingLayer* block10_pool = network->addPoolingNd (
942+ *block10_convbn->getOutput (0 ), nvinfer1::PoolingType::kAVERAGE , nvinfer1::DimsHW{dims.d [2 ], dims.d [3 ]});
943+ nvinfer1::IShuffleLayer* block10_reshape = network->addShuffle (*block10_pool->getOutput (0 ));
944+ block10_reshape->setReshapeDimensions (nvinfer1::Dims2{kBatchSize , 1280 });
945+ nvinfer1::IConstantLayer* block10_linear_weight =
946+ network->addConstant (nvinfer1::Dims2{kClsNumClass , 1280 }, weightMap[" model.10.linear.weight" ]);
947+ nvinfer1::IConstantLayer* block10_linear_bias =
948+ network->addConstant (nvinfer1::Dims2{kClsNumClass , 1 }, weightMap[" model.10.linear.bias" ]);
949+ nvinfer1::IMatrixMultiplyLayer* block10_linear_matrix_multiply =
950+ network->addMatrixMultiply (*block10_reshape->getOutput (0 ), nvinfer1::MatrixOperation::kNONE ,
951+ *block10_linear_weight->getOutput (0 ), nvinfer1::MatrixOperation::kTRANSPOSE );
952+ nvinfer1::IElementWiseLayer* block10_linear_add =
953+ network->addElementWise (*block10_linear_matrix_multiply->getOutput (0 ), *block10_linear_bias->getOutput (0 ),
954+ nvinfer1::ElementWiseOperation::kSUM );
955+ nvinfer1::IActivationLayer* output =
956+ network->addActivation (*block10_linear_add->getOutput (0 ), nvinfer1::ActivationType::kSIGMOID );
957+ assert (output);
958+
959+ output->getOutput (0 )->setName (kOutputTensorName );
960+ network->markOutput (*output->getOutput (0 ));
961+ // Use setMemoryPoolLimit instead of deprecated setMaxWorkspaceSize
962+ config->setMemoryPoolLimit (nvinfer1::MemoryPoolType::kWORKSPACE , 16 * (1 << 20 ));
963+
964+ #if defined(USE_FP16)
965+ config->setFlag (nvinfer1::BuilderFlag::kFP16 );
966+ #elif defined(USE_INT8)
967+ std::cerr << " INT8 not supported for YOLO26 model yet." << std::endl;
968+ #endif
969+
970+ std::cout << " Building engine, please wait for a while..." << std::endl;
971+ nvinfer1::IHostMemory* serialized_model = builder->buildSerializedNetwork (*network, *config);
972+ std::cout << " Build engine successfully!" << std::endl;
973+
974+ delete network;
975+
976+ for (auto & mem : weightMap) {
977+ free ((void *)(mem.second .values ));
978+ }
979+ return serialized_model;
980+ }
0 commit comments