Tencent / ncnn

ncnn is a high-performance neural network inference framework optimized for the mobile platform
Other
20.14k stars 4.14k forks source link

Whether ncnn supports Resnext101 net or not #2792

Open ciwei123 opened 3 years ago

ciwei123 commented 3 years ago

@nihui Thanks for your sharing. I converted the torchvision.models.resnext101_32x8dto ONNX and then to NCNN. This process did not report any errors,but I used the NCNN model to get extremely large numbers(for example,1386897268249205487435776). And I tried torchvision.models.resnet50 with the same process and it worked. Could you tell me the reason?Thank you very much!!

nihui commented 3 years ago

ncnn shall support Resnext101

this faq may help https://github.com/Tencent/ncnn/wiki/FAQ-ncnn-produce-wrong-result

ciwei123 commented 3 years ago

ncnn shall support Resnext101

this faq may help https://github.com/Tencent/ncnn/wiki/FAQ-ncnn-produce-wrong-result

@nihui Thanks for your reply. I read the link you said, but it didn't work. Could you replay the process. And my process is very easy : #pytorch to onnx

import torch
import torchvision

**_#define resnext101 model_**
model = torchvision.models.resnext101_32x8d(pretrained=True)
#define input shape
x = torch.rand(1, 3, 224, 224)
#define input and output nodes, can be customized
input_names = ["x"]
output_names = ["y"]
#convert pytorch to onnx
torch_out = torch.onnx.export(model, x, "resnext101_32x8d.onnx", input_names=input_names, output_names=output_names)

#onnx to ncnn onnx2ncnn resnext101_32x8d.onnx resnext101_32x8d.param resnext101_32x8d.bin

#ncnn read an image

open = getAssets().open(temp);
open_image = getAssets().open(temp);
Bitmap bitmap = BitmapFactory.decodeStream(open_image);

ncnn::Mat in = ncnn::Mat::from_android_bitmap_resize(env, bitmap_in, ncnn::Mat::PIXEL_RGB, 224, 224);
ncnn::Extractor ex = srnet.create_extractor();
ex.set_vulkan_compute(use_gpu);
ex.input("x", in);
ex.extract ("y", out);
    std::vector<float> res;
    for (int q = 0; q<out.c; q++)
    {
        const float* ptr = out.channel(q);
        for (int y = 0; y<out.h; y++)
        {
            for (int x = 0; x<out.w; x++)
            {
                res.push_back(ptr[x]);
                __android_log_print(ANDROID_LOG_DEBUG, "QoE out0:","%d,%d,%d  %f ", q,y,x,ptr[x]);
            }
            ptr += out.w;
        }
    }

    std::vector<float>::iterator max_id = std::max_element(res.begin(), res.end());
    __android_log_print(ANDROID_LOG_DEBUG,"predicted class:","%d , predicted value: %f", std::distance(std::begin(res), max_id),*max_id);

Thank you very much!!

nihui commented 1 month ago

针对onnx模型转换的各种问题,推荐使用最新的pnnx工具转换到ncnn In view of various problems in onnx model conversion, it is recommended to use the latest pnnx tool to convert your model to ncnn

pip install pnnx
pnnx model.onnx inputshape=[1,3,224,224]

详细参考文档 Detailed reference documentation https://github.com/pnnx/pnnx https://github.com/Tencent/ncnn/wiki/use-ncnn-with-pytorch-or-onnx#how-to-use-pnnx