Reputation: 125
I have a folder of images on my computer but I do not know how to set up the input layer. This is the code they provided on there sample page. I am not sure how to implement my folder into this format.
def main(unused_argv):
# Load training and eval data
mnist = tf.contrib.learn.datasets.load_dataset("mnist")
train_data = mnist.train.images # Returns np.array
train_labels = np.asarray(mnist.train.labels, dtype=np.int32)
eval_data = mnist.test.images # Returns np.array
eval_labels = np.asarray(mnist.test.labels, dtype=np.int32)
Upvotes: 0
Views: 3107
Reputation: 4165
You need to convert your images in the folders to vectors (set of numbers) to feed into the network.
In the above example of mnist you actually use a compressed binary version of the images. So no preprocessing is required from your end (except scaling pixels to be between 0-1).
Suppose you have a image that is RGB named orig.jpg
in current working directory.
You will load that image as
import numpy as np
from PIL import Image
img = Image.open('orig.jpg').convert('RGB')
img = np.array(img)
This will give you all the image content in the form of numbers to a numpy array variable. img
in this case.
From your question, you are implementing a Convolution Neural network, so you do not need to flatten the img
variable to one dimensional vector.
You can check the shape of the variable by running
np.shape(img)
Typically the shape will be a (lenght, width, 3)
One last step before you train your model with the image you may need to normalize the RGB values. You can do so by dividing img
with 255. This usually improves your optimization algorithm
If you want to iterate over the complete image in a directory. You could do so by
for filename in os.listdir(directory_name):
if filename.endswith(".jpg"):
# code to extract the values using os.path.join(directory_name,filename)
The complete code to iterate and save all the images with .jpg to np.array is as follows.
from PIL import Image
import numpy as np
import os.path
length = 128 # pixels in length
width = 128 # pixels in width
imgs = np.empty((0,length, width, 3)) #empty dummy array, we will append to this array all the images
for filename in os.listdir(directory_name):
if filename.endswith(".jpg"):
img = Image.open(os.path.join(directory_name,filename)).convert('RGB')
imgs = np.append(imgs, np.array(img).reshape((1, length, width, 3)), axis=0)
After the loop completes, the imgs
will have a shape of (num_image, length, width, 3)
Now you can use this imgs
and build some input pipeline with tf.data.Dataset.from_tensor_slices(....)
or you can directly use the imgs
Upvotes: 2