Nodes/ComfyUI-DMENet/DMENet Focus/Defocus Map
ComfyUI Node

DMENet Focus/Defocus Map

The map that knows what's actually blurry

By MoRanYue·Created 3 months ago·Updated 3 months ago· 0
DMENet Focus/Defocus Map
  • image
  • focus_map
  • defocus_map
  • sigma_map_7_norm
checkpoint
normalizeraw
focus_gamma1.00
defocus_gamma1.00
smooth0
max_side0

You already know depth maps - white is close, black is far. DMENet Focus/Defocus Map is the cousin nobody warns you about: it estimates the opposite thing, not how far away something is but how out of focus it is. Same grayscale-map shape, completely different question. And the question matters, because a lot of what you'll want to do with a photo - selective sharpening, refocusing, deciding where a diffusion pass is allowed to redraw - depends on blur, not distance. A sharp subject at the back of the frame and a blurry leaf in the foreground can have almost identical depth values. They never share a defocus value.

The node is a PyTorch 2.x inference-only port of DMENet, a CVPR 2019 paper ("Deep Defocus Map Estimation Using Domain Adaptation"). The original was TensorFlow 1.15/TensorLayer, trained by generating synthetic defocus from depth maps plus a Gaussian blur, then adapting to real photos. This port keeps the network and drops everything else - training, the GAN/domain-adaptation losses, logging, MATLAB deconvolution. One-way street from image to map, and that's fine.

What you actually get

Three MASK outputs, and only one is the one you'll wire into anything:

  • focus_map - 1 - defocus_map, where 1 means in focus and 0 means out of focus. This is your mask. Feed it to a mask-based upscaler, a regional diffusion pass, or a ControlNet masking workflow.
  • defocus_map - the raw network output; higher values mean stronger estimated blur (bigger Gaussian sigma, if you want the paper jargon).
  • sigma_map_7_norm - the normalized blur-sigma visualization, matching the original evaluation script so your output lines up with the paper's figures. You'll almost never need it.

The inputs that matter

  • checkpoint - a single-choice dropdown whose label literally reads put_DMENet_BDCS.npz_or_converted_pt_in_ComfyUI_models_dmenet. That's the whole hint: the model file sits at ComfyUI/models/dmenet/DMENet_BDCS.npz (or .pt). If you don't have it, this is the thing that's broken.
  • normalize - raw (default, closest to the original network output) or minmax_per_image. The README is blunt about which to pick: if you're making a control mask, use minmax_per_image. It stretches the map per image so a diffusion pass sees contrast instead of a washed-out gradient.
  • focus_gamma / defocus_gamma (0.1–5, default 1) - gamma correction on the two maps. Crank one up to bias the mask toward more in-focus or more blurry pixels; fine for tuning, not required.
  • smooth (0–63, default 0) - a smoothing radius on the map. The README's advice here is worth quoting: "Use smooth and avoid early thresholding when the mask controls super-resolution or diffusion strength; soft maps reduce halos." Hard-threshold a soft mask and you get a bright ring around your subject. Leave it soft.
  • max_side (0–4096, default 0) - caps the longest image edge before inference. 0 means process at native size; set it when your input is huge and you just want the map, not a slower pass.

One preprocessing note: the original DMENet cropped inputs to a multiple of 16. This port pads to a multiple of 16 and crops back, so the output is exactly your input size. Deliberate fix, not a bug.

Install

The pack's a single repo, no exotic deps beyond what ComfyUI already has (torch 2.x):

cd ComfyUI/custom_nodes
git clone https://github.com/MoRanYue/ComfyUI-DMENet

then restart ComfyUI - or just search "ComfyUI-DMENet" in ComfyUI Manager and click install. After that, grab the checkpoint:

# from https://huggingface.co/MoRanYue/ComfyUI-DMENet
# put DMENet_BDCS.npz (or .pt) in ComfyUI/models/dmenet/

The node can load the original TensorLayer .npz directly, but startup is slow. Convert once:

cd ComfyUI/custom_nodes/ComfyUI-DMENet
python convert_npz.py ../../models/dmenet/DMENet_BDCS.npz ../../models/dmenet/DMENet_BDCS.pt

Issues that will actually bite you

  • Nothing connects until the checkpoint exists. The node loads the model file by that exact name and path; until ComfyUI/models/dmenet/ has one of the two files, the node errors immediately. The dropdown label is trying to tell you that in advance.
  • License. The original DMENet repo is GNU AGPLv3 and marked non-commercial. This port inherits both. If anything you ship commercially could touch it, get a separate license from the original authors first - this is not a "whatever, it's open source" situation.
  • Halos. If you've hard-thresholded a focus mask and your upscale looks like it has a glowing outline, that's the threshold talking. Soften the mask, add smooth, and don't binarize early.

Defocus estimation is a niche nobody's writing Reddit threads about - search the community and you'll mostly find people asking how to de-blur a DOF background. But if your workflow is "restore or refocus this photo but don't touch the parts the photographer already blurred," a focus mask from this node is the rare tool that actually answers that. It's the one I'd reach for.

Categoryimage/analysis

Inputs (7)

NameTypeDefaultDescription
imageIMAGE
checkpointCOMBO1 options: put_DMENet_BDCS.npz_or_converted_pt_in_ComfyUI_models_dmenet
normalizeCOMBOraw2 options: raw, minmax_per_image
focus_gammaFLOAT1.000.1–5
defocus_gammaFLOAT1.000.1–5
smoothINT00–63
max_sideINT00–4096

Outputs (3)

NameTypeDescription
focus_mapMASK
defocus_mapMASK
sigma_map_7_normMASK