Exemplo n.º 1
0
def main():
    # loads, encodes and normalizes the dataset
    X_train, y_train, X_test, y_test, N_class = load_data()
    encoder = OneHotEncoder(sparse=True)
    y_train = encoder.fit_transform(y_train.reshape(-1, 1)).toarray()
    y_test = encoder.transform(y_test.reshape(-1, 1)).toarray()
    X_train = normalize(X_train)
    X_train, y_train = shuffle(X_train, y_train)
    X_test = normalize(X_test)
    X_test, X_validation, y_test, y_validation = train_test_split(
        X_test, y_test, test_size=0.50, random_state=0)

    # Convolutional neural network using 2 filters
    nn = CNN(name="test",
             imageWidth=100,
             imageHeight=100,
             hiddenSize=256,
             outputSize=N_class,
             filters=[(5, 5, 3, 20), (3, 3, 20, 50)],
             poolSize=(2, 2),
             learningRate=0.0001,
             decay=0.99,
             momentum=0.90)

    nn.train(X_train,
             y_train,
             X_validation,
             y_validation,
             batchSize=128,
             epochs=2)
    count = 0
    for i in range(len(X_test)):

        j = nn.predictOne(X_test[i])
        p = np.zeros(N_class)
        p[j] = 1
        if j != np.argmax(y_test[i]):
            count += 1
            plt.imshow(X_test[i])
            plt.show()
            print(encoder.inverse_transform([p]), ':',
                  encoder.inverse_transform([y_test[i]]))
    print((len(X_test) - count) / len(X_test))

    nn.close()
def main(argv=None):
    try:
        p = Pool(5) # process 5 images simultaneously
        images_train, image_labels_train, images_val, image_labels_val = load_data(Constants.WORDS_FILE, p)
        
        # Write the raw image files to images.tfrecords.
        # First, process the two images into tf.Example messages.
        # Then, write to a .tfrecords file.
        record_file = Constants.TRAIN_TFRECORD
        with tf.io.TFRecordWriter(record_file) as writer:
          for i in range(len(images_train)):
            tf_example = image_example(images_train[i], image_labels_train[i])
            writer.write(tf_example.SerializeToString())
        print("Successfully generated "+record_file)
            
        record_file = Constants.VAL_TFRECORDS
        with tf.io.TFRecordWriter(record_file) as writer:
          for i in range(len(images_val)):
            tf_example = image_example(images_val[i], image_labels_val[i])
            writer.write(tf_example.SerializeToString())
        print("Successfully generated "+record_file)

    except:
        raise
def main():
    # loads, encodes and normalizes the dataset
    X_train, y_train, X_test, y_test, N_class = load_data()
    encoder = OneHotEncoder(sparse=True)
    y_train = encoder.fit_transform(y_train.reshape(-1, 1)).toarray()
    y_test = encoder.transform(y_test.reshape(-1, 1)).toarray()
    X_train = normalize(X_train)
    X_train, y_train = shuffle(X_train, y_train)
    X_test = normalize(X_test)
    X_test, X_validation, y_test, y_validation = train_test_split(X_test, y_test, test_size=0.50, random_state=0)

    """ IN THIS SECTION WE COMPARE DIFFENT REGULARIZATIONS """

    nn1 = CNN(
        name="No_regularization",
        imageWidth=100,
        imageHeight=100,
        hiddenSize=256,
        outputSize=N_class,
        filters=[(3, 3, 3, 20), (3, 3, 20, 50)],
        poolSize=(2, 2),
        initialization="xavier_glorot",
        regularization="None"
    )
    cost1, accuracy1 = nn1.train(X_train, y_train, X_validation, y_validation, batchSize=128, epochs=20)


    nn2 = CNN(
        name="dropout_regularization",
        imageWidth=100,
        imageHeight=100,
        hiddenSize=256,
        outputSize=N_class,
        filters=[(3, 3, 3, 20), (3, 3, 20, 50)],
        poolSize=(2, 2),
        initialization="xavier_glorot",
        regularization="dropout"
    )

    cost2, accuracy2 = nn2.train(X_train, y_train, X_validation, y_validation, batchSize=128, epochs=20)


    nn3 = CNN(
        name="l1_regularization",
        imageWidth=100,
        imageHeight=100,
        hiddenSize=256,
        outputSize=N_class,
        filters=[(3, 3, 3, 20), (3, 3, 20, 50)],
        poolSize=(2, 2),
        initialization="xavier_glorot",
        regularization="l1"
    )

    cost3, accuracy3 = nn3.train(X_train, y_train, X_validation, y_validation, batchSize=128, epochs=20)
    
    
    nn4 = CNN(
        name="l2_regularization",
        imageWidth=100,
        imageHeight=100,
        hiddenSize=256,
        outputSize=N_class,
        filters=[(3, 3, 3, 20), (3, 3, 20, 50)],
        poolSize=(2, 2),
        initialization="xavier_glorot",
        regularization="l2"
    )

    cost4, accuracy4 = nn4.train(X_train, y_train, X_validation, y_validation, batchSize=128, epochs=20)
    
    plt.xlabel("Epochs")
    plt.ylabel("Cost")
    plt.plot(cost1, label='None')
    plt.plot(cost2, label='dropout')
    plt.plot(cost3, label='l1')
    plt.plot(cost4, label='l2')
    plt.legend(loc='upper left')
    plt.show()
    
    plt.xlabel("Epochs")
    plt.ylabel("Validation Accuracy")
    plt.plot(accuracy1, label='None')
    plt.plot(accuracy2, label='dropout')
    plt.plot(accuracy3, label='l1')
    plt.plot(accuracy4, label='l2')
    plt.legend(loc='upper left')
    plt.show()
    
    
    count1=np.zeros((4,4),dtype=int)
    count2=np.zeros((4,4),dtype=int)
    count3=np.zeros((4,4),dtype=int)
    count4=np.zeros((4,4),dtype=int)
    for i in range(len(X_test)):
        k = np.argmax(y_test[i])
        j1 = nn1.predictOne(X_test[i])
        j2 = nn2.predictOne(X_test[i])
        j3 = nn3.predictOne(X_test[i])
        j4 = nn4.predictOne(X_test[i])
        if j1!=k:
            count1[k][j1] += 1
        if j2!=k:
            count2[k][j2] += 1
        if j3!=k:
            count3[k][j3] += 1
        if j4!=k:
            count4[k][j4] += 1
    
    for i in range(N_class):
        p = np.zeros(N_class)
        p[i] = 1
        print(i,':',encoder.inverse_transform([p])) 
    print("Test phase")
    print("------")
    print("None:")
    print("mistakes")
    print(count1)
    print("Test accuracy:",(len(X_test)-np.sum(count1))/len(X_test))
    print("------")
    print("dropout:")
    print("mistakes")
    print(count2)
    print("Test accuracy:",(len(X_test)-np.sum(count2))/len(X_test))
    print("------")
    print("l1:")
    print("mistakes")
    print(count3)
    print("Test accuracy:",(len(X_test)-np.sum(count3))/len(X_test))
    print("------")
    print("l2:")
    print("mistakes")
    print(count4)
    print("Test accuracy:",(len(X_test)-np.sum(count4))/len(X_test))
Exemplo n.º 4
0
def main():
    # loads, encodes and normalizes the dataset
    X_train, y_train, X_test, y_test, N_class = load_data()
    encoder = OneHotEncoder(sparse=True)
    y_train = encoder.fit_transform(y_train.reshape(-1, 1)).toarray()
    y_test = encoder.transform(y_test.reshape(-1, 1)).toarray()
    X_train = normalize(X_train)
    X_train, y_train = shuffle(X_train, y_train)
    X_test = normalize(X_test)
    X_test, X_validation, y_test, y_validation = train_test_split(X_test, y_test, test_size=0.50, random_state=0)


    # Convolutional neural network using 2 filters
    nn = CNN(
            name="test",
            imageWidth=100,
            imageHeight=100,
            hiddenSize=256,
            outputSize=N_class,
            filters=[(12, 12, 3, 20), (12, 12, 20, 50)],
            poolSize=(2,2),
            initialization="xavier_glorot",
            regularization="l2"
        )

    cost, accuracy = nn.train(X_train, y_train, X_validation, y_validation, batchSize=128, epochs=9)
    
    plt.xlabel("Epochs")
    plt.ylabel("Cost")
    plt.plot(cost) 
    plt.show()
    
    plt.xlabel("Epochs")
    plt.ylabel("Validation Accuracy")
    plt.plot(accuracy)
    plt.show()
    
    count=np.zeros((4,4),dtype=int)
    for i in range(len(X_test)):
        k = np.argmax(y_test[i])
        j = nn.predictOne(X_test[i])
        if j!=k:
            count[k][j] += 1
            
            #To turn on output of individual images misclassified simply change to true
            if False:
                p = np.zeros(N_class)
                p[j] = 1
                print("This picture should be classed as ",encoder.inverse_transform([y_test[i]]))
                plt.imshow(X_test[i])
                plt.show()
                print("It has instead been classified as ",encoder.inverse_transform([p]))
    
    for i in range(N_class):
        p = np.zeros(N_class)
        p[i] = 1
        print(i,':',encoder.inverse_transform([p])) 
    print("Test phase")
    print("mistakes")
    print(count)
    print("Test accuracy:",(len(X_test)-np.sum(count))/len(X_test))
    
    nn.close()
Exemplo n.º 5
0
def main():
    # loads, encodes and normalizes the dataset
    X_train, y_train, X_test, y_test, N_class = load_data()
    encoder = OneHotEncoder(sparse=True)
    y_train = encoder.fit_transform(y_train.reshape(-1, 1)).toarray()
    y_test = encoder.transform(y_test.reshape(-1, 1)).toarray()
    X_train = normalize(X_train)
    X_train, y_train = shuffle(X_train, y_train)
    X_test = normalize(X_test)
    X_test, X_validation, y_test, y_validation = train_test_split(
        X_test, y_test, test_size=0.50, random_state=0)
    """ IN THIS SECTION WE COMPARE DIFFENT FILTER SIZES """
    # 3x3 filters
    nn3 = CNN(name="small_filter_size",
              imageWidth=100,
              imageHeight=100,
              hiddenSize=256,
              outputSize=N_class,
              filters=[(3, 3, 3, 20), (3, 3, 20, 50)],
              poolSize=(2, 2),
              initialization="xavier_glorot",
              regularization="dropout")
    cost3, accuracy3 = nn3.train(X_train,
                                 y_train,
                                 X_validation,
                                 y_validation,
                                 batchSize=128,
                                 epochs=20)

    # 6X6 filters
    nn6 = CNN(name="medium_filter_size",
              imageWidth=100,
              imageHeight=100,
              hiddenSize=256,
              outputSize=N_class,
              filters=[(6, 6, 3, 20), (6, 6, 20, 50)],
              poolSize=(2, 2),
              initialization="xavier_glorot",
              regularization="dropout")

    cost6, accuracy6 = nn6.train(X_train,
                                 y_train,
                                 X_validation,
                                 y_validation,
                                 batchSize=128,
                                 epochs=20)

    # 12x12 filters
    nn12 = CNN(name="large_filter_size",
               imageWidth=100,
               imageHeight=100,
               hiddenSize=256,
               outputSize=N_class,
               filters=[(12, 12, 3, 20), (12, 12, 20, 50)],
               poolSize=(2, 2),
               initialization="xavier_glorot",
               regularization="dropout")

    cost12, accuracy12 = nn12.train(X_train,
                                    y_train,
                                    X_validation,
                                    y_validation,
                                    batchSize=128,
                                    epochs=20)

    plt.xlabel("Epochs")
    plt.ylabel("Cost")
    plt.plot(cost3, label='3')
    plt.plot(cost6, label='6')
    plt.plot(cost12, label='12')
    plt.legend(loc='upper left')
    plt.show()

    plt.xlabel("Epochs")
    plt.ylabel("Validation Accuracy")
    plt.plot(accuracy3, label='3')
    plt.plot(accuracy6, label='6')
    plt.plot(accuracy12, label='12')
    plt.legend(loc='upper left')
    plt.show()

    count3 = np.zeros((4, 4), dtype=int)
    count6 = np.zeros((4, 4), dtype=int)
    count12 = np.zeros((4, 4), dtype=int)
    for i in range(len(X_test)):
        k = np.argmax(y_test[i])
        j3 = nn3.predictOne(X_test[i])
        j6 = nn6.predictOne(X_test[i])
        j12 = nn12.predictOne(X_test[i])
        if j3 != k:
            count3[k][j3] += 1
        if j6 != k:
            count6[k][j6] += 1
        if j12 != k:
            count12[k][j12] += 1

    for i in range(N_class):
        p = np.zeros(N_class)
        p[i] = 1
        print(i, ':', encoder.inverse_transform([p]))
    print("Test phase")
    print("------")
    print("3:")
    print("mistakes")
    print(count3)
    print("Test accuracy:", (len(X_test) - np.sum(count3)) / len(X_test))
    print("------")
    print("6:")
    print("mistakes")
    print(count6)
    print("Test accuracy:", (len(X_test) - np.sum(count6)) / len(X_test))
    print("------")
    print("12:")
    print("mistakes")
    print(count12)
    print("Test accuracy:", (len(X_test) - np.sum(count12)) / len(X_test))
Exemplo n.º 6
0
def main():
    X_train, y_train, X_test, y_test, N_class = load_data()
    img_width = X_train.shape[1]
    img_height = X_train.shape[1]
    img_size = img_width * img_height

    # Encode targets
    encoder = OneHotEncoder(sparse=True)

    y_train = encoder.fit_transform(y_train.reshape(-1, 1)).toarray()
    y_test = encoder.transform(y_test.reshape(-1, 1)).toarray()

    # Need to scale! don't leave as 0..255
    # Also need indicator matrix for cost calculation
    X_train = normalize(X_train)
    X_train, y_train = shuffle(X_train, y_train)

    X_test = normalize(X_test)

    # gradient descent params
    max_iter = 6
    print_period = 10
    N = X_train.shape[0]
    batch_sz = 128
    n_batches = N // batch_sz

    # initial weights
    M = 256
    K = N_class

    W1_shape = (
        5, 5, 3, 20
    )  # (filter_width, filter_height, num_color_channels, num_feature_maps)
    W1_init = init_filter(W1_shape)
    b1_init = np.zeros(W1_shape[-1],
                       dtype=np.float32)  # one bias per output feature map

    W2_shape = (
        5, 5, 20, 50
    )  # (filter_width, filter_height, old_num_feature_maps, num_feature_maps)
    W2_init = init_filter(W2_shape)
    b2_init = np.zeros(W2_shape[-1], dtype=np.float32)

    # vanilla ANN weights
    W3_init = np.random.randn(W2_shape[-1] * 25 * 25,
                              M) / np.sqrt(W2_shape[-1] * 25 * 25 + M)
    b3_init = np.zeros(M, dtype=np.float32)
    W4_init = np.random.randn(M, K) / np.sqrt(M + K)
    b4_init = np.zeros(K, dtype=np.float32)

    # define variables and expressions
    # using None as the first shape element takes up too much RAM unfortunately
    X = tf.placeholder(tf.float32,
                       shape=(None, img_width, img_height, 3),
                       name='X')
    T = tf.placeholder(tf.int32, shape=(None, N_class), name='T')
    W1 = tf.Variable(W1_init.astype(np.float32))
    b1 = tf.Variable(b1_init.astype(np.float32))
    W2 = tf.Variable(W2_init.astype(np.float32))
    b2 = tf.Variable(b2_init.astype(np.float32))
    W3 = tf.Variable(W3_init.astype(np.float32))
    b3 = tf.Variable(b3_init.astype(np.float32))
    W4 = tf.Variable(W4_init.astype(np.float32))
    b4 = tf.Variable(b4_init.astype(np.float32))

    Z1 = convpool(X, W1, b1)
    Z2 = convpool(Z1, W2, b2)
    Z2_shape = Z2.get_shape().as_list()
    Z2r = tf.reshape(Z2, [-1, np.prod(Z2_shape[1:])])
    Z3 = tf.nn.relu(tf.matmul(Z2r, W3) + b3)
    Yish = tf.matmul(Z3, W4) + b4

    cost = tf.reduce_sum(
        tf.nn.softmax_cross_entropy_with_logits_v2(logits=Yish, labels=T))

    train_op = tf.train.RMSPropOptimizer(0.0001, decay=0.99,
                                         momentum=0.9).minimize(cost)

    # we'll use this to calculate the error rate
    predict_op = tf.argmax(Yish, 1)

    t0 = datetime.now()
    LL = []
    W1_val = None
    W2_val = None
    init = tf.global_variables_initializer()
    with tf.Session() as session:
        session.run(init)

        error = []
        accuracy = []
        for i in range(max_iter):
            epochErr = 0

            for j in range(n_batches):
                print("\rEpoch " + str(i + 1) + " of " + str(max_iter) +
                      " | Training batch " + str(j + 1) + " of " +
                      str(n_batches),
                      end="")

                Xbatch = X_train[j * batch_sz:(j * batch_sz + batch_sz), ]
                Ybatch = y_train[j * batch_sz:(j * batch_sz + batch_sz), ]

                session.run(train_op, feed_dict={X: Xbatch, T: Ybatch})
                epochErr += session.run(cost, feed_dict={X: Xbatch, T: Ybatch})

            # after one epoch calculates the accuracy
            testSucc = 0
            testAcc = 0
            for k in range(0, len(y_test) // batch_sz):
                Xbatch = X_test[k * batch_sz:(k + 1) * batch_sz, ]
                Ybatch = y_test[k * batch_sz:(k + 1) * batch_sz, ]

                prediction = session.run(predict_op,
                                         feed_dict={
                                             X: Xbatch,
                                             T: Ybatch
                                         })
                testSucc += np.sum(prediction == np.argmax(Ybatch))
                testAcc = testSucc / len(y_test)

            error.append(epochErr)
            accuracy.append(testAcc)
            print(" | Epoch error: {0:.0f}".format(epochErr) +
                  " | Accuracy: {0:.2f}".format(testAcc))

        W1_val = W1.eval()
        W2_val = W2.eval()
    print("Elapsed time:", (datetime.now() - t0))
    # plt.plot(LL)
    # plt.show()
    print(error)