Skip to content

Commit e9c0139

Browse files
Johannes Ballécopybara-github
authored andcommitted
Changes relative imports in models/ to package imports.
Relative imports don't work out of Colab notebooks because notebooks are interactive and aren't run as a module. To work around this, we now prefix model imports with the model name. This requires the model files to live in a directory either in the Python path or in the current directory. Note that there is a small chance that a model name clashes with a global module name, but this risk seems unavoidable under the circumstances. PiperOrigin-RevId: 350441806 Change-Id: I4a13fa8337b11b132142f23f89a7824db14ec88f
1 parent 8a4a7b6 commit e9c0139

File tree

9 files changed

+42
-22
lines changed

9 files changed

+42
-22
lines changed

models/hific/archs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
import tensorflow_compression as tfc
2929

30-
from .helpers import ModelMode
30+
from hific.helpers import ModelMode
3131

3232
SCALES_MIN = 0.11
3333
SCALES_MAX = 256

models/hific/configs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
# ==============================================================================
1515
"""Configurations for HiFiC."""
1616

17-
from . import helpers
17+
from hific import helpers
1818

1919

2020
_CONFIGS = {

models/hific/evaluate.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929
import tensorflow.compat.v1 as tf
3030
import tensorflow_compression as tfc
3131

32-
from . import configs
33-
from . import helpers
34-
from . import model
32+
from hific import configs
33+
from hific import helpers
34+
from hific import model
3535

3636

3737
# Show custom tf.logging calls.

models/hific/hific_test.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020
import numpy as np
2121
import tensorflow.compat.v1 as tf
2222

23-
from . import configs
24-
from . import helpers
25-
from . import model
26-
from . import train
23+
from hific import configs
24+
from hific import helpers
25+
from hific import model
26+
from hific import train
2727

2828

2929
class FakeHiFiC(model.HiFiC):

models/hific/model.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
import tensorflow.compat.v1 as tf
2222
import tensorflow_datasets as tfds
2323

24-
from . import archs
25-
from . import helpers
26-
from .helpers import ModelMode
27-
from .helpers import ModelType
28-
from .helpers import TFDSArguments
24+
from hific import archs
25+
from hific import helpers
26+
from hific.helpers import ModelMode
27+
from hific.helpers import ModelType
28+
from hific.helpers import TFDSArguments
2929

3030

3131
# How many dataset preprocessing processes to use.

models/hific/train.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919

2020
import tensorflow.compat.v1 as tf
2121

22-
from . import configs
23-
from . import helpers
24-
from . import model
22+
from hific import configs
23+
from hific import helpers
24+
from hific import model
2525

2626
# Show custom tf.logging calls.
2727
tf.logging.set_verbosity(tf.logging.INFO)

models/toy_sources/ntc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import tensorflow.compat.v2 as tf
66
import tensorflow_probability as tfp
77
import tensorflow_compression as tfc
8-
from . import compression_model
8+
from toy_sources import compression_model
99

1010
tfpd = tfp.distributions
1111

models/toy_sources/toy_sources.ipynb

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,26 @@
2525
"This code requires tensorflow-compression v2.x.\n"
2626
]
2727
},
28+
{
29+
"cell_type": "code",
30+
"metadata": {
31+
"cellView": "form",
32+
"id": "wdA5NUZ-fSxG"
33+
},
34+
"source": [
35+
"#@title Dependencies for Colab\n",
36+
"\n",
37+
"# Run this cell to install the necessary dependencies when running the notebook\n",
38+
"# directly in a Colaboratory hosted runtime from Github.\n",
39+
"\n",
40+
"%tensorflow_version 2.x\n",
41+
"!pip install tensorflow-compression>=2.0\n",
42+
"![[ -e tfc ]] || git clone https://github.com/tensorflow/compression tfc\n",
43+
"%cd tfc/models\n"
44+
],
45+
"execution_count": null,
46+
"outputs": []
47+
},
2848
{
2949
"cell_type": "code",
3050
"execution_count": null,
@@ -88,7 +108,7 @@
88108
"source": [
89109
"#@title Source definitions\n",
90110
"\n",
91-
"from . import sawbridge\n",
111+
"from toy_sources import sawbridge\n",
92112
"\n",
93113
"\n",
94114
"def _rotation_2d(degrees):\n",
@@ -138,8 +158,8 @@
138158
"\n",
139159
"import pywt\n",
140160
"import scipy\n",
141-
"from . import ntc\n",
142-
"from . import vecvq\n",
161+
"from toy_sources import ntc\n",
162+
"from toy_sources import vecvq\n",
143163
"\n",
144164
"\n",
145165
"def _get_activation(activation, dtype):\n",

models/toy_sources/vecvq.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Variational entropy-constrained vector quantization."""
22

33
import tensorflow.compat.v2 as tf
4-
from . import compression_model
4+
from toy_sources import compression_model
55

66

77
class VECVQModel(compression_model.CompressionModel):

0 commit comments

Comments
 (0)