I am struggling with a problem that I just can't get to work! I am currently working with Tensorflow and worked through the basic tutorials.
As you can see in the tutorial, the neural-network-model expects a train_images Numpy array of shape (60000, 28, 28)
as there are 60,000 images of size 28x28 in the training set of the tutorial. I am reading in the Flavia-dataset, which is a set of pictures of leaves. The traning set consists of 1588 pictures which have 300x300px resolution. Here's my code:
for root, dirs, files in os.walk(pathname_data):
for name in files:
img = keras.preprocessing.image.load_img(os.path.join(root,name), color_mode="grayscale",target_size=(300,300)) #get image in 300x300 grayscale
array = keras.preprocessing.image.img_to_array(img) #convert to numpy array
array = array.squeeze(axis=2) #convert to 300x300 2d array
array = array / 255.0 #preprocess data
pathSegments = os.path.normpath(os.path.join(root,name)).split(os.sep) #split path
if pathSegments[len(pathSegments)-3] == "Train": #assign to training- or testSet
#TODO: how to store the 2x2 arrays ??
#store in training set
elif pathSegments[len(pathSegments)-3] == "Test":
#store in test set
My question is now, how do I store "array" so that I end up with a (1588, 300, 300)
-shaped Numpy array that I can feed to my model? I already tried to experiment with reshape
, append and transpose, but as of yet to no avail :( Any help greatly appreciated!
I assume that every 'array' you generate from a file is a (300, 300)
shape
You can either pregenerate the array and use a counter
all_img = np.empty((1588, 300, 300))
count = 0
for root, dirs, files in os.walk(pathname_data):
for name in files:
...
all_img[count] = array.copy()
count += 1
...
or you could append all the images to a list and change it into an array later
all_img = []
for root, dirs, files in os.walk(pathname_data):
for name in files:
...
all_img.append(array)
...
all_img = np.array(all_img)
Both this method will give you a (1588, 300, 300)
array, I've no experience with Tensorflow, but this is the shape you required.