Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update code to be compatible with tensorflow 2.3.0 #12

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
keras>=2.0.0
tensorflow>=1.4.0
tensorflow>=2.3.0
pytest
49 changes: 26 additions & 23 deletions tests/wrapper_test.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import sys
import numpy as np
import pytest

from keras.models import Model
from keras.layers import Input
from keras.layers import Lambda
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input
from tensorflow.keras.layers import Lambda

sys.path.append('..')
from tta_wrapper import tta_classification
from tta_wrapper import tta_segmentation

Expand All @@ -16,7 +16,27 @@ def identity_model(input_shape):
identity_model = Model(inp, x)
return identity_model


# inputs
input_sample = np.arange(9).reshape((1, 3, 3, 1))

# outputs
tta_segmentation_output = input_sample
tta_classification_output = np.ones((1, 3, 3, 1)) * 4

# model
seg_identity_model = identity_model(input_sample.shape[1:])
cls_identity_model = identity_model(input_sample.shape[1:])

@pytest.mark.parametrize("wrapper, base_model, inputs, outputs",
[(tta_segmentation,
seg_identity_model,
input_sample,
tta_segmentation_output),
(tta_classification,
cls_identity_model,
input_sample,
tta_classification_output)
])
def test_wrapper(wrapper, base_model, inputs, outputs):
print('[TEST] wrapping model with {} ... '.format(wrapper.__name__))

Expand All @@ -35,21 +55,4 @@ def test_wrapper(wrapper, base_model, inputs, outputs):
prediction = model.predict(inputs)

assert np.allclose(prediction, outputs), f"\nprediction: \n{prediction}\n\nground_truth: \n{outputs}"
print('[TEST] {} - test passed. '.format(wrapper.__name__))


if __name__ == '__main__':

# inputs
input_sample = np.arange(9).reshape((1, 3, 3, 1))

# outputs
tta_segmentation_output = input_sample
tta_classification_output = np.ones((1, 3, 3, 1)) * 4

# model
seg_identity_model = identity_model(input_sample.shape[1:])
cls_identity_model = identity_model(input_sample.shape[1:])

test_wrapper(tta_segmentation, seg_identity_model, input_sample, tta_segmentation_output)
test_wrapper(tta_classification, cls_identity_model, input_sample, tta_classification_output)
print('[TEST] {} - test passed. '.format(wrapper.__name__))
2 changes: 1 addition & 1 deletion tta_wrapper/__version__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
VERSION = (0, 0, 1)
VERSION = (0, 0, 2)

__version__ = '.'.join(map(str, VERSION))
9 changes: 5 additions & 4 deletions tta_wrapper/functional.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import tensorflow as tf
from tensorflow.python.ops import manip_ops


class DualTransform:
Expand Down Expand Up @@ -81,21 +82,21 @@ class HShift(DualTransform):
identity_param = 0

def forward(self, image, param):
return tf.manip.roll(image, param, axis=0)
return manip_ops.roll(image, param, axis=0)

def backward(self, image, param):
return tf.manip.roll(image, -param, axis=0)
return manip_ops.roll(image, -param, axis=0)


class VShift(DualTransform):

identity_param = 0

def forward(self, image, param):
return tf.manip.roll(image, param, axis=1)
return manip_ops.roll(image, param, axis=1)

def backward(self, image, param):
return tf.manip.roll(image, -param, axis=1)
return manip_ops.roll(image, -param, axis=1)


class Contrast(SingleTransform):
Expand Down
2 changes: 1 addition & 1 deletion tta_wrapper/layers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import tensorflow as tf
from keras.layers import Layer
from tensorflow.keras.layers import Layer

from . import functional as F

Expand Down
4 changes: 2 additions & 2 deletions tta_wrapper/wrappers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from keras.models import Model
from keras.layers import Input
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input

from .layers import Repeat, TTA, Merge
from .augmentation import Augmentation
Expand Down