🚀 免費嘗試 Zilliz Cloud,完全托管的 Milvus,體驗速度提升 10 倍!立即嘗試

milvus-logo
LFAI
  • Home
  • Blog
  • 在 Google Colaboratory 設定 Milvus,輕鬆建立 ML 應用程式

在 Google Colaboratory 設定 Milvus,輕鬆建立 ML 應用程式

  • Engineering
December 23, 2020
milvus

科技的進步讓人工智慧 (AI) 和機器規模分析變得更容易取得和使用。開放原始碼軟體、公共資料集和其他免費工具的激增是推動這股趨勢的主要力量。透過搭配MilvusGoogle Colaboratory(簡稱「Colab」) 這兩種免費資源,任何人都可以建立強大、靈活的 AI 和資料分析解決方案。本文提供在 Colab 中設定 Milvus,以及使用 Python 軟體開發套件 (SDK) 執行基本操作的說明。

跳至:

Milvus 是什麼?

Milvus是一個開放原始碼的向量相似性搜尋引擎,可以整合廣泛採用的索引函式庫,包括 Faiss、NMSLIB 和 Annoy。該平台還包含一套完整的直覺式 API。透過 Milvus 與人工智慧 (AI) 模型的搭配,可以建立多樣化的應用程式,包括

  • 圖像、視訊、音訊和語意文字搜尋引擎。
  • 推薦系統和聊天機器人。
  • 新藥開發、基因篩選及其他生物醫學應用。

什麼是 Google Colaboratory?

Google Colaboratory是 Google 研究團隊的產品,可讓任何人透過網頁瀏覽器撰寫並執行 python 程式碼。Colab 是針對機器學習和資料分析應用而打造,提供免費的 Jupyter 記事本環境、與 Google Drive 同步,並讓使用者存取強大的雲端運算資源 (包括 GPU)。該平台支援許多流行的機器學習函式庫,並可與 PyTorch、TensorFlow、Keras 和 OpenCV 整合。

在 Google Colaboratory 開始使用 Milvus

雖然 Milvus 建議使用 Docker安裝並啟動服務,但目前的 Google Colab 雲端環境並不支援 Docker 安裝。此外,本教學的目的是盡可能讓大家容易上手 - 並非每個人都會使用 Docker。透過編譯 Milvus 的原始碼來安裝和啟動系統,以避免使用 Docker。

下載 Milvus 的原始碼,並在 Colab 中建立新筆記本

Google Colab 預先安裝了 Milvus 的所有支援軟體,包括所需的編譯工具 GCC、CMake 和 Git,以及驅動程式 CUDA 和 NVIDIA,簡化了 Milvus 的安裝和設定過程。首先,請下載 Milvus 的原始碼,並在 Google Colab 中建立新的筆記型電腦:

  1. 下載 Milvus 原始碼:Milvus_tutorial.ipynb。

Wget https://raw.githubusercontent.com/milvus-io/bootcamp/0.10.0/getting_started/basics/milvus_tutorial/Milvus_tutorial.ipynb

  1. 上傳 Milvus 原始碼到Google Colab並建立新的筆記型電腦。

Blog_Set Up Milvus in Google Colaboratory for Easy ML Application Building_2.png Blog_Set Up Milvus in Google Colaboratory for Easy ML Application Building_2.png

從原始碼編譯 Milvus

下載 Milvus 原始碼

git clone -b 0.10.3 https://github.com/milvus-io/milvus.git

安裝相依性

% cd /content/milvus/core ./ubuntu_build_deps.sh./ubuntu_build_deps.sh

建立 Milvus 原始碼

% cd /content/milvus/core
!ls
!./build.sh -t Release
# To build GPU version, add -g option, and switch the notebook settings with GPU
#((Edit -> Notebook settings -> select GPU))
# !./build.sh -t Release -g

注意:如果GPU版本編譯正確,會出現 "GPU resources ENABLED!

啟動 Milvus 伺服器

將 lib/ 目錄加入 LD_LIBRARY_PATH:

% cd /content/milvus/core/milvus
! echo $LD_LIBRARY_PATH
import os
os.environ['LD_LIBRARY_PATH'] +=":/content/milvus/core/milvus/lib"
! echo $LD_LIBRARY_PATH

在背景啟動並執行 Milvus 伺服器:

% cd scripts
! ls
! nohup ./start_server.sh &

顯示 Milvus 伺服器狀態:

! ls
! cat nohup.out

注意: 如果成功啟動 Milvus 伺服器,會出現以下提示:

Blog_Set Up Milvus in Google Colaboratory for Easy ML Application Building_3.png Blog_Set Up Milvus in Google Colaboratory for Easy ML Application Building_3.png

在 Google Colab 中使用 Python 執行 Milvus 的基本操作

在 Google Colab 成功啟動後,Milvus 可以提供 Python、Java、Go、Restful 及 C++ 等多種 API 介面。以下是在 Colab 中使用 Python 介面執行 Milvus 基本操作的說明。

安裝 pymilvus:

! pip install pymilvus==0.2.14

連接到伺服器:

# Connect to Milvus Server
milvus = Milvus(_HOST, _PORT)


# Return the status of the Milvus server.
server_status = milvus.server_status(timeout=10)

建立集合/分區/索引:

# Information needed to create a collection
param={'collection_name':collection_name, 'dimension': _DIM, 'index_file_size': _INDEX_FILE_SIZE, 'metric_type': MetricType.L2}

# Create a collection.
milvus.create_collection(param, timeout=10)

# Create a partition for a collection.
milvus.create_partition(collection_name=collection_name, partition_tag=partition_tag, timeout=10)
ivf_param = {'nlist': 16384}

# Create index for a collection.
milvus.create_index(collection_name=collection_name, index_type=IndexType.IVF_FLAT, params=ivf_param)

插入和刷新:

# Insert vectors to a collection.
milvus.insert(collection_name=collection_name, records=vectors, ids=ids)

# Flush vector data in one collection or multiple collections to disk.
milvus.flush(collection_name_array=[collection_name], timeout=None)
# Load a collection for caching.
milvus.load_collection(collection_name=collection_name, timeout=None)

# Search vectors in a collection.
search_param = { "nprobe": 16 }
milvus.search(collection_name=collection_name,query_records=[vectors[0]],partition_tags=None,top_k=10,params=search_param)

取得資料集/索引資訊

# Return information of a collection.    milvus.get_collection_info(collection_name=collection_name, timeout=10)

# Show index information of a collection.    milvus.get_index_info(collection_name=collection_name, timeout=10)

依 ID 取得向量:

# List the ids in segment
# you can get the segment_name list by get_collection_stats() function.
milvus.list_id_in_segment(collection_name =collection_name, segment_name='1600328539015368000', timeout=None)

# Return raw vectors according to ids, and you can get the ids list by list_id_in_segment() function.
milvus.get_entity_by_id(collection_name=collection_name, ids=[0], timeout=None)

取得/設定參數:

# Get Milvus configurations.    milvus.get_config(parent_key='cache', child_key='cache_size')

# Set Milvus configurations.    milvus.set_config(parent_key='cache', child_key='cache_size', value='5G')

刪除索引/向量/分區/集合:

# Remove an index.    milvus.drop_index(collection_name=collection_name, timeout=None)

# Delete vectors in a collection by vector ID.
# id_array (list[int]) -- list of vector id    milvus.delete_entity_by_id(collection_name=collection_name, id_array=[0], timeout=None)

# Delete a partition in a collection.    milvus.drop_partition(collection_name=collection_name, partition_tag=partition_tag, timeout=None)

# Delete a collection by name.    milvus.drop_collection(collection_name=collection_name, timeout=10)


Milvus 與 Google Colaboratory 完美結合

Google Colaboratory 是免費且直覺的雲端服務,可大幅簡化從原始碼編譯 Milvus 以及執行基本 Python 作業。這兩種資源任何人都可以使用,讓 AI 和機器學習技術更加普及。如需更多關於 Milvus 的資訊,請參閱下列資源:

  • 如需其他涵蓋各種應用的教學,請造訪Milvus Bootcamp
  • 對於有興趣貢獻或利用系統的開發人員,請在 GitHub 上找到Milvus
  • 如需有關推出 Milvus 的公司的更多資訊,請造訪Zilliz.com

    Try Managed Milvus for Free

    Zilliz Cloud is hassle-free, powered by Milvus and 10x faster.

    Get Started

    Like the article? Spread the word

    繼續閱讀