def Featurize_X_Histogram(x_raw): x = [] for sample in x_raw: # x-gradient histogram image = Image.open(sample) xGradients = Assignment5Support.Convolution3x3(image, [[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]) xFeatures = CalculateHistogramFeatures(xGradients) x.append(xFeatures) return x
def Featurize_X_Gradient(x_raw): x = [] for sample in x_raw: # x-gradient 9 grids of 8x8 pixels image = Image.open(sample) xGradients = Assignment5Support.Convolution3x3(image, [[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]) xFeatures = CalculateGradientFeatures(xGradients) x.append(xFeatures) return x
def Featurize_Y_Histogram(x_raw): x = [] for sample in x_raw: # y-gradient histogram image = Image.open(sample) yGradients = Assignment5Support.Convolution3x3(image, [[1, 2, 1],[0, 0, 0],[-1, -2, -1]]) yFeatures = CalculateHistogramFeatures(yGradients) x.append(yFeatures) return x
def Featurize_Y_Gradient(x_raw): x = [] for sample in x_raw: # y-gradient 9 grids of 8x8 pixels image = Image.open(sample) yGradients = Assignment5Support.Convolution3x3(image, [[1, 2, 1],[0, 0, 0],[-1, -2, -1]]) yFeatures = CalculateGradientFeatures(yGradients) x.append(yFeatures) return x
def Featurize(x_raw): x = [] for sample in x_raw: features = [] image = Image.open(sample) yGradients = Assignment5Support.Convolution3x3( image, [[1, 2, 1], [0, 0, 0], [-1, -2, -1]]) xGradients = Assignment5Support.Convolution3x3( image, [[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]) # y-gradient 9 grids of 8x8 pixels features += CalculateGradientFeatures(yGradients) # y-gradient histogram features += CalculateHistogramFeatures(yGradients) # x-gradient 9 grids of 8x8 pixels features += CalculateGradientFeatures(xGradients) # x-gradient histogram features += CalculateHistogramFeatures(xGradients) x.append(features) return x
## Some of this references my answers to previous assignments. ## Replace my references with references to your answers to those assignments. ## IMPORTANT NOTE !! ## Remember to install the Pillow library (which is required to execute 'import PIL') import Assignment5Support import numpy as np ## NOTE update this with your equivalent code.. import TrainTestSplit kDataPath = "/Users/Mims/Documents/school/assignments/BlinkSupport/dataset_B_Eye_Images" (xRaw, yRaw) = Assignment5Support.LoadRawData(kDataPath, includeLeftEye=True, includeRightEye=True) (xTrainRaw, yTrainRaw, xTestRaw, yTestRaw) = TrainTestSplit.TrainTestSplit(xRaw, yRaw, percentTest=.25) print("Train is %f percent closed." % (sum(yTrainRaw) / len(yTrainRaw))) print("Test is %f percent closed." % (sum(yTestRaw) / len(yTestRaw))) print("Calculating features...") (xTrain, xTest) = Assignment5Support.Featurize(xTrainRaw, xTestRaw, includeGradients=True, includeRawPixels=False, includeIntensities=True) yTrain = yTrainRaw
## Replace my references with references to your answers to those assignments. ## IMPORTANT NOTE !! ## Remember to install the Pillow library (which is required to execute 'import PIL') import matplotlib.pyplot as plt import Assignment5Support import Featurize import RandomForest import EvaluationsStub if __name__ == "__main__": kDataPath = "..\\..\\dataset_B_Eye_Images" (xRaw, yRaw) = Assignment5Support.LoadRawData(kDataPath, includeLeftEye=True, includeRightEye=True) (xTrainRaw, yTrainRaw, xTestRaw, yTestRaw) = Assignment5Support.TrainTestSplit(xRaw, yRaw, percentTest=.25) print("Train is %f percent closed." % (sum(yTrainRaw) / len(yTrainRaw))) print("Test is %f percent closed." % (sum(yTestRaw) / len(yTestRaw))) yTrain = yTrainRaw yTest = yTestRaw ############################ print("========== Use Random Forest for Y-Gradient ==========") print("### Calculating features ###") xTrain_Y_Gradient = Featurize.Featurize_Y_Gradient(xTrainRaw) xTest_Y_Gradient = Featurize.Featurize_Y_Gradient(xTestRaw)
## Some of this references my answers to previous assignments. ## Replace my references with references to your answers to those assignments. ## IMPORTANT NOTE !! ## Remember to install the Pillow library (which is required to execute 'import PIL') ## Remember to install Pytorch: https://pytorch.org/get-started/locally/ (if you want GPU you need to figure out CUDA...) import Assignment5Support ## NOTE update this with your equivalent code. import TrainTestSplit kDataPath = "..\\Datasets\\FaceData\\dataset_B_Eye_Images" (xRaw, yRaw) = Assignment5Support.LoadRawData(kDataPath, includeLeftEye=True, includeRightEye=True) (xTrainRaw, yTrainRaw, xTestRaw, yTestRaw) = TrainTestSplit.TrainTestSplit(xRaw, yRaw, percentTest=.25) print("Train is %f percent closed." % (sum(yTrainRaw) / len(yTrainRaw))) print("Test is %f percent closed." % (sum(yTestRaw) / len(yTestRaw))) from PIL import Image import torch import torchvision.transforms as transforms import CrossValidationSupport torch.cuda.set_device(0)
import numpy as np import copy import CrossValidationSupport import Evaluations import ErrorBounds import NeuralNetworkModel predictionThreshold = .5 #thresholds = [.5] #for i in range(0,101): # thresholds.append(i*.01) kDataPath = "..\\Datasets\\FaceData\\dataset_B_Eye_Images" (xRaw, yRaw) = Assignment5Support.LoadRawData(kDataPath, includeLeftEye = True, includeRightEye = True) (xTrainRaw, yTrainRaw, xTestRaw, yTestRaw) = TrainTestSplit.TrainTestSplit(xRaw, yRaw, percentTest = .25) print("Train is %f percent closed." % (sum(yTrainRaw)/len(yTrainRaw))) print("Test is %f percent closed." % (sum(yTestRaw)/len(yTestRaw))) print("Calculating features...") (xTrain, xTest) = Assignment5Support.Featurize(xTrainRaw, xTestRaw, includeGradients=False, includeXGradients=False, includeYGradients=False, includeYHistogram=False, includeXHistogram=False, includeRawPixels=False, includeIntensities=True) yTrain = yTrainRaw yTest = yTestRaw k = 5 UseCrossValidation = True
## Some of this references my answers to previous assignments. ## Replace my references with references to your answers to those assignments. ## IMPORTANT NOTE !! ## Remember to install the Pillow library (which is required to execute 'import PIL') import Assignment5Support import EvaluationsStub import matplotlib.pyplot as plt import numpy as np if __name__ == "__main__": kDataPath = "..\\..\\dataset_B_Eye_Images" (xRaw, yRaw) = Assignment5Support.LoadRawData(kDataPath, includeLeftEye=True, includeRightEye=True) (xTrainRaw, yTrainRaw, xTestRaw, yTestRaw) = Assignment5Support.TrainTestSplit(xRaw, yRaw, percentTest=.25) print("Train is %f percent closed." % (sum(yTrainRaw) / len(yTrainRaw))) print("Test is %f percent closed." % (sum(yTestRaw) / len(yTestRaw))) print('Featurize the Samples') (xTrain, xTest) = Assignment5Support.Featurize(xTrainRaw, xTestRaw, includeGradients=False, includeRawPixels=False, includeIntensities=True) yTrain = np.array(yTrainRaw)[np.newaxis].T yTest = np.array(yTestRaw)[np.newaxis].T
## Some of this references my answers to previous assignments. ## Replace my references with references to your answers to those assignments. ## IMPORTANT NOTE !! ## Remember to install the Pillow library (which is required to execute 'import PIL') import Assignment5Support import matplotlib.pyplot as plt import numpy as np if __name__ == "__main__": kDataPath = "..\\..\\dataset_B_Eye_Images" (xRaw, yRaw) = Assignment5Support.LoadRawData(kDataPath, includeLeftEye=True, includeRightEye=True) (xTrainRaw, yTrainRaw, xTestRaw, yTestRaw) = Assignment5Support.TrainTestSplit(xRaw, yRaw, percentTest=.25) print("Train is %f percent closed." % (sum(yTrainRaw) / len(yTrainRaw))) print("Test is %f percent closed." % (sum(yTestRaw) / len(yTestRaw))) print("Calculating features...") (xTrain, xTest) = Assignment5Support.Featurize(xTrainRaw, xTestRaw, includeGradients=True, includeRawPixels=False, includeIntensities=False) yTrain = yTrainRaw yTest = yTestRaw
from PIL import Image import Assignment5Support import Featurize image = Image.open( '../dataset_B_Eye_Images/openRightEyes/Oscar_DLeon_0001_R.jpg') # y-gradient 9 grids of 8x8 pixels yGradients = Assignment5Support.Convolution3x3( image, [[1, 2, 1], [0, 0, 0], [-1, -2, -1]]) yFeatures = Featurize.CalculateGradientFeatures(yGradients) print(yFeatures[:12]) # x-gradient 9 grids of 8x8 pixels xGradients = Assignment5Support.Convolution3x3( image, [[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]) xFeatures = Featurize.CalculateGradientFeatures(xGradients) print(xFeatures[:12]) # y-graident 5-bin histogram yFeatures = Featurize.CalculateHistogramFeatures(yGradients) print(yFeatures[:5]) # # x-gradient 5-bin histogram xFeatures = Featurize.CalculateHistogramFeatures(xGradients) print(xFeatures[:5]) # result: # [-59, 63, -6.59375, -92, 78] # [-125, 249, 13.3125, -196, 326] # [0.6545138888888888, 0.2482638888888889, 0.06597222222222222, 0.024305555555555556, 0.006944444444444444]
import Assignment5Support import numpy as np import NeuralNetStack import TrainTestSplit import Evaluations kDataPath = "/Users/Mims/Documents/school/assignments/BlinkSupport/dataset_B_Eye_Images" (xRaw, yRaw) = Assignment5Support.LoadRawData(kDataPath, includeLeftEye=True, includeRightEye=True) (xTrainRaw, yTrainRaw, xTestRaw, yTestRaw) = TrainTestSplit.TrainTestSplit(xRaw, yRaw, percentTest=.25) print("Train is %f percent closed." % (sum(yTrainRaw) / len(yTrainRaw))) print("Test is %f percent closed." % (sum(yTestRaw) / len(yTestRaw))) print("Calculating features...") (xTrain, xTest) = Assignment5Support.Featurize(xTrainRaw, xTestRaw, includeGradients=False, includeRawPixels=False, includeIntensities=True) yTrain = yTrainRaw yTest = yTestRaw #T = [(t0, [1,0,0,0,0]), (t1, [0,1,0,0,0]), (t2, [0,0,1,0,0]), (t3, [0,0,0,1,0]), (t4, [0,0,0,0,1])] trainingData = [] for i in range(len(xTrain)):