Yolact: Calling Yolact from another Python file

Created on 18 Dec 2019  Â·  12Comments  Â·  Source: dbolya/yolact

Hi @dbolya,
Thanks again for such a useful model.
I am trying to use yolact inside another file and I will use its outputs in terms of masks, bounding-boxes, confidence rates and classes.
But it gives errors when I import yolact.eval --> ImportError: cannot import name 'Yolact'
If I import yolact, there is no error. If I from yolact import eval --> ImportError: cannot import name 'Yolact', it gives the same error.
After import yolact without no errors, I type yolact and it gives <module 'yolact' (namespace)>. But if I import another lib, it shows the path to that.
How can I call eval.py from another python file and use its outputs?
By the way my config is python 3.6, pytorch 1.0.1, cuda 9.0...

Most helpful comment

What does your final structure look like? It sounds like you're importing yolact.py not importing from the yolact directory. Though that's actually what you want (importing yolact.py, that is).

Here's the rough procedure:

import torch
import cv2 # Optional, see below

from yolact import Yolact
from data import set_cfg
from utils.augmentations import FastBaseTransform
from layers.output_utils import postprocess

##### Setup #####

# CUDA setup, I should really just replace all Tensor initializations but I'm in too deep at this point
torch.backends.cudnn.fastest = True
torch.set_default_tensor_type('torch.cuda.FloatTensor')

# Use whichever config you want here.
set_cfg('yolact_base_config')

net = Yolact().cuda()
net.load_weights('path/to/weight/file')
net.eval()

transform = FastBaseTransform()

##### Now to use ######

# Get your image from somewhere (BGR format, [0, 255], numpy)
img = cv2.imread('path/to/image')
h, w, _ = img.shape

batch = transform(torch.from_numpy(img).cuda().float()[None, ...])
preds = net(batch)

# You can modify the score threshold to your liking
classes, scores, boxes, masks = postprocess(preds , w, h, score_threshold=0.15)

Sorry for the number of lines of code necessary to do this. I should probably make an example script and put it on the readme or something. (Also this code is untested, if there are any bugs lmk).

All 12 comments

@bayraktare hi this is good thought , just wanted to understand are you trying to import yolact in another pipeline of python or are you calling it for c++

What does your final structure look like? It sounds like you're importing yolact.py not importing from the yolact directory. Though that's actually what you want (importing yolact.py, that is).

Here's the rough procedure:

import torch
import cv2 # Optional, see below

from yolact import Yolact
from data import set_cfg
from utils.augmentations import FastBaseTransform
from layers.output_utils import postprocess

##### Setup #####

# CUDA setup, I should really just replace all Tensor initializations but I'm in too deep at this point
torch.backends.cudnn.fastest = True
torch.set_default_tensor_type('torch.cuda.FloatTensor')

# Use whichever config you want here.
set_cfg('yolact_base_config')

net = Yolact().cuda()
net.load_weights('path/to/weight/file')
net.eval()

transform = FastBaseTransform()

##### Now to use ######

# Get your image from somewhere (BGR format, [0, 255], numpy)
img = cv2.imread('path/to/image')
h, w, _ = img.shape

batch = transform(torch.from_numpy(img).cuda().float()[None, ...])
preds = net(batch)

# You can modify the score threshold to your liking
classes, scores, boxes, masks = postprocess(preds , w, h, score_threshold=0.15)

Sorry for the number of lines of code necessary to do this. I should probably make an example script and put it on the readme or something. (Also this code is untested, if there are any bugs lmk).

hi @abhigoku10 yes i am trying to import it in another python pipeline.
thanks for the reply @dbolya
Actually, I am trying to get the outputs in terms of masks, bounding-boxes, confidence rates and classes as I can get them from eval.py
i will let you know right after testing this after modifying to get those outputs.

Hey @dbolya, I have run the code and the first thing I figured out is that only yolact_base_54_800000 and yolact_im700_54_800000 are running without errors as weights.
The second thing with this code appears at the line preds = net(batch) as you can see in the following i am copy-pasting:
Traceback (most recent call last): File "demo.py", line 45, in <module> preds = net(batch) File "/home/bayraktare/.local/lib/python3.6/site-packages/torch/nn/modules/module.py", line 489, in __call__ result = self.forward(*input, **kwargs) File ".../yolact/yolact.py", line 705, in forward return self.detect(pred_outs) File ".../yolact/layers/functions/detection.py", line 71, in __call__ result = self.detect(batch_idx, conf_preds, decoded_boxes, mask_data, inst_data) File ".../yolact/layers/functions/detection.py", line 103, in detect boxes, masks, classes, scores = self.traditional_nms(boxes, masks, scores, self.nms_thresh, self.conf_thresh) File ".../yolact/layers/functions/detection.py", line 208, in traditional_nms preds = torch.cat([boxes[conf_mask], cls_scores[:, None]], dim=1).cpu().numpy() RuntimeError: Can't call numpy() on Variable that requires grad. Use var.detach().numpy() instead.

I have checked the batch and it is a tensor with appropriate values.
On the other hand, I modified this code by changing the last part after transform = FastBaseTransform() with the def evalimage() as follows:

path = '/path/to/image.png'
frame = torch.from_numpy(cv2.imread(path)).cuda().float()
batch = FastBaseTransform()(frame.unsqueeze(0))
preds = net(batch)
imname = path
img_numpy = prep_display(preds, frame, imname, None, None, undo_transform=False)
plt.imshow(img_numpy)
plt.title(path)
plt.show()

But it also gave the same error as before.

Edit:
I think the previous error can be solved by adding with torch.no_grad(): before the code.
But this time, classes, scores, boxes, masks = postprocess(preds , w, h, score_threshold=0.15) yields a problem as follows:

Traceback (most recent call last):
File "demo.py", line 89, in
classes, scores, boxes, masks = postprocess(preds , w, h, score_threshold=0.15)
File ".../yolact/layers/output_utils.py", line 61, in postprocess
if cfg.mask_proto_debug:
AttributeError: 'Config' object has no attribute 'mask_proto_debug'

In addition to above recommendation of @dbolya and my last edit, adding necessary paths and appending them to current directory you try run the python file solved my problem.

I'm using Pytorch 1.3.1 and for me it worked to do the following.

You can avoid explicit calling when calling the model to eval from another Python file
torch.set_default_tensor_type('torch.cuda.FloatTensor')
if you fix the following lines.

In layers/functions/detection.py, line 210
it was keep = torch.Tensor(keep, device=boxes.device).long()
and it became keep = torch.Tensor(keep).to(boxes.device).long()

In yolact.py, line 248
it was self.priors = torch.Tensor(prior_data, device=device).view(-1, 4).detach()
and it became self.priors = torch.Tensor(prior_data).view(-1, 4).to(device).detach()

Now I can predict with the yolact model without setting the default tensor type as torch.cuda.FloatTensor as it hurts other parts of my script.

Hey @dbolya,
I'm able to call yolact from another python file and everything is working perfectly. I want to know how can I set the top_k in my new python file? Thanks

@Auth0rM0rgan You can use the same snippet eval.py uses:

idx = scores.argsort(0, descending=True)[:top_k]
classes, scores, boxes, masks = [x[idx] for x in [classes, scores, boxes, masks]]

Though this is something that should natively be in the API once we make one.

In addition to above recommendation of @dbolya and my last edit, adding necessary paths and appending them to current directory you try run the python file solved my problem.

Hey, I have a similar problem:
AttributeError: 'Config' object has no attribute 'mask_proto_debug'

I want to know what you said "necessary paths" and "current directory",I only see two places in the code where I need to fill the path,(weights and img)

@zyxdb

Hey, I have a similar problem:
AttributeError: 'Config' object has no attribute 'mask_proto_debug'

I want to know what you said "necessary paths" and "current directory",I only see two places in the code where I need to fill the path,(weights and img)

For a lack of better solution (or rather inability to find one), I just commented calling mask_proto_debug out in output_utils.py line 63:

#if cfg.mask_proto_debug:
        #    np.save('scripts/proto.npy', proto_data.cpu().numpy())

"Fixed" the problem without any devastating consequences (for now)

this is my script, to recognize objecte in an image, someone can help me

from yolact import Yolact
import cv2
from layers.output_utils import postprocess
from data import cfg, set_cfg, set_dataset
import torch
import os
from utils.augmentations import BaseTransform, FastBaseTransform, Resize


def main():

    torch.backends.cudnn.fastest = True
    torch.set_default_tensor_type('torch.cuda.FloatTensor')


ROOT_DIR = os.path.dirname(os.path.abspath(__file__)).replace("\\", "/")
WEIGHT_DIR = ROOT_DIR + "/weights/"

if __name__ == '__main__':
    main()

    print('Loading model...', end='')

    net = Yolact().cuda()
    cfg = "yolact_plus_resnet50" + '_config'
    set_cfg(cfg)
    net.load_weights(WEIGHT_DIR + "yolact_plus_resnet50_54_800000.pth")

    net.eval()
    print(' Done.')
    transform = FastBaseTransform()

    img_path = ROOT_DIR + "/test_images/123.jpg"

    img = cv2.imread(img_path)
    h, w, _ = img.shape
    scale_percernt = 20
    width = int(w * scale_percernt / 100)
    height = int(h * scale_percernt / 100)
    dim = (width, height)
    resized_img = cv2.resize(img, dim, interpolation=cv2.INTER_AREA)

    batch = transform(torch.from_numpy(resized_img).cuda().float()[None, ...])
    preds = net(batch)

    with torch.no_grad():
        classes, scores, boxes, masks = postprocess(preds, width, height, score_threshold=0.15)
        print(classes)
    cv2.destroyAllWindows()

but I always get the tensor empty
loading model... Done. tensor([])

Hi

Please answer this question
😌😌😌
It is possible to crop and save the detected images in the YOLACT++.?

On Mon, 22 Feb 2021 at 20:17, Mahmoudi1993 notifications@github.com wrote:

Hi @bayraktare https://github.com/bayraktare
It is possible to crop and save the detected images in the YOLACT++.

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/dbolya/yolact/issues/256#issuecomment-783511187, or
unsubscribe
https://github.com/notifications/unsubscribe-auth/AR34IPSBW5BLXYDO27QGDDTTAKDB5ANCNFSM4J4KNGTQ
.

Was this page helpful?
0 / 5 - 0 ratings