Real-Time Segmentation on Video #1973
-
Hello nnUNet Community. I have trained an 2D nnUNet on my dataset and now I want to implement it into an OpenCV pipeline that does real-time segmentation on video frame-by-frame and outputs the result. I know how to use the "nnUNetV2_predict" command; however, it seems that saving the frame in an input folder, specifying an output folder, and then sourcing the segmentation mask from the output folder and then overlaying seems very convoluted. Is there a better way to run this kind of inference? I don't have much coding background and this is my first time trying to implement something like this, so I apologize if the question is very basic. Any advice on how to best achieve this with nnUNet inference or where to look to get started? Thank you in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 8 replies
-
Hello, here is an update on what I have tried. Based on the inference documentation, the command-line command has to re-initialize the network and weights with every prediction request, so I thought it would be better to instantiate the predictor as an object which can be called on the fly to avoid this step. I am using a Windows machine so I could not directly run the "predict_from_files" without encapsuating in `"if name == 'main': ", due to multiprocessing on Windows. However, the predict_from_files is extremely slow, when I run this code, which takes about 15 seconds. My CUDA_VISIBLE_DEVICES=1 is an NVIDIA RTX A4500. I am using the following version (this is the output of "git describe --tags" on my nnUNet directory)
However, the predict_from_files is unacceptably slow. When I run this code, which takes ~15 seconds to do a single inference in total. My CUDA_VISIBLE_DEVICES=1 is an NVIDIA RTX A4500. While the initialization of the network prior to invoking "predict_from_files" seems to be ~1 second, which I can accept, what is contributing to the slowness is what appears to be the entire re-initialization being called multiple times after "predict_from_files",. which accumulates, as evidenced by the output below of the above code.
Any suggestions on what I can do to address this problem and improve the overall speed of inference? I am willing to sacrifice the quality of segmentation a bit, so are there processing areas I could possibly cut out to make this run faster? I am really stuck on this and any advice is appreciated. Thank you! |
Beta Was this translation helpful? Give feedback.
-
@rahulghosh2 Hello, I am currently experiencing the same problem as you. I would like to ask, how long does it take you to process one frame? |
Beta Was this translation helpful? Give feedback.
-
Maybe just rip the trained model out of nnunet and run a regular PyTorch 2d unet with your own inference script |
Beta Was this translation helpful? Give feedback.
You can directly run and time the
run_case_npy
and see if using it gives you the same result aspredict_from_files
.Among other things,
run_case_npy
executes cropping, normalization and resampling. As far as I know, resampling takes a lot of time, but you need to measure for your case.One way to optimize the code is to implement the numpy operations inplace for normalization, and cropping could be reimplemented like this: https://github.com/ancestor-mithril/nnUNet/blob/7b53480b2a16dd4dd05a6e02b1797e15f456dcc7/nnunetv2/preprocessing/cropping/cropping.py#L23.
But once again, you should measure which steps take the most time in your case and try to optimize those.