X Degrees of Separation


What is the connection between a 4000 year old clay figure and Van Gogh's Starry Night? How do you get from Bruegel's Tower of Babel to the street art of Rio de Janeiro? What links an African mask to a Japanese wood cut?

The following notebook implelements Google's X Degrees of Separation project in pytorch using images from WikiArt. The basic idea is to find the shortest path between two images.

Example of WikiArt datasets that can be used for this task link1 link2



install a fast lib for calculating kNNs

install a fast lib for calculting PCAs on extracted image features FBPCA.

install Graph analysis library iGraph.





Download a subset of wikiart images

WikiArt Metadata

Loading output library...
Loading output library...

WikiArt images


download the images using the URLs in the metadata files like WikiArt-Emotions-Ag4.tsv.


display a random set of the downloaded images

Loading output library...

Feature extraction


Extract the feature form those images to use it later to calculate distance between images

Pre-Trained ResNet model


get a feature extractor from the backbone imagenet classifier

PCA on extracted features


First reduce the dimensionality of feautres using PCA (we will take the eigen vectors).

load raw features

Loading output library...

apply PCA

load the names



calculate t-SNE on the engien vectors

normalize t-SNE values into the 0, 1 space

visualize the grouping resulting from t-SNE calculation

plot a random subset of the images of size 1k

Loading output library...

kNN on images


Now let's use the extracted features to caclculate the 10 nearest neigborhood for each image

Loading output library...

for each image calculate it's k=5 kNN

Loading output library...

Graph based on kNN


helper function to plot images which consititue a path in the previously constructed Graph

path between randmly chosen pictures

Loading output library...
Loading output library...
Loading output library...
Loading output library...
Loading output library...

The end.