Beispiel #1
0
def do_testing(mname, pname, directory, splitParam):
    learnedLikelihood = pickle.load(open(mname, "rb"))
    priorProb = pickle.load(open(pname, "rb"))

    true_labels = rd.load_label(directory['paths'][3])
    allLabels = len(list(set(true_labels)))
    total_images = len(true_labels)

    print("===========================")
    print("Reading data")

    processedData = df.createDataWithLabel('testing', directory['paths'][2],
                                           true_labels, total_images,
                                           directory['featuretuple'],
                                           directory['dimensions'][1],
                                           directory['dimensions'][0],
                                           splitParam)

    print("===========================")
    print("Initializing testing")

    predicted_value = pc.posteriorProbability(processedData, allLabels,
                                              priorProb, learnedLikelihood)

    diff = 0
    for i in range(len(true_labels)):
        if (int(true_labels[i]) != predicted_value[i]):
            diff = diff + 1

    accuracy = 100 - ((diff * 100) / total_images)
    print("Total error in testing data: ", diff)
    print("Accuracy: ", accuracy)
Beispiel #2
0
def do_training(pathDict, totalEpoch, mname, path, splitParameter):
    numberOfFeatures = int(
        pathDict['dimensions'][1] / pathDict['featuretuple'][0]) * int(
            pathDict['dimensions'][0] / pathDict['featuretuple'][0])

    true_labels = rd.load_label(pathDict['paths'][1])
    allLabels = len(list(set(true_labels)))
    total_images = len(true_labels)

    print("===========================")
    print("Reading data")
    dataWithLabel, labels = df.createDataWithLabel(
        'training', pathDict['paths'][0], true_labels, total_images,
        pathDict['featuretuple'], pathDict['dimensions'][1],
        pathDict['dimensions'][0], splitParameter)
    print("===========================")
    print("Initializing training")

    weights = np.random.rand(numberOfFeatures, allLabels)
    for epoch in range(int(totalEpoch)):
        print("Epoch %d/%d" % (epoch + 1, totalEpoch))
        error = 0
        for i in range(len(dataWithLabel)):
            eachDict = dataWithLabel[i]

            curr_label = int(eachDict['label'])
            curr_feature = eachDict['features']
            temp_numpy = np.zeros((numberOfFeatures, 1))

            for j in range(len(curr_feature)):
                temp_tuple = curr_feature[j]
                temp_numpy[j] = temp_tuple[0] + temp_tuple[1]

            dot_product = np.dot(weights.T, temp_numpy)
            predicted_label = np.argmax(dot_product)

            if (predicted_label != curr_label):
                error = error + 1
                weight_Diff = (weights[:, predicted_label] -
                               weights[:, curr_label])
                dotProduct_feature = np.dot(temp_numpy.T, temp_numpy)

                tau_numerator = np.dot(weight_Diff.T, temp_numpy) + 1
                tau_denom = 2 * dotProduct_feature
                tau = tau_numerator / tau_denom

                weights[:, curr_label] = weights[:, curr_label] + (
                    tau * temp_numpy[:, 0])
                weights[:, predicted_label] = weights[:, predicted_label] - (
                    tau * temp_numpy[:, 0])
        accuracy = 100 - ((error / total_images) * 100)
        print("Accuracy: ", accuracy)
        if (accuracy == 100.0):
            break
    with open(path + mname, 'wb') as file:
        pickle.dump(weights, file)
Beispiel #3
0
def do_training(directory, mname, pname, path, splitParam):
    true_labels = rd.load_label(directory['paths'][1])
    allLabels = len(list(set(true_labels)))
    total_images = len(true_labels)

    print("===========================")
    print("Reading data")

    dataWithLabel, updatedLabels = df.createDataWithLabel(
        'training', directory['paths'][0], true_labels, total_images,
        directory['featuretuple'], directory['dimensions'][1],
        directory['dimensions'][0], splitParam)

    total_images = len(updatedLabels)
    print("===========================")
    print("Initializing training")

    trainingDict = pc.training_Bayesian(dataWithLabel, allLabels,
                                        directory['featuretuple'],
                                        directory['dimensions'][1],
                                        directory['dimensions'][0])

    prior_prob = {}
    for each_label in range(allLabels):
        val, tot = pc.calculatePrior(directory['paths'][1], each_label)
        prior_prob[each_label] = (val, tot)

    predicted_value = pc.posteriorProbability(dataWithLabel, allLabels,
                                              prior_prob, trainingDict)

    diff = 0
    for i in range(total_images):
        if (int(updatedLabels[i]) != predicted_value[i]):
            diff = diff + 1

    accuracy = 100 - ((diff * 100) / total_images)
    print("Training done with accuracy ", accuracy)

    with open(path + mname, 'wb') as file:
        pickle.dump(trainingDict, file)
    with open(path + pname, 'wb') as file:
        pickle.dump(prior_prob, file)
def do_testing(mname, pathDict, splitParameter):
    numberOfFeatures = int(
        pathDict['dimensions'][1] / pathDict['featuretuple'][0]) * int(
            pathDict['dimensions'][0] / pathDict['featuretuple'][0])
    learnedWeights = pickle.load(open(mname, "rb"))

    true_labels = rd.load_label(pathDict['paths'][3])
    total_images = len(true_labels)

    print("===========================")
    print("Reading data")
    dataWithoutLabel = df.createDataWithLabel('testing', pathDict['paths'][2],
                                              true_labels, total_images,
                                              pathDict['featuretuple'],
                                              pathDict['dimensions'][1],
                                              pathDict['dimensions'][0],
                                              splitParameter)
    print("===========================")
    print("Initializing testing")

    error_test = 0
    for i in range(len(dataWithoutLabel)):

        eachDict = dataWithoutLabel[i]

        curr_feature = eachDict['features']
        temp_numpy = np.zeros((numberOfFeatures, 1))

        for j in range(len(curr_feature)):
            temp_tuple = curr_feature[j]
            temp_numpy[j] = temp_tuple[0] + temp_tuple[1]

        dot_product_test = np.dot(learnedWeights.T, temp_numpy)
        predicted_label = np.argmax(dot_product_test)
        if (predicted_label != int(true_labels[i])):
            error_test = error_test + 1

    print("Total error in testing data: ", error_test)
    print("Accuracy: ", 100 - ((error_test / total_images) * 100))
    return (100 - ((error_test / total_images) * 100))