File size: 4,255 Bytes
eb1aec4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": [],
      "gpuType": "T4"
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    },
    "accelerator": "GPU"
  },
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "## C03 - Use GPS2Vec embeddings\n",
        "\n",
        "Simple example of how to obtain pretrained GPS2Vec embeddings. Read the paper here:[https://ieeexplore.ieee.org/abstract/document/9360464?casa_token=N4drK3PtmXMAAAAA:c096iwtxjU271IVjZePsFCH8Xm-7RWl7JztS-QxLgVKIo5ayltwAjzdEXXLb7xcyQKNyOvvN](https://ieeexplore.ieee.org/abstract/document/9360464?casa_token=N4drK3PtmXMAAAAA:c096iwtxjU271IVjZePsFCH8Xm-7RWl7JztS-QxLgVKIo5ayltwAjzdEXXLb7xcyQKNyOvvN).\n",
        "\n",
        "First install needed packages."
      ],
      "metadata": {
        "id": "ngz8zz9Gvbxh"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "!rm -r sample_data .config # Empty current directory\n",
        "!git clone https://github.com/yifangyin/GPS2Vec.git . # Clone GPS2Vec repository"
      ],
      "metadata": {
        "id": "I4-8JQhl0ntG"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "!pip install utm"
      ],
      "metadata": {
        "id": "_y16t1ho04qm"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Download pretrained models. For more details, see here: [https://github.com/yifangyin/GPS2Vec](https://github.com/yifangyin/GPS2Vec)"
      ],
      "metadata": {
        "id": "KLmByT0cB1Y_"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "!wget -O models_tag.zip 'https://www.dropbox.com/s/j8b4h3ynkv42gj4/models_tag.zip?dl=1'\n",
        "!wget -O models_visual.zip 'https://www.dropbox.com/s/kcsadz2fl6ynymh/models_visual.zip?dl=1'"
      ],
      "metadata": {
        "id": "Op4IY-t-2QO3"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "!unzip -q models_tag.zip\n",
        "!unzip -q models_visual.zip"
      ],
      "metadata": {
        "id": "GXd2WAYt2fsb"
      },
      "execution_count": 4,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Write helper function."
      ],
      "metadata": {
        "id": "VWhjuK-tB5lV"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "from gps2vec import *\n",
        "import torch\n",
        "import numpy as np"
      ],
      "metadata": {
        "id": "e7yzSifX01l4"
      },
      "execution_count": 5,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def get_gps2vec(locations,basedir,model='visual'):\n",
        "    nrows = 20\n",
        "    ncols = 20\n",
        "    sigma = 20000\n",
        "    if model=='visual':\n",
        "      modeldir=basedir+\"/models_visual\"\n",
        "      flag = 0\n",
        "    elif model=='tag':\n",
        "      modeldir=basedir+\"/models_tag\"\n",
        "      flag = 1\n",
        "    else:\n",
        "      raise ValueError('Invalid model')\n",
        "    out = []\n",
        "    for location in locations:\n",
        "      geofea = georep(location,modeldir,nrows,ncols,sigma,flag)\n",
        "      out.append(np.asarray(geofea))\n",
        "    return np.asarray(out, dtype=object)"
      ],
      "metadata": {
        "id": "MYtxk8NCvr0M"
      },
      "execution_count": 13,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Obtain location embeddings."
      ],
      "metadata": {
        "id": "Jv3GRaaIB7EJ"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "c = torch.Tensor([[-74.0060, 40.7128], [-118.2437, 34.0522]])  # Represents a batch of 2 locations (lon/lat)\n",
        "\n",
        "emb = get_gps2vec(np.flip(c.numpy(),1),'',model='visual')"
      ],
      "metadata": {
        "id": "l7xHob8y1OSM"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}