0
votes

I currently work myself through the caffe/examples/ to learn more about caffe/pycaffe.

In the 02-fine-tuning.ipynb-notebook there is a codecell which shows how to create a caffenet which takes unlabeled "dummmy data" as input, allowing us to set its input images externally. The notebook can be found here:

https://github.com/BVLC/caffe/blob/master/examples/02-fine-tuning.ipynb

There is a given code-cell, which throws an error:

dummy_data = L.DummyData(shape=dict(dim=[1, 3, 227, 227]))
imagenet_net_filename = caffenet(data=dummy_data, train=False)
imagenet_net = caffe.Net(imagenet_net_filename, weights, caffe.TEST)

error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-9f0ecb4d95e6> in <module>()
      1 dummy_data = L.DummyData(shape=dict(dim=[1, 3, 227, 227]))
----> 2 imagenet_net_filename = caffenet(data=dummy_data, train=False)
      3 imagenet_net = caffe.Net(imagenet_net_filename, weights, caffe.TEST)

<ipython-input-5-53badbea969e> in caffenet(data, label, train, num_classes, classifier_name, learn_all)
     68     # write the net to a temporary file and return its filename
     69     with tempfile.NamedTemporaryFile(delete=False) as f:
---> 70         f.write(str(n.to_proto()))
     71         return f.name

~/anaconda3/envs/testcaffegpu/lib/python3.6/tempfile.py in func_wrapper(*args, **kwargs)
    481             @_functools.wraps(func)
    482             def func_wrapper(*args, **kwargs):
--> 483                 return func(*args, **kwargs)
    484             # Avoid closing the file as long as the wrapper is alive,
    485             # see issue #18879.

TypeError: a bytes-like object is required, not 'str'

Anyone knows how to do this right?

1

1 Answers

1
votes

tempfile.NamedTemporaryFile() opens a file in binary mode ('w+b') by default. Since you are using Python3.x, string is not the same type as for Python 2.x, hence providing a string as input to f.write() results in error since it expects bytes. Overriding the binary mode should avoid this error.

Replace

with tempfile.NamedTemporaryFile(delete=False) as f:

with

with tempfile.NamedTemporaryFile(delete=False, mode='w') as f:

This has been explained in a previous post:

TypeError: 'str' does not support the buffer interface