Skip to content

Commit c6374be

Browse files
committed
do not use tf2.20, adapt to keras 3 api
1 parent 1d92393 commit c6374be

9 files changed

Lines changed: 19 additions & 22 deletions

File tree

.github/workflows/tox.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ jobs:
1414
- ubuntu-latest
1515
- windows-latest
1616
py:
17-
- "3.13"
1817
- "3.12"
1918
- "3.11"
2019
- "3.10"
@@ -23,6 +22,8 @@ jobs:
2322
py: "3.10.11"
2423
- os: macos-latest-xlarge
2524
py: "3.11.8"
25+
- os: macos-latest-xlarge
26+
py: "3.12.12"
2627
steps:
2728
- name: Setup python for test ${{ matrix.py }}
2829
uses: actions/setup-python@v4

basic_pitch/layers/signal.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# See the License for the specific language governing permissions and
1616
# limitations under the License.
1717

18-
from typing import Any, Callable, Optional
18+
from typing import Any, Callable, Optional, Tuple
1919
import tensorflow as tf
2020
from basic_pitch.layers.math import log_base_b
2121

@@ -62,7 +62,7 @@ def __init__(
6262
self.center = center
6363
self.pad_mode = pad_mode
6464

65-
def build(self, input_shape: tf.TensorShape) -> None:
65+
def build(self, input_shape: Tuple[Optional[int], ...]) -> None:
6666
if self.window_length < self.fft_length:
6767
lpad = (self.fft_length - self.window_length) // 2
6868
rpad = self.fft_length - self.window_length - lpad
@@ -77,10 +77,11 @@ def padded_window(window_length: int, dtype: tf.dtypes.DType = tf.float32) -> tf
7777
self.final_window_fn = padded_window
7878

7979
if self.center:
80+
rank = len(input_shape)
8081
self.spec = tf.keras.layers.Lambda(
8182
lambda x: tf.pad(
8283
x,
83-
[[0, 0] for _ in range(input_shape.rank - 1)] + [[self.fft_length // 2, self.fft_length // 2]],
84+
[[0, 0] for _ in range(rank - 1)] + [[self.fft_length // 2, self.fft_length // 2]],
8485
mode=self.pad_mode,
8586
)
8687
)
@@ -159,9 +160,9 @@ class NormalizedLog(tf.keras.layers.Layer):
159160
This layer adds 1e-10 to all values as a way to avoid NaN math.
160161
"""
161162

162-
def build(self, input_shape: tf.Tensor) -> None:
163+
def build(self, input_shape: Tuple[Optional[int], ...]) -> None:
163164
self.squeeze_batch = lambda batch: batch
164-
rank = input_shape.rank
165+
rank = len(input_shape)
165166
if rank == 4:
166167
assert input_shape[1] == 1, "If the rank is 4, the second dimension must be length 1"
167168
self.squeeze_batch = lambda batch: tf.squeeze(batch, axis=1)

basic_pitch/models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ def get_cqt(inputs: tf.Tensor, n_harmonics: int, use_batchnorm: bool) -> tf.Tens
184184
bins_per_octave=12 * CONTOURS_BINS_PER_SEMITONE,
185185
)(x)
186186
x = signal.NormalizedLog()(x)
187-
x = tf.expand_dims(x, -1)
187+
x = tfkl.Lambda(lambda t: tf.expand_dims(t, -1))(x)
188188
if use_batchnorm:
189189
x = tfkl.BatchNormalization()(x)
190190
return x
@@ -263,7 +263,7 @@ def model(
263263
x_contours = nn.FlattenFreqCh(name=contour_name)(x_contours) # contour output
264264

265265
# reduced contour output as input to notes
266-
x_contours_reduced = tf.expand_dims(x_contours, -1)
266+
x_contours_reduced = tfkl.Lambda(lambda t: tf.expand_dims(t, -1))(x_contours)
267267
else:
268268
x_contours_reduced = x_contours
269269

basic_pitch/train.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,6 @@ def main(
150150
model.compile(
151151
loss=loss,
152152
optimizer=tf.keras.optimizers.Adam(learning_rate),
153-
sample_weight_mode={"contour": None, "note": None, "onset": None},
154153
)
155154

156155
logging.info("--- Model Training specs ---")

pyproject.toml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,18 @@ classifiers = [
1515
"Programming Language :: Python :: 3.10",
1616
"Programming Language :: Python :: 3.11",
1717
"Programming Language :: Python :: 3.12",
18-
"Programming Language :: Python :: 3.13",
1918
"Programming Language :: Python :: Implementation :: CPython",
2019
]
2120
dependencies = [
2221
"coremltools; platform_system == 'Darwin'",
2322
"librosa>=0.8.0",
2423
"mir_eval>=0.6.0",
25-
"numpy>=1.18",
2624
"onnxruntime; platform_system == 'Windows' and python_version < '3.11'",
2725
"pretty_midi>=0.2.9",
2826
"resampy>=0.2.2,<0.4.3",
2927
"scikit-learn",
3028
"scipy>=1.4.1",
31-
"tensorflow>=2.16.0;python_version >= '3.11'",
29+
"tensorflow>=2.16.0,<2.20.0;python_version >= '3.11'",
3230
"tflite-runtime; platform_system == 'Linux' and python_version < '3.11'",
3331
"typing_extensions",
3432
]
@@ -68,7 +66,7 @@ test = [
6866
"mido"
6967
]
7068
tf = [
71-
"tensorflow>=2.16.1"
69+
"tensorflow>=2.16.0,<2.20.0"
7270
]
7371
coreml = ["coremltools"]
7472
onnx = ["onnxruntime"]

tests/data/conftest.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,21 @@
1212
SLAKH_TEST_INDEX = json.load(open(RESOURCES_PATH / "data" / "slakh" / "dummy_index.json"))
1313

1414

15-
@pytest.fixture # type: ignore[misc]
15+
@pytest.fixture # type: ignore[untyped-decorator]
1616
def mock_slakh_index() -> None: # type: ignore[misc]
1717
with mock.patch("mirdata.datasets.slakh.Dataset.download"):
1818
with mock.patch("mirdata.datasets.slakh.Dataset._index", new=SLAKH_TEST_INDEX):
1919
yield
2020

2121

22-
@pytest.fixture # type: ignore[misc]
22+
@pytest.fixture # type: ignore[untyped-decorator]
2323
def mock_medleydb_pitch_index() -> None: # type: ignore[misc]
2424
with mock.patch("mirdata.datasets.medleydb_pitch.Dataset.download"):
2525
with mock.patch("mirdata.datasets.medleydb_pitch.Dataset._index", new=MEDLEYDB_PITCH_TEST_INDEX):
2626
yield
2727

2828

29-
@pytest.fixture # type: ignore[misc]
29+
@pytest.fixture # type: ignore[untyped-decorator]
3030
def mock_maestro_index() -> None: # type: ignore[misc]
3131
index_with_metadata = MAESTRO_TEST_INDEX
3232
metadata = {mdata["midi_filename"].split(".")[0]: mdata for mdata in METADATA_TEST_INDEX}
@@ -36,14 +36,14 @@ def mock_maestro_index() -> None: # type: ignore[misc]
3636
yield
3737

3838

39-
@pytest.fixture # type: ignore[misc]
39+
@pytest.fixture # type: ignore[untyped-decorator]
4040
def mock_guitarset_index() -> None: # type: ignore[misc]
4141
with mock.patch("mirdata.datasets.guitarset.Dataset.download"):
4242
with mock.patch("mirdata.datasets.guitarset.Dataset._index", new=GUITAR_SET_TEST_INDEX):
4343
yield
4444

4545

46-
@pytest.fixture # type: ignore[misc]
46+
@pytest.fixture # type: ignore[untyped-decorator]
4747
def mock_ikala_index() -> None: # type: ignore[misc]
4848
with mock.patch("mirdata.datasets.ikala.Dataset.download"):
4949
with mock.patch("mirdata.datasets.ikala.Dataset._index", new=IKALA_TEST_INDEX):

tests/test_callbacks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,6 @@ def test_visualize_callback_on_epoch_end(tmpdir: str) -> None:
5656
contours=True,
5757
)
5858

59-
vc.model = MockModel()
59+
vc.set_model(MockModel())
6060

6161
vc.on_epoch_end(1, {"loss": np.random.random(), "val_loss": np.random.random()})

tests/test_nn.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ def test_defaults(self) -> None:
6767
model.compile(
6868
loss="binary_crossentropy",
6969
optimizer=tf.keras.optimizers.Adam(0.1),
70-
sample_weight_mode=None,
7170
)
7271

7372
model.fit(
@@ -102,7 +101,6 @@ def test_fractions(self) -> None:
102101
model.compile(
103102
loss="binary_crossentropy",
104103
optimizer=tf.keras.optimizers.Adam(0.1),
105-
sample_weight_mode=None,
106104
)
107105

108106
model.fit(

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[tox]
2-
envlist = py313,py312,py311,py310,full,data,manifest,check-formatting,lint,mypy
2+
envlist = py312,py311,py310,full,data,manifest,check-formatting,lint,mypy
33
skipsdist = True
44
usedevelop = True
55
requires =

0 commit comments

Comments
 (0)