forked from Marhabibi/DeepTable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeepTableEval.py
57 lines (45 loc) · 1.78 KB
/
DeepTableEval.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
from inputvars import *
md1, output, inp = inputvarsEval(sys.argv[1:], sys.argv[0])
import pandas as pd
import numpy as np
import random
import pickle
import os
os.environ['KERAS_BACKEND'] = 'tensorflow'
import keras
from keras.layers import Embedding, Dense, Input, Flatten, Conv1D, Conv2D, MaxPooling1D, Embedding, Concatenate, \
Dropout, AveragePooling1D, LSTM, GRU, Bidirectional, TimeDistributed, Convolution2D, MaxPooling2D, AveragePooling2D, \
Permute
from keras.layers.core import Permute
from keras.models import Model, Sequential, load_model
from sklearn.metrics import classification_report as cr
from sklearn.metrics import confusion_matrix as cm
from keras import backend as K
np.random.seed(813306)
from input_transformation import *
def read_input(inp):
with open(inp, 'rb') as f:
[data, Y, dicTab] = pickle.load(f)
train_size = int(Y.shape[0] * 0.75)
return data[train_size:, :, :, :], Y[train_size:]
if __name__ == "__main__":
# variable initialization
MAX_COL = 9
MAX_COL_LENGTH = 9
MAX_CELL_LENGTH = 4
X_test, y_test, dictionary = transform_tables(inp, "test")
# load model
model = load_model(md1)
# predict labels
pred = model.predict(X_test, verbose=1)
refs = [r.tolist().index(max(r.tolist())) for r in y_test]
preds = [p.tolist().index(max(p.tolist())) for p in pred]
# write predictions in a file
refs_preds = pd.DataFrame(
[(r.tolist().index(max(r.tolist())), p.tolist().index(max(p.tolist()))) for r, p in zip(y_test, pred)],
columns=["reference", "prediction"])
refs_preds.to_csv(output + ".csv", index=False)
# display performances
print(cr(refs, preds, digits=4))
print("confusion_matrix:\n", cm(refs, preds))
print("predictions are saved in \"" + output + ".csv\"")