库存引擎产品CLIPQdrant(图像库存产品引擎是一个)
分类:时时快讯
30
本文翻译自:“Automatic Product Inventory and Recommendation Engine Using CLIP and Qdrant”原文地址:https://www.superteams.ai/blog/automatic-product-inventory-and-recommendation-engine-using-clip-and-qdrant假设您是一个电子商务平台,库存中有许多不同类别的产品每次新产品进入您的库存时,您都希望它自动分类到预定义的类别之一此外,您还想知道您的库存中有哪些类似的产品,以便跟踪您拥有的商品这种类型的系统对于电子商务平台非常有用,因为它简化了产品分类流程,节省时间和人力,并实现高效的库存管理此外,通过识别类似产品,您可以更好地了解您的产品分类,识别潜在的差距或冗余,并就产品采购、定价和促销做出明智的决策通过这种方式,您可以优化库存水平,最大限度地减少缺货和库存过剩在 Superteams.ai,我们决定接受这个问题陈述并创建一个人工智能驱动的解决方案来解决它我们设计了一个系统,将产品图像分类为各自的类别,然后建议库存中已有的类似产品为了实现这一目标,我们利用了两项尖端技术:OpenAI 的 CLIP 模型和 Qdrant 矢量数据库OpenAI 的 CLIP 模型OpenAI 的 CLIP(对比语言图像预训练)模型是人工智能领域的游戏规则改变者这个强大的模型在共享向量空间中生成文本和图像嵌入,从而实现文本概念和视觉表示之间的无缝映射神奇之处在于该模型能够将语义相似的文本和图像紧密地放置在向量空间内这意味着图像及其相应的文本描述自然对齐,使得将图像与其各自的类别相匹配变得非常容易CLIP 如何嵌入不同图像和文本的演示Qdrant虽然 CLIP 模型为我们的解决方案奠定了基础,但 Qdrant 矢量数据库将其提升到了一个新的水平Qdrant 是一个开源数据库,专门用于以卓越的速度和效率存储和检索向量嵌入及其相关元数据其先进的相似性搜索功能使我们能够快速探索广阔的向量空间并从数据库语料库中检索最相关的信息通过利用 Qdrant,我们可以根据存储的向量嵌入快速查找用户查询这意味着,当上传新产品图片时,我们的系统可以快速识别您的库存中已有的最相似的产品,使您能够就产品分类和库存管理做出明智的决策工作流程我们使用了Kaggle 的时尚产品数据集该数据集包含约 44,000 张各种时尚产品及其各自产品类别的图像大约有143种类别它们看起来像这样:短裤裤子雨裤毛衣纱丽服耸耸肩运动凉鞋…….手镯沐浴露和磨砂膏袖珍的树干男士美容套装拳击手连衣裤遮瑕膏除臭剂帽子高跟鞋头巾钱包免费礼品第一步是创建这些类别的 CLIP 嵌入,并将它们存储在 Qdrant 中名为“文本嵌入(Text-Embeddings)”的集合中然后,对于我们的推荐系统,我们正在创建另一个名为“图像嵌入(Image-Embeddings)”的集合我们将计算所有 44,000 个产品图像的嵌入并将它们存储在集合中用户上传产品图像,我们计算图像的嵌入将此查询嵌入与“Text-Embeddings”集合的相似性搜索产生产品上传图像的类别使用“Image-Embeddings”集合对查询嵌入进行相似性搜索,给出与上传的产品图像相似的库存产品图像工作流程图让我们看看代码安装所需的软件包
pip install torch transformers qdrant-client gradio pillow pandas kaggle
下载数据集:kaggle datasets download -d paramaggarwal/fashion-product-images-small
提取产品类别import pandas as pd# Replace 'path_to_csv' with the actual path to your CSV filecsv_file_path = 'styles.csv'# Read the CSV file into a DataFramedf = pd.read_csv(csv_file_path, on_bad_lines='skip')# Now 'df' holds the DataFrame object with the data from the CSV fileprint(df)Item_List = list(set(df['articleType'].tolist()))
加载 CLIP 模型:from transformers import CLIPProcessor, CLIPModelimport torchmodel_id = "openai/clip-vit-base-patch32"processor = CLIPProcessor.from_pretrained(model_id)model = CLIPModel.from_pretrained(model_id)# move model to device if possibledevice = 'cuda' if torch.cuda.is_available() else 'cpu'model.to(device)
计算类别文本的嵌入:tokens = processor( text=Item_List, padding=True, images=None, return_tensors='pt').to(device)tokens.keys()text_emb = model.get_text_features( tokens)text_emb_list = text_emb.detach().cpu().numpy().tolist()
在本地主机上启动 Qdrant 实例:docker run -p 6333:6333 -p 6334:6334 \ -v $(pwd)/qdrant_storage:/qdrant/storage:z \ qdrant/qdrant
将嵌入更新插入到名为“text_embeddings”的集合中我们还添加了有效负载元数据以及嵌入元数据包含产品类别的文本标签稍后我们将使用此元数据来检索产品类别from qdrant_client import QdrantClient, modelsclient = QdrantClient(url="http://localhost:6333")client.create_collection( collection_name="text_embeddings", vectors_config=models.VectorParams(size=512, distance=models.Distance.COSINE),)# Convert list into a list of dictionariespayload = [{'itemtype': item} for item in Item_List]# Show the resultprint(payload)# Create a list of IDs from 1 to the length of the items listid_list = [i for i in range(1, len(Item_List) + 1)]# Show the resultprint(id_list)client.upsert( collection_name="text_embeddings", points=models.Batch( ids=id_list, payloads=payload, vectors=text_emb_list, ),)
接下来,让我们为图像嵌入创建另一个集合client.delete_collection(collection_name="image_embeddings")client.create_collection( collection_name="image_embeddings", vectors_config=models.VectorParams(size=512, distance=models.Distance.COSINE),)
接下来,我们创建一个 Pandas 数据框来组织图像嵌入和关联的元数据(图像的文件路径)import osimport torchfrom PIL import Imageimport pandas as pd# Define the directory where the images are storedimage_directory = 'images'# Initialize an empty list to store the datadata = []# Loop over all files in the image directoryfor i, filename in enumerate(os.listdir(image_directory)): if filename.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.gif')): # Add any other file types if needed # Construct the full path to the image file file_path = os.path.join(image_directory, filename) with Image.open(file_path) as img: # Prepare the image for the model tokens = processor( text=None, images=img, return_tensors="pt" )["pixel_values"].to(device) # Get image embeddings from the model image_embeddings = model.get_image_features( tokens ) # Append the filename and embeddings to the data list data.append((filename, image_embeddings.detach().cpu().numpy().tolist()[0]) # Print the iteration number print(f'Iteration {i}: Processed {filename}') # Create a DataFrame with the datadf = pd.DataFrame(data, columns=['Filename', 'ImageEmbeddings'])# Display the DataFrameprint(df)In [ ]:# Assuming 'df' is your existing DataFrame# Create a new 'ID' column that starts at 1 and increments by 1 for each rowdf['ID'] = range(1, len(df) + 1)# Display the updated DataFrameprint(df)# Create the list of dictionaries with "Filename" as the keyfilenames_list = [{'Filename': filename} for filename in df['Filename']]# Create the list of ImageEmbeddings (assuming ImageTensor is already a list of embeddings)image_embeddings_list = df['ImageEmbeddings'].tolist()# Create the list of IDsids_list = df['ID'].tolist()
由于我们有大量图像嵌入(44,000 个产品图像),因此最好将其以 1000 个批次更新插入到 VectorDB 中,以提高速度和效率batch_size = 1000total_points = len(ids_list)for start_index in range(0, total_points, batch_size): # End index is the start of the next batch or the end of the list end_index = min(start_index + batch_size, total_points) # Slice the lists to create the current batch batch_ids = ids_list[start_index:end_index] batch_filenames = filenames_list[start_index:end_index] batch_image_embeddings = image_embeddings_list[start_index:end_index] # Upsert the current batch client.upsert( collection_name="image_embeddings", points=models.Batch( ids=batch_ids, payloads=batch_filenames, vectors=batch_image_embeddings, ), )
我们现在将开始为 Gradio UI 编写函数第一个函数将 PIL 图像作为输入然后,它使用文本嵌入集合执行相似性搜索并返回顶部结果,这基本上是产品的类别def image_classifier(image): # Prepare the image for the model tokens = processor( text=None, images=image, return_tensors="pt" )["pixel_values"].to(device) # Get image embeddings from the model image_embeddings = model.get_image_features( tokens ) query_vector = image_embeddings.detach().cpu().numpy().tolist()[0] record = client.search( collection_name="text_embeddings", query_vector=query_vector, limit=1, ) return record[0].payload['itemtype']
第二个函数将 PIL 图像作为输入,并通过对图像嵌入集合执行相似性搜索,以文件路径列表的形式返回前十个图像(来自库存)def image_path_list(image): # Prepare the image for the model tokens = processor( text=None, images=image, return_tensors="pt" )["pixel_values"].to(device) # Get image embeddings from the model image_embeddings = model.get_image_features( tokens ) query_vector = image_embeddings.detach().cpu().numpy().tolist()[0] record = client.search( collection_name="image_embeddings", query_vector=query_vector, limit=10, ) return [('fashion-dataset/fashion-dataset/images/' + element.payload['Filename'], None) for element in record]
Gradio UI 的代码:import gradio as grwith gr.Blocks() as demo: with gr.Row(): upload_image = gr.Image(label= "Upload Your Image", type = 'pil') classifier_text = gr.Textbox(label= "Type of Item") with gr.Row(): image_gallery = gr.Gallery(label= "Similar items in the inventory", object_fit= 'contain', columns=[5], rows=[2],) with gr.Row(): clr_btn = gr.Button(value= "Clear") first_step = upload_image.upload(fn= image_classifier, inputs= upload_image, outputs= classifier_text) first_step.then(fn= image_path_list, inputs= upload_image, outputs = image_gallery) clr_btn.click(fn=lambda: (None, None, []), inputs=None, outputs=[upload_image, classifier_text, image_gallery]) demo.launch(share=True)
以下是代码的简要说明:1. `with gr.Blocks() as demo:` 语句创建一个名为 `demo` 的 Gradio 接口2. 在“demo”块内,有三个“gr.Row()”组件,每个组件代表用户界面中的一行3. 第一行:- `upload_image` 是一个 `gr.Image` 组件,允许用户上传图像它有一个标签“上传您的图像”并接受 PIL(Python 图像库)图像- `classifier_text` 是一个 `gr.Textbox` 组件,它根据图像分类显示项目的类型4. 第二行:- `image_gallery` 是一个 `gr.Gallery` 组件,用于显示库存中的类似项目它有一个标签“库存中的类似商品”,并配置为显示 5 列和 2 行图像5. 第三行:- `clr_btn` 是一个带有“Clear”标签的 `gr.Button` 组件6. “first_step”变量被赋值为“upload_image.upload()”的结果,当上传图片时会触发“image_classifier”函数上传的图像作为函数的输入传递,输出显示在“classifier_text”文本框中7. 在“image_classifier”函数完成后,“first_step.then()”语句将另一个函数调用链接到“image_path_list”它将上传的图像作为输入,并使用推荐图像路径的结果列表更新“image_gallery”8. `clr_btn.click()` 语句定义单击“清除”按钮时的行为它将 `upload_image`、`classifier_text` 和 `image_gallery` 组件设置为其默认值(分别为 None、None 和空列表)9. 最后,“demo.launch(share=True)”启动 Gradio 界面并使其可共享,允许其他人通过生成的 URL 访问它结果以下是我们的用户界面的一些屏幕截图:GitHub您可以访问此 Github 存储库中的代码:https://github.com/vardhanam/Product_Classifier_Recommendation/tree/main
(图片来自网络侵删)
上一篇:欧曼自卸车的取力器有几个开关?
相关文章
猜你喜欢
-
人工智能成新新功能应用程序再添中生(图像人工智能功能新功能应用程序)
在向Photoshop添加生成式AI驱动的编辑功能近一年后,Adobe正在为其旗舰产品提供更多AI周二,该公司宣布Photoshop能够直接在应用程序中生成带有简单文本提示的图像还有一些新功能可以让AI从参考图像中汲取灵感,以创建新图像并更轻松地生成背景Adobe认为,这些工具...
-
我用秀色美人试伊妆GANPS(妆容图像矩阵我用秀色)
选自arXiv作者:WentaoJiang等机器之心编译参与:Panda我们有时候可能会想知道如果将其他人的妆容放在自己脸上会是怎样现在,不需要耗费时间学习化妆技巧以及花钱购买化妆品,借助深度生成模型,我们就能轻松尝试别人的妆容效果近日,北京航空航天大学和中国科学院大学等机构的研究者新提出的姿态稳...
-
技术如何实现原理AI(技术图像特征如何实现原理)
在这个当下的数字化时代,AI人脸识别技术备受瞩目,广泛应用它不仅可以增强安全性,还能为我们提供极其便捷的用户体验那么,你知道AI人脸识别技术是如何实现的吗?让我来带你一起探索其原理和应用吧技术原理:1.图像采集是AI人脸识别技术的第一步,通过摄像头或其他成像设备获取人脸图像确保采集到清晰、准确的人脸...
全部评论(0)
评论
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。
最新发布
-
200多家公司脱手港股年内回购逾1500亿港元
2024-07-31 -
南山团体 发起部分要约收购 连续看好恒通股份将来 发展
2024-07-31 -
万科拟向苏州高新地产出售合资公司50%股权
2024-07-31 -
小摩下调诺瓦瓦克斯医药评级至“减持”
2024-07-31 -
“白酒股下跌空间已不大!”公募人士不认同瑞银证券看空观点 多家酒企也回应了
2024-07-31