Skip to content

Commit

Permalink
Merge pull request #62 from lscheinkman/DEVOPS-398
Browse files Browse the repository at this point in the history
DEVOPS-398: Fix tensorflow tests
  • Loading branch information
lscheinkman committed Sep 12, 2019
2 parents df59496 + 201bba9 commit 109a8bc
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 63 deletions.
7 changes: 5 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,12 @@ jobs:
- restore_cache:
key: pip-cache-v1-{{ arch }}-{{ checksum "requirements.txt" }}-{{ checksum "requirements-dev.txt" }}
- run: pip install --user flake8-formatter-junit-xml
- run: pip install --user -r requirements-dev.txt
- run: pip install --user ray
- run: python setup.py install --user
- run: pip install --user git+https://github.com/numenta/nupic.tensorflow.git#egg=nupic.tensorflow
- run: pip install --user git+https://github.com/numenta/nupic.torch.git#egg=nupic.torch
- run: pip install --user -r requirements.txt
- run: pip install --user -r requirements-dev.txt
- run: python setup.py develop --user
- save_cache:
key: pip-cache-v1-{{ arch }}-{{ checksum "requirements.txt" }}-{{ checksum "requirements-dev.txt" }}
paths:
Expand Down
5 changes: 5 additions & 0 deletions nupic/research/frameworks/tensorflow/utils/pytorch_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@
"""

import numpy as np
import tensorflow as tf
import tensorflow.keras.backend as K

TF_LOGGER = tf.get_logger()


def _reflatten_linear_weight(x):
"""
Expand Down Expand Up @@ -129,5 +132,7 @@ def load_pytorch_weights(model_tf, model_pt, weights_map=None):
if transform is not None:
value = transform(value)
batch_values.append((var, value))
else:
TF_LOGGER.warn("Unknown variable: %s", var.name)

K.batch_set_value(batch_values)
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
# http://numenta.org/licenses/
#
# ------------------------------------------------------------------------------
nupic.torch
nupic.tensorflow
numpy
matplotlib
pandas
Expand All @@ -27,9 +29,7 @@ librosa
scikit-image
requests
ray
nupic.tensorflow
tqdm
torchvision
nupic.torch
python-dotenv
elasticsearch
6 changes: 4 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ license_files = LICENSE
[bdist_wheel]
universal=0

#[aliases]
#test=pytest
[aliases]
test=pytest

[tool:pytest]
testpaths=tests
filterwarnings =
ignore:.*:DeprecationWarning

[flake8]
; Select all
Expand Down
7 changes: 7 additions & 0 deletions tests/unit/frameworks/tensorflow/k_winner_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import numpy as np
import tensorflow as tf
from tensorflow.python.framework import test_util
from tensorflow.python.keras.layers import Input
from tensorflow.python.keras.models import Model

Expand Down Expand Up @@ -54,6 +55,7 @@ def setUp(self):
)
self.duty_cycle = tf.constant(1.0 / 3.0, shape=(2, 7))

@test_util.run_all_in_graph_and_eager_modes
def test_inference(self):
"""boost factor 0, k=3, batch size 2"""
expected = np.zeros(self.x.shape)
Expand Down Expand Up @@ -107,6 +109,7 @@ def setUp(self):
x[1, 2, 1, 1] = 1.7
self.x2 = tf.constant(x)

@test_util.run_all_in_graph_and_eager_modes
def test_one(self):
"""Equal duty cycle, boost factor 0, k=4, batch size 1."""
x = self.x
Expand All @@ -126,6 +129,7 @@ def test_one(self):
y = k_winners(x, training=True)
self.assertAllClose(y, expected)

@test_util.run_all_in_graph_and_eager_modes
def test_two(self):
"""Equal duty cycle, boost factor 0, k=3."""
x = self.x
Expand All @@ -144,6 +148,7 @@ def test_two(self):
y = k_winners(x, training=True)
self.assertAllClose(y, expected)

@test_util.run_all_in_graph_and_eager_modes
def test_three(self):
"""Equal duty cycle, boost factor=0, k=4, batch size=2."""
x = self.x2
Expand All @@ -167,6 +172,7 @@ def test_three(self):
y = k_winners(x, training=True)
self.assertAllClose(y, expected)

@test_util.run_all_in_graph_and_eager_modes
def test_four(self):
"""Equal duty cycle, boost factor=0, k=3, batch size=2."""
x = self.x2
Expand All @@ -188,6 +194,7 @@ def test_four(self):
y = k_winners(x, training=True)
self.assertAllClose(y, expected)

@test_util.run_all_in_graph_and_eager_modes
def test_five(self):
x = self.x2
expected = np.zeros_like(x)
Expand Down
100 changes: 43 additions & 57 deletions tests/unit/frameworks/tensorflow/load_pytorch_weights_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,82 +21,68 @@
import numpy as np
import tensorflow as tf
import torch
from tensorflow.python.framework import test_util
from tensorflow.python.keras import keras_parameterized
from tensorflow.python.platform import test

from nupic.research.frameworks.tensorflow.utils import load_pytorch_weights
from nupic.tensorflow.models import GSCSparseCNN, GSCSuperSparseCNN

SEED = 18

# Tensorflow configuration.
# Make sure to use one thread in order to keep the results deterministic
CONFIG = tf.ConfigProto(
intra_op_parallelism_threads=1,
inter_op_parallelism_threads=1,
device_count={
"CPU": 1},
)
@keras_parameterized.run_all_keras_modes
class LoadPytorchWeightsTest(keras_parameterized.TestCase):

def setUp(self):
super(LoadPytorchWeightsTest, self).setUp()
self.data = np.random.rand(10, 1, 32, 32)

class LoadPytorchWeightsTest(tf.test.TestCase):

@classmethod
def setUpClass(cls) -> None:
super().setUpClass()
cls.data = np.random.rand(10, 1, 32, 32)

@test_util.run_all_in_graph_and_eager_modes
def test_gsc_sparse_cnn(self):
"""
Test loading gsc_sparse_cnn from pytorch
"""
with self.session(config=CONFIG):
model_pt = torch.hub.load(github="numenta/nupic.torch",
model="gsc_sparse_cnn",
pretrained=True)
model_pt.eval()

model_tf = GSCSparseCNN(pre_trained=False)
load_pytorch_weights(model_tf, model_pt)
model_tf.compile(optimizer="sgd",
loss="sparse_categorical_crossentropy",
metrics=["accuracy"])
data_pt = torch.tensor(self.data, dtype=torch.float32)
data_tf = tf.convert_to_tensor(self.data.transpose(0, 2, 3, 1),
dtype=tf.float32)

out_pt = model_pt(data_pt)
out_tf = model_tf(data_tf, training=False)
out_tf = tf.log(out_tf)

self.assertAllClose(out_pt.detach().numpy(), out_tf)
model_pt = torch.hub.load(github="numenta/nupic.torch",
model="gsc_sparse_cnn",
pretrained=True)
model_pt.eval()

model_tf = GSCSparseCNN(pre_trained=False)
load_pytorch_weights(model_tf, model_pt)
model_tf.compile(optimizer="sgd",
loss="sparse_categorical_crossentropy",
metrics=["accuracy"])
data_pt = torch.tensor(self.data, dtype=torch.float32)
data_tf = tf.convert_to_tensor(self.data.transpose(0, 2, 3, 1),
dtype=tf.float32)

out_pt = model_pt(data_pt)
out_tf = model_tf(data_tf, training=False)
out_tf = tf.log(out_tf)

self.assertAllClose(out_pt.detach().numpy(), out_tf)

@test_util.run_all_in_graph_and_eager_modes
def test_gsc_super_sparse_cnn(self):
"""
Test loading gsc_sparse_cnn from pytorch
"""
with self.session(config=CONFIG):
model_pt = torch.hub.load(github="numenta/nupic.torch",
model="gsc_super_sparse_cnn",
pretrained=True)
model_pt.eval()
model_pt = torch.hub.load(github="numenta/nupic.torch",
model="gsc_super_sparse_cnn",
pretrained=True)
model_pt.eval()

model_tf = GSCSuperSparseCNN(pre_trained=False)
load_pytorch_weights(model_tf, model_pt)
model_tf.compile(optimizer="sgd",
loss="sparse_categorical_crossentropy",
metrics=["accuracy"])
data_pt = torch.tensor(self.data, dtype=torch.float32)
data_tf = tf.convert_to_tensor(self.data.transpose(0, 2, 3, 1),
dtype=tf.float32)
model_tf = GSCSuperSparseCNN(pre_trained=False)
load_pytorch_weights(model_tf, model_pt)
model_tf.compile(optimizer="sgd",
loss="sparse_categorical_crossentropy",
metrics=["accuracy"])
data_pt = torch.tensor(self.data, dtype=torch.float32)
data_tf = tf.convert_to_tensor(self.data.transpose(0, 2, 3, 1),
dtype=tf.float32)

out_pt = model_pt(data_pt)
out_tf = model_tf(data_tf, training=False)
out_tf = tf.log(out_tf)
out_pt = model_pt(data_pt)
out_tf = model_tf(data_tf, training=False)
out_tf = tf.log(out_tf)

self.assertAllClose(out_pt.detach().numpy(), out_tf)
self.assertAllClose(out_pt.detach().numpy(), out_tf)


if __name__ == "__main__":
tf.test.main()
test.main()

0 comments on commit 109a8bc

Please sign in to comment.