IndentationError:需要缩进的块-Python机器学习的猫/狗

时间:2018-12-03 13:35:02

标签: python machine-learning conv-neural-network cat dog

那是我的代码:

test_image = image.load_img('dataset/single_prediction/cat_or_dog_1.jpg', target_size = (64, 64))
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis = 0)
result = classifier.predict(test_image)
training_set.class_indices
if result[0][0] == 1:
prediction = 'dog'
else:
prediction = 'cat' `

我有这样的错误:

File "<ipython-input-31-35ebf5fa8bf7>", line 7
prediction = 'dog'
         ^
IndentationError: expected an indented block

有人可以帮助我吗?

4 个答案:

答案 0 :(得分:1)

您的代码未正确缩进:

test_image = image.load_img('dataset/single_prediction/cat_or_dog_1.jpg', target_size = (64, 64))
test_image = image.img_to_array(test_image)
 test_image = np.expand_dims(test_image, axis = 0)
result = classifier.predict(test_image)
training_set.class_indices
if result[0][0] == 1:
    prediction = 'dog'
else:
    prediction = 'cat' 

答案 1 :(得分:0)

Python的缩进功能块。参考:
https://www.python.org/dev/peps/pep-0008/#indentation

Your code should be like:  
if result[0][0] == 1:  
<4 spaces>prediction = 'dog'  
else:  
<4 spaces>prediction = 'cat'  

答案 2 :(得分:0)

在Python中,缩进很重要。 因此,您需要缩进块(例如if中的内容):

if result[0][0] == 1:
  prediction = 'dog'
#...

答案 3 :(得分:0)

Python循环结构(如if,for等)需要在:之后加上适当的缩进。 在IDE中键入:之后,应按Enter键,以自动跳至应添加将在循环中添加下一行代码的位置。如果您没有正确缩进python,请考虑在if循环之外还是抛出缩进错误。您的代码应如下所示:

test_image = image.load_img('dataset/single_prediction/cat_or_dog_1.jpg',target_size=(64,64))
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis = 0)
result = classifier.predict(test_image)
training_set.class_indices
if result[0][0] == 1:
    prediction = 'dog'
else:
    prediction = 'cat'