预期整数参数,但得到浮点数

我在尝试编写代码以训练系统识别不同动物的图像,这就是我的代码。我使用Anaconda作为解释器,并使用PyCharm作为开发环境。

import tensorflow as tfimport os, sysfrom PIL import Imageimage_path = 'test_images/leopard2.jpg'size = (299, 299)infile = image_pathoutfile = 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_dataimage_data = tf.gfile.FastGFile(outfile, 'rb').read()# Loads label file, strips off carriage returnlabel_lines = [line.rstrip() for line                 in tf.gfile.GFile("output_labels.txt")]# Unpersists graph from filewith 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 predictionsoftmax_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 confidencetop_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.pyTraceback (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 pasteself.im.paste(im, box)TypeError: integer argument expected, got floatProcess finished with exit code 1

我使用的图像是JPEG格式,并且路径与代码中定义的路径一致。有人知道这里的问题出在哪里吗?


回答:

你的除以2的操作没有完全被int()函数包围。如果你的(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)))

Related Posts

Keras Dense层输入未被展平

这是我的测试代码: from keras import…

无法将分类变量输入随机森林

我有10个分类变量和3个数值变量。我在分割后直接将它们…

如何在Keras中对每个输出应用Sigmoid函数?

这是我代码的一部分。 model = Sequenti…

如何选择类概率的最佳阈值?

我的神经网络输出是一个用于多标签分类的预测类概率表: …

在Keras中使用深度学习得到不同的结果

我按照一个教程使用Keras中的深度神经网络进行文本分…

‘MatMul’操作的输入’b’类型为float32,与参数’a’的类型float64不匹配

我写了一个简单的TensorFlow代码,但不断遇到T…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注