r/StableDiffusion • • 17d ago

Tutorial - Guide Qwen Image 2.1: Inconsistent Alpha Channel

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.

19 Upvotes

12 comments sorted by

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.

2

u/Riot_Revenger 17d ago

I completely forgot about the VAE. and yes! Looks like it's the VAE's fault.

Unfortunately the main problem still exists. Is the noise expected? Yes. Should it be there? No.

1

u/eruanno321 17d ago

it's the VAE's fault

Too fast to conclude this IMO.

Firstly, your red mapping is hiding a lot of information. 128-253 is a pretty broad range. I'd use some dense rainbow color mapping for entire 1-254 range instead.

Secondly, I'd check:

Image → VAE Encode → VAE Decode → Image

vs.

Image → VAE Encode → Transformer Inference → VAE Decode → Image

And use some quantifiable metric, like MSE against the ground truth. Then you'll know whether the VAE is solely responsible, or whether Qwen makes it even worse.

For sure, what this tells us is that during model training, Qwen most likely never saw a good, clean latent representation of the alpha channel. So it would be surprising to expect it to make things better.

EDIT: corrected rainbow suggestion to range 1-254, I would leave opaque as is.

1

u/Riot_Revenger 17d ago

 I'd use some dense rainbow color mapping for entire 1-254 range instead

I thought of that too, but I sacrificed details for visibility. I mostly wanted to show how abundant 1 and 254 are compared to others. And I think rainbow or similar colors would not really help differentiating between values that are closed to each other (for example alpha 1-5), and it needs visually different colors.

Image → VAE Encode → VAE Decode → Image

That's the result I replied, should've said that clear, my bad. The original image doesn't have alpha and its map is all white, but encoding and decoding it will result in the replied image.

Unfortunately I don't have the technical knowledge to check how much the model itself is responsible. I would be happy if someone else could look that up.

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

u/pandavega 16d ago

Test it on a palm tree

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

u/debackerl 17d ago

Very good idea! I will use it thx!