Python 图像分类故障排除



嗨,伙计们,这可能是一个愚蠢的问题,但我一直在寻找这个变量来自哪里

def predict(file):

正如您在"预测"上看到的,有一个名为"文件"的参数 我想知道"文件"上的数据从何而来

谢谢!

这是完整的代码

import os
import numpy as np
from keras.preprocessing.image import ImageDataGenerator, load_img, img_to_array
from keras.models import Sequential, load_model
import time
start = time.time()
# Define Path
# model_path = './models/model.h5'
# model_weights_path = './models/weights.h5'
test_path = 'data/test_image'
# Load the pre-trained models
# model = load_model(model_path)
# model.load_weights(model_weights_path)
# Define image parameters
img_width, img_height = 150, 150
# Prediction Function

def predict(file):
model_path = './models/model.h5'
model_weights_path = './models/weights.h5'
model = load_model(model_path)
model.load_weights(model_weights_path)
x = load_img(file, target_size=(img_width, img_height))
x = img_to_array(x)
x = np.expand_dims(x, axis=0)
array = model.predict(x)
result = array[0]
# print(result)
answer = np.argmax(result)
if answer == 0:
print("Predicted: Drusen")
elif answer == 1:
print("Predicted: Normal")
return answer

# Walk the directory for every image
for i, ret in enumerate(os.walk(test_path)):
for i, filename in enumerate(ret[2]):
if filename.startswith("."):
continue
print(ret[0] + '/' + filename)
result = predict(ret[0] + '/' + filename)
print(" ")
# Calculate execution time
end = time.time()
dur = end-start
if dur < 60:
print("Execution Time:", dur, "seconds")
elif dur > 60 and dur < 3600:
dur = dur/60
print("Execution Time:", dur, "minutes")
else:
dur = dur/(60*60)
print("Execution Time:", dur, "hours")

predict(file)的调用如下:

result = predict(ret[0] + '/' + filename)

因此,它采用文件名作为输入。在功能内部,这部分

x = load_img(file, target_size=(img_width, img_height))

使用keras.preprocessing.image模块中的load_img函数。因此,file参数采用文件名,并将其转换为图像:

x = img_to_array(x)
x = np.expand_dims(x, axis=0)
array = model.predict(x)

在变成NumPy数组后,它被添加了一个通道维度。最后,预测其标签。

最新更新