Skip to content

Commit

Permalink
Merge pull request PaddlePaddle#59 from jerrywgz/move_dir
Browse files Browse the repository at this point in the history
mv paddlevlp to paddlemix
  • Loading branch information
lyuwenyu authored Aug 14, 2023
2 parents 519e181 + ecbcf42 commit 5747a4a
Show file tree
Hide file tree
Showing 616 changed files with 19,766 additions and 15,231 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@
description: Check C++ code style using cpplint.py.
entry: bash ./.travis/codestyle/cpplint_pre_commit.hook
language: system
files: \.(c|cc|cxx|cpp|cu|h|hpp|hxx)$
files: \.(c|cc|cxx|cpp|cu|h|hpp|hxx)$
2 changes: 1 addition & 1 deletion .travis/codestyle/cpplint_pre_commit.hook
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ for file in $files; do
fi
done

exit $TOTAL_ERRORS
exit $TOTAL_ERRORS
25 changes: 25 additions & 0 deletions .travis/codestyle/pylint_pre_commit.hook
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash

TOTAL_ERRORS=0


DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
export PYTHONPATH=$DIR:$PYTHONPATH

readonly VERSION="2.12.0"
version=$(pylint --version | grep 'pylint')

if ! [[ $version == *"$VERSION"* ]]; then
pip install pylint==2.12.0
fi

# The trick to remove deleted files: https://stackoverflow.com/a/2413151
for file in $(git diff --name-status | awk '$1 != "D" {print $2}'); do
pylint --disable=all --load-plugins=docstring_checker \
--enable=doc-string-one-line,doc-string-end-with,doc-string-with-all-args,doc-string-triple-quotes,doc-string-missing,doc-string-indent-error,doc-string-with-returns,doc-string-with-raises $file;
TOTAL_ERRORS=$(expr $TOTAL_ERRORS + $?);
done

exit $TOTAL_ERRORS
#For now, just warning:
#exit 0
15 changes: 15 additions & 0 deletions .travis/precommit.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
#!/bin/bash

# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

function abort(){
echo "Your commit not fit PaddlePaddle code style" 1>&2
echo "Please use pre-commit scripts to auto-format your code" 1>&2
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,16 @@ pip install -e .
</ul>
<li><b>图文预训练</b></li>
<ul>
<li><a href="paddlevlp/examples/evaclip">EVA-CLIP</a></li>
<li><a href="paddlevlp/examples/blip2">BLIP-2</a></li>
<li><a href="paddlevlp/examples/minigpt4">miniGPT-4</a></li>
<li><a href="paddlevlp/examples/visualglm">VIsualGLM</a></li>
<li><a href="paddlemix/examples/evaclip">EVA-CLIP</a></li>
<li><a href="paddlemix/examples/blip2">BLIP-2</a></li>
<li><a href="paddlemix/examples/minigpt4">miniGPT-4</a></li>
<li><a href="paddlemix/examples/visualglm">VIsualGLM</a></li>
</ul>
</ul>
<li><b>开放世界视觉模型</b></li>
<ul>
<li><a href="paddlevlp/examples/groundingdino">Grounding DINO</a></li>
<li><a href="paddlevlp/examples/Sam">SAM</a></li>
<li><a href="paddlemix/examples/groundingdino">Grounding DINO</a></li>
<li><a href="paddlemix/examples/Sam">SAM</a></li>
</ul>
</td>
<td>
Expand Down
77 changes: 37 additions & 40 deletions applications/Automatic_label/automatic_label.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,24 @@
import os
from dataclasses import dataclass, field

import paddle
import matplotlib.pyplot as plt
import nltk
import numpy as np
import requests
from paddlenlp.trainer import PdArgumentParser
from PIL import Image

import paddle
import paddle.nn.functional as F
from PIL import Image, ImageDraw, ImageFont

from paddlevlp.processors.groundingdino_processing import GroudingDinoProcessor
from paddlevlp.models.groundingdino.modeling import GroundingDinoModel
from paddlevlp.models.sam.modeling import SamModel
from paddlevlp.processors.sam_processing import SamProcessor
import requests
from paddlenlp.trainer import PdArgumentParser
from paddlenlp.transformers import AutoTokenizer
from paddlevlp.processors.blip_processing import BlipImageProcessor, BlipTextProcessor
from paddlevlp.models.blip2.modeling import Blip2ForConditionalGeneration
from paddlevlp.processors.blip_processing import Blip2Processor
import nltk
from PIL import Image, ImageDraw, ImageFont

from paddlevlp.utils.log import logger
import matplotlib.pyplot as plt
from paddlemix.models.blip2.modeling import Blip2ForConditionalGeneration
from paddlemix.models.groundingdino.modeling import GroundingDinoModel
from paddlemix.models.sam.modeling import SamModel
from paddlemix.processors.blip_processing import (
Blip2Processor, BlipImageProcessor, BlipTextProcessor)
from paddlemix.processors.groundingdino_processing import GroudingDinoProcessor
from paddlemix.processors.sam_processing import SamProcessor
from paddlemix.utils.log import logger


def show_mask(mask, ax, random_color=False):
Expand All @@ -54,7 +50,7 @@ def show_box(box, ax, label):
w, h = box[2] - box[0], box[3] - box[1]
ax.add_patch(
plt.Rectangle(
(x0, y0), w, h, edgecolor='green', facecolor=(0, 0, 0, 0), lw=2))
(x0, y0), w, h, edgecolor="green", facecolor=(0, 0, 0, 0), lw=2))
ax.text(x0, y0, label)


Expand All @@ -71,15 +67,16 @@ class DataArguments:

prompt: str = field(
default="describe the image",
metadata={"help": "The prompt of the image to be generated."
}) # "Question: how many cats are there? Answer:"
metadata={"help": "The prompt of the image to be generated."},
) # "Question: how many cats are there? Answer:"


@dataclass
class ModelArguments:
"""
Arguments pertaining to which model/config/tokenizer we are going to fine-tune from.
"""

blip2_model_name_or_path: str = field(
default="paddlemix/blip2-caption-opt2.7b",
metadata={"help": "Path to pretrained model or model identifier"}, )
Expand Down Expand Up @@ -125,24 +122,24 @@ def generate_caption(raw_image, prompt, processor, blip2_model):
def generate_tags(caption):
lemma = nltk.wordnet.WordNetLemmatizer()

nltk.download(['punkt', 'averaged_perceptron_tagger', 'wordnet'])
nltk.download(["punkt", "averaged_perceptron_tagger", "wordnet"])
tags_list = [
word for (word, pos) in nltk.pos_tag(nltk.word_tokenize(caption))
if pos[0] == 'N'
if pos[0] == "N"
]
tags_lemma = [lemma.lemmatize(w) for w in tags_list]
tags = ', '.join(map(str, tags_lemma))
tags = ", ".join(map(str, tags_lemma))

return tags


def main():
parser = PdArgumentParser((ModelArguments, DataArguments))
model_args, data_args = parser.parse_args_into_dataclasses()
url = (data_args.input_image)
url = data_args.input_image

logger.info("blip2_model: {}".format(model_args.blip2_model_name_or_path))
#bulid blip2 processor
# bulid blip2 processor
blip2_tokenizer_class = AutoTokenizer.from_pretrained(
model_args.text_model_name_or_path, use_fast=False)
blip2_image_processor = BlipImageProcessor.from_pretrained(
Expand All @@ -162,27 +159,27 @@ def main():
logger.info("blip2_model build finish!")

logger.info("dino_model: {}".format(model_args.dino_model_name_or_path))
#bulid dino processor
# bulid dino processor
dino_processor = GroudingDinoProcessor.from_pretrained(
model_args.dino_model_name_or_path)
#bulid dino model
# bulid dino model
dino_model = GroundingDinoModel.from_pretrained(
model_args.dino_model_name_or_path)
dino_model.eval()
logger.info("dino_model build finish!")

#buidl sam processor
# buidl sam processor
sam_processor = SamProcessor.from_pretrained(
model_args.sam_model_name_or_path)
#bulid model
# bulid model
logger.info("SamModel: {}".format(model_args.sam_model_name_or_path))
sam_model = SamModel.from_pretrained(
model_args.sam_model_name_or_path, input_type="boxs")
logger.info("SamModel build finish!")

#read image
# read image
if os.path.isfile(url):
#read image
# read image
image_pil = Image.open(data_args.input_image)
else:
image_pil = Image.open(requests.get(url, stream=True).raw)
Expand All @@ -191,26 +188,26 @@ def main():
image_pil,
prompt=data_args.prompt,
processor=blip2_processor,
blip2_model=blip2_model)
blip2_model=blip2_model, )

det_prompt = generate_tags(caption)
logger.info("det prompt: {}".format(det_prompt))

image_pil = image_pil.convert("RGB")

#preprocess image text_prompt
# preprocess image text_prompt
image_tensor, mask, tokenized_out = dino_processor(
images=image_pil, text=det_prompt)

with paddle.no_grad():
outputs = dino_model(
image_tensor,
mask,
input_ids=tokenized_out['input_ids'],
attention_mask=tokenized_out['attention_mask'],
input_ids=tokenized_out["input_ids"],
attention_mask=tokenized_out["attention_mask"],
text_self_attention_masks=tokenized_out[
'text_self_attention_masks'],
position_ids=tokenized_out['position_ids'])
"text_self_attention_masks"],
position_ids=tokenized_out["position_ids"], )

logits = F.sigmoid(outputs["pred_logits"])[0] # (nq, 256)
boxes = outputs["pred_boxes"][0] # (nq, 4)
Expand Down Expand Up @@ -265,12 +262,12 @@ def main():
show_box(box, plt.gca(), label)

plt.title(caption)
plt.axis('off')
plt.axis("off")
plt.savefig(
os.path.join(model_args.output_dir, 'mask_pred.jpg'),
os.path.join(model_args.output_dir, "mask_pred.jpg"),
bbox_inches="tight",
dpi=300,
pad_inches=0.0)
pad_inches=0.0, )

logger.info("finish!")

Expand Down
Loading

0 comments on commit 5747a4a

Please sign in to comment.