avatarLars Nielsen

Summary

Lama-cleaner is an open-source image editor that uses AI-based tools like Stable Diffusion for image editing, offering superior results compared to traditional methods and enabling context-aware editing.

Abstract

Lama-cleaner is an open-source project that utilizes AI-based tools like Stable Diffusion for image editing. Its custom-trained model provides superior results compared to traditional OpenCV methods, allowing for context-aware image editing. The tool can remove watermarks, objects, and people from images, restore old photographs, and replace objects with custom 'prompt-based image snippets.' Installation can be done via pip or a one-click installer, and the package is actively being updated with bug fixes and improvements.

Opinions

  • Lama-cleaner's custom-trained model is far superior to traditional OpenCV methods.
  • The tool enables context-aware image editing, which was previously only available in costly software like Adobe Photoshop.
  • The installation process is well-documented and offers a one-click installer for beginners.
  • The package is actively being updated with bug fixes and improvements.
  • The tool can handle various use-cases, including watermark removal, object removal, old photograph restoration, and custom image replacement.
  • The tool requires a unique token from the hugging-face website for the Stable Diffusion model, which needs registration and creation of a unique token.
  • The tool may encounter Out-Of-Memory (OOM) errors, which can be resolved by reducing the size of the input image, changing the memory allocation in CUDA for Pytorch, or changing the model in the 'settings' to 'ldm' or 'lama.'

IMAGE EDITOR, AI TEXT-TO-IMAGE

lama-cleaner : AI based watermark remover & Intelligent Image Editor

Open source Image Editor with support for Stable Diffusion

Image edited by Author ( Original Image : unsplash.com )

About lama-cleaner

Lama Cleaner is the latest opensource project that uses some of the text-to-Image tools like Stable Diffusion for Image editing. Their custom trained model is far superior to the traditional OpenCV methods. Now you can do context-aware-image editing which was only available in costly softwares like Adobe Photoshop.

In case you are impatient and just want to try it out NOW, you can download the latest repo from here : https://github.com/Sanster/lama-cleaner . Else — just to know about the utility , browse on .

Use-cases : What can it do ?

  1. Remove watermarks from Images
  2. Remove objects & people from Images & replace with an intelligent background
  3. Remove blemishes & restore old photographs
  4. Replace objects with custom generated ‘prmopt-text based images

Here are the 4 use-cases explained :

  1. Removing watermarks. ( pretty self explanatory — see the image below to see the steps )
Image edited by Author ( Original Image : unsplash.com )

2. Removing Objects/People/ racoons :) etc from images

Photobombed , did you say ? — dont you worry !

And here is the result … amazing as it is !

Image edited by author, original source : reddit.com/r/funny

3. Restore your old photographs

Here are two examples and you can see what kind of old-photo-restoration is possible.

Image edited by author
Image Edited by Author

4. Using Stable-Diffusion model for image editing by replacing parts of images with custom ‘prompt-based image snippets’

prompt : “framed artwork on the wall” , Screenshot by Author from the application

Installation process

The installation process is well documented in their GitHub README file. But to put in one line —

Option a. you can do a pip install of the package !!

>> pip install lama-cleaner

#installs the package



>> lama-cleaner --model=lama --device=cuda --port=8080

#starts the program using the GPU available on the machine
# -model = which model to use ( lama , ldm , cv2, sd1.5 etc )
# --device = cpu / cuda ( for GPU )
# --port = which port to open on the default browser eg: http://127.0.0.1:8080

Option b. One-Click-installer ! ( you should try this !! — Reccomended for a beginner to just try out the application)

For those who do not want to bother with python , virtual environments , dependencies etc, the creators have provided a neat One-Click-installer which you can download and run the program. Details of the installation etc is here : https://github.com/Sanster/lama-cleaner/blob/main/scripts/README.md

Some advanced tips , information and things to keep in mind

1️⃣ You can choose which model you want to run based on what you want to achieve — if you are doing a simple watermark removal , the deafult model ‘lama’ would be your best option .

screenshot from the application settings

2️⃣ When you run it for the first time, the app downloads all the required models that it needs , into your local environment folder. For windows users , it will download to

WINDOWS \ USERS \  <username> \ .cache \ torch \ hub \ checkpoints

3️⃣If are using the Stable Diffusion model for the first time, you might have to give your unique token from the hugging-face website . ( needs registration on their site and creation of unique token. )

Note : Make sure the rights specified on the hugging face token are both ‘read’ and ‘write’

4️⃣If you are running into Out-Of-memory (OOM) errors , try the following work arounds .

Here is what an “CUDA out of memory” error looks like :

CUDA Out of Memory Error if you are using a GPU card with around 6 GB VRAM or sometimes * GB

Some of the tested workarounds :

🍀 Reduce the size of the input image to something close to 500x 500

🍀 Change the Memory allocation in CUDA for Pytorch —

Here are the environment variables to change the memory allocation values :

Windows: 
>> set 'PYTORCH_CUDA_ALLOC_CONF=max_split_size_mb:1024'

Linux:
>> export 'PYTORCH_CUDA_ALLOC_CONF=max_split_size_mb:1024'

🍀 The Memory error in the current version is only if you are using the StableDiffusion model. So change the model in the ‘settings’ to ldm or lama if you are doing only the basic operations

Updates to package :

Note : The package is actively being updated ( as of the day of publishing this article ), so watch out for more bug fixes and general improvements in their github repo ( https://github.com/Sanster/lama-cleaner )

Interested in similar topics ?

If you are interested in the use of AI in images/ graphics and design , here are some of the related articles —

  1. An advanced guide to writing prompts for Midjourney ( text-to-image)

2. How to fix bad face generations in midjourney.

3. How Adobe is implementing AI into its environment.

Machine Learning
Stable Diffusion
AI
Python
Ml So Good
Recommended from ReadMedium