In [1]:
%load_ext autoreload
%autoreload 2
import os
import re
import vislab._results
import pandas as pd

import vislab
import vislab.datasets
import vislab.results
import vislab.vw3

In [21]:
def write_pascal_as_feat(
        dataset_name, mc_pred_df, target_feature_name,
        feature_name='pascal_mc'):
    """
    Get the ids of the target feature, and output to txt.gz in the same order.
    """    
    # Get the id order of the target feature.
    target_feature_ids_name = '{}/{}/{}.txt.gz.ids.txt'.format(
        vislab.config['paths']['feats'], dataset_name, target_feature_name)
    if not os.path.exists(target_feature_ids_name):
        print("Feature ids file does not exist! Run scripts/get_feat_file_ids.sh")
    image_ids = pd.Index([x.strip() for x in open(target_feature_ids_name)])
    
    # Output this feature in the same order to the same directory.
    output_filename = '{}/{}/{}_for_{}.txt.gz'.format(
        vislab.config['paths']['feats'], dataset_name, feature_name, target_feature_name)
    mc_pred_df = mc_pred_df.ix[image_ids.tolist()]
    
    # Note that we may not have all the ids.
    # In that case, just fill with column means to introduce no information.
    print('{}/{} records are null'.format(
        mc_pred_df.isnull().any(1).sum(), mc_pred_df.shape[0]))
    df = mc_pred_df.fillna(mc_pred_df.mean())
    
    # Standardize
    df = (df - df.values.mean(0)) / df.values.std(0)
    print df.info()
    
    # Write to file.
    vislab.vw3.write_data_in_vw_format(df, feature_name, output_filename)
    
    print("Success, written to {}".format(output_filename))

In [52]:
pascal_feat='decaf_fc6 None vw'

In [10]:
## AVA Style

results_df, preds_panel = aphrodite.results.load_pred_results(
    'pascal_meta_on_ava_oct29', os.path.expanduser('~/work/aphrodite/data/results2'),
    multiclass=False, force=False)

mc_pred_df = preds_panel.minor_xs(pascal_feat).astype(float)
mc_pred_df.columns = [x.replace('clf ava_style_', '') for x in mc_pred_df.columns]

# best feature is decaf_fc6
#write_pascal_as_feat('ava_style', mc_pred_df, 'decaf_fc6')
#write_pascal_as_feat('ava_style', mc_pred_df, 'lab_hist')
#write_pascal_as_feat('ava_style', mc_pred_df, 'noise')

write_pascal_as_feat('ava_style', mc_pred_df, 'fusion_ava_style_oct22')


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-10-ead079007b19> in <module>()
      1 ## AVA Style
      2 
----> 3 results_df, preds_panel = aphrodite.results.load_pred_results(
      4     'pascal_meta_on_ava_oct29', os.path.expanduser('~/work/aphrodite/data/results2'),
      5     multiclass=False, force=False)

NameError: name 'aphrodite' is not defined

In [22]:
## Flickr

results_df, preds_panel = vislab._results.load_pred_results(
    'pascal_mc_on_flickr_mar23', os.path.expanduser('~/work/vislab-git/data/results'),
    multiclass=True, force=False)

pascal_feat='caffe_fc7 False vw'
mc_pred_df = preds_panel.minor_xs(pascal_feat)
mc_pred_df = mc_pred_df[[x for x in mc_pred_df.columns if x.startswith('pred_metaclass_')]]
mc_pred_df

# RUN ./scripts/get_feat_file_ids.sh BEFOREHAND
write_pascal_as_feat('flickr', mc_pred_df, 'fusion_flickr_mar23')


Loaded from cache: 1 records
0/80000 records are null
<class 'pandas.core.frame.DataFrame'>
Index: 80000 entries, 10000032526 to 9999123683
Data columns (total 4 columns):
pred_metaclass_animal     80000 non-null float64
pred_metaclass_indoor     80000 non-null float64
pred_metaclass_person     80000 non-null float64
pred_metaclass_vehicle    80000 non-null float64
dtypes: float64(4)None
Success, written to /Users/sergeyk/work/vislab-git/data/feats/flickr/pascal_mc_for_fusion_flickr_mar23.txt.gz

In [21]:
## Wikipaintings

results_df, preds_panel = aphrodite.results.load_pred_results(
    'pascal_on_wp_oct29', os.path.expanduser('~/work/aphrodite/data/results2'),
    multiclass=False, force=False)

mc_pred_df = preds_panel.minor_xs(pascal_feat).astype(float)
mc_pred_df.columns = [x.replace('clf wikipaintings_', '') for x in mc_pred_df.columns]
mc_pred_df = mc_pred_df[[x for x in mc_pred_df.columns if x.startswith('metaclass_')]]
mc_pred_df

# best feature is mc_bit, but we'll run on decaf to finish in a reasonable time
#write_pascal_as_feat('wikipaintings', mc_pred_df, 'decaf_fc6')
#write_pascal_as_feat('wikipaintings', mc_pred_df, 'noise')

write_pascal_as_feat('wikipaintings', mc_pred_df, 'fusion_wikipaintings_oct25')


Loaded from cache: 9 records
Out[21]:
<class 'pandas.core.frame.DataFrame'>
Index: 75831 entries, paul-reed_12-1964 to emily-carr_zunoqua-of-the-cat-village-1931
Data columns (total 4 columns):
metaclass_animal     75831  non-null values
metaclass_indoor     75831  non-null values
metaclass_person     75831  non-null values
metaclass_vehicle    75831  non-null values
dtypes: float64(4)

In [17]:
import aphrodite.image
mc_pred_df = mc_pred_df.sort('metaclass_animal', ascending=False)
for image_id in mc_pred_df.iloc[:5].index:
    imshow(aphrodite.image.get_image_for_id(image_id, 'ava_style'))
    figure()


<matplotlib.figure.Figure at 0x126a233d0>

In [36]:
import aphrodite.image
mc_pred_df = mc_pred_df.sort('metaclass_person', ascending=False)
for image_id in mc_pred_df.iloc[:5].index:
    imshow(aphrodite.image.get_image_for_id(image_id, 'ava_style'))
    figure()


Downloading image for id: 891389
Downloading image for id: 271000
Downloading image for id: 591081
Downloading image for id: 238172
Downloading image for id: 269186
<matplotlib.figure.Figure at 0x117f73cd0>

In [37]:
import aphrodite.image
mc_pred_df = mc_pred_df.sort('metaclass_vehicle', ascending=False)
for image_id in mc_pred_df.iloc[:5].index:
    imshow(aphrodite.image.get_image_for_id(image_id, 'ava_style'))
    figure()


Downloading image for id: 734200
Downloading image for id: 326123
Downloading image for id: 388718
Downloading image for id: 352846
Downloading image for id: 210905
<matplotlib.figure.Figure at 0x117f5e550>

In [38]:
import aphrodite.image
mc_pred_df = mc_pred_df.sort('metaclass_indoor', ascending=False)
for image_id in mc_pred_df.iloc[:5].index:
    imshow(aphrodite.image.get_image_for_id(image_id, 'ava_style'))
    figure()


Downloading image for id: 21992
Downloading image for id: 324148
Downloading image for id: 96872
Downloading image for id: 324525
Downloading image for id: 423197
<matplotlib.figure.Figure at 0x1193faa90>

In [39]:
results_df, preds_panel = aphrodite.results.load_pred_results(
    'pascal_on_wp_oct29', os.path.expanduser('~/work/aphrodite/data/results2'),
    multiclass=False, force=False)
pred_prefix = 'pred'
print preds_panel.minor_axis
preds_panel


Results in collection pascal_on_wp_oct29: 9
Index([u'decaf_fc6 False vw', u'label', u'split'], dtype='object')
Out[39]:
<class 'pandas.core.panel.Panel'>
Dimensions: 9 (items) x 75831 (major_axis) x 3 (minor_axis)
Items axis: clf wikipaintings_class_bicycle to clf wikipaintings_metaclass_vehicle
Major_axis axis: paul-reed_12-1964 to emily-carr_zunoqua-of-the-cat-village-1931
Minor_axis axis: decaf_fc6 False vw to split

In [47]:
dataset_name = 'wikipaintings'
mc_pred_df = preds_panel.minor_xs('decaf_fc6 False vw')
mc_pred_df.columns = [x.replace('clf {}_'.format(dataset_name), '') for x in mc_pred_df.columns]
mc_pred_df


Out[47]:
<class 'pandas.core.frame.DataFrame'>
Index: 75831 entries, paul-reed_12-1964 to emily-carr_zunoqua-of-the-cat-village-1931
Data columns (total 9 columns):
class_bicycle        75831  non-null values
class_car            75831  non-null values
class_cat            75831  non-null values
class_dog            75831  non-null values
class_horse          75831  non-null values
metaclass_animal     75831  non-null values
metaclass_indoor     75831  non-null values
metaclass_person     75831  non-null values
metaclass_vehicle    75831  non-null values
dtypes: object(9)

In [49]:
import aphrodite.image
mc_pred_df = mc_pred_df.sort('metaclass_person', ascending=False)
for image_id in mc_pred_df.iloc[:5].index:
    imshow(aphrodite.image.get_image_for_id(image_id, dataset_name))
    figure()


Downloading image for id: albert-anker_the-zaeslin-siblings-1896
Downloading image for id: pyotr-konchalovsky_spanish-women-1937
Downloading image for id: lucas-cranach-the-elder_christ-taking-leave-of-his-mother-1520
Downloading image for id: bertalan-por_the-family-1909
Downloading image for id: jacob-jordaens_suffer-the-little-children-to-come-unto-me
<matplotlib.figure.Figure at 0x117f5e6d0>

In [50]:
import aphrodite.image
mc_pred_df = mc_pred_df.sort('metaclass_animal', ascending=False)
for image_id in mc_pred_df.iloc[:5].index:
    imshow(aphrodite.image.get_image_for_id(image_id, dataset_name))
    figure()


<matplotlib.figure.Figure at 0x118a85890>