Reading Images in Tensorflow

I watched this Tensorflow tutorial .

In the tutorial, images are magically read as follows:

mnist = learn.datasets.load_dataset("mnist")
train_data = mnist.train.images

My images are located in two directories:

../input/test/
../input/train/

All of them have a completion *.jpg.

So how can I read them in my program?

I don’t think I can use it learn.datasets.load_dataset, because this, apparently, refers to the specialized structure of the data set, whereas I only have folders with images.

+4
source share
2 answers

mnist.train.images - , , [55000, 784]. 55000 - , 784 - ( - 28x28).

numpy , . , , numpy, [num_examples, image_size]

:

import os
import cv2
import numpy as np
def load_data(img_dir):
    return np.array([cv2.imread(os.path.join(img_dir, img)).flatten() for img in os.listdir(img_dir) if img.endswith(".jpg")])

:

import os
list_of_imgs = []
img_dir = "../input/train/"
for img in os.listdir("."):
    img = os.path.join(img_dir, img)
    if not img.endswith(".jpg"):
        continue
    a = cv2.imread(img)
    if a is None:
        print "Unable to read image", img
        continue
    list_of_imgs.append(a.flatten())
train_data = np.array(list_of_imgs)

: 28x28x1 (- ), ( cnn_model_fn). , , MNIST. Alexnet RGB.

+5
+2

Source: https://habr.com/ru/post/1680825/


All Articles