CivArchive
    ← All articles
    Published June 19, 2024by MassBrainImpact

    The Code I Use To Maximize the Effectiveness of my Pony LoRAs.

    118 views3 reactions1 comments on CivitAI1 collected
    pdxl lorapdxl datasetdata prepcode

    So, you're probably wondering what sort of data prep I do for my Pony LoRAs. Well, it's the same as I do for my other LoRAs (basically the Kohya method) but with these two codes:

    A code to delete images that don't fit inside the smallest ratio that fits SDXL.

    from PIL import Image
    
    # Set the paths to your photo folder
    photo_folder = "/content/drive/MyDrive/Loras/[INSERT FOLDER HERE]/dataset"
    
    # Define the minimum size threshold in pixels (width * height)
    min_size_threshold = 983040  # Adjust this value as needed
    
    # Initialize counters
    photos_deleted = 0
    photos_remaining = 0
    
    # Iterate through each photo file in the folder
    for photo_file in os.listdir(photo_folder):
        # Check if the file is an image
        if not photo_file.lower().endswith(('.jpg', '.jpeg', '.png')):
            continue  # Skip non-image files
    
        # Get the photo size using Pillow
        photo_path = os.path.join(photo_folder, photo_file)
        try:
            with Image.open(photo_path) as img:
                width, height = img.size
                photo_size = width * height
        except OSError:
            # Handle potential errors while opening images
            print(f"Error opening {photo_file}. Skipping...")
            continue
    
        # Check if the size is below the threshold
        if photo_size < min_size_threshold:
            # Delete the photo
            os.remove(photo_path)
            print(f"Deleted: {photo_file} (Size: {photo_size})")
            photos_deleted += 1
        else:
            photos_remaining += 1
    
    print(f"Process completed.")
    print(f"Photos Deleted: {photos_deleted}")
    print(f"Photos Remaining: {photos_remaining}")

    If I see images with the Sample Tag or with more than 900,000 KB, what I do is restore them then manually resize them by replacing the lowest number with 1024. (or see out the original sized images for the Samples).

    If somehow I end up with more than half my images deleted, even with this, what I would then do is bring the deleted images to a new folder and run this code which will automatically resize them by a certain percentage.

    # Define the path to your image folder on Google Drive (replace with your actual path)
    image_folder_path = '/content/drive/MyDrive/Loras/[INSERT FOLDER HERE]/ToResize'
    
    # Define the resize factor (The number after 1. would be your percentage, with 2 being outright doubling and so forth.)
    resize_factor = 1.25
    
    # Loop through all files in the image folder
    for filename in os.listdir(image_folder_path):
      # Check if it's an image file
      if filename.lower().endswith(('.jpg', '.jpeg', '.png')):
        # Open the image
        img = Image.open(os.path.join(image_folder_path, filename))
    
        # Get image size
        width, height = img.size
    
        # Resize the image
        new_width = int(width * resize_factor)
        new_height = int(height * resize_factor)
        resized_img = img.resize((new_width, new_height), resample=Image.LANCZOS)  # Use LANCZOS resampling
    
        # Save the resized image with a new name (optional)
        # new_filename = f"{filename[:-4]}_resized.{filename[-3:]}"
        new_filename = filename  # Save over the original file (comment out the line above)
    
        # Save the resized image
        resized_img.save(os.path.join(image_folder_path, new_filename))
    
    print("Images resized successfully!")

    By how much is up to you, but I prefer 25% if it's only a few more images you need and up to 2 times the original size if you want as many images as you can. Anything after 2 times would be overkill, especially since the next step I do is to bring them back into the dataset where I run the deletion again, this time observing which ones still go and repeating the process.

    Or, if I were to put this in BASIC terms.

    10 REM Start Deletion Process
    20 GOSUB DeleteCode
    30 IF SampleImageFound THEN GOSUB AddToDataset
    40 IF ImageSize > 900000 THEN GOSUB RestoreAndResize
    50 IF ImagesRemaining < TotalImages / 2 THEN GOSUB RestoreAndAdd
    60 GOSUB Tagging
    70 END
    
    80 DeleteCode:
    90 REM Deletion code logic here
    100 RETURN
    
    110 AddToDataset:
    120 REM Logic to find original image and add to dataset
    130 RETURN
    
    140 RestoreAndResize:
    150 REM Logic to restore and resize image
    160 RETURN
    
    170 RestoreAndAdd:
    180 REM Logic to restore images and add to restore images
    190 REM Call resize code
    200 GOSUB ResizeCode
    210 RETURN
    
    220 ResizeCode:
    230 REM Logic to resize image
    240 RETURN
    
    250 Tagging:
    260 REM Tagging code logic here
    270 RETURN