|
| 1 | +import unittest |
| 2 | +import tempfile |
| 3 | +import os |
| 4 | +import numpy as np |
| 5 | + |
| 6 | +import caffe |
| 7 | + |
| 8 | +def simple_net_file(num_output): |
| 9 | + """Make a simple net prototxt, based on test_net.cpp, returning the name |
| 10 | + of the (temporary) file.""" |
| 11 | + |
| 12 | + f = tempfile.NamedTemporaryFile(delete=False) |
| 13 | + f.write("""name: 'testnet' force_backward: true |
| 14 | + layers { type: DUMMY_DATA name: 'data' top: 'data' top: 'label' |
| 15 | + dummy_data_param { num: 5 channels: 2 height: 3 width: 4 |
| 16 | + num: 5 channels: 1 height: 1 width: 1 |
| 17 | + data_filler { type: 'gaussian' std: 1 } |
| 18 | + data_filler { type: 'constant' } } } |
| 19 | + layers { type: CONVOLUTION name: 'conv' bottom: 'data' top: 'conv' |
| 20 | + convolution_param { num_output: 11 kernel_size: 2 pad: 3 |
| 21 | + weight_filler { type: 'gaussian' std: 1 } |
| 22 | + bias_filler { type: 'constant' value: 2 } } |
| 23 | + weight_decay: 1 weight_decay: 0 } |
| 24 | + layers { type: INNER_PRODUCT name: 'ip' bottom: 'conv' top: 'ip' |
| 25 | + inner_product_param { num_output: """ + str(num_output) + """ |
| 26 | + weight_filler { type: 'gaussian' std: 2.5 } |
| 27 | + bias_filler { type: 'constant' value: -3 } } } |
| 28 | + layers { type: SOFTMAX_LOSS name: 'loss' bottom: 'ip' bottom: 'label' |
| 29 | + top: 'loss' }""") |
| 30 | + f.close() |
| 31 | + return f.name |
| 32 | + |
| 33 | +class TestNet(unittest.TestCase): |
| 34 | + def setUp(self): |
| 35 | + self.num_output = 13 |
| 36 | + net_file = simple_net_file(self.num_output) |
| 37 | + self.net = caffe.Net(net_file) |
| 38 | + # fill in valid labels |
| 39 | + self.net.blobs['label'].data[...] = \ |
| 40 | + np.random.randint(self.num_output, |
| 41 | + size=self.net.blobs['label'].data.shape) |
| 42 | + os.remove(net_file) |
| 43 | + |
| 44 | + def test_memory(self): |
| 45 | + """Check that holding onto blob data beyond the life of a Net is OK""" |
| 46 | + |
| 47 | + params = sum(map(list, self.net.params.itervalues()), []) |
| 48 | + blobs = self.net.blobs.values() |
| 49 | + del self.net |
| 50 | + |
| 51 | + # now sum everything (forcing all memory to be read) |
| 52 | + total = 0 |
| 53 | + for p in params: |
| 54 | + total += p.data.sum() + p.diff.sum() |
| 55 | + for bl in blobs: |
| 56 | + total += bl.data.sum() + bl.diff.sum() |
| 57 | + |
| 58 | + def test_forward_backward(self): |
| 59 | + self.net.forward() |
| 60 | + self.net.backward() |
| 61 | + |
| 62 | + def test_inputs_outputs(self): |
| 63 | + self.assertEqual(self.net.inputs, []) |
| 64 | + self.assertEqual(self.net.outputs, ['loss']) |
| 65 | + |
| 66 | + def test_save_and_read(self): |
| 67 | + f = tempfile.NamedTemporaryFile(delete=False) |
| 68 | + f.close() |
| 69 | + self.net.save(f.name) |
| 70 | + net_file = simple_net_file(self.num_output) |
| 71 | + net2 = caffe.Net(net_file, f.name) |
| 72 | + os.remove(net_file) |
| 73 | + os.remove(f.name) |
| 74 | + for name in self.net.params: |
| 75 | + for i in range(len(self.net.params[name])): |
| 76 | + self.assertEqual(abs(self.net.params[name][i].data |
| 77 | + - net2.params[name][i].data).sum(), 0) |
0 commit comments