ultralytics / yolov5

YOLOv5 πŸš€ in PyTorch > ONNX > CoreML > TFLite
https://docs.ultralytics.com
GNU Affero General Public License v3.0
50.72k stars 16.34k forks source link

Ram Usage #12839

Closed vishal3046 closed 6 months ago

vishal3046 commented 7 months ago

Search before asking

YOLOv5 Component

No response

Bug

https://www.kaggle.com/code/vishalb19mia1012/vindr-yolo

please refer this notebook

While executing the 12k and 10k there are issues where my ram usage goes to the peak and my notebook crashes and the list of warnings are very new as well

but if i do the same for 4k-test-640 the code is running

idk what im dong wrong here im been stuck for 2 weeks please help

and here the number basically means the number of images ill share the code how the images are generated as well im doing some preprocessing

from PIL import Image import matplotlib.pyplot as plt import albumentations as A import numpy as np import os import zipfile import pydicom import cv2 import matplotlib.patches as patches from math import isnan

zip_file_path = 'D:\Python files\yolo_breast_cancer\vindr-mammo-a-large-scale-benchmark-dataset-for-computer-aided-detection-and-diagnosis-in-full-field-digital-mammography-1.0.0.zip'

def load_and_transform_dicom_image(zip_file_path, study_id, image_id, output_image_dir, output_label_dir, original_bounding_boxes, image_number): try: with zipfile.ZipFile(zip_file_path, 'r') as zip_ref: with zip_ref.open(f'vindr-mammo-a-large-scale-benchmark-dataset-for-computer-aided-detection-and-diagnosis-in-full-field-digital-mammography-1.0.0/images/{study_id}/{image_id}.dicom') as file: ds = pydicom.dcmread(file)

    pixel_data = ds.pixel_array
    min_pixel_value = pixel_data.min()
    max_pixel_value = pixel_data.max()
    pixel_data = ((pixel_data - min_pixel_value) / (max_pixel_value - min_pixel_value) * 255).astype(int)
    pixel_data = cv2.convertScaleAbs(pixel_data)
    pixel_data = cv2.cvtColor(pixel_data, cv2.COLOR_BGR2GRAY) if len(pixel_data.shape) == 3 else pixel_data
    clahe = cv2.createCLAHE(clipLimit=30.0, tileGridSize=(8, 8))
    pixel_data = clahe.apply(pixel_data)

    for i in range(len(original_bounding_boxes)):
        bbox_list = list(original_bounding_boxes[i])
        bbox_list[2] = np.abs(bbox_list[2] - 0.5 / pixel_data.shape[0])
        bbox_list[3] = np.abs(bbox_list[3] - 0.5 / pixel_data.shape[1])
        original_bounding_boxes[i] = tuple(bbox_list)

    # Check the value of 'upsampling'
    if row['Oversampling'] == 1:
        transform = A.Compose([
            A.RandomRotate90(p=0.5),
            A.Rotate(limit=(-60, 60), interpolation=cv2.INTER_LINEAR, border_mode=cv2.BORDER_CONSTANT, p=0.5),
            A.Transpose(p=0.5),
            A.HorizontalFlip(p=0.5),
            A.VerticalFlip(p=0.5),
            A.Resize(height=640, width=640, always_apply=True),
        ], bbox_params={'format': 'pascal_voc'})

        transformed = transform(image=pixel_data, bboxes=np.array(original_bounding_boxes, dtype=np.float32))
        transformed_bboxes = transformed["bboxes"]
    else:
        transform = A.Compose([
            A.Resize(height=640, width=640, always_apply=True),
        ], bbox_params={'format': 'pascal_voc'})

        transformed = transform(image=pixel_data, bboxes=np.array(original_bounding_boxes, dtype=np.float32))
        transformed_bboxes = transformed["bboxes"]

    study_id = row['study_id']
    image_id = row['image_id']
    classification = row['classification']
    oversampling = row['Oversampling']

    for bbox in transformed_bboxes:
        yolo_format = convert_to_yolo_format(640, 640, bbox)
        print(f"YOLO Coordinates: {yolo_format}")

    image_file_name = f'{oversampling}_{classification}_{image_number}.png'
    bbox_file_name = f'{oversampling}_{classification}_{image_number}.txt'

    image_file_path = os.path.join(output_image_dir, image_file_name)
    bbox_file_path = os.path.join(output_label_dir, bbox_file_name)

    cv2.imwrite(image_file_path, transformed["image"])

    with open(bbox_file_path, 'w') as bbox_file:
        for bbox in transformed_bboxes:
            yolo_format = convert_to_yolo_format(640, 640, bbox)
            bbox_file.write(yolo_format + "\n")

    return {
        "transformed_image": transformed["image"],
        "transformed_bboxes": transformed_bboxes
    }

except ValueError as ve:
    print(f"Error processing Study ID: {study_id}, Image ID: {image_id}")
    print(f"Error message: {ve}")
    return {
        "study_id": study_id,
        "image_id": image_id
    }

def display_image_dimensions(transformed_image): height, width = transformed_image.shape[:2] print(f"Transformed Image Dimensions: Height={height}, Width={width}")

def convert_to_yolo_format(image_width, image_height, bbox): x_min, y_min, x_max, y_max, class_label = bbox center_x = (x_min + x_max) / 2 center_y = (y_min + y_max) / 2 width = x_max - x_min height = y_max - y_min center_x /= image_width center_y /= image_height width /= image_width height /= image_height return f"{class_label} {center_x} {center_y} {width} {height}"

error_images = [] image_number_counter = 1

for _, row in dataset_new.iterrows(): study_id = row['study_id'] image_id = row['image_id'] if row['classification'] == 'Malignant': class_label = 1 else: class_label = 0

x_min = row['xmin']
y_min = row['ymin']
x_max = row['xmax']
y_max = row['ymax']
image_width = row['width']
image_height = row['height']

if isnan(x_min) or isnan(x_max) or isnan(y_min) or isnan(y_max):
    x_min, y_min, x_max, y_max = 0, 0, image_width, image_height

original_bounding_boxes = [(x_min, y_min, x_max, y_max, class_label)]

output_image_directory = 'D:\Python files\yolo_breast_cancer\dataset_new_images'
output_label_directory = 'D:\Python files\yolo_breast_cancer\dataset_new_labels'
os.makedirs(output_image_directory, exist_ok=True)
os.makedirs(output_label_directory, exist_ok=True)

result = load_and_transform_dicom_image(zip_file_path, study_id, image_id, output_image_directory, output_label_directory, original_bounding_boxes, image_number_counter)
if result is not None and "transformed_image" in result and "transformed_bboxes" in result:
    error_images.append(result)
    # Display the transformed image dimensions
    display_image_dimensions(result["transformed_image"])

image_number_counter += 1

print("Study ID and Image ID pairs for images with ValueError:", error_images)

Environment

No response

Minimal Reproducible Example

No response

Additional

No response

Are you willing to submit a PR?

github-actions[bot] commented 7 months ago

πŸ‘‹ Hello @vishal3046, thank you for your interest in YOLOv5 πŸš€! Please visit our ⭐️ Tutorials to get started, where you can find quickstart guides for simple tasks like Custom Data Training all the way to advanced concepts like Hyperparameter Evolution.

If this is a πŸ› Bug Report, please provide a minimum reproducible example to help us debug it.

If this is a custom training ❓ Question, please provide as much information as possible, including dataset image examples and training logs, and verify you are following our Tips for Best Training Results.

Requirements

Python>=3.8.0 with all requirements.txt installed including PyTorch>=1.8. To get started:

git clone https://github.com/ultralytics/yolov5  # clone
cd yolov5
pip install -r requirements.txt  # install

Environments

YOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):

Status

YOLOv5 CI

If this badge is green, all YOLOv5 GitHub Actions Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv5 training, validation, inference, export and benchmarks on macOS, Windows, and Ubuntu every 24 hours and on every commit.

Introducing YOLOv8 πŸš€

We're excited to announce the launch of our latest state-of-the-art (SOTA) object detection model for 2023 - YOLOv8 πŸš€!

Designed to be fast, accurate, and easy to use, YOLOv8 is an ideal choice for a wide range of object detection, image segmentation and image classification tasks. With YOLOv8, you'll be able to quickly and accurately detect objects in real-time, streamline your workflows, and achieve new levels of accuracy in your projects.

Check out our YOLOv8 Docs for details and get started with:

pip install ultralytics
glenn-jocher commented 7 months ago

@vishal3046 hi there! It seems like you're encountering significant RAM usage issues during your project, especially with larger image datasets. High RAM usage usually comes from loading a large number of images into memory at once or performing resource-intensive operations on them.

Considering you're working with a variable number of images (12k, 10k, and 4k) and using data augmentation, it's vital to manage memory efficiently. Here are a couple of suggestions:

  1. Batch Processing: Instead of loading all images into memory at once, process them in smaller batches. This approach significantly reduces peak memory usage.
  2. Optimize Images: Before applying transformations, ensure images are in a memory-efficient format and size. Sometimes, resizing images before further processing can help.
  3. Garbage Collection: Explicitly free up memory by deleting variables holding large objects no longer needed and calling gc.collect() from Python's gc module if necessary.
  4. Memory Profiling: Utilize memory profiling tools to pinpoint exactly where the high memory consumption is happening. This will help you focus your optimization efforts effectively.

Given the complexity of the transformation you're doing, especially with DICOM images, high memory usage can occur, especially with higher resolutions or large datasets. If the issue persists, consider processing images in a lower resolution or optimizing your pipeline further to reduce memory footprint.

Feel free to share specific details or error messages you encounter, and we can dive deeper into this issue together. Keep exploring and optimizing! πŸš€

github-actions[bot] commented 6 months ago

πŸ‘‹ Hello there! We wanted to give you a friendly reminder that this issue has not had any recent activity and may be closed soon, but don't worry - you can always reopen it if needed. If you still have any questions or concerns, please feel free to let us know how we can help.

For additional resources and information, please see the links below:

Feel free to inform us of any other issues you discover or feature requests that come to mind in the future. Pull Requests (PRs) are also always welcomed!

Thank you for your contributions to YOLO πŸš€ and Vision AI ⭐