LETR: Line Segment Detection Using Transformers without Edges

Related tags

Deep Learning LETR
Overview

LETR: Line Segment Detection Using Transformers without Edges

Introduction

This repository contains the official code and pretrained models for Line Segment Detection Using Transformers without Edges. Yifan Xu*, Weijian Xu*, David Cheung, and Zhuowen Tu. CVPR2021 (Oral)

In this paper, we present a joint end-to-end line segment detection algorithm using Transformers that is post-processing and heuristics-guided intermediate processing (edge/junction/region detection) free. Our method, named LinE segment TRansformers (LETR), takes advantages of having integrated tokenized queries, a self-attention mechanism, and encoding-decoding strategy within Transformers by skipping standard heuristic designs for the edge element detection and perceptual grouping processes. We equip Transformers with a multi-scale encoder/decoder strategy to perform fine-grained line segment detection under a direct endpoint distance loss. This loss term is particularly suitable for detecting geometric structures such as line segments that are not conveniently represented by the standard bounding box representations. The Transformers learn to gradually refine line segments through layers of self-attention.

Model Pipeline

Changelog

05/07/2021: Code for LETR Basic Usage Demo are released.

04/30/2021: Code and pre-trained checkpoint for LETR are released.

Results and Checkpoints

Name sAP10 sAP15 sF10 sF15 URL
Wireframe 65.6 68.0 66.1 67.4 LETR-R101
YorkUrban 29.6 32.0 40.5 42.1 LETR-R50

Reproducing Results

Step1: Code Preparation

git clone https://github.com/mlpc-ucsd/LETR.git

Step2: Environment Installation

mkdir -p data
mkdir -p evaluation/data
mkdir -p exp


conda create -n letr python anaconda
conda activate letr
conda install -c pytorch pytorch torchvision
conda install cython scipy
pip install -U 'git+https://github.com/cocodataset/cocoapi.git#subdirectory=PythonAPI'
pip install docopt

Step3: Data Preparation

To reproduce our results, you need to process two datasets, ShanghaiTech and YorkUrban. Files located at ./helper/wireframe.py and ./helper/york.py are both modified based on the code from L-CNN, which process the raw data from download.

  • ShanghaiTech Train Data
    • To Download (modified based on from L-CNN)
      cd data
      bash ../helper/gdrive-download.sh 1BRkqyi5CKPQF6IYzj_dQxZFQl0OwbzOf wireframe_raw.tar.xz
      tar xf wireframe_raw.tar.xz
      rm wireframe_raw.tar.xz
      python ../helper/wireframe.py ./wireframe_raw ./wireframe_processed
      
  • YorkUrban Train Data
    • To Download
      cd data
      wget https://www.dropbox.com/sh/qgsh2audfi8aajd/AAAQrKM0wLe_LepwlC1rzFMxa/YorkUrbanDB.zip
      unzip YorkUrbanDB.zip 
      python ../helper/york.py ./YorkUrbanDB ./york_processed
      
  • Processed Evaluation Data
    bash ./helper/gdrive-download.sh 1T4_6Nb5r4yAXre3lf-zpmp3RbmyP1t9q ./evaluation/data/wireframe.tar.xz
    bash ./helper/gdrive-download.sh 1ijOXv0Xw1IaNDtp1uBJt5Xb3mMj99Iw2 ./evaluation/data/york.tar.xz
    tar -vxf ./evaluation/data/wireframe.tar.xz -C ./evaluation/data/.
    tar -vxf ./evaluation/data/york.tar.xz -C ./evaluation/data/.
    rm ./evaluation/data/wireframe.tar.xz
    rm ./evaluation/data/york.tar.xz

Step4: Train Script Examples

  1. Train a coarse-model (a.k.a. stage1 model).

    # Usage: bash script/*/*.sh [exp name]
    bash script/train/a0_train_stage1_res50.sh  res50_stage1 # LETR-R50  
    bash script/train/a1_train_stage1_res101.sh res101_stage1 # LETR-R101 
  2. Train a fine-model (a.k.a. stage2 model).

    # Usage: bash script/*/*.sh [exp name]
    bash script/train/a2_train_stage2_res50.sh  res50_stage2  # LETR-R50
    bash script/train/a3_train_stage2_res101.sh res101_stage2 # LETR-R101 
  3. Fine-tune the fine-model with focal loss (a.k.a. stage2_focal model).

    # Usage: bash script/*/*.sh [exp name]
    bash script/train/a4_train_stage2_focal_res50.sh   res50_stage2_focal # LETR-R50
    bash script/train/a5_train_stage2_focal_res101.sh  res101_stage2_focal # LETR-R101 

Step5: Evaluation

  1. Evaluate models.
    # Evaluate sAP^10, sAP^15, sF^10, sF^15 (both Wireframe and YorkUrban datasets).
    bash script/evaluation/eval_stage1.sh [exp name]
    bash script/evaluation/eval_stage2.sh [exp name]
    bash script/evaluation/eval_stage2_focal.sh [exp name]

Citation

If you use this code for your research, please cite our paper:

@InProceedings{Xu_2021_CVPR,
    author    = {Xu, Yifan and Xu, Weijian and Cheung, David and Tu, Zhuowen},
    title     = {Line Segment Detection Using Transformers Without Edges},
    booktitle = {Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition (CVPR)},
    month     = {June},
    year      = {2021},
    pages     = {4257-4266}
}

Acknowledgments

This code is based on the implementations of DETR: End-to-End Object Detection with Transformers.

Comments
  • cuda out of memory error while reproducing the result

    cuda out of memory error while reproducing the result

    i am getting the following error while reproducing the result.

    CUDA out of memory. Tried to allocate 96.00 MiB (GPU 0; 10.76 GiB total capacity; 8.86 GiB already allocated; 64.94 MiB free; 8.99 GiB reserved in total by PyTorch).

    raise subprocess.CalledProcessError(returncode=last_return_code, cmd=cmd)

    Setting OMP_NUM_THREADS environment variable for each process to be 1 in default, to avoid your system being overloaded, please further tune the variable for optimal performance in your application as needed.

    I have tried to reduce the batch size, image resolution and cleared pytorch cache as well; still the issue exists. Any suggestion on this would be a great help.

    opened by madhu9595 6
  • Inference on test images?

    Inference on test images?

    Hi,

    Thanks for sharing the code and nice/solid works! I wonder do you have some example code for inference on the test images without ground truth? The current implementation is only for train/evaluation, and the wrapper of dataloader is with both image and target. Thank you.

    opened by tianchen-ns 5
  • Problems in running the training code

    Problems in running the training code

    Hello, I encountered the following problem when running the training code, and I hope to solve it.

    `(letr) root@shuusv005:~/sjc/LETR# bash ./script/train/a0_train_stage1_res50.sh res50_stage1

    folder not exist | distributed init (rank 1): env:// Traceback (most recent call last): File "src/main.py", line 215, in main(args) File "src/main.py", line 21, in main utils.init_distributed_mode(args) File "/home/shu-usv005/sjc/LETR/src/util/misc.py", line 421, in init_distributed_mode torch.cuda.set_device(args.gpu) File "/home/shu-usv005/anaconda3/envs/letr/lib/python3.7/site-packages/torch/cuda/init.py", line 261, in set_device torch._C._cuda_setDevice(device) RuntimeError: CUDA error: invalid device ordinal Traceback (most recent call last): File "src/main.py", line 215, in main(args) File "src/main.py", line 21, in main utils.init_distributed_mode(args) File "/home/shu-usv005/sjc/LETR/src/util/misc.py", line 421, in init_distributed_mode torch.cuda.set_device(args.gpu) File "/home/shu-usv005/anaconda3/envs/letr/lib/python3.7/site-packages/torch/cuda/init.py", line 261, in set_device torch._C._cuda_setDevice(device) RuntimeError: CUDA error: invalid device ordinal Traceback (most recent call last): File "src/main.py", line 215, in main(args) File "src/main.py", line 21, in main utils.init_distributed_mode(args) File "/home/shu-usv005/sjc/LETR/src/util/misc.py", line 421, in init_distributed_mode torch.cuda.set_device(args.gpu) File "/home/shu-usv005/anaconda3/envs/letr/lib/python3.7/site-packages/torch/cuda/init.py", line 261, in set_device torch._C._cuda_setDevice(device) RuntimeError: CUDA error: invalid device ordinal | distributed init (rank 0): env:// Traceback (most recent call last): File "src/main.py", line 215, in main(args) File "src/main.py", line 21, in main utils.init_distributed_mode(args) File "/home/shu-usv005/sjc/LETR/src/util/misc.py", line 421, in init_distributed_mode torch.cuda.set_device(args.gpu) File "/home/shu-usv005/anaconda3/envs/letr/lib/python3.7/site-packages/torch/cuda/init.py", line 261, in set_device torch._C._cuda_setDevice(device) RuntimeError: CUDA error: invalid device ordinal Traceback (most recent call last): File "src/main.py", line 215, in Traceback (most recent call last): File "src/main.py", line 215, in main(args) File "src/main.py", line 21, in main utils.init_distributed_mode(args)main(args)

    File "/home/shu-usv005/sjc/LETR/src/util/misc.py", line 421, in init_distributed_mode File "src/main.py", line 21, in main utils.init_distributed_mode(args) File "/home/shu-usv005/sjc/LETR/src/util/misc.py", line 421, in init_distributed_mode torch.cuda.set_device(args.gpu) File "/home/shu-usv005/anaconda3/envs/letr/lib/python3.7/site-packages/torch/cuda/init.py", line 261, in set_device torch.cuda.set_device(args.gpu) File "/home/shu-usv005/anaconda3/envs/letr/lib/python3.7/site-packages/torch/cuda/init.py", line 261, in set_device torch._C._cuda_setDevice(device) RuntimeError: CUDA error: invalid device ordinal torch._C._cuda_setDevice(device) RuntimeError: CUDA error: invalid device ordinal Traceback (most recent call last): File "/home/shu-usv005/anaconda3/envs/letr/lib/python3.7/runpy.py", line 193, in _run_module_as_main "main", mod_spec) File "/home/shu-usv005/anaconda3/envs/letr/lib/python3.7/runpy.py", line 85, in _run_code exec(code, run_globals) File "/home/shu-usv005/anaconda3/envs/letr/lib/python3.7/site-packages/torch/distributed/launch.py", line 340, in main() File "/home/shu-usv005/anaconda3/envs/letr/lib/python3.7/site-packages/torch/distributed/launch.py", line 326, in main sigkill_handler(signal.SIGTERM, None) # not coming back File "/home/shu-usv005/anaconda3/envs/letr/lib/python3.7/site-packages/torch/distributed/launch.py", line 301, in sigkill_handler raise subprocess.CalledProcessError(returncode=last_return_code, cmd=cmd) subprocess.CalledProcessError: Command '['/home/shu-usv005/anaconda3/envs/letr/bin/python', '-u', 'src/main.py', '--coco_path', 'data/wireframe_processed', '--output_dir', 'exp/res50_stage1', '--backbone', 'resnet50', '--resume', 'https://dl.fbaipublicfiles.com/detr/detr-r50-e632da11.pth', '--batch_size', '1', '--epochs', '500', '--lr_drop', '200', '--num_queries', '1000', '--num_gpus', '1', '--layer1_num', '3']' returned non-zero exit status 1.


    Setting OMP_NUM_THREADS environment variable for each process to be 1 in default, to avoid your system being overloaded, please further tune the variable for optimal performance in your application as needed.


    Killing subprocess 6288 Killing subprocess 6289 Killing subprocess 6290 Killing subprocess 6291 Killing subprocess 6292 Killing subprocess 6293 Killing subprocess 6294 Killing subprocess 6295 `

    opened by Sun15194 2
  • ImportError: cannot import name '_new_empty_tensor' from 'torchvision.ops'

    ImportError: cannot import name '_new_empty_tensor' from 'torchvision.ops'

    Hi guys,

    I'm trying to run: bash script/train/a0_train_stage1_res50.sh res50_stage1

    But unfortunately, I'm getting the following error:

    Traceback (most recent call last):
      File "/home/cristopher/Workspace/LETR/src/main.py", line 13, in <module>
        import datasets
      File "/home/cristopher/Workspace/LETR/src/datasets/__init__.py", line 5, in <module>
        from .coco import build as build_coco
      File "/home/cristopher/Workspace/LETR/src/datasets/coco.py", line 10, in <module>
        import datasets.transforms as T
      File "/home/cristopher/Workspace/LETR/src/datasets/transforms.py", line 18, in <module>
        from util.misc import interpolate
      File "/home/cristopher/Workspace/LETR/src/util/misc.py", line 22, in <module>
        from torchvision.ops import _new_empty_tensor
    ImportError: cannot import name '_new_empty_tensor' from 'torchvision.ops' (/home/cristopher/miniconda3/envs/letr/lib/python3.9/site-packages/torchvision/ops/__init__.py)
    Traceback (most recent call last):
      File "/home/cristopher/Workspace/LETR/src/main.py", line 13, in <module>
        import datasets
      File "/home/cristopher/Workspace/LETR/src/datasets/__init__.py", line 5, in <module>
        from .coco import build as build_coco
      File "/home/cristopher/Workspace/LETR/src/datasets/coco.py", line 10, in <module>
        import datasets.transforms as T
      File "/home/cristopher/Workspace/LETR/src/datasets/transforms.py", line 18, in <module>
        from util.misc import interpolate
      File "/home/cristopher/Workspace/LETR/src/util/misc.py", line 22, in <module>
        from torchvision.ops import _new_empty_tensor
    ImportError: cannot import name '_new_empty_tensor' from 'torchvision.ops' (/home/cristopher/miniconda3/envs/letr/lib/python3.9/site-packages/torchvision/ops/__init__.py)
    Traceback (most recent call last):
    Traceback (most recent call last):
      File "/home/cristopher/Workspace/LETR/src/main.py", line 13, in <module>
      File "/home/cristopher/Workspace/LETR/src/main.py", line 13, in <module>
            import datasetsimport datasets
    
      File "/home/cristopher/Workspace/LETR/src/datasets/__init__.py", line 5, in <module>
      File "/home/cristopher/Workspace/LETR/src/datasets/__init__.py", line 5, in <module>
            from .coco import build as build_cocofrom .coco import build as build_coco
    
      File "/home/cristopher/Workspace/LETR/src/datasets/coco.py", line 10, in <module>
      File "/home/cristopher/Workspace/LETR/src/datasets/coco.py", line 10, in <module>
            import datasets.transforms as Timport datasets.transforms as T
    
      File "/home/cristopher/Workspace/LETR/src/datasets/transforms.py", line 18, in <module>
      File "/home/cristopher/Workspace/LETR/src/datasets/transforms.py", line 18, in <module>
            from util.misc import interpolatefrom util.misc import interpolate
    
      File "/home/cristopher/Workspace/LETR/src/util/misc.py", line 22, in <module>
      File "/home/cristopher/Workspace/LETR/src/util/misc.py", line 22, in <module>
            from torchvision.ops import _new_empty_tensorfrom torchvision.ops import _new_empty_tensor
    
    ImportErrorImportError: : cannot import name '_new_empty_tensor' from 'torchvision.ops' (/home/cristopher/miniconda3/envs/letr/lib/python3.9/site-packages/torchvision/ops/__init__.py)cannot import name '_new_empty_tensor' from 'torchvision.ops' (/home/cristopher/miniconda3/envs/letr/lib/python3.9/site-packages/torchvision/ops/__init__.py)
    

    My setup has the following packages:

    pytorch                   1.9.0           py3.9_cuda10.2_cudnn7.6.5_0    pytorch
    torchvision               0.10.0                   pypi_0    pypi
    

    I tried finding what was the actual problem, and had failed so far.

    opened by cristophersfr 2
  • Unused parameters warning while reproducing the results

    Unused parameters warning while reproducing the results

    Getting the following warning during training:

    [W reducer.cpp:1050] Warning: find_unused_parameters=True was specified in DDP constructor, but did not find any unused parameters. This flag results in an extra traversal of the autograd graph every iteration, which can adversely affect performance. If your model indeed never has any unused parameters, consider turning this flag off. Note that this warning may be a false positive your model has flow control causing later iterations to have unused parameters. (function operator())

    is file reducer.cpp a part of back-end network and how to access and modify it ?

    opened by madhu9595 1
  • Dataset with COCO

    Dataset with COCO

    Hello,

    In the paper you mention the COCO dataset, How did you use it with polyline ? There is no lines in COCO dataset Could you publish an example of your dataset in coco ?

    Thanks

    opened by aquexel 0
  • Why is the memory requirement increases even after few iterations

    Why is the memory requirement increases even after few iterations

    Why does the memory requirement keep on increasing for several iterations? Is this an expected behaviour for transformers? Even with resnet50 as backbone, on 16gb GPU I am able to run with only 2 batch-size?

    Epoch: [0] [ 0/8819] eta: 6:34:13 lr: 0.000100 loss: 33.1388 (33.1388) time: 2.6822 data: 1.0370 max mem: 3331 Epoch: [0] [ 10/8819] eta: 1:06:08 lr: 0.000100 loss: 28.7849 (29.3637) time: 0.4505 data: 0.1035 max mem: 3409 Epoch: [0] [ 20/8819] eta: 0:52:00 lr: 0.000100 loss: 25.1816 (26.4967) time: 0.2383 data: 0.0167 max mem: 4454 Epoch: [0] [ 30/8819] eta: 0:46:17 lr: 0.000100 loss: 22.1077 (24.6563) time: 0.2421 data: 0.0178 max mem: 4454 Epoch: [0] [ 40/8819] eta: 0:43:19 lr: 0.000100 loss: 19.9209 (23.0615) time: 0.2347 data: 0.0120 max mem: 4454 Epoch: [0] [ 50/8819] eta: 0:43:07 lr: 0.000100 loss: 16.4599 (21.6173) time: 0.2626 data: 0.0404 max mem: 4454 Epoch: [0] [ 60/8819] eta: 0:42:41 lr: 0.000100 loss: 13.7710 (20.2928) time: 0.2848 data: 0.0647 max mem: 4454 Epoch: [0] [ 70/8819] eta: 0:41:28 lr: 0.000100 loss: 12.7223 (19.3061) time: 0.2573 data: 0.0365 max mem: 5913 Epoch: [0] [ 80/8819] eta: 0:40:41 lr: 0.000100 loss: 12.4632 (18.5024) time: 0.2398 data: 0.0171 max mem: 5913 Epoch: [0] [ 90/8819] eta: 0:40:52 lr: 0.000100 loss: 12.4809 (17.8598) time: 0.2686 data: 0.0488 max mem: 5913

    opened by danishansari 0
  • Memmory issue with demo letr notebook

    Memmory issue with demo letr notebook

    I launched a notebook (demo_letr.ipynb) with example with pretrained model res101_stage2_focal. If I run it once everything is ok but if I try to run in again I always get gpu memory issue (I have RTX 3080). My investigation shows that after infer model doesn’t free memory. What am I doing wrong or is it a bug?

    opened by vitalek84 0
  • york eval data

    york eval data

    Hi,very thanks for your contribution. I am trying to eval the model on york data,but i meet the error as follows. "Unable to read file 'evaluation/data/york/valid_copy/P1020171_line.mat'. No such file or directory." I check the dataset you provided, it lost the .mat files in york valid file. So could you please update or send me the full York_eval data? Thank you very much!

    opened by WangShuaixian 0
  • prepare dataset error

    prepare dataset error

    hello ,thanks for you code. when i try to prepare ShanghaiTech Train Data,use your download code,but i can not download. is there any other way to download data? thanks

    opened by kidcats 1
Owner
mlpc-ucsd
mlpc-ucsd
Official Tensorflow implementation of "M-LSD: Towards Light-weight and Real-time Line Segment Detection"

M-LSD: Towards Light-weight and Real-time Line Segment Detection Official Tensorflow implementation of "M-LSD: Towards Light-weight and Real-time Line

NAVER/LINE Vision 357 Jan 4, 2023
Pytorch implementation of "M-LSD: Towards Light-weight and Real-time Line Segment Detection"

M-LSD: Towards Light-weight and Real-time Line Segment Detection Pytorch implementation of "M-LSD: Towards Light-weight and Real-time Line Segment Det

null 123 Jan 4, 2023
Code for "Learning to Segment Rigid Motions from Two Frames".

rigidmask Code for "Learning to Segment Rigid Motions from Two Frames". ** This is a partial release with inference and evaluation code.

Gengshan Yang 157 Nov 21, 2022
[CVPR2021] DoDNet: Learning to segment multi-organ and tumors from multiple partially labeled datasets

DoDNet This repo holds the pytorch implementation of DoDNet: DoDNet: Learning to segment multi-organ and tumors from multiple partially labeled datase

null 116 Dec 12, 2022
code for `Look Closer to Segment Better: Boundary Patch Refinement for Instance Segmentation`

Look Closer to Segment Better: Boundary Patch Refinement for Instance Segmentation (CVPR 2021) Introduction PBR is a conceptually simple yet effective

H.Chen 143 Jan 5, 2023
COD-Rank-Localize-and-Segment (CVPR2021)

COD-Rank-Localize-and-Segment (CVPR2021) Simultaneously Localize, Segment and Rank the Camouflaged Objects Full camouflage fixation training dataset i

JingZhang 52 Dec 20, 2022
Temporal Segment Networks (TSN) in PyTorch

TSN-Pytorch We have released MMAction, a full-fledged action understanding toolbox based on PyTorch. It includes implementation for TSN as well as oth

null 1k Jan 3, 2023
【ACMMM 2021】DSANet: Dynamic Segment Aggregation Network for Video-Level Representation Learning

DSANet: Dynamic Segment Aggregation Network for Video-Level Representation Learning (ACMMM 2021) Overview We release the code of the DSANet (Dynamic S

Wenhao Wu 46 Dec 27, 2022
This project aims to segment 4 common retinal lesions from Fundus Images.

This project aims to segment 4 common retinal lesions from Fundus Images.

Husam Nujaim 1 Oct 10, 2021
Identify the emotion of multiple speakers in an Audio Segment

MevonAI - Speech Emotion Recognition Identify the emotion of multiple speakers in a Audio Segment Report Bug · Request Feature Try the Demo Here Table

Suyash More 110 Dec 3, 2022
Pytorch implementation of paper "Learning Co-segmentation by Segment Swapping for Retrieval and Discovery"

SegSwap Pytorch implementation of paper "Learning Co-segmentation by Segment Swapping for Retrieval and Discovery" [PDF] [Project page] If our project

xshen 41 Dec 10, 2022
some classic model used to segment the medical images like CT、X-ray and so on

github_project This is a project for medical image segmentation. This project includes common medical image segmentation models such as U-net, FCN, De

null 2 Mar 30, 2022
Code & Models for Temporal Segment Networks (TSN) in ECCV 2016

Temporal Segment Networks (TSN) We have released MMAction, a full-fledged action understanding toolbox based on PyTorch. It includes implementation fo

null 1.4k Jan 1, 2023
An e-commerce company wants to segment its customers and determine marketing strategies according to these segments.

customer_segmentation_with_rfm Business Problem : An e-commerce company wants to

Buse Yıldırım 3 Jan 6, 2022
As-ViT: Auto-scaling Vision Transformers without Training

As-ViT: Auto-scaling Vision Transformers without Training [PDF] Wuyang Chen, Wei Huang, Xianzhi Du, Xiaodan Song, Zhangyang Wang, Denny Zhou In ICLR 2

VITA 68 Sep 5, 2022
A Data Annotation Tool for Semantic Segmentation, Object Detection and Lane Line Detection.(In Development Stage)

Data-Annotation-Tool How to Run this Tool? To run this software, follow the steps: git clone https://github.com/Autonomous-Car-Project/Data-Annotation

TiVRA AI 13 Aug 18, 2022
Multivariate Time Series Forecasting with efficient Transformers. Code for the paper "Long-Range Transformers for Dynamic Spatiotemporal Forecasting."

Spacetimeformer Multivariate Forecasting This repository contains the code for the paper, "Long-Range Transformers for Dynamic Spatiotemporal Forecast

QData 440 Jan 2, 2023
Find-Lane-Line - Use openCV library and Python to detect the road-lane-line

Find-Lane-Line This project is to use openCV library and Python to detect the road-lane-line. Data Pipeline Step one : Color Selection Step two : Cann

Kenny Cheng 3 Aug 17, 2022