如何将图像加载到张量流以与模型一起使用?

时间:2019-07-11 09:27:05

标签: python machine-learning keras tf.keras

我刚刚开始学习机器学习,并且正在使用Tensorflow 1.14。我刚刚使用内置的<link href="https://cdn.jsdelivr.net/npm/vuetify@1.2.2/dist/vuetify.min.css" rel="stylesheet" /> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/vuetify@1.2.2/dist/vuetify.min.js"></script> <div id="app"> <template> <div class="home"> <v-img src="https://images.unsplash.com/photo-1562696482-77619dec0ae7?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1351&q=80" max-height="100%"> <v-container style="height: 100%;" align-center d-flex fluid class="pa-0"> <v-layout align-center column> <v-flex class="mb-2"> <span class="primary--text text-uppercase display-3 font-weight-thin">List</span> <span class="white--text text-uppercase display-3 font-weight-thin">Series</span> </v-flex> <v-flex class="mb-4"> <h4 class="subheading grey--text">Follow the series you've been watching or are still watching!</h4> </v-flex> <v-flex> <v-btn color="primary" depressed flat outline to="/add"> go to add </v-btn> </v-flex> </v-layout> </v-img> </v-container> </div> </template> </div>数据集使用static public ILog MainLogger { get { if (_logger == null) { _logger = LogManager.GetLogger(@"MyLibLogger"); } return _logger; } } 创建了我的第一个模型。这是我的模型的代码:

tensorflow.keras

现在模型已经训练好了,我可以将tensorflow.keras.datasets.mnist图像输入到import tensorflow as tf from tensorflow import keras mnist = keras.datasets.mnist (x_train, y_train), (x_test, y_test) = mnist.load_data() class Stopper(keras.callbacks.Callback): def on_epoch_end(self, epoch, log={}): if log.get('acc') >= 0.99: self.model.stop_training = True print('\nReached 99% Accuracy. Stopping Training...') model = keras.Sequential([ keras.layers.Flatten(), keras.layers.Dense(1024, activation=tf.nn.relu), keras.layers.Dense(10, activation=tf.nn.softmax)]) model.compile( optimizer=tf.train.AdamOptimizer(), loss='sparse_categorical_crossentropy', metrics=['accuracy']) x_train, x_test = x_train / 255, x_test / 255 model.fit(x_train, y_train, epochs=10, callbacks=[Stopper()]) 中,效果很好。但是我想知道如何将自己的图像(JPG和PNG)输入到模型的x_test方法中?

我查看了documentation,他们的方法对我来说是一个错误。我特别尝试了以下方法:

model.predict()

请提供逐步指南,以将图像(JPG和PNG)获取到我的模型中进行预测。非常感谢。

2 个答案:

答案 0 :(得分:0)

每个图像基本上都是由像素组成的,您可以将这些像素值传递到神经网络。

要将图像转换为像素阵列,可以使用skimage之类的库,如下所示。

from skimage.io import imread
imagedata=imread(imagepath)
#you can pass this image to the model

要读取一组图像,请将其循环,然后将数据存储在数组中。 另外,您还必须调整大小以标准化所有图片,以将其加载到NN中。

resized_image = imagedata.resize(preferred_width, preferred_height, Image.ANTIALIAS)     

您也可以选择将图像转换为黑白图像以减少计算量,我使用的是枕头库,这里是一个常见的图像预处理库,用于应用黑白滤镜

from PIL import Image
# load the image
image = Image.open('opera_house.jpg')
# convert the image to grayscale
gs_image = image.convert(mode='L')

预处理顺序可以是

1. convert images to black and white 
2. resize the images
3. convert them into numpy array using imread  

答案 1 :(得分:0)

from PIL import Image
img = Image.open("image_file_path").convert('L').resize((28, 28), Image.ANTIALIAS)
img = np.array(img)
model.predict(img[None,:,:])

您已经使用大小(28 X 28)的图像训练了模型,因此必须将图像调整为相同大小。您不能使用其他尺寸的图像。

预测需要一批图像,但是由于要对单个图像进行预测,因此必须为该单个图像添加批次的其他尺寸。这是通过expand_dimreshapeimg[None,:,:]

完成的