Dockerfile配置PYTHONPATH及解决模块导入与文件路径错误
Solution for Mask R-CNN Import and Model File Errors
Fix the "No module named 'maskrcnn'" Error
- Create an empty
__init__.pyfile inside your localmaskrcnndirectory. This tells Python to recognize the folder as an importable module. - In your Dockerfile, add an environment variable to include your project root in Python's module search path. Assuming your project files are copied to
/app(a standard Docker setup), add this line:ENV PYTHONPATH="/app:$PYTHONPATH" - Ensure your Dockerfile sets the working directory before copying project files:
WORKDIR /app COPY . .
Fix the "mask_rcnn_coco.h5 not found" Error
Choose one of the following options based on your setup:
Copy local model file into Docker
If you already havemask_rcnn_coco.h5in your localmaskrcnnfolder, add this COPY command to your Dockerfile (after setting the working directory):COPY ./maskrcnn/mask_rcnn_coco.h5 /app/maskrcnn/mask_rcnn_coco.h5Update the path in
m_rcnn.pyto match:/app/maskrcnn/mask_rcnn_coco.h5, or use a relative path like./mask_rcnn_coco.h5if the script runs from themaskrcnndirectory.Download model during Docker build
If you don't have the file locally, add a RUN command to download the official pre-trained COCO model directly into the container (ensurewgetis installed):RUN mkdir -p /app/maskrcnn && wget -O /app/maskrcnn/mask_rcnn_coco.h5 https://github.com/matterport/Mask_RCNN/releases/download/v2.0/mask_rcnn_coco.h5
Sample Complete Dockerfile Snippet
# Use a compatible Python base image FROM python:3.9-slim # Set working directory WORKDIR /app # Install required dependencies (adjust based on your project needs) RUN pip install --no-cache-dir tensorflow keras pillow numpy opencv-python # Configure PYTHONPATH ENV PYTHONPATH="/app:$PYTHONPATH" # Copy all project files into the container COPY . . # Uncomment below to download model if not present locally # RUN mkdir -p /app/maskrcnn && wget -O /app/maskrcnn/mask_rcnn_coco.h5 https://github.com/matterport/Mask_RCNN/releases/download/v2.0/mask_rcnn_coco.h5 # Command to start your inference app CMD ["python", "app.py"]
内容的提问来源于stack exchange,提问作者BodeTech
相关产品推荐
相关产品推荐

