Python ERROR: TypeError: a bytes-like object is required, not 'str' (tempfile)

在跑caffe demo的时候遇到的问题,具体报错如下:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-49-fe99b8f3cc41> in <module>
      1 dummy_data = L.DummyData(shape=dict(dim=[1, 3, 227, 227]))
----> 2 caffenet(data=dummy_data, train=False)
      3 # print(str(caffenet(data=dummy_data, train=False)))
      4 # imagenet_net_filename = caffenet(data=dummy_data, train=False)
      5 # print(imagenet_net_filename)

<ipython-input-48-7c8a9a79285d> in caffenet(data, label, train, num_classes, classifier_name, learn_all)
     38     # write the net to a temporary file and return its filename
     39     with tempfile.NamedTemporaryFile(delete=False) as f:
---> 40         f.write(str(n.to_proto()))
     41         return f.name

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

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

报错位置是:

---> 40         f.write(str(n.to_proto()))

但实际上是需要修改上一行,

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

问题解决!

发布了349 篇原创文章 · 获赞 237 · 访问量 65万+

猜你喜欢

转载自blog.csdn.net/Felaim/article/details/104683192
今日推荐