test_net_spec.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import unittest
  2. import tempfile
  3. import caffe
  4. from caffe import layers as L
  5. from caffe import params as P
  6. def lenet(batch_size):
  7. n = caffe.NetSpec()
  8. n.data, n.label = L.DummyData(shape=[dict(dim=[batch_size, 1, 28, 28]),
  9. dict(dim=[batch_size, 1, 1, 1])],
  10. transform_param=dict(scale=1./255), ntop=2)
  11. n.conv1 = L.Convolution(n.data, kernel_size=5, num_output=20,
  12. weight_filler=dict(type='xavier'))
  13. n.pool1 = L.Pooling(n.conv1, kernel_size=2, stride=2, pool=P.Pooling.MAX)
  14. n.conv2 = L.Convolution(n.pool1, kernel_size=5, num_output=50,
  15. weight_filler=dict(type='xavier'))
  16. n.pool2 = L.Pooling(n.conv2, kernel_size=2, stride=2, pool=P.Pooling.MAX)
  17. n.ip1 = L.InnerProduct(n.pool2, num_output=500,
  18. weight_filler=dict(type='xavier'))
  19. n.relu1 = L.ReLU(n.ip1, in_place=True)
  20. n.ip2 = L.InnerProduct(n.relu1, num_output=10,
  21. weight_filler=dict(type='xavier'))
  22. n.loss = L.SoftmaxWithLoss(n.ip2, n.label)
  23. return n.to_proto()
  24. def anon_lenet(batch_size):
  25. data, label = L.DummyData(shape=[dict(dim=[batch_size, 1, 28, 28]),
  26. dict(dim=[batch_size, 1, 1, 1])],
  27. transform_param=dict(scale=1./255), ntop=2)
  28. conv1 = L.Convolution(data, kernel_size=5, num_output=20,
  29. weight_filler=dict(type='xavier'))
  30. pool1 = L.Pooling(conv1, kernel_size=2, stride=2, pool=P.Pooling.MAX)
  31. conv2 = L.Convolution(pool1, kernel_size=5, num_output=50,
  32. weight_filler=dict(type='xavier'))
  33. pool2 = L.Pooling(conv2, kernel_size=2, stride=2, pool=P.Pooling.MAX)
  34. ip1 = L.InnerProduct(pool2, num_output=500,
  35. weight_filler=dict(type='xavier'))
  36. relu1 = L.ReLU(ip1, in_place=True)
  37. ip2 = L.InnerProduct(relu1, num_output=10,
  38. weight_filler=dict(type='xavier'))
  39. loss = L.SoftmaxWithLoss(ip2, label)
  40. return loss.to_proto()
  41. def silent_net():
  42. n = caffe.NetSpec()
  43. n.data, n.data2 = L.DummyData(shape=dict(dim=3), ntop=2)
  44. n.silence_data = L.Silence(n.data, ntop=0)
  45. n.silence_data2 = L.Silence(n.data2, ntop=0)
  46. return n.to_proto()
  47. class TestNetSpec(unittest.TestCase):
  48. def load_net(self, net_proto):
  49. f = tempfile.NamedTemporaryFile(mode='w+', delete=False)
  50. f.write(str(net_proto))
  51. f.close()
  52. return caffe.Net(f.name, caffe.TEST)
  53. def test_lenet(self):
  54. """Construct and build the Caffe version of LeNet."""
  55. net_proto = lenet(50)
  56. # check that relu is in-place
  57. self.assertEqual(net_proto.layer[6].bottom,
  58. net_proto.layer[6].top)
  59. net = self.load_net(net_proto)
  60. # check that all layers are present
  61. self.assertEqual(len(net.layers), 9)
  62. # now the check the version with automatically-generated layer names
  63. net_proto = anon_lenet(50)
  64. self.assertEqual(net_proto.layer[6].bottom,
  65. net_proto.layer[6].top)
  66. net = self.load_net(net_proto)
  67. self.assertEqual(len(net.layers), 9)
  68. def test_zero_tops(self):
  69. """Test net construction for top-less layers."""
  70. net_proto = silent_net()
  71. net = self.load_net(net_proto)
  72. self.assertEqual(len(net.forward()), 0)