diff --git a/.gitea/workflows/build.yaml b/.gitea/workflows/build.yaml new file mode 100644 index 0000000..1966567 --- /dev/null +++ b/.gitea/workflows/build.yaml @@ -0,0 +1,47 @@ +name: Build +run-name: ${{ github.actor }} is upgrade release πŸš€ +on: [push] +env: + REPOSITORY: ${{ github.repository }} + COMMIT_ID: ${{ github.sha }} +jobs: + Build-Deploy-Actions: + runs-on: ubuntu-latest + steps: + - run: echo "πŸŽ‰ The job was automatically triggered by a ${{ github.event_name }} event." + - run: echo "🐧 This job is now running on a ${{ runner.os }} server hosted by Gitea!" + - run: echo "πŸ”Ž The name of your branch is ${{ github.ref }} and your repository is ${{ github.repository }}." + - name: Check out repository code + uses: actions/checkout@v3 + - + name: Setup Git LFS + run: | + git lfs install + git lfs fetch + git lfs checkout + - name: List files in the repository + run: | + ls ${{ github.workspace }} + - + name: Docker Image Info + id: image-info + run: | + echo "::set-output name=image_name::$(echo $REPOSITORY | tr '[:upper:]' '[:lower:]')" + echo "::set-output name=image_tag::${COMMIT_ID:0:10}" + - + name: Login to Docker Hub + uses: docker/login-action@v2 + with: + registry: artifacts.iflytek.com + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + - + name: Build and push + run: | + docker version + docker buildx build -t artifacts.iflytek.com/docker-private/atp/${{ steps.image-info.outputs.image_name }}:${{ steps.image-info.outputs.image_tag }} . --file ${{ github.workspace }}/Dockerfile --load + docker push artifacts.iflytek.com/docker-private/atp/${{ steps.image-info.outputs.image_name }}:${{ steps.image-info.outputs.image_tag }} + docker rmi artifacts.iflytek.com/docker-private/atp/${{ steps.image-info.outputs.image_name }}:${{ steps.image-info.outputs.image_tag }} + - run: echo "🍏 This job's status is ${{ job.status }}." diff --git a/Base-RCNN-FPN.yml b/Base-RCNN-FPN.yml new file mode 100644 index 0000000..9f94e32 --- /dev/null +++ b/Base-RCNN-FPN.yml @@ -0,0 +1,69 @@ +MODEL: + MASK_ON: True + META_ARCHITECTURE: "GeneralizedRCNN" + PIXEL_MEAN: [123.675, 116.280, 103.530] + PIXEL_STD: [58.395, 57.120, 57.375] + BACKBONE: + NAME: "build_vit_fpn_backbone" + VIT: + OUT_FEATURES: ["layer3", "layer5", "layer7", "layer11"] + DROP_PATH: 0.1 + IMG_SIZE: [224,224] + POS_TYPE: "abs" + FPN: + IN_FEATURES: ["layer3", "layer5", "layer7", "layer11"] + ANCHOR_GENERATOR: + SIZES: [[32], [64], [128], [256], [512]] # One size for each in feature map + ASPECT_RATIOS: [[0.5, 1.0, 2.0]] # Three aspect ratios (same for all in feature maps) + RPN: + IN_FEATURES: ["p2", "p3", "p4", "p5", "p6"] + PRE_NMS_TOPK_TRAIN: 2000 # Per FPN level + PRE_NMS_TOPK_TEST: 1000 # Per FPN level + # Detectron1 uses 2000 proposals per-batch, + # (See "modeling/rpn/rpn_outputs.py" for details of this legacy issue) + # which is approximately 1000 proposals per-image since the default batch size for FPN is 2. + POST_NMS_TOPK_TRAIN: 1000 + POST_NMS_TOPK_TEST: 1000 + ROI_HEADS: + NAME: "StandardROIHeads" + IN_FEATURES: ["p2", "p3", "p4", "p5"] + NUM_CLASSES: 5 + ROI_BOX_HEAD: + NAME: "FastRCNNConvFCHead" + NUM_FC: 2 + POOLER_RESOLUTION: 7 + ROI_MASK_HEAD: + NAME: "MaskRCNNConvUpsampleHead" + NUM_CONV: 4 + POOLER_RESOLUTION: 14 +DATASETS: + TRAIN: ("publaynet_train",) + TEST: ("publaynet_val",) +SOLVER: + LR_SCHEDULER_NAME: "WarmupCosineLR" + AMP: + ENABLED: True + OPTIMIZER: "ADAMW" + BACKBONE_MULTIPLIER: 1.0 + CLIP_GRADIENTS: + ENABLED: True + CLIP_TYPE: "full_model" + CLIP_VALUE: 1.0 + NORM_TYPE: 2.0 + WARMUP_FACTOR: 0.01 + BASE_LR: 0.0004 + WEIGHT_DECAY: 0.05 + IMS_PER_BATCH: 32 +INPUT: + CROP: + ENABLED: True + TYPE: "absolute_range" + SIZE: (384, 600) + MIN_SIZE_TRAIN: (480, 512, 544, 576, 608, 640, 672, 704, 736, 768, 800) + FORMAT: "RGB" +DATALOADER: + FILTER_EMPTY_ANNOTATIONS: False +VERSION: 2 +AUG: + DETR: True +SEED: 42 \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..d1e0184 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,27 @@ +# FROM python:3.9 +# COPY . /app +# WORKDIR /app +# RUN pip install --trusted-host pypi.python.org -r requirements.txt +# CMD python app.py + +# Use an official Python runtime as a parent image +FROM python:3.7.4-slim +RUN sed -i 's#http://deb.debian.org#https://mirrors.ustc.edu.cn#g' /etc/apt/sources.list && sed -i 's|security.debian.org/debian-security|mirrors.ustc.edu.cn/debian-security|g' /etc/apt/sources.list + +WORKDIR /app +COPY requirements.txt /app +RUN pip config set global.index-url https://pypi.mirrors.ustc.edu.cn/simple/ + +RUN apt update && apt install -y libgl1-mesa-glx libglib2.0-0 build-essential +RUN pip3 install --trusted-host pypi.python.org -r requirements.txt +RUN python -m pip install detectron2 -f \ + https://dl.fbaipublicfiles.com/detectron2/wheels/cu113/torch1.10/index.html +# Set the working directory to /app + + +# Copy the current directory contents into the container at /app +COPY . /app + + +# Run main.py when the container launches +CMD ["python", "app.py"] diff --git a/app.py b/app.py new file mode 100644 index 0000000..fd12f8b --- /dev/null +++ b/app.py @@ -0,0 +1,80 @@ +import os + +import sys +sys.path.append("unilm") + +import cv2 + +from unilm.dit.object_detection.ditod import add_vit_config + +import torch + +from detectron2.config import CfgNode as CN +from detectron2.config import get_cfg +from detectron2.utils.visualizer import ColorMode, Visualizer +from detectron2.data import MetadataCatalog +from detectron2.engine import DefaultPredictor + +import gradio as gr +from gradio.themes.utils import sizes + + +theme = gr.themes.Default(radius_size=sizes.radius_none).set( + block_label_text_color = '#4D63FF', + block_title_text_color = '#4D63FF', + button_primary_text_color = '#4D63FF', + button_primary_background_fill='#FFFFFF', + button_primary_border_color='#4D63FF', + button_primary_background_fill_hover='#EDEFFF', +) + + +# Step 1: instantiate config +cfg = get_cfg() +add_vit_config(cfg) +cfg.merge_from_file("cascade_dit_base.yml") + +# Step 2: add model weights URL to config +cfg.MODEL.WEIGHTS = "https://layoutlm.blob.core.windows.net/dit/dit-fts/publaynet_dit-b_cascade.pth" + +# Step 3: set device +cfg.MODEL.DEVICE = "cuda" if torch.cuda.is_available() else "cpu" + +# Step 4: define model +predictor = DefaultPredictor(cfg) + + +def analyze_image(img): + md = MetadataCatalog.get(cfg.DATASETS.TEST[0]) + if cfg.DATASETS.TEST[0]=='icdar2019_test': + md.set(thing_classes=["table"]) + else: + md.set(thing_classes=["text","title","list","table","figure"]) + + output = predictor(img)["instances"] + v = Visualizer(img[:, :, ::-1], + md, + scale=1.0, + instance_mode=ColorMode.SEGMENTATION) + result = v.draw_instance_predictions(output.to("cpu")) + result_image = result.get_image()[:, :, ::-1] + + return result_image + + +with gr.Blocks(theme=theme, css="footer {visibility: hidden}") as demo: + gr.Markdown(""" +
η‰ˆι’εˆ†ζž
+ """) + with gr.Row(): + with gr.Column(): + image = gr.Image(label="图片", type="numpy") + with gr.Row(): + button = gr.Button("提亀", variant="primary") + box2 = gr.Image(label="图片", type="numpy") + + button.click(fn=analyze_image, inputs=[image], outputs=box2) + examples = gr.Examples(examples=[['publaynet_example.jpeg']], inputs=[image], label="例子") + + +demo.launch(server_name = "0.0.0.0") diff --git a/cascade_dit_base.yml b/cascade_dit_base.yml new file mode 100644 index 0000000..0b84d7f --- /dev/null +++ b/cascade_dit_base.yml @@ -0,0 +1,20 @@ +_BASE_: "Base-RCNN-FPN.yml" +MODEL: + PIXEL_MEAN: [ 127.5, 127.5, 127.5 ] + PIXEL_STD: [ 127.5, 127.5, 127.5 ] + WEIGHTS: "https://layoutlm.blob.core.windows.net/dit/dit-pts/dit-base-224-p16-500k-62d53a.pth" + VIT: + NAME: "dit_base_patch16" + ROI_HEADS: + NAME: CascadeROIHeads + ROI_BOX_HEAD: + CLS_AGNOSTIC_BBOX_REG: True + RPN: + POST_NMS_TOPK_TRAIN: 2000 +SOLVER: + WARMUP_ITERS: 1000 + IMS_PER_BATCH: 16 + MAX_ITER: 60000 + CHECKPOINT_PERIOD: 2000 +TEST: + EVAL_PERIOD: 2000 diff --git a/publaynet_example.jpeg b/publaynet_example.jpeg new file mode 100644 index 0000000..023ac06 Binary files /dev/null and b/publaynet_example.jpeg differ diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..6d48afa --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +gradio +opencv-python +shapely diff --git a/unilm b/unilm new file mode 160000 index 0000000..b5b99a3 --- /dev/null +++ b/unilm @@ -0,0 +1 @@ +Subproject commit b5b99a36ab8ea83ef64d1efaa307133184243c57