You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have read the README carefully. 我已经仔细阅读了README上的操作指引。
I want to train my custom dataset, and I have read the tutorials for training your custom data carefully and organize my dataset correctly; (FYI: We recommand you to apply the config files of xx_finetune.py.) 我想训练自定义数据集,我已经仔细阅读了训练自定义数据的教程,以及按照正确的目录结构存放数据集。(FYI: 我们推荐使用xx_finetune.py等配置文件训练自定义数据集。)
I have pulled the latest code of main branch to run again and the problem still existed. 我已经拉取了主分支上最新的代码,重新运行之后,问题仍不能解决。
Search before asking
I have searched the YOLOv6 issues and found no similar questions.
Question
I trained the yolov6s_finetune.py model with a custom dataset and obtained the .pt and .onnx files. Now I want convert it in .mlmodel for using in xcode (iOs app). but, I need inizalice the model in a python file and I don't know how do this.
currently, Coremltools support PyTorch model file to convertion with this code:
PyTorch Conversion Workflow from coreml documetation
importtorchimporttorchvision# Load a pre-trained version of MobileNetV2torch_model=torchvision.models.mobilenet_v2(pretrained=True) #This is the model inicialization# Set the model in evaluation mode.torch_model.eval()
# Trace the model with random data.example_input=torch.rand(1, 3, 224, 224)
traced_model=torch.jit.trace(torch_model, example_input)
out=traced_model(example_input)
importcoremltoolsasct# Using image_input in the inputs parameter:# Convert to Core ML program using the Unified Conversion API.model=ct.convert(
traced_model,
convert_to="mlprogram",
inputs=[ct.TensorType(shape=example_input.shape)]
)
model.save("newmodel.mlpackage")
the main problen is the load of pytorch model. I tried to replicate the code but it doesn't work.
importtorchimporttorchvision.transformsastransformsimportcv2importcoremltoolsasctfromyolov6.utils.checkpointimportload_state_dict, load_checkpointfromyolov6.utils.eventsimportLOGGER, load_yamlfromyolov6.layers.commonimportDetectBackendfromyolov6.utils.nmsimportnon_max_suppressionfromyolov6.core.infererimportInfererweights='Model1.pt'yaml='data/dataset_Custom.yaml'image=cv2.imread('image2.jpeg')
#image = cv2.resize(image, (640,640))transform=transforms.Compose([ transforms.ToTensor()])
img_tensor=transform(image) #3D tensorimg_tensor=img_tensor.unsqueeze(0)# 4D tensor #torch.Size([1, 3, 640, 640])#---Init modeldevice=torch.device("cuda:0"iftorch.cuda.is_available() else"cpu")
model=DetectBackend(weights, device=device) #####<<---- attempt to initialize the modelclass_labels=load_yaml(yaml)['names']
defmodel_switch(model):
# Model switch to deploy status fromyolov6.layers.commonimportRepVGGBlockforlayerinmodel.modules():
ifisinstance(layer, RepVGGBlock):
layer.switch_to_deploy()
elifisinstance(layer, torch.nn.Upsample) andnothasattr(layer, 'recompute_scale_factor'):
layer.recompute_scale_factor=None# torch 1.11.0 compatibilityLOGGER.info("Switch model to deploy modality.")
model_switch(model.model)
model.model.float() #cpumodel.eval()
model(image) #### error in this line ###traced_model=torch.jit.trace(model, img_tensor)
#---Convert to coremlscale=1/(0.226*255.0)
bias= [-0.485/(0.229) , -0.456/(0.224), -0.406/(0.225)]
image_input=ct.ImageType(shape=img_tensor.shape,
scale=scale, bias=bias)
ml_model=ct.convert(traced_model,source="pytorch",inputs=[image_input],
classifier_config=ct.ClassifierConfig(class_labels),convert_to='neuralnetwork')
ml_model.save("alphabetModel_image.mlmodel")
The error shown is the following:
Loading checkpoint from Model1.pt
Fusing model...
Switch model to deploy modality.
Traceback (most recent call last):
File "/Users/.../YOLOv6/convert_to_coreml.py", line 45, in<module>
model(image)
File "/Users/.../Library/Python/3.9/lib/python/site-packages/torch/nn/modules/module.py", line 1511, in _wrapped_call_impl
return self._call_impl(*args, **kwargs)
File "/Users/../Library/Python/3.9/lib/python/site-packages/torch/nn/modules/module.py", line 1520, in _call_impl
return forward_call(*args, **kwargs)
File "/Users/.../Desktop/YOLOv6/yolov6/layers/common.py", line 563, in forward
y, _ = self.model(im)
File "/Users/.../Library/Python/3.9/lib/python/site-packages/torch/nn/modules/module.py", line 1511, in _wrapped_call_impl
return self._call_impl(*args, **kwargs)
File "/Users/.../Library/Python/3.9/lib/python/site-packages/torch/nn/modules/module.py", line 1520, in _call_impl
return forward_call(*args, **kwargs)
File "/Users/.../Desktop/YOLOv6/yolov6/models/yolo.py", line 35, in forward
x = self.backbone(x)
File "/Users/.../Library/Python/3.9/lib/python/site-packages/torch/nn/modules/module.py", line 1511, in _wrapped_call_impl
return self._call_impl(*args, **kwargs)
File "/Users/.../Library/Python/3.9/lib/python/site-packages/torch/nn/modules/module.py", line 1520, in _call_impl
return forward_call(*args, **kwargs)
File "/Users/.../Desktop/YOLOv6/yolov6/models/efficientrep.py", line 107, in forward
x = self.stem(x)
File "/Users/.../Library/Python/3.9/lib/python/site-packages/torch/nn/modules/module.py", line 1511, in _wrapped_call_impl
return self._call_impl(*args, **kwargs)
File "/Users/.../Library/Python/3.9/lib/python/site-packages/torch/nn/modules/module.py", line 1520, in _call_impl
return forward_call(*args, **kwargs)
File "/Users/.../Desktop/YOLOv6/yolov6/layers/common.py", line 248, in forward
return self.nonlinearity(self.se(self.rbr_reparam(inputs)))
File "/Users/.../Library/Python/3.9/lib/python/site-packages/torch/nn/modules/module.py", line 1511, in _wrapped_call_impl
return self._call_impl(*args, **kwargs)
File "/Users/.../Library/Python/3.9/lib/python/site-packages/torch/nn/modules/module.py", line 1520, in _call_impl
return forward_call(*args, **kwargs)
File "/Users/.../Library/Python/3.9/lib/python/site-packages/torch/nn/modules/conv.py", line 460, in forward
return self._conv_forward(input, self.weight, self.bias)
File "/Users/.../Library/Python/3.9/lib/python/site-packages/torch/nn/modules/conv.py", line 456, in _conv_forward
return F.conv2d(input, weight, bias, self.stride,
TypeError: conv2d() received an invalid combination of arguments - got (numpy.ndarray, Parameter, Parameter, tuple, tuple, tuple, int), but expected one of:
* (Tensor input, Tensor weight, Tensor bias, tuple of ints stride, tuple of ints padding, tuple of ints dilation, int groups)
didn't match because some of the arguments have invalid types: (numpy.ndarray, Parameter, Parameter, tuple of (int, int), tuple of (int, int), tuple of (int, int), int) * (Tensor input, Tensor weight, Tensor bias, tuple of ints stride, str padding, tuple of ints dilation, int groups) didn't match because some of the arguments have invalid types: (numpy.ndarray, Parameter, Parameter, tuple of (int, int), tuple of (int, int), tuple of (int, int), int)
I don't know what I'm doing wrong... I tried to initialize the model as done in infer.py or eval.py but it seems I'm not doing it right.
Could someone help me figure out what I'm doing wrong or if there's an easier way to do it? Any help is really appreciated, thanks.
Additional
No response
The text was updated successfully, but these errors were encountered:
Before Asking
I have read the README carefully. 我已经仔细阅读了README上的操作指引。
I want to train my custom dataset, and I have read the tutorials for training your custom data carefully and organize my dataset correctly; (FYI: We recommand you to apply the config files of xx_finetune.py.) 我想训练自定义数据集,我已经仔细阅读了训练自定义数据的教程,以及按照正确的目录结构存放数据集。(FYI: 我们推荐使用xx_finetune.py等配置文件训练自定义数据集。)
I have pulled the latest code of main branch to run again and the problem still existed. 我已经拉取了主分支上最新的代码,重新运行之后,问题仍不能解决。
Search before asking
Question
I trained the yolov6s_finetune.py model with a custom dataset and obtained the .pt and .onnx files. Now I want convert it in .mlmodel for using in xcode (iOs app). but, I need inizalice the model in a python file and I don't know how do this.
currently, Coremltools support PyTorch model file to convertion with this code:
PyTorch Conversion Workflow from coreml documetation
the main problen is the load of pytorch model. I tried to replicate the code but it doesn't work.
The error shown is the following:
I don't know what I'm doing wrong... I tried to initialize the model as done in infer.py or eval.py but it seems I'm not doing it right.
Could someone help me figure out what I'm doing wrong or if there's an easier way to do it? Any help is really appreciated, thanks.
Additional
No response
The text was updated successfully, but these errors were encountered: