Skip to content

Commit 317a869

Browse files
committedJan 4, 2024
Merge branch 'main' of https://github.com/compphoto/Intrinsic into main
2 parents aa84b9b + 91289ce commit 317a869

15 files changed

+276
-36
lines changed
 

‎LICENSE

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Copyright 2021, Chris Careaga, Yağız Aksoy, Computational Photography Laboratory. All rights reserved.
2+
3+
This software is for academic use only. A redistribution of this
4+
software, with or without modifications, has to be for academic
5+
use only, while giving the appropriate credit to the original
6+
authors of the software. The methods implemented as a part of
7+
this software may be covered under patents or patent applications.
8+
9+
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR IMPLIED
10+
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
11+
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR
12+
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
13+
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
14+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
15+
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
16+
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
17+
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

‎README.md

+99-31
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,118 @@
11
# Intrinsic Image Decomposition via Ordinal Shading
2-
Code for the paper: Intrinsic Image Decomposition via Ordinal Shading, [Chris Careaga](https://ccareaga.github.io/) and [Yağız Aksoy](https://yaksoy.github.io) , ACM Transactions on Graphics, 2023
3-
### [Project Page](https://yaksoy.github.io/intrinsic) | [Paper]() | [Video]() | [Supplementary]() | [Data]()
4-
5-
In this work, we achieve high-resolution intrinsic
6-
decomposition by breaking the problem into two parts. First, we present a
7-
dense ordinal shading formulation using a shift- and scale-invariant loss in
8-
order to estimate ordinal shading cues without restricting the predictions to
9-
obey the intrinsic model. We then combine low- and high-resolution ordinal
10-
estimations using a second network to generate a shading estimate with both
11-
global coherency and local details. We encourage the model to learn an ac-
12-
curate decomposition by computing losses on the estimated shading as well
13-
as the albedo implied by the intrinsic model. We develop a straightforward
14-
method for generating dense pseudo ground truth using our model’s pre-
15-
dictions and multi-illumination data, enabling generalization to in-the-wild
16-
imagery.
2+
Code for the paper: Intrinsic Image Decomposition via Ordinal Shading, [Chris Careaga](https://ccareaga.github.io/) and [Yağız Aksoy](https://yaksoy.github.io), ACM Transactions on Graphics, 2023
3+
### [Project Page](https://yaksoy.github.io/intrinsic) | [Paper](https://yaksoy.github.io/papers/TOG23-Intrinsic.pdf) | [Video](https://www.youtube.com/watch?v=pWtJd3hqL3c) | [Supplementary](https://yaksoy.github.io/papers/TOG23-Intrinsic-Supp.pdf) | [Data](https://github.com/compphoto/MIDIntrinsics)
4+
5+
We propose a method for generating high-resolution intrinsic image decompositions, for in-the-wild images. Our method relies on a carefully formulated ordinal shading representation, and real-world supervision from multi-illumination data in order to predict highly accurate albedo and shading.
6+
7+
[![YouTube Video](./figures/thumbnail.jpg)](https://www.youtube.com/watch?v=pWtJd3hqL3c)
8+
9+
10+
Try out our pipeline on your own images! [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/compphoto/Intrinsic/blob/main/intrinsic_inference.ipynb)
11+
12+
## Method
13+
The inherently under-constrained and scale-invariant nature of the intrinsic decomposition makes it a challenging problem.
14+
Shading, which represents complex interactions in the scene, is difficult for neural networks to predict.
15+
Compounded by the scarcity of dense ground-truth data, state-of-the-art models fail at high resolutions in real-world scenarios.
16+
17+
![intro_itw_comp_avo](./figures/intro_itw_comp_avo.png)
18+
19+
Our method focuses on generating high-resolution shading estimations, rather than attempting to estimate shading and albedo separately.
20+
Since shading values are unbounded, we develop a representation of shading values called "inverse shading" which maps the shading values into the zero-one range.
21+
This creates a balanced distribution of values in a well-defined range that is desirable for training neural networks.
22+
23+
![ordinal_shd_rep](./figures/ordinal_shd_rep.jpg)
24+
25+
Rather than directly regressing the inverse shading values, we relax the problem and aim to predict *ordinal* shading values.
26+
To do this, we train our network using shift- and scale-invariant loss functions.
27+
This simplifies the task of shading estimation as the model does not need to estimate precise values that satisfy the core intrinsic decomposition model
28+
29+
![ord_behavior_itw](./figures/ord_behavior_itw.png)
30+
31+
Our ordinal estimations exhibit specific behaviors at different resolutions.
32+
At low resolutions, the model can generate globally coherent predictions, but the outputs lack details.
33+
At high resolutions, the model can predict fine local details, but at the cost of global coherency.
34+
35+
![pool_table](./figures/pool_table.png)
36+
37+
To generate a final shading estimation we combine two ordinal estimations, at low and high resolutions, with the input image and send them through a second network.
38+
We use the final shading estimation, and the input image in order to compute our estimated albedo. This allows us to compute losses on both shading and albedo while
39+
using only a single network.
40+
41+
![network_pipeline_circles](./figures/network_pipeline_circles.jpg)
42+
43+
We train our method on multiple rendered datasets. In order to generate real-world supervision for our method we use multi-illumination data.
44+
Using our pipeline we estimate the albedo for each image in a given multi-illumination scene. By taking the median across these albedo estimations, small errors are removed resulting in a single accurate albedo.
45+
We use these 25,000 pseudo-ground-truth pairs as training data and continue training our pipeline.
46+
47+
![multi_illum_examples](./figures/multi_illum_examples.png)
48+
49+
Our method can be used for complex image editing tasks such as recoloring and relighting
50+
51+
![yellow_chair](./figures/yellow_chair.png)
52+
1753

1854
## Setup
1955
Depending on how you would like to use the code in this repository there are two options to setup the code.
2056
In either case, you should first create a fresh virtual environment (`python3 -m venv intrinsic_env`) and start it (`source intrinsic_env/bin/activate`)
2157

22-
#### Option 1
23-
If you would like to download the repository to run and make changes you can simply clone the repo:
58+
You can install this repository as a package using `pip`:
2459
```
2560
git clone https://github.com/compphoto/Intrinsic
2661
cd Intrinsic
62+
pip install .
2763
```
28-
then pip install all the dependencies of the repo:
29-
```
30-
pip install -r requirements.txt
31-
```
32-
33-
#### Option 2
34-
Alternatively, you can install this repository as a package using `setup.py`:
35-
```
36-
git clone https://github.com/compphoto/Intrinsic
37-
cd Intrinsic
38-
python setup.py
39-
```
64+
If you want to make changes to the code and have it reflected when you import the package use `pip install --editable`
4065
Or perform the same action without cloning the code using:
4166
```
42-
pip install https://github.com/compphoto/Intrinsic/archive/master.zip
67+
pip install https://github.com/compphoto/Intrinsic/archive/main.zip
4368
```
44-
This will allow you to import the repository as a python package, and use our pipeline as part of your codebase.
69+
This will allow you to import the repository as a Python package, and use our pipeline as part of your codebase.
4570

4671
## Inference
4772
To run our pipeline on your own images you can use the decompose script:
73+
```python
74+
from chrislib.general import view, tile_imgs, view_scale, uninvert
75+
from chrislib.data_util import load_image
76+
77+
from intrinsic.pipeline import run_pipeline
78+
from intrinsic.model_util import load_models
79+
80+
# load the models from the given paths
81+
models = load_models('final_weights.pt')
82+
83+
# load an image (np float array in [0-1])
84+
image = load_image('/path/to/input/image')
85+
86+
# run the model on the image using R_0 resizing
87+
results = run_pipeline(
88+
models,
89+
image,
90+
resize_conf=0.0,
91+
maintain_size=True
92+
)
93+
94+
albedo = results['albedo']
95+
inv_shd = results['inv_shading']
96+
97+
# compute shading from inverse shading
98+
shading = uninvert(inv_shd)
99+
48100
```
101+
This will run our pipeline and output the linear albedo and shading. You can run this in your browser as well! [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/compphoto/Intrinsic/blob/main/intrinsic_inference.ipynb)
102+
103+
## Citation
49104

50105
```
106+
@ARTICLE{careagaIntrinsic,
107+
author={Chris Careaga and Ya\u{g}{\i}z Aksoy},
108+
title={Intrinsic Image Decomposition via Ordinal Shading},
109+
journal={ACM Trans. Graph.},
110+
year={2023},
111+
}
112+
```
113+
114+
## License
115+
116+
This implementation is provided for academic use only. Please cite our paper if you use this code or any of the models.
117+
118+
The methodology presented in this work is safeguarded under intellectual property protection. For inquiries regarding licensing opportunities, kindly reach out to SFU Technology Licensing Office &#60;tlo_dir <i>ατ</i> sfu <i>δøτ</i> ca&#62; and Dr. Yağız Aksoy &#60;yagiz <i>ατ</i> sfu <i>δøτ</i> ca&#62;.

‎figures/avocado.png

2.85 MB
Loading

‎figures/intro_itw_comp_avo.png

1.99 MB
Loading

‎figures/multi_illum_examples.png

1.42 MB
Loading

‎figures/network_pipeline.png

5.54 MB
Loading

‎figures/network_pipeline_circles.jpg

457 KB
Loading

‎figures/ord_behavior_itw.png

1010 KB
Loading

‎figures/ordinal_shd_rep.jpg

331 KB
Loading

‎figures/ordinal_shd_rep_supp1.jpg

247 KB
Loading

‎figures/pool_table.png

4.88 MB
Loading

‎figures/thumbnail.jpg

499 KB
Loading

‎figures/yellow_chair.png

573 KB
Loading

‎intrinsic/model_util.py

+18-5
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22
from altered_midas.midas_net import MidasNet
33
from altered_midas.midas_net_custom import MidasNet_small
44

5-
def load_models(ord_path, iid_path, device='cuda'):
5+
def load_models(path, device='cuda'):
66
"""Load the ordinal network and the intrinsic decomposition network
77
into a dictionary that can be used to run our pipeline
88
99
params:
10-
ord_path (str): the path to the weights file for the ordinal model
11-
iid_path (str): the path to the weights file for the intrinsic decomposition model
10+
path (str or list): the path to the combined weights file, or to each individual weights file (ordinal first, then iid)
1211
device (str) optional: the device to run the model on (default "cuda")
1312
1413
returns:
@@ -18,13 +17,27 @@ def load_models(ord_path, iid_path, device='cuda'):
1817
"""
1918
models = {}
2019

20+
if isinstance(path, list):
21+
ord_state_dict = torch.load(path[0])
22+
iid_state_dict = torch.load(path[1])
23+
else:
24+
if path == 'paper_weights':
25+
combined_dict = torch.hub.load_state_dict_from_url('https://github.com/compphoto/Intrinsic/releases/download/v1.0/final_weights.pt', map_location=device, progress=True)
26+
elif path == 'rendered_only':
27+
combined_dict = torch.hub.load_state_dict_from_url('https://github.com/compphoto/Intrinsic/releases/download/v1.0/rendered_only_weights.pt', map_location=device, progress=True)
28+
else:
29+
combined_dict = torch.load(path)
30+
31+
ord_state_dict = combined_dict['ord_state_dict']
32+
iid_state_dict = combined_dict['iid_state_dict']
33+
2134
ord_model = MidasNet()
22-
ord_model.load_state_dict(torch.load(ord_path))
35+
ord_model.load_state_dict(ord_state_dict)
2336
ord_model.eval()
2437
ord_model = ord_model.to(device)
2538

2639
iid_model = MidasNet_small(exportable=False, input_channels=5, output_channels=1)
27-
iid_model.load_state_dict(torch.load(iid_path))
40+
iid_model.load_state_dict(iid_state_dict)
2841
iid_model.eval()
2942
iid_model = iid_model.to(device)
3043

‎intrinsic_inference.ipynb

+142
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
{
2+
"nbformat": 4,
3+
"nbformat_minor": 0,
4+
"metadata": {
5+
"colab": {
6+
"provenance": [],
7+
"gpuType": "T4"
8+
},
9+
"kernelspec": {
10+
"name": "python3",
11+
"display_name": "Python 3"
12+
},
13+
"language_info": {
14+
"name": "python"
15+
},
16+
"accelerator": "GPU"
17+
},
18+
"cells": [
19+
{
20+
"cell_type": "markdown",
21+
"source": [
22+
"Make sure to set the runtime to GPU: Runtime -> Change runtime type -> T4 GPU\n",
23+
"\n",
24+
"You can upload your own images, then change the relevant code cells to load it and send it through the model."
25+
],
26+
"metadata": {
27+
"id": "-lKPW1wZEulh"
28+
}
29+
},
30+
{
31+
"cell_type": "code",
32+
"execution_count": null,
33+
"metadata": {
34+
"id": "_cPkUxN-ikld"
35+
},
36+
"outputs": [],
37+
"source": [
38+
"# install the intrinsic decomposition repo from github\n",
39+
"!pip install https://github.com/compphoto/Intrinsic/archive/main.zip"
40+
]
41+
},
42+
{
43+
"cell_type": "code",
44+
"source": [
45+
"import torch\n",
46+
"\n",
47+
"# import some helper functions from chrislib (will be installed by the intrinsic repo)\n",
48+
"from chrislib.general import show, view, uninvert\n",
49+
"from chrislib.data_util import load_image\n",
50+
"\n",
51+
"# import model loading and running the pipeline\n",
52+
"from intrinsic.pipeline import run_pipeline\n",
53+
"from intrinsic.model_util import load_models"
54+
],
55+
"metadata": {
56+
"id": "J0gn82ZSjomn"
57+
},
58+
"execution_count": null,
59+
"outputs": []
60+
},
61+
{
62+
"cell_type": "code",
63+
"source": [
64+
"# download the pretrained weights and return the model (may take a bit to download weights)\n",
65+
"intrinsic_model = load_models('paper_weights')"
66+
],
67+
"metadata": {
68+
"id": "Ap3HubpwC_KG"
69+
},
70+
"execution_count": null,
71+
"outputs": []
72+
},
73+
{
74+
"cell_type": "code",
75+
"source": [
76+
"# load an example image from the github repo\n",
77+
"torch.hub.download_url_to_file('https://raw.githubusercontent.com/compphoto/Intrinsic/main/figures/avocado.png', 'avo.png')"
78+
],
79+
"metadata": {
80+
"id": "m_NYfDx0AhTw"
81+
},
82+
"execution_count": null,
83+
"outputs": []
84+
},
85+
{
86+
"cell_type": "code",
87+
"source": [
88+
"# load the image to run through the pipeline\n",
89+
"img = load_image('/content/avo.png')"
90+
],
91+
"metadata": {
92+
"id": "ALb4Pjfvj-MU"
93+
},
94+
"execution_count": null,
95+
"outputs": []
96+
},
97+
{
98+
"cell_type": "code",
99+
"source": [
100+
"# run the image through the pipeline (use R0 resizing dicussed in the paper)\n",
101+
"result = run_pipeline(\n",
102+
" intrinsic_model,\n",
103+
" img,\n",
104+
" resize_conf=0.0,\n",
105+
" maintain_size=True,\n",
106+
" linear=False,\n",
107+
" device='cuda'\n",
108+
")"
109+
],
110+
"metadata": {
111+
"id": "QW0TiFypkOj-"
112+
},
113+
"execution_count": null,
114+
"outputs": []
115+
},
116+
{
117+
"cell_type": "code",
118+
"source": [
119+
"# convert the inverse shading to regular shading for visualization\n",
120+
"shd = uninvert(result['inv_shading'])\n",
121+
"alb = result['albedo']"
122+
],
123+
"metadata": {
124+
"id": "XpYY2MNjkp2f"
125+
},
126+
"execution_count": null,
127+
"outputs": []
128+
},
129+
{
130+
"cell_type": "code",
131+
"source": [
132+
"# show the result (gamma corrects the linear intrinsic components and scales to [0-1])\n",
133+
"show([img, view(shd), view(alb)], size=(20, 7))"
134+
],
135+
"metadata": {
136+
"id": "8KKbyoVLki9s"
137+
},
138+
"execution_count": null,
139+
"outputs": []
140+
}
141+
]
142+
}

0 commit comments

Comments
 (0)
Please sign in to comment.