Skip to content

Commit c323e08

Browse files
committed
Changing the structure of project.
1 parent 99d0857 commit c323e08

18 files changed

+31
-25
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

root/dataset/api.py renamed to dataset/api.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
from collections import namedtuple
33
from keras.utils import to_categorical
44
from keras_preprocessing.sequence import pad_sequences
5-
from root.constants import NO_ENTITY_TOKEN, MAX_LEN, PAD
5+
from constants import NO_ENTITY_TOKEN, MAX_LEN, PAD
66
from .data_processor import numericalize
77
from .vocab import TextVocab, LabelVocab, PosVocab, CharacterVocab
88

99

1010
def load_dataset():
1111
# load examples
12-
train_examples = load_examples('../dataset/raw/train.txt')
13-
val_examples = load_examples('../dataset/raw/valid.txt')
14-
test_examples = load_examples('../dataset/raw/test.txt')
12+
train_examples = load_examples('data/raw/train.txt')
13+
val_examples = load_examples('data/raw/valid.txt')
14+
test_examples = load_examples('data/raw/test.txt')
1515

1616
# build vocabularies
1717
text_vocab = TextVocab.build(list(map(lambda e: e.sentence, train_examples)))
@@ -30,7 +30,7 @@ def load_examples(file_path):
3030
"""
3131
Loads sentences from file in CoNLL 2003 format.
3232
33-
:param file_path: Path to file with CoNLL data.
33+
:param file_path: Path to file with CoNLL dataset.
3434
:return: list(Example)
3535
"""
3636
examples = []
File renamed without changes.

root/dataset/vocab.py renamed to dataset/vocab.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from abc import ABC, abstractmethod
22
from collections import defaultdict, Counter
3-
from root.constants import PAD, UNK
3+
from constants import PAD, UNK
44

55

66
class Vocab(ABC):

embedding/glove.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import sys
22
import numpy as np
33

4-
GLOVE_DIR = '../embedding/glove.6B.100d.txt'
4+
GLOVE_DIR = 'embedding/glove.6B.100d.txt'
55

66

77
def get_pretrained_glove(num_words, text_vocab):

root/model.py renamed to model.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from keras.utils.vis_utils import plot_model
88
from keras.callbacks import TensorBoard
99

10-
from root.constants import MAX_LEN
10+
from constants import MAX_LEN
1111

1212

1313
class NeuralNetwork(object):
@@ -64,7 +64,7 @@ def train(self, epochs, embedding=None):
6464

6565
model.compile(optimizer="rmsprop", loss='categorical_crossentropy', metrics=['accuracy'])
6666

67-
plot_model(model, to_file='../models/ner_model_image.png')
67+
plot_model(model, to_file='models/ner_model_image.png')
6868
print(model.summary())
6969

7070
model.compile(optimizer="rmsprop", metrics=['accuracy'], loss='categorical_crossentropy')
@@ -93,16 +93,16 @@ def train(self, epochs, embedding=None):
9393

9494

9595
def create_dir():
96-
runs = ([x[0] for x in os.walk("../results/logs")])
96+
runs = ([x[0] for x in os.walk("results/logs")])
9797
runs = [x for x in runs if "run" in x]
9898
runs = list(map(int, re.findall(r'\d+', "".join(runs))))
9999
runs.sort()
100100
if len(runs) == 0:
101-
return "../results/logs/run1"
101+
return "results/logs/run1"
102102

103103
dir_idx = runs[-1] + 1
104104

105-
dir = "../results/logs/run" + str(dir_idx)
105+
dir = "results/logs/run" + str(dir_idx)
106106

107107
if not os.path.exists(dir):
108108
os.makedirs(dir)

root/predict.py renamed to predict.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
1+
import os
12
import argparse
23
import spacy
34
import numpy as np
45
from keras.models import load_model
5-
from root.dataset.data_processor import numericalize
6+
from dataset.data_processor import numericalize
67
from utils.serialization import load_object
7-
from root.constants import NO_ENTITY_TOKEN
8+
from constants import NO_ENTITY_TOKEN
89

910

1011
def parse_args():
1112
parser = argparse.ArgumentParser(description='Script for using NER model')
1213
parser.add_argument('-p', '--path', help='Path to model and vocabulary directory.')
13-
return parser.parse_args()
14+
15+
args = parser.parse_args()
16+
# add path separator (/) at the end if needed
17+
args.path = args.path if args.path[-1] == os.path.sep else args.path + os.path.sep
18+
19+
return args
1420

1521

1622
def main():

root/test_model.py renamed to test_model.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from keras.models import load_model
66
from sklearn.metrics import confusion_matrix, precision_recall_fscore_support
77

8-
from root.constants import MAX_LEN
8+
from constants import MAX_LEN
99
from utils.classification_report import classification_report
1010
from utils.plot_confusion_matrix_util import plot_confusion_matrix
1111

@@ -41,13 +41,13 @@ def test_model(model_path, test, text_vocab, labels_vocab):
4141
print(report)
4242

4343
# plot_classification_report(report)
44-
# plt.savefig('../results/classification_report.png', dpi=200, format='png', bbox_inches='tight')
44+
# plt.savefig('results/classification_report.png', dpi=200, format='png', bbox_inches='tight')
4545
# plt.close()
4646

4747
# Confusion Matrix
4848
cnf_matrix = confusion_matrix(true_values, predicted_values)
4949
np.set_printoptions(precision=2)
5050
# TODO fix classes
5151
plot_confusion_matrix(cnf_matrix, classes=list(labels_vocab.stoi.keys()), normalize=True, title='Normalized confusion matrix')
52-
plt.savefig('../results/confusion_matrix.png', dpi=200, format='png', bbox_inches='tight')
52+
plt.savefig('results/confusion_matrix.png', dpi=200, format='png', bbox_inches='tight')
5353
plt.close()

root/main.py renamed to train.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import matplotlib.pyplot as plt
22

33
from embedding.glove import get_pretrained_glove
4-
from root.dataset.api import load_dataset
5-
from root.model import NeuralNetwork
6-
from root.test_model import test_model
4+
from dataset.api import load_dataset
5+
from model import NeuralNetwork
6+
from test_model import test_model
77
from datetime import datetime
88
from utils.serialization import save_object
99

@@ -15,7 +15,7 @@
1515
num_chars = len(character_vocab.itos)
1616

1717
# save vocabulary
18-
save_path = '../models/' + datetime.now().strftime("%Y-%m-%d-%H:%M") + '/'
18+
save_path = 'models/' + datetime.now().strftime("%Y-%m-%d-%H:%M") + '/'
1919
save_object(text_vocab, save_path + 'text_vocab')
2020
save_object(labels_vocab, save_path + 'labels_vocab')
2121

@@ -34,7 +34,7 @@
3434
plt.ylabel('Accuracy')
3535
plt.xlabel('Epoch')
3636
plt.legend(['train', 'validation'], loc='lower right')
37-
plt.savefig('../results/model_accuracy.png', dpi=200, format='png', bbox_inches='tight')
37+
plt.savefig('results/model_accuracy.png', dpi=200, format='png', bbox_inches='tight')
3838
plt.close()
3939

4040
# Plot loss
@@ -44,5 +44,5 @@
4444
plt.ylabel('Loss')
4545
plt.xlabel('Epoch')
4646
plt.legend(['train', 'validation'], loc='upper right')
47-
plt.savefig('../results/model_loss.png', dpi=200, format='png', bbox_inches='tight')
47+
plt.savefig('results/model_loss.png', dpi=200, format='png', bbox_inches='tight')
4848
plt.close()

utils/classification_report.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def classification_report(y_true, y_pred, labels=None, target_names=None,
2424
digits : int
2525
Number of digits for formatting output floating point values
2626
average : string, ['weighted' (default), 'binary', 'micro', 'macro']
27-
Determines the type of averaging performed on the data, after reporting the individual results per class:
27+
Determines the type of averaging performed on the dataset, after reporting the individual results per class:
2828
``'binary'``:
2929
Only report results for the class specified by ``pos_label``.
3030
This is applicable only if targets (``y_{true,pred}``) are binary.

0 commit comments

Comments
 (0)