from utils.models import cnn_midfusion_model_v2, lstm_model import sys import math import numpy as np import os if __name__ == "__main__": dataset = sys.argv[1] method = sys.argv[2] proto_num = int(sys.argv[3]) os.environ["CUDA_VISIBLE_DEVICES"] = sys.argv[4] max_seq_lenth = max_seq_len(dataset) nb_class = nb_classes(dataset) dim_num = nb_dims(dataset) nb_cnn = int(round(math.log(max_seq_lenth, 2)) - 3) #model = cnn_raw_model(nb_cnn, proto_num, max_seq_lenth, nb_class) #model = cnn_dtwfeatures_model(nb_cnn, proto_num, max_seq_lenth, nb_class) #model = cnn_earlyfusion_model(nb_cnn, proto_num, max_seq_lenth, nb_class) model = cnn_midfusion_model_v2(nb_cnn, dim_num, proto_num, max_seq_lenth, nb_class) #model = cnn_latefusion_model(nb_cnn, proto_num, max_seq_lenth, nb_class) print("Number of Pooling Layers: %s" % str(nb_cnn)) train_model(model, dataset, method, proto_num,
def load_dataset_at(index, method, proto_num, normalize_timeseries=False, verbose=True) -> (np.array, np.array): dim = nb_dims(index) train_data1 = "data/all-raw-train-data-%s-%s-%s.txt" % (index, method, str(proto_num)) test_data1 = "data/all-raw-test-data-%s-%s-%s.txt" % (index, method, str(proto_num)) train_data2 = "data/all-dtw_features-train-data-%s-%s-%s.txt" % ( index, method, str(proto_num)) test_data2 = "data/all-dtw_features-test-data-%s-%s-%s.txt" % ( index, method, str(proto_num)) train_labels = "data/all-train-label-%s-%s-%s.txt" % (index, method, str(proto_num)) test_labels = "data/all-test-label-%s-%s-%s.txt" % (index, method, str(proto_num)) is_timeseries = True # assume all input data is univariate time series if os.path.exists(train_data1): df = pd.read_csv(train_data1, delimiter=' ', header=None, encoding='latin-1') else: raise FileNotFoundError('File %s not found!' % (train_data1)) X_train1 = df.values X_train1 = np.reshape( X_train1, (np.shape(X_train1)[0], dim, int(np.shape(X_train1)[1] / (dim)))) if normalize_timeseries: X_train1_min = np.min(X_train1) X_train1_max = np.max(X_train1) X_train1 = 2. * (X_train1 - X_train1_min) / (X_train1_max - X_train1_min) - 1. if os.path.exists(train_data2): df = pd.read_csv(train_data2, delimiter=' ', header=None, encoding='latin-1') else: raise FileNotFoundError('File %s not found!' % (train_data2)) X_train2 = df.values X_train2 = np.reshape(X_train2, (np.shape(X_train2)[0], proto_num, int(np.shape(X_train2)[1] / (proto_num)))) if normalize_timeseries: X_train2_min = np.min(X_train2) X_train2_max = np.max(X_train2) X_train2 = 2. * (X_train2 - X_train2_min) / (X_train2_max - X_train2_min) - 1. if os.path.exists(train_labels): df = pd.read_csv(train_labels, delimiter=' ', header=None, encoding='latin-1') else: raise FileNotFoundError('File %s not found!' % (train_labels)) y_train = df[[1]].values no_classes = nb_classes(index) #len(np.unique(y_train)) if os.path.exists(test_data1): df = pd.read_csv(test_data1, delimiter=' ', header=None, encoding='latin-1') else: raise FileNotFoundError('File %s not found!' % (test_data1)) X_test1 = df.values X_test1 = np.reshape( X_test1, (np.shape(X_test1)[0], dim, int(np.shape(X_test1)[1] / (dim)))) if normalize_timeseries: X_test1 = 2. * (X_test1 - X_train1_min) / (X_train1_max - X_train1_min) - 1. if os.path.exists(test_data2): df = pd.read_csv(test_data2, delimiter=' ', header=None, encoding='latin-1') else: raise FileNotFoundError('File %s not found!' % (test_data2)) X_test2 = df.values X_test2 = np.reshape(X_test2, (np.shape(X_test2)[0], proto_num, int(np.shape(X_test2)[1] / (proto_num)))) if normalize_timeseries: X_test2 = 2. * (X_test2 - X_train2_min) / (X_train2_max - X_train2_min) - 1. if os.path.exists(test_labels): df = pd.read_csv(test_labels, delimiter=' ', header=None, encoding='latin-1') else: raise FileNotFoundError('File %s not found!' % (test_labels)) y_test = df[[1]].values if verbose: print("Finished loading test dataset..") print() print("Number of train samples : ", X_train1.shape[0], "Number of test samples : ", X_test1.shape[0]) print("Number of classes : ", no_classes) print("Sequence length : ", X_train1.shape[-1]) return X_train1, X_train2, y_train, X_test1, X_test2, y_test, is_timeseries