Skip to content

Commit

Permalink
Fix tensorflow-keras integration version (#468)
Browse files Browse the repository at this point in the history
* Fix tensorflow-keras integration version

* Review changes

* Fix

* Fixes
  • Loading branch information
HubertJaworski authored Mar 23, 2021
1 parent dec2c25 commit d56aad5
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 1,964 deletions.
4 changes: 2 additions & 2 deletions integrations/tensorflow-keras/MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
include versioneer.py
include neptune/alpha/integrations/tensorflow_keras/impl/_version.py
include neptune_tensorflow_keras/impl/__init__.py
include neptune_tensorflow_keras/__init__.py
include neptune_tensorflow_keras/_version.py
103 changes: 0 additions & 103 deletions integrations/tensorflow-keras/neptune_tensorflow_keras/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,106 +13,3 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#

from typing import Optional

from neptune.new import Run
from neptune.new.exceptions import NeptuneException

from ._version import get_versions
__version__ = get_versions()['version']
del get_versions

# Note: we purposefully try to import `tensorflow.keras.callbacks.Callback`
# before `keras.callbacks.Callback` because the former is compatible with both
# `tensorflow.keras` and `keras`, while the latter is only compatible
# with `keras`. See https://github.com/keras-team/keras/issues/14125
try:
from tensorflow.keras.callbacks import Callback
except ImportError:
try:
from keras.callbacks import Callback
except ImportError:
msg = """
keras package not found.
As Keras is now part of Tensorflow you should install it by running
pip install tensorflow"""
raise ModuleNotFoundError(msg) # pylint:disable=undefined-variable


class NeptuneCallback(Callback):
"""Logs Keras metrics to Neptune.
Goes over the `last_metrics` and `smooth_loss` after each batch and epoch
and logs them to Neptune.
See the example experiment here https://ui.neptune.ai/shared/keras-integration/e/KERAS-23/logs
Args:
experiment: `neptune.new.Experiment`:
Neptune experiment, required.
base_namespace: str, optional:
Namespace, in which all series will be put.
Example:
Initialize Neptune client:
.. code:: python
import neptune.new as neptune
experiment = neptune.init(api_token='ANONYMOUS',
project='shared/keras-integration')
Instantiate the callback and pass
it to callbacks argument of `model.fit()`:
.. code:: python
from neptune.new.integrations.tensorflow_keras import NeptuneCallback
model.fit(x_train, y_train,
epochs=PARAMS['epoch_nr'],
batch_size=PARAMS['batch_size'],
callbacks=[NeptuneMonitor(experiment)])
Note:
You need to have Keras or Tensorflow 2 installed on your computer to use this module.
"""

def __init__(self, experiment: Run, base_namespace: Optional[str] = None):
super().__init__()
if experiment is None or not isinstance(experiment, Run):
raise ValueError("Neptune experiment is missing")
self._base_namespace = ''
if base_namespace:
if base_namespace.endswith("/"):
self._base_namespace = base_namespace[:-1]
else:
self._base_namespace = base_namespace
if self._base_namespace:
self._metric_logger = experiment[self._base_namespace]
else:
self._metric_logger = experiment

def _log_metrics(self, logs, trigger):
if not logs:
return

logger = self._metric_logger[trigger]

for metric, value in logs.items():
try:
if metric in ('batch', 'size'):
continue
logger[metric].log(value)
except NeptuneException:
pass

def on_batch_end(self, batch, logs=None): # pylint:disable=unused-argument
self._log_metrics(logs, 'batch')

def on_epoch_end(self, epoch, logs=None): # pylint:disable=unused-argument
self._log_metrics(logs, 'epoch')
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#
# Copyright (c) 2021, Neptune Labs Sp. z o.o.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

from neptune.new import Run
from neptune.new.exceptions import NeptuneException
from typing import Optional

# Note: we purposefully try to import `tensorflow.keras.callbacks.Callback`
# before `keras.callbacks.Callback` because the former is compatible with both
# `tensorflow.keras` and `keras`, while the latter is only compatible
# with `keras`. See https://github.com/keras-team/keras/issues/14125
try:
from tensorflow.keras.callbacks import Callback
except ImportError:
try:
from keras.callbacks import Callback
except ImportError:
msg = """
keras package not found.
As Keras is now part of Tensorflow you should install it by running
pip install tensorflow"""
raise ModuleNotFoundError(msg) # pylint:disable=undefined-variable


class NeptuneCallback(Callback):
"""Logs Keras metrics to Neptune.
Goes over the `last_metrics` and `smooth_loss` after each batch and epoch
and logs them to Neptune.
See the example run here https://ui.neptune.ai/shared/keras-integration/e/KERAS-23/logs
Args:
run: `neptune.new.Run`:
Neptune run, required.
base_namespace: str, optional:
Namespace, in which all series will be put.
Example:
Initialize Neptune client:
.. code:: python
import neptune.new as neptune
run = neptune.init(api_token='ANONYMOUS',
project='shared/keras-integration')
Instantiate the callback and pass
it to callbacks argument of `model.fit()`:
.. code:: python
from neptune.new.integrations.tensorflow_keras import NeptuneCallback
model.fit(x_train, y_train,
epochs=PARAMS['epoch_nr'],
batch_size=PARAMS['batch_size'],
callbacks=[NeptuneMonitor(run)])
Note:
You need to have Keras or Tensorflow 2 installed on your computer to use this module.
"""

def __init__(self, run: Run, base_namespace: Optional[str] = None):
super().__init__()

if run is None or not isinstance(run, Run):
raise ValueError("Neptune run is missing")
self._base_namespace = ''
if base_namespace:
if base_namespace.endswith("/"):
self._base_namespace = base_namespace[:-1]
else:
self._base_namespace = base_namespace
if self._base_namespace:
self._metric_logger = run[self._base_namespace]
else:
self._metric_logger = run

def _log_metrics(self, logs, trigger):
if not logs:
return

logger = self._metric_logger[trigger]

for metric, value in logs.items():
try:
if metric in ('batch', 'size'):
continue
logger[metric].log(value)
except NeptuneException:
pass

def on_batch_end(self, batch, logs=None): # pylint:disable=unused-argument
self._log_metrics(logs, 'batch')

def on_epoch_end(self, epoch, logs=None): # pylint:disable=unused-argument
self._log_metrics(logs, 'epoch')
18 changes: 15 additions & 3 deletions integrations/tensorflow-keras/setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import versioneer
import os

from neptune_tensorflow_keras._version import get_versions
from setuptools import setup


Expand All @@ -16,9 +18,19 @@ def main():

base_libs = ['neptune-client>=0.5.1', 'tensorflow']

version = None
if os.path.exists('PKG-INFO'):
with open('PKG-INFO', 'r') as f:
lines = f.readlines()
for line in lines:
if line.startswith('Version:'):
version = line[8:].strip()
else:
version = get_versions()["version"]

setup(
name='neptune-tensorflow-keras',
version=versioneer.get_version(),
version=version,
description='Neptune.ai tensorflow-keras integration library',
author='neptune.ai',
support='[email protected]',
Expand All @@ -29,7 +41,7 @@ def main():
license='MIT License',
install_requires=base_libs,
extras_require=extras,
packages=['neptune_tensorflow_keras'],
packages=['neptune_tensorflow_keras', 'neptune_tensorflow_keras.impl'],
zip_safe=False
)

Expand Down
Loading

0 comments on commit d56aad5

Please sign in to comment.