Skip to content

Instantly share code, notes, and snippets.

Last active January 10, 2021 07:55
Show Gist options
  • Save aman-tiwari/d294a906161eb983bb5e3263f733e28e to your computer and use it in GitHub Desktop.
Save aman-tiwari/d294a906161eb983bb5e3263f733e28e to your computer and use it in GitHub Desktop.
Document containing install instructions and cool links for the Making Maps with ML workshop!

Messing with Maps and ML quickstart

This document:

Installation instructions

By far the most annoying part of getting started with messing with ML is installing researcher-made code and turning it into something fun to play with.

Before doing any of these, please install Miniconda. If you don't have it installed already, here's how:

For OSX, this is:

curl "" -o ""

For Linux this is:

wget "" -O ""

For Windows, go to and run the installer.

After running those commands, you'll have to go through the installation process, which involves:

  1. holding down return to scroll past the licence and typing in yes to accept it.
  2. Leave the installation path to the default
  3. Answer yes when it asks Do you wish the installer to prepend the Miniconda3 install location to PATH...

Then, run source ~/.bashrc

Now, set up two environments:

conda create -n py3k anaconda numpy scipy scikit-image rasterio python=3.6
conda create -n analogies anaconda numpy scipy scikit-image python=3.6


For all of the models, we need two aligned datasets of images. This basically means that we need two folders, called datasetA/train and datasetB/train such that datasetA/train/abcd.png corresponds in some way to datasetB/train/abcd.png. For instance, one could be the outline of a handbag and the other the picture of the handbag itself. See pix2pix for some examples.

Luckily, we'll be working with slippy maps, for which it is easy to create two aligned datasets. I've created a script that lets you scrape any slippy map once you get a url in the form{z}/{x}/{y}....

To install the scraper,

git clone
cd the-ultimate-tile-stitcher
source activate py3k
pip install -r requirements.txt

If you get an error regarding libgeos, try brew install geos for OSX or install geos through your package manager on Linux.

For the data, you could create two maps in mapbox studio and get the the api url from the Styles -> { Select a style } -> Share, develop & use -> User style in GIS apps -> CartoDB. Or, you can use any other slippy map service that gives you an api of this form (e.g,

Here's one I made earlier: (Use these URLS in the scraper):{z}/{x}/{y}@2x?access_token=pk.eyJ1IjoiYW1hbi10aXdhcmkiLCJhIjoiY2ozajdzOXM4MDBqYjJ3cXNnbHg3YjF3dyJ9.DjsmHW5ahovyG4sYPGQ-Zw{z}/{x}/{y}@2x?access_token=pk.eyJ1IjoiYW1hbi10aXdhcmkiLCJhIjoiY2ozajdzOXM4MDBqYjJ3cXNnbHg3YjF3dyJ9.DjsmHW5ahovyG4sYPGQ-Zw

Then, go to to and create a GeoJSON containing the area you want to sample the dataset of tiles from.

Then, run (still in the scraper directory), where {slippy map url 1} and {slippy map url 2} are the two styles you want to scrape:

mkdir tiles_1
mkdir tiles_1/train
python --poly {your geojson} --zoom {the zoom level you want} --url {slippy map url 1} --out-dir tiles_1/train

And also

mkdir tiles_2
mkdir tiles_2/train
python --poly {your geojson} --zoom {the zoom level you want} --url {slippy map url 2} --out-dir tiles_2/train

Image Analogies

Image analogies is a generative technique based on matching patches between two sets of images:

To install it,

source activate analogies
pip install neural-image-analogies

Then, ff your laptop doesn't have a gpu run pip install tensorflow-cpu otherwise run pip install Theano=0.8.1. Theano is annoying to install and use, so I'd suggest using the first one

Then, download the vgg16_weights.h5 weights from:

Then, follow the instructions at to make your own image analogies between the tiles you scraped.


Many of you may be familiar with stuff like:

All of these are instances of a class of generative models called pix2pix, with the zebra <-> horse being a CycleGan, in the class of Image-to-Image translation networks.

Today we'll look at Pix2pix, because it's easy to explain and powerful.

Intallation, data and training

We'll use the pytorch implementation located at:

To install, (after doing the above conda installation instructions):

Activate the conda environment. This means anything you install will be kept neatly inside this environment without trampling over everything else

source activate py3k
conda install -c menpo opencv
pip install visdom dominate
conda update

then, follow the instructions on to install PyTorch.


git clone
cd pytorch-CycleGAN-and-pix2pix
mkdir datasets/tiles_dataset
python datasets/ --fold_A {path to tiles_1 that you scraped} --fold_B {path to tiles_2} --fold_AB datasets/tiles_dataset

This will create concatenate the images you scraped to be side by side, preparing them to train the model.

Then, in another terminal, rin:

source activate py3k
python -m visdom.server

and then, to begin training, run:

python --dataroot datasets/tiles_dataset --name tiles --model pix2pix  --which_model_netG unet_256 --which_direction AtoB --lambda_A 100 --dataset_mode aligned --no_lsgan --norm batch --gpu_ids=-1 

This will take a long time, and especially if you don't have a gpu. Here's one I prepared earlier:

Download latest_net_D.pth and latest_net_G.pth and put them in checkpoints/tiles_pretrained/

Then, draw some images using the palette of , and put them in drawn_imgs

Then, run python --dataroot drawn_imgs --name tiles_pretrained --model test --which_model_netG unet_256 --which_direction BtoA --dataset_mode single --gpu_ids=-1 --norm batch (remove the gpu_ids argument to use the gpu). to generate some fake images! These will be placed in ./checkpoints/tiles_pretrained/web/images

(The pretrained one above was trained on the styles and

Useful, interesting links




Copy link

micahstubbs commented Aug 21, 2017

screen shot 2017-08-20 at 5 18 05 pm

this helped me fix this error message: jazzsaxmafia/video_to_sequence#3 I encountered on MacOS

pip install opencv-python

Copy link

micahstubbs commented Aug 21, 2017

if you want to resume training from where you left off in an earlier run, you can continue from the last checkpoint by adding this flag to the train command --checkpoints_dir checkpoints

which gives us this command in full:

python --dataroot datasets/tiles_dataset --name tiles --model pix2pix  --which_model_netG unet_256 --which_direction AtoB --lambda_A 100 --dataset_mode aligned --no_lsgan --norm batch --gpu_ids=-1 --checkpoints_dir checkpoints

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment