In [11]:
from __future__ import division
import numpy
import imaginet.task as task
import imaginet.defn.audiovis_rhn as audiovis
import imaginet.defn.visual2_rhn as vis2

In [119]:
import imaginet.vendrov_provider as dp

prov = dp.getDataProvider(dataset='coco', root='/home/gchrupala/reimaginet/')
sent = list(prov.iterSentences(split='val'))

In [57]:
model_s = task.load("/home/gchrupala/reimaginet/run-rhn-coco-9-resume/model.r.e9.zip")
model_w = task.load("/home/gchrupala/reimaginet/run-rhn-coco-word-4/model.r.e14.zip")

In [5]:
import json

In [6]:
score_s = json.load(open("/home/gchrupala/reimaginet/run-rhn-coco-9-resume/scores.9.json"))
score_w = json.load(open("/home/gchrupala/reimaginet/run-rhn-coco-word-4/scores.14.json"))

In [18]:
import numpy
def extreme(good, worse):
    ratio = numpy.array(good['ranks']) / numpy.array(worse['ranks'])
    return numpy.argsort(ratio)

In [26]:
for j in extreme(score_w, score_s)[:10]:
    print score_w['ranks'][j], "/", score_s['ranks'][j], sent[j]['raw']


1 / 2255 an engineer is climbing into the engine compartment
1 / 1294 a west highland white terrier dressed in a neon yellow vest pants in the sunshine
1 / 987 some large streamers some look like a clown fish
2 / 1307 a young foal nuzzling its mother in the nose
1 / 601 cow tethered with chain eating hay in outdoor field
1 / 529 three men eat lunch together at a convention
1 / 523 a black bull carrying items and working as a beast of burden
1 / 484 a guy posing with a guitar while making a song
2 / 968 church alter with blue and yellow stained glass
1 / 431 subway map art cow sculpture on the sidewalk

In [195]:
for j in extreme(score_s, score_w)[:50]:
    print score_w['ranks'][j], "/", score_s['ranks'][j], j, sent[j]['raw']


632 / 1 19021 a food process sitting on a counter that has a mixture in it
492 / 1 17931 a yellow and white birtd is in flight
898 / 2 20002 an inlaid vase sitting in front of a map
3370 / 10 19288 a man with a pony tail and bread sitting down
279 / 1 12234 there are people loitering in between train cars
470 / 2 9544 while in the immediate foreground juts a gnarled tree branch the majority of the view consists of a an expanse of short grass dotted with a few longer tufts and a number of scattered grazing sheep
141 / 1 11564 a woman standing in the middle of a farmer marked
4344 / 34 5214 a cat with a durham bulls helmet on its head
439 / 4 17995 artwork of a ship with three masts and one sail open with a scull and crossbones on it in bluish gray water with gray cement wall in background
302 / 3 19318 several japanese colored fans are at different heights
190 / 2 10269 a little kid sits on a horse while playing with a lasso
734 / 8 12644 the organizer is sitting next to the computer screen
624 / 7 12713 this is a streeet sign at an intersection
3294 / 37 10684 a couple of bathtubs are leaning against each other
174 / 2 14538 a simplistic industrial restroom with all ones necessities
2036 / 24 17264 an abandoned bus on the sid eof the rode
76 / 1 22663 tennis player trying to reach tennis ball while other player watches from other side of net
301 / 4 14181 two young boys gnaw on large orange carrots
446 / 6 23409 an old adobe mission with a clock tower stands behind a sparsely leaved tree
61 / 1 10991 to scears going up a steep side of a mountain together
913 / 16 14890 a blue and black ray holding sandwich coffee and liquor bottles
282 / 5 16327 not the biggest workspace in the world but it works
3413 / 62 12257 there is no photo here to provide a caption for
688 / 13 18705 various equine (horses and zebras) inside stalls under a tent
101 / 2 7776 a short train traveling through a rustic contryside
48 / 1 7623 a pitcher filled with yellow tulips on a black and white background
47 / 1 22841 a man attaches a screen on a toilet seat
45 / 1 12587 a tray of yellow swirl cookies with toothpicks sits on a table
45 / 1 1148 the young man is setting in the floor with his computer ear an outlet
90 / 2 9080 a man sitting on a bench next to a star wars storm trooper
43 / 1 22554 a half of a pizza with pepperonis next to a coffee cup
42 / 1 605 two men standing on a very tall clock tower with a white clock and two thermometers
124 / 3 12649 these two riders are far ahead of the ones behind them
2553 / 62 15761 an empty noodle packed with chopped up scallions on it
41 / 1 3535 men are taking pictures of a skater grinding on a car
40 / 1 11426 a woman with a baby in a stroller near a river
306 / 8 3653 a living room decorated with many loud patterns
38 / 1 23269 a big pan with three piazzas on it
35 / 1 10352 the young boy is setting down on his skateboard
69 / 2 142 a freeway sign states las vegas north in the background behind a yellow and blue fire hydrant
69 / 2 728 a giraffe is standing near the water with a lot of sailboats lined up
34 / 1 18313 a nice refrigerated micro wave combo on a wall
102 / 3 22158 a man with his eyes closed sits on a toilet with a blue doll placed on his crotch
135 / 4 19247 a mouse kept just besides a cats tail
160 / 5 9586 a lady dressed as a cow girl is parading down a street holding a flag
319 / 10 9460 a persons reflection is shown in the bathroom mirror they are photographing
4494 / 142 10253 some people playing with a disc in a grassy field
31 / 1 1048 a black and white photograph of a person that is in rushing waters
31 / 1 719 a commercial jet with its wheels down in a blue sky
91 / 3 23346 a messy desk with a computer cups glasses bottles books on the desk and the floor

In [179]:
extreme(score_w, score_s)[-10:],extreme(score_s, score_w)[:10]


Out[179]:
(array([19318, 17995,  5214, 11564,  9544, 12234, 19288, 20002, 17931, 19021]),
 array([19021, 17931, 20002, 19288, 12234,  9544, 11564,  5214, 17995, 19318]))

In [218]:
from collections import Counter
vocab = Counter((word for s in prov.iterSentences(split='train') for word in s['tokens']))

In [227]:
set_s = set(word for j in extreme(score_s, score_w)[:100] for word in sent[j]['tokens'])
set_w = set(word for j in extreme(score_w, score_s)[:100] for word in sent[j]['tokens'])
print numpy.sum([1 for word in set_s if vocab[word] < 5])
print numpy.sum([1 for word in set_w if vocab[word] < 5])
print [word for word in set_s if vocab[word] < 5]
print [word for word in set_w if vocab[word] < 5]


28
16
['piazzas', 'brooms', 'leaved', 'durham', 'sid', 'contryside', 'scull', 'zebras)', '(horses', 'thermometers', 'equine', 'loud', 'streeet', 'immediate', 'majority', 'gnaw', 'simplistic', 'inlaid', 'tufts', 'restricted', 'severe', 'scears', 'gnarled', 'birtd', 'humorously', 'bathtubs', 'attaches', 'devise']
['smies', 'whilst', 'tore', 'cairn', 'pubs', 'nutrition', 'macaw', '1929', 'goodmayes', 'chances', 'april', 'highland', 'pictogram', 'minds', 'hampster', 'burden']

In [217]:
for s in prov.iterSentences(split='train'):
    if 'streeet' in s['tokens']:
        print s['raw']


traffic is traveling both ways on the busy streeet
a bus is travling down a non crowded streeet
two trucks are parked side by side on the streeet
a streeet with buses and other vehicles and stop lights

In [30]:
def extreme_stats(good, worse, N=100):
    J = extreme(good, worse)[:N]
    L = [len(sent[j]['tokens']) for j in J]
    R = [good['ranks'][j] / worse['ranks'][j] for j in J]
    return (L, R)

In [32]:
Lw,Rw = extreme_stats(score_w, score_s)
Ls,Rs = extreme_stats(score_s, score_w)

In [36]:
numpy.max(Lw), numpy.max(Ls)


Out[36]:
(16, 47)

In [41]:
numpy.mean(Rw), numpy.mean(Rs)


Out[41]:
(0.0072925481796025475, 0.030546706215076193)

In [43]:
import pandas as pd

In [180]:
data = pd.DataFrame(dict(Length=numpy.hstack([Lw,Ls]), better=numpy.hstack([numpy.repeat("text",100), numpy.repeat("speech",100)])))

In [181]:
with open("error-length.txt","w") as f:
    f.write(data.to_csv(index=False))

In [120]:
images = list(prov.iterImages(split='val'))
from imaginet.evaluate import Cdist
from imaginet.simple_data import words

In [121]:
def retrieve(model, query, images, config):
        task = model.task
        scaler = model.scaler
        batcher = model.batcher
        mapper = batcher.mapper
        sents = [ query ] 
        sents_tok =  [ config['tokenize'](query) ]
        predictions = config['encode_sentences'](model, sents_tok, batch_size=32)
        img_fs = task.encode_images([ img['feat'] for img in images ])
        correct_img = numpy.array([ [ sents[i]['imgid']==images[j]['imgid']
                                      for j in range(len(images)) ]
                                    for i in range(len(sents)) ] )
        distances = Cdist(batch_size=2**13)(predictions, img_fs)
        row = distances[0]
        ranked = numpy.argsort(row)
        return ranked

In [123]:
coco = json.load(open("/home/gchrupala/reimaginet/data/coco/dataset.json"))

In [124]:
COCOID = {}

for img in coco['images']:
    COCOID[img['imgid']]=img['cocoid']

In [160]:
from IPython.display import Image
from IPython.core.display import HTML 

def display(imgid):
    return Image(url='http://mscoco.org/images/%d'%(COCOID[images2[imgid]['imgid']]))

In [165]:
def audio(query):
    return query['audio']
r_w = retrieve(model_w, sent[17931], images, dict(encode_sentences=vis2.encode_sentences, tokenize=words))
r_s = retrieve(model_s, sent[17931], images, dict(encode_sentences=audiovis.encode_sentences, tokenize=audio))

In [139]:
import imaginet.data_provider as dp2

In [141]:
prov2 = dp2.getDataProvider(dataset='coco', root='/home/gchrupala/reimaginet/', audio_kind=None)


Could not read file /home/gchrupala/reimaginet/data/coco/dataset.None.npy: audio features not available

In [142]:
images2 = list(prov2.iterImages(split='val'))

In [161]:
images[0]['imgid'], images[0]['sentences'][0]['raw']


Out[161]:
(0, 'a child holding a flowered umbrella and petting a yak')

In [162]:
sent[17931]['raw']


Out[162]:
'a yellow and white birtd is in flight'

In [163]:
display(r_w[0])


Out[163]:

In [164]:
display(r_s[1])


Out[164]:

In [156]:
for i in range(5):
    print images2[r_s[1]]['sentences'][i]['raw']


A parrot flying through a tree filled forest.
A yellow and white birtd is in flight.
A white bird spreads its wings among tree limbs.
A bird is jumping off of a branch.
A white bird in the air with wings outstretched.

In [169]:
def compare(i,j=0):
    r_w = retrieve(model_w, sent[i], images, dict(encode_sentences=vis2.encode_sentences, tokenize=words))
    r_s = retrieve(model_s, sent[i], images, dict(encode_sentences=audiovis.encode_sentences, tokenize=audio))
    return(sent[i]['raw'], r_s, r_w)

In [170]:
s, i1, i2 = compare(9544)

In [171]:
s


Out[171]:
'while in the immediate foreground juts a gnarled tree branch the majority of the view consists of a an expanse of short grass dotted with a few longer tufts and a number of scattered grazing sheep'

In [175]:
display(i1[1])


Out[175]:

In [176]:
display(i2[1])


Out[176]:

In [182]:
compare(11564)


Out[182]:
('a woman standing in the middle of a farmer marked',
 array([2312, 1088, 1459, ..., 3935, 3049, 4719]),
 array([ 188, 1563, 1335, ..., 3982, 3734, 2906]))

In [183]:
display(2312)


Out[183]:

In [185]:
display(188)


Out[185]:

In [190]:
compare(10991,j=1)


Out[190]:
('to scears going up a steep side of a mountain together',
 array([2198,  691, 2193, ...,  858, 3080,  340]),
 array([ 703, 2193, 1953, ..., 2491,  340, 3114]))

In [188]:
display(2198)


Out[188]:

In [189]:
display(703)


Out[189]:

In [192]:
display(2193)


Out[192]:

In [193]:
display(1953)


Out[193]:

In [ ]: