-
Notifications
You must be signed in to change notification settings - Fork 0
/
vsqa.py
102 lines (86 loc) · 2.72 KB
/
vsqa.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import urllib
import sys
import os
import zipfile
import tarfile
import json
import hashlib
import re
import itertools
import cPickle
import time
import pandas as pd
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
def fill_unk(unk):
global glove_wordmap
glove_wordmap[unk] = RS.multivariate_normal(m,np.diag(v))
return glove_wordmap[unk]
def sentence2sequence(sentence):
tokens = sentence.strip('"(),-').lower().split(" ")
rows = []
words = []
for token in tokens:
i = len(token)
while len(token) > 0:
word = token[:i]
if word in glove_wordmap:
rows.append(glove_wordmap[word])
words.append(word)
token = token[i:]
continue
else:
i = i-1
if i==0:
rows.append(fill_unk(token))
words.append(token)
break
return np.array(rows),words
def contextualize(questions):
ques= []
ans=[]
for q in questions:
ques.append(sentence2sequence(q))
return ques
def Compute(df):
images = df[1]
l = []
for name in images:
file = tf.read_file('VQAMed2018Train-QA.csv'+name+'jpg')
img = tf.image.decode_jpeg(file, channels=3)
resized_image = tf.image.resize_images(img, [300, 300])
print name+" "+str(resized_image)
l.append(resized_image)
X = tf.placeholder(tf.float32, shape=(None,300,300, 3), name="X")
with tf.name_scope("cnn"):
conv1 = tf.layers.conv2d(inputs=X,filters=64,kernel_size=[5, 5],padding="same",activation=tf.nn.relu,name='conv1')
pool1 = tf.layers.max_pooling2d(inputs=conv1, pool_size=[2, 2], strides=2,name='pool1')
norm1 = tf.layers.batch_normalization(inputs=pool1,axis=-1,name = "norm1")
conv2 = tf.layers.conv2d(inputs=norm1,filters=64,kernel_size=[5, 5],padding="same",activation=tf.nn.relu,name='conv2')
pool2 = tf.layers.max_pooling2d(inputs=conv2, pool_size=[2, 2],strides=2,name="pool2")
norm2 = tf.layers.batch_normalization(inputs=pool2,axis=-1,name = "norm2")
norm2 = tf.contrib.layers.flatten(norm2)
fc1 = tf.layers.dense(inputs=norm2, units=256, activation=tf.nn.relu,name="fc1")
fc2 = tf.layers.dense(inputs=fc1, units=128, activation=tf.nn.relu,name="fc2")
if __name__ == '__main__':
#Reading File
df = pd.read_csv('VQAMed2018Train-QA.csv', sep='\t',header=None)
glove_wordmap = {}
glove_vectors_file="glove.6B.50d.txt"
with open(glove_vectors_file, "r") as glove:
for line in glove:
name, vector = tuple(line.split(" ", 1))
glove_wordmap[name] = np.fromstring(vector, sep=" ")
wvecs=[]
for item in glove_wordmap.items():
wvecs.append(item[1])
s=np.vstack(wvecs)
v=np.var(s,0)
m=np.mean(s,0)
RS = np.random.RandomState()
train_data=contextualize(df[2])
# test_data=contextualize("VQAMed2018Valid-QA.csv")
print train_data[0][0].shape