-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredicting.py
233 lines (200 loc) · 6.77 KB
/
predicting.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
'''
A Dynamic Recurrent Neural Network (LSTM) implementation example using
TensorFlow library. This example is using a toy dataset to classify linear
sequences. The generated sequences have variable length.
Long Short Term Memory paper: http://deeplearning.cs.cmu.edu/pdfs/Hochreiter97_lstm.pdf
Author: Aymeric Damien
Project: https://github.com/aymericdamien/TensorFlow-Examples/
'''
from __future__ import print_function
import tensorflow as tf
import random
dimMot = 128
dimImgs = 4096
nbExTot = 20
longMaxLeg = 39
#don't forget to add the BEGIN and END word to all captions in the input data
# ====================
# TOY DATA GENERATOR
# ====================
def litEtTraiteTsLesExemples():
#one line = idImage#4096 nbs representing the image
#return tf.constant(tab)
monFichier = open("imATesterVal20-40Imgs.txt", "r")
resImgs = []
idImgs = []
for curL in monFichier :
premSplit = curL.split('#')
if len(premSplit) >= 2:
res1Im = []
estPrem = True
for curV in premSplit:
if estPrem :
#print(curV)
idImgs += [curV]
estPrem = False
else :
#split2 = curV.split(' ')
#resImgs += [[float(val) for val in split2]]
split2 = curV.split(' ')
tab = []
for val in split2:
if(len(val) > 0 and val != "\n"):
#print(val)
tab += [float(val)]
resImgs += [[tab]]
monFichier.close()
return idImgs, resImgs
def litDico():
#one line: a word#its coordinates
monFichier = open("dico_lemma.txt", "r")
listeMots = []
cooMots = []
j = 0
for curL in monFichier :
j+=1
premSplit = curL.split('#')
estPrem = True
estBon = True
estDeb = False
for curV in premSplit:
if estPrem :
if(len(curV) == 0):
print(j)
estBon = False
else:
listeMots += [curV]
if curV == "DEB":
estDeb = True
estPrem = False
else :
if estBon :
split2 = curV.split(' ')
tab = []
for val in split2:
if(len(val) > 0 and val != "\n"):
#print(val)
tab += [float(val)]
cooMots += [tab]
if estDeb :
estDeb = False
cooDEB = tab[:]
#print("done", cooDEB)
#split2 = curV.split(' ')
#cooMots += [[float(val) for val in split2]]
monFichier.close()
return listeMots, cooMots, cooDEB
idImgs, imgsLues = litEtTraiteTsLesExemples()
listeMots, cooMots, cooDEB = litDico()
def dist(coo1, coo2):
res = 0
for i in range(len(coo1)):
res += (coo1[i] - coo2[i]) * (coo1[i] - coo2[i])
return res
def motPlusProche(listeCoo):
motsPP = []
for i in range(len(listeCoo)):
motsPP += [(0, dist(listeCoo[i], cooMots[0]))]
for i in range(len(cooMots)):
for motGen in range(len(motsPP)):
(oldId, oldDist) = motsPP[motGen]
if dist(listeCoo[motGen], cooMots[i]) < oldDist:
motsPP[motGen] = (i, dist(listeCoo[motGen], cooMots[i]))
resu = []
for (i, cooM) in motsPP:
resu += [cooMots[i]]
return resu
# ==========
# MODEL
# ==========
# Parameters
learning_rate = 0.001
training_iters = 10000#1000000
batch_size = 20 #128 avt
display_step = 10
# Network Parameters
seq_max_len = longMaxLeg # Sequence max length ie nbMotsMax dans la legende
n_hidden = 2024 # hidden layer num of features
n_classes = dimMot
with tf.device('/gpu:0'):
x = tf.placeholder("float", [None, seq_max_len, dimMot])
# A placeholder for indicating each sequence length
seqlen = tf.placeholder(tf.int32, [None])
#the images
imgs = tf.placeholder("float", [None, 1, dimImgs])
# Define weights
weights = {
'out': tf.Variable(tf.random_normal([n_hidden, n_classes])),
'in' : tf.Variable(tf.random_normal([dimImgs, n_classes]))
}
biases = {
'out': tf.Variable(tf.random_normal([n_classes])),
'in': tf.Variable(tf.random_normal([n_classes]))
}
def dynamicRNN(x, imgs, seqlen, weights, biases):
#get images into the right dimension using the learned matrix
imgsBonFormat = [tf.matmul(tf.gather(imgs, i), weights['in']) + biases['in'] for i in range(batch_size)]
bonX = tf.concat([imgsBonFormat, x], 1)#on concatene mots et image pour chaque exemple
# Prepare data shape to match `rnn` function requirements
# Current data input shape: (batch_size, n_steps, n_input)
# Required shape: 'n_steps' tensors list of shape (batch_size, n_input)
# Permuting batch_size and n_steps
bonX = tf.transpose(bonX, [1, 0, 2])
# Reshaping to (n_steps*batch_size, n_input)
bonX = tf.reshape(bonX, [-1, dimMot])
# Split to get a list of 'n_steps' tensors of shape (batch_size, n_input)
bonX = tf.split(num_or_size_splits=seq_max_len + 1, axis = 0, value = bonX)
# Define a lstm cell with tensorflow
lstm_cell = tf.contrib.rnn.BasicLSTMCell(n_hidden)
outputs, states = tf.contrib.rnn.static_rnn(lstm_cell, bonX, dtype=tf.float32)
# 'outputs' is a list of output at every timestep
#we multiply by W and add bias at every step
newOutput = [tf.matmul(curOut, weights['out']) + biases['out'] for curOut in outputs]
#we take out the last word
newOutput = tf.stack(newOutput[:-1])
# and change back dimension to [batch_size, n_step, n_input]
newOutput = tf.transpose(newOutput, [1, 0, 2])
return newOutput # dimension : nbExs * nbSteps - 1 * wordDimension
output = dynamicRNN(x, imgs, seqlen, weights, biases)
# Initializing the variables
init = tf.global_variables_initializer()
saver = tf.train.Saver()
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.2)
configu = tf.ConfigProto(allow_soft_placement = True, gpu_options=gpu_options)
# Launch the graph
#with tf.Session() as sess:
with tf.Session(config = configu) as sess:
sess.run(init)
saver.restore(sess, "/users/eleves-b/2015/anouk.paradis/model/rapTr25000nH2024Ite1000000.ckpt")
print("Model restored.")
legendes = [[cooDEB] + [[0.0 for j in range(dimMot)] for i in range(longMaxLeg - 1)] for i in range(len(idImgs))]
seqLen = [longMaxLeg for i in range(len(idImgs))]
print("legendes :", len(legendes))
print(len(legendes[0]), len(legendes[0][0]))
print("imgs :", len(imgsLues))
#print("idImgs", idImgs)
#print("legendes", legendes)
#print("seqlen", seqLen)
for i in range(longMaxLeg - 1):
print("mot no", i)
res = sess.run(output, {x: legendes, seqlen: seqLen, imgs: imgsLues})
nouvMots = []
for curI in range(len(legendes)):
nouvMots += [res[curI][i + 1]]
bonMots = motPlusProche(nouvMots)
print(bonMots)
for curI in range(len(legendes)):
legendes[curI][i + 1] = bonMots[curI]
#res = sess.run({"out": output, "state":states}, {x: legendes, seqlen: seqLen, imgs: imgsLues})
#print("output ", res)
fichSortie = open("res20-40ImgsVal.txt","w")
#print(legendes)
for i in range(len(idImgs)):
fichSortie.write(idImgs[i])
fichSortie.write("#")
for j in range(longMaxLeg):
for k in range(dimMot):
fichSortie.write(str(legendes[i][j][k]))
fichSortie.write(' ')
fichSortie.write("#")
fichSortie.write("\n")