Reputation: 731
The code I previously wrote works well on a single image. But now I want the program to run multiple images. I just need to give the folder name as an argument.
I modified my code in such a way that it opens a directory and saves the images but I get an error
ValueError: cannot reshape array of size 98304 into shape (1,128,128,3)
on x_batch=images.reshape(1,128,128,3)
:
images = []
for filename in os.listdir(folder):
image = cv2.imread(os.path.join(folder,filename))
image = cv2.resize(image, (128, 128))
images = np.append(images, image)
images = np.array(images, dtype=np.uint8)
images = images.astype('float32')
images = np.multiply(images, 1.0/255.0)
x_batch=images.reshape(1,128,128,3) <------ ERROR HERE
sess = tf.Session()
saver = tf.train.import_meta_graph('/home/kubuntu/SimpleCode/.meta')
saver.restore(sess, tf.train.latest_checkpoint('./'))
graph = tf.get_default_graph()
y_pred = graph.get_tensor_by_name("y_pred:0")
x= graph.get_tensor_by_name("x:0")
y_true = graph.get_tensor_by_name("y_true:0")
y_test_images = np.zeros((1, 6))
feed_dict_testing= {x:x_batch, y_true:y_test_images}
result=sess.run(y_pred, feed_dict=feed_dict_testing)
print("Up :"+str(result[0][0]))
print("Down :"+str(result[0][1]))
print("Left :"+str(result[0][2]))
print("Right :"+str(result[0][3]))
print("Forward :"+str(result[0][4]))
print("Backward :"+str(result[0][5]))
Is this the correct way of reading images from a folder? How do I classify all the images inside a given folder and gives the prediction for each image?
Upvotes: 0
Views: 838
Reputation: 5555
Based on your answers you should do the following:
for filename in os.listdir(folder):
image = cv2.imread(os.path.join(folder,filename))
image = cv2.resize(image, (128, 128))
image = np.array(image, dtype=np.uint8)
image = image.astype('float32')
image = np.multiply(image, 1.0/255.0)
x_batch=image.reshape(1,128,128,3)
The code failed when you were reading the second image, because the images
array had two images appended and you were trying to reshape it like there is only one image.
Besides, it's an extremely bad practice to iteratively create tf.Session
in a for loop and load the graph all the time. I would change the whole code in the following way:
with tf.Session() as sess:
saver = tf.train.import_meta_graph('/home/kubuntu/SimpleCode/.meta')
saver.restore(sess, tf.train.latest_checkpoint('./'))
graph = tf.get_default_graph()
y_pred = graph.get_tensor_by_name("y_pred:0")
x = graph.get_tensor_by_name("x:0")
y_true = graph.get_tensor_by_name("y_true:0")
y_test_images = np.zeros((1, 6))
for filename in os.listdir(folder):
image = cv2.imread(os.path.join(folder,filename))
image = cv2.resize(image, (128, 128))
image = np.array(image, dtype=np.uint8)
image = image.astype('float32')
image = np.multiply(image, 1.0/255.0)
x_batch=image.reshape(1,128,128,3)
feed_dict_testing= {x:x_batch, y_true:y_test_images}
result = sess.run(y_pred, feed_dict=feed_dict_testing)
print("Up :"+str(result[0][0]))
print("Down :"+str(result[0][1]))
print("Left :"+str(result[0][2]))
print("Right :"+str(result[0][3]))
print("Forward :"+str(result[0][4]))
print("Backward :"+str(result[0][5]))
Upvotes: 1