parent
ded5ff6966
commit
b26283e4dd
8 changed files with 462 additions and 8 deletions
@ -0,0 +1,15 @@ |
||||
from modelscope.pipelines import pipeline |
||||
from modelscope.outputs import OutputKeys |
||||
from PIL import Image |
||||
from background_generation import modelscope_warpper |
||||
|
||||
model = "damo/cv_background_generation_sd" |
||||
pipe = pipeline('background_generation_task', model=model, device='gpu', auto_collate=False, model_revision='v1.1.0') |
||||
out = pipe( |
||||
'https://vision-poster.oss-cn-shanghai.aliyuncs.com/lllcho.lc/data/test_data/demo_example/%E5%8C%96%E5%A6%86%E5%93%81/1c33fc5e8b084269ffdb4e0557c2c3c4.png', |
||||
'https://vision-poster.oss-cn-shanghai.aliyuncs.com/lllcho.lc/data/test_data/5d873b5f64b82bcbb235748347602dce38c6ec1d.jpg', |
||||
num_inference_steps=20, |
||||
num_images_per_prompt=2, |
||||
seed=None, |
||||
noise_level=500 |
||||
) |
||||
@ -0,0 +1,55 @@ |
||||
import cv2 |
||||
import os |
||||
import numpy as np |
||||
import torch |
||||
from modelscope import snapshot_download |
||||
from PIL import Image |
||||
import onnxruntime |
||||
|
||||
def softmax(x): |
||||
x -= np.max(x, axis=0, keepdims=True) |
||||
x = np.exp(x) / np.sum(np.exp(x), axis=0, keepdims=True) |
||||
return x |
||||
|
||||
def get_rot(image, ort_session): |
||||
img_cv = cv2.cvtColor(np.asarray(image), cv2.COLOR_RGB2BGR) |
||||
img_clone = img_cv.copy() |
||||
img_np = cv2.resize(img_cv, (224, 224)) |
||||
img_np = img_np.astype(np.float32) |
||||
mean = np.array([103.53, 116.28, 123.675], dtype=np.float32).reshape((1, 1, 3)) |
||||
norm = np.array([0.01742919, 0.017507, 0.01712475], dtype=np.float32).reshape((1, 1, 3)) |
||||
img_np = (img_np - mean) * norm |
||||
img_tensor = torch.from_numpy(img_np) |
||||
img_tensor = img_tensor.unsqueeze(0) |
||||
img_nchw = img_tensor.permute(0, 3, 1, 2) |
||||
ort_inputs = {ort_session.get_inputs()[0].name: img_nchw.numpy()} |
||||
outputs = ort_session.run(None, ort_inputs) |
||||
logits = outputs[0].reshape((-1,)) |
||||
probs = softmax(logits) |
||||
rot_idx = np.argmax(probs) |
||||
if rot_idx == 1: |
||||
print('rot 90') |
||||
img_clone = cv2.transpose(img_clone) |
||||
img_clone = np.flip(img_clone, 1) |
||||
return Image.fromarray(cv2.cvtColor(img_clone, cv2.COLOR_BGR2RGB)) |
||||
elif rot_idx == 2: |
||||
print('rot 180') |
||||
img_clone = cv2.flip(img_clone, -1) |
||||
return Image.fromarray(cv2.cvtColor(img_clone, cv2.COLOR_BGR2RGB)) |
||||
elif rot_idx == 3: |
||||
print('rot 270') |
||||
img_clone = cv2.transpose(img_clone) |
||||
img_clone = np.flip(img_clone, 0) |
||||
return Image.fromarray(cv2.cvtColor(img_clone, cv2.COLOR_BGR2RGB)) |
||||
else: |
||||
return image |
||||
|
||||
model_dir = snapshot_download('Cherrytest/rot_bgr', revision='v1.0.0') |
||||
model_path = os.path.join(model_dir, 'rot_bgr.onnx') |
||||
ort_session = onnxruntime.InferenceSession(model_path) |
||||
img_path = 'path_of_your_image' |
||||
image = Image.open(img_path) |
||||
image = image.convert('RGB') |
||||
image = get_rot(image, ort_session) |
||||
out_path = 'path_to_save_image' |
||||
image.save(out_path) |
||||
@ -0,0 +1,52 @@ |
||||
import tempfile |
||||
from modelscope.msdatasets import MsDataset |
||||
from modelscope.metainfo import Trainers |
||||
from modelscope.trainers import build_trainer |
||||
from modelscope.utils.constant import DownloadMode |
||||
from modelscope.utils.hub import snapshot_download |
||||
|
||||
|
||||
train_dataset = MsDataset( |
||||
MsDataset.load( |
||||
"coco_2014_caption", |
||||
namespace="modelscope", |
||||
split="train[:100]", |
||||
download_mode=DownloadMode.REUSE_DATASET_IF_EXISTS).remap_columns({ |
||||
'image': 'image', |
||||
'caption': 'text' |
||||
})) |
||||
test_dataset = MsDataset( |
||||
MsDataset.load( |
||||
"coco_2014_caption", |
||||
namespace="modelscope", |
||||
split="validation[:20]", |
||||
download_mode=DownloadMode.REUSE_DATASET_IF_EXISTS).remap_columns({ |
||||
'image': 'image', |
||||
'caption': 'text' |
||||
})) |
||||
|
||||
|
||||
def cfg_modify_fn(cfg): |
||||
cfg.train.hooks = [{ |
||||
'type': 'CheckpointHook', |
||||
'interval': 2 |
||||
}, { |
||||
'type': 'TextLoggerHook', |
||||
'interval': 1 |
||||
}, { |
||||
'type': 'IterTimerHook' |
||||
}] |
||||
cfg.train.max_epochs=2 |
||||
return cfg |
||||
|
||||
pretrained_model = 'damo/ofa_pretrain_base_zh' |
||||
pretrain_path = snapshot_download(pretrained_model, revision='v1.0.2') |
||||
|
||||
args = dict( |
||||
model=pretrain_path, |
||||
train_dataset=train_dataset, |
||||
eval_dataset=test_dataset, |
||||
cfg_modify_fn=cfg_modify_fn, |
||||
work_dir = tempfile.TemporaryDirectory().name) |
||||
trainer = build_trainer(name=Trainers.ofa, default_args=args) |
||||
trainer.train() |
||||
@ -0,0 +1,28 @@ |
||||
# require modelscope>=0.3.7,目前默认已经超过,您检查一下即可 |
||||
# 按照更新镜像的方法处理或者下面的方法 |
||||
# pip install --upgrade modelscope -f https://modelscope.oss-cn-beijing.aliyuncs.com/releases/repo.html |
||||
# 需要单独安装decord,安装方法:pip install decord |
||||
import torch |
||||
from modelscope.utils.constant import Tasks |
||||
from modelscope.pipelines import pipeline |
||||
from modelscope.preprocessors.image import load_image |
||||
|
||||
pipeline = pipeline(task=Tasks.multi_modal_embedding, |
||||
model='damo/multi-modal_clip-vit-large-patch14_336_zh', model_revision='v1.0.1') |
||||
input_img = load_image('https://clip-cn-beijing.oss-cn-beijing.aliyuncs.com/pokemon.jpeg') # 支持皮卡丘示例图片路径/本地图片 返回PIL.Image |
||||
input_texts = ["杰尼龟", "妙蛙种子", "小火龙", "皮卡丘"] |
||||
|
||||
# 支持一张图片(PIL.Image)或多张图片(List[PIL.Image])输入,输出归一化特征向量 |
||||
img_embedding = pipeline.forward({'img': input_img})['img_embedding'] # 2D Tensor, [图片数, 特征维度] |
||||
|
||||
# 支持一条文本(str)或多条文本(List[str])输入,输出归一化特征向量 |
||||
text_embedding = pipeline.forward({'text': input_texts})['text_embedding'] # 2D Tensor, [文本数, 特征维度] |
||||
|
||||
# 计算图文相似度 |
||||
with torch.no_grad(): |
||||
# 计算内积得到logit,考虑模型temperature |
||||
logits_per_image = (img_embedding / pipeline.model.temperature) @ text_embedding.t() |
||||
# 根据logit计算概率分布 |
||||
probs = logits_per_image.softmax(dim=-1).cpu().numpy() |
||||
|
||||
print("图文匹配概率:", probs) |
||||
@ -0,0 +1,304 @@ |
||||
<?php |
||||
$operator = PyCore::import("operator"); |
||||
$builtins = PyCore::import("builtins"); |
||||
/** 与之对应的是多行注释 |
||||
用三个双引号表示,这两段双引号当中的内容都会被视作是注释 |
||||
*/ |
||||
|
||||
$values = new PyList([]); |
||||
$kv = new PyDict([ |
||||
"hello" => "world", |
||||
]); |
||||
$__value = 3; |
||||
$values->__setitem__(0, $__value); |
||||
$__value = 10; |
||||
$values->__setitem__(1, $__value); |
||||
$c = 1 + 1; |
||||
$d = 8 - 1; |
||||
$e = 10 * 2; |
||||
$f = 35 / 5; |
||||
$g = $operator->floordiv(5 , 3); |
||||
$h = $operator->floordiv(-5 , 3); |
||||
$j = $operator->floordiv(5.5 , 3); |
||||
$k = $operator->floordiv(-5 , 3); |
||||
$__value = 7 % 3; |
||||
$values->__setitem__(10, $__value); |
||||
$__value = $operator->pow(2 , 3); |
||||
$values->__setitem__(11, $__value); |
||||
$__value = 1 + 3 * 2; |
||||
$values->__setitem__(12, $__value); |
||||
$__value = 1 + 3 * 2; |
||||
$values->__setitem__(13, $__value); |
||||
$_ = true; |
||||
$_ = false; |
||||
$_ = !true; |
||||
$_ = !false; |
||||
$_ = true && false; |
||||
$_ = false || true; |
||||
$_ = true + true; |
||||
$_ = true * 8; |
||||
$_ = false - 5; |
||||
$_ = 0 == false; |
||||
$_ = 1 == true; |
||||
$_ = 2 == true; |
||||
$_ = -5 != false; |
||||
$_ = PyCore::bool(0); |
||||
$_ = PyCore::bool(4); |
||||
$_ = PyCore::bool(-6); |
||||
$_ = 0 && 2; |
||||
$_ = -5 || 0; |
||||
$_ = 1 == 1; |
||||
$_ = 2 == 1; |
||||
$_ = 1 != 1; |
||||
$_ = 2 != 1; |
||||
$_ = 1 < 10; |
||||
$_ = 1 > 10; |
||||
$_ = 2 <= 2; |
||||
$_ = 2 >= 2; |
||||
$_ = 1 < 2 && 2 < 3; |
||||
$_ = 2 < 3 && 3 < 2; |
||||
$_ = 1 < 2; |
||||
$_ = 2 < 3; |
||||
$a = new PyList([1, 2, 3, 4]); |
||||
$b = $a; |
||||
$_ = $b == $a; |
||||
$_ = $b == $a; |
||||
$_ = new PyList([1, 2, 3, 4]); |
||||
$_ = $b == $a; |
||||
$_ = $b == $a; |
||||
$_ = "This is a string."; |
||||
$_ = "This is also a string."; |
||||
$_ = "Hello " + "world!"; |
||||
$_ = "Hello world!"; |
||||
$_ = "This is a string"->__getitem__(0); |
||||
$_ = PyCore::len("This is a string"); |
||||
$name = "Reiko"; |
||||
$_ = "She said her name is " . $name . "."; |
||||
$_ = $name . " is " . PyCore::len($name) . " characters long."; |
||||
$_ = null; |
||||
$_ = "etc" == null; |
||||
$_ = null == null; |
||||
$_ = PyCore::bool(null); |
||||
$_ = PyCore::bool(0); |
||||
$_ = PyCore::bool(""); |
||||
$_ = PyCore::bool(new PyList([])); |
||||
$_ = PyCore::bool(new PyDict([ |
||||
])); |
||||
$_ = PyCore::bool([]); |
||||
PyCore::print("I'm Python. Nice to meet you!"); |
||||
PyCore::print("Hello, World", end: "!"); |
||||
$input_string_var = PyCore::input("Enter some data: "); |
||||
$some_var = 5; |
||||
$_ = 3 > 2 ? "yahoo!" : 2; |
||||
|
||||
function test() { |
||||
if (3 > 2) { |
||||
return "yahoo"; |
||||
} else { |
||||
return 2; |
||||
} |
||||
|
||||
} |
||||
|
||||
|
||||
$li = new PyList([]); |
||||
$other_li = new PyList([4, 5, 6]); |
||||
$li->append(1); |
||||
$li->append(2); |
||||
$li->append(4); |
||||
$li->append(3); |
||||
$li->pop(); |
||||
$li->append(3); |
||||
$_ = $li->__getitem__(0); |
||||
$_ = $li->__getitem__(-1); |
||||
$_ = $li->__getitem__(4); |
||||
$_ = $li->__getitem__(PyCore::slice(1, 3, null)); |
||||
$_ = $li->__getitem__(PyCore::slice(2, null, null)); |
||||
$_ = $li->__getitem__(PyCore::slice(null, 3, null)); |
||||
$_ = $li->__getitem__(PyCore::slice(null, null, 2)); |
||||
$_ = $li->__getitem__(PyCore::slice(null, null, -1)); |
||||
$li2 = $li->__getitem__(PyCore::slice(null, null, null)); |
||||
$li->__delitem__(2); |
||||
|
||||
$li->remove(2); |
||||
$li->remove(2); |
||||
$li->insert(1, 2); |
||||
$li->index(2); |
||||
$li->index(4); |
||||
$tup = [1, 2, 3]; |
||||
$tup->__getitem__(0); |
||||
$__value = 3; |
||||
$tup->__setitem__(0, $__value); |
||||
PyCore::type(1); |
||||
PyCore::type([1]); |
||||
PyCore::type([]); |
||||
$_ = PyCore::len($tup); |
||||
$_ = $tup + [4, 5, 6]; |
||||
$_ = $tup->__getitem__(PyCore::slice(null, 2, null)); |
||||
$_ = $tup->__contains__(2); |
||||
[$a, $b, $c] = [1, 2, 3]; |
||||
[$d, $e, $f] = [4, 5, 6]; |
||||
[$e, $d] = [$d, $e]; |
||||
$invalid_dict = new PyDict([ |
||||
1 => "123", |
||||
]); |
||||
$_ = $invalid_dict->__getitem__("one"); |
||||
$_ = $invalid_dict->get("one"); |
||||
$filled_dict = new PyDict([ |
||||
"one" => 1, |
||||
"two" => 2, |
||||
"three" => 3, |
||||
]); |
||||
$_ = PyCore::list($filled_dict->keys()); |
||||
$_ = PyCore::list($filled_dict->keys()); |
||||
$_ = PyCore::list($filled_dict->values()); |
||||
$_ = PyCore::list($filled_dict->values()); |
||||
$_ = $filled_dict->__contains__("one"); |
||||
$_ = $filled_dict->__contains__(1); |
||||
$empty_set = PyCore::set(); |
||||
$some_set = new PySet([1, 1, 2, 2, 3, 4]); |
||||
$other_set = new PySet([3, 4, 5, 6]); |
||||
$filled_set = new PySet([1, 2, 3]); |
||||
$_ = $operator->bitand($filled_set , $other_set); |
||||
$_ = $operator->bitor($filled_set , $other_set); |
||||
$_ = new PySet([1, 2, 3, 4]) - new PySet([2, 3, 5]); |
||||
$_ = $operator->bitxor(new PySet([1, 2, 3, 4]) , new PySet([2, 3, 5])); |
||||
$_ = new PySet([1, 2]) >= new PySet([1, 2, 3]); |
||||
$_ = new PySet([1, 2]) <= new PySet([1, 2, 3]); |
||||
if ($some_var > 10) { |
||||
PyCore::print("some_var is totally bigger than 10."); |
||||
} else { |
||||
if ($some_var < 10) { |
||||
PyCore::print("some_var is smaller than 10."); |
||||
} else { |
||||
PyCore::print("some_var is indeed 10."); |
||||
} |
||||
|
||||
} |
||||
|
||||
$__iter = PyCore::iter(new PyList(["dog", "cat", "mouse"])); |
||||
while($current = PyCore::next($__iter)) { |
||||
$animal = $current; |
||||
PyCore::print(PyCore::str("{} is a mammal")->format($animal)); |
||||
} |
||||
$__iter = PyCore::iter(PyCore::range(4)); |
||||
while($current = PyCore::next($__iter)) { |
||||
$i = $current; |
||||
PyCore::print($i); |
||||
} |
||||
$animals = new PyList(["dog", "cat", "mouse"]); |
||||
$__iter = PyCore::iter(PyCore::enumerate($animals)); |
||||
while($current = PyCore::next($__iter)) { |
||||
[$i, $value] = $current; |
||||
PyCore::print($i, $value); |
||||
} |
||||
$x = 0; |
||||
while($x < 4) { |
||||
PyCore::print($x); |
||||
$x += 1; |
||||
} |
||||
try { |
||||
throw $builtins->IndexError("This is an index error"); |
||||
} catch(PyError $e) { |
||||
if (PyCore::isinstance($e, $builtins->IndexError)) { |
||||
throw $builtins->IndexError("This is an index error"); |
||||
} elseif (PyCore::isinstance($e, new PyTuple([$builtins->TypeError, $builtins->NameError]))) { |
||||
throw $builtins->IndexError("This is an index error"); |
||||
} else { |
||||
throw $e; |
||||
} |
||||
} finally { |
||||
PyCore::print("We can clean up resources here"); |
||||
} |
||||
$f__object = PyCore::open("myfile.txt"); |
||||
$f = $f__object->__enter__(); |
||||
try { |
||||
$__iter = PyCore::iter($f); |
||||
while($current = PyCore::next($__iter)) { |
||||
$line = $current; |
||||
PyCore::print($line); |
||||
} |
||||
} finally { |
||||
$f__object->__exit__(); |
||||
} |
||||
|
||||
$contents = new PyDict([ |
||||
"aa" => 12, |
||||
"bb" => 21, |
||||
]); |
||||
$file__object = PyCore::open("myfile1.txt", "w+"); |
||||
$file = $file__object->__enter__(); |
||||
try { |
||||
$file->write(PyCore::str($contents)); |
||||
} finally { |
||||
$file__object->__exit__(); |
||||
} |
||||
|
||||
$file__object = PyCore::open("myfile2.txt", "w+"); |
||||
$file = $file__object->__enter__(); |
||||
try { |
||||
$file->write($json->dumps($contents)); |
||||
} finally { |
||||
$file__object->__exit__(); |
||||
} |
||||
|
||||
$file__object = PyCore::open("myfile1.txt", "r+"); |
||||
$file = $file__object->__enter__(); |
||||
try { |
||||
$contents = $file->read(); |
||||
} finally { |
||||
$file__object->__exit__(); |
||||
} |
||||
|
||||
PyCore::print($contents); |
||||
$file__object = PyCore::open("myfile2.txt", "r+"); |
||||
$file = $file__object->__enter__(); |
||||
try { |
||||
$contents = $json->load($file); |
||||
} finally { |
||||
$file__object->__exit__(); |
||||
} |
||||
|
||||
PyCore::print($contents); |
||||
$filled_dict = new PyDict([ |
||||
"one" => 1, |
||||
"two" => 2, |
||||
"three" => 3, |
||||
]); |
||||
$our_iterable = $filled_dict->keys(); |
||||
PyCore::print($our_iterable); |
||||
$__iter = PyCore::iter($our_iterable); |
||||
while($current = PyCore::next($__iter)) { |
||||
$i = $current; |
||||
PyCore::print($i); |
||||
} |
||||
$our_iterable->__getitem__(1); |
||||
$our_iterator = PyCore::iter($our_iterable); |
||||
PyCore::next($our_iterator); |
||||
PyCore::next($our_iterator); |
||||
PyCore::next($our_iterator); |
||||
PyCore::next($our_iterator); |
||||
$our_iterator = PyCore::iter($our_iterable); |
||||
$__iter = PyCore::iter($our_iterator); |
||||
while($current = PyCore::next($__iter)) { |
||||
$i = $current; |
||||
PyCore::print($i); |
||||
} |
||||
PyCore::list($our_iterable); |
||||
PyCore::list($our_iterator); |
||||
|
||||
function add($x, $y) { |
||||
PyCore::print(PyCore::str("x is {} and y is {}")->format($x, $y)); |
||||
return $x + $y; |
||||
} |
||||
|
||||
|
||||
add(5, 6); |
||||
add(y: 6, x: 5); |
||||
|
||||
function varargs(...$args) { |
||||
return $args; |
||||
} |
||||
|
||||
|
||||
varargs(1, 2, 3); |
||||
Loading…
Reference in new issue