minivision-ai / Silent-Face-Anti-Spoofing

静默活体检测(Silent-Face-Anti-Spoofing)
Apache License 2.0
1.36k stars 452 forks source link

conversion to caffe Error? #9

Closed PhenomenalOnee closed 4 years ago

PhenomenalOnee commented 4 years ago

Really nice. I was trying to convert the given models in caffe using https://github.com/xxradon/PytorchToCaffe The first model was successfully converted i.e 2.7_80x80_MiniFASNetV2.pth but for model 4_0_0_80x80_MiniFASNetV1SE.pth error occurs: conv1.conv conv: blob1 conv1 was added to layers 139799641391824:conv_blob1 was added to blobs conv1.bn batch_norm1 was added to layers 139799641391176:batch_norm_blob1 was added to blobs bn_scale1 was added to layers conv1.prelu prelu1 was added to layers 139799641391680:prelu_blob1 was added to blobs conv2_dw.conv conv: prelu_blob1 conv2 was added to layers 139799641392040:conv_blob2 was added to blobs conv2_dw.bn batch_norm2 was added to layers 139799641391752:batch_norm_blob2 was added to blobs bn_scale2 was added to layers conv2_dw.prelu prelu2 was added to layers 139799641392256:prelu_blob2 was added to blobs conv_23.conv.conv conv: prelu_blob2 conv3 was added to layers 139799641392616:conv_blob3 was added to blobs conv_23.conv.bn batch_norm3 was added to layers 139799641392976:batch_norm_blob3 was added to blobs bn_scale3 was added to layers conv_23.conv.prelu prelu3 was added to layers 139799641392184:prelu_blob3 was added to blobs conv_23.conv_dw.conv conv: prelu_blob3 conv4 was added to layers 139799641392400:conv_blob4 was added to blobs conv_23.conv_dw.bn batch_norm4 was added to layers 139799641391536:batch_norm_blob4 was added to blobs bn_scale4 was added to layers conv_23.conv_dw.prelu prelu4 was added to layers 139799641391320:prelu_blob4 was added to blobs conv_23.project.conv conv: prelu_blob4 conv5 was added to layers 139799641391608:conv_blob5 was added to blobs conv_23.project.bn batch_norm5 was added to layers 139799641392328:batch_norm_blob5 was added to blobs bn_scale5 was added to layers conv_3.model.0.conv.conv conv: batch_norm_blob5 conv6 was added to layers 139799641391464:conv_blob6 was added to blobs conv_3.model.0.conv.bn batch_norm6 was added to layers 139799641392760:batch_norm_blob6 was added to blobs bn_scale6 was added to layers conv_3.model.0.conv.prelu prelu5 was added to layers 139799641392904:prelu_blob5 was added to blobs conv_3.model.0.conv_dw.conv conv: prelu_blob5 conv7 was added to layers 139799641392112:conv_blob7 was added to blobs conv_3.model.0.conv_dw.bn batch_norm7 was added to layers 139799641391896:batch_norm_blob7 was added to blobs bn_scale7 was added to layers conv_3.model.0.conv_dw.prelu prelu6 was added to layers 139799641392472:prelu_blob6 was added to blobs conv_3.model.0.project.conv conv: prelu_blob6 conv8 was added to layers 139799641394992:conv_blob8 was added to blobs conv_3.model.0.project.bn batch_norm8 was added to layers 139799641394920:batch_norm_blob8 was added to blobs bn_scale8 was added to layers add1 was added to layers 139799641395064:add_blob1 was added to blobs conv_3.model.1.conv.conv conv: add_blob1 conv9 was added to layers 139799641394704:conv_blob9 was added to blobs conv_3.model.1.conv.bn batch_norm9 was added to layers 139799641394632:batch_norm_blob9 was added to blobs bn_scale9 was added to layers conv_3.model.1.conv.prelu prelu7 was added to layers 139799641394776:prelu_blob7 was added to blobs conv_3.model.1.conv_dw.conv conv: prelu_blob7 conv10 was added to layers 139799641394416:conv_blob10 was added to blobs conv_3.model.1.conv_dw.bn batch_norm10 was added to layers 139799641394344:batch_norm_blob10 was added to blobs bn_scale10 was added to layers conv_3.model.1.conv_dw.prelu prelu8 was added to layers 139799641394488:prelu_blob8 was added to blobs conv_3.model.1.project.conv conv: prelu_blob8 conv11 was added to layers 139799641394128:conv_blob11 was added to blobs conv_3.model.1.project.bn batch_norm11 was added to layers 139799641394056:batch_norm_blob11 was added to blobs bn_scale11 was added to layers add2 was added to layers 139799641394272:add_blob2 was added to blobs conv_3.model.2.conv.conv conv: add_blob2 conv12 was added to layers 139799641393840:conv_blob12 was added to blobs conv_3.model.2.conv.bn batch_norm12 was added to layers 139799641393768:batch_norm_blob12 was added to blobs bn_scale12 was added to layers conv_3.model.2.conv.prelu prelu9 was added to layers 139799641393984:prelu_blob9 was added to blobs conv_3.model.2.conv_dw.conv conv: prelu_blob9 conv13 was added to layers 139799641393552:conv_blob13 was added to blobs conv_3.model.2.conv_dw.bn batch_norm13 was added to layers 139799641393480:batch_norm_blob13 was added to blobs bn_scale13 was added to layers conv_3.model.2.conv_dw.prelu prelu10 was added to layers 139799641393696:prelu_blob10 was added to blobs conv_3.model.2.project.conv conv: prelu_blob10 conv14 was added to layers 139799641393264:conv_blob14 was added to blobs conv_3.model.2.project.bn batch_norm14 was added to layers 139799640977344:batch_norm_blob14 was added to blobs bn_scale14 was added to layers add3 was added to layers 139799641393408:add_blob3 was added to blobs conv_3.model.3.conv.conv conv: add_blob3 conv15 was added to layers 139799640976912:conv_blob15 was added to blobs conv_3.model.3.conv.bn batch_norm15 was added to layers 139799640976768:batch_norm_blob15 was added to blobs bn_scale15 was added to layers conv_3.model.3.conv.prelu prelu11 was added to layers 139799640977200:prelu_blob11 was added to blobs conv_3.model.3.conv_dw.conv conv: prelu_blob11 conv16 was added to layers 139799640976336:conv_blob16 was added to blobs conv_3.model.3.conv_dw.bn batch_norm16 was added to layers 139799640976192:batch_norm_blob16 was added to blobs bn_scale16 was added to layers conv_3.model.3.conv_dw.prelu prelu12 was added to layers 139799640976624:prelu_blob12 was added to blobs conv_3.model.3.project.conv conv: prelu_blob12 conv17 was added to layers 139799640975760:conv_blob17 was added to blobs conv_3.model.3.project.bn batch_norm17 was added to layers 139799640975616:batch_norm_blob17 was added to blobs bn_scale17 was added to layers conv_3.model.3.se_module.avg_pool ave_pool1 was added to layers 139799640975184:ave_pool_blob1 was added to blobs conv_3.model.3.se_module.fc1 conv: ave_pool_blob1 conv18 was added to layers 139799640976048:conv_blob18 was added to blobs conv_3.model.3.se_module.bn1 batch_norm18 was added to layers 139799640975040:batch_norm_blob18 was added to blobs bn_scale18 was added to layers conv_3.model.3.se_module.relu relu1 was added to layers 139799640975472:relu_blob1 was added to blobs conv_3.model.3.se_module.fc2 conv: relu_blob1 conv19 was added to layers 139799640974608:conv_blob19 was added to blobs conv_3.model.3.se_module.bn2 batch_norm19 was added to layers 139799640974464:batch_norm_blob19 was added to blobs bn_scale19 was added to layers mul1 was added to layers 139799640974032:mul_blob1 was added to blobs WARNING: CANNOT FOUND blob 139799640974896 Traceback (most recent call last): File "silent_spoof_convert_to_caffe.py", line 36, in pytorch_to_caffe.trans_net(net,input,name) File "./pytorch_to_caffe.py", line 786, in trans_net out = net.forward(input_var) File "/home/boson/WORKDONE/FaceSpoofing/Silent-Face-Anti-Spoofing/src/model_lib/MiniFASNet.py", line 222, in forward out = self.conv_3(out) File "/home/boson/Pytorch-cu9.2/venv/lib/python3.6/site-packages/torch/nn/modules/module.py", line 541, in call result = self.forward(*input, kwargs) File "/home/boson/WORKDONE/FaceSpoofing/Silent-Face-Anti-Spoofing/src/model_lib/MiniFASNet.py", line 134, in forward return self.model(x) File "/home/boson/Pytorch-cu9.2/venv/lib/python3.6/site-packages/torch/nn/modules/module.py", line 541, in call result = self.forward(*input, *kwargs) File "/home/boson/Pytorch-cu9.2/venv/lib/python3.6/site-packages/torch/nn/modules/container.py", line 92, in forward input = module(input) File "/home/boson/Pytorch-cu9.2/venv/lib/python3.6/site-packages/torch/nn/modules/module.py", line 541, in call result = self.forward(input, kwargs) File "/home/boson/WORKDONE/FaceSpoofing/Silent-Face-Anti-Spoofing/src/model_lib/MiniFASNet.py", line 156, in forward x = self.se_module(x) File "/home/boson/Pytorch-cu9.2/venv/lib/python3.6/site-packages/torch/nn/modules/module.py", line 541, in call result = self.forward(*input, *kwargs) File "/home/boson/WORKDONE/FaceSpoofing/Silent-Face-Anti-Spoofing/src/model_lib/MiniFASNet.py", line 113, in forward return module_input x File "./pytorch_to_caffe.py", line 583, in _mul bottom=[log.blobs(input), log.blobs(args[0])], top=top_blobs) File "./Caffe/layer_param.py", line 33, in init self.bottom.extend(bottom) TypeError: None has type NoneType, but expected one of: bytes, unicode

Can you help?

zhuyingSeu commented 4 years ago

It seems that the se_module is not supported in the PytorchToCaffe project. We will upload the caffemodel.

PhenomenalOnee commented 4 years ago

Thank You. Hope we can get it soon. In the meantime , can you tell me how you convert the models into caffe, using which github repo or tool?

zhangw864680355 commented 4 years ago

@PhenomenalOnee how do you convert 2.7_80x80_MiniFASNetV2.pth to caffe ? I try to do it, but fails. In fact, if you set input image size more than (80, 80), it will sucess. how do set the input size? thers are my codes as follows (torch1.2.0+cpu):

import sys sys.path.insert(0, '.') import torch import pytorch_to_caffe from MiniFASNet import MiniFASNetV2, MiniFASNetV1SE

if name == 'main': name = 'MiniFASNet' net= MiniFASNetV2() checkpoint = torch.load("2.7_80x80_MiniFASNetV2.pth", map_location='cpu')

# net= MiniFASNetV1SE()
# checkpoint = torch.load("4_0_0_80x80_MiniFASNetV1SE.pth", map_location='cpu')
net.load_state_dict(checkpoint, strict=False)
net.eval()
input = torch.ones([1, 3, 80, 80])
pytorch_to_caffe.trans_net(net, input, name)
pytorch_to_caffe.save_prototxt('{}.prototxt'.format(name))
pytorch_to_caffe.save_caffemodel('{}.caffemodel'.format(name))
PhenomenalOnee commented 4 years ago

Well i am using the net returned by the actual repo file: anti_spoof_predict.py. So I prefer you to see that file. In the file, the model loading line is: self.model = MODEL_MAPPING [model_type] (conv6_kernel=self.kernel_size).to(self.device)

The difference i can see in your model loading is that you need to provide the kernel_size in MiniFASNetV1SE() function call which is calculated by using input image size ( you can see that in the anti_spoof_file.py too). for size 80x80, kernel_size is (5,5). So try to pass it in you calling function. And remove strict=False in torch.load as it will not through any error if there some mismatch between you net and given weight dict.

zhangw864680355 commented 4 years ago

@PhenomenalOnee Thanks! Converting successfully 2.7_80x80_MiniFASNetV2.pth to caffemodel according to your advices. It is trying to convert other one with other method.

PhenomenalOnee commented 4 years ago

@zhangwei147258 Hope you can convert the other model and please share the result