colorization-deploy-v2
#1
by omrope792 - opened
- .gitattributes +2 -0
- submissions/colorization/LICENSE +23 -0
- submissions/colorization/README.md +37 -0
- submissions/colorization/colorization_deploy_v2_2026april.onnx +3 -0
- submissions/colorization/demo.py +42 -0
- submissions/colorization/example_outputs/input_image.jpg +3 -0
- submissions/colorization/example_outputs/output_image.png +3 -0
- submissions/colorization/model_conversion.py +48 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
*.jpg filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
*.png filter=lfs diff=lfs merge=lfs -text
|
submissions/colorization/LICENSE
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Copyright (c) 2016, Richard Zhang, Phillip Isola, Alexei A. Efros
|
| 2 |
+
All rights reserved.
|
| 3 |
+
|
| 4 |
+
Redistribution and use in source and binary forms, with or without
|
| 5 |
+
modification, are permitted provided that the following conditions are met:
|
| 6 |
+
|
| 7 |
+
* Redistributions of source code must retain the above copyright notice, this
|
| 8 |
+
list of conditions and the following disclaimer.
|
| 9 |
+
|
| 10 |
+
* Redistributions in binary form must reproduce the above copyright notice,
|
| 11 |
+
this list of conditions and the following disclaimer in the documentation
|
| 12 |
+
and/or other materials provided with the distribution.
|
| 13 |
+
|
| 14 |
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
| 15 |
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
| 16 |
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
| 17 |
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
| 18 |
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
| 19 |
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
| 20 |
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
| 21 |
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
| 22 |
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
| 23 |
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
submissions/colorization/README.md
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Colorization
|
| 2 |
+
|
| 3 |
+
Automatic colorization of grayscale images using a CNN trained on ImageNet.
|
| 4 |
+
Based on the paper: [Colorful Image Colorization](http://richzhang.github.io/colorization/)
|
| 5 |
+
by Richard Zhang, Phillip Isola, Alexei A. Efros (ECCV 2016).
|
| 6 |
+
|
| 7 |
+
The network takes the L channel of a LAB image as input and predicts the
|
| 8 |
+
ab channels, which are then merged back with L to produce a full-color output.
|
| 9 |
+
|
| 10 |
+
## Model Details
|
| 11 |
+
- **Architecture**: Custom CNN (VGG-style encoder + dilated convolutions)
|
| 12 |
+
- **Input**: Grayscale image (L channel of LAB), 224×224
|
| 13 |
+
- **Output**: ab channels, upsampled to original size
|
| 14 |
+
- **Framework**: ONNX (converted from original Caffe model)
|
| 15 |
+
- **Original weights**: http://eecs.berkeley.edu/~rich.zhang/projects/2016_colorization/
|
| 16 |
+
|
| 17 |
+
## Usage
|
| 18 |
+
|
| 19 |
+
### Python
|
| 20 |
+
```bash
|
| 21 |
+
python demo.py --model colorization_deploy_v2_2026april.onnx --image example_outputs/input_image.jpg --output example_outputs/output_image.png
|
| 22 |
+
```
|
| 23 |
+
|
| 24 |
+
Or import directly:
|
| 25 |
+
```python
|
| 26 |
+
import cv2
|
| 27 |
+
|
| 28 |
+
net = cv2.dnn.readNet("colorization_deploy_v2_2026apr.onnx")
|
| 29 |
+
# see demo.py for full inference pipeline
|
| 30 |
+
```
|
| 31 |
+
|
| 32 |
+
## License
|
| 33 |
+
See [LICENSE](./LICENSE) — original model is released under BSD license by Richard Zhang.
|
| 34 |
+
|
| 35 |
+
## References
|
| 36 |
+
- Paper: https://arxiv.org/abs/1603.08511
|
| 37 |
+
- Original repo: https://github.com/richzhang/colorization
|
submissions/colorization/colorization_deploy_v2_2026april.onnx
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:2539e993e890cc67f207109df39cb3d9858fae321b53a5c8ccc626c52ae08b4d
|
| 3 |
+
size 128964893
|
submissions/colorization/demo.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Grayscale image colorization using colorization_deploy_v2 ONNX model.
|
| 3 |
+
Usage:
|
| 4 |
+
python demo.py --model colorization_deploy_v2_2026april.onnx --image example_outputs/input_image.jpg
|
| 5 |
+
"""
|
| 6 |
+
import argparse
|
| 7 |
+
import numpy as np
|
| 8 |
+
import cv2 as cv
|
| 9 |
+
|
| 10 |
+
def colorize(model_path: str, image_path: str, output_path: str = "example_outputs/output_image.png"):
|
| 11 |
+
net = cv.dnn.readNet(model_path)
|
| 12 |
+
|
| 13 |
+
img = cv.imread(image_path)
|
| 14 |
+
if img is None:
|
| 15 |
+
print(f"Error: Could not read image at {image_path}")
|
| 16 |
+
return
|
| 17 |
+
|
| 18 |
+
img_rgb = (img[:, :, ::-1] * 1.0 / 255).astype(np.float32)
|
| 19 |
+
img_lab = cv.cvtColor(img_rgb, cv.COLOR_RGB2Lab)
|
| 20 |
+
|
| 21 |
+
img_l = img_lab[:, :, 0] # L channel
|
| 22 |
+
img_l_rs = cv.resize(img_l, (224, 224))
|
| 23 |
+
img_l_rs -= 50.0 # mean-center subtraction
|
| 24 |
+
|
| 25 |
+
net.setInput(cv.dnn.blobFromImage(img_l_rs))
|
| 26 |
+
ab_dec = net.forward()[0, :, :, :].transpose((1, 2, 0))
|
| 27 |
+
|
| 28 |
+
(h, w) = img_rgb.shape[:2]
|
| 29 |
+
ab_dec_us = cv.resize(ab_dec, (w, h))
|
| 30 |
+
img_lab_out = np.concatenate((img_l[:, :, np.newaxis], ab_dec_us), axis=2)
|
| 31 |
+
|
| 32 |
+
img_bgr_out = np.clip(cv.cvtColor(img_lab_out, cv.COLOR_Lab2BGR), 0, 1)
|
| 33 |
+
cv.imwrite(output_path, (img_bgr_out * 255).astype(np.uint8))
|
| 34 |
+
print(f"Saved colorized image to {output_path}")
|
| 35 |
+
|
| 36 |
+
if __name__ == "__main__":
|
| 37 |
+
parser = argparse.ArgumentParser(description="Colorize a grayscale image")
|
| 38 |
+
parser.add_argument("--model", required=True, help="Path to .onnx model")
|
| 39 |
+
parser.add_argument("--image", required=True, help="Path to input image")
|
| 40 |
+
parser.add_argument("--output", default="example_outputs/output_image.png", help="Output image path")
|
| 41 |
+
args = parser.parse_args()
|
| 42 |
+
colorize(args.model, args.image, args.output)
|
submissions/colorization/example_outputs/input_image.jpg
ADDED
|
Git LFS Details
|
submissions/colorization/example_outputs/output_image.png
ADDED
|
Git LFS Details
|
submissions/colorization/model_conversion.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
import numpy as np
|
| 4 |
+
from colorization.colorizers import eccv16
|
| 5 |
+
|
| 6 |
+
class ExactCaffeMatch(nn.Module):
|
| 7 |
+
def __init__(self):
|
| 8 |
+
super().__init__()
|
| 9 |
+
self.core = eccv16(pretrained=True).eval()
|
| 10 |
+
|
| 11 |
+
# Load the color palette kernel
|
| 12 |
+
pts_in_hull = np.load('opencv_extra/testdata/dnn/colorization_pts_in_hull.npy')
|
| 13 |
+
|
| 14 |
+
weight_tensor = torch.tensor(pts_in_hull.flatten()).float().view(2, 313, 1, 1)
|
| 15 |
+
self.register_buffer('decode_weight', weight_tensor)
|
| 16 |
+
|
| 17 |
+
def forward(self, x):
|
| 18 |
+
x = x / 100.0
|
| 19 |
+
x = self.core.model1(x)
|
| 20 |
+
x = self.core.model2(x)
|
| 21 |
+
x = self.core.model3(x)
|
| 22 |
+
x = self.core.model4(x)
|
| 23 |
+
x = self.core.model5(x)
|
| 24 |
+
x = self.core.model6(x)
|
| 25 |
+
x = self.core.model7(x)
|
| 26 |
+
x = self.core.model8(x)
|
| 27 |
+
|
| 28 |
+
# 1. Apply Caffe temperature scaling
|
| 29 |
+
x = x * 2.606# 2. Softmax
|
| 30 |
+
x = torch.softmax(x, dim=1)
|
| 31 |
+
|
| 32 |
+
x = torch.nn.functional.conv2d(x, self.decode_weight)
|
| 33 |
+
|
| 34 |
+
return x
|
| 35 |
+
|
| 36 |
+
model = ExactCaffeMatch()
|
| 37 |
+
dummy_input = torch.randn(1, 1, 224, 224)
|
| 38 |
+
|
| 39 |
+
torch.onnx.export(
|
| 40 |
+
model, dummy_input,
|
| 41 |
+
"colorization/colorization_deploy_v2_2026april.onnx",
|
| 42 |
+
export_params=True,
|
| 43 |
+
opset_version=11,
|
| 44 |
+
do_constant_folding=True,
|
| 45 |
+
input_names=['data_l'],
|
| 46 |
+
output_names=['class8_ab'],
|
| 47 |
+
operator_export_type=torch.onnx.OperatorExportTypes.ONNX_FALLTHROUGH
|
| 48 |
+
)
|