使用imageio到标量变量的无效索引

时间:2019-06-19 08:43:36

标签: python image neural-network python-imageio

我正在使用imageio将猫和马的图像导入神经网络vgg-19,然后我只想导入单个图像来预测类,但是标量变量的索引无效。

这是我在运行模型vgg19之前导入图像的方式。

def generator(batch_size, datapath):
    from random import shuffle
    target = glob.glob(datapath + '*.png')  
    n_samples = len(target)  
    n_batches = n_samples // batch_size 
    b = n_batches

    while True:

        if b == n_batches:  
            shuffle(target)
            b = 0
            print("epoch finished - " + datapath)

        # initialize current batch
        batch_features = np.zeros((batch_size, LEFT, RIGHT, 3))  
        batch_labels = np.zeros((batch_size, 2))
        target_b = target[b * batch_size:(b + 1) * batch_size]  

        # populate current batch
        for i, t in enumerate(target_b):
            batch_features[i, :, :, :] = imageio.imread(t)[:, :, :3]  

            batch_labels[i, :] = np.array([1, 0]) if "cat" in t else np.array([0, 1])

        b += 1
        yield batch_features, batch_labels

比运行模型vgg-19。

这是训练模型后如何导入单个图像的方法:

im = imageio.imread('test_image.png')[:, :, :3]
im = np.expand_dims(im, axis=0)
print(im.shape)
from keras.preprocessing.image import img_to_array

yhat = model.predict(im)
print(yhat)
class_labels = ['cat', 'horse'] 
pred = np.argmax(class_labels)
print(class_labels[pred[0]])

在代码的最后一行,标量变量索引无效。它显示“ yhat”,但不给预测提供类别标签。 我应该如何将代码修复为相同格式?

0 个答案:

没有答案