I created a custom object detection model using the yoloV5 PyTorch version, so I tried to expose it as flask rest API, by referring to this pytorch guide
I trained and developing flask applications in the same python environment which installed PyTorch 1.7 cuda version
def transform_image(image_bytes):
my_transforms = transforms.Compose([transforms.Resize(255),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(
[0.485, 0.456, 0.406],
[0.229, 0.224, 0.225])])
image = Image.open(io.BytesIO(image_bytes)).convert('RGB')
return my_transforms(image).unsqueeze(0)
model = torch.load("weights/best.pt", map_location="cuda")['model']
model.eval()
with open("BikesHelmets128.png", 'rb') as f:
image_bytes = f.read()
tensor = transform_image(image_bytes=image_bytes)
print(tensor)
outputs = model.forward(tensor)
_, y_hat = outputs.max(1)
predicted_idx = str(y_hat.item())
print(predicted_idx)
Error stack
Traceback (most recent call last):
File "app.py", line 46, in <module>
outputs = model.forward(tensor)
File "C:\Users\D.ShaN\Documents\projects\python\fyp\helmet_covers_yolo\test\models\yolo.py", line 121, in forward
return self.forward_once(x, profile) # single-scale inference, train
File "C:\Users\D.ShaN\Documents\projects\python\fyp\helmet_covers_yolo\test\models\yolo.py", line 137, in forward_once
x = m(x) # run
File "C:\Users\D.ShaN\AppData\Local\conda\conda\envs\fyp\lib\site-packages\torch\nn\modules\module.py", line 727, in _call_impl
result = self.forward(*input, **kwargs)
File "C:\Users\D.ShaN\Documents\projects\python\fyp\helmet_covers_yolo\test\models\common.py", line 93, in forward
return self.conv(torch.cat([x[..., ::2, ::2], x[..., 1::2, ::2], x[..., ::2, 1::2], x[..., 1::2, 1::2]], 1))
File "C:\Users\D.ShaN\AppData\Local\conda\conda\envs\fyp\lib\site-packages\torch\nn\modules\module.py", line 727, in _call_impl
result = self.forward(*input, **kwargs)
File "C:\Users\D.ShaN\Documents\projects\python\fyp\helmet_covers_yolo\test\models\common.py", line 34, in forward
return self.act(self.bn(self.conv(x)))
File "C:\Users\D.ShaN\AppData\Local\conda\conda\envs\fyp\lib\site-packages\torch\nn\modules\module.py", line 727, in _call_impl
result = self.forward(*input, **kwargs)
File "C:\Users\D.ShaN\AppData\Local\conda\conda\envs\fyp\lib\site-packages\torch\nn\modules\conv.py", line 423, in forward
return self._conv_forward(input, self.weight)
File "C:\Users\D.ShaN\AppData\Local\conda\conda\envs\fyp\lib\site-packages\torch\nn\modules\conv.py", line 419, in _conv_forward
return F.conv2d(input, weight, self.bias, self.stride,
RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.cuda.HalfTensor) should be the same
I tried several ways by changing transforms, changing device type, and read images using open cv, etc.
thank you