Esempio n. 1
0
def train_and_validate():
    print('Seizure Detection Learning')
    print('---------------------------------------------------------------')

    do_train = True

    ds_seizure = SeizureDataset(FLAGS)

    if do_train:
        X_train, y_train = ds_seizure.load_train_data(FLAGS.train_set)
        print('Data sample size:', X_train[0].shape)
        print('------------------------------------')
        print('Batch size: ', FLAGS.batch_size)
        print('------------------------------------')

        # Note : Make sure dataset is divisible by batch_size
        try:
            assert(len(X_train) % FLAGS.batch_size == 0)

        except:
            print("Make sure dataset size is divisble by batch_size!")
            sys.exit(1)

        with tf.Graph().as_default():
            # create and train the network
            rnn_net = SeizureClassifier(FLAGS)
            rnn_net.setup_loss()
            rnn_net.do_train(ds_seizure, X_train, y_train, FLAGS)
            #print('Final evaluation on the training data')
            # print('---------------------------------------------------------------')
            #rnn_net.do_eval(X_train, y_train)

        # At this point we are done with the training data
        del X_train
        del y_train

    print('Testing')
    print('---------------------------------------------------------------')
    X_test, ids = ds_seizure.load_test_data(FLAGS.test_set)
    with tf.Graph().as_default():
        # create and train the network
        rnn_net = SeizureClassifier(FLAGS)
        rnn_net.setup_loss()
        predictions = rnn_net.predict(X_test, FLAGS)
        # Save the results
        frame = pd.DataFrame(
            {'File': ids,
             'Class': predictions
             })
        cols = frame.columns.tolist()
        cols = cols[-1:] + cols[:-1]
        frame = frame[cols]
        frame['Class'] = frame['Class'].astype(float)
        frame.to_csv(FLAGS.test_set + '_res.csv', index=False)
        print('Saved results in: ', FLAGS.test_set)
def main(_):
    username = os.getlogin()
    user_dir = '/home/' + username
    instances_count = 1
    total_patient = 3

    for patient in xrange(total_patient):
        patient_id = 1 + patient
        FLAGS.patient_id = patient_id
        FLAGS.train_set = 'image_train_{0}_1000/single_side_fft/'.format(
            patient_id)
        FLAGS.test_set = 'image_test_{0}_1000/single_side_fft/'.format(
            patient_id)
        predictions = 0
        for instances in xrange(instances_count):
            ds_seizure = SeizureDataset(FLAGS)
            X_train, y_train = ds_seizure.load_train_data(FLAGS.train_set)
            X_test, y_ids = ds_seizure.load_test_data(FLAGS.test_set)
            ds = NetworkDataset(X_train, y_train, X_test, y_ids)
            FLAGS.model_dir = user_dir + '/seizure_models/single_side_fft/patient_{0}/model_{1}.cpkd'.format(
                FLAGS.patient_id, instances)
            predictions += np.array(train_and_validate(ds, instances))

        store_total_avg_pred(ds, instances_count, predictions)
def train_and_validate():
    height = 300
    width = 300
    depth = 16
    output_dim = 1
    patch_size_1 = 5
    patch_size_2 = 5
    feature_size_1 = 32
    feature_size_2 = 64
    fc_size = 1024

    print('Seizure Detection Learning')
    print('---------------------------------------------------------------')

    do_train = True

    ds_seizure = SeizureDataset(FLAGS)

    if do_train:

        X_train, y_train = ds_seizure.load_train_data(FLAGS.train_set)
        train_set_size = int(round(FLAGS.train_ds_ratio * len(X_train)))
        X_train_set = X_train[:train_set_size]
        y_train_set = y_train[:train_set_size]

        X_val_set = X_train[train_set_size:]
        y_val_set = y_train[train_set_size:]
        print('Data sample size = ', X_train[0].shape)
        print('Trainig samples count = ', len(y_train_set))
        print('Validation samples count = ', len(y_val_set))
        print('------------------------------------')
        print('Batch size: ', FLAGS.batch_size)
        print('------------------------------------')

        # Note : Make sure dataset is divisible by batch_size
        # try:
        #     assert(len(X_train) % FLAGS.batch_size == 0)
        # except:
        #     print("Make sure dataset size is divisble by batch_size!")
        #     sys.exit(1)
        y_1s_train_cnt = np.sum(y_train_set)
        y_0s_train_cnt = train_set_size - y_1s_train_cnt
        FLAGS.pos_weight = (1. * y_0s_train_cnt) / y_1s_train_cnt
        print('Positive weight = ', FLAGS.pos_weight)
        with tf.Graph().as_default():
            # create and train the network
            cnn_net = SeizureClassifier(FLAGS, height, width, depth,
                                        output_dim, patch_size_1, patch_size_2,
                                        feature_size_1, feature_size_2,
                                        fc_size)
            cnn_net.setup_loss_and_trainOp(FLAGS)

            cnn_net.do_train(ds_seizure, X_train_set, y_train_set, X_val_set,
                             y_val_set, FLAGS)

    # Start a new graph
    with tf.Graph().as_default():
        cnn_net = SeizureClassifier(FLAGS, height, width, depth, output_dim,
                                    patch_size_1, patch_size_2, feature_size_1,
                                    feature_size_2, fc_size)
        cnn_net.setup_loss_and_trainOp(FLAGS)
        cnn_net.predict(ds_seizure, FLAGS)