def test_train_test_split(self):
     g = nx.Graph()
     g.add_edge(0, 1, weight=1)
     g.add_edge(1, 2, weight=1)
     g.add_edge(2, 3, weight=1)
     g.add_edge(3, 0, weight=1)
     g.add_edge(3, 1, weight=1)
     np.random.seed(0)
     new_g, test_edge_list = train_test_split(g)
     self.assertEqual(len(new_g.edges), 4)
     self.assertEqual(test_edge_list, [(1, 2)])
def evaluate(num_times, method_dic, G):
    '''
        num_times: int
    '''
    report = {}
    for alg_name in method_dic:
        report[alg_name] = {
            "tpr": 0,
            "tnr": 0,
            "acc": 0
        }

    for i in range(num_times):
        new_g, test_edge_list = train_test_split(G)     
        for alg_name, method in method_dic.items():
            logging.info('eval ' + alg_name + ' round=%d/%d'%(i, num_times))    
            res = evaluate_single(method, test_edge_list, new_g)      
            for k in report[alg_name]:
                report[alg_name][k] += res[k]
    for alg_name in method_dic:
        for k in report[alg_name]:
            report[alg_name][k] /= num_times
        
    return report
Beispiel #3
0
def train(config_file):
    """
    Main function that train and persists model based on training set/

    Args:
        config_file [str]: path to config file

    Returns:
        None
    """
    ################
    # config logger
    ################
    logger = set_logger("../log/train.log")

    ###############################
    # Load config from config file
    ###############################
    logger.info(f"Load config from {config_file}")
    config = parse_config(config_file)

    keypoints_csv = Path(config['common']['labels_csv_path'])
    val_split = config['common']['val_split']
    train_img_scr_path = config['common']['img_source_path']
    test_img_scr_path = config['common']['img_source_path']
    image_width = config['common']['in_image_width']
    image_height = config['common']['in_image_height']

    epochs = config['train']['epochs']
    train_batch_size = config['train']['batch_size']
    weight_path = config['common']['weight_path']
    no_of_aug = config['train']['no_of_aug']
    test_batch_size = config['test']['batch_size']

    ############
    # Load Data
    ############
    logger.info(f"----------------Load the data----------------")
    selected_img, keypoint_df = load_data(keypoints_csv)
    logger.info(f"Number of selected images are {selected_img.shape}")
    logger.info(f"Few of the selected images are {selected_img[0:5]}")

    ####################################
    # Get train and test data generators
    ####################################

    X_train, y_train, X_test, y_test = train_test_split(
        selected_img, keypoint_df, val_split)

    train_gen = Car(x_set=X_train,
                    y_set=y_train,
                    mode='Train',
                    data_path=train_img_scr_path,
                    image_width=image_width,
                    image_height=image_height,
                    batch_size=train_batch_size,
                    augmentations='Self',
                    no_of_aug=no_of_aug)
    test_gen = Car(
        x_set=X_test,
        y_set=y_test,
        mode='Test',
        data_path=test_img_scr_path,
        image_width=image_width,
        image_height=image_height,
        batch_size=test_batch_size,
    )

    #####################
    # Set and train model
    #####################

    logger.info(
        f"-------------------------Initiate Model---------------------")
    model = KeyPointModel().getModel()

    logger.info(
        f"--------------------Model Summary---------------------------")
    logger.info(f"{model.summary}")

    # compile the model
    model.compile(loss='mean_squared_error',
                  optimizer='adam',
                  metrics=['mean_absolute_error'])

    # modelCheckPoint = ModelCheckpoint('car-{val_loss:.2f}.h5', monitor='val_loss', verbose=1, save_best_only=True, save_weights_only=True)
    earlyS = EarlyStopping(monitor='val_loss',
                           min_delta=1,
                           patience=3,
                           restore_best_weights=True)
    reducelr = ReduceLROnPlateau(monitor='val_loss',
                                 factor=0.1,
                                 patience=2,
                                 min_lr=1e-7)

    history = model.fit(x=train_gen,
                        validation_data=test_gen,
                        callbacks=[earlyS, reducelr],
                        epochs=epochs)
    logger.info(history)
    logger.info("------------Saving Weights--------------")
    model.save_weights(weight_path)
Beispiel #4
0
    parser = argparse.ArgumentParser()
    parser.add_argument("--height",
                        help="maximum height of decision tree for Q1")
    args = parser.parse_args()
    ht = -1
    if args.height:
        ht = int(args.height)

    print("\n ============= READING DATA ============ \n")

    DATA_FILE = 'PercentageIncreaseCOVIDWorldwide.csv'
    df = read_data(DATA_FILE)
    print("Time elapsed  =  {} ms".format(time.time() - start))
    print("\n ============= DATA READ ============ \n\n")

    train, test = train_test_split(df)
    print("============= TRAIN TEST SPLIT COMPLETE ============\n")
    print("train data size: {}, test data size = {} \n\n".format(
        len(train), len(test)))

    print("============== SOLVING Q1 ==============\n")
    # print("select a height ({greater then 0} or {-1}): ")
    # ht = int(input())
    print("height selected: {}".format(ht if ht != -1 else "Full Tree"))
    print("\n========= TRAINING STARTED =========\n")

    X_train = train
    start = time.time()
    tree, train, valid, mse_avg, acc_avg = randomize_select_best_tree(
        train, ht, test)
    print("Time elapsed  =  {} ms".format(time.time() - start))
def evaluate_single_wrapper(alg, G):
    new_g, test_edge_list = train_test_split(G)
    return evaluate_single(alg, test_edge_list, new_g)
Beispiel #6
0
X, labels = load_data(image_dir, sample_percent=sample_percent)
length, width = X[0].shape
X = X.reshape(-1, length, width, 1)
X = X.astype('float32')
X /= 255
unique_y = unique(labels)
num_unique_labels = len(unique_y)
mapping = {
    label: one_hot(i, num_unique_labels)
    for i, label in enumerate(unique_y)
}
y = np.array([mapping[label] for label in labels])
y = y.astype('float32')
X_train, X_test, y_train, y_test = train_test_split(X,
                                                    y,
                                                    test_size=0.1,
                                                    shuffle=False)

model = Sequential()
model.add(Flatten(input_shape=(length, width, 1)))
model.add(Dense(256, activation='relu'))
model.add(Dense(256, activation='relu'))
model.add(Dense(256, activation='relu'))
model.add(Dense(num_unique_labels, activation='softmax'))

model.compile(loss='categorical_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])

model.fit(X_train,
          y_train,