python - 需要整数参数,得到 float

标签 python tensorflow machine-learning

我正在尝试编写图像识别代码,以针对不同动物图像训练系统,这就是代码。我使用 anaconda 作为解释器, 使用pycharm作为环境。

import tensorflow as tf
import os, sys
from PIL import Image

image_path = 'test_images/leopard2.jpg'

size = (299, 299)

infile = image_path
outfile = os.path.splitext(infile)[0] + '_resized.jpg'
try:
  im = Image.open(infile)
  im.thumbnail(size, Image.ANTIALIAS)
  old_im_size = im.size

## By default, black colour would be used as the background for padding!
new_im = Image.new("RGB", size)

new_im.paste(im,(int(size[0]-old_im_size[0])/2,int(size[1]- 
old_im_size[1])/2))

new_im.save(outfile, "JPEG")
except IOError:
print("Cannot resize '%s'") %infile



# Read in the image_data
image_data = tf.gfile.FastGFile(outfile, 'rb').read()

# Loads label file, strips off carriage return
label_lines = [line.rstrip() for line 
                in tf.gfile.GFile("output_labels.txt")]

# Unpersists graph from file
with tf.gfile.FastGFile("output_graph.pb", 'rb') as f:
   graph_def = tf.GraphDef()
   graph_def.ParseFromString(f.read())
  _  = tf.import_graph_def(graph_def, name='')

init_ops = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init_ops)
# Feed the image_data as input to the graph and get first prediction
softmax_tensor = sess.graph.get_tensor_by_name('final_result:0')

predictions = sess.run(softmax_tensor, \
         {'DecodeJpeg/contents:0': image_data})

# Sort to show labels of first prediction in order of confidence
top_k = predictions[0].argsort()[-len(predictions[0]):][::-1]

 for node_id in top_k:
    human_string = label_lines[node_id]
    score = predictions[0][node_id]
    print('%s (score = %.5f)' % (human_string, score))
 os.remove(outfile)

出现的错误是

C:\Users\snklp\Anaconda3\envs\untitled\python.exe 
C:/Users/snklp/Downloads/Transfer-Learning-for-Animal-Classification-in- 
Tensorflow-master/Transfer-Learning-for-Animal-Classification-in-Tensorflow- 
master/test.py
Traceback (most recent call last):
File "C:/Users/snklp/Downloads/Transfer-Learning-for-Animal-Classification- 
in-Tensorflow-master/Transfer-Learning-for-Animal-Classification-in- 
Tensorflow-master/test.py", line 19, in <module>
new_im.paste(im,(int(size[0]-old_im_size[0])/2,int(size[1]- 
old_im_size[1])/2))
File "C:\Users\snklp\Anaconda3\envs\untitled\lib\site-packages\PIL\Image.py", 
line 1423, in paste
self.im.paste(im, box)
TypeError: integer argument expected, got float

Process finished with exit code 1

我使用的图像是 jpeg 格式,并且位于代码中定义的正确路径中。有人知道这里有什么问题吗?

最佳答案

除以 2 没有被 int() all 包裹。如果您的 (size[0]-old_im_size[0])(size[1]- old_im_size[1]) 出现奇怪的结果,则代码将会损坏。

试试这个:

new_im.paste(im,(int((size[0]-old_im_size[0])/2), int((size[1]- old_im_size[1])/2)))

关于python - 需要整数参数,得到 float ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51179084/

相关文章:

python - 限制 Keras 中使用的核心数

scala - 如何将微风.linalg.DenseMatrix 转换为 Map[String,Value]?

python - pandas.DataFrame.plot(kind ="bar")的更多绘图选项

Python 曲线平移

python - TF 2.0 中的 tf.GradientTape 是否等同于 tf.gradients?

python - 在大数据集上训练模型的最佳实践是什么

tree - 使用机器学习比较树木变化的最佳技术/算法是什么?

r - 如何使用 R 将两个或多个变量转换(计算)为一个?

python - 使用命令行中的 python 文件可执行文件去除空格和注释

python - 在Python 3中计算贷款的结束日期