DrSleep / tensorflow-deeplab-resnet

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

Predicting on the fly #142

Closed Zumbalamambo closed 6 years ago

Zumbalamambo commented 7 years ago

Im using the following code to do the segmentation,

# Prepare image.
    img = tf.image.decode_jpeg(tf.read_file(args.img_path), channels=3)
    # Convert RGB to BGR.
    img_r, img_g, img_b = tf.split(axis=2, num_or_size_splits=3, value=img)
    img = tf.cast(tf.concat(axis=2, values=[img_b, img_g, img_r]), dtype=tf.float32)
    # Extract mean.
    img -= IMG_MEAN 

    # Create network.
    net = DeepLabResNetModel({'data': tf.expand_dims(img, dim=0)}, is_training=False, num_classes=args.num_classes)

    # Which variables to load.
    restore_var = tf.global_variables()

    # Predictions.
    raw_output = net.layers['fc1_voc12']
    raw_output_up = tf.image.resize_bilinear(raw_output, tf.shape(img)[0:2,])
    raw_output_up = tf.argmax(raw_output_up, dimension=3)
    pred = tf.expand_dims(raw_output_up, dim=3)

    # Set up TF session and initialize variables. 
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    sess = tf.Session(config=config)
    init = tf.global_variables_initializer()

    sess.run(init)

Over here, each and everytime when I change the image, I need to reinitialise the model. Is there by any means that I can initialise the model once and do prediction on the fly by varying images?

salehjg commented 7 years ago

I am not sure if this is what you want but have a look at inference_webcam.py : https://github.com/salehjg/tensorflow-deeplab-resnet/blob/master/inference_webcam.py

 while True:
        preds = sess.run(pred,feed_dict={img_input:frame})

        msk = decode_labels(preds, num_classes=args.num_classes)
        im = Image.fromarray(msk[0])

        open_cv_image = np.array(im)
        # Convert RGB to BGR
        open_cv_image = open_cv_image[:, :, ::-1].copy()

        cv2.imshow("SemanticSegmentation", open_cv_image)
        cv2.imshow("preview", frame)
        rval, frame = vc.read()

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
Zumbalamambo commented 7 years ago

@salehjg wow awesome... thank you so much... may I know how do I increase the shape of input because if I increase the input shape, it is crashing...

chichivica commented 7 years ago

@salehjg many thanks for working solution. @Zumbalamambo I made a workaround with

pic = pic.resize((IMAGE_WIDTH, IMAGE_HEIGTH), Image.BILINEAR)

or

img = tf.image.resize_bilinear(tf.expand_dims(img, dim=0), [IMAGE_HEIGTH, IMAGE_WIDTH])

Beware that resizing may cost you accuracy! e.g.: @DrSleep by the way) I would say great thanks for really awesome project!! Wish you good luck with your topic.

DrSleep commented 6 years ago

you can create a placeholder for the image, build the model and then feed a new image into the placeholder at each iteration as done in the training scripts.