Skip to main content

Remove image background

Project description

rembg-aws-lambda-lite

Minimal rembg fork for Lambda-style CPU inference.

This repository is a modified fork of rnag/rembg-aws-lambda. It keeps the original MIT license and copyright notices, but narrows the package to the lightweight u2netp model path.

Why this fork exists

This fork is not universally better than rnag/rembg-aws-lambda. It is better for a narrower use case: CPU-only AWS Lambda deployments that want explicit model packaging, lighter default artifacts, and current Python support.

Compared with the upstream fork, this repository adds:

  • u2netp as the default model instead of u2net
  • build-time model packaging selection through REMBG_PACKAGE_MODELS
  • explicit failure when a selected model file is missing or still only a Git LFS pointer
  • support for both u2net and u2netp in code, while keeping u2netp as the default packaging target
  • a normal tracked default light-model asset, instead of forcing Git LFS for the lightweight path
  • Python support through 3.12
  • CPU-first ONNX Runtime provider selection, which fits Lambda inference better than taking every available local provider

Compared with the upstream README and packaging behavior, this fork also documents an important operational constraint more honestly: model selection at runtime and model packaging at build time are separate concerns.

What changed

  • new_session() now defaults to u2netp.
  • new_session("u2net") and new_session("u2netp") are both supported in code.
  • The default build target packages only u2netp, not u2net.
  • Python support is updated for Python through 3.12.
  • The package no longer relies on runtime model downloads.

Supported runtime

  • CPU only
  • AWS Lambda base images such as public.ecr.aws/lambda/python:3.12
  • Local development on Python through 3.12

Installation

pip install rembg-aws-lambda-lite

This fork expects the requested model file to be present either:

  • inside the installed rembg package as rembg/u2netp.onnx or rembg/u2net.onnx, or
  • in the directory pointed to by U2NET_HOME

If the requested model file is missing, new_session() raises FileNotFoundError immediately.

Build-time model selection

Bundled model selection is controlled at build time with REMBG_PACKAGE_MODELS.

Supported values:

  • u2netp - default, bundles only u2netp.onnx
  • u2net - bundles only u2net.onnx
  • both - bundles both model files
  • none - bundles no model file; use this only if you will mount models through U2NET_HOME

Examples:

REMBG_PACKAGE_MODELS=u2netp python -m build
REMBG_PACKAGE_MODELS=u2net python -m build
REMBG_PACKAGE_MODELS=both python -m build
REMBG_PACKAGE_MODELS=none python -m build

Docker example:

ARG REMBG_PACKAGE_MODELS=u2netp
ENV REMBG_PACKAGE_MODELS=${REMBG_PACKAGE_MODELS}

RUN python -m pip install --no-cache-dir . --target "${LAMBDA_TASK_ROOT}"

Important:

  • Runtime model selection does not change packaged contents.
  • If you request u2net or u2netp at build time and the corresponding .onnx file is missing, the build fails immediately.
  • u2netp.onnx is tracked directly in the repository so the default light-model build works from a plain checkout.
  • u2net.onnx is still a heavyweight optional path. If you select it and the file is only a Git LFS pointer in your checkout, the build fails until you fetch the real object.
  • If you care about Lambda cold starts, do not build with both unless you actually need a single artifact to contain both models.

Library usage

from rembg import new_session, remove

session = new_session()
cutout = remove(input_image, session=session)

Explicit model selection:

from rembg import new_session

session = new_session("u2netp")

If you need the larger model and have supplied the file:

from rembg import new_session

session = new_session("u2net")

Supported input types:

  • bytes
  • PIL.Image.Image
  • numpy.ndarray

Supported output behavior:

  • remove(...) returns bytes for byte input
  • remove(...) returns a PIL.Image.Image for PIL input
  • remove(...) returns a numpy.ndarray for ndarray input
  • remove(..., only_mask=True) returns the predicted mask

Provider selection

This fork is intended for CPU-only Lambda use. By default it prefers CPUExecutionProvider when ONNX Runtime exposes multiple providers.

If you need to override that behavior, set REMBG_ONNX_PROVIDERS to a comma-separated list of ONNX Runtime provider names:

REMBG_ONNX_PROVIDERS=CPUExecutionProvider python your_script.py

Docker smoke test

FROM public.ecr.aws/lambda/python:3.10

ENV OMP_NUM_THREADS=1
ENV REMBG_PACKAGE_MODELS=u2netp

RUN python -m pip install --no-cache-dir rembg-aws-lambda-lite --target "${LAMBDA_TASK_ROOT}" \
    && python -c "from rembg import new_session; new_session('u2netp')"

The default u2netp smoke test works from a plain checkout because the light model is stored directly in the repository. A u2net or both build still requires the heavyweight model object to be present locally.

Development notes

  • u2netp.onnx is committed as a normal repository file so the default light-model path is easy to clone and build.
  • u2net.onnx remains optional and heavyweight; it should only be supplied intentionally.
  • The default build selection is u2netp; u2net is only packaged when explicitly selected.
  • Code support for u2net remains available when the file is mounted or included in a custom build.

References

License

Licensed under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rembg_aws_lambda_lite-0.1.2.tar.gz (4.3 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rembg_aws_lambda_lite-0.1.2-py3-none-any.whl (4.2 MB view details)

Uploaded Python 3

File details

Details for the file rembg_aws_lambda_lite-0.1.2.tar.gz.

File metadata

  • Download URL: rembg_aws_lambda_lite-0.1.2.tar.gz
  • Upload date:
  • Size: 4.3 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for rembg_aws_lambda_lite-0.1.2.tar.gz
Algorithm Hash digest
SHA256 80be6993bded92d0f3213a52e8721249edeba90688d9897928ac5c0918893f79
MD5 fa27a838884e1115ec65ffbacbc960d0
BLAKE2b-256 f65db4657e17e3361a728eeb8e1b5594ae5ce80088f432354814b2b282390f92

See more details on using hashes here.

Provenance

The following attestation bundles were made for rembg_aws_lambda_lite-0.1.2.tar.gz:

Publisher: publish_pypi.yml on anurag-as/rembg-aws-lambda-lite

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rembg_aws_lambda_lite-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for rembg_aws_lambda_lite-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 65479e7e3eecbedcce1ba0012c457720da5dc06ea839a1a002fc89520de79da9
MD5 7fe5181a554a1fa255f4e8ece7a16814
BLAKE2b-256 41412a5bfd90fb1dd31c6603088e166e5dc5fb759edf2593e88ace6733218144

See more details on using hashes here.

Provenance

The following attestation bundles were made for rembg_aws_lambda_lite-0.1.2-py3-none-any.whl:

Publisher: publish_pypi.yml on anurag-as/rembg-aws-lambda-lite

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page