Ejemplo n.º 1
0
    def predict(self, img):
        """ # Arguments
                img: a numpy array

            # Returns
                The url to an image with the segmentation
            """

        with self.graph.as_default():
            img = Image.fromarray(img)
            # RGB -> BGR
            b, g, r = img.split()
            img = Image.merge("RGB", (r, g, b))
            img -= self.IMG_MEAN

            # Predictions.
            raw_output = self.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, axis=3)
            self.pred = tf.expand_dims(raw_output_up, dim=3)

            preds = self.sess.run(self.pred, feed_dict={self.image_placeholder: np.expand_dims(img, axis=0)})

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

        filename = str(uuid.uuid4()) + '.jpg'
        save_dir = './outputs'
        if not os.path.exists(save_dir):
            os.makedirs(save_dir)
        save_path = os.path.join(save_dir, filename)
        im.save(save_path)

        return json.dumps({'output': filename})
Ejemplo n.º 2
0
    def _process(self, req):
        # type: (DeepLabRequest) -> None
        # img_path = os.path.join(self.manager.upload_path, req.prefix, req.input_file_name)
        # ret_path = os.path.join(self.manager.result_path, req.prefix, req.result_file_name)
        # Prepare image.
        # img_rgb = tf.image.decode_jpeg(tf.read_file(img_path), channels=3)
        jpg_tensor, jpg_data = self.manager.load_img(req.input_file_name)
        img_rgb = tf.image.decode_jpeg(jpg_tensor, channels=3)
        # Convert RGB to BGR.
        img_r, img_g, img_b = tf.split(img_rgb, 3, axis=2)
        img_bgr = tf.cast(tf.concat([img_b, img_g, img_r], 2),
                          dtype=tf.float32)
        # Extract mean.
        img_bgr -= self.manager.img_mean

        # Create network.
        net = DeepLabResNetModel({'data': tf.expand_dims(img_bgr, dim=0)},
                                 is_training=False)
        tf.get_variable_scope().reuse_variables()

        # 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_bgr)[0:2, ])

        # CRF.
        raw_output_up = tf.nn.softmax(raw_output_up)
        raw_output_up = tf.py_func(
            dense_crf,
            [raw_output_up, tf.expand_dims(img_rgb, dim=0)], tf.float32)
        raw_output_up = tf.argmax(raw_output_up, dimension=3)
        pred = tf.expand_dims(raw_output_up, dim=3)

        if not self.initialized:
            # Set up TF session and initialize variables.
            config = tf.ConfigProto(device_count={'GPU': self.gpu_id})
            config.gpu_options.allow_growth = True
            self.sess = tf.Session(config=config)
            init = tf.global_variables_initializer()

            self.sess.run(init)

            # Load weights.
            loader = tf.train.Saver(var_list=restore_var)
            loader.restore(self.sess, self.manager.weights_model_path)
            self.initialized = True

        # Perform inference.
        preds = self.sess.run(pred, feed_dict={jpg_tensor: jpg_data})

        msk = decode_labels(preds)
        im = Image.fromarray(msk[0])
        mask_path = req.result_file_name
        # os.makedirs(os.path.dirname(mask_path), exist_ok=True)
        # im.save(mask_path)
        self.manager.save_img(mask_path, im)
Ejemplo n.º 3
0
def main():
    """Create the model and start the evaluation process."""
    args = get_arguments()

    filename = os.path.basename(args.img_path).split('.')[0]
    # 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)

    # Load weights.
    loader = tf.train.Saver(var_list=restore_var)
    load(loader, sess, args.model_weights)

    # Perform inference.
    preds = sess.run(pred)

    msk = decode_labels(preds, num_classes=args.num_classes)
    im = Image.fromarray(msk[0])
    if not os.path.exists(args.save_dir):
        os.makedirs(args.save_dir)
    mask_path = args.save_dir + filename + '_mask.png'
    im.save(mask_path)

    apply_mask_all_classes(args.img_path, mask_path, args.save_dir)
    # im_png = Image.open(args.save_dir + filename + '_result.png')
    # im_png.convert('RGB').save(args.save_dir + filename + '_result.jpg',"JPEG")

    print('The output file has been saved to {}'.format(args.save_dir +
                                                        filename +
                                                        '_result.jpeg'))
Ejemplo n.º 4
0
def infer(img_path, save_dir=SAVE_DIR, model_weights=MODEL_WEIGHTS, num_classes=NUM_CLASSES):

    """Create the model and start the evaluation process."""

    # Prepare image.
    img = tf.image.decode_jpeg(tf.read_file(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=num_classes)


    # 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)

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

    # Load weights.
    loader = tf.train.Saver(var_list=restore_var)
    load(loader, sess, model_weights)

    # for path in img_path:
    #     print("img_path {}, save path {}".format(path, save_dir))
    #     pred = prepare_pred(path, num_classes)

    # Perform inference.
    preds = sess.run(pred)

    msk = decode_labels(preds, num_classes=num_classes)
    im = Image.fromarray(msk[0])
    if not os.path.exists(save_dir):
        os.makedirs(save_dir)
    im.save(save_dir + os.path.basename(img_path))
    print('The segmentation mask has been saved to {}'.format(save_dir + os.path.basename(img_path)))

    tf.reset_default_graph()
    sess.close()
Ejemplo n.º 5
0
def process_frame(frame):
    frame = cv2.resize(frame, (IMG_SIZE, IMG_SIZE))
    input_img_feed = np.array(frame, dtype=float)
    input_img_feed = np.expand_dims(input_img_feed, axis=0)

    start_time = time.time()
    preds = sess.run(pred, feed_dict={img_tf: input_img_feed})
    elapsed_time = time.time() - start_time
    print(("FPS: ", 1 / elapsed_time))
    msk = decode_labels(preds, num_classes=NUM_CLASSES)
    im = msk[0]
    final = cv2.addWeighted(im, 0.9, frame, 0.7, 0)
    return final
Ejemplo n.º 6
0
def process_frame(frame):
    frame = cv2.resize(frame, (IMG_SIZE, IMG_SIZE))
    input_img_feed = np.array(frame, dtype=float)
    input_img_feed = np.expand_dims(input_img_feed, axis=0)

    start_time = time.time()
    preds = sess.run(pred, feed_dict={img_tf: input_img_feed})
    elapsed_time = time.time() - start_time
    print(("FPS: ", 1 / elapsed_time))
    msk = decode_labels(preds, num_classes=NUM_CLASSES)
    im = msk[0]
    final = cv2.addWeighted(im,0.9,frame,0.7,0)
    return final
Ejemplo n.º 7
0
def main():
    """主函数:模型构建和evaluate."""
    args = get_arguments()

    # 读取图片.
    img = tf.image.decode_jpeg(tf.read_file(args.img_path), channels=3)
    # 通道转换: RGB --> 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)
    # 减去像素均值.
    img -= IMG_MEAN

    # 构建DeepLab-ResNet-101网络.
    net = DeepLabResNetModel({'data': tf.expand_dims(img, dim=0)},
                             is_training=False,
                             num_classes=args.num_classes)

    # 设定要预加载的网络权重参数
    restore_var = tf.global_variables()

    # 执行预测.
    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)

    # 建立tf session
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    sess = tf.Session(config=config)

    # 执行权重变量初始化
    init = tf.global_variables_initializer()
    sess.run(init)

    # 加载已有的checkpoint文件
    loader = tf.train.Saver(var_list=restore_var)
    load(loader, sess, args.model_weights)

    # 执行推断.
    preds = sess.run(pred)

    msk = decode_labels(preds, num_classes=args.num_classes)
    im = Image.fromarray(msk[0])
    if not os.path.exists(args.save_dir):
        os.makedirs(args.save_dir)
    im.save(args.save_dir + 'mask.png')

    print('The output file has been saved to {}'.format(args.save_dir +
                                                        'mask.png'))
def main():
    """Create the model and start the evaluation process."""
    args = get_arguments()

    # 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(value=img, num_or_size_splits=3, axis=2)
    img = tf.cast(tf.concat([img_b, img_g, img_r], axis=2), dtype=tf.float32)
    # Extract mean.
    img -= IMG_MEAN

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

    # 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

    graph = tf.get_default_graph()
    with tf.Session(graph=graph) as session:
        init = tf.global_variables_initializer()
        session.run(init)

        # Load weights.
        loader = tf.train.Saver(tf.global_variables())
        load(loader, session, args.model_weights)

        # Perform inference.
        preds = session.run(pred)

    msk = decode_labels(preds)
    im = Image.fromarray(msk[0])
    if not os.path.exists(args.save_dir):
        os.makedirs(args.save_dir)
    im.save(args.save_dir + 'mask.png')

    print('The output file has been saved to {}'.format(args.save_dir +
                                                        'mask.png'))
Ejemplo n.º 9
0
def main():
    """Create the model and start the evaluation process."""
    args = get_arguments()
    
    # Prepare image.
    my_img = mpimg.imread(args.img_path)
    input_data = tf.placeholder(tf.float32, shape=list(my_img.shape))
    # Convert RGB to BGR.
    img_r, img_g, img_b = tf.split(axis=2, num_or_size_splits=3, value=input_data)
    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)
    
    # Load weights.
    loader = tf.train.Saver(var_list=restore_var)
    load(loader, sess, args.model_weights)
    
    # Perform inference.
    preds = sess.run(pred, feed_dict={input_data: my_img})
    
    msk = decode_labels(preds, num_classes=args.num_classes)
    im = Image.fromarray(msk[0])
    if not os.path.exists(args.save_dir):
        os.makedirs(args.save_dir)
    im.save(args.save_dir + 'mask.png')
    
    print('The output file has been saved to {}'.format(args.save_dir + 'mask.png'))
Ejemplo n.º 10
0
def main():
    """Create the model and start the evaluation process."""
    args = get_arguments()
    
    # 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)
    
    # Load weights.
    loader = tf.train.Saver(var_list=restore_var)
    load(loader, sess, args.model_weights)
    
    # Perform inference.
    preds = sess.run(pred)
    
    msk = decode_labels(preds, num_classes=args.num_classes)
    im = Image.fromarray(msk[0])
    if not os.path.exists(args.save_dir):
        os.makedirs(args.save_dir)
    im.save(args.save_dir + 'mask.png')
    
    print('The output file has been saved to {}'.format(args.save_dir + 'mask.png'))
Ejemplo n.º 11
0
def main():
    """Create the model and start the evaluation process."""
    args = get_arguments()
    
    # 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(split_dim=2, num_split=3, value=img)
    img = tf.cast(tf.concat(2, [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)})

    # 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)

    # Which variables to load.
    trainable = tf.trainable_variables()
    
    # Set up TF session and initialize variables. 
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    sess = tf.Session(config=config)
    init = tf.initialize_all_variables()
    
    sess.run(init)
    
    # Load weights.
    saver = tf.train.Saver(var_list=trainable)
    load(saver, sess, args.model_weights)
    
    # Perform inference.
    preds = sess.run([pred])
    
    msk = decode_labels(np.array(preds)[0, 0, :, :, 0])
    im = Image.fromarray(msk)
    if not os.path.exists(args.save_dir):
        os.makedirs(args.save_dir)
    im.save(args.save_dir + 'mask.png')
    
    print('The output file has been saved to {}'.format(args.save_dir + 'mask.png'))
Ejemplo n.º 12
0
def infer_and_save_colormask(img_path, model_weights, use_crf):
    # Perform inference.
    preds = infer_absolute_path(img_path, model_weights, use_crf)
    # Have to add an extra dimension in the front because `infer_absolute_path` removed the batch dimension
    preds = np.expand_dims(preds, axis=0)

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

    # Save the image
    head, tail = os.path.split(img_path)
    filename, file_extension = os.path.splitext(tail)
    save_path = COLOR_MASK_SAVE_DIR + filename + '_colormask.png'
    if not os.path.exists(COLOR_MASK_SAVE_DIR):
        os.makedirs(COLOR_MASK_SAVE_DIR)
    im.save(save_path)

    print('The output file has been saved to {}'.format(save_path))
Ejemplo n.º 13
0
    def predict(self, img):
        """ # Arguments
                img: a numpy array

            # Returns
                The url to an image with the segmentation
            """

        with self.graph.as_default():
            img = Image.fromarray(img)
            # RGB -> BGR
            b, g, r = img.split()
            img = Image.merge("RGB", (r, g, b))
            img -= self.IMG_MEAN

            # Predictions.
            raw_output = self.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, axis=3)
            self.pred = tf.expand_dims(raw_output_up, dim=3)

            preds = self.sess.run(self.pred,
                                  feed_dict={
                                      self.image_placeholder:
                                      np.expand_dims(img, axis=0)
                                  })

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

        filename = str(uuid.uuid4()) + '.jpg'
        save_dir = './outputs'
        if not os.path.exists(save_dir):
            os.makedirs(save_dir)
        save_path = os.path.join(save_dir, filename)
        im.save(save_path)

        return json.dumps({'output': filename})
Ejemplo n.º 14
0
    for line in lines:
        img_path, seg_path = line.split('\t')
        img_path = os.path.join(args.data_dir, img_path)
        seg_path = os.path.join(args.data_dir, seg_path)

        if not os.path.exists(seg_path):
            print("skip non-existent: "+ seg_path)
            continue
        if not os.path.exists(img_path):
            print("skip non-existent: "+ img_path)
            continue

        seg = cv2.imread(seg_path, cv2.IMREAD_UNCHANGED)

        seg = np.expand_dims(np.expand_dims(seg, 0), -1)

        msk = decode_labels(seg, num_classes=np.max(seg) + 1)
        im = msk[0]
        img_o = cv2.imread(img_path)

        img_path = str(img_path)

        print(im.shape, im.dtype)
        print(img_o.shape, img_o.dtype)
        # img = np.array(im) * 0.9 + np.array(img_o) * 0.7
        img = np.hstack([im, img_o])
        img[img > 255] = 255

        cv2.imshow("labels", img.astype(np.uint8))
        cv2.waitKey(0)
Ejemplo n.º 15
0
def main():
    """Create the model and start the evaluation process."""
    args = get_arguments()
    print(args)
    os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
    os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu_mask

    with open(args.data_list) as f:
        list_of_filenames = [line.rstrip() for line in f]
        num_steps = len(list_of_filenames)

    # Create queue coordinator.
    coord = tf.train.Coordinator()

    # Load reader.
    with tf.name_scope("create_inputs"):
        reader = ImageReader(
            args.data_dir,
            args.data_list,
            (512, 512),  # No defined input size.
            False,  # No random scale.
            False,  # No random mirror.
            args.ignore_label,
            IMG_MEAN,
            coord,
            shuffle=False)
        image, label = reader.image, reader.label
    image_batch, label_batch = tf.expand_dims(image, dim=0), tf.expand_dims(label,
                                                                            dim=0)  # Add one batch dimension.

    # # 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': image_batch}, 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 = net.layers['concat_conv6']
    raw_output_up = tf.image.resize_area(raw_output, tf.shape(label_batch)[1:3, ])
    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.
    sess = tf.Session()
    init = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer())

    sess.run(init)

    threads = tf.train.start_queue_runners(coord=coord, sess=sess)

    # Load weights.
    loader = tf.train.Saver(var_list=restore_var)
    if '.ckpt' in args.model_weights:
        load(loader, sess, args.model_weights)
    else:
        load(loader, sess, tf.train.latest_checkpoint(args.model_weights))

    if not os.path.exists(args.save_dir):
        os.makedirs(args.save_dir)

    # Perform inference.
    for step in range(num_steps):
        preds = sess.run([pred])
        msk = decode_labels(preds[0], num_classes=args.num_classes)
        im = Image.fromarray(msk[0])
        im.save(args.save_dir + list_of_filenames[step].split("/")[-1] + '_mask.png')

        print('The output file has been saved to {}'.format(
            args.save_dir + list_of_filenames[step].split("/")[-1] + '_mask.png'))

    coord.request_stop()
    coord.join(threads)
Ejemplo n.º 16
0
def main():
    """Create the model and start the evaluation process."""
    args = get_arguments()
    num_steps = file_len(args.data_list)
    # Create queue coordinator.
    coord = tf.train.Coordinator()

    # Load reader.
    with tf.name_scope("create_inputs"):
        reader = ImageReader(
            args.img_path,
            args.data_list,
            None,  # No defined input size.
            False,  # No random scale.
            False,  # No random mirror.
            255,
            IMG_MEAN,
            coord)
        image, label = reader.image, reader.label
        title = reader.queue[0]
    image_batch, label_batch = tf.expand_dims(image, dim=0), tf.expand_dims(
        label, dim=0)  # Add one batch dimension.

    # Create network.
    net = DeepLabResNetModel({'data': image_batch},
                             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(image_batch)[1:3, ])
    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)

    # Load weights.
    loader = tf.train.Saver(var_list=restore_var)
    load(loader, sess, args.model_weights)

    # Start queue threads.
    threads = tf.train.start_queue_runners(coord=coord, sess=sess)

    start_time = time.time()
    if not os.path.exists(args.save_dir):
        os.makedirs(args.save_dir)
    # Perform inference.
    for step in range(num_steps):
        preds, jpg_path = sess.run([pred, title])
        msk = decode_labels(preds, num_classes=args.num_classes)
        im = Image.fromarray(msk[0])
        img_o = Image.open(jpg_path)
        jpg_path = jpg_path.decode()
        jpg_path = jpg_path.split('/')[-1].split('.')[0]
        img = np.array(im) * 0.9 + np.array(img_o) * 0.7
        img[img > 255] = 255
        img = Image.fromarray(np.uint8(img))
        img.save(args.save_dir + jpg_path + '.png')
        print('Image processed {}.png'.format(jpg_path))

    total_time = time.time() - start_time
    print('The output files have been saved to {}'.format(args.save_dir))
    print('It took {} sec on each image.'.format(total_time / num_steps))
Ejemplo n.º 17
0
def main():
    args, preds = get_arguments(), []

    # Create queue coordinator.
    coord = tf.train.Coordinator()

    # Load reader.
    with tf.name_scope("create_inputs"):
        reader = ImageReader(
            args.data_dir,
            args.target_list,
            None,  # No defined input size.
            False,  # No random scale.
            False,  # No random mirror.
            args.ignore_label,
            IMG_MEAN,
            coord)
        image_orig = reader.image

    for rots in range(4):
        image = tf.image.rot90(image_orig, k=rots)
        image_batch = tf.expand_dims(image, dim=0)  # Add one batch dimension.

        # Create network.
        net = DeepLabResNetModel({'data': image_batch},
                                 is_training=False,
                                 num_classes=args.num_classes)
        tf.get_variable_scope().reuse_variables()

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

        # Predictions.
        raw_output = net.layers['fc1_voc12']
        raw_output = tf.image.resize_bilinear(raw_output,
                                              tf.shape(image_batch)[1:3, ])

        # CRF.
        if args.crf:
            inv_image = tf.py_func(inv_preprocess, [image_batch, 1, IMG_MEAN],
                                   tf.uint8)
            raw_output = tf.py_func(dense_crf,
                                    [tf.nn.softmax(raw_output), inv_image],
                                    tf.float32)

        # Rotate to original
        raw_output = tf.image.rot90(tf.squeeze(raw_output), k=(4 - rots))
        raw_output = tf.expand_dims(raw_output, dim=0)
        preds.append(raw_output)

        if not args.augment:
            break

    pred = tf.reduce_mean(tf.concat(preds, axis=0), axis=0)

    if args.heatmap < 0:
        pred = tf.argmax(tf.expand_dims(pred, dim=0), dimension=3)
        pred = tf.cast(tf.expand_dims(pred, dim=3), tf.int32)
    else:
        pred = tf.expand_dims(pred[:, :, args.heatmap], dim=0)
        pred = tf.cast(pred, tf.int32)

    # 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)
    sess.run(tf.local_variables_initializer())

    # Load weights.
    loader = tf.train.Saver(var_list=restore_var)
    load(loader, sess, args.restore_from)

    # Start queue threads.
    threads = tf.train.start_queue_runners(coord=coord, sess=sess)

    if not os.path.exists(args.save_dir):
        os.makedirs(args.save_dir)

    # Iterate over training steps.
    for step in tqdm(range(args.num_steps)):
        preds, img_path = sess.run([pred, reader.queue[0]])

        if args.heatmap < 0:
            preds = decode_labels(preds, num_classes=args.num_classes)
            im = Image.fromarray(preds[0])
        else:
            pr = np.zeros((1, preds.shape[1], preds.shape[2], 3))
            preds += abs(np.min(preds))
            preds *= 255 / np.max(preds)
            pr[:, :, :, 0] = preds
            pr[:, :, :, 1] = preds
            pr[:, :, :, 2] = preds
            im = Image.fromarray(pr[0].astype('uint8'))

        img_name = os.path.split(img_path)[-1]
        im.save(os.path.join(args.save_dir + img_name))
    coord.request_stop()
    coord.join(threads)
Ejemplo n.º 18
0
def main():
    """Create the model and start the evaluation process."""
    args, rot_preds = get_arguments(), []

    # Prepare image.
    img_orig = tf.image.decode_png(tf.read_file(args.img_path), channels=3)

    for rots in range(4):
        # Convert RGB to BGR.
        img_r, img_g, img_b = tf.split(axis=2,
                                       num_or_size_splits=3,
                                       value=img_orig)
        img = tf.cast(tf.concat(axis=2, values=[img_b, img_g, img_r]),
                      dtype=tf.float32)
        # Extract mean.
        img -= IMG_MEAN

        img = tf.image.rot90(img, k=rots)

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

        # 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, ])

        # CRF.
        if args.crf:
            raw_output_up = tf.nn.softmax(raw_output_up)
            raw_output_up = tf.py_func(
                dense_crf,
                [raw_output_up, tf.expand_dims(img_orig, dim=0)], tf.float32)

        raw_output_up = tf.image.rot90(tf.squeeze(raw_output_up), k=(4 - rots))
        raw_output_up = tf.expand_dims(raw_output_up, dim=0)
        rot_preds.append(raw_output_up)

        if not args.augment:
            break

    pred = tf.reduce_mean(tf.concat(rot_preds, axis=0), axis=0)
    pred = tf.argmax(tf.expand_dims(pred, dim=0), dimension=3)
    pred = tf.cast(tf.expand_dims(pred, dim=3), tf.int32)

    # 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)

    # Load weights.
    loader = tf.train.Saver(var_list=restore_var)
    load(loader, sess, args.model_weights)

    # Perform inference.
    preds = sess.run(pred)

    msk = decode_labels(preds, num_classes=args.num_classes)
    im = Image.fromarray(msk[0])
    if not os.path.exists(args.save_dir):
        os.makedirs(args.save_dir)
    im.save(args.save_dir + 'mask.png')

    print('The output file has been saved to {}'.format(args.save_dir +
                                                        'mask.png'))
Ejemplo n.º 19
0
def deeplabProcessing(gpuId):
    """Create the model and start the evaluation process."""
    print("Starting worker on GPU " + str(gpuId) + "...")

    def printWorker(msg):
        print(str(timestampMs()) + " [gpu-worker-" + str(gpuId) + "] " + msg)

    printWorker("Waiting for segmentation requests...")
    initialized = False
    while (not quit):
        fileId = requestQueue.get()  # will block
        if fileId == "quit" + str(gpuId):
            printWorker("Received quit command")
            break

        printWorker("Received request for DL segmentaiton: " + fileId)
        printWorker("Requests queue size: " + str(requestQueue.qsize()))

        t1 = timestampMs()  #datetime.datetime.now()

        imgPath = os.path.join(uploadPath, fileId)
        # Prepare image.
        imgRGB = tf.image.decode_jpeg(tf.read_file(imgPath), channels=3)
        # Convert RGB to BGR.
        img_r, img_g, img_b = tf.split(imgRGB, 3, axis=2)
        imgBGR = tf.cast(tf.concat([img_b, img_g, img_r], 2), dtype=tf.float32)
        # Extract mean.
        imgBGR -= IMG_MEAN

        printWorker("Will create network")
        # Create network.
        net = DeepLabResNetModel({'data': tf.expand_dims(imgBGR, dim=0)},
                                 is_training=False)
        tf.get_variable_scope().reuse_variables()

        printWorker("Network created")

        # 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(imgBGR)[0:2, ])

        printWorker("Predictions")

        # CRF.
        raw_output_up = tf.nn.softmax(raw_output_up)
        raw_output_up = tf.py_func(
            dense_crf,
            [raw_output_up, tf.expand_dims(imgRGB, dim=0)], tf.float32)

        printWorker("CRF")

        raw_output_up = tf.argmax(raw_output_up, dimension=3)
        pred = tf.expand_dims(raw_output_up, dim=3)

        if not initialized:
            printWorker("Setup tf session")
            # Set up TF session and initialize variables.
            config = tf.ConfigProto(device_count={'GPU': gpuId})
            config.gpu_options.allow_growth = True
            sess = tf.Session(config=config)
            init = tf.global_variables_initializer()

            sess.run(init)
            printWorker("TF session initialized")

            # Load weights.
            loader = tf.train.Saver(var_list=restore_var)
            load(loader, sess, weightsModelPath)

            initialized = True

        # Perform inference.
        preds = sess.run(pred)

        msk = decode_labels(preds)
        im = Image.fromarray(msk[0])
        maskPath = os.path.join(resultsPath, fileId) + ".png"
        im.save(maskPath)

        originalFile = os.path.join(uploadPath, fileId)
        os.remove(originalFile)

        t2 = timestampMs()  #datetime.datetime.now()
        printWorker("Processing took " + str(t2 - t1) + "ms. Result is at " +
                    maskPath)
Ejemplo n.º 20
0
def main():
    """Create the model and start the evaluation process."""
    args = get_arguments()
    num_steps = file_len(args.data_list)
    # Create queue coordinator.
    coord = tf.train.Coordinator()
    
    # Load reader.
    with tf.name_scope("create_inputs"):
        reader = ImageReader(
            args.img_path,
            args.data_list,
            None, # No defined input size.
            False, # No random scale.
            False, # No random mirror.
            255,
            IMG_MEAN,
            coord)
        image, label = reader.image, reader.label
        title = reader.queue[0]
    image_batch, label_batch = tf.expand_dims(image, dim=0), tf.expand_dims(label, dim=0) # Add one batch dimension.
    
    # Create network.
    net = DeepLabResNetModel({'data': image_batch}, 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(image_batch)[1:3,])
    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)
    
    # Load weights.
    loader = tf.train.Saver(var_list=restore_var)
    load(loader, sess, args.model_weights)
    
    # Start queue threads.
    threads = tf.train.start_queue_runners(coord=coord, sess=sess)

    start_time = time.time()
    if not os.path.exists(args.save_dir):
      os.makedirs(args.save_dir)
    # Perform inference.
    for step in range(num_steps):
        preds, jpg_path = sess.run([pred, title])
        msk = decode_labels(preds, num_classes=args.num_classes)
        im = Image.fromarray(msk[0])
        img_o = Image.open(jpg_path)
        jpg_path = jpg_path.split('/')[-1].split('.')[0]
        img = np.array(im)*0.9 + np.array(img_o)*0.7
        img[img>255] = 255
        img = Image.fromarray(np.uint8(img))
        img.save(args.save_dir + jpg_path + '.png')
        print('Image processed {}.png'.format(jpg_path))
    
    total_time = time.time() - start_time
    print('The output files have been saved to {}'.format(args.save_dir))
    print('It took {} sec on each image.'.format(total_time/num_steps))
    # 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)
    sess.run(tf.local_variables_initializer())

    # Load weights.
    loader = tf.train.Saver(var_list=restore_var)
    if args.restore_from is not None:
        load(loader, sess, args.restore_from)

    # Start queue threads.
    threads = tf.train.start_queue_runners(coord=coord, sess=sess)

    # make output dir if
    if not os.path.exists(args.output_dir):
        os.mkdir(args.output_dir)

    # Iterate over training steps.
    for step in tqdm(range(len(reader.image_list))):
        preds = sess.run(pred)
        msk = decode_labels(preds, num_classes=args.num_classes)
        im = Image.fromarray(msk[0])
        im.save(os.path.join(args.output_dir, os.path.basename(reader.image_list[step])+'_mask.png'))
    coord.request_stop()
    coord.join(threads)
Ejemplo n.º 22
0
def predict(preds, scores, img_path, scale, num_classes, save_dir, heavy, t1):

    msk = decode_labels(preds, num_classes=num_classes)
    """
    
    

    print('The output file has been saved to {}'.format( save_dir + 'mask.png'))
    print(time.time() - t1)"""

    means, labels_means = filter_label(msk, scores, 0.98)

    my_msk = msk
    labels = []

    #Rule 1 : (skirt - shirt) vs (skirt - t-shirt) vs (dress)
    try:
        _sk = means[labels_means.index(18)]
    except ValueError:
        _sk = 0
    try:
        _sh = means[labels_means.index(15)]
    except ValueError:
        _sh = 0
    try:
        _tsh = means[labels_means.index(22)]
    except ValueError:
        _tsh = 0
    try:
        _dr = means[labels_means.index(7)]
    except ValueError:
        _dr = 0

    _out1 = np.mean([_sk, _sh])
    _out2 = np.mean([_sk, _tsh])
    my_max = np.argmax([_out1, _out2, _dr])
    if my_max == 0:
        change_label(my_msk, scores, [7, 22], 15, _sh)
    elif my_max == 1:
        change_label(my_msk, scores, [7, 15], 22, _tsh)
    else:
        change_label(my_msk, scores, [18, 22, 15], 7, _dr)

    #Rule 2 : (pants) vs (leggins) vs (shorts)
    try:
        _pa = means[labels_means.index(13)]
    except ValueError:
        _pa = 0
    try:
        _leg = means[labels_means.index(21)]
    except ValueError:
        _leg = 0
    try:
        _sh = means[labels_means.index(17)]
    except ValueError:
        _sh = 0
    my_max = np.argmax([_pa, _leg, _sh])

    if my_max == 0:
        change_label(my_msk, scores, [21, 17], 13, _pa)
    elif my_max == 1:
        change_label(my_msk, scores, [13, 17], 21, _leg)
    else:
        change_label(my_msk, scores, [13, 21], 17, _sh)

    #Rule 3 : (boots) vs (shoes) vs (socks)
    try:
        _bo = means[labels_means.index(5)]
    except ValueError:
        _bo = 0
    try:
        _sh = means[labels_means.index(16)]
    except ValueError:
        _sh = 0
    try:
        _so = means[labels_means.index(19)]
    except ValueError:
        _so = 0
    my_max = np.argmax([_bo, _sh, _so])

    if my_max == 0:
        change_label(my_msk, scores, [16, 19], 5, _bo)
    elif my_max == 1:
        change_label(my_msk, scores, [5, 19], 16, _sh)
    else:
        change_label(my_msk, scores, [5, 16], 19, _so)

    #Rule 4 : (cardigan) vs (blazer)
    try:
        _ca = means[labels_means.index(20)]
    except ValueError:
        _ca = 0
    try:
        _bl = means[labels_means.index(11)]
    except ValueError:
        _bl = 0

    my_max = np.argmax([_ca, _bl])
    if heavy:
        if my_max == 0:
            if _ca > 0:
                change_label(my_msk, scores, [11, 15, 22], 20, _ca)
        else:
            if _bl > 0:
                change_label(my_msk, scores, [20, 15, 22], 11, _bl)
    else:
        if my_max == 0:
            change_label(my_msk, scores, [11], 20, _ca)
        else:
            change_label(my_msk, scores, [20], 11, _bl)

    #Rule 5: Coat over the all
    if heavy:
        try:
            _coat = means[labels_means.index(6)]
        except ValueError:
            _coat = 0
        if _coat != 0:
            change_label(my_msk, scores, [20, 11, 15, 22], 6, _coat)

    # Filter POST edit
    means, labels_means = filter_label(my_msk, scores, 0.98)

    image = Image.fromarray(msk[0])

    if not os.path.exists(save_dir):
        os.makedirs(save_dir)
    image.save(save_dir + 'mask.png')

    # Create Dictionary of time
    data = {}
    data['time'] = str(time.time() - t1)

    # Create Dictionary of pic
    data['pic'] = img_path

    # Create Dictionary of labels used
    data['labels'] = []
    idx = 0
    for l in labels_means:
        label = {}
        label['label'] = code_colours[l]
        label['num'] = str(l)
        label['score'] = str(means[idx])
        data.get('labels').append(label)
        idx += 1

    # Create Dictionary of BoundingBox
    bboxes = extract_region(my_msk[0], labels_means)
    data['bounding_box'] = []
    for bbox in bboxes:
        bb = {}
        bb['min_x'] = int(bbox[0] * scale)
        bb['max_x'] = int(bbox[1] * scale)
        bb['min_y'] = int(bbox[2] * scale)
        bb['max_y'] = int(bbox[3] * scale)
        data.get('bounding_box').append(bb)

    bb_ins, rate = extract_inside_region(my_msk[0, :, :, 0], labels_means,
                                         scale)

    data['bounding_box_inside'] = []
    for bbox in bb_ins:
        bb = {}
        bb['min_x'] = bbox[0]
        bb['max_x'] = bbox[0] + 50
        bb['min_y'] = bbox[1]
        bb['max_y'] = bbox[1] + 50
        data.get('bounding_box_inside').append(bb)
    return image, data
Ejemplo n.º 23
0
def main():
    """Create the model and start the evaluation process."""
    args = get_arguments()

    os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
    os.environ["CUDA_VISIBLE_DEVICES"] = args.GPU

    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    sess = tf.Session(config=config)

    img_list = utils.generate_filelist_test(args.test_set)
    img_queue = tf.train.slice_input_producer([img_list], shuffle=False)
    img_content = tf.read_file(img_queue[0])

    # Prepare image.
    img = tf.image.decode_png(img_content, 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
    img = tf.reshape(img, [pic_height, pic_width, 3])

    image_test, image_name = tf.train.batch([img, img_queue[0]], 1)
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)

    # Create network.
    net = DeepLabResNetModel_50({'data': image_test},
                                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.

    init = tf.global_variables_initializer()

    sess.run(init)

    # Load weights.
    loader = tf.train.Saver(var_list=restore_var)
    load(loader, sess, args.model_weights)

    for time in range(test_batch_num):
        # Perform inference.

        preds, test_img_name = sess.run([pred, image_name])

        test_img_name = test_img_name[0].decode()
        test_img_name = test_img_name.split('/')[-1].strip('.png\n')
        print(test_img_name)

        msk = decode_labels(preds, num_classes=args.num_classes)

        im = Image.fromarray(msk[0])
        if not os.path.exists(args.save_dir):
            os.makedirs(args.save_dir)
        im.save(args.save_dir + test_img_name + '.png')

        print('The output file has been saved to {}'.format(args.save_dir +
                                                            'mask.png'))
    coord.request_stop()
    coord.join(threads)
Ejemplo n.º 24
0
def main():
    """Create the model and start the evaluation process."""
    args = get_arguments()
    num_steps = file_len(os.path.join(args.img_path, args.data_list))
    # Create queue coordinator.
    coord = tf.train.Coordinator()

    # Load reader.
    with tf.name_scope("create_inputs"):
        reader = ImageReader(
            os.path.join(args.img_path, "texture"),
            os.path.join(args.img_path, args.data_list),
            None,  # No defined input size.
            False,  # No random scale.
            False,  # No random mirror.
            255,
            IMG_MEAN,
            coord,
        )
        image, label = reader.image, reader.label
        title = reader.queue[0]
    image_batch, label_batch = (
        tf.expand_dims(image, axis=0),
        tf.expand_dims(label, axis=0),
    )  # Add one batch dimension.

    # Create network.
    net = DeepLabResNetModel(
        {"data": image_batch}, is_training=False, num_classes=args.num_classes
    )

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

    # Predictions.
    raw_output = net.layers["fc1_voc12"]
    before_argmax = tf.image.resize_bilinear(raw_output, tf.shape(image_batch)[1:3,])
    raw_output_up = tf.argmax(before_argmax, dimension=3)
    pred = tf.expand_dims(raw_output_up, axis=3)
    hw_only = pred[0, :, :, 0]
    class_0 = tf.where(tf.equal(hw_only, 0))
    class_1 = tf.where(tf.equal(hw_only, 1))
    class_2 = tf.where(tf.equal(hw_only, 2))
    class_3 = tf.where(tf.equal(hw_only, 3))
    class_4 = tf.where(tf.equal(hw_only, 4))
    class_5 = tf.where(tf.equal(hw_only, 5))
    class_6 = tf.where(tf.equal(hw_only, 6))

    # 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)

    # Load weights.
    loader = tf.train.Saver(var_list=restore_var)
    load(loader, sess, args.model_weights)

    # Start queue threads.
    threads = tf.train.start_queue_runners(coord=coord, sess=sess)

    start_time = time.time()
    os.makedirs(os.path.join(args.img_path, args.body_dir), exist_ok=True)
    os.makedirs(os.path.join(args.img_path, args.vis_dir), exist_ok=True)

    # write the header
    rois_file = os.path.join(args.img_path, "rois.csv")
    if os.path.isfile(rois_file):
        print(f"The rois file {rois_file} already exists...")
        ans = None
        while all(ans != choice for choice in ("a", "o", "q")):
            ans = input("Do you want to (a)ppend, (o)verwrite, or (q)uit? ")
        if ans == "o":
            print("Overwriting existing rois file...")
            write_header(rois_file)
        elif ans == "q":
            sys.exit(1)
    else:
        write_header(rois_file)

    # Perform inference.
    t = trange(num_steps, desc="Inference progress", unit="img")
    for step in t:
        # run through the model
        jpg_path, c0, c1, c2, c3, c4, c5, c6, raw_output_up_ = sess.run(
            [
                title,
                class_0,
                class_1,
                class_2,
                class_3,
                class_4,
                class_5,
                class_6,
                raw_output_up,
            ]
        )

        # == First, save the body segmentation ==
        if not args.no_body:
            # convert to a 2D compressed matrix, because we have a lot of 0's for the
            # background
            compressed = sparse.csr_matrix(np.squeeze(raw_output_up_))
            fname = os.path.splitext(os.path.basename(str(jpg_path)))[0]
            out = os.path.join(args.img_path, args.body_dir, fname)
            sparse.save_npz(out, compressed)

        # == Next, save the ROIs ==
        if not args.no_rois:
            img_id = extract_nums_only(fname)
            for c in (c0, c1, c2, c3, c4, c5, c6):
                try:
                    min_x = np.min(c[:, 1])
                except ValueError:
                    min_x = None
                try:
                    min_y = np.min(c[:, 0])
                except ValueError:
                    min_y = None
                try:
                    max_x = np.max(c[:, 1])
                except ValueError:
                    max_x = None
                try:
                    max_y = np.max(c[:, 0])
                except ValueError:
                    max_y = None
                # write out the stuff
                with open(rois_file, "a") as f:
                    f.write(
                        ",".join(
                            (img_id, str(min_x), str(min_y), str(max_x), str(max_y), "\n")
                        )
                    )

        # Save an image of the mask for our own reference every 1000 steps
        if not args.no_vis and step % args.visualize_step == 0:
            preds = np.expand_dims(raw_output_up_, axis=3)
            msk = decode_labels(preds, num_classes=args.num_classes)
            # the mask
            im = Image.fromarray(msk[0])
            # # Save the mask separately
            # jpg_path = str(jpg_path).split('/')[-1].split('.')[0]
            # out = os.path.join(args.vis_dir, jpg_path + '.png')
            # im.save(out)
            # Save the mask with background
            img_orig = Image.open(jpg_path)
            # create the final result using the mask and the original
            img = np.array(im) * 0.9 + np.array(img_orig) * 0.7
            # clip surpassed colors
            img[img > 255] = 255
            img = Image.fromarray(np.uint8(img))
            out = os.path.join(args.img_path, args.vis_dir, fname + ".png")
            img.save(out)
            # # print('Image processed {}.png'.format(jpg_path))
        t.set_description("Finished " + fname)

    total_time = time.time() - start_time
    print(f"The output files have been saved to {args.img_path}/{args.body_dir}")
    print(f"It took {total_time / num_steps} sec on each image.")
Ejemplo n.º 25
0
def main():
    """Create the model and start the evaluation process."""
    os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"  # see issue #152

    args = get_arguments()
    os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu_mask
    # Create queue coordinator.
    coord = tf.train.Coordinator()

    # Load reader.
    with tf.name_scope("create_inputs"):
        reader = ImageReader(
            args.data_dir,
            args.data_list,
            None,  # No defined input size.
            False,  # No random scale.
            False,  # No random mirror.
            args.ignore_label,
            IMG_MEAN,
            coord,
            shuffle=False)
        image, label = reader.image, reader.label
    image_batch, label_batch = tf.expand_dims(image, dim=0), tf.expand_dims(
        label, dim=0)  # Add one batch dimension.

    # Create network.
    net = DeepLabResNetModel({'data': image_batch},
                             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 = tf.image.resize_bilinear(raw_output,
                                          tf.shape(image_batch)[1:3, ])
    raw_output = tf.argmax(raw_output, dimension=3)
    pred = tf.expand_dims(raw_output, dim=3)  # Create 4-d tensor.
    # img-list = reader.images
    # mIoU
    # pred = tf.reshape(pred, [-1,])
    # gt = tf.reshape(label_batch, [-1,])
    # weights = tf.cast(tf.less_equal(gt, args.num_classes - 1), tf.int32) # Ignoring all labels greater than or equal to n_classes.
    # mIoU, update_op = tf.contrib.metrics.streaming_mean_iou(pred, gt, num_classes=args.num_classes, weights=weights)
    #
    # 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)
    sess.run(tf.local_variables_initializer())

    # Load weights.
    loader = tf.train.Saver(var_list=restore_var)
    if args.restore_from is not None:
        load(loader, sess, args.restore_from)

    # Start queue threads.
    threads = tf.train.start_queue_runners(coord=coord, sess=sess)

    image_fns = reader.images.eval(session=sess)
    for image_fn in image_fns:
        start_time = time.time()

        # print (image_fn)

        # Iterate over training steps.
        # for step in range(args.num_steps):
        # Perform inference.
        preds = sess.run(pred)

        msk = decode_labels(preds, num_classes=args.num_classes)
        im = Image.fromarray(msk[0])
        if not os.path.exists(args.save_dir):
            os.makedirs(args.save_dir)
        # print (args.save_dir + os.path.split(image_fn)[1].replace('jpg','png'))
        im.save(args.save_dir +
                os.path.split(image_fn)[1].replace('jpg', 'png'))

        duration = time.time() - start_time
        print('duration per pred  ({:.3f} sec/step)'.format(duration))
    print('The output file has been saved to {}'.format(args.save_dir))

    coord.request_stop()
    coord.join(threads)
Ejemplo n.º 26
0
def main():
    """Create the model and start the evaluation process."""
    args = get_arguments()

    # Create queue coordinator.
    coord = tf.train.Coordinator()

    # Load validation

    # Load reader.
    with tf.name_scope("create_inputs"):
        reader = ImageReader(
            args.data_dir,
            args.data_list,
            None,  # No defined input size.
            False,  # No random scale.
            False,  # No random mirror.
            args.ignore_label,
            IMG_MEAN,
            coord)
        image, label, file, mask = reader.image, reader.label, reader.image_list, reader.label_list
    image_batch, label_batch, file_batch, mask_batch = tf.expand_dims(image, dim=0), \
                                                       tf.expand_dims(label, dim=0), \
                                                       tf.expand_dims(file, dim=0), \
                                                       tf.expand_dims(mask, dim=0)# Add one batch dimension.

    # Create network.
    net = DeepLabResNetModel({'data': image_batch},
                             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 = tf.image.resize_bilinear(raw_output,
                                          tf.shape(image_batch)[1:3, ])
    probabilities = tf.nn.softmax(raw_output)

    raw_output = tf.argmax(raw_output, dimension=3)
    preds = tf.expand_dims(raw_output, dim=3)  # Create 4-d tensor.

    # mIoU
    # pred = tf.reshape(preds, [-1,])
    # gt = tf.reshape(label_batch, [-1,])
    # weights = tf.cast(tf.less_equal(gt, args.num_classes - 1), tf.int32) # Ignoring all labels greater than or equal to n_classes.
    # mIoU, update_op = tf.contrib.metrics.streaming_mean_iou(pred, gt, num_classes=args.num_classes, weights=weights)
    file_name = file_batch
    mask_link = mask_batch
    # 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)
    sess.run(tf.local_variables_initializer())

    # Load weights.
    loader = tf.train.Saver(var_list=restore_var)
    if args.restore_from is not None:
        load(loader, sess, args.restore_from)

    # Start queue threads.
    threads = tf.train.start_queue_runners(coord=coord, sess=sess)

    # Iterate over training steps.
    result_data = {}

    total_miuo_score = 0
    nb = 0
    global OUT_DIR
    global IMAGE
    global IMAGE_PATH

    for step in range(args.num_steps):
        predict, probmap, fb, mk = sess.run(
            [preds, probabilities, file_name, mask_link])
        print('step {:d}'.format(step))

        if IS_POD:
            IMAGE = fb[0][step].decode('utf8').replace(
                "D:\Data\POD/JpegImages/", "").replace(".jpg", "")
            IMAGE_PATH = "D:\Data\POD\JpegImages\\" + IMAGE + ".jpg"

            labels, bounding_boxs, confidence_score, miuo_score, pixel_score = \
                get_bounding_boxs(probmap)

            result_data[IMAGE] = {
                "labels": labels,
                "confidence_score": confidence_score,
                "boxes": bounding_boxs,
                "pixel_accuracy": pixel_score,
                "mIoU": miuo_score
            }

            # Draw bouding boxes to image
            # drw_img(cv2.imread(IMAGE_PATH), labels, bounding_boxs)

            OUT_DIR = "mask_pod/"
        else:
            # print(mk[0][step].decode('utf8'))
            IMAGE = mk[0][step].decode('utf8').replace(
                "D:\Data\PLAD/MaskImage/", "")
            result_data[IMAGE] = probabilities_map_evaluate(
                probmap, mk[0][step].decode('utf8'))
            OUT_DIR = "mask_plad/"

        print(result_data)

        # Draw mask image
        msk = decode_labels(predict, num_classes=args.num_classes)
        im = Image.fromarray(msk[0])
        im.save(OUT_DIR + IMAGE + '.png')

        # with open('predicted_boxes.json', 'w') as outfile:
        #     json.dump(result_data, outfile)

    # print('Mean IoU: {:.3f}'.format(mIoU.eval(session=sess)))
    coord.request_stop()
    coord.join(threads)
Ejemplo n.º 27
0
def main():
    """Create the model and start the training."""
    args = get_arguments()

    h, w = map(int, args.input_size.split(','))
    input_size = (h, w)

    # Create queue coordinator.
    coord = tf.train.Coordinator()

    # Load reader.
    with tf.name_scope("create_inputs"):
        reader = ImageReader(args.data_dir, args.data_list, input_size,
                             RANDOM_SCALE, coord)
        image_batch, label_batch = reader.dequeue(args.batch_size)

    # Create network.
    net = DeepLabResNetModel({'data': image_batch})

    # Predictions.
    raw_output = net.layers['fc1_voc12']

    prediction = tf.reshape(raw_output, [-1, n_classes])
    label_proc = prepare_label(label_batch,
                               tf.pack(raw_output.get_shape()[1:3]))
    gt = tf.reshape(label_proc, [-1, n_classes])

    # Pixel-wise softmax loss.
    loss = tf.nn.softmax_cross_entropy_with_logits(prediction, gt)
    reduced_loss = tf.reduce_mean(loss)

    # Processed predictions.
    raw_output_up = tf.image.resize_bilinear(raw_output,
                                             tf.shape(image_batch)[1:3, ])
    raw_output_up = tf.argmax(raw_output_up, dimension=3)
    pred = tf.expand_dims(raw_output_up, dim=3)

    # Define loss and optimisation parameters.
    optimiser = tf.train.AdamOptimizer(learning_rate=args.learning_rate)
    trainable = tf.trainable_variables()
    optim = optimiser.minimize(reduced_loss, var_list=trainable)

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

    sess.run(init)

    # Saver for storing checkpoints of the model.
    saver = tf.train.Saver(var_list=trainable, max_to_keep=40)
    if args.restore_from is not None:
        load(saver, sess, args.restore_from)

    # Start queue threads.
    threads = tf.train.start_queue_runners(coord=coord, sess=sess)

    if not os.path.exists(args.save_dir):
        os.makedirs(args.save_dir)

    # Iterate over training steps.
    for step in range(args.num_steps):
        start_time = time.time()

        if step % args.save_pred_every == 0:
            loss_value, images, labels, preds, _ = sess.run(
                [reduced_loss, image_batch, label_batch, pred, optim])
            fig, axes = plt.subplots(args.save_num_images, 3, figsize=(16, 12))
            for i in xrange(args.save_num_images):
                axes.flat[i * 3].set_title('data')
                axes.flat[i * 3].imshow(
                    (images[i] + IMG_MEAN)[:, :, ::-1].astype(np.uint8))

                axes.flat[i * 3 + 1].set_title('mask')
                axes.flat[i * 3 + 1].imshow(decode_labels(labels[i, :, :, 0]))

                axes.flat[i * 3 + 2].set_title('pred')
                axes.flat[i * 3 + 2].imshow(decode_labels(preds[i, :, :, 0]))
            plt.savefig(args.save_dir + str(start_time) + ".png")
            plt.close(fig)
            save(saver, sess, args.snapshot_dir, step)
        else:
            loss_value, _ = sess.run([reduced_loss, optim])
        duration = time.time() - start_time
        print('step {:d} \t loss = {:.3f}, ({:.3f} sec/step)'.format(
            step, loss_value, duration))
    coord.request_stop()
    coord.join(threads)
Ejemplo n.º 28
0
def main(argv=None):
    # 1.获取测试集数据
    # map()会根据提供的函数对指定序列做映射。FLAGS.input_size.split(',')得到['321','321']
    image_height, image_width = map(int, FLAGS.input_size.split(','))
    input_size = (image_height, image_width)

    # 创建一个线程管理器
    coord = tf.train.Coordinator()

    with tf.name_scope("create_inputs"):
        reader = ImageReader(FLAGS.data_dir,
                             FLAGS.data_list,
                             input_size=None,
                             random_scale=False,
                             coord=coord)
        image, label = reader.image, reader.label
        image_batch, label_batch = tf.expand_dims(image,
                                                  dim=0), tf.expand_dims(label,
                                                                         dim=0)

    # 2.建立模型
    net = DeepLabV3Model(is_training=False, num_classes=FLAGS.num_classes)

    # 3.预测结果
    raw_output_up = net.preds(image_batch)
    #
    pred = tf.expand_dims(raw_output_up, dim=3)

    # 4.定义一个初始化变量op
    init_variable = tf.global_variables_initializer()

    with tf.Session() as sess:
        sess.run(init_variable)

        # 5.
        restore_var = tf.global_variables()
        loader = tf.train.Saver(var_list=restore_var)
        loader.restore(sess, FLAGS.model_weights)
        print("Restored model parameters from {}".format(FLAGS.model_weights))

        # 6.
        threads = tf.train.start_queue_runners(coord=coord, sess=sess)

        # 7.mIoU
        predictions = tf.reshape(pred, [
            -1,
        ])
        labels = tf.reshape(tf.cast(label_batch, tf.int32), [
            -1,
        ])

        mask = labels <= FLAGS.num_classes - 1

        predictions = tf.boolean_mask(predictions, mask)
        labels = tf.boolean_mask(labels, mask)

        # Define the evaluation metric.
        mIoU, update_op = tf.contrib.metrics.streaming_mean_iou(
            predictions, labels, FLAGS.num_classes)
        # 一定要有
        sess.run(tf.local_variables_initializer())
        # ###

        # 8.Iterate over images.
        for step in range(FLAGS.num_steps):
            # Perform inference.
            preds, img, _ = sess.run(
                [pred, tf.cast(image_batch, tf.uint8), update_op])

            msk = decode_labels(preds, num_classes=FLAGS.num_classes)

            im0 = Image.fromarray(msk[0])

            im0.save(FLAGS.save_image_dir + 'mask' + str(step) + '.png')

            print('The output file has been saved to {}'.format(
                FLAGS.save_image_dir + 'mask' + str(step) + '.png'))

            # CRF
            raw_im = dense_crf(img[0], msk[0])
            #
            # plt.subplot(1,2,1)
            # plt.imshow(msk[0])
            # plt.subplot(1, 2, 2)
            # plt.imshow(raw_im)
            # plt.show()

            im = Image.fromarray(raw_im)

            im.save(FLAGS.save_image_dir + 'mask_crf' + str(step) + '.png')

            print('The output file has been saved to {}'.format(
                FLAGS.save_image_dir + 'mask_crf' + str(step) + '.png'))

        print('Mean IoU: {:.3f}'.format(mIoU.eval(session=sess)))

        coord.request_stop()
        coord.join(threads)

    return None
def main():
    """Create the model and start the evaluation process."""
    args = get_arguments()

    # Create queue coordinator.
    coord = tf.train.Coordinator()

    # Load reader.
    with tf.name_scope("create_inputs"):
        reader = ImageReader(
            args.data_dir,
            args.data_list,
            None,  # No defined input size.
            False,  # No random scale.
            False,  # No random mirror.
            args.ignore_label,
            IMG_MEAN,
            coord)
        image, label = reader.image, reader.label
    # Add one batch dimension.
    image_batch, label_batch = tf.expand_dims(image,
                                              dim=0), tf.expand_dims(label,
                                                                     dim=0)

    # Create network.
    net = DeepLabResNetModel({'data': image_batch},
                             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 = tf.image.resize_bilinear(raw_output,
                                          tf.shape(image_batch)[1:3, ])
    raw_output = tf.argmax(raw_output, dimension=3)
    pred = tf.expand_dims(raw_output, dim=3)  # Create 4-d tensor.

    # mIoU
    pred = tf.reshape(pred, [
        -1,
    ])
    gt = tf.reshape(label_batch, [
        -1,
    ])
    # Ignoring all labels greater than or equal to n_classes.
    weights = tf.cast(tf.less_equal(gt, args.num_classes - 1), tf.int32)
    mIoU, update_op = tf.contrib.metrics.streaming_mean_iou(
        pred, gt, num_classes=args.num_classes, weights=weights)

    # 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)
    sess.run(tf.local_variables_initializer())

    # Load weights.
    loader = tf.train.Saver(var_list=restore_var)
    if args.restore_from is not None:
        load(loader, sess, args.restore_from)

    # Start queue threads.
    threads = tf.train.start_queue_runners(coord=coord, sess=sess)

    # Iterate over training steps.
    for step in range(args.num_steps):
        preds, _ = sess.run([pred, update_op])

        if args.save_dir is not None:
            img = decode_labels(preds[0, :, :, 0])
            im = Image.fromarray(img)
            im.save(args.save_dir + str(step) + '_vis.png')
            cv2.imwrite(args.save_dir + str(step) + '.png', preds[0, :, :, 0])

        if step % 100 == 0:
            print('step {:d}'.format(step))
    print('Mean IoU: {:.3f}'.format(mIoU.eval(session=sess)))
    coord.request_stop()
    coord.join(threads)
Ejemplo n.º 30
0
def main():
    """Create the model and start the evaluation process."""
    args = get_arguments()

    # Create queue coordinator.
    coord = tf.train.Coordinator()

    # Load reader.
    with tf.name_scope("create_inputs"):
        reader = ImageReader_MultiClass_Loss(
            args.data_dir,
            args.data_list,
            None,  # No defined input size.
            RANDOM_SEED,
            False,  # No random scale.
            False,  # No random mirror.
            coord)
        image, l2_catg, binary_catg, hinge_catg = reader.image, reader.l2_catg, reader.binary_catg, reader.hinge_catg
    image_batch = tf.expand_dims(image, dim=0)
    binary_catg_batch = tf.expand_dims(binary_catg, dim=0)

    # Create network.
    net = DeepLabResNetModel({'data': image_batch}, is_training=False)

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

    # Predictions.
    raw_output = net.layers['fc1_voc12']

    # Do the global average pooling
    raw_output_bcgd_rmvd = raw_output[:, :, :, 1:]
    g_avg_pool = tf.reduce_mean(tf.reduce_mean(raw_output_bcgd_rmvd, axis=1, keep_dims=True),\
                           axis=2, keep_dims=True) # Avg across the width and height dimension -> [Bx21]
    g_avg_pool_sqzd = tf.squeeze(g_avg_pool, axis=[1, 2])
    pred = tf.nn.softmax(g_avg_pool_sqzd)

    # Get the class activation map
    raw_output_up = tf.image.resize_bilinear(raw_output_bcgd_rmvd,
                                             tf.shape(image_batch)[1:3, ])
    raw_output_up = raw_output_up - tf.reduce_min(tf.reduce_min(
        raw_output_up, axis=1, keep_dims=True),
                                                  axis=2,
                                                  keep_dims=True) + EPSILON
    raw_output_up = raw_output_up / tf.reduce_max(tf.reduce_max(
        raw_output_up, axis=1, keep_dims=True),
                                                  axis=2,
                                                  keep_dims=True)
    cam_m_1 = tf.argmax(raw_output_up, dimension=3) + 1
    raw_output_catgs_rmvd = raw_output_up * tf.expand_dims(
        tf.expand_dims(binary_catg_batch, 1), 2)
    cam_m_2 = tf.argmax(raw_output_catgs_rmvd, dimension=3) + 1
    cam = tf.cast(tf.equal(cam_m_1, cam_m_2), tf.int64) * cam_m_1

    cam_batch = tf.expand_dims(cam, 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)
    sess.run(tf.local_variables_initializer())

    # Load weights.
    loader = tf.train.Saver(var_list=restore_var)
    if args.restore_from is not None:
        load(loader, sess, args.restore_from)

    # Start queue threads.
    threads = tf.train.start_queue_runners(coord=coord, sess=sess)

    # Iterate over training steps.
    for step in range(args.num_steps):
        preds, images, cams, bin_catg = sess.run(
            [pred, image_batch, cam_batch, binary_catg])
        """
        print(bin_catg)
        print(np.unique(np.unique(cams)))
        """
        img = inv_preprocess(images)
        attMap = decode_labels(cams)
        output_dir = './output_maps_binary_without_norm/'
        img_name = output_dir + str(step) + '.jpg'
        map_name = output_dir + str(step) + '.png'
        misc.imsave(img_name, img[0, :, :, :])
        misc.imsave(map_name, attMap[0, :, :, :])
    coord.request_stop()
    coord.join(threads)
def main():
    """Create the model and start the evaluation process."""
    args = get_arguments()

    # 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_squeeze = tf.squeeze(raw_output_up, axis=0)
    raw_output_up_squeeze = tf.nn.softmax(raw_output_up_squeeze, )
    # raw_output_up = tf.argmax(raw_output_up, dimension=3)
    # print(raw_output_up.get_shape()) #(1, ?, ?)
    # pred = tf.expand_dims(raw_output_up, dim=3)
    # print(pred.get_shape()) #(1, ?, ?, 1)
    # 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)

    # Load weights.
    loader = tf.train.Saver(var_list=restore_var)
    load(loader, sess, args.model_weights)

    # Perform inference.
    start = time.time()
    processed_probabilities = sess.run(raw_output_up_squeeze)
    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)
    img = sess.run(img)

    #---------------------------------CRF
    import sys

    import pydensecrf.densecrf as dcrf

    from pydensecrf.utils import compute_unary, create_pairwise_bilateral, \
        create_pairwise_gaussian, softmax_to_unary

    import skimage.io as io

    softmax = processed_probabilities.transpose((2, 0, 1))

    # The input should be the negative of the logarithm of probability values
    # Look up the definition of the softmax_to_unary for more information
    unary = softmax_to_unary(softmax)
    # The inputs should be C-continious -- we are using Cython wrapper
    unary = np.ascontiguousarray(unary)  #(21,n)
    d = dcrf.DenseCRF(img.shape[0] * img.shape[1], 21)

    d.setUnaryEnergy(unary)

    # This potential penalizes small pieces of segmentation that are
    # spatially isolated -- enforces more spatially consistent segmentations
    feats = create_pairwise_gaussian(sdims=(10, 10), shape=img.shape[:2])

    d.addPairwiseEnergy(feats,
                        compat=3,
                        kernel=dcrf.DIAG_KERNEL,
                        normalization=dcrf.NORMALIZE_SYMMETRIC)

    # This creates the color-dependent features --
    # because the segmentation that we get from CNN are too coarse
    # and we can use local color features to refine them
    feats = create_pairwise_bilateral(sdims=(50, 50),
                                      schan=(20, 20, 20),
                                      img=img,
                                      chdim=2)

    d.addPairwiseEnergy(feats,
                        compat=10,
                        kernel=dcrf.DIAG_KERNEL,
                        normalization=dcrf.NORMALIZE_SYMMETRIC)
    #迭代次数,对于IMG_1702(2592*1456)这张图,迭代5 16.807087183s 迭代20 37.5700438023s
    Q = d.inference(5)
    res = np.argmax(Q, axis=0).reshape((img.shape[0], img.shape[1]))

    #-----------------------------------

    # res = tf.expand_dims(res, dim=3)
    res = res[np.newaxis, :, :, np.newaxis]
    msk = decode_labels(res, num_classes=args.num_classes)
    im = Image.fromarray(msk[0])
    if not os.path.exists(args.save_dir):
        os.makedirs(args.save_dir)
    im.save(args.save_dir + '16_crf.png')
    end = time.time()
    print('{}'.format(end - start))
Ejemplo n.º 32
0
def infer_and_save_matlab_for_filelist(filelist=FILELIST,
                                       also_save_colormask=False,
                                       color_mask_save_dir=COLOR_MASK_SAVE_DIR,
                                       matlab_save_dir=MATLAB_SAVE_DIR,
                                       model_weights=MODEL_WEIGHTS,
                                       use_crf=False):
    # Open and parse the filelist
    text_file = open(filelist, "r")
    lines = text_file.read().splitlines()
    text_file.close()

    # Perform inference.
    print('Starting inference')
    preds = infer_multiple_images(filelist, model_weights, use_crf)

    # Get the color values for the labels if the colormask is also saved
    if also_save_colormask == True:
        print('Decoding the labels')
        masks = decode_labels(preds,
                              num_images=preds.shape[0],
                              num_classes=NUM_CLASSES)

    # Prepare the location for saving the colormask
    if use_crf == True:
        crf_used_string = 'with_crf'
    else:
        crf_used_string = ''

    if not os.path.exists(color_mask_save_dir):
        os.makedirs(color_mask_save_dir)
    if not os.path.exists(matlab_save_dir):
        os.makedirs(matlab_save_dir)

    for index, img_name in enumerate(lines):
        # Save the colormask as .png
        if also_save_colormask == True:
            im = Image.fromarray(masks[index])
            # os.path.splitext(img_name)[0] removes the '.png' extension from the image name
            colormask_save_path = color_mask_save_dir + os.path.splitext(
                img_name)[0] + crf_used_string + '_colormask.png'
            im.save(colormask_save_path)
            print('The colormask has been saved to {}'.format(
                colormask_save_path))

        # Save the mask as MATLAB .mat
        # os.path.splitext(img_name)[0] removes the '.png' extension from the image name
        matlab_labels_save_path = matlab_save_dir + os.path.splitext(
            img_name)[0] + '.mat'
        if use_crf == False:
            scipy.io.savemat(matlab_labels_save_path,
                             mdict={'labels': preds[index].astype(np.uint16)})
            print('The matlab file has been saved to {}'.format(
                matlab_labels_save_path))
            continue
        # TODO Save the inference correctly in the case that CRF is used
        # preds_crf = infer_absolute_path(img_absolute_path, model_weights, use_crf=True)
        # scipy.io.savemat(labels_absolute_path,
        #                  mdict={'labels': preds.astype(np.uint16), 'labels_crf': preds_crf.astype(np.uint16)})
        # print('The output file has been saved to {}'.format(labels_absolute_path))

    return
def main():
    """Create the model and start the evaluation process."""
    args = get_arguments()

    # Create queue coordinator.
    coord = tf.train.Coordinator()

    # Load reader.
    with tf.name_scope("create_inputs"):
        reader = ImageReader(
            args.data_dir,
            args.data_list,
            None,  # No defined input size.
            False,  # No random scale.
            False,  # No random mirror.
            args.ignore_label,
            IMG_MEAN,
            coord)
        image, label = reader.image, reader.label
    image_batch, label_batch = tf.expand_dims(image, dim=0), tf.expand_dims(
        label, dim=0)  # Add one batch dimension.

    # Create network.
    net = DeepLabResNetModel({'data': image_batch},
                             is_training=False,
                             num_classes=args.num_classes)

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

    # Predictions.
    raw_output = net.layers['fc_out']
    raw_output = tf.image.resize_bilinear(raw_output,
                                          tf.shape(image_batch)[1:3, ])
    raw_output = tf.argmax(raw_output, dimension=3)
    pred = tf.expand_dims(raw_output, dim=3)  # Create 4-d tensor.

    # mIoU

    pred_flatten = tf.reshape(pred, [
        -1,
    ])
    gt = tf.reshape(label_batch, [
        -1,
    ])
    weights = tf.cast(
        tf.less_equal(gt, args.num_classes - 1),
        tf.int32)  # Ignoring all labels greater than or equal to n_classes.
    mIoU, update_op = tf.contrib.metrics.streaming_mean_iou(
        predictions=pred_flatten,
        labels=gt,
        num_classes=args.num_classes,
        weights=weights)

    # 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)
    sess.run(tf.local_variables_initializer())

    # Load weights.
    loader = tf.train.Saver(var_list=restore_var)

    ckpt = tf.train.get_checkpoint_state(SNAPSHOT_DIR)

    if ckpt and ckpt.model_checkpoint_path:
        loader = tf.train.Saver(var_list=restore_var)
        load_step = int(
            os.path.basename(ckpt.model_checkpoint_path).split('-')[1])
        load(loader, sess, ckpt.model_checkpoint_path)
    else:
        print('No checkpoint file found.')
        load_step = 0

    # Start queue threads.
    threads = tf.train.start_queue_runners(coord=coord, sess=sess)

    if not os.path.exists(SAVE_DIR):
        os.makedirs(SAVE_DIR)

    for step in range(args.num_steps):
        preds, _ = sess.run([pred, update_op])

        if IS_SAVE == True:
            msk = decode_labels(preds, num_classes=args.num_classes)
            im = Image.fromarray(msk[0])
            filename = 'mask' + str(step) + '.png'
            im.save(SAVE_DIR + filename)

        if step % 10 == 0:
            print('step {0} mIoU: {1}'.format(step, mIoU.eval(session=sess)))

    coord.request_stop()
    coord.join(threads)
Ejemplo n.º 34
0
def main():
    """Create the model and start the evaluation process."""
    args = get_arguments()
    num_steps = file_len(os.path.join(args.img_path, args.data_list))
    # Create queue coordinator.
    coord = tf.train.Coordinator()

    # Load reader.
    with tf.name_scope("create_inputs"):
        reader = ImageReader(
            os.path.join(args.img_path, "texture"),
            os.path.join(args.img_path, args.data_list),
            None,  # No defined input size.
            False,  # No random scale.
            False,  # No random mirror.
            255,
            IMG_MEAN,
            coord,
        )
        image, label = reader.image, reader.label
        title = reader.queue[0]
    image_batch, label_batch = (
        tf.expand_dims(image, axis=0),
        tf.expand_dims(label, axis=0),
    )  # Add one batch dimension.

    # Create network.
    net = DeepLabResNetModel({"data": image_batch},
                             is_training=False,
                             num_classes=args.num_classes)

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

    # Predictions.
    raw_output = net.layers["fc1_voc12"]
    before_argmax = tf.image.resize_bilinear(raw_output,
                                             tf.shape(image_batch)[1:3, ])
    raw_output_up = tf.argmax(before_argmax, dimension=3)
    pred = tf.expand_dims(raw_output_up, axis=3)
    hw_only = pred[0, :, :, 0]
    class_0 = tf.where(tf.equal(hw_only, 0))
    class_1 = tf.where(tf.equal(hw_only, 1))
    class_2 = tf.where(tf.equal(hw_only, 2))
    class_3 = tf.where(tf.equal(hw_only, 3))
    class_4 = tf.where(tf.equal(hw_only, 4))
    class_5 = tf.where(tf.equal(hw_only, 5))
    class_6 = tf.where(tf.equal(hw_only, 6))

    # 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)

    # Load weights.
    loader = tf.train.Saver(var_list=restore_var)
    load(loader, sess, args.model_weights)

    # Start queue threads.
    threads = tf.train.start_queue_runners(coord=coord, sess=sess)

    start_time = time.time()
    os.makedirs(os.path.join(args.img_path, args.body_dir), exist_ok=True)
    os.makedirs(os.path.join(args.img_path, args.vis_dir), exist_ok=True)

    # write the header
    rois_file = os.path.join(args.img_path, "rois.csv")
    if os.path.isfile(rois_file):
        print(f"The rois file {rois_file} already exists...")
        ans = None
        while all(ans != choice for choice in ("a", "o", "q")):
            ans = input("Do you want to (a)ppend, (o)verwrite, or (q)uit? ")
        if ans == "o":
            print("Overwriting existing rois file...")
            write_header(rois_file)
        elif ans == "q":
            sys.exit(1)
    else:
        write_header(rois_file)

    # Perform inference.
    t = trange(num_steps, desc="Inference progress", unit="img")
    for step in t:
        # run through the model
        jpg_path, c0, c1, c2, c3, c4, c5, c6, raw_output_up_ = sess.run([
            title,
            class_0,
            class_1,
            class_2,
            class_3,
            class_4,
            class_5,
            class_6,
            raw_output_up,
        ])

        # == First, save the body segmentation ==
        if not args.no_body:
            # convert to a 2D compressed matrix, because we have a lot of 0's for the
            # background
            compressed = sparse.csr_matrix(np.squeeze(raw_output_up_))
            fname = os.path.splitext(os.path.basename(str(jpg_path)))[0]
            out = os.path.join(args.img_path, args.body_dir, fname)
            sparse.save_npz(out, compressed)

        # == Next, save the ROIs ==
        if not args.no_rois:
            img_id = extract_nums_only(fname)
            for c in (c0, c1, c2, c3, c4, c5, c6):
                try:
                    min_x = np.min(c[:, 1])
                except ValueError:
                    min_x = None
                try:
                    min_y = np.min(c[:, 0])
                except ValueError:
                    min_y = None
                try:
                    max_x = np.max(c[:, 1])
                except ValueError:
                    max_x = None
                try:
                    max_y = np.max(c[:, 0])
                except ValueError:
                    max_y = None
                # write out the stuff
                with open(rois_file, "a") as f:
                    f.write(",".join((img_id, str(min_x), str(min_y),
                                      str(max_x), str(max_y), "\n")))

        # Save an image of the mask for our own reference every 1000 steps
        if not args.no_vis and step % args.visualize_step == 0:
            preds = np.expand_dims(raw_output_up_, axis=3)
            msk = decode_labels(preds, num_classes=args.num_classes)
            # the mask
            im = Image.fromarray(msk[0])
            # # Save the mask separately
            # jpg_path = str(jpg_path).split('/')[-1].split('.')[0]
            # out = os.path.join(args.vis_dir, jpg_path + '.png')
            # im.save(out)
            # Save the mask with background
            img_orig = Image.open(jpg_path)
            # create the final result using the mask and the original
            img = np.array(im) * 0.9 + np.array(img_orig) * 0.7
            # clip surpassed colors
            img[img > 255] = 255
            img = Image.fromarray(np.uint8(img))
            out = os.path.join(args.img_path, args.vis_dir, fname + ".png")
            img.save(out)
            # # print('Image processed {}.png'.format(jpg_path))
        t.set_description("Finished " + fname)

    total_time = time.time() - start_time
    print(
        f"The output files have been saved to {args.img_path}/{args.body_dir}")
    print(f"It took {total_time / num_steps} sec on each image.")
def main():
    """Create the model and start the evaluation process."""
    args = get_arguments()
    # remove_huge_images(args.data_list, args.img_path)
    num_steps = file_len(args.data_list)
    # Create queue coordinator.
    coord = tf.train.Coordinator()

    # Load reader.
    print(args.img_path, ' ', file_len(args.data_list))
    with tf.name_scope("create_inputs"):
        reader = ImageReader(
            args.img_path,
            args.data_list,
            None,  # No defined input size.
            False,  # No random scale.
            False,  # No random mirror.
            255,
            IMG_MEAN,
            coord)
        image, label = reader.image, reader.label
        title = reader.queue[0]
    image_batch, label_batch = tf.expand_dims(image, dim=0), tf.expand_dims(
        label, dim=0)  # Add one batch dimension.

    # Create network.
    net = DeepLabResNetModel({'data': image_batch},
                             is_training=False,
                             num_classes=args.num_classes)

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

    # Predictions.
    fc1_voc12_layer = net.layers['fc1_voc12']
    raw_output_up = tf.image.resize_bilinear(fc1_voc12_layer,
                                             tf.shape(image_batch)[1:3, ])
    # uncomment to see only stock segmentation
    # raw_output_up = tf.slice(raw_output_up, [0,0,0,0], [-1,-1,-1,7])
    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)

    # Load weights.
    loader = tf.train.Saver(var_list=restore_var)
    load(loader, sess, args.model_weights)

    # Start queue threads.
    threads = tf.train.start_queue_runners(coord=coord, sess=sess)

    start_time = time.time()
    os.makedirs(args.save_dir, exist_ok=True)

    path_parts = args.img_path.split("/")
    if path_parts[-1].strip() == "":
        path_parts = path_parts[:-1]
    if path_parts[0] == "":
        path_parts[0] = "/"
    bottleneck_dir = os.path.join(*path_parts[:-1],
                                  path_parts[-1] + "_hp_bottlenecks")
    os.makedirs(bottleneck_dir, exist_ok=True)

    # Perform inference.
    for step in range(num_steps):
        jpg_name = None
        try:
            preds, jpg_path, fc1_voc12_val = sess.run(
                [pred, title, fc1_voc12_layer])

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

            jpg_path = str(jpg_path)

            jpg_name = Path(jpg_path).name.split('.')[0]
            img = np.array(im) * 0.9 + np.array(img_o) * 0.7
            img[img > 255] = 255
            img = Image.fromarray(np.uint8(img))
            img.save(os.path.join(args.save_dir, str(jpg_name + '.png')))
            img_bgr = cv2.cvtColor(np.array(img_o), cv2.COLOR_BGR2RGB)
            cv2.imwrite(
                os.path.join(args.save_dir,
                             "stacked_" + str(jpg_name + '.png')),
                np.hstack([img_bgr, im]))

            bottleneck_path = os.path.join(bottleneck_dir,
                                           jpg_name + "_hp_bottleneck.h5")
            with h5py.File(bottleneck_path, "w") as bottleneck_file:
                bottleneck_file.create_dataset("fc1_voc12", data=fc1_voc12_val)
            print('Image processed {}.png'.format(jpg_name))
            print(
                'Wrote human parsing bottleneck to {}'.format(bottleneck_path))
        except Exception as e:
            print(e)
            print('Image failed: ', jpg_name)

    total_time = time.time() - start_time
    print('The output files have been saved to {}'.format(args.save_dir))
    print('It took {} sec on each image.'.format(total_time / num_steps))