In [1]:
%load_ext autoreload
%autoreload 2
from IPython.display import HTML
import vislab.datasets
import vislab.results
import os
import pandas as pd

In [55]:
from IPython.display import HTML
def top_k_images(df, k=10):
    return ' '.join(
        '<div style="display: inline-block;"><img src="{}" width="210px" /><br />{}</div>'.format(row['image_url'], row.name)
        for i, row in df[:k].iterrows())

import subprocess
import shlex
import re
def top_images_for_style(df, style, split=None):
    df_ = df.copy()
    if split is not None:
        df_ = df_[df_['split'] == split]
    title = '<h4>{}, results: {}</h4>'.format(
        style, df_.shape[0])
    df_ = df_.sort(style, ascending=False)
    
    # download and resize to folder
    d = os.path.expanduser('~/dropbox_work/aphrodite-writeup/figures/wp_on_wp/')
    dirname = vislab.util.makedirs(d + '{}/'.format(style))
    w_dirname = vislab.util.makedirs(d + '{}/w/'.format(style))
    h_dirname = vislab.util.makedirs(d + '{}/h/'.format(style))
    counter = 0
    for i, row in df_[:5].iterrows():
        cmd = 'wget {} -O {}.jpg'.format(row['image_url'], counter)
        subprocess.call(shlex.split(cmd), cwd=dirname)
        
        cmd = 'find . -name "*.jpg" -depth 1 -exec convert {} -resize x310 -gravity Center -crop 192x310+0+0 -density 300 -units PixelsPerInch h/{} \;'
        subprocess.call(shlex.split(cmd), cwd=dirname)
        
        cmd = 'find . -name "*.jpg" -depth 1 -exec convert {} -resize 500 -gravity Center -crop 500x310+0+0 -density 300 -units PixelsPerInch w/{} \;'
        subprocess.call(shlex.split(cmd), cwd=dirname)
        counter += 1 
    
    return title + top_k_images(df_, k=5)

In [35]:
pred_df = pd.read_hdf(
    "../data/results/data_wikipaintings_style_ALL_features_['caffe_fc7']_num_test_16492_num_train_49475_num_val_16492_quadratic_None_task_clf.h5",
    'df'
)
pred_df.index.name = 'image_id'
pred_df = pred_df[[x for x in pred_df.columns if x.startswith('pred_style_')]]
wp_df = vislab.datasets.wikipaintings.get_style_df()
wp_df.index.name = 'image_id'
df = pred_df.join(wp_df)
df = df[df['_split'] == 'test']

In [58]:
style_labels = [x for x in df.columns if x.startswith('style_')]
for style in style_labels:
    top_images_for_style(df, style)