调研 “图片提取文字描述”
调研 “图片提取文字描述”

调研 “图片提取文字描述”

需求

给一张图片,需要用文字描述该图片的特征,即将图片转为 prompt。

其他项目

Clip interrogator

需要的显示内存 (VRAM) 默认 6.3 GB,最少 2.7 GB,不知道显示内存如何查看,看这里

Linux

我用到的是 GPU:NVIDIA T4,想查看显卡的使用情况:
nvidia-smi
如果这个命令没有,那就可能没有装驱动。

Python

写一点其中涉及到的 Python 知识,在写代码时会引入包,如果管理呢?不要使用这个 pip freeze > requirements.txt ,这个会把本地安装的所有包保存起来。
正确做法:
# 在终端运行 pip install pipreqs pipreqs --force ./
生成的 requirements.txt 文件内容只会涉及到你代码使用到的包。
如果想通过 requirements.txt 安装呢,使用如下:
pip install -r requirements.txt

Error

  1. ConnectionResetError: [Errno 104] Connection reset by peer
    1. pip3 install transformers==4.26.1
  1. (优化)Flavor chain too slow
    1. 将这句 ci = Interrogator(Config(clip_model_name="ViT-L-14/openai")) 初始化一次,不需要每次识别图片时都执行。

演示

不同项目对于该图片的演示效果。
No.
Image
CLIP-Interrogato (best)
图文案
https://imgs.iwaty.com/uploads/public/640/58c/665/64058c6650695706138202.jpg?x-oss-process=image%2Fresize%2Cw_344%2Ch_420%2Fquality%2Cq_80%2Fwatermark%2Cimage_dXBsb2Fkcy9wdWJsaWMvNjMxLzk2My82ODYvNjMxOTYzNjg2MzVmODU5MTc3NDQwOS5wbmc_eC1vc3MtcHJvY2Vzcz1pbWFnZS9yZXNpemUsUF8xMg%2Cg_ne%2Ct_50%2Cx_10%2Cy_10%2Cvoffset_0
a woman holding a black purse in front of a white wall, ebay website, polka dot, cool japanese street fashion, wearing a blouse, up face with 1 9 2 0 s hairstyle, tiny waist, green shirt, cute top, loosely cropped, inspired by Muqi, flying black marble balls, head to waist, see, tokyo fashion.
a woman holding a black purse and wearing a polka dot blouse, frilled blooming collar, loose - fitting blouses, high-waist-black-skirt, cropped wide sleeve, polka dot, round-cropped, off the shoulder shirt, romanticist style, ornate frilly regal shirt, wearing a blouse, button - up shirt, polkadots.
https://imgs.iwaty.com/uploads/public/63e/b3b/9a3/63eb3b9a39522859252376.jpg?x-oss-process=image%2Fresize%2Cw_344%2Ch_420%2Fquality%2Cq_80%2Fwatermark%2Cimage_dXBsb2Fkcy9wdWJsaWMvNjMxLzk2My82ODYvNjMxOTYzNjg2MzVmODU5MTc3NDQwOS5wbmc_eC1vc3MtcHJvY2Vzcz1pbWFnZS9yZXNpemUsUF8xMg%2Cg_ne%2Ct_50%2Cx_10%2Cy_10%2Cvoffset_0
a woman standing in front of a white wall, ebay website, tokyo fashion, is tall, wearing dresses, utilitarian, inspired by Muqi, style of angela deane, most blues, wearing a baggy, stripe over eye, in 2 0 1 8, wearing a nightgown, wide belt, dark washed tint black, warm and soft and subdued colors
a woman standing in front of a white wall, long dress with apron, loose - fitting blouses, vintage dress, ffffound, long dress female, long tunic, dress in voile, peasant dress, romanticist style, voluminous sleeves, wearing a white folkdrakt dress, linen, wide leg hakama trousers, long blue-cape
https://imgs.iwaty.com/uploads/public/62b/292/60c/62b29260c872a360694571.jpg?x-oss-process=image/watermark,image_dXBsb2Fkcy9wdWJsaWMvNjMxLzk2My82ODYvNjMxOTYzNjg2MzVmODU5MTc3NDQwOS5wbmc_eC1vc3MtcHJvY2Vzcz1pbWFnZS9yZXNpemUsUF8xMg,g_ne,t_50,x_10,y_10,voffset_0/quality,q_100/format,webp
a dress and purse sitting on top of a table, isometry, indian flag, loputyn and matcha, twen magazine, swirling magical energy, elegant up to the elbow, worth1000.com, patterned scarf, inspired by Diego Giacometti, half - body, sleeveless tops, green and white, pixvy, store
a green dress with fish on it next to a purse, idian dress, dress of leaves, green tunic, long dress with apron, female figure in maxi dress, full body green dress, belly free teal dress, elegant tropical prints, luxury dress, green and blue color scheme, sea - green and white clothes, tattered green dress, long dress female
https://imgs.iwaty.com/uploads/public/63f/f11/8fd/63ff118fdb36d722004732.jpg?x-oss-process=image%2Fresize%2Cw_400%2Ch_400%2Ctype_4%2Fquality%2Cq_80%2Fwatermark%2Cimage_dXBsb2Fkcy9wdWJsaWMvNjMxLzk2My82ODYvNjMxOTYzNjg2MzVmODU5MTc3NDQwOS5wbmc_eC1vc3MtcHJvY2Vzcz1pbWFnZS9yZXNpemUsUF8xMg%2Cg_ne%2Ct_50%2Cx_10%2Cy_10%2Cvoffset_0
a woman wearing a t - shirt with the word pindy on it, trending pinterest, tokyo fashion, above the waist, neutral colours, elegant girl in urban outfit, she is about 2 0 years old, large dark eyes, wearing vr, vogue photo style, no makeup, grey, police, wet drapery
a woman wearing a t - shirt with the word pindy on it, cute casual streetwear, black oversized clothes, tokyo fashion, wearing a t-shirt, neutral dull colors, wear's beige shirt, white poet shirt, fear of god style, street fashion outfit, streetwear fashion, cool japanese street fashion, japanese city street fashion, grey clothes
https://imgs.iwaty.com/uploads/public/624/fd2/577/624fd25773669146464101.jpg?x-oss-process=image%2Fresize%2Cw_400%2Ch_400%2Ctype_4%2Fquality%2Cq_80%2Fwatermark%2Cimage_dXBsb2Fkcy9wdWJsaWMvNjMxLzk2My82ODYvNjMxOTYzNjg2MzVmODU5MTc3NDQwOS5wbmc_eC1vc3MtcHJvY2Vzcz1pbWFnZS9yZXNpemUsUF8xMg%2Cg_ne%2Ct_50%2Cx_10%2Cy_10%2Cvoffset_0
a dress sitting on top of a table next to a purse, black peonies, midi skirt, by INO, long tie, half - body, chrysanthemums, item, inverted triangle body type, full-length, paint style, best selling, dress of leaves, black scarf, with two front pockets, textures.com, hip-length, girly
a woman's dress and shoes on a white surface, long dress with apron, feminine girly dress, black knee - length skirt, floral art novuea dress, sophisticated dress, high-waist-black-skirt, midi skirt, romantic dress, vine dress, dark black long dress, long dress female, dress and cloth, stylish dress, beautiful clothes
总结,使用 CLIP 生成的结果会总结额外的描述,原因主要在用模型的选择上,如图:
https://huggingface.co/spaces/pharma/CLIP-Interrogator
上面的比对数据选择的是 best 方式,我尝试选择了 classic ,前后对比结果:
notion image
  • Best: a woman standing in front of a white wall, ebay website, tokyo fashion, is tall, wearing dresses, utilitarian, inspired by Muqi, style of angela deane, most blues, wearing a baggy, stripe over eye, in 2 0 1 8, wearing a nightgown, wide belt, dark washed tint black, warm and soft and subdued colors.
  • Classic: a woman standing in front of a white wall, a digital rendering inspired by Adélaïde Labille-Guiard, trending on pinterest, orphism, long dress with apron, loose - fitting blouses, vintage dress.
 

CLIP interrogate 背景图片验证

在识别图片时,对主体图片之外的背景如何描述,测试几组图片,选择了 best 方式。
notion image
there is a woman standing on a set of stairs wearing a dress, cute huge pockets hardware, trending on r/streetwear, inspired by Dong Qichang, long shirt, wearing big black circle glasses, the photo shows a large, trending on artforum, skinny male mage, blue tunic, erika ikuta, long boi
 
 
notion image
there is a woman that is standing in front of a refrigerator, butterfly embroidery, white trendy clothes, loose fantasy clothing, 1 : 1 brutal design, left ear, oversized, backshot, grafitti paint, bad print, tied - up shirt, tafy, tail
 
notion image
woman wearing a blue dress and a gray jacket standing next to a vase, very beautiful elven top model, taoist robe, images on the store website, palette is black violet gray red, bladed wings lace wear, in a black chiffon layered robe, pastel faded grey rainbow, cardigan, catalog photo
notion image
arafy women's long sleeved cardigan sweaters, dark brown duster, cardigan, wearing in cardigan, brown jacket with long sleeves, wearing a cardigan, technological longcoat, elegant cape, light brown coat, grey turtleneck coat, wearing a duster coat, long cape, brown hooded cloak, long tunic, grey tarnished longcoat, brown tuffle coat
notion image
arafed woman in a dress and hat taking a picture of herself, thom browne, greta thunberg, prison jumpsuit, miura kentaro style, subtle earthy tones, absolute chad, japanesse farmer, milan fashion week backstage, wearing a baggy pajamas, thisset style, inspired by Elfriede Lohse-Wächtler, tall
notion image
arafed woman standing in a room with a chair and a table, baggy pants, inspired by Wang Wu, fine image on the store website, jumpsuit, lined in cotton, big oatmeal, but minimalist, sheath, 2 0 2 0 fashion, ulzzang, garments
 
notion image
arafed woman in a black dress and black sandals standing in front of a window, cool japanese street fashion, carrying big sack, inspired by Walasse Ting, long shirt, wearing an oversized hoodie, with a long, androgynous, a very tall, navy blue, lossless, linen
 
notion image
there is a woman standing in a room with a purse, black turtleneck, 128k, long dress, cotton fabric, jumpsuit, low colour, bust with a very long neck, korean girl, wearing stripe shirt, iso : 2 0 0, ( ( ( black witch hat ) ) ) ), android 18
notion image
there is a woman standing in a room with a bag, short flat hourglass slim figure, sleeveless turtleneck, images on the store website, korean hanbok, waistcoat, sexy style, very long neck, iamag, heavy grain-s 150, ipseity, saint woman