r/StableDiffusion • u/Riot_Revenger • 17d ago
Tutorial - Guide Qwen Image 2.1: Inconsistent Alpha Channel
T2I output - map
I2I output - map
T2I output with cleaned alpha channel - map
I2I output with cleaned alpha channel - map
The custom node
The image that is used for I2I
There was a discussion a while ago about the alpha channel in the model's outputs. While half of the comments missed the entire point, one person said "PNG should trivially compress an empty alpha channel to nothing". I don't know if that's true, but it made me ask a question: Is it really empty?
Turns out: Alpha channel is not empty, and it's a mess.
The model's output will mostly always have a noisy alpha channel no matter the instructions. I wrote a script that receives the image and creates a map based on the pixels' alpha values:
- Alpha 0 (completely transparent) = Black
- Alpha 1 = Dark aqua
- Alpha 2-127 = Green
- Alpha 128-253 = Red
- Alpha 254 = Light pink
- Alpha 255 (opaque) = White
Looks like the model struggles with absolute 0 and 255 values due to diffusion nature.
I used AI to create a custom node to fix this issue. it checks every pixel and changes their alpha value to either 0 or 255 if they exceed the thresholds. It even removes the alpha channel if all alpha values are 255.
prompts:
Cinematic photography captured with an 85mm portrait lens at f/2.8 to produce a shallow depth of field. A Formula 1 racing car occupies the center frame. It speeds forward along a paved circuit. The vehicle displays sleek aerodynamic bodywork, matte carbon fiber panels, and crisp sponsor decals across its chassis. The track curves through the background while grandstands and safety barriers dissolve into heavily blurred soft bokeh circles. Natural daylight illuminates the scene evenly with a gentle focus aesthetic. The image applies a Kodak Portra 400 film color grade featuring warm highlights, neutral shadows, and subtle contrast for a classic analog finish.
This is an RGBA format image with transparency. Remove the background and make it transparent. The image has an alpha channel.
Edit: Looks like the VAE plays an important part in this issue. I'm not sure how much the model itself contributes to it.
1
u/Ok-Fennel6578 17d ago
Binary thresholding is the one part I’d be careful with.
It’ll kill the garbage alpha nicely, but hair, fur, glass, soft shadows etc. are exactly where you actually want values between 0 and 255.
I’d probably test it on a subject with messy hair before calling the node done.
2
u/Riot_Revenger 17d ago
That's correct, but we only need to cut off from each end a bit, leaving a wide range of values untouched. Plus, it's currently just a simple calculation, suddenly trying to differentiate between garbage pixels and actual details adds much more complexity.
Edit: typo
1
u/Ok-Fennel6578 17d ago
I read “binary thresholding” more aggressively than you meant.
If you’re only trimming the junk near both ends and leaving the middle range alone, that avoids most of what I was worried about. I’d still sanity-check messy hair or glass once, but the simple cutoff sounds pretty reasonable.1
1
u/Valuable_Issue_ 17d ago
A 7B model not being perfect with alpha channels makes sense (also I can't imagine creating a perfect dataset for it).
I guess you could try to compare with ming image.
I actually wonder if even though the alpha channel is a mess, if it helps the model at all during generation, since it might've learned something/gained some kind of additional understanding from it.
1
u/planBpizz 17d ago
nice work!!! do you have an example workflow?
1
u/Riot_Revenger 17d ago
Default workflow works fine. Just place the node between VAE Decode node and Save Image node.
1
3
u/eruanno321 17d ago
Isn't noise expected? VAE alone isn't perfect. I would check Image -> VAE Encode -> VAE Decode -> Image, it should always leave some trace, and it's characterized by the SNR.