如何创建一个以未标记的“虚拟数据”作为输入的网络?

时间:2018-05-31 08:04:56

标签: caffe pycaffe

我目前正在通过caffe/examples/了解有关caffe/pycaffe的更多信息。

02-fine-tuning.ipynb-notebook中有一个代码单元,显示如何创建一个以未标记"dummmy data"为输入的caffenet,允许我们在外部设置其输入图像。笔记本可以在这里找到:

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

  

有一个给定的代码单元,它会抛出一个错误:

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)
  

错误:

---------------------------------------------------------------------------
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'

任何人都知道如何做到这一点?

1 个答案:

答案 0 :(得分:1)

tempfile.NamedTemporaryFile()默认以二进制模式('w + b')打开文件。由于您使用的是Python3.x,因此字符串与Python 2.x的类型不同,因此提供字符串作为f.write()的输入会导致错误,因为它需要字节。覆盖二进制模式应避免此错误。

替换

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

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

这已在前一篇文章中解释过:

TypeError: 'str' does not support the buffer interface

相关问题