コード例 #1
0
def FeedForward_UnitTest():
    featureSize = 12
    colorChannels = 1

    # Load Centroids and Initialize KMean module
    centroids = datamanipulation.LoadCentroids("C:/Images/TempCentroids_" +
                                               str(colorChannels) + ".txt")
    KMean = kmean.KMean(featureSize * featureSize * colorChannels,
                        len(centroids))

    # Load image and slice it
    image_a = datamanipulation.LoadImageData(["C:/Images/Satellite_1.png"],
                                             colorChannels)
    image_b = datamanipulation.LoadImageData(["C:/Images/Satellite_1_LQ.png"],
                                             colorChannels)
    compare_1 = datamanipulation.CompareImages(image_a, image_a)
    compare_2 = datamanipulation.CompareImages(image_a, image_b)
    data = datamanipulation.SliceImage(image_b, featureSize * colorChannels,
                                       featureSize)
    loadedPoints = len(data)

    # Classify points
    data = KMean.KmeanClassification(data, centroids)

    # Sort points by centroid
    classifiedPoints = 0
    for centroid in range(0, len(centroids)):
        points = [x for x in data if x[-3] == centroid]
        classifiedPoints = classifiedPoints + len(points)
        encoder = autoencoder.AutoEncoder([
            featureSize * featureSize * colorChannels, 100,
            featureSize * featureSize * colorChannels
        ])
        if (encoder.LoadParameters("C:/Images/EncoderParameters_" +
                                   str(colorChannels) + "_" + str(centroid) +
                                   ".txt")):
            print("feeding points into encoder")
            for point in points:
                output = encoder.FeedForward(
                    np.array(point[:-3]).reshape(144, 1))
                image_b = datamanipulation.InsertSubImage(
                    image_b,
                    output.flatten(),
                    featureSize,
                    colorChannels,
                    point[-2],
                    point[-1],
                    referenceImage=image_a)
        else:
            print("unable to load encoder")
        #	return False

    compare_3 = datamanipulation.CompareImages(image_a, image_b)
    print("Compare 1: " + str(compare_1))
    print("Compare 2: " + str(compare_2))
    print("Compare 3: " + str(compare_3))

    if (loadedPoints == classifiedPoints):
        return True
    return False
コード例 #2
0
def KmeanClassification_UnitTest():
    featureSize = 12
    colorChannels = 1

    # Load Centroids and Initialize KMean module
    centroids = datamanipulation.LoadCentroids("C:/Images/TempCentroids_" +
                                               str(colorChannels) + ".txt")
    KMean = kmean.KMean(featureSize * featureSize * colorChannels,
                        len(centroids))

    # Load Image and Slice it
    image = datamanipulation.LoadImageData(["C:/Images/Satellite_1_LQ.png"])
    data = datamanipulation.SliceImage(image, featureSize * colorChannels,
                                       featureSize)
    originalClusters = [x[-3] for x in data]

    # Classify Images
    data = KMean.KmeanClassification(data, centroids)
    finalClusters = [x[-3] for x in data]

    for a, b in zip(originalClusters, finalClusters):
        if (a == b):
            print("One or more points were left unclassified")
            return False

    return True
コード例 #3
0
def CreateTrainingSetFromData_UnitTest():
    featureSize = 12
    colorChannels = 1

    # Load Centroids and Initialize KMean module
    centroids = datamanipulation.LoadCentroids("C:/Images/TempCentroids_" +
                                               str(colorChannels) + ".txt")
    KMean = kmean.KMean(featureSize * featureSize * colorChannels,
                        len(centroids))

    # Load image and slice it
    images = ("C:/Images/Satellite_1.png", "C:/Images/Satellite_2.png",
              "C:/Images/Satellite_3.png", "C:/Images/Satellite_4.png",
              "C:/Images/Satellite_5.png")
    data = datamanipulation.SliceImage(datamanipulation.LoadImageData(images),
                                       featureSize * colorChannels,
                                       featureSize)

    # Classify points
    loadedPoints = len(data)
    data = KMean.KmeanClassification(data, centroids)

    print("Creating Training Sets with Threshold")
    for index in range(0, len(centroids)):
        trainingSet = datamanipulation.CreateTrainingSetFromData(
            data, centroids, index)
        if (len(trainingSet) > 0):
            print("Training Set for centroid " + str(index) + " has length " +
                  str(len(trainingSet)))
        else:
            print("Training Set for centroid " + str(index) + " is empty")

    print("Creating Training Sets without Threshold")
    trainingPoints = 0
    for index in range(0, len(centroids)):
        trainingSet = datamanipulation.CreateTrainingSetFromData(
            data, centroids, index, threshold=False)
        trainingPoints = trainingPoints + len(trainingSet)
        if (len(trainingSet) > 0):
            print("Training Set for centroid " + str(index) + " has length " +
                  str(len(trainingSet)))
        else:
            print("Training Set for centroid " + str(index) + " is empty")

    print("Loaded points vs training points: " + str(loadedPoints) + " / " +
          str(trainingPoints))
    if (loadedPoints == trainingPoints):
        return True
    return False
コード例 #4
0
def KMeanClustering_UnitTest():
    featureSize = 12
    colorChannels = 3
    numberOfCentroids = 12
    images = ("C:/Images/Satellite_1.png", "C:/Images/Satellite_2.png")
    data = datamanipulation.SliceImage(datamanipulation.LoadImageData(images),
                                       featureSize * colorChannels,
                                       featureSize)
    KMean = kmean.KMean(featureSize * featureSize * colorChannels,
                        numberOfCentroids)
    data, centroids = KMean.KMeanClustering(data, KMean.GenerateCentroids())

    for index in range(0, numberOfCentroids):
        points = [x for x in data if x[-3] == index]
        if (len(points) < 1): return False

    return True
コード例 #5
0
ファイル: main.py プロジェクト: spendola/KMeanEncoder
def main():
    print("Image Sharpening with Machine Learning\n")
    print("Sebastian Pendola, NY 2017\n\n")
    print("1) Train KMean Clustering")
    print("2) Train AutoEncoders")
    print("3) Enhance Image")
    print("4) Display Centroids")
    print("5) Test on Image")
    task = int(raw_input("\nChoose a Task: "))

    if task == 1:
        featureSize = int(raw_input("Feature Size: "))
        colorChannels = int(raw_input("Color Channels: "))
        numberOfCentroids = int(raw_input("Clusters: "))
        epochs = int(raw_input("Epochs: "))
        useLastCentroids = raw_input("Use last centroids? (y/n) ")
        startTime = dt.datetime.now().replace(microsecond=0)
        #images = ("images/Satellite_1.png", "images/Satellite_2.png", "images/Satellite_3.png", "images/Satellite_4.png", "images/Satellite_5.png")
        images = ("images/image_1.png", "images/image_2.png",
                  "images/image_3.png", "images/image_4.png",
                  "images/image_5.png", "images/image_6.png",
                  "images/image_7.png", "images/image_8.png")
        data = datamanipulation.SliceImage(
            datamanipulation.LoadImageData(images),
            featureSize * colorChannels, featureSize)
        publisher.PublishMsg("KMean Clustering Started")
        publisher.PublishMsg(
            str(numberOfCentroids) + " clusters of size " + str(featureSize) +
            "x" + str(featureSize))
        publisher.PublishCmd("validationgraph")
        publisher.PublishCmd("flushgraph")
        KMean = kmean.KMean(featureSize * featureSize * colorChannels,
                            numberOfCentroids)
        if (useLastCentroids == "y" or useLastCentroids == "Y"):
            data, centroids = KMean.KMeanClustering(
                data,
                datamanipulation.LoadCentroids("images/TempCentroids_" +
                                               str(colorChannels) + ".txt"),
                limit=epochs)
        else:
            data, centroids = KMean.KMeanClustering(data, limit=epochs)
        for centroid in range(0, len(centroids)):
            points = [x for x in data if x[-3] == centroid]
            print("Cluster " + str(centroid) + " has " + str(len(points)) +
                  " points")
        print("Execution Time: " +
              str(dt.datetime.now().replace(microsecond=0) - startTime))
        datamanipulation.SaveCentroids(
            centroids, "images/TempCentroids_" + str(colorChannels) + ".txt")

    elif task == 2:
        learningRate = float(raw_input("Learning Rate: "))
        lmbda = float(raw_input("Lambda: "))
        epochs = int(raw_input("Epochs: "))
        featureSize = int(raw_input("Feature Size: "))
        colorChannels = int(raw_input("Color Channels: "))

        # Load Centroids and Initialize KMean module
        centroids = datamanipulation.LoadCentroids("images/TempCentroids_" +
                                                   str(colorChannels) + ".txt")
        KMean = kmean.KMean(featureSize * featureSize * colorChannels,
                            len(centroids))

        # Load points and classify them
        #images = ("images/Satellite_1.png", "images/Satellite_2.png", "images/Satellite_3.png", "images/Satellite_4.png", "images/Satellite_5.png")
        images = ("images/image_1.png", "images/image_2.png",
                  "images/image_3.png", "images/image_4.png",
                  "images/image_5.png", "images/image_6.png",
                  "images/image_7.png", "images/image_8.png")
        data = datamanipulation.SliceImage(
            datamanipulation.LoadImageData(images),
            featureSize * colorChannels, featureSize)
        data = KMean.KmeanClassification(data, centroids)

        trainingCosts = []
        for centroid in range(0, len(centroids)):
            encoder = autoencoder.AutoEncoder([
                featureSize * featureSize * colorChannels, 100,
                featureSize * featureSize * colorChannels
            ])
            encoder.LoadParameters("images/EncoderParameters_" +
                                   str(colorChannels) + "_" + str(centroid) +
                                   ".txt")
            trainingSet = datamanipulation.CreateTrainingSetFromData(
                data, centroids, centroid, threshold=False)
            if (len(trainingSet) > 0):
                print("Training Samples of size " + str(len(trainingSet[0])))
                savePath = "images/EncoderParameters_" + str(
                    colorChannels) + "_" + str(centroid) + ".txt"
                trainingCosts.append(
                    encoder.Train(
                        trainingSet,
                        learningRate,
                        lmbda,
                        epochs,
                        len(trainingSet) if len(trainingSet) < 20 else 20,
                        path=savePath))
            else:
                print("Training Set " + str(index) + " is empty")
                trainingCosts.append(-1)

        for index in range(0, len(centroids)):
            print("AutoEncoder for Feature " + str(index) +
                  " has a learning cost of " + str(trainingCosts[index]))

    elif task == 3:
        featureSize = int(raw_input("Feature Size: "))
        colorChannels = int(raw_input("Color Channels: "))

        # Load Centroids and Initialize KMean module
        centroids = datamanipulation.LoadCentroids("images/TempCentroids_" +
                                                   str(colorChannels) + ".txt")
        KMean = kmean.KMean(featureSize * featureSize * colorChannels,
                            len(centroids))

        # Load reference and target images
        image_a = datamanipulation.LoadImageData(["images/Satellite_1.png"],
                                                 colorChannels)
        image_b = datamanipulation.LoadImageData(["images/Satellite_1_LQ.png"],
                                                 colorChannels)
        compare_1 = datamanipulation.CompareImages(image_a, image_b)

        # Slice reference image and classify slices
        data = datamanipulation.SliceImage(image_b,
                                           featureSize * colorChannels,
                                           featureSize)
        data = KMean.KmeanClassification(data, centroids)

        for centroid in range(0, len(centroids)):
            points = [x for x in data if x[-3] == centroid]
            if (len(points) > 0):
                encoder = autoencoder.AutoEncoder([
                    featureSize * featureSize * colorChannels, 100,
                    featureSize * featureSize * colorChannels
                ])
                if (encoder.LoadParameters("images/EncoderParameters_" +
                                           str(colorChannels) + "_" +
                                           str(centroid) + ".txt")):
                    for point in points:
                        input = np.array(point[:-3]).reshape(144, 1)
                        output = encoder.FeedForward(input / 256.0) * 256.0
                        image_b = datamanipulation.InsertSubImage(
                            image_b,
                            output.flatten(),
                            featureSize,
                            colorChannels,
                            point[-2],
                            point[-1],
                            reference=image_a)
                else:
                    print("unable to load encoder")

        compare_2 = datamanipulation.CompareImages(image_a, image_b)
        print("Initial Comparation: " + str(compare_1))
        print("Final Comparation: " + str(compare_2))

    elif task == 4:
        centroids = datamanipulation.LoadCentroids("images/TempCentroids.txt")
        datamanipulation.DisplayCentroids(centroids)

    elif task == 5:
        featureSize = int(raw_input("Feature Size: "))
        colorChannels = int(raw_input("Color Channels: "))
        centroids = datamanipulation.LoadCentroids("images/TempCentroids_" +
                                                   str(colorChannels) + ".txt")
        datamanipulation.MarkImage("images/Satellite_2.png", centroids,
                                   featureSize, colorChannels)