类型错误:write() 参数在保存 .npy 文件时必须是 str,而不是字节 [英] TypeError: write() argument must be str, not bytes while saving .npy file

查看:20
本文介绍了类型错误:write() 参数在保存 .npy 文件时必须是 str,而不是字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在 keras 博客文章.

代码写入 .npy 文件如下:

The code writes to a .npy file as follows:

bottleneck_features_train = model.predict_generator(generator, nb_train_samples // batch_size)
np.save(open('bottleneck_features_train.npy', 'w'),bottleneck_features_train)

然后从这个文件中读取:

It then reads from this file:

def train_top_model():
    train_data = np.load(open('bottleneck_features_train.npy'))

现在我收到一条错误消息:

Now I get an error saying:

Found 2000 images belonging to 2 classes.
Traceback (most recent call last):
  File "kerasbottleneck.py", line 103, in <module>
    save_bottlebeck_features()
  File "kerasbottleneck.py", line 69, in save_bottlebeck_features
    np.save(open('bottleneck_features_train.npy', 'w'),bottleneck_features_train)
  File "/opt/anaconda3/lib/python3.6/site-packages/numpy/lib/npyio.py", line 511, in save
    pickle_kwargs=pickle_kwargs)
  File "/opt/anaconda3/lib/python3.6/site-packages/numpy/lib/format.py", line 565, in write_array
version)
  File "/opt/anaconda3/lib/python3.6/site-packages/numpy/lib/format.py", line 335, in _write_array_header
fp.write(header_prefix)
TypeError: write() argument must be str, not bytes

此后,我尝试将文件模式从w"更改为wb".这导致读取文件时出错:

After this, I tried changing the file mode from 'w' to 'wb'. This resulted in an error while reading the file:

Found 2000 images belonging to 2 classes.
Found 800 images belonging to 2 classes.
Traceback (most recent call last):
  File "kerasbottleneck.py", line 104, in <module>
    train_top_model()
  File "kerasbottleneck.py", line 82, in train_top_model
    train_data = np.load(open('bottleneck_features_train.npy'))
  File "/opt/anaconda3/lib/python3.6/site-packages/numpy/lib/npyio.py", line 404, in load
magic = fid.read(N)
  File "/opt/anaconda3/lib/python3.6/codecs.py", line 321, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x93 in position 0: invalid start byte

我该如何解决这个错误?

How can I fix this error?

推荐答案

博文中的代码针对 Python 2,在 Python 2 中写入和读取文件使用字节串.在 Python 3 中,您需要以二进制模式打开文件,既要写入又要读取:

The code in the blog post is aimed at Python 2, where writing to and reading from a file works with bytestrings. In Python 3, you need to open the file in binary mode, both for writing and then reading again:

np.save(
    open('bottleneck_features_train.npy', 'wb'),
    bottleneck_features_train)

阅读时:

train_data = np.load(open('bottleneck_features_train.npy', 'rb'))

注意模式参数中的 b 字符.

Note the b character in the mode arguments there.

我会将该文件用作上下文管理器以确保它完全关闭:

I'd use the file as a context manager to ensure it is cleanly closed:

with open('bottleneck_features_train.npy', 'wb') as features_train_file
    np.save(features_train_file, bottleneck_features_train)

with open('bottleneck_features_train.npy', 'wb') as features_train_file:
    train_data = np.load(features_train_file)

无论如何,博客文章中的代码应该使用这两个更改,因为在 Python 2 中,如果模式文本文件中没有 b 标志,则具有特定于平台的换行符转换约定,并且在 Windows 上,流中的某些字符将具有特定含义(包括导致文件看起来比 EOF 字符出现时的实际短).二进制数据可能是一个真正的问题.

The code in the blog post should use both of these changes anyway, because in Python 2, without the b flag in the mode text files have platform-specific newline conventions translated, and on Windows certain characters in the stream will have specific meaning (including causing the file to appear shorter than it really is if a EOF characte appears). With binary data that could be a real problem.

这篇关于类型错误:write() 参数在保存 .npy 文件时必须是 str,而不是字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆