Skip to content

Commit 6b2b4c2

Browse files
committed
notebook for recoloring
1 parent 8f95cfd commit 6b2b4c2

File tree

1 file changed

+57
-40
lines changed

1 file changed

+57
-40
lines changed

recoloring.ipynb

+57-40
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"colab": {
66
"provenance": [],
77
"gpuType": "T4",
8+
"authorship_tag": "ABX9TyPD3pXRpOuigYiFeN4QIF32",
89
"include_colab_link": true
910
},
1011
"kernelspec": {
@@ -27,27 +28,16 @@
2728
"<a href=\"https://colab.research.google.com/github/compphoto/Intrinsic/blob/main/recoloring.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
2829
]
2930
},
30-
{
31-
"cell_type": "markdown",
32-
"source": [
33-
"Make sure to set the runtime to GPU: Runtime -> Change runtime type -> T4 GPU\n",
34-
"\n",
35-
"You can upload your own images, then change the relevant code cells to load it and send it through the model."
36-
],
37-
"metadata": {
38-
"id": "-lKPW1wZEulh"
39-
}
40-
},
4131
{
4232
"cell_type": "code",
4333
"execution_count": null,
4434
"metadata": {
45-
"id": "_cPkUxN-ikld"
35+
"id": "nnOA8b8E3Y6U"
4636
},
4737
"outputs": [],
4838
"source": [
49-
"# install the intrinsic decomposition repo from github\n",
50-
"!pip install https://github.com/compphoto/Intrinsic/archive/main.zip"
39+
"!git clone https://github.com/compphoto/Intrinsic\n",
40+
"!cd Intrinsic/ && pip install ."
5141
]
5242
},
5343
{
@@ -56,95 +46,122 @@
5646
"import torch\n",
5747
"\n",
5848
"# import some helper functions from chrislib (will be installed by the intrinsic repo)\n",
59-
"from chrislib.general import show, view, uninvert\n",
49+
"from chrislib.general import show, view, uninvert, match_scale\n",
6050
"from chrislib.data_util import load_image\n",
6151
"\n",
6252
"# import model loading and running the pipeline\n",
6353
"from intrinsic.pipeline import run_pipeline\n",
6454
"from intrinsic.model_util import load_models"
6555
],
6656
"metadata": {
67-
"id": "J0gn82ZSjomn"
57+
"id": "mS9AFGEj3jhc"
6858
},
6959
"execution_count": null,
7060
"outputs": []
7161
},
7262
{
7363
"cell_type": "code",
7464
"source": [
75-
"# download the pretrained weights and return the model (may take a bit to download weights)\n",
7665
"intrinsic_model = load_models('paper_weights')"
7766
],
7867
"metadata": {
79-
"id": "Ap3HubpwC_KG"
68+
"id": "nw0poq363mqy"
8069
},
8170
"execution_count": null,
8271
"outputs": []
8372
},
8473
{
8574
"cell_type": "code",
8675
"source": [
87-
"# load an example image from the github repo\n",
88-
"torch.hub.download_url_to_file('https://raw.githubusercontent.com/compphoto/Intrinsic/main/figures/avocado.png', 'avo.png')"
76+
"# three different example scenes from the paper\n",
77+
"scene_name = 'yellow_chair'\n",
78+
"# scene_name = 'brown_chairs'\n",
79+
"# scene_name = 'spain_museum'"
8980
],
9081
"metadata": {
91-
"id": "m_NYfDx0AhTw"
82+
"id": "l5YBpZ3a5rfS"
9283
},
9384
"execution_count": null,
9485
"outputs": []
9586
},
9687
{
9788
"cell_type": "code",
9889
"source": [
99-
"# load the image to run through the pipeline\n",
100-
"img = load_image('/content/avo.png')"
90+
"inp = load_image(f'Intrinsic/examples/{scene_name}/input.png')[:, :, :3]\n",
91+
"msk = load_image(f'Intrinsic/examples/{scene_name}/mask.png')[:, :, :3]\n",
92+
"tex = load_image(f'Intrinsic/examples/{scene_name}/texture.png')[:, :, :3] ** 2.2"
10193
],
10294
"metadata": {
103-
"id": "ALb4Pjfvj-MU"
95+
"id": "zmWeGYSP77W4"
10496
},
10597
"execution_count": null,
10698
"outputs": []
10799
},
108100
{
109101
"cell_type": "code",
110102
"source": [
111-
"# run the image through the pipeline (use R0 resizing dicussed in the paper)\n",
112-
"result = run_pipeline(\n",
103+
"results = run_pipeline(\n",
113104
" intrinsic_model,\n",
114-
" img,\n",
115-
" resize_conf=0.0,\n",
116-
" maintain_size=True,\n",
117-
" linear=False,\n",
118-
" device='cuda'\n",
119-
")"
105+
" inp,\n",
106+
" resize_conf=None,\n",
107+
" maintain_size=True\n",
108+
")\n",
109+
"\n",
110+
"alb = results['albedo']\n",
111+
"image = results['image']\n",
112+
"inv_shd = results['inv_shading']\n",
113+
"\n",
114+
"shd = uninvert(inv_shd)[:, :, None]"
120115
],
121116
"metadata": {
122-
"id": "QW0TiFypkOj-"
117+
"id": "pDr4Wh7M5pIE"
123118
},
124119
"execution_count": null,
125120
"outputs": []
126121
},
127122
{
128123
"cell_type": "code",
129124
"source": [
130-
"# convert the inverse shading to regular shading for visualization\n",
131-
"shd = uninvert(result['inv_shading'])\n",
132-
"alb = result['albedo']"
125+
"def perform_recolor(msk, alb, shd, shd_power=1.0, recolor=None):\n",
126+
" # this function will perform the illumination-aware recoloring, or apply a shading curve\n",
127+
" # msk - numpy array (HxWx1) denoting the region to perform the edit\n",
128+
" # alb - linear albedo of the image\n",
129+
" # shd - linear shading of the image\n",
130+
" # shd_power - exponent to apply to the shading (<1 for more diffuse, >1 for more specular)\n",
131+
" # recolor - a texture to apply to the edited region, no recoloring is performed if set to None\n",
132+
"\n",
133+
" if recolor is None:\n",
134+
" our_new_alb = alb\n",
135+
" else:\n",
136+
" # we match the scale of the texture to the albedo in the edited region to\n",
137+
" # ensure the appearance of the region is maintained, but this can be altered\n",
138+
" recolor = match_scale(recolor, alb, msk.astype(bool))\n",
139+
" our_new_alb = ((1.0 - msk) * alb) + (msk * recolor)\n",
140+
"\n",
141+
" # apply exponentiation to the shading of the region and composite\n",
142+
" masked_shd = msk * (shd ** shd_power)\n",
143+
" new_shd = ((1.0 - msk) * shd) + masked_shd\n",
144+
"\n",
145+
" # combine edited albedo and shading, gamma correct and clip\n",
146+
" recolored = (our_new_alb * new_shd) ** (1/2.2)\n",
147+
"\n",
148+
" return recolored.clip(0, 1)"
133149
],
134150
"metadata": {
135-
"id": "XpYY2MNjkp2f"
151+
"id": "kip8Y1tE83Zi"
136152
},
137153
"execution_count": null,
138154
"outputs": []
139155
},
140156
{
141157
"cell_type": "code",
142158
"source": [
143-
"# show the result (gamma corrects the linear intrinsic components and scales to [0-1])\n",
144-
"show([img, view(shd), view(alb)], size=(20, 7))"
159+
"# NOTE: setting the shading exponent to >1 will make the shading appear more specular,\n",
160+
"# but small errors in the shading (albedo leakage) will be amplified in some cases\n",
161+
"show(perform_recolor(msk, alb, shd, 1.0, recolor=tex))"
145162
],
146163
"metadata": {
147-
"id": "8KKbyoVLki9s"
164+
"id": "oF2ljuOv84WA"
148165
},
149166
"execution_count": null,
150167
"outputs": []

0 commit comments

Comments
 (0)