xValidate = torch.stack([transform(image) for image in xValidateImages]) yValidate = torch.Tensor([[yValue] for yValue in yValidate]) xTestImages = [Image.open(path) for path in xTestRaw] xTest = torch.stack([transform(image) for image in xTestImages]) yTest = torch.Tensor([[yValue] for yValue in yTest]) ###### ###### import MachineLearningCourse.Assignments.Module03.SupportCode.BlinkNeuralNetwork as BlinkNeuralNetwork # Create the model model = BlinkNeuralNetwork.BlinkNeuralNetwork(hiddenNodes=5) # Create the loss function to use (Mean Square Error) lossFunction = torch.nn.MSELoss(reduction='sum') # Create the optimization method (Stochastic Gradient Descent) and the step size (lr -> learning rate) optimizer = torch.optim.SGD(model.parameters(), lr=0.0001) ## # Move the model and data to the GPU if you're using your GPU ## device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") print("Device is:", device) model.to(device)
def ExecuteFitting(runSpecification, xTrain, yTrain, xValidate, yValidate): startTime = time.time() # Create features and train based on type of model # Create the model model = BlinkNeuralNetwork.BlinkNeuralNetwork(hiddenNodes = 6, hiddenNodesTwo = 4) device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") print("Device is:", device) model.to(device) # Move the data onto whichever device was selected xTrain = xTrain.to(device) yTrain = yTrain.to(device) xValidate = xValidate.to(device) yValidate = yValidate.to(device) converged = False epoch = 1 lastLoss = None convergence = runSpecification['convergence'] optimizer = torch.optim.SGD(model.parameters(), lr=runSpecification['learning_rate']) lossFunction = torch.nn.MSELoss(reduction='mean') patience = 0 while not converged and epoch < 5000: # Do the forward pass yTrainPredicted = model(xTrain) trainLoss = lossFunction(yTrainPredicted, yTrain) # Reset the gradients in the network to zero optimizer.zero_grad() # Backprop the errors from the loss on this iteration trainLoss.backward() # Do a weight update step optimizer.step() loss = trainLoss.item() # print(loss) if epoch > 10 and lastLoss != None and abs(lastLoss - loss) < convergence: if patience >= 0: converged = True pass else: patience += 1 else: lastLoss = loss patience = 0 epoch = epoch + 1 model.train(mode=True) endTime = time.time() runSpecification['runtime'] = endTime - startTime runSpecification['epoch'] = epoch yValidatePredicted = model(xValidate) validAccuracy = EvaluateBinaryClassification.Accuracy(yValidate, [ 1 if pred > 0.5 else 0 for pred in yValidatePredicted ]) runSpecification['accuracy'] = validAccuracy num_samples = len(xValidate) (low_bound, high_bound) = ErrorBounds.GetAccuracyBounds(validAccuracy, num_samples, 0.5) errorBound = (high_bound - low_bound) / 2 runSpecification['50PercentBound'] = errorBound return runSpecification
## num_trials = 3 validationAccuracyResults = [] testAccuracyResults = [] # Set up to hold information for creating ROC curves seriesFPRs = [] seriesFNRs = [] seriesLabels = [] errorImages = {} for i in range(num_trials): errorImages[i] = [] model = BlinkNeuralNetwork.LeNet(imageSize=xTrain[0].shape[1], convFilters=[(12, 6), (18, 5)], fcLayers=[20, 10]) model.to(device) optimizer = torch.optim.Adam(model.parameters(), lr=step) acc = trainModel(model, optimizer, maxEpoch, 25, "LossEpochFinal12-6-x-x-20-10-{}".format(i)) lower, _ = ErrorBounds.GetAccuracyBounds(acc, len(yValidate), 0.95) validationAccuracyResults.append((acc, acc - lower)) yTestPredicted = model(xTest) testAccuracy = EvaluateBinaryClassification.Accuracy( yTest, [1 if pred > 0.5 else 0 for pred in yTestPredicted]) lowerTest, _ = ErrorBounds.GetAccuracyBounds(testAccuracy, len(yTest), 0.95) testAccuracyResults.append((testAccuracy, testAccuracy - lowerTest))
except NotImplementedError: raise UserWarning("The 'model' parameter must have a 'predict' method that supports using a 'classificationThreshold' parameter with range [ 0 - 1.0 ] to create classifications.") return (FPRs, FNRs, thresholds) # Gather ROC curves FNRs_series = [] FPRs_series = [] Label_series = [] ####Edge features only model print("Starting with just edge features...") # Create features and train based on type of model # Create the model model = BlinkNeuralNetwork.BlinkNeuralNetwork(hiddenNodes = 6, hiddenNodesTwo = 4) device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") print("Device is:", device) model.to(device) # Move the data onto whichever device was selected xTrain = xTrain.to(device) yTrain = yTrain.to(device) xValidate = xValidate.to(device) yValidate = yValidate.to(device) converged = False epoch = 1 lastLoss = None
transforms.ToTensor() ,transforms.Normalize(mean=[0.], std=[0.5]) ]) xTrainImages = [ Image.open(path) for path in xTrainRaw ] xTrain = torch.stack([ transform(image) for image in xTrainImages ]) yTrain = torch.Tensor([ [ yValue ] for yValue in yTrain ]) xValidateImages = [ Image.open(path) for path in xValidateRaw ] xValidate = torch.stack([ transform(image) for image in xValidateImages ]) yValidate = torch.Tensor([ [ yValue ] for yValue in yValidate ]) xTestImages = [ Image.open(path) for path in xTestRaw ] xTest = torch.stack([ transform(image) for image in xTestImages ]) yTest = torch.Tensor([ [ yValue ] for yValue in yTest ]) # Creat the model model = BlinkNeuralNetwork.BlinkNeuralNetwork(first_convolution = 6, second_convolution = 16, first_connected_nodes = 120, second_connected_nodes = 84, output_nodes = 1) # Find the graphics card device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") print("Device is:", device) # Move the data onto whichever device was selected model.to(device) xTrain = xTrain.to(device) yTrain = yTrain.to(device) xValidate = xValidate.to(device) yValidate = yValidate.to(device) model.train_model_persample(xTrain, yTrain) # model.train_model(xTrain, yTrain)