it will execute multiple times when import from test.py
See original GitHub issueIn my commamd line, the ‘test.py’ works well .
I want use this file in other python file, So I add a function to ‘test.py’, it is:
def run(self, imgs=None, model="config/ade20k-resnet50dilated-ppm_deepsup.yaml", gpu=0):
if imgs == None:
return False
cfg.merge_from_file(model)
cfg.MODEL.arch_encoder = cfg.MODEL.arch_encoder.lower()
cfg.MODEL.arch_decoder = cfg.MODEL.arch_decoder.lower()
# absolute paths of model weights
cfg.MODEL.weights_encoder = os.path.join(
cfg.DIR, 'encoder' + cfg.TEST.suffix)
cfg.MODEL.weights_decoder = os.path.join(
cfg.DIR, 'decoder' + cfg.TEST.suffix)
if not os.path.exists(cfg.MODEL.weights_encoder):
return (False, "checkpoint does not exitst!")
# generate testing image list
if os.path.isdir(imgs[0]):
imgs = find_recursive(imgs[0])
else:
imgs = [imgs]
assert len(imgs), "imgs should be a path to image (.jpg) or directory."
cfg.list_test = [{'fpath_img': x} for x in imgs]
if not os.path.isdir(cfg.TEST.result):
os.makedirs(cfg.TEST.result)
main(cfg, gpu)
However, In another file, when I use this function like this:
import run from test
run("test.jpg")
It outputs error:
Loading weights for net_encoder
Loading weights for net_decoder
# samples: 1
Loading weights for net_encoder
Loading weights for net_encoder
Loading weights for net_encoder
Loading weights for net_encoder
Loading weights for net_encoder
Loading weights for net_decoder
Loading weights for net_decoder
Loading weights for net_decoder
# samples: 1
Loading weights for net_decoder
Loading weights for net_decoder
# samples: 1
# samples: 1
# samples: 1
......
then my command line is crash.
However, to use this function in the file test.py works well. Only in other file will it failed.
This function is as same as the code in the if __name__ == '__main__':
, so where is wrong?
Thanks.
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (1 by maintainers)
Top Results From Across the Web
How can I repeat each test multiple times in a py.test run?
I attempted to do this in the pytest_collection_modifyitems() hook. I modified the list of items passed in, to specify each item more than...
Read more >How to correctly run pytest.main() programmatically multiple ...
If I invoke pytest.main(['test_foo.py']) multiple time from the same running script, it will give the same result, even if test_foo.py ...
Read more >pytest-repeat - PyPI
Each test collected by pytest will be run count times. If you want to mark a test in your code to be repeated...
Read more >Pytest Tutorial: Executing Multiple Test Cases From Single File
With pytest you can run multiple test cases from a single file, or even run selective tests with custom markers or by grouping...
Read more >Usage and Invocations — pytest documentation
Pytest supports several ways to run and select tests from the command-line. Run tests in a module. pytest test_mod.py. Run tests in a...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I finally find why: I try to print the
__name__
in the begin of the functionrun()
. At first it print__main__
, after that it print many__mp_main__
. It is the modulemultiprocess
's problem and only occurs on Windows.Those too many new processes is the reason why my cammand line crash.
Solution: in other file, please judge
if __name__ == '__main__':
every time, like this:This problem just occurs on Windows.
For more can see multiprocess , torch.multiprocessing
@acdzh maybe I have a bug here, can you try to replace
imgs[0]
withimgs
?