mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-06 12:20:52 +01:00
Summary: These are all essentially no-op changes which allow for nose-style (or pytest-style) test discovery. With this patch, you can use any of these methods to discover and run tests under `caffe2/python`: ``` python -m unittest discover -p '*test*.py' caffe2/python/ python -m nose caffe2/python/ python -m pytest caffe2/python/ ``` Future work: * Get all of the tests to pass * Some seem to be testing operations which don't have GPU implementations * I get a segfault unless I set `CUDA_VISIBLE_DEVICES=0` * Some tests are flaky * Allow test discovery throughout the whole project (e.g. the `experiments/` dir) Closes https://github.com/caffe2/caffe2/pull/199 Reviewed By: pietern Differential Revision: D4704504 Pulled By: Yangqing fbshipit-source-id: 8f5687ec9c8aa873dfaff30dbf44272bc38a206b
76 lines
2.7 KiB
Python
76 lines
2.7 KiB
Python
# This a large test that goes through the translation of the bvlc caffenet
|
|
# model, runs an example through the whole model, and verifies numerically
|
|
# that all the results look right. In default, it is disabled unless you
|
|
# explicitly want to run it.
|
|
|
|
from caffe2.proto import caffe2_pb2
|
|
from caffe.proto import caffe_pb2
|
|
from google.protobuf import text_format
|
|
import numpy as np
|
|
import os
|
|
from caffe2.python import caffe_translator, utils, workspace, test_util
|
|
import sys
|
|
import unittest
|
|
|
|
|
|
@unittest.skipIf(not os.path.exists('data/testdata/caffe_translator'),
|
|
'No testdata existing for the caffe translator test. Exiting.')
|
|
def setUpModule():
|
|
# We will do all the computation stuff in the global space.
|
|
caffenet = caffe_pb2.NetParameter()
|
|
caffenet_pretrained = caffe_pb2.NetParameter()
|
|
text_format.Merge(
|
|
open('data/testdata/caffe_translator/deploy.prototxt').read(), caffenet
|
|
)
|
|
caffenet_pretrained.ParseFromString(
|
|
open(
|
|
'data/testdata/caffe_translator/bvlc_reference_caffenet.caffemodel')
|
|
.read()
|
|
)
|
|
net, pretrained_params = caffe_translator.TranslateModel(
|
|
caffenet, caffenet_pretrained, is_test=True
|
|
)
|
|
with open('data/testdata/caffe_translator/'
|
|
'bvlc_reference_caffenet.translatedmodel',
|
|
'w') as fid:
|
|
fid.write(str(net))
|
|
for param in pretrained_params.protos:
|
|
workspace.FeedBlob(param.name, utils.Caffe2TensorToNumpyArray(param))
|
|
# Let's also feed in the data from the Caffe test code.
|
|
data = np.load('data/testdata/caffe_translator/data_dump.npy').astype(
|
|
np.float32)
|
|
workspace.FeedBlob('data', data)
|
|
# Actually running the test.
|
|
workspace.RunNetOnce(net.SerializeToString())
|
|
|
|
|
|
class TestNumericalEquivalence(test_util.TestCase):
|
|
def testBlobs(self):
|
|
names = [
|
|
"conv1", "pool1", "norm1", "conv2", "pool2", "norm2", "conv3",
|
|
"conv4", "conv5", "pool5", "fc6", "fc7", "fc8", "prob"
|
|
]
|
|
for name in names:
|
|
print('Verifying {}'.format(name))
|
|
caffe2_result = workspace.FetchBlob(name)
|
|
reference = np.load(
|
|
'data/testdata/caffe_translator/' + name + '_dump.npy'
|
|
)
|
|
self.assertEqual(caffe2_result.shape, reference.shape)
|
|
scale = np.max(caffe2_result)
|
|
np.testing.assert_almost_equal(
|
|
caffe2_result / scale,
|
|
reference / scale,
|
|
decimal=5
|
|
)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
if len(sys.argv) == 1:
|
|
print(
|
|
'If you do not explicitly ask to run this test, I will not run it. '
|
|
'Pass in any argument to have the test run for you.'
|
|
)
|
|
sys.exit(0)
|
|
unittest.main()
|