教你如何玩转Modelscope (一、图片编辑与描述生成)

本文涉及的产品
模型训练 PAI-DLC,5000CU*H 3个月
模型在线服务 PAI-EAS,A10/V100等 500元 1个月
交互式建模 PAI-DSW,每月250计算时 3个月
简介: 本文通过一个有趣的图片处理例子,教你如何利用modelscope强大且多样的模型能力去解锁你专属的图片编辑能力。

简介:

本文将会用到2个视觉类相关的模型:达摩卡通化模型达摩人像抠图模型分别对一张图片进行处理,并最终利用一个多模态的模型:达摩图片英文描述生成模型对处理前后的图片进行描述,让你一键解锁快速图片编辑以及图片标题的能力。


操作:

参考快速开始,里面有一些关于ModelScope的基本介绍


环境准备:

  • 为了更快的体验产品,使用了ModelScope提供的远程环境,即Notebook进行开发,更加便捷。
  • 如果本地测试,也可以先安装依赖:
# 深度学习依赖pip install torch torchvision torchaudio
pip install --upgrade tensorflow
# modelscope模型依赖pip install "modelscope[cv]"-f https://modelscope.oss-cn-beijing.aliyuncs.com/releases/repo.html
pip install "modelscope[multi-modal]"-f https://modelscope.oss-cn-beijing.aliyuncs.com/releases/repo.html

模型准备:

本文涉及到的模型有3个,包括:

达摩卡通化模型:https://modelscope.cn/#/models/damo/cv_unet_person-image-cartoon_compound-models/summary

达摩人像抠图模型: https://modelscope.cn/#/models/damo/cv_unet_image-matting/summary

以及达摩图片英文描述生成模型: https://modelscope.cn/#/models/damo/ofa_image-caption_coco_large_en/summary


前两个模型是cv模型因此安装 modelscope[cv] ,第三个是涉及到图片和文本的多模态模型因此需要安装 modelscope[multi-modal]



模型调试:

首先我们选择一张美丽好看的图片作为这次处理的目标:https://modelscope.oss-cn-beijing.aliyuncs.com/test/images/image_cartoon.png

image_cartoon.png

达摩卡通化模型,将图片卡通化:

importcv2frommodelscope.hub.snapshot_downloadimportsnapshot_downloadfrommodelscope.pipelinesimportpipelinemodel_dir=snapshot_download('damo/cv_unet_person-image-cartoon_compound-models', cache_dir='.')
img_cartoon=pipeline('image-portrait-stylization', model=model_dir)
result=img_cartoon('https://modelscope.oss-cn-beijing.aliyuncs.com/test/images/image_cartoon.png')
cv2.imwrite('result.png', result['output_img'])
print('finished!')

从notebook中获得如下结果

2022-08-1115:01:43,132-modelscope-INFO-File .DS_Storealreadyincache, skipdownloading!2022-08-1115:01:43,134-modelscope-INFO-FileREADME.mdalreadyincache, skipdownloading!2022-08-1115:01:43,136-modelscope-INFO-Filealpha.jpgalreadyincache, skipdownloading!2022-08-1115:01:43,139-modelscope-INFO-Filecartoon_anime_bg.pbalreadyincache, skipdownloading!2022-08-1115:01:43,141-modelscope-INFO-Filecartoon_anime_h.pbalreadyincache, skipdownloading!2022-08-1115:01:43,143-modelscope-INFO-Fileconfiguration.jsonalreadyincache, skipdownloading!2022-08-1115:01:43,147-modelscope-INFO-Filedemo.gifalreadyincache, skipdownloading!2022-08-1115:01:43,149-modelscope-INFO-Filedetector.pbalreadyincache, skipdownloading!2022-08-1115:01:43,151-modelscope-INFO-Filekeypoints.pbalreadyincache, skipdownloading!2022-08-1115:01:43,155-modelscope-INFO-initiatemodelfrom ./damo/cv_unet_person-image-cartoon_compound-models2022-08-1115:01:43,156-modelscope-INFO-initiatemodelfromlocation ./damo/cv_unet_person-image-cartoon_compound-models.
2022-08-1115:01:43,523-modelscope-INFO-loadingmodelfrom ./damo/cv_unet_person-image-cartoon_compound-models/cartoon_anime_h.pb2022-08-1115:01:43,583-modelscope-INFO-loadmodel ./damo/cv_unet_person-image-cartoon_compound-models/cartoon_anime_h.pbdone.
2022-08-1115:01:43,584-modelscope-INFO-loadingmodelfrom ./damo/cv_unet_person-image-cartoon_compound-models/cartoon_anime_bg.pb2022-08-1115:01:43,638-modelscope-INFO-loadmodel ./damo/cv_unet_person-image-cartoon_compound-models/cartoon_anime_bg.pbdone.
finished!

可以在notebook任务栏中查看图片结果

image.png

image.png

是不是很有趣?


达摩人像抠图模型,将人像抠出来

接着上面的步骤,我们获得如下抠出来的结果

img_matting=pipeline(Tasks.portrait_matting,model='damo/cv_unet_image-matting')
result=img_matting('https://modelscope.oss-cn-beijing.aliyuncs.com/test/images/image_cartoon.png')
cv2.imwrite('result1.png', result[OutputKeys.OUTPUT_IMG])
2022-08-1115:12:03,172-modelscope-INFO-FileREADME.mdalreadyincache, skipdownloading!2022-08-1115:12:03,174-modelscope-INFO-Fileconfiguration.jsonalreadyincache, skipdownloading!2022-08-1115:12:03,176-modelscope-INFO-Filetf_graph.pbalreadyincache, skipdownloading!2022-08-1115:12:03,181-modelscope-INFO-initiatemodelfrom/mnt/workspace/.cache/modelscope/damo/cv_unet_image-matting2022-08-1115:12:03,181-modelscope-INFO-initiatemodelfromlocation/mnt/workspace/.cache/modelscope/damo/cv_unet_image-matting.
2022-08-1115:12:03,190-modelscope-INFO-loadingmodelfrom/mnt/workspace/.cache/modelscope/damo/cv_unet_image-matting/tf_graph.pb2022-08-1115:12:03,444-modelscope-INFO-loadmodeldoneTrue

result1.png


至此,我们有一张图的三种不同的样式了,最后送到我们的英文描述模型里看看效果比较一下吧。

达摩图片英文描述生成模型

注意,由于是多模态模型比较大,所以第一次使用的时候下载速度可能会比较慢,稍等几分钟即可,包括推理时间也较为长,

img_captioning=pipeline(
Tasks.image_captioning,
model='damo/ofa_image-caption_coco_large_en')
image_list= [
    {'image': 'result.png'},
    {'image': 'result1.png'},
    {'image': 'https://modelscope.oss-cn-beijing.aliyuncs.com/test/images/image_cartoon.png'}
]
result=img_captioning(image_list)
print(result)
2022-08-1115:40:03,932-modelscope-INFO-FileREADME.mdalreadyincache, skipdownloading!2022-08-1115:40:03|INFO|modelscope|FileREADME.mdalreadyincache, skipdownloading!2022-08-1115:40:03,933-modelscope-INFO-Fileconfig.jsonalreadyincache, skipdownloading!2022-08-1115:40:03|INFO|modelscope|Fileconfig.jsonalreadyincache, skipdownloading!2022-08-1115:40:03,934-modelscope-INFO-Fileconfiguration.jsonalreadyincache, skipdownloading!2022-08-1115:40:03|INFO|modelscope|Fileconfiguration.jsonalreadyincache, skipdownloading!2022-08-1115:40:03,934-modelscope-INFO-Filemerges.txtalreadyincache, skipdownloading!2022-08-1115:40:03|INFO|modelscope|Filemerges.txtalreadyincache, skipdownloading!2022-08-1115:40:03,935-modelscope-INFO-Filepytorch_model.binalreadyincache, skipdownloading!2022-08-1115:40:03|INFO|modelscope|Filepytorch_model.binalreadyincache, skipdownloading!2022-08-1115:40:03,936-modelscope-INFO-Fileleaderboard.pngalreadyincache, skipdownloading!2022-08-1115:40:03|INFO|modelscope|Fileleaderboard.pngalreadyincache, skipdownloading!2022-08-1115:40:03,936-modelscope-INFO-Fileofa_caption_score.pngalreadyincache, skipdownloading!2022-08-1115:40:03|INFO|modelscope|Fileofa_caption_score.pngalreadyincache, skipdownloading!2022-08-1115:40:03,937-modelscope-INFO-Fileofa_frame.pngalreadyincache, skipdownloading!2022-08-1115:40:03|INFO|modelscope|Fileofa_frame.pngalreadyincache, skipdownloading!2022-08-1115:40:03,938-modelscope-INFO-Fileofa_image_captioning_en.pngalreadyincache, skipdownloading!2022-08-1115:40:03|INFO|modelscope|Fileofa_image_captioning_en.pngalreadyincache, skipdownloading!2022-08-1115:40:03,938-modelscope-INFO-Filetokenizer.jsonalreadyincache, skipdownloading!2022-08-1115:40:03|INFO|modelscope|Filetokenizer.jsonalreadyincache, skipdownloading!2022-08-1115:40:03,939-modelscope-INFO-Filevocab.jsonalreadyincache, skipdownloading!2022-08-1115:40:03|INFO|modelscope|Filevocab.jsonalreadyincache, skipdownloading!2022-08-1115:40:03,945-modelscope-INFO-initiatemodelfrom/mnt/workspace/.cache/modelscope/damo/ofa_image-caption_coco_large_en2022-08-1115:40:03|INFO|modelscope|initiatemodelfrom/mnt/workspace/.cache/modelscope/damo/ofa_image-caption_coco_large_en2022-08-1115:40:03,946-modelscope-INFO-initiatemodelfromlocation/mnt/workspace/.cache/modelscope/damo/ofa_image-caption_coco_large_en.
2022-08-1115:40:03|INFO|modelscope|initiatemodelfromlocation/mnt/workspace/.cache/modelscope/damo/ofa_image-caption_coco_large_en.
2022-08-1115:40:03,951-modelscope-INFO-initializemodelfrom/mnt/workspace/.cache/modelscope/damo/ofa_image-caption_coco_large_en2022-08-1115:40:03|INFO|modelscope|initializemodelfrom/mnt/workspace/.cache/modelscope/damo/ofa_image-caption_coco_large_en2022-08-1115:40:12,260-modelscope-INFO-initializemodelfrom/mnt/workspace/.cache/modelscope/damo/ofa_image-caption_coco_large_en2022-08-1115:40:12|INFO|modelscope|initializemodelfrom/mnt/workspace/.cache/modelscope/damo/ofa_image-caption_coco_large_enTruncationwasnotexplicitlyactivatedbut `max_length` isprovidedaspecificvalue, pleaseuse `truncation=True` toexplicitlytruncateexamplestomaxlength. Defaultingto'longest_first'truncationstrategy. Ifyouencodepairsofsequences (GLUE-style) withthetokenizeryoucanselectthisstrategymorepreciselybyprovidingaspecificstrategyto `truncation`.
[{'caption': 'a woman standing in front of a car with her hand out', 'samples': [{'image': 'result.png'}], 'text': None, 'boxes': None, 'labels': None, 'scores': None}, {'caption': 'a woman standing next to a car with her hand outstretched', 'samples': [{'image': 'result1.png'}], 'text': None, 'boxes': None, 'labels': None, 'scores': None}, {'caption': 'a woman standing next to a car with her hand outstretched', 'samples': [{'image': 'https://modelscope.oss-cn-beijing.aliyuncs.com/test/images/image_cartoon.png'}], 'text': None, 'boxes': None, 'labels': None, 'scores': None}]


结果:

比对下来原图的描述为:

a woman standing next to a car with her hand outstretched

动画图的描述为:

a woman standing in front of a car with her hand out

抠图后的描述为:

a woman standing next to a car with her hand outstretched


惊奇的发现抠图的结果居然和原图的描述一样,这里可以看出抠图确实真真实实的扣出了人像,但是还保留原图的信息。用户甚至可以在图片软件中还原出来原图。


同时,动画效果展示也不错的情况下,描述也惊人的和原图一致,只是手部动作描述略有不同。


看完了这个操作,是不是感觉很有意思?要不要打开你的脑洞一起来玩呦~


相关实践学习
借助OSS搭建在线教育视频课程分享网站
本教程介绍如何基于云服务器ECS和对象存储OSS,搭建一个在线教育视频课程分享网站。
相关文章
|
4月前
|
人工智能 开发者
基于ModelScope 实现图片 千变万换 ReplaceAnything
【7月更文挑战第17天】基于ModelScope 实现图片 千变万换 ReplaceAnything
|
6月前
|
存储 人工智能 算法
基于向量检索服务与ModelScope模型搭建文本搜图片---魏红斌版
【1月更文挑战第9天】综合产品理解和实操经验,总结向量检索服务的综合水平
98961 4
基于向量检索服务与ModelScope模型搭建文本搜图片---魏红斌版
|
数据采集 机器学习/深度学习 自然语言处理
ModelScope模型库体验之根据图像生成英文描述
ModelScope模型库体验之根据图像生成英文描述
798 0
ModelScope模型库体验之根据图像生成英文描述
|
6月前
|
自然语言处理
在ModelScope中,你可以通过设置模型的参数来控制输出的阈值
在ModelScope中,你可以通过设置模型的参数来控制输出的阈值
155 1
|
6月前
|
API 语音技术
ModelScope-FunASR**有支持热词又支持时间戳的模型**。
【2月更文挑战第30天】ModelScope-FunASR**有支持热词又支持时间戳的模型**。
211 2
|
6月前
|
人工智能 API 决策智能
Modelscope结合α-UMi:基于Modelscope的多模型协作Agent
基于单个开源小模型的工具调用Agent,由于模型容量和预训练能力获取的限制,无法在推理和规划、工具调用、回复生成等任务上同时获得比肩大模型等性能。
|
6月前
|
文字识别 并行计算 语音技术
ModelScope问题之下载模型文件报错如何解决
ModelScope模型报错是指在使用ModelScope平台进行模型训练或部署时遇到的错误和问题;本合集将收集ModelScope模型报错的常见情况和排查方法,帮助用户快速定位问题并采取有效措施。
800 3
|
6月前
|
人工智能 达摩院 自然语言处理
超好用的开源模型平台,ModelScope阿里达摩院
超好用的开源模型平台,ModelScope阿里达摩院
456 1
|
3月前
|
人工智能 开发框架 物联网
赢万元奖金 | 第七届CCF开源创新大赛ModelScope开源模型应用挑战赛开启报名!
第七届CCF开源创新大赛(后简称“大赛”) 由中国计算机学会(CCF)主办,长沙理工大学、CCF开源发展委员会联合承办。
|
6月前
|
API
使用ModelScope平台进行模型验证时
【2月更文挑战第9天】使用ModelScope平台进行模型验证时
167 4