prrao87 commited on
Commit
bbe5f84
·
verified ·
1 Parent(s): 387397e

Update README with LanceDB examples

Browse files
Files changed (1) hide show
  1. README.md +72 -0
README.md CHANGED
@@ -60,6 +60,18 @@ ds = lance.dataset("hf://datasets/lance-format/flickr30k-lance/data/train.lance"
60
  print(ds.count_rows(), ds.schema.names, ds.list_indices())
61
  ```
62
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  ## Cross-modal text→image search
64
 
65
  ```python
@@ -89,6 +101,30 @@ for h in hits:
89
  print(h)
90
  ```
91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
  ## Image→caption (image-to-text retrieval)
93
 
94
  ```python
@@ -102,6 +138,26 @@ neighbors = ds.scanner(
102
  ).to_table().to_pylist()
103
  ```
104
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105
  ## Full-text search on captions
106
 
107
  ```python
@@ -114,6 +170,22 @@ hits = ds.scanner(
114
  ).to_table().to_pylist()
115
  ```
116
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117
  ## Working with images
118
 
119
  ```python
 
60
  print(ds.count_rows(), ds.schema.names, ds.list_indices())
61
  ```
62
 
63
+ ## Load with LanceDB
64
+
65
+ These tables can also be consumed by [LanceDB](https://lancedb.github.io/lancedb/), the serverless vector database built on Lance, for simplified vector search and other queries.
66
+
67
+ ```python
68
+ import lancedb
69
+
70
+ db = lancedb.connect("hf://datasets/lance-format/flickr30k-lance/data")
71
+ tbl = db.open_table("train")
72
+ print(f"LanceDB table opened with {len(tbl)} image-caption pairs")
73
+ ```
74
+
75
  ## Cross-modal text→image search
76
 
77
  ```python
 
101
  print(h)
102
  ```
103
 
104
+ ### LanceDB cross-modal text→image search
105
+
106
+ ```python
107
+ import lancedb, open_clip, torch
108
+
109
+ model, _, _ = open_clip.create_model_and_transforms("ViT-B-32", pretrained="laion2b_s34b_b79k")
110
+ tokenizer = open_clip.get_tokenizer("ViT-B-32")
111
+ model = model.eval().cuda().half()
112
+ with torch.no_grad():
113
+ q = model.encode_text(tokenizer(["a man surfing at sunset"]).cuda())
114
+ q = (q / q.norm(dim=-1, keepdim=True)).float().cpu().numpy()[0]
115
+
116
+ db = lancedb.connect("hf://datasets/lance-format/flickr30k-lance/data")
117
+ tbl = db.open_table("train")
118
+
119
+ results = (
120
+ tbl.search(q.tolist(), vector_column_name="image_emb")
121
+ .metric("cosine")
122
+ .select(["image_id", "caption"])
123
+ .limit(10)
124
+ .to_list()
125
+ )
126
+ ```
127
+
128
  ## Image→caption (image-to-text retrieval)
129
 
130
  ```python
 
138
  ).to_table().to_pylist()
139
  ```
140
 
141
+ ### LanceDB image→caption search
142
+
143
+ ```python
144
+ import lancedb
145
+
146
+ db = lancedb.connect("hf://datasets/lance-format/flickr30k-lance/data")
147
+ tbl = db.open_table("train")
148
+
149
+ ref = tbl.search().limit(1).select(["image_emb", "caption"]).to_list()[0]
150
+ query_embedding = ref["image_emb"]
151
+
152
+ results = (
153
+ tbl.search(query_embedding, vector_column_name="text_emb")
154
+ .metric("cosine")
155
+ .select(["caption"])
156
+ .limit(10)
157
+ .to_list()
158
+ )
159
+ ```
160
+
161
  ## Full-text search on captions
162
 
163
  ```python
 
170
  ).to_table().to_pylist()
171
  ```
172
 
173
+ ### LanceDB full-text search
174
+
175
+ ```python
176
+ import lancedb
177
+
178
+ db = lancedb.connect("hf://datasets/lance-format/flickr30k-lance/data")
179
+ tbl = db.open_table("train")
180
+
181
+ results = (
182
+ tbl.search("dog playing in the snow")
183
+ .select(["image_id", "caption"])
184
+ .limit(10)
185
+ .to_list()
186
+ )
187
+ ```
188
+
189
  ## Working with images
190
 
191
  ```python