示例#1
0
    def _getTrainingData(self, startPos=0, numSamples=5000):
        galaxyFactory = GalaxyFactory(isTrainingData=True)
        sr = SolutionReader(TRAINING_SOLUTIONS_PATH)
        sr.skipTo(startPos)

        galaxyIds = []
        solution_data = [[], [], []]
        training_data = []

        galaxyIndex = 0

        trainingSol = sr.next()
        while trainingSol:
            galaxy = galaxyFactory.getGalaxyForImage(os.path.join(TRAINING_IMAGE_PATH,
                                                                  str(trainingSol.GalaxyID) + '.jpg'))

            aspectRatio = galaxy.getAspectRatio()

            if not math.isnan(aspectRatio):
                galaxyIds.append(galaxy.id)

                training_data.append([aspectRatio])

                solution_data[0].append(trainingSol.Class2_1)
                solution_data[1].append(trainingSol.Class2_2)

            galaxyIndex += 1
            if galaxyIndex > numSamples:
                break

            trainingSol = sr.next()

        return {'galaxyIds': galaxyIds, 'training_data': training_data, 'solution_data': solution_data}
示例#2
0
def generateTestResults():
    classifiers = getClassifiers()

    print "Scoring Solutions"
    galaxyFactory = GalaxyFactory()

    results = SolutionWriter(config.TEST_SOLUTION_OUTPUT_PATH)
    #numSamples =100
    sampleNum = 0
    for imagePath in glob.glob(config.TEST_IMAGE_PATH + '/*.jpg'):
        galaxy = galaxyFactory.getGalaxyForImage(imagePath)

        solution = [galaxy.id]
        for classifier in classifiers:
            predictions = classifier.predict(galaxy)
            predictions = [prediction if prediction >= 0.0 else 0.0 for prediction in predictions]
            solution.extend(predictions)

        if len(solution) != 38:
            print "Problem with galaxy with id %s. Solution is not the correct length." % galaxy.id
        else:
            results.append(Solution._make(solution))

        if not sampleNum % 10:
            print sampleNum

        sampleNum += 1
        #if sampleNum > numSamples:
        #    break

    results.close()
示例#3
0
def generateSolutionsFromTrainingSet(startPos, numSamples):
    classifiers = getClassifiers()

    print "Classifying Solutions"
    galaxyFactory = GalaxyFactory()

    reader = SolutionReader(config.TRAINING_SOLUTIONS_PATH)
    results = SolutionWriter(config.TRAINING_SOLUTION_OUTPUT_PATH)
    reader.skipTo(startPos)

    sampleNum = 0
    solution = reader.next()
    while solution:
        imagePath = os.path.join(config.TRAINING_IMAGE_PATH, str(solution.GalaxyID) + '.jpg')
        galaxy = galaxyFactory.getGalaxyForImage(imagePath)

        predictedSolution = [galaxy.id]
        for classifier in classifiers:
            predictedSolution.extend(classifier.predict(galaxy))

        if len(solution) != 38:
            print "Problem with galaxy with id %s. Solution is not the correct length." % galaxy.id
        else:
            results.append(Solution._make(predictedSolution))

        sampleNum += 1
        if sampleNum > numSamples:
            break

        solution = reader.next()

    results.close()
示例#4
0
    def plot(self):
        c = Class4()
        c.train()

        sr = SolutionReader(TRAINING_SOLUTIONS_PATH)
        sr.skipTo(15000)

        galaxyFactory = GalaxyFactory(isTrainingData=True)
        trainingSol = sr.next()
        galaxy = galaxyFactory.getGalaxyForImage(os.path.join(TRAINING_IMAGE_PATH, str(trainingSol.GalaxyID) + '.jpg'))

        print c.predict(galaxy)
示例#5
0
    def _getTrainingData(self, startPos=0, numSamples=5000, verbose=False):
        galaxyFactory = GalaxyFactory(isTrainingData=True)
        sr = SolutionReader(TRAINING_SOLUTIONS_PATH)
        sr.skipTo(startPos)

        galaxyIds = []
        solution_data = [[], [], [], []]
        training_data = []
        success = []

        galaxyIndex = 0

        trainingSol = sr.next()
        while trainingSol:
            galaxy = galaxyFactory.getGalaxyForImage(
                os.path.join(TRAINING_IMAGE_PATH, str(trainingSol.GalaxyID) + ".jpg")
            )

            if galaxy.initSuccess:
                if galaxy.ellipse.ellipse_area() > 0:
                    features = self._extractFeatures(galaxy, useCache=True)

                    if features:
                        training_data.append(
                            [
                                features["bulgeIntensity"],
                                features["R"] - features["G"],
                                features["bulgeBlobFractionalArea"],
                            ]
                        )

                        solution_data[0].append(trainingSol.Class5_1)
                        solution_data[1].append(trainingSol.Class5_2)
                        solution_data[2].append(trainingSol.Class5_3)
                        solution_data[3].append(trainingSol.Class5_4)

                        galaxyIds.append(trainingSol.GalaxyID)
                        success.append(True)

                        if verbose and not galaxyIndex % 10:
                            print galaxyIndex

                        galaxyIndex += 1
                        if galaxyIndex > numSamples:
                            break

            else:
                galaxyIds.append(trainingSol.GalaxyID)
                success.append(False)

            trainingSol = sr.next()

        return {"galaxyIds": galaxyIds, "training_data": training_data, "solution_data": solution_data}
示例#6
0
    def _getTrainingData(self, startPos=0, numSamples=5000, verbose=False):
        galaxyFactory = GalaxyFactory(isTrainingData=True)
        sr = SolutionReader(TRAINING_SOLUTIONS_PATH)
        sr.skipTo(startPos)

        galaxyIds = []
        solution_data = [[], []]

        rot_line_test = []
        rot_line_test_deriv = []
        scaled_img = []
        rot_bias = []

        galaxyIndex = 0

        trainingSol = sr.next()
        while trainingSol:
            galaxy = galaxyFactory.getGalaxyForImage(os.path.join(TRAINING_IMAGE_PATH,
                                                                  str(trainingSol.GalaxyID) + '.jpg'))

            features = self._extractFeatures(galaxy, useCache=True)

            if features != None and features['rotLineTestResult'] != None:
                galaxyIds.append(galaxy.id)

                rot_line_test.append(features['rotLineTestResult'])
                rot_line_test_deriv.append(features['rotLineTestDerivative'])
                scaled_img.append(features['scaledImg'])
                rot_bias.append(features['rotBias'])

                solution_data[0].append(trainingSol.Class4_1)
                solution_data[1].append(trainingSol.Class4_2)

            if verbose and not galaxyIndex % 10:
                print galaxyIndex

            galaxyIndex += 1
            if galaxyIndex > numSamples:
                break

            trainingSol = sr.next()

        return {'galaxyIds': galaxyIds,
                'rot_line_test': rot_line_test,
                'rot_line_test_deriv': rot_line_test_deriv,
                'scaled_img': scaled_img,
                'rot_bias': rot_bias,
                'solution_data': solution_data}
示例#7
0
    def _getTrainingData(self, startPos=0, numSamples=5000, verbose=False):
        galaxyFactory = GalaxyFactory(isTrainingData=True)
        sr = SolutionReader(TRAINING_SOLUTIONS_PATH)
        sr.skipTo(startPos)

        galaxyIds = []
        solution_data = [[], [], []]
        training_data = []
        success = []

        galaxyIndex = 0

        trainingSol = sr.next()
        while trainingSol:
            galaxy = galaxyFactory.getGalaxyForImage(os.path.join(TRAINING_IMAGE_PATH,
                                                                  str(trainingSol.GalaxyID) + '.jpg'))

            if galaxy.initSuccess:
                features = self._extractFeatures(galaxy, useCache=True)

                if features:
                    training_data.append([features['bulgeIntensity'],
                                          features['R'] - features['G'],
                                          features['asymScore'],
                                          features['rotBias']])

                    solution_data[0].append(trainingSol.Class1_1)
                    solution_data[1].append(trainingSol.Class1_2)
                    solution_data[2].append(trainingSol.Class1_3)

                    galaxyIds.append(trainingSol.GalaxyID)
                    success.append(True)

                    if verbose and not galaxyIndex % 10:
                        print galaxyIndex

                    galaxyIndex += 1
                    if galaxyIndex > numSamples:
                        break

            else:
                galaxyIds.append(trainingSol.GalaxyID)
                success.append(False)

            trainingSol = sr.next()

        return {'galaxyIds': galaxyIds, 'training_data': training_data, 'solution_data': solution_data, 'success': success}
示例#8
0
    def _getTrainingData(self, startPos=0, numSamples=5000, verbose=False):
        galaxyFactory = GalaxyFactory(isTrainingData=True)
        sr = SolutionReader(TRAINING_SOLUTIONS_PATH)
        sr.skipTo(startPos)

        galaxyIds = []
        solution_data = [[], [], []]
        training_data = []

        galaxyIndex = 0

        trainingSol = sr.next()
        while trainingSol:
            galaxy = galaxyFactory.getGalaxyForImage(
                os.path.join(TRAINING_IMAGE_PATH, str(trainingSol.GalaxyID) + ".jpg")
            )

            aspectRatio = galaxy.getAspectRatio()

            if not math.isnan(aspectRatio):
                galaxyIds.append(galaxy.id)

                training_data.append([aspectRatio])

                solution_data[0].append(trainingSol.Class7_1)
                solution_data[1].append(trainingSol.Class7_2)
                solution_data[2].append(trainingSol.Class7_3)

            galaxyIndex += 1
            if galaxyIndex > numSamples:
                break

            if verbose and not galaxyIndex % 10:
                print galaxyIndex

            trainingSol = sr.next()

        return {"galaxyIds": galaxyIds, "training_data": training_data, "solution_data": solution_data}