File size: 3,776 Bytes
402ae9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
---
license: mit
language:
  - en
tags:
  - image-segmentation
  - chroma-key
  - green-screen
  - color-segmentation
  - pytorch
  - codelor-color
  - lightweight
pipeline_tag: image-segmentation
---

# Codelor Color 1.2

**Codelor Color** is a lightweight image segmentation model built specifically for **chroma key**: detecting and removing green from an image or video (green screens, leaves, fabric, green objects) and producing a clean alpha mask.

It takes an RGB image and a target color (green by default) and outputs a per-pixel binary mask.

---

## Model at a glance

- **Architecture**: Lightweight Transformer with linear attention and depthwise convolutions, optimized for CPU.
- **Parameters**: 1,641,055
- **File size**: ~6.3 MB in safetensors format.
- **Input**: RGB image (any size) + RGB triplet of the target color.
- **Output**: probability mask (1 channel, same resolution as input).
- **Use cases**: green screen, video chroma key, green object removal, color-similarity segmentation.

---

## Quick start

### Installation

You can install the standalone architecture package directly from PyPI:

    pip install codelor-color

Or install all the required dependencies to run this specific model:

    pip install torch safetensors huggingface_hub pillow numpy

### Load the model

You can use the standalone PyPI package, or load the architecture directly from this repository:

    from codelor_color import CodelorColor
    from safetensors.torch import load_file
    import torch

    # If loading from the HF repo directly:
    # from modeling_codelor import load_from_pretrained
    # model = load_from_pretrained("YOUR_USERNAME/codelor-color-1.2", device="cpu")

    # If using the PyPI package and local weights:
    model = CodelorColor(dim=128, depth=8, heads=8, tied_after=4)
    state_dict = load_file("model.safetensors")
    model.load_state_dict(state_dict)
    model.eval()

### Predict a mask on an image

    import torch
    from PIL import Image
    import numpy as np
    from modeling_codelor import predict_mask

    img = Image.open("photo.jpg").convert("RGB")
    x = torch.from_numpy(np.array(img)).float().permute(2, 0, 1) / 255.0

    mask, prob = predict_mask(model, x, color_rgb=(0.20, 0.85, 0.30), threshold=0.5)

    print("Green surface detected: {:.1f}%".format(mask.mean() * 100))

### Remove green from an image (RGBA output)

    from modeling_codelor import remove_green

    rgba = remove_green(x, model)
    rgba_np = (rgba.permute(1, 2, 0).numpy() * 255).astype("uint8")

    Image.fromarray(rgba_np, mode="RGBA").save("no_green.png")

---

## Performance and statistics

### Model metrics

| Metric | Value |
|---|---|
| Pixel accuracy | _to be filled_ |
| Mean IoU | _to be filled_ |
| Best loss | _to be filled_ |
| Parameters | 1,641,055 |
| File size | ~6.3 MB |
| CPU latency (160x160) | ~80-150 ms |

### Comparison with other approaches

| Method | Parameters | IoU / mIoU | CPU latency | Notes |
|---|---|---|---|---|
| K-means (HSV) | - | 0.642 (IoU) | very fast | Sensitive to lighting |
| U-Net | 34.5M | 0.794 (mIoU) | ~290 ms | Heavy baseline |
| Attention U-Net | 34.9M | 0.845 (IoU) | ~331 ms | Even heavier |
| BASNet | 87.1M | 0.561 (mIoU) | high | Very heavy |
| MobileSAM | 10.1M | ~0.80 (mIoU) | ~407 ms | Promptable, heavier |
| **Codelor Color 1.2** | **1.64M** | _to be filled_ | **~80-150 ms** | **CPU-friendly** |

---

## Architecture

- A **ConvNeXt-style convolutional stem** with anti-aliased downsampling.
- **8 Transformer blocks** with 2D RoPE and QK-Norm.
- A **color cross-attention** module injected at every block.
- A **gated FPN decoder** with deep supervision.
- An **edge head** (Sobel) to refine boundaries.
- **LayerScale** and **DropPath**.

---

## License

MIT.