예제 #1
0
    def evaluate_wrt_sbj_entity(self, x_test, y_test, pos_test, neg_test, pos_name_level_test, neg_name_level_test,
                                predicates_per_question):
        pred_words_ids = self._pred_words_ids
        pred_names_ids = self._pred_names_ids

        pred_words_ids = ModelUtils.pad_seq(pred_words_ids, self._max_label_words_len)
        pred_names_ids = ModelUtils.pad_seq(pred_names_ids, self._max_label_names_len)

        labels_tile_ = [np.expand_dims(np.array([pred_words_ids[cand] for cand in list(set(rel_cand))]), axis=0) for
                        rel_cand in predicates_per_question]

        labels_tile_names = [np.expand_dims(np.array([pred_names_ids[cand] for cand in list(set(rel_cand))]), axis=0)
                             for rel_cand in predicates_per_question]

        predictions = []
        for i in range(len(x_test)):
            if len(labels_tile_[i].shape) >= 3:

                prediction = self.model.predict(
                    [[x_test[i]], pos_test, neg_test, labels_tile_[i], pos_name_level_test, neg_name_level_test,
                     labels_tile_names[i]], batch_size=1)

                predictions.append(list(set(predicates_per_question[i]))[np.argmax(prediction, -1)[0]])
            else:
                predictions.append(0)

        print(np.mean(predictions == np.argmax(a=y_test, axis=-1)))

        return predictions
    def _load_data_with_neg(self, path, dataset_name):
        df = pd.read_csv(path + dataset_name + "/data.csv", usecols=[3, 4])
        x = df["data"].apply(ast.literal_eval).to_list()
        y = df["targets"]
        y = np.array(y)
        # padding
        x = keras.preprocessing.sequence.pad_sequences(
            x,
            maxlen=self._max_seq_len,
            dtype="int32",
            padding="post",
            value=0)

        pred_intersect = ModelUtils.predicate_intersection_wrt_words(
            self.pred_words, self._domain_intersection)

        if self._remove_domains is not None:
            x, y = self._zero_shot_setting(path, dataset_name, x, y)
        if self._one_go and self._incl_names:
            # creates dataset in a way that distances of
            # negative samples per question can be computed in one-go
            pos_w_l, neg_w_l, pos_n_l, neg_n_l = ModelUtils.create_negatives2_onego(
                x, y, self._max_seq_len, self._max_label_words_len,
                self._max_label_names_len, self._total_negatives[dataset_name],
                self._negatives_intersection[dataset_name], pred_intersect,
                self.pred_words_ids, self.pred_names_ids)

            # one hot
            y = keras.utils.to_categorical(y, num_classes=self._num_classes)

            return x, y, pos_w_l, neg_w_l, pos_n_l, neg_n_l

        elif (not self._one_go) and self._incl_names:
            # include both word level and name level
            x, y, pos_w_l, neg_w_l, pos_n_l, neg_n_l = ModelUtils.create_negatives2(
                x, y, self._max_seq_len, self._max_label_words_len,
                self._max_label_names_len, self._total_negatives[dataset_name],
                self._negatives_intersection[dataset_name], pred_intersect,
                self.pred_words_ids, self.pred_names_ids)

            # one hot
            y = keras.utils.to_categorical(y, num_classes=self._num_classes)

            return x, y, pos_w_l, neg_w_l, pos_n_l, neg_n_l

        elif (not self._one_go) and (not self._incl_names):
            # include only word level
            x, y, pos_labels, neg_labels = ModelUtils.create_negatives(
                x, y, self._max_seq_len, self._max_label_words_len,
                self._total_negatives[dataset_name],
                self._negatives_intersection[dataset_name], pred_intersect,
                self.pred_words_ids)

            # one hot
            y = keras.utils.to_categorical(y, num_classes=self._num_classes)

            return x, y, pos_labels, neg_labels
예제 #3
0
    def evaluate_test(self, x_test, y_test, pos_test, neg_test, pos_name_level_test, neg_name_level_test):
        pred_words_ids = self._pred_words_ids
        pred_names_ids = self._pred_names_ids

        pred_words_ids = ModelUtils.pad_seq(pred_words_ids, self._max_label_words_len)
        pred_names_ids = ModelUtils.pad_seq(pred_names_ids, self._max_label_names_len)

        labels_tile = np.tile(pred_words_ids, [x_test.shape[0], 1, 1])
        labels_tile_names = np.tile(pred_names_ids, [x_test.shape[0], 1, 1])

        predictions = self.model.predict(
            [x_test, pos_test, neg_test, labels_tile, pos_name_level_test, neg_name_level_test, labels_tile_names],
            batch_size=64, verbose=1)
        print('Accuracy: ', np.sum(np.argmax(predictions, axis=-1) == np.argmax(y_test, axis=-1)) / y_test.shape[0])
        return predictions
    def __init__(self, max_seq_len=36, embed_dim=300, n_tags=2, ix2word=None):

        self._max_seq_len = max_seq_len
        self._embed_dim = embed_dim
        self._n_tags = n_tags
        self._ix2word = ix2word
        self._embedding_matrix = ModelUtils.emb_matrix(self._ix2word)
        self._nb_words = self._embedding_matrix.shape[0]

        super().__init__()
    def _load_data(self, path, dataset_name):
        """
        :param path: path to data
        :param dataset_name: "train", "test", "validation"
        :return: np arrays of data, targets and relations (text)
        """

        df = pd.read_csv(
            path + dataset_name + "/data.csv",
            usecols=[1, 3, 4])  # , names=["relation","data","annotation"])
        x = df["data"].apply(ast.literal_eval).to_list()
        y = df["annotation"].apply(ast.literal_eval).to_list()
        r = df["relation"].to_list()
        r = np.array(r)

        # padding
        x = ModelUtils.pad_seq(x, self._max_seq_len)
        y = ModelUtils.pad_seq(y, self._max_seq_len)

        # categorical values for labels
        y = [to_categorical(i, num_classes=self._n_tags) for i in y]
        y = np.array(y)

        return x, y, r
예제 #6
0
    x = model.output
    x = SpatialPyramidPooling([1, 2, 3, 6])(x)
    # x = Dense(1024, activation='relu')(x)
    predictions = Dense(classes, activation='softmax')(x)
    model = Model(inputs=model.input,
                  outputs=predictions,
                  name='inception_v3_spp')
    return model


if __name__ == "__main__":
    start = datetime.now()
    model = InceptionV1(include_top=True,
                        input_shape=(224, 224, 3),
                        weights=None,
                        classes=2)
    # model = make_model()

    model.summary()
    util = ModelUtils(epochs=120)
    # util.get_train_data(resize=(224, 224))

    # util.train(model)
    # util.evaluate()
    # util.save()
    # util.confusion_matrix()
    # util.plot_loss_accuracy()
    util.plot_multiple_roc(model, (224, 224))

    time_elapsed = datetime.now() - start
    print('Time elapsed (hh:mm:ss.ms) {}'.format(time_elapsed))
예제 #7
0
    # CREATE MODEL
    model = ResNet50(include_top=False,
                     input_shape=(None, None, 3),
                     weights='imagenet')
    x = model.output
    x = SpatialPyramidPooling([1, 2, 3, 6])(x)
    predictions = Dense(classes, activation='softmax')(x)
    model = Model(inputs=model.input,
                  outputs=predictions,
                  name='resnet50spp_pretrained')
    return model


if __name__ == "__main__":
    start = datetime.now()
    model = make_model()

    model.summary()
    util = ModelUtils(epochs=100)
    util.get_train_data()
    util.get_val_data()
    util.get_test_data()
    util.mean_subtraction()
    util.train(model)
    util.evaluate()
    util.save()
    util.confusion_matrix()
    util.plot_loss_accuracy()

    time_elapsed = datetime.now() - start
    print('Time elapsed (hh:mm:ss.ms) {}'.format(time_elapsed))
예제 #8
0
from datetime import datetime
from utils.model_utils import ModelUtils
from keras.layers import Dense, Dropout, Flatten, Activation, Conv2D, GlobalAveragePooling2D
from keras.models import Model

ACTIVATION='Mish'
if __name__ == "__main__":
    start = datetime.now()
    # CREATE MODEL 

    # # this is the model we will train
    vgg = VGG19(input_shape=(224, 224, 3), classes=3, activation=ACTIVATION, include_top=False, weights='imagenet')
    model = vgg.model()
    # model = set_non_trainable(model)
    x = model.output
    x=Dense(4096,activation=ACTIVATION)(x) 
    x=Dense(4096,activation=ACTIVATION)(x) 
    x=Dense(3,activation='softmax')(x) 
    model = Model(model.input, x, name='vgg19')
    model.summary()
    util = ModelUtils(epochs=20)
    util.get_train_data(resize=(224,224))

    util.train(model, name=ACTIVATION)
    util.evaluate()
    util.save(name=ACTIVATION)
    util.confusion_matrix(title=model.name)
    util.plot_loss_accuracy(path=model.name+'.json', name=model.name)
    
    time_elapsed = datetime.now() - start 
    print('Time elapsed (hh:mm:ss.ms) {}'.format(time_elapsed))
예제 #9
0
from datetime import datetime
from utils.model_utils import ModelUtils
import tensorflow as tf
tf.set_random_seed(1000)
random.seed(1000)
np.random.seed(1000)
MODEL_SIZE=(227, 227)

if __name__ == "__main__":
    start = datetime.now()
    # CREATE MODEL 
    alexnet = AlexNet(input_shape=(227,227, 3), classes=2)

    model = alexnet.model()

    model.summary()

    util = ModelUtils(epochs=120)
    util.get_train_data()
    # util.get_val_data(resize=(MODEL_SIZE))

    # util.train(model)
    # util.evaluate()
    # util.save()
    # util.confusion_matrix()
    # util.plot_roc_curve()
    # util.plot_loss_accuracy()
    util.plot_multiple_roc(model)
    
    time_elapsed = datetime.now() - start 
    print('Time elapsed (hh:mm:ss.ms) {}'.format(time_elapsed))
    return InceptionV1SPP(include_top=True,
                          input_shape=(224, 224, 3),
                          weights=None,
                          classes=2)


def resnet50():
    return ResNet50(include_top=True,
                    input_shape=(224, 224, 3),
                    weights=None,
                    classes=2)


def resnet50SPP():
    return ResNet50SPP(include_top=True,
                       input_shape=(224, 224, 3),
                       weights=None,
                       classes=2)


if __name__ == "__main__":
    start = datetime.now()
    util = ModelUtils(epochs=120)
    util.get_train_data()
    util.get_results([alexNet().model(), alexNetSPP().model()])
    # util.get_train_data(resize=MODEL_SIZE)
    util.get_results([googlenet(), googlenetSPP(), resnet50(), resnet50SPP()])
    # util.get_train_data(resize=MODEL_SIZE)

    # util.get_results([googlenet(), googlenetSPP(), resnet50(), resnet50SPP()])
예제 #11
0
sys.path.append("..")
from data_utils import *
# from resnet50 import  ResNet50
from datetime import datetime
from keras.applications import ResNet50
from utils.model_utils import ModelUtils

MODEL_SIZE = (224, 224)

if __name__ == "__main__":
    start = datetime.now()
    # CREATE MODEL
    model = ResNet50(include_top=True,
                     input_shape=(224, 224, 3),
                     weights=None,
                     classes=2)
    # model = resnet50.model()

    model.summary()
    util = ModelUtils(epochs=120)
    util.get_train_data(resize=(224, 224))

    # util.train(model)
    # util.evaluate()
    # util.save()
    # util.confusion_matrix()
    # util.plot_loss_accuracy()
    util.plot_multiple_roc(model, (224, 224))

    time_elapsed = datetime.now() - start
    print('Time elapsed (hh:mm:ss.ms) {}'.format(time_elapsed))
예제 #12
0
 def delete_model_byId(model_id):
     model = ModelInfo.objects.get(pk=model_id)
     model.delete()
     ModelUtils.update_model_dir(ModelInfo, "model_dir")
from datetime import datetime
from utils.model_utils import ModelUtils

DATASET_PATH = '../data/train/'
TEST_PATH = 'D:\Data/test/'
TEST_PATH_NAME = os.path.join(TEST_PATH, 'china.pkl')
IMAGESET_NAME = os.path.join(DATASET_PATH, 'china.pkl')

if __name__ == "__main__":
    start = datetime.now()
    # CREATE MODEL
    alexnet = AlexNet(input_shape=(227, 227, 3), classes=2)

    model = alexnet.model()
    model.summary()
    import pdb
    pdb.set_trace()

    util = ModelUtils(epochs=120)
    # util.get_train_data()
    # util.get_val_data(resize=(227, 227))

    # util.train(model)
    # util.evaluate()
    # util.save()
    # util.confusion_matrix()
    # util.plot_roc_curve()
    util.plot_multiple_roc(model)

    time_elapsed = datetime.now() - start
    print('Time elapsed (hh:mm:ss.ms) {}'.format(time_elapsed))
예제 #14
0
import sys
sys.path.append("..")
from data_utils import *
from alexnet import AlexNet
from datetime import datetime
from utils.model_utils import ModelUtils
from keras.layers import Dense, Dropout, Flatten, Activation, Conv2D, GlobalAveragePooling2D
from keras.models import Model
from keras.applications.vgg16 import VGG16

if __name__ == "__main__":
    start = datetime.now()
    # CREATE MODEL

    # # this is the model we will train
    alexnet = AlexNet(input_shape=(224, 224, 3), classes=3)
    model = alexnet.model()
    model.summary()

    util = ModelUtils(epochs=50)
    util.get_train_data(resize=(224, 224))

    util.train(model)
    util.evaluate()
    util.save()
    util.confusion_matrix(title="AlexNet")
    util.plot_loss_accuracy(path=model.name + '.json', name="AlexNet")

    time_elapsed = datetime.now() - start
    print('Time elapsed (hh:mm:ss.ms) {}'.format(time_elapsed))