Xenova HF staff commited on
Commit
ae27bf5
โ€ข
1 Parent(s): d774f8a

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +120 -3
README.md CHANGED
@@ -1,3 +1,120 @@
1
- ---
2
- license: mit
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ library_name: transformers.js
4
+ ---
5
+
6
+ https://github.com/VicenteVivan/geo-clip with ONNX weights to be compatible with Transformers.js.
7
+
8
+ ## Usage (Transformers.js)
9
+
10
+ If you haven't already, you can install the [Transformers.js](https://huggingface.co/docs/transformers.js) JavaScript library from [NPM](https://www.npmjs.com/package/@xenova/transformers) using:
11
+ ```bash
12
+ npm i @xenova/transformers
13
+ ```
14
+
15
+ **Example:** Perform worldwide image geolocalization.
16
+ ```js
17
+ import { AutoModel, AutoProcessor, RawImage, Tensor, dot, softmax } from '@xenova/transformers';
18
+
19
+ // Load vision and location models
20
+ const model_id = 'Xenova/geoclip-large-patch14';
21
+ const vision_model = await AutoModel.from_pretrained(model_id, {
22
+ model_file_name: 'vision_model',
23
+ });
24
+ const location_model = await AutoModel.from_pretrained(model_id, {
25
+ model_file_name: 'location_model',
26
+ quantized: false,
27
+ });
28
+
29
+ // Load image processor
30
+ const processor = await AutoProcessor.from_pretrained('openai/clip-vit-large-patch14');
31
+
32
+ // Read and preprocess image
33
+ const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/moraine-lake.png';
34
+ const image = await RawImage.fromURL(url);
35
+ const vision_inputs = await processor(image);
36
+
37
+ // Compute image embeddings
38
+ const { image_embeds } = await vision_model(vision_inputs);
39
+ const norm_image_embeds = image_embeds.normalize().data;
40
+
41
+ // Define a list of candidate GPS coordinates
42
+ // https://github.com/VicenteVivan/geo-clip/blob/main/geoclip/model/gps_gallery/coordinates_100K.csv
43
+ const coordinate_data = 'https://huggingface.co/Xenova/geoclip-large-patch14/resolve/main/gps_gallery/coordinates_100K.json';
44
+ const gps_data = await (await fetch(coordinate_data)).json();
45
+
46
+ // Compute location embeddings and compare to image embeddings
47
+ const coordinate_batch_size = 512;
48
+ const exp_logit_scale = Math.exp(3.681034803390503); // Used for scaling logits
49
+ const scores = [];
50
+ for (let i = 0; i < gps_data.length; i += coordinate_batch_size) {
51
+ const chunk = gps_data.slice(i, i + coordinate_batch_size);
52
+
53
+ const { location_embeds } = await location_model({
54
+ location: new Tensor('float32', chunk.flat(), [chunk.length, 2])
55
+ });
56
+
57
+ const norm_location_embeds = location_embeds.normalize().tolist();
58
+ for (const embed of norm_location_embeds) {
59
+ const score = exp_logit_scale * dot(norm_image_embeds, embed);
60
+ scores.push(score);
61
+ }
62
+ }
63
+
64
+ // Get top predictions
65
+ const top_k = 50;
66
+ const results = softmax(scores)
67
+ .map((x, i) => [x, i])
68
+ .sort((a, b) => b[0] - a[0])
69
+ .slice(0, top_k)
70
+ .map(([score, index]) => ({ index, gps: gps_data[index], score }));
71
+
72
+ console.log('=======================');
73
+ console.log('Top 5 GPS Predictions ๐Ÿ“');
74
+ console.log('=======================');
75
+ for (let i = 0; i < 5; ++i) {
76
+ console.log(results[i]);
77
+ }
78
+ console.log('=======================');
79
+ ```
80
+
81
+ Outputs:
82
+ ```
83
+ =======================
84
+ Top 5 GPS Predictions ๐Ÿ“
85
+ =======================
86
+ {
87
+ index: 75129,
88
+ gps: [ 51.327447, -116.183509 ],
89
+ score: 0.06895832493631944
90
+ }
91
+ {
92
+ index: 5158,
93
+ gps: [ 51.326401, -116.18263 ],
94
+ score: 0.06843383108770337
95
+ }
96
+ {
97
+ index: 77752,
98
+ gps: [ 51.328198, -116.180934 ],
99
+ score: 0.06652924543010541
100
+ }
101
+ {
102
+ index: 32529,
103
+ gps: [ 51.327809, -116.180334 ],
104
+ score: 0.065981075526145
105
+ }
106
+ {
107
+ index: 461,
108
+ gps: [ 51.322353, -116.18557 ],
109
+ score: 0.06476605375767822
110
+ }
111
+ =======================
112
+ ```
113
+
114
+ As seen on the map, this is indeed the location of [Moraine Lake](https://en.wikipedia.org/wiki/Moraine_Lake) in Canada.
115
+
116
+ ![image/png](https://cdn-uploads.huggingface.co/production/uploads/61b253b7ac5ecaae3d1efe0c/ThtHHkOmXEll2tyGV85GY.png)
117
+
118
+ ---
119
+
120
+ Note: Having a separate repo for ONNX weights is intended to be a temporary solution until WebML gains more traction. If you would like to make your models web-ready, we recommend converting to ONNX using [๐Ÿค— Optimum](https://huggingface.co/docs/optimum/index) and structuring your repo like this one (with ONNX weights located in a subfolder named `onnx`).