Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to implement batch predict? #61

Closed
fanweiya opened this issue May 13, 2021 · 3 comments
Closed

How to implement batch predict? #61

fanweiya opened this issue May 13, 2021 · 3 comments

Comments

@fanweiya
Copy link

Hi Ayoola, many thanks for creating this library.How do I implement batch predict?How can this library process hundreds of images at once?

@ayoolaolafenwa
Copy link
Owner

@fanweiya you will create a folder and put the images inside the folder. Perform batch prediction with this modified code.

import pixellib
from pixellib.instance import instance_segmentation
import cv2
#import glob to use it to retrieve image names
import glob


seg = instance_segmentation()
seg.load_model("mask_rcnn_coco.h5")

#get image names with glob
imgnames = sorted(glob.glob("pics/*.jpg"))

#loop through each image in the folder
for imgname in imgnames:
    #process images
   #segment each image consecutively through the loop
    segvalues, output = seg.segmentImage(imgname)
    
    # save output images
   #get image name with the corresponding extension and save them 
  #image saved like sample_output.jpg
    name, ext = os.path.splitext(imgname)
    imgname2 = imgname+"_output"+ext
    cv2.imwrite(imgname2, output) 

Note You did not specify whether you want to batch predict in instance segmentation or semantic segmentation, but the code for iterating through the folder of images is still the same for both, except you will replace the code for instance segmentation for semantic segmentation for performing batch prediction in semantic segmentation.

@fanweiya
Copy link
Author

fanweiya commented May 18, 2021

@ayoolaolafenwa I want to predict more than one image at a time,something like Keras model.predict_on_batch.

@fanweiya you will create a folder and put the images inside the folder. Perform batch prediction with this modified code.

import pixellib
from pixellib.instance import instance_segmentation
import cv2
#import glob to use it to retrieve image names
import glob


seg = instance_segmentation()
seg.load_model("mask_rcnn_coco.h5")

#get image names with glob
imgnames = sorted(glob.glob("pics/*.jpg"))

#loop through each image in the folder
for imgname in imgnames:
    #process images
   #segment each image consecutively through the loop
    segvalues, output = seg.segmentImage(imgname)
    
    # save output images
   #get image name with the corresponding extension and save them 
  #image saved like sample_output.jpg
    name, ext = os.path.splitext(imgname)
    imgname2 = imgname+"_output"+ext
    cv2.imwrite(imgname2, output) 

Note You did not specify whether you want to batch predict in instance segmentation or semantic segmentation, but the code for iterating through the folder of images is still the same for both, except you will replace the code for instance segmentation for semantic segmentation for performing batch prediction in semantic segmentation.

@ayoolaolafenwa
Copy link
Owner

ayoolaolafenwa commented May 22, 2021

@fanweiya this is presently the way to predict multiple images with pixellib and in the nearest future I may introduce a batch prediction function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants