Skip to content

Commit ced9fb8

Browse files
Fabian Mentzercopybara-github
authored andcommitted
Update hific colab to work with TFC 2.0.
PiperOrigin-RevId: 363445673 Change-Id: Ifba60b483cd8f9f3660e42c289881b8db693ff79
1 parent 198fe48 commit ced9fb8

File tree

1 file changed

+58
-63
lines changed

1 file changed

+58
-63
lines changed

models/hific/colab.ipynb

Lines changed: 58 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,4 @@
11
{
2-
"nbformat": 4,
3-
"nbformat_minor": 0,
4-
"metadata": {
5-
"colab": {
6-
"name": "HiFiC_Interactive_Colab.ipynb",
7-
"provenance": [],
8-
"collapsed_sections": [],
9-
"toc_visible": true
10-
},
11-
"kernelspec": {
12-
"display_name": "Python 3",
13-
"name": "python3"
14-
},
15-
"accelerator": "GPU"
16-
},
172
"cells": [
183
{
194
"cell_type": "markdown",
@@ -40,18 +25,17 @@
4025
},
4126
{
4227
"cell_type": "code",
28+
"execution_count": null,
4329
"metadata": {
4430
"id": "LO_MNEQ7Bhbw"
4531
},
32+
"outputs": [],
4633
"source": [
47-
"%tensorflow_version 1.x\n",
48-
"!pip install tensorflow-compression\\<2.0\n",
34+
"!pip install tensorflow-compression\n",
4935
"![[ -e /tfc ]] || git clone https://github.com/tensorflow/compression /tfc\n",
5036
"%cd /tfc/models\n",
51-
"import tfci # Check if tfci.py is available.\n"
52-
],
53-
"execution_count": null,
54-
"outputs": []
37+
"import tfci # Check if tfci.py is available."
38+
]
5539
},
5640
{
5741
"cell_type": "markdown",
@@ -68,19 +52,19 @@
6852
},
6953
{
7054
"cell_type": "code",
55+
"execution_count": null,
7156
"metadata": {
7257
"id": "x-yLUG_tmo3M"
7358
},
59+
"outputs": [],
7460
"source": [
7561
"import tensorflow as tf\n",
7662
"\n",
77-
"if not tf.test.is_gpu_available():\n",
63+
"if not tf.config.list_physical_devices('GPU'):\n",
7864
" print('WARNING: No GPU found. Might be slow!')\n",
7965
"else:\n",
8066
" print('Found GPU.')"
81-
],
82-
"execution_count": null,
83-
"outputs": []
67+
]
8468
},
8569
{
8670
"cell_type": "markdown",
@@ -93,9 +77,11 @@
9377
},
9478
{
9579
"cell_type": "code",
80+
"execution_count": null,
9681
"metadata": {
9782
"id": "vtd1l70Pf95V"
9883
},
84+
"outputs": [],
9985
"source": [
10086
"import os\n",
10187
"import zipfile\n",
@@ -121,20 +107,18 @@
121107
"File = collections.namedtuple('File', ['full_path', 'num_bytes', 'bpp'])\n",
122108
"\n",
123109
"def print_html(html):\n",
124-
" display(HTML(html + '<br/>'))\n",
110+
" display(HTML(html + '\u003cbr/\u003e'))\n",
125111
"\n",
126112
"def make_cell_large():\n",
127113
" display(Javascript(\n",
128114
" '''google.colab.output.setIframeHeight(0, true, {maxHeight: 5000})'''))\n",
129115
"\n",
130116
"def get_default_image(output_dir):\n",
131117
" output_path = os.path.join(output_dir, os.path.basename(DEFAULT_IMAGE_URL))\n",
132-
" print('Downloading', DEFAULT_IMAGE_URL, '\\n->', output_path)\n",
118+
" print('Downloading', DEFAULT_IMAGE_URL, '\\n-\u003e', output_path)\n",
133119
" urllib.request.urlretrieve(DEFAULT_IMAGE_URL, output_path)\n",
134120
"\n"
135-
],
136-
"execution_count": null,
137-
"outputs": []
121+
]
138122
},
139123
{
140124
"cell_type": "markdown",
@@ -147,9 +131,11 @@
147131
},
148132
{
149133
"cell_type": "code",
134+
"execution_count": null,
150135
"metadata": {
151136
"id": "NgtIlL2ADCI2"
152137
},
138+
"outputs": [],
153139
"source": [
154140
"#@title Setup { vertical-output: false, run: \"auto\", display-mode: \"form\" }\n",
155141
"#@markdown #### Custom Images\n",
@@ -173,30 +159,30 @@
173159
"\n",
174160
"model = 'hific-lo' #@param [\"hific-lo\", \"hific-mi\", \"hific-hi\"]\n",
175161
"\n"
176-
],
177-
"execution_count": null,
178-
"outputs": []
162+
]
179163
},
180164
{
181165
"cell_type": "code",
166+
"execution_count": null,
182167
"metadata": {
183168
"id": "GYcbc2HupTRD"
184169
},
170+
"outputs": [],
185171
"source": [
186172
"if 'upload_custom_images' not in locals():\n",
187173
" print('ERROR: Please run the previous cell!')\n",
188174
" # Setting defaults anyway.\n",
189175
" upload_custom_images = False\n",
190176
" model = 'hific-lo'"
191-
],
192-
"execution_count": null,
193-
"outputs": []
177+
]
194178
},
195179
{
196180
"cell_type": "code",
181+
"execution_count": null,
197182
"metadata": {
198183
"id": "e0C4vMqZsnqA"
199184
},
185+
"outputs": [],
200186
"source": [
201187
"all_files = os.listdir(FILES_DIR)\n",
202188
"if not upload_custom_images or not all_files:\n",
@@ -213,22 +199,7 @@
213199
" img = img.resize((w // 15, h // 15))\n",
214200
" print('- ' + file_name + ':')\n",
215201
" display(img)"
216-
],
217-
"execution_count": null,
218-
"outputs": []
219-
},
220-
{
221-
"cell_type": "code",
222-
"metadata": {
223-
"id": "46eq2rCRpXzC"
224-
},
225-
"source": [
226-
"print(f'Caching model \"{model}\"...')\n",
227-
"tfci.import_metagraph(model)\n",
228-
"print('Done')"
229-
],
230-
"execution_count": null,
231-
"outputs": []
202+
]
232203
},
233204
{
234205
"cell_type": "markdown",
@@ -241,9 +212,11 @@
241212
},
242213
{
243214
"cell_type": "code",
215+
"execution_count": null,
244216
"metadata": {
245217
"id": "kd02HOhLBj6e"
246218
},
219+
"outputs": [],
247220
"source": [
248221
"SUPPORTED_EXT = {'.png', '.jpg'}\n",
249222
"\n",
@@ -296,9 +269,7 @@
296269
" get_bpp(Image.open(full_path).size, num_bytes)))\n",
297270
"\n",
298271
"print('All done!')"
299-
],
300-
"execution_count": null,
301-
"outputs": []
272+
]
302273
},
303274
{
304275
"cell_type": "markdown",
@@ -311,20 +282,20 @@
311282
},
312283
{
313284
"cell_type": "code",
285+
"execution_count": null,
314286
"metadata": {
315287
"id": "3nVCPeDnskD8"
316288
},
289+
"outputs": [],
317290
"source": [
318291
"make_cell_large() # Larger output window.\n",
319292
"\n",
320293
"for file in all_outputs:\n",
321-
" print_html('<hr/>')\n",
294+
" print_html('\u003chr/\u003e')\n",
322295
" print(f'Showing {file.full_path} | {file.num_bytes//1000}kB | {file.bpp:.4f}bpp')\n",
323296
" display(Image.open(file.full_path))\n",
324-
" print_html('<hr/>')"
325-
],
326-
"execution_count": null,
327-
"outputs": []
297+
" print_html('\u003chr/\u003e')"
298+
]
328299
},
329300
{
330301
"cell_type": "markdown",
@@ -345,9 +316,11 @@
345316
},
346317
{
347318
"cell_type": "code",
319+
"execution_count": null,
348320
"metadata": {
349321
"id": "9BKccvcTpj1k"
350322
},
323+
"outputs": [],
351324
"source": [
352325
"ZIP = '/content/images.zip'\n",
353326
"\n",
@@ -357,9 +330,31 @@
357330
" zf.write(f.full_path, os.path.basename(path_with_bpp))\n",
358331
"\n",
359332
"files.download(ZIP) "
333+
]
334+
}
335+
],
336+
"metadata": {
337+
"accelerator": "GPU",
338+
"colab": {
339+
"collapsed_sections": [],
340+
"last_runtime": {
341+
"build_target": "//research/colab/notebook:notebook_backend_py3",
342+
"kind": "private"
343+
},
344+
"name": "HiFiC_Interactive_Colab.ipynb",
345+
"provenance": [
346+
{
347+
348+
"timestamp": 1615973093906
349+
}
360350
],
361-
"execution_count": null,
362-
"outputs": []
351+
"toc_visible": true
352+
},
353+
"kernelspec": {
354+
"display_name": "Python 3",
355+
"name": "python3"
363356
}
364-
]
357+
},
358+
"nbformat": 4,
359+
"nbformat_minor": 0
365360
}

0 commit comments

Comments
 (0)