I am trying to train a CNN with 20 jpeg images just as an exercise. I chose the shape of the input layer to be input_shape=(32, 32, 3) but I am getting errors. When I run a "print shape" for the image data array I get (10, ). I am not sure why it is like this. Shouldnt a color image shape have 3 or 4 dimensions? The shapes of my array of jpegs seems to be (10,)...one dimensional. How do I transform the shape in order to use the fit function below and to what shape?
import tensorflow as tf
from tensorflow.keras import layers, models
from matplotlib import pyplot
import random
import numpy as np
from os import listdir
from matplotlib import image
# load all the cat train images in the cat train directory
imagesWithLabels = []
for filename in listdir('C:/AI/images/airplanes'):
# load image
img_data = image.imread('C:/AI/images/airplanes/' +\
filename)
# store loaded image in a list
imagesWithLabels.append((img_data,0))
print('> loaded %s %s' % (filename, img_data.shape))
for filename in listdir('C:/AI/images/automobiles'):
# load image
img_data = image.imread('C:/AI/images/automobiles/' +\
filename)
# store loaded image in the list
imagesWithLabels.append((img_data,1))
print('> loaded %s %s' % (filename, img_data.shape))
#check to see that all 20 images of planes and autos are there
len(imagesWithLabels)
random.shuffle(imagesWithLabels)
type(imagesWithLabels)
train = imagesWithLabels[:10]
test = imagesWithLabels[10:]
x_train, y_train = zip(*train)
x_test, y_test = zip(*test)
x_train = np.array(x_train)
x_test = np.array(x_test)
y_train = np.array(y_train)
y_test = np.array(y_test)
type(x_train)
type(x_test)
for i in range(10):
pyplot.imshow(x_train[i] )
pyplot.show()
CNN_model = models.Sequential()
CNN_model.add(layers.Conv2D(50, (2, 2), activation='relu',\
input_shape=(32, 32, 3)))
CNN_model.add(layers.MaxPooling2D((3, 3)))
CNN_model.add(layers.Flatten())
CNN_model.add(layers.Dense(50, activation='relu'))
CNN_model.add(layers.Dropout(.1))
CNN_model.add(layers.Dense(10, activation='softmax'))
optimizer = tf.optimizers.Adam(learning_rate = .005)
CNN_model.compile(optimizer=optimizer,
loss=tf.keras.losses.SparseCategoricalCrossentropy
(from_logits=False),metrics=['accuracy'])
history = CNN_model.fit(x_train, y_train, epochs=5,validation_data=( x_test, y_test))
Assuming all the images are RGB images of the same size (W, H, C) = (32, 32, 3)
, you can load them and store them in a Python list as you are doing (with labels). If you read 10 images, then this list is to length 10, and each item is a Numpy array or Tensor of shape (32, 32, 3)
.
However, to train a network you must transform this list of Tensors into one Tensor. You achieve this by stacking the images along one new dimension:
x_train, y_train = zip(*train)
x_test, y_test = zip(*test)
x_train = tf.stack(x_train)
x_test = tf.stack(x_test)
y_train = tf.convert_to_tensor(y_train)
y_test = tf.convert_to_tensor(y_test)
Those tensors should be of size (N, 32, 32, 3)
where N
is the number of images in each set. Do not forget to transform everything into a Tensor before use in a network. You should usetf.convert_to_tensor()
which convert Python lists and Numpy arrays to Tensors.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.