简体   繁体   English

python3 解释器给出的结果与 scipy.misc.imread 的脚本不同

[英]python3 interpreter gives different results than script for scipy.misc.imread

I am trying to read image data into Python as a matrix.我正在尝试将图像数据作为矩阵读入 Python。

To this extent, I am trying to use scipy.misc.imread('image.jpg').astype(np.float) .在这方面,我正在尝试使用scipy.misc.imread('image.jpg').astype(np.float)

When I execute the proper sequence of steps from a python3 interpreter, everything works swimmingly, and I get a matrix as expected.当我从python3解释器中执行正确的步骤序列时,一切正常,我得到了一个预期的矩阵。

When I invoke the command from a script ( python3 foo.py ...), however, I get an error complaining that the argument to convert via float cannot be of type JpegImageFile .但是,当我从脚本 ( python3 foo.py ...) 调用命令时,我收到一个错误,抱怨通过float转换的参数不能是JpegImageFile类型。 I've ensured that I've pip install -U pillow to ensure that PIL is available.我确保我已经pip install -U pillow以确保 PIL 可用。

What gives?是什么赋予了? How could this be possible?这怎么可能? I've verified over and over that the same lines of code are executed in each case, the only difference seems to be that the invocation inside of a script happens inside of a defined function, but even if I pdb.set_trace() from elsewhere in the script the same results happen.我一遍又一遍地验证在每种情况下都执行相同的代码行,唯一的区别似乎是脚本内部的调用发生在定义的函数内部,但即使我pdb.set_trace()来自其他地方在脚本中会发生相同的结果。

What could be causing fluctuation in results from the interpreter to the script?什么可能导致解释器到脚本的结果波动?

EDIT : OK, to be precise, I am running the neural_style.py script from here: https://github.com/anishathalye/neural-style .编辑:好的,准确地说,我正在从这里运行neural_style.py脚本: https : //github.com/anishathalye/neural-style scipy , numpy , tensorflow , and pillow must be installed.必须安装scipynumpytensorflowpillow I am using python3 .我正在使用python3 Any parameters for --content , --styles , and --output should work to dupe the bug. --content--styles--output任何参数都应该可以--styles错误。

Full error traceback:完整的错误回溯:

Traceback (most recent call last):
  File "neural_style.py", line 150, in <module>
    main()
  File "neural_style.py", line 84, in main
    content_image = imread(options.content)
  File "neural_style.py", line 141, in imread
    return scipy.misc.imread(path).astype(np.float)
TypeError: float() argument must be a string or a number, not 'JpegImageFile'

But, a small simple script such as the following actually works:但是,像下面这样的一个简单的小脚本实际上是有效的:

import numpy as np
import scipy.misc
print(scipy.misc.imread('content').astype(np.float))

I figured out the solution to this.我想出了解决这个问题的办法。 The neural_style.py script seems to be getting a different version of scipy module (or submodule) due to a side effect of importing stylize.py and, in turn, vgg.py .neural_style.py脚本似乎得到了不同版本的scipy模块(或子模块)由于进口的副作用stylize.py ,反过来, vgg.py Adding this line:添加这一行:

import scipy.misc

To the very top of stylize.py (in front of import vgg ) fixes it.stylize.py的最顶部(在import vgg )修复它。

I'm really not sure why, though.不过,我真的不知道为什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM