DrSleep / tensorflow-deeplab-resnet

DeepLab-ResNet rebuilt in TensorFlow
MIT License
1.25k stars 429 forks source link

Running inference for a batch of multiple images #143

Closed brijml closed 7 years ago

brijml commented 7 years ago

How can one use the inference.py file for a batch a of multiple images?

brijml commented 7 years ago

I figured it out by using a placeholder in place of "img" in this line, net = DeepLabResNetModel({'data': tf.expand_dims(img, dim=0)}, is_training=False, num_classes=args.num_classes) to img_ph = tf.placeholder(tf.float32, shape = [None, None, None, 3]) net = DeepLabResNetModel({'data': img_ph}, is_training=False, num_classes=args.num_classes) and feeding in the batch of images

Thank You

burhanbvk commented 6 years ago

Could you share any sample code showing how to feed the images?

Thanks

brijml commented 6 years ago

import cv2, os, numpy import tensorflow as tf

#if you have stored the images in a directory, directory_path is the absolute path to the directory

imgs = [] for file_ in os.listdir(directory_path): img = cv2.imread(os.path.join(directory_path,file_)) imgs.append(img)

#all the images must be of same size, you will have to resize if they are not of same size #imgs is list of numpy ndarrays, you can convert this list to array so that now this array can be used as a tensor

imgs_array = numpy.array(img)

#You will have defined an op called 'pred' #Now when you sess.run the op, you will use feed_dict to feed the imgs_array tensor with tf.Session() as sess: preds = sess.run(pred, feed_dict={imgs_ph:imgs_array})

burhanbvk commented 6 years ago

Thanks, that helped a lot. I was using pillow and converting the loaded image to a numpy array. Using opencv made it work without a problem.

wycm2022 commented 4 years ago

How to implement the complete code, thank you

davodogster commented 4 years ago

How to implement the complete code, thank you @wycm2022 I wrote the code here: https://gist.github.com/davodogster/88794fea3f6800fd2150f7d436f06e34