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:
u2netpas the default model instead ofu2net- 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
u2netandu2netpin code, while keepingu2netpas 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 tou2netp.new_session("u2net")andnew_session("u2netp")are both supported in code.- The default build target packages only
u2netp, notu2net. - 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
rembgpackage asrembg/u2netp.onnxorrembg/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 onlyu2netp.onnxu2net- bundles onlyu2net.onnxboth- bundles both model filesnone- bundles no model file; use this only if you will mount models throughU2NET_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
u2netoru2netpat build time and the corresponding.onnxfile is missing, the build fails immediately. u2netp.onnxis tracked directly in the repository so the default light-model build works from a plain checkout.u2net.onnxis 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
bothunless 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:
bytesPIL.Image.Imagenumpy.ndarray
Supported output behavior:
remove(...)returns bytes for byte inputremove(...)returns aPIL.Image.Imagefor PIL inputremove(...)returns anumpy.ndarrayfor ndarray inputremove(..., 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.onnxis committed as a normal repository file so the default light-model path is easy to clone and build.u2net.onnxremains optional and heavyweight; it should only be supplied intentionally.- The default build selection is
u2netp;u2netis only packaged when explicitly selected. - Code support for
u2netremains available when the file is mounted or included in a custom build.
References
- Original upstream fork: https://github.com/rnag/rembg-aws-lambda
- Original
rembg: https://github.com/danielgatis/rembg - U-2-Net paper: https://arxiv.org/abs/2005.09007
- U-2-Net repository: https://github.com/xuebinqin/U-2-Net
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
80be6993bded92d0f3213a52e8721249edeba90688d9897928ac5c0918893f79
|
|
| MD5 |
fa27a838884e1115ec65ffbacbc960d0
|
|
| BLAKE2b-256 |
f65db4657e17e3361a728eeb8e1b5594ae5ce80088f432354814b2b282390f92
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
rembg_aws_lambda_lite-0.1.2.tar.gz -
Subject digest:
80be6993bded92d0f3213a52e8721249edeba90688d9897928ac5c0918893f79 - Sigstore transparency entry: 1805217667
- Sigstore integration time:
-
Permalink:
anurag-as/rembg-aws-lambda-lite@acaa76897377249211619405f5297d2ef3c47dc7 -
Branch / Tag:
refs/tags/v0.1.2 - Owner: https://github.com/anurag-as
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish_pypi.yml@acaa76897377249211619405f5297d2ef3c47dc7 -
Trigger Event:
push
-
Statement type:
File details
Details for the file rembg_aws_lambda_lite-0.1.2-py3-none-any.whl.
File metadata
- Download URL: rembg_aws_lambda_lite-0.1.2-py3-none-any.whl
- Upload date:
- Size: 4.2 MB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
65479e7e3eecbedcce1ba0012c457720da5dc06ea839a1a002fc89520de79da9
|
|
| MD5 |
7fe5181a554a1fa255f4e8ece7a16814
|
|
| BLAKE2b-256 |
41412a5bfd90fb1dd31c6603088e166e5dc5fb759edf2593e88ace6733218144
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
rembg_aws_lambda_lite-0.1.2-py3-none-any.whl -
Subject digest:
65479e7e3eecbedcce1ba0012c457720da5dc06ea839a1a002fc89520de79da9 - Sigstore transparency entry: 1805217676
- Sigstore integration time:
-
Permalink:
anurag-as/rembg-aws-lambda-lite@acaa76897377249211619405f5297d2ef3c47dc7 -
Branch / Tag:
refs/tags/v0.1.2 - Owner: https://github.com/anurag-as
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish_pypi.yml@acaa76897377249211619405f5297d2ef3c47dc7 -
Trigger Event:
push
-
Statement type: