Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MD5.0 "ModuleNotFoundError: No module named 'utils' #70

Closed
agentmorris opened this issue May 20, 2023 · 3 comments
Closed

MD5.0 "ModuleNotFoundError: No module named 'utils' #70

agentmorris opened this issue May 20, 2023 · 3 comments

Comments

@agentmorris
Copy link
Owner

I'm very eager to try the Yolov5 version of Megadetector.

I had success getting MD4.0 up on Google Colab, as shown in:

https://colab.research.google.com/github/microsoft/CameraTraps/blob/master/detection/megadetector_colab.ipynb

I tried just replacing the call to load the model from:
!wget -O /content/megadetector_v4_1_0.pb https://lilablobssc.blob.core.windows.net/models/camera_traps/megadetector/md_v4.1.0/md_v4.1.0.pb

To:
!wget -O /content/megadetector_v5a.0.0.pt https://github.com/microsoft/CameraTraps/releases/download/v5.0/md_v5a.0.0.pt

Also, call to run_detector_batch.py from:
!python /content/CameraTraps/detection/run_detector_batch.py megadetector_v4_1_0.pb "$images_dir" "$output_file_path" --recursive

To:
!python /content/CameraTraps/detection/run_detector_batch.py megadetector_v5a.0.0.pt "$images_dir" "$output_file_path" --recursive

But I get the following error:

19202 image files found in the input directory
PyTorch reports 1 available CUDA devices
GPU available: True
Traceback (most recent call last):
File "/content/CameraTraps/detection/pytorch_detector.py", line 17, in
from utils.general import non_max_suppression, scale_coords, xyxy2xywh
ModuleNotFoundError: No module named 'utils'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "run_detector_batch.py", line 655, in
main()
File "run_detector_batch.py", line 635, in main
quiet=args.quiet)
File "run_detector_batch.py", line 344, in load_and_run_detector_batch
detector = load_detector(model_file)
File "/content/CameraTraps/detection/run_detector.py", line 234, in load_detector
from detection.pytorch_detector import PTDetector
File "/content/CameraTraps/detection/pytorch_detector.py", line 20, in
raise ModuleNotFoundError('Could not import YOLOv5 functions.')
ModuleNotFoundError: Could not import YOLOv5 functions.

Is there something else I need to do to make MDv5 work on colab?

Thanks.


Issue cloned from Microsoft/CameraTraps, original issue posted by robertzak133 on Jul 16, 2022.

@agentmorris
Copy link
Owner Author

Hi,

You can fix this issue by cloning the yolov5 repository.

So you could add this cell right after you Set PYTHONPATH to include CameraTraps and ai4eutils:

!git clone https://github.com/ultralytics/yolov5
os.environ['PYTHONPATH'] += ":/content/yolov5"

That should solve the ModuleNotFoundError but you may run into another problem when running:
!python /content/CameraTraps/detection/run_detector_batch.py megadetector_v5a.0.0.pt "$images_dir" "$output_file_path" --recursive.

Initially, I got this output:

48 image files found in the input directory
PyTorch reports 1 available CUDA devices
GPU available: True
Using PyTorch version 1.12.0+cu113
Fusing layers... 
Model summary: 574 layers, 139990096 parameters, 0 gradients
Sending model to GPU
Loaded model in 9.97 seconds
Loaded model in 9.97 seconds
  0% 0/48 [00:00<?, ?it/s]Processing image /content/snapshotserengeti/D05_R4/S1_D05_R4_PICT0083.JPG
PTDetector: image /content/snapshotserengeti/D05_R4/S1_D05_R4_PICT0083.JPG failed during inference: **'Upsample' object has no attribute 'recompute_scale_factor'**
  2% 1/48 [00:04<03:11,  4.06s/it]Processing image /content/snapshotserengeti/D05_R4/S1_D05_R4_PICT0075.JPG
PTDetector: image /content/snapshotserengeti/D05_R4/S1_D05_R4_PICT0075.JPG failed during inference: **'Upsample' object has no attribute 'recompute_scale_factor'**
Processing image /content/snapshotserengeti/D05_R4/S1_D05_R4_PICT0087.JPG

To get around this problem, you will have to modify the file '/usr/local/lib/python3.7/dist-packages/torch/nn/modules/upsampling.py' by commenting out the line 'recompute_scale_factor=self.recompute_scale_factor' as follows:

def forward(self, input: Tensor) -> Tensor:
    return F.interpolate(input, self.size, self.scale_factor, self.mode, self.align_corners,
                         #recompute_scale_factor=self.recompute_scale_factor
                         )

(Same issue was raised here: ultralytics/yolov5#6948)


(Comment originally posted by obyrnemj)

@agentmorris
Copy link
Owner Author

Thanks! I got the same error re recompute_scale_factor, but was able to make patch. For anyone wanting to keep everything in a Colab file, here is the sed command:

!sed -i '154 s@recompute_scale_factor=self.recompute_scale_factor)@)@' /usr/local/lib/python3.7/dist-packages/torch/nn/modules/upsampling.py

After making this patch, the new model is working great.


(Comment originally posted by robertzak133)

@agentmorris
Copy link
Owner Author

Closing since it seems like everyone found a solution here.

FYI the patching solution is only stable for a particular release of PyTorch (i.e., it may stop working if stuff changes in that file), plus there are any number of other reasons that new versions of PyTorch and/or YOLOv5 could break MegaDetector compatibility, so all other things being equal, we recommend checking out the specific commit of YOLOv5 against which we've tested (c23a441c9df7ca9b1f275e8c8719c949269160d1), and installing PyTorch version 1.10.1 (instead of the most recent version).

Our instructions for conda environments already handle this, but it would be great if a Colab user wanted to post instructions for setting up a Colab environment that matches our recommended conda environments.


(Comment originally posted by agentmorris)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant