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

Use CUPY instead of Tensorflow to count GPUs #505

Open
wants to merge 41 commits into
base: experimental
Choose a base branch
from

Conversation

MYusufY
Copy link

@MYusufY MYusufY commented Aug 30, 2024

Summary by Sourcery

Replace TensorFlow with CuPy for GPU management, add new UI features including a swap faces button and live camera options, implement NSFW filtering, and update documentation with new features and contribution guidelines.

New Features:

  • Introduce a swap faces button to allow users to swap source and target paths in the UI.
  • Add NSFW filtering functionality to prevent processing of inappropriate media.
  • Implement live mirror and resizable options for the live camera display.

Enhancements:

  • Refactor the webcam preview function to simplify camera handling and improve resolution settings.
  • Improve the efficiency of the NSFW prediction by preloading the model once.

Documentation:

  • Update README to include new command line arguments and installation instructions for macOS.
  • Add a TODO section in the README to outline future development plans.
  • Introduce a CONTRIBUTING.md file to guide contributors on the workflow.

hacksider and others added 30 commits August 10, 2024 13:54
Revert "Enable to choose a camera device in UI" Will put it on experimental as of this moment
… install requirements. Now the code is working. You are able to run it on your Windows machine.
Fix this cyclic dependency issue which was coming while installing rquirements
Fixed a typo.
relinking this to the old repo
Nothing we say is legally binding in here, but it
behooves us to be stronger in our language - getting consent
to be digitally reproduced should be required
and standard procedure.
Grammar updates

Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
…-optimisation

macOS optimisations for Silicon Macs for GPU / Metal usage (reduced CPU load from 600% to 150%)
…ew-is-closed

Stop live mode when the preview window is closed
hides NSFW button
To fix bugs and support more options for the Live function (see details in Commits tab)
Hides NSFW
This reverts commit 9d20e04.
Update README.md to address the issue with the GTK package on MAC OS
@refer to the PR hacksider#237 in the `experimental` branch.

Signed-off-by: Vic P <[email protected]>
vic4key and others added 11 commits August 21, 2024 02:16
Re-enabled the NSFW function (turn-off by default) with its bug fixes at PR hacksider#237.
- The live camera display as you see it in the front-facing camera frame (like iPhone's Mirror Front Camera).
- The live camera frame is resizable.
Note: These options are turned off by default. Enabling both options may reduce performance by ~2%.

Signed-off-by: Vic P <[email protected]>
@refer to the PR hacksider#293 in the `experimental` branch.
Disable NSFW in accordance to github rules
Adding a swap faces button to easily swap source/target images
Copy link
Contributor

sourcery-ai bot commented Aug 30, 2024

Reviewer's Guide by Sourcery

This pull request implements several changes to improve the functionality and user experience of the face swapping application. Key changes include adding NSFW filtering, enhancing the live preview feature, updating the UI, and improving performance.

File-Level Changes

Change Details Files
Added NSFW filtering functionality
  • Implemented NSFW check function
  • Added command-line argument for NSFW filter
  • Integrated NSFW filtering into image and video processing
modules/ui.py
modules/core.py
modules/globals.py
modules/predicter.py
Enhanced live preview functionality
  • Added face swapping button to swap source and target faces
  • Implemented live mirror option for webcam preview
  • Added resizable option for live preview window
modules/ui.py
modules/core.py
modules/globals.py
Updated UI elements and layout
  • Renamed and repositioned UI buttons
  • Removed NSFW switch from UI
  • Added new command-line arguments for live preview options
modules/ui.py
README.md
Improved performance and resource management
  • Replaced TensorFlow with CuPy for GPU memory management
  • Optimized NSFW prediction by preloading the model
  • Updated resource limitation function
modules/core.py
modules/predicter.py
Updated project documentation and contribution guidelines
  • Added new TODO items in README.md
  • Updated installation instructions for macOS
  • Created CONTRIBUTING.md file with guidelines for contributors
README.md
CONTRIBUTING.md

Tips
  • Trigger a new Sourcery review by commenting @sourcery-ai review on the pull request.
  • Continue your discussion with Sourcery by replying directly to review comments.
  • You can change your review settings at any time by accessing your dashboard:
    • Enable or disable the Sourcery-generated pull request summary or reviewer's guide;
    • Change the review language;
  • You can always contact us if you have any questions or feedback.

@MYusufY MYusufY changed the title Use CUPY instead of Tensorflow wh Use CUPY instead of Tensorflow to count GPUs Aug 30, 2024
Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @MYusufY - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider splitting this PR into smaller, more focused changes to make review and testing easier. The switch from TensorFlow to CuPy and the removal of camera selection are significant changes that warrant careful consideration.
  • Some new features, like the NSFW filter, could benefit from more detailed documentation or code comments explaining their functionality.
Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟡 Documentation: 2 issues found

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment to tell me if it was helpful.

@@ -6,11 +6,14 @@

MAX_PROBABILITY = 0.85

# Preload the model once for efficiency
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Consider more encapsulated approach for model caching

The global caching of the NSFW model is a good optimization. However, consider using a more encapsulated approach, such as a singleton pattern or a module-level function that manages the model instance, to avoid potential issues with global state.

class NSFWModel:
    _instance = None

    @classmethod
    def get_instance(cls):
        if cls._instance is None:
            cls._instance = cls._load_model()
        return cls._instance

    @staticmethod
    def _load_model():
        # Load and return the NSFW model
        pass

@@ -33,6 +33,10 @@ We highly recommend to work with a `venv` to avoid issues.
```
pip install -r requirements.txt
```
For MAC OS, You have to install or upgrade python-tk package:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (documentation): Correct capitalization of macOS

The correct capitalization is 'macOS' instead of 'MAC OS'.

Suggested change
For MAC OS, You have to install or upgrade python-tk package:
For macOS, you have to install or upgrade the python-tk package:

@@ -0,0 +1 @@
Please always push on the experimental to ensure we don't mess with the main branch. All the test will be done on the experimental and will be pushed to the main branch after few days of testing.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (documentation): Consider expanding CONTRIBUTING.md

While this is a good start, consider expanding CONTRIBUTING.md with more detailed guidelines for potential contributors. This could include coding standards, pull request process, and how to report issues.

# Contributing Guidelines

## Branch Management
- Always push changes to the `experimental` branch.
- The `main` branch is protected for stability.
- All tests will be conducted on the `experimental` branch.
- Changes will be merged to `main` after thorough testing (typically a few days).

## Pull Request Process
1. Create a pull request from your feature branch to `experimental`.
2. Ensure all tests pass and code meets our standards.
3. Wait for review and address any feedback.

## Reporting Issues
- Use the GitHub Issues tab to report bugs or suggest enhancements.
- Provide a clear description and steps to reproduce if applicable.


def webcam_preview(camera_index: int):
def webcam_preview():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (complexity): Consider refactoring the webcam_preview function into smaller, more focused functions.

The webcam_preview function has become overly complex, handling multiple responsibilities including camera initialization, frame processing, UI updates, and virtual camera output. To improve maintainability and adhere to the Single Responsibility Principle, consider refactoring this function into smaller, more focused functions:

  1. Extract camera initialization:
def initialize_camera(width, height, fps):
    camera = cv2.VideoCapture(0)
    camera.set(cv2.CAP_PROP_FRAME_WIDTH, width)
    camera.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
    camera.set(cv2.CAP_PROP_FPS, fps)
    return camera
  1. Create a separate function for frame processing:
def process_frame(frame, source_image, frame_processors):
    temp_frame = frame.copy()
    if modules.globals.live_mirror:
        temp_frame = cv2.flip(temp_frame, 1)
    if modules.globals.live_resizable:
        temp_frame = fit_image_to_size(temp_frame, PREVIEW.winfo_width(), PREVIEW.winfo_height())
    for frame_processor in frame_processors:
        temp_frame = frame_processor.process_frame(source_image, temp_frame)
    return temp_frame
  1. Extract UI update logic:
def update_preview_ui(frame):
    image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    image = Image.fromarray(image)
    image = ImageOps.contain(image, (frame.shape[1], frame.shape[0]), Image.LANCZOS)
    image = ctk.CTkImage(image, size=image.size)
    preview_label.configure(image=image)
    ROOT.update()
  1. Create a separate function for virtual camera output (if needed):
def output_to_virtual_camera(frame, virtual_cam):
    virtual_cam.send(frame)
    virtual_cam.sleep_until_next_frame()

Then, refactor the main webcam_preview function to use these helper functions:

def webcam_preview():
    if modules.globals.source_path is None:
        return

    camera = initialize_camera(PREVIEW_DEFAULT_WIDTH, PREVIEW_DEFAULT_HEIGHT, 60)
    preview_label.configure(width=PREVIEW_DEFAULT_WIDTH, height=PREVIEW_DEFAULT_HEIGHT)
    PREVIEW.deiconify()

    frame_processors = get_frame_processors_modules(modules.globals.frame_processors)
    source_image = get_one_face(cv2.imread(modules.globals.source_path)) if modules.globals.source_path else None

    while camera:
        ret, frame = camera.read()
        if not ret:
            break

        processed_frame = process_frame(frame, source_image, frame_processors)
        update_preview_ui(processed_frame)

        if PREVIEW.state() == 'withdrawn':
            break

    camera.release()
    PREVIEW.withdraw()

This refactoring separates concerns, making the code more modular and easier to maintain. Each function now has a single responsibility, improving readability and testability.

@MYusufY
Copy link
Author

MYusufY commented Aug 30, 2024

I thinks this solves

Copy link

@perfect-repairs perfect-repairs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

p

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

Successfully merging this pull request may close these issues.