【问题标题】:How to import images to google colab to use in my model如何将图像导入 google colab 以在我的模型中使用
【发布时间】:2022-10-19 19:44:19
【问题描述】:

我使用 cifar10 数据集在 google Collaboratory 中创建了一个模型,并用它来预测图像及其标签。这非常有效,我对结果非常满意。然后我想预测我自己的图像,因为这就是我要使用它的目的。我想通过安装我的驱动器将图像上传到我目前正在做的谷歌 colab 中。然后我想把那个图像文件夹变成一个形状数组(图像数量,32,32,3)我目前正在重塑它们并使用 keras.preprocessing.image.dataGenerator 然后使用 .flow_from_directory 来获取图片。当我将它放入模型时它似乎可以工作,但我想使用 matplotlib.imshow 查看图像。当我尝试这个时,它会抛出一个错误,它无法将形状 (8,32,32,3) 的输入数组广播到数组大小 (8)。为什么它试图重塑阵列。对不起,我对这一切都很陌生。这是我的代码。它非常混乱,我尝试了很多愚蠢的事情。

import tensorflow as tf
from keras.preprocessing.image import img_to_array
from keras.preprocessing.image import array_to_img
from keras.preprocessing.image import load_img
from keras.preprocessing.image import ImageDataGenerator

from keras.preprocessing.image import DirectoryIterator
from google.colab import files

test=ImageDataGenerator(rescale=1./255)
test_ims=DirectoryIterator('/content/drive/MyDrive/test/',test,target_size=(32,32),batch_size=32,class_mode='sparse')
test_set=test.flow_from_directory('/content/drive/MyDrive/test/',target_size=(32,32),batch_size=32,class_mode='sparse')
#print(test_set[0])
print(test_ims)

#imarray=np.array([img_to_array(img)])

!ls saved_model
modelll=tf.keras.models.load_model('/content/saved_model/mymode3')

#history=modelll(test_set)
#print(history)
#print(np.argmax(history[0]))
probability_model1 = tf.keras.Sequential([modelll, 
                                         tf.keras.layers.Softmax()])
prediction1=probability_model1.predict(test_set)
#print(prediction1)

#print('10')
history1=np.argmax(prediction1[6])
print(test_set.__getitem__(0))
plt.imshow(test_set.__getitem__(0))
#print(history1)
#print(test_set)


#print(cifclassnems[history[0]])
#print('the rock')```
But yeah I just want to import images and run them through the model. The model is named modelll(don't ask). Anything is helpful! Thank you!

【问题讨论】:

    标签: tensorflow keras google-colaboratory


    【解决方案1】:

    尝试迭代图像数组以在 matplotlib 中显示,如下所示

    # plot test_set images
     n_samples = 3
     for i in range(n_samples):
        pyplot.subplot(2, n_samples, 1 + i)
        pyplot.axis('off')
        pyplot.imshow(test_set[i].astype('uint8'))
     pyplot.show()
    

    【讨论】:

      【解决方案2】:

      我真的很好奇您为什么要添加另一个 softmax 函数,我假设您已经从保存的模型中获得了最后一层,您在训练期间是否使用了任何形式的迁移学习,如果是这种情况,您将需要使用预训练模型的精确预处理函数对传入的图像进行预处理

      【讨论】:

        猜你喜欢
        • 2020-06-14
        • 2022-01-08
        • 1970-01-01
        • 2019-03-14
        • 2021-03-30
        • 2020-01-24
        • 2021-08-14
        • 2020-12-15
        • 2021-06-21
        相关资源
        最近更新 更多