Showing posts with label Machine Learning. Show all posts
Showing posts with label Machine Learning. Show all posts

Monday, 25 November 2024

Combine ResNet-50 embeddings with metadata for improved accuracy in your image search system

Step 1: Install Required Libraries

Ensure you have the necessary libraries:

  • Install Milvus and its Python SDK pymilvus.
  • Install libraries for handling images and metadata (e.g., TensorFlow, Scikit-learn).
pip install pymilvus tensorflow scikit-learn

Step 2: Set Up Milvus

  1. Start Milvus:

    • Install and run Milvus locally or use a hosted service like Zilliz Cloud.
    docker-compose up -d
  2. Connect to Milvus:

    • Use the Python SDK to connect to the Milvus server.
    from pymilvus import connections # Connect to Milvus connections.connect("default", host="127.0.0.1", port="19530")
  3. Create a Collection:

    • Design a schema to store image embeddings and metadata.
    from pymilvus import CollectionSchema, FieldSchema, DataType, Collection # Define schema fields = [ FieldSchema(name="image_embedding", dtype=DataType.FLOAT_VECTOR, dim=2048), # ResNet-50 embeddings FieldSchema(name="category", dtype=DataType.VARCHAR, max_length=50), # Categorical metadata FieldSchema(name="brand", dtype=DataType.VARCHAR, max_length=50), # Categorical metadata FieldSchema(name="price", dtype=DataType.FLOAT), # Numerical metadata ] schema = CollectionSchema(fields, description="Image search collection") # Create collection collection = Collection("ecommerce_image_search", schema)

Step 3: Index Data

  1. Extract and Encode Features:

    • Use ResNet-50 to extract embeddings and encode metadata (as explained in earlier steps).
  2. Insert Data into Milvus:

    • Combine embeddings with metadata and add them to Milvus.
    # Example data image_embedding = [0.1, 0.2, ..., 0.9] # Example 2048-d embedding category = "shoes" brand = "Nike" price = 99.99 # Insert data into Milvus data = [[image_embedding], [category], [brand], [price]] collection.insert(data) print("Data inserted successfully")
  3. Create Index for Faster Search:

    • Create a vector index for the image_embedding field to optimize similarity search.
    index_params = {"index_type": "IVF_FLAT", "metric_type": "L2", "params": {"nlist": 128}} collection.create_index(field_name="image_embedding", index_params=index_params) print("Index created successfully")

Step 4: User Input (Query)

  1. Image Upload:

    • Extract ResNet-50 embedding from the uploaded image.
    query_image_embedding = extract_features("uploaded_image.jpg")
  2. Metadata Filters:

    • Get metadata selections (e.g., category: "shoes", brand: "Nike").
    • Convert filters to SQL-like queries for Milvus.

Step 5: Perform Hybrid Search

  1. Combine Image and Metadata Search:

    • Milvus supports hybrid searches using metadata filters.
    # Define a query search_params = {"metric_type": "L2", "params": {"nprobe": 10}} filters = "category == 'shoes' && brand == 'Nike'" # User-selected metadata filters # Perform search results = collection.search( data=[query_image_embedding], # Input vector anns_field="image_embedding", # Vector field name param=search_params, limit=10, # Number of results expr=filters # Metadata filter ) # Display results for hit in results[0]: print(f"ID: {hit.id}, Score: {hit.score}, Metadata: {hit.entity}")

Step 6: Return Results

  1. Retrieve Matching Products:

    • Use the IDs of the search results to fetch additional product details (e.g., names, images) from your database.
  2. Display Results:

    • Show visually similar products filtered by the selected metadata on the front end.

Step 7: Refine and Optimize

  1. Weighting Embeddings and Metadata:

    • If image features are more critical, assign higher weight to embeddings.
    combined_embedding = 0.8 * image_embedding + 0.2 * metadata_vector
  2. Tune Milvus Parameters:

    • Experiment with nlist and nprobe in the index parameters for better speed and accuracy.
  3. Monitor Performance:

    • Regularly update indexes and handle metadata updates efficiently.

Advantages of Using Milvus

  • Efficient handling of large-scale image and metadata data.
  • Native support for hybrid searches (combining vectors and metadata).
  • Scalable and integrates well with machine learning workflows.

Would you like more details on any specific step or a complete code example?

Thank you

Sunday, 6 October 2024

Open-source solution for OCR (Optical Character Recognition)

1. Tesseract OCR

  • License: Apache 2.0 (Open-source)
  • Description: Tesseract is one of the most widely-used open-source OCR engines. It is highly reliable and supports many languages. You can train it for specific use cases, which can be useful for specialized motorcycle-related texts, part numbers, and more.
  • Best For: General-purpose OCR, multilingual text extraction.
  • Integration: It can be integrated with various programming languages like Python, PHP, and Node.js, making it flexible.
  • Repository: Tesseract GitHub

2. EasyOCR

  • License: Apache 2.0 (Open-source)
  • Description: EasyOCR is a lightweight and fast OCR library that supports over 80 languages. It’s easy to set up, uses deep learning, and can handle complex scripts and multilingual text extraction, making it ideal for a global audience.
  • Best For: Multilingual OCR, ease of integration, and using GPUs for faster processing.
  • Integration: Works with Python and can be integrated into larger machine learning pipelines or standalone applications.
  • Repository: EasyOCR GitHub

3. PaddleOCR

  • License: Apache 2.0 (Open-source)
  • Description: PaddleOCR is part of the PaddlePaddle ecosystem, offering strong support for over 80 languages and delivering high accuracy. It is particularly suitable for complex document layouts and multilingual scenarios.
  • Best For: High accuracy in OCR, complex layouts, and global language support.
  • Integration: Works in Python and is based on the PaddlePaddle deep learning framework.
  • Repository: PaddleOCR GitHub

4. OpenCV (with Tesseract)

  • License: BSD 3-Clause (Open-source)
  • Description: OpenCV includes support for OCR via Tesseract. It’s highly useful when combining text recognition with other computer vision tasks, like detecting objects or motorcycles before performing OCR.
  • Best For: Combining OCR with image processing tasks, preprocessing images for better text recognition.
  • Integration: Works with multiple languages such as Python, C++, and Java. It can be used with Tesseract for OCR tasks.
  • Repository: OpenCV GitHub

Recommendation

If you're looking for an open-source solution, Tesseract or EasyOCR would be the most straightforward and well-supported choices. Both offer excellent language support, flexibility, and ease of integration into your ecommerce platform. If your application involves more complex document layouts or multilingual needs, you might consider PaddleOCR for its enhanced capabilities.

Thank you.

Publish npm package

  Để publish   pav-kit  lên NPM, bạn hãy làm theo các bước dưới đây. Tôi đã tạo thêm file  index.js  để đảm bảo gói tin hợp lệ. Bước 1: Tạo ...