In [1]:
import prediction
In [2]:
model = prediction.prepare_model()
In [3]:
import os
import sys
import glob
from os.path import dirname, abspath
def get_files(file_path, file_type):
"""Get all files under directory.
- Args:
file_path: path of example folder
FILE_TYPE: type of files, i.e. jpg
"""
return glob.glob(''.join([file_path, file_type]))
def get_example_path():
"""Get example images path"""
curent_path = os.path.dirname(os.path.realpath('__file__'))
exampl_path = '/'.join([os.path.abspath(os.path.join(curent_path, os.pardir)),'example_data/'])
return exampl_path
example_imgs = get_files(''.join([get_example_path(),'example/']), '*.jpg')
example_imgs
Out[3]:
In [7]:
from IPython.display import Image, display
for idx,img in enumerate(example_imgs):
display(Image(img))
print prediction.make_prediction(url=None, img_id=idx, model=model, classes=prediction.classes, from_path=img)
In [43]:
import pandas as pd
photos_accordion_player = pd.read_csv("".join([get_example_path(), 'diversification/accordion_player.csv']))
photo_base_path = ''.join([get_example_path(),'diversification/'])
photo_path = []
for idx, photo in photos_accordion_player.iterrows():
photo_path.append(''.join([photo_base_path, str(idx), '.jpg']))
photos_accordion_player['path'] = photo_path
photos_accordion_player.head(5)
Out[43]:
In [48]:
# predict intents
from tqdm import tqdm
def predict_intents(original_rank):
"""For top 20 images, predict intents."""
res = []
for idx, p in tqdm(original_rank.iterrows()):
prob, label = prediction.make_prediction(None, idx, model, prediction.classes, p.path)
p['prob'] = prob
p['label'] = label[0]
p['rank'] = idx
res.append(p)
return pd.DataFrame(res)
photos_accordion_player = predict_intents(photos_accordion_player)
photos_accordion_player.head(5)
Out[48]:
In [55]:
# rerank based on intents
def re_rank(original_rank):
"""Rerank based on intents."""
grouped = original_rank.groupby(['label'])
groups = grouped.groups
# for each group, pick top 1 and re-rank by ranking position
big_groups = []
for idx in range(0,19):
small_group = []
for k in groups:
try:
small_group.append(groups.get(k)[idx])
except IndexError:
pass
if small_group:
big_groups.append(small_group)
# for each group within big groups, sort by original ranking position
re_ranked_df = []
for g in big_groups:
group_df = original_rank.iloc[g]
group_df = group_df.sort_values(by = ['rank'])
re_ranked_df.append(group_df)
re_ranked_df = pd.concat(re_ranked_df, axis=0)
return re_ranked_df
reranked_accordion_player = re_rank(photos_accordion_player)
reranked_accordion_player.head(3)
Out[55]:
In [59]:
# show result
def paste_images(dataframe):
import sys
from PIL import Image
images = map(Image.open, dataframe.path)
widths, heights = zip(*(i.size for i in images))
total_width = sum(widths)
max_height = max(heights)
new_im = Image.new('RGBA', (total_width, max_height))
x_offset = 0
for im in images:
new_im.paste(im, (x_offset,0))
x_offset += im.size[0]
return new_im
In [84]:
reranked_image = paste_images(reranked_accordion_player)
original_image = paste_images(photos_accordion_player)
In [85]:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.pyplot import imshow
%matplotlib inline
fig = plt.figure(figsize = (20,20))
imshow(np.asanyarray(original_image))
Out[85]:
In [87]:
fig = plt.figure(figsize = (20,20))
imshow(np.asarray(reranked_image))
Out[87]: