Esempio n. 1
0
File: cnn.py Progetto: Dimassio/cnn
def fit(n_windows, win_width, rand_state, data_set, data_labels, filename='weights.pkl'):
    # Permuting data
    rng = np.random.RandomState(8000)
    indices = rng.permutation(len(data_set))
    data_set = np.array(data_set)
    data_labels = np.array(data_labels)
    data_set, data_labels = data_set[indices], data_labels[indices]
    print str(len(data_set)) + " all samples"

    train_len = int(len(data_set) * 9.0 / 10.0)
    valid_len = len(data_set) - train_len
    print "Train: " + str(train_len)
    print "Validate: " + str(valid_len)

    # Splitting fs
    train_dir = fs.File("training.hdf5", "a")
    train_data = train_dir.create_dataset('train_data', shape=((train_len + 1) * n_windows, 41, 41), dtype='i')
    train_labels = train_dir.create_dataset('train_labels', shape=((train_len + 1) * n_windows,), dtype='i')

    valid_dir = fs.File("validating.hdf5", "a")
    valid_data = valid_dir.create_dataset('valid_data', shape=((valid_len + 1) * n_windows, 41, 41), dtype='i')
    valid_labels = valid_dir.create_dataset('valid_labels', shape=((valid_len + 1) * n_windows,), dtype='i')
    counter = 0
    next_counter = 0
    for iter, data_sample in enumerate(data_set):
        if iter % 10000 == 0:
            print iter
        windows = WinExt.get_windows(data_sample, n_windows, win_width, rand_state)
        for window in windows:
            # First windows part for training
            # Second part for validation
            if iter < train_len:
                train_data[counter] = window
                train_labels[counter] = data_labels[iter]
                counter += 1
            else:
                valid_data[next_counter] = window
                valid_labels[next_counter] = data_labels[iter]
                next_counter += 1
    # Setting real length
    train_len = counter
    valid_len = next_counter
    print "Size of train is " + str(train_len)
    print "Size of valid is " + str(valid_len)
    print "Extracting has finished its work..."

    batch_size = 500

    if train_len % batch_size != 0:  # if the last batch is not full, just don't use the remainder
        whole = (train_len / batch_size) * batch_size
        train_len = whole
    if valid_len % batch_size != 0:
        whole = (valid_len / batch_size) * batch_size
        valid_len = whole

    n_train_batches = train_len / batch_size
    n_valid_batches = valid_len / batch_size

    print "Building net..."
    nkerns = [5, 10, 20]
    x = T.dtensor3('x')  # dtensor3 for 3d array
    y = T.ivector('y')  # the labels are presented as 1D vector of [int] labels
    rng = np.random.RandomState(8000)

    classifier = CNN(
        rng=rng,
        input=x,
        nkerns=nkerns,
        batch_size=batch_size
    )

    # 4 layer is logistic
    cost = classifier.layer4.negative_log_likelihood(y)
    # create a list of gradients for all model parameters
    grads = T.grad(cost, classifier.params)
    learning_rate = 0.3 / float(n_train_batches)

    updates = [
        (param_i, param_i - learning_rate * grad_i)
        for param_i, grad_i in zip(classifier.params, grads)
        ]

    data_tr = theano.shared(
        np.asarray(np.zeros((batch_size, 41, 41), dtype=np.int), dtype=theano.config.floatX),
        borrow=True)
    labels_tr = theano.shared(
        np.asarray(np.zeros(batch_size, dtype=np.int), dtype='int32'),
        borrow=True)
    data_val = theano.shared(
        np.asarray(np.zeros((batch_size, 41, 41), dtype=np.int), dtype=theano.config.floatX),
        borrow=True)
    labels_val = theano.shared(
        np.asarray(np.zeros(batch_size, dtype=np.int), dtype='int32'),
        borrow=True)
    # When we will call them, updates will be recalculated, and errors(cost) will be returned
    # for valid set we don't have to do random shuffle
    validate_model = theano.function(
        inputs=[],
        outputs=classifier.errors(y),
        givens={
            x: data_val,
            y: labels_val
        }
    )

    # indices - for random shuffle
    train_model = theano.function(
        inputs=[],
        outputs=cost,
        updates=updates,
        givens={
            x: data_tr,
            y: labels_tr
        }
    )

    # ONLY if interrupted
    file = open(filename, 'rb')
    classifier.__setstate__(cPickle.load(file))
    file.close()

    print "Training..."
    # GDM with batches
    epoch = 14
    n_epochs = 30
    min_error = 100.0
    errors = []
    indices = rng.permutation(train_len)
    while epoch < n_epochs:
        print "================= " + str(epoch + 1) + " epoch =============== "
        for minibatch_index in range(n_train_batches):
            if minibatch_index % 50 == 0:
                print str(minibatch_index) + " batch"
            data_tr.set_value(np.array(
                [train_data[indices[minibatch_index * batch_size + i]] for i in range(batch_size)]), borrow=True)
            labels_tr.set_value(np.array(
                [train_labels[indices[minibatch_index * batch_size + i]] for i in range(batch_size)]), borrow=True)
            train_model()
        # compute zero-one loss on validation set
        validation_losses = []
        for i in range(n_valid_batches):
            data_val.set_value(np.array(
                valid_data[i * batch_size: (i + 1) * batch_size]), borrow=True)
            labels_val.set_value(np.array(
                valid_labels[i * batch_size: (i + 1) * batch_size]), borrow=True)
            validation_losses.append(validate_model())
        this_validation_loss = np.mean(validation_losses) * 100
        errors.append(this_validation_loss)
        print str(this_validation_loss) + "% error"
        if this_validation_loss < min_error:
            min_error = this_validation_loss
            save_parameters(classifier, filename)
        epoch += 1
        print "Shuffling..."
        indices = rng.permutation(train_len)

    show_errors(errors, 'You heading goes here...')

    # Cleaning data
    train_dir.clear()
    valid_dir.clear()
    train_dir.close()
    valid_dir.close()
Esempio n. 2
0
def fit(n_windows, win_width, rand_state, data_set, data_labels, filename="LR_weights.pkl"):
    # Permuting data
    rng = np.random.RandomState(8000)
    indices = rng.permutation(len(data_set))
    data_set = np.array(data_set)
    data_labels = np.array(data_labels)
    data_set, data_labels = data_set[indices], data_labels[indices]
    print str(len(data_set)) + " all samples"

    train_len = int(len(data_set) * 9.0 / 10.0)
    valid_len = len(data_set) - train_len
    print "Train: " + str(train_len)
    print "Validate: " + str(valid_len)

    # Splitting fs
    train_dir = fs.File("LR_training.hdf5", "a")
    train_data = train_dir.create_dataset("LR_train_data", shape=((train_len + 1) * n_windows, 41, 41), dtype="i")
    train_labels = train_dir.create_dataset("LR_train_labels", shape=((train_len + 1) * n_windows,), dtype="i")

    valid_dir = fs.File("LR_validating.hdf5", "a")
    valid_data = valid_dir.create_dataset("LR_valid_data", shape=((valid_len + 1) * n_windows, 41, 41), dtype="i")
    valid_labels = valid_dir.create_dataset("LR_valid_labels", shape=((valid_len + 1) * n_windows,), dtype="i")
    counter = 0
    next_counter = 0
    for iter, data_sample in enumerate(data_set):
        if iter % 10000 == 0:
            print iter
        windows = WinExt.get_windows(data_sample, n_windows, win_width, rand_state)
        for window in windows:
            # First windows part for training
            # Second part for validation
            if iter < train_len:
                train_data[counter] = window
                train_labels[counter] = data_labels[iter]
                counter += 1
            else:
                valid_data[next_counter] = window
                valid_labels[next_counter] = data_labels[iter]
                next_counter += 1
    # Setting real length
    train_len = counter
    valid_len = next_counter
    print "Size of train is " + str(train_len)
    print "Size of valid is " + str(valid_len)
    print "Extracting has finished its work..."

    batch_size = 500

    if train_len % batch_size != 0:  # if the last batch is not full, just don't use the remainder
        whole = (train_len / batch_size) * batch_size
        train_len = whole
    if valid_len % batch_size != 0:
        whole = (valid_len / batch_size) * batch_size
        valid_len = whole

    n_train_batches = train_len / batch_size
    n_valid_batches = valid_len / batch_size

    data_tr = theano.shared(
        np.asarray(np.zeros((batch_size, 41, 41), dtype=np.int), dtype=theano.config.floatX), borrow=True
    )
    labels_tr = theano.shared(np.asarray(np.zeros(batch_size, dtype=np.int), dtype="int32"), borrow=True)
    data_val = theano.shared(
        np.asarray(np.zeros((batch_size, 41, 41), dtype=np.int), dtype=theano.config.floatX), borrow=True
    )
    labels_val = theano.shared(np.asarray(np.zeros(batch_size, dtype=np.int), dtype="int32"), borrow=True)

    print "Building logistic regression classifier..."
    x = T.dtensor3("x")  # dtensor3 for 3d array
    y = T.ivector("y")  # the labels are presented as 1D vector of [int] labels
    rng = np.random.RandomState(8000)

    classifier = LogisticRegression(input=x.flatten(2), n_in=41 * 41, n_out=2)

    cost = classifier.negative_log_likelihood(y)
    learning_rate = 0.03  # 0.3 / float(n_train_batches)

    g_W = T.grad(cost=cost, wrt=classifier.W)
    g_b = T.grad(cost=cost, wrt=classifier.b)

    # start-snippet-3
    # specify how to update the parameters of the model as a list of
    # (variable, update expression) pairs.
    updates = [(classifier.W, classifier.W - learning_rate * g_W), (classifier.b, classifier.b - learning_rate * g_b)]

    validate_model = theano.function(inputs=[], outputs=classifier.errors(y), givens={x: data_val, y: labels_val})

    # indices - for random shuffle
    train_model = theano.function(
        inputs=[], outputs=classifier.errors(y), updates=updates, givens={x: data_tr, y: labels_tr}
    )

    print "Training..."
    # GDM with batches
    epoch = 0
    n_epochs = 30
    min_error = 100.0
    errors = []
    indices = rng.permutation(train_len)
    while epoch < n_epochs:
        print "================= " + str(epoch + 1) + " epoch =============== "
        for minibatch_index in range(n_train_batches):
            if minibatch_index % 50 == 0:
                print str(minibatch_index) + " batch"
            data_tr.set_value(
                np.array([train_data[indices[minibatch_index * batch_size + i]] for i in range(batch_size)]),
                borrow=True,
            )
            labels_tr.set_value(
                np.array([train_labels[indices[minibatch_index * batch_size + i]] for i in range(batch_size)]),
                borrow=True,
            )
            train_model()
        # compute zero-one loss on validation set
        validation_losses = []
        for i in range(n_valid_batches):
            data_val.set_value(np.array(valid_data[i * batch_size : (i + 1) * batch_size]), borrow=True)
            labels_val.set_value(np.array(valid_labels[i * batch_size : (i + 1) * batch_size]), borrow=True)
            validation_losses.append(validate_model())
        this_validation_loss = np.mean(validation_losses) * 100
        errors.append(this_validation_loss)
        if this_validation_loss < min_error:
            print str(this_validation_loss) + "% error"
            min_error = this_validation_loss
            save_parameters(classifier, filename)
        epoch += 1
        print "Shuffling..."
        indices = rng.permutation(train_len)

    show_errors(errors, "LogReg: 4 windows, h=41")

    # Cleaning data
    train_dir.clear()
    valid_dir.clear()
    train_dir.close()
    valid_dir.close()
Esempio n. 3
0
File: cnn.py Progetto: Dimassio/cnn
def predict(n_windows, win_width, rand_state, test_dataset, labels, filename='weights.pkl'):
    false_positive = 0.0
    true_positive = 0.0
    false_negative = 0.0
    true_negative = 0.0

    print "Test size is " + str(len(test_dataset))
    rng = np.random.RandomState(8000)
    x = T.matrix('x')
    y = T.ivector('y')  # the labels are presented as 1D vector of [int] labels
    index = T.lscalar()

    classifier = CNN(
        rng=rng,
        input=x,
        nkerns=[5, 10, 20],
        batch_size=1  # Only for one image will predict
    )

    file = open(filename, 'rb')
    classifier.__setstate__(cPickle.load(file))
    file.close()

    result = []
    for iter, image in enumerate(test_dataset):
        if iter % 1000 == 0:
            print iter
        windows = WinExt.get_windows(image, n_windows, win_width, rand_state)
        test_data = theano.shared(np.asarray(windows, dtype=theano.config.floatX), borrow=True)
        test_labels = theano.shared(np.asarray(np.zeros(len(windows)), dtype='int32'), borrow=True)

        predict_model = theano.function(
            inputs=[index],
            outputs=classifier.layer4.pred_probs(),
            givens={
                x: test_data[index],
                y: test_labels
            },
            on_unused_input='ignore'  # because we dont need test lables,for now
        )

        predicts = [predict_model(i) for i in range(len(windows))]
        windows_predictions = np.array(predicts).reshape((len(windows), 2))
        avg_0 = 0.0
        avg_1 = 0.0
        for win_prediction in windows_predictions:
            avg_0 += win_prediction[0]
            avg_1 += win_prediction[1]
        avg_0 /= len(windows_predictions)
        avg_1 /= len(windows_predictions)
        result.append(np.argmax([avg_0, avg_1]))  # Predicting on average answers
        if result[iter] != labels[iter]:
            if labels[iter] == 0:
                false_positive += 1
            else:
                false_negative += 1
            image_save(n_windows, image, iter)
            print str(iter) + " image proba:"
            print windows_predictions
        else:
            if labels[iter] == 0:
                true_negative += 1
            else:
                true_positive += 1
    print "for negative class"
    print "recall: "
    print float(true_negative / (true_negative + false_positive))
    print "precision: "
    print float(true_negative / (true_negative + false_negative))
    print "for positive class"
    print "recall: "
    print float(true_positive / (true_positive + false_negative))
    print "precision: "
    print float(true_positive / (true_positive + false_positive))
    print "Accuracy " + str(T.mean(T.neq(result, labels)).eval())