OSError:在python中使用print()时,raw write()返回无效长度

san*_*who 8 python image-processing python-3.x tensorflow

我正在使用python tensorflow训练模型来识别python中的图像.但是当我尝试从github执行train.py时,我遇到了以下错误

Traceback (most recent call last):
File "train.py", line 1023, in <module>
tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)
File "C:\Users\sande\Anaconda3\envs\tensorflow\lib\site-
packages\tensorflow\python\platform\app.py", line 48, in run
_sys.exit(main(_sys.argv[:1] + flags_passthrough))
File "train.py", line 766, in main
bottleneck_tensor)
File "train.py", line 393, in cache_bottlenecks
jpeg_data_tensor, bottleneck_tensor)
File "train.py", line 341, in get_or_create_bottleneck
bottleneck_tensor)
File "train.py", line 290, in create_bottleneck_file
print('Creating bottleneck at ' + bottleneck_path)
OSError: raw write() returned invalid length 112 (should have been between 0 
and 56)
Run Code Online (Sandbox Code Playgroud)

下面是create_bottleneck_file()的代码

def create_bottleneck_file(bottleneck_path, image_lists, label_name, index,
                       image_dir, category, sess, jpeg_data_tensor,
                       bottleneck_tensor):
"""Create a single bottleneck file."""
print('Creating bottleneck at ' + bottleneck_path)
image_path = get_image_path(image_lists, label_name, index,
                          image_dir, category)
if not gfile.Exists(image_path):
    tf.logging.fatal('File does not exist %s', image_path)
image_data = gfile.FastGFile(image_path, 'rb').read()
try:
    bottleneck_values = run_bottleneck_on_image(
        sess, image_data, jpeg_data_tensor, bottleneck_tensor)
except:
    raise RuntimeError('Error during processing file %s' % image_path)

bottleneck_string = ','.join(str(x) for x in bottleneck_values)
with open(bottleneck_path, 'w') as bottleneck_file:
    bottleneck_file.write(bottleneck_string)
Run Code Online (Sandbox Code Playgroud)

我尝试减少文件名,以便bottleneck_path是一个小值,但是没有用.我试图在网上搜索这个错误,但没有发现任何有用的东西.如果您对此问题有疑问,请与我们联系

小智 12

如果您无法迁移到3.6或像我这样从Windows迁移,请安装win_unicode_console软件包,导入它并在脚本的开头添加此行以启用它:

win_unicode_console.enable()
Run Code Online (Sandbox Code Playgroud)

这个问题似乎通常是3.6之前的Python所特有的,因为负责处理文本输出的代码被重写为最新版本.这也意味着我们很可能看不到针对此问题的解决方案.

资料来源:https://bugs.python.org/issue32245


Lli*_*ane 10

我认为这是11月创建者更新引入的stdout/stderr流上的一个错误,它发生在powershell.exe和cmd.exe中

它似乎只发生在Windows 10版本1709(OS Build 16299.64)上.我的猜测是它是unicode实现的(输出大小是预期长度的两倍)

一个(非常)快速和脏的修复方法是仅在控制台上输出ASCII:

mystring.encode("utf-8").decode("ascii")

https://github.com/Microsoft/vscode/issues/39149#issuecomment-347260954