Caffe2 - Python API
A deep learning, cross platform ML framework
test_utils.py
1 ## @package onnx
2 # Module caffe2.python.onnx.tests.test_utils
3 
4 from __future__ import absolute_import
5 from __future__ import division
6 from __future__ import print_function
7 from __future__ import unicode_literals
8 
9 import os
10 import unittest
11 
12 import numpy as np
13 from caffe2.python.models.download import downloadFromURLToFile, getURLFromName, deleteDirectory
14 
15 
16 class TestCase(unittest.TestCase):
17 
18  def setUp(self):
19  np.random.seed(seed=0)
20 
21  def assertSameOutputs(self, outputs1, outputs2, decimal=7):
22  self.assertEqual(len(outputs1), len(outputs2))
23  for o1, o2 in zip(outputs1, outputs2):
24  self.assertEqual(o1.dtype, o2.dtype)
25  np.testing.assert_almost_equal(o1, o2, decimal=decimal)
26 
27  def add_test_case(self, name, test_func):
28  if not name.startswith('test_'):
29  raise ValueError('Test name must start with test_: {}'.format(name))
30  if hasattr(self, name):
31  raise ValueError('Duplicated test name: {}'.format(name))
32  setattr(self, name, test_func)
33 
34 
36 
37  def _download(self, model):
38  model_dir = self._model_dir(model)
39  assert not os.path.exists(model_dir)
40  os.makedirs(model_dir)
41  for f in ['predict_net.pb', 'init_net.pb', 'value_info.json']:
42  url = getURLFromName(model, f)
43  dest = os.path.join(model_dir, f)
44  try:
45  try:
46  downloadFromURLToFile(url, dest,
47  show_progress=False)
48  except TypeError:
49  # show_progress not supported prior to
50  # Caffe2 78c014e752a374d905ecfb465d44fa16e02a28f1
51  # (Sep 17, 2017)
52  downloadFromURLToFile(url, dest)
53  except Exception as e:
54  print("Abort: {reason}".format(reason=e))
55  print("Cleaning up...")
56  deleteDirectory(model_dir)
57  raise AssertionError("Test model downloading failed")