def train(net, opt, trainBatcher, validBatcher, saver, minContext): while True: start = time.time() #Run epochs trainLoss, trainAcc = utils.runData(net, opt, trainBatcher, trainable=True, verbose=True) validLoss, validAcc = utils.runData(net, opt, validBatcher, minContext=minContext) trainLoss, validLoss = np.exp(trainLoss), np.exp(validLoss) #Print statistics print('\nEpoch: ', saver.epoch(), ', Time: ', time.time() - start) print('| Train Perp: ', trainLoss, ', Train Acc: ', trainAcc) print('| Valid Perp: ', validLoss, ', Valid Acc: ', validAcc) if np.isnan(validLoss) or np.isnan(trainLoss): print('Got a bad update. Resetting epoch') saver.refresh(net) else: saver.update(net, trainLoss, trainAcc, validLoss, validAcc)
def runToScore(model, dataset, data_paths, n=-1): ''' Runs on n many train data and returns the score and the resultant image _______ In: model: The model being used dataset: The ProbaVDataset data_paths: List of paths of the train dataset Out: scores: The scores vs the baseline results: The resultant images ''' scores = [] results = [] model.to(device) model.eval() if n < 0: n = len(dataset) counter = 0 for d, _, _ in dataset: if counter < n: result = runData(model, d) score = score_image(result, data_paths[counter]) scores.append(score) results.append(result) counter += 1 else: break return scores, results
def predictTest(model_file): model = torch.load(model_file).to(device) data_paths = all_scenes_paths(path_to_test) dataset = pd.ProbaVDataset(data_paths, True) images = [] model.eval() for d in dataset: images.append(runData(model, d)) return images
def test(net, batcher, minContext, name='Test'): start = time.time() loss, acc = utils.runData(net, None, batcher, minContext=minContext) loss = np.exp(loss) #Print statistics print('Time: ', time.time() - start) print(name, ' Perp: ', loss, ', Acc: ', acc)
self.som[mask] += self.alpha * update[...,None][mask]*self.diff[mask] self.som[self.som > 1] = 1 self.som[self.som < 0] = 0 def updateLearnParams(self): ''' Update the learning parameters for the next iteration. ''' self.curIter += 1 self.alpha = self.alphaInit * exp(-self.curIter/self.delta) omega = self.r * exp(-self.curIter/self.delta) self.omega2 = omega * omega if __name__ == '__main__': from matplotlib import pyplot as pp from matplotlib import animation as ani from utils import runData, simpleView colors = array([[0,0,0], [1,0,0], [0,1,0], [0,0,1], [1,0,1], [1,1,0], [0,1,1], [1,1,1]]) som = SOM(3,shape=(20,20,20)) runData(som, colors, 1000) simpleView(som, shape=(1,1), color=True)