def main(): args = parse_args() processeddata_dir = \ "../Hanover_Dataset/HannoverDataset/processed_data/juncs_trips_%02.0f.npy"%args.min_trips rawdata_file = "../Hanover_Dataset/HannoverDataset" # Process and save the data if args.process_data: junctTrajs_dirs = sorted( glob.glob(os.path.join(rawdata_file, "junctTrajs/*.csv"))) junctions_dir = os.path.join(rawdata_file, "junctions.csv") juncs_trips = preprocess(junctTrajs_dirs, junctions_dir, min_trips=args.min_trips, upper_threshold=args.upper_threshold, lower_threshold=args.lower_threshold, window_size=args.window_size) np.save(processeddata_dir, juncs_trips) else: juncs_trips = np.load(processeddata_dir) # Load the sequence data using the sliding window scheme data_loader = Load_data(juncs_trips, window_size=args.window_size, stride=args.stride) # Load the sequence data and get the data index data = [sequence for sequence in data_loader.sliding_window()] data = np.reshape(data, (-1, 19)) # data index + features = 10 + 9 = 19 # Note, due to the data imbalance, # merge tram_rails (-1) and yield_sigh (1) to priority_sign (2) if args.num_classes == 3: data[data[:, 2] == -1, 2] = 2 data[data[:, 2] == 1, 2] = 2 # # Filter out -1 and 1 # data = data[data[:, 2]!=-1, :] # data = data[data[:, 2]!=1, :] # new target class: # uncontrolled:0, # traffic_light:1, # tram_rails/yield_sigh/priority_sign data[data[:, 2] == 4, 2] = 1 # Filter out 3:"stop S." and 5:"roundabout" data = data[data[:, 2] != 3, :] data = data[data[:, 2] != 5, :] # Normalize the features data[:, 10:] = normalization(data[:, 10:]) # Get the class label label = data[:, 2].astype(int) assert args.num_classes == len( np.unique(label)), "The number of classes is not correct" _label = np.eye(args.num_classes)[label].reshape(-1, args.window_size, args.num_classes) # Question: how to do the data partitioning data = np.reshape(data, (-1, args.window_size, 19)) print(data.shape) train_val_split = data_partition(data, args) train_data_index = data[train_val_split, -1, :10] # the last step of the sliding window # 10/0: junc_utm_to_center, # 11/1: utm_east, # 12/2: utm_north, # 13/3: utm_east_speed, # 14/4: utm_east_speed, # 15/5: speed_1, # 16/6: speed_2, (old table speed) # 17/7: delta_time # 18/8: angle # ============================================================================= # train_x = data[train_val_split, :, 10:19] # train_x = np.concatenate((train_x[:, :, 0:6], train_x[:, :, 7:8]), axis=2) # skip the old speed # train_y = _label[train_val_split, -1, :] # the last step of the sliding window # # val_data_index = data[~train_val_split, -1, :10] # the last step of the sliding window # val_x = data[~train_val_split, :, 10:19] # val_x = np.concatenate((val_x[:, :, 0:6], val_x[:, :, 7:8]), axis=2) # val_y = _label[~train_val_split, -1, :] # the last step of the sliding window # ============================================================================= # ToDo, feature/ablation # remove delta_t train_x = data[train_val_split, :, 10:19] train_x = np.concatenate((train_x[:, :, 0:5], train_x[:, :, 7:8]), axis=2) # train_x = train_x[:, :, 2:5] train_y = _label[train_val_split, -1, :] val_data_index = data[~train_val_split, -1, :10] val_x = data[~train_val_split, :, 10:19] val_x = np.concatenate((val_x[:, :, 0:5], val_x[:, :, 7:8]), axis=2) # val_x = val_x[:, :, 2:5] val_y = _label[~train_val_split, -1, :] print( np.unique(np.argmax(val_y.reshape(-1, args.num_classes), axis=1), return_counts=True)) print("train_data_index", train_data_index.shape) print("train_x", train_x.shape) print("train_y", train_y.shape) print("val_data_index", val_data_index.shape) print("val_x", val_x.shape) print("val_y", val_y.shape) ########################################################################## ## START THE CLASSIFICATION TASK # Define the callback and early stop if not os.path.exists("../models"): os.mkdir("../models") timestr = time.strftime("%Y%m%d-%H%M%S") filepath = "../models/cvae_%0.f_%s.hdf5" % (args.epochs, timestr) ## Eraly stop earlystop = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=args.patience) checkpoint = ModelCheckpoint(filepath, monitor='val_loss', verbose=0, save_best_only=True, mode='min') callbacks_list = [earlystop, checkpoint] # # Instantiate the model cvae = CVAE(args) # Contruct the cave model train = cvae.training() train.summary() # # Start training phase if args.train_mode: # train.load_weights("../models/cvae_500_20201008-213111_03_01_90.hdf5") print("Start training the model...") train.fit(x=[train_x, train_y], y=train_y, shuffle=True, epochs=args.epochs, batch_size=args.batch_size, verbose=1, callbacks=callbacks_list, validation_data=([val_x, val_y], val_y)) train.load_weights(filepath) else: print('Run pretrained model') # train.load_weights("../models/cvae_200_20200709-211305.hdf5") train.load_weights("../models/cvae_500_20201008-213111_03_01_90.hdf5") # # Start inference phase x_encoder = cvae.X_encoder() decoder = cvae.Decoder() x_encoder.summary() decoder.summary() x_latent = x_encoder.predict(val_x, batch_size=args.batch_size) y_primes = [] for i, x_ in enumerate(x_latent): # sampling z from a normal distribution x_ = np.reshape(x_, [1, -1]) z_sample = np.random.rand(1, args.z_dim) y_p = decoder.predict(np.column_stack([z_sample, x_])) y_primes.append(y_p) y_primes = np.reshape(y_primes, (-1, args.num_classes)) ## Evaluation print("Prediction for each sliding window...") target_names = ['uc', 'tl', 'ps'] eva = Evaluation(val_y.reshape(-1, args.num_classes), y_primes, target_names) confusion_matrix = eva.cf_matrix() classification_report = eva.report() # Sum up the prediction for each trip and each junction print("Prediction for each arm...") junc_classifier = Junction_class(val_data_index, val_y, y_primes) arm_gt, arm_pd = junc_classifier.avg_classfier() arm_eva = Evaluation(arm_gt, arm_pd[:, 0], target_names, arg=False) arm_confusion_matrix = arm_eva.cf_matrix() arm_classification_report = arm_eva.report()