Example #1
0
 def __init__(self, nInputs=None, nHiddenLayers=None, bias=True, new=True, fileName=""):
     self.params = {}
     self.config = ConfigProvider().getNNTrainConfig()
     if new:
         self.params["nInputs"] = nInputs
         self.params["nHiddenLayers"] = nHiddenLayers
         self.params["bias"] = bias
         self.nn = NeuralNetwork().createNew(nInputs, nHiddenLayers, N_OUTPUT, bias)
     else:
         self.nn = NeuralNetwork().load(fileName)
    def test_saveAndLoad(self):
        ds = self.createORData()
        self.nn.train(ds)
        self.nn.save(name)
        
        nn2 = NeuralNetwork()
        nn2.load(name)
        
        self.assertNotEqual(self.nn, nn2)
        
        self.assertTrue(sameEntries(self.nn.net.params, nn2.net.params))
        for inpt, _ in ds:
            self.assertEqual(self.nn.activate(inpt), nn2.activate(inpt))

        self.removeFile()
Example #3
0
class NetworkUtil(object):
    '''
    Class to train and test a neural network 
    '''

    def __init__(self, nInputs=None, nHiddenLayers=None, bias=True, new=True, fileName=""):
        self.params = {}
        self.config = ConfigProvider().getNNTrainConfig()
        if new:
            self.params["nInputs"] = nInputs
            self.params["nHiddenLayers"] = nHiddenLayers
            self.params["bias"] = bias
            self.nn = NeuralNetwork().createNew(nInputs, nHiddenLayers, N_OUTPUT, bias)
        else:
            self.nn = NeuralNetwork().load(fileName)

    def train(self, trainData, convergence=False):
        start = time.time()
        print "start Training at " + str(datetime.fromtimestamp(start))
        if convergence:
            self.nn.trainConvergence(trainData, **self.config)
        else:
            self.nn.train(trainData, **self.config)
        print "Training Done in %.2fs" % (time.time() - start)
    
    def test(self, testData=None):
        self.nn.test(testData)
        print "Testing Done"

    def activate(self, testData):
        total = (len(testData) / 2) / 100.0
        matrix = np.array([[0, 0, 0], [0, 0, 0]])
        resArr = []
        for inpt, target in testData:
            res = self.nn.activate(inpt)
            clazz = self.nn._clazz(res)
            resArr.append([res, clazz, target])
            matrix[target[0]][clazz] += 1
        matrix[0, 2] = matrix[0, 0] / total
        matrix[1, 2] = matrix[1, 1] / total 
        return matrix, np.array(resArr)

    def save(self, name):
        self.nn.save(name)

    def __str__(self):
        self.params.update(self.config)
        return str(self.nn) + "\n" + str(self.params)
Example #4
0
 def _initNeuralNetwork(self, networkFile):
     nnCreate = self.config.getNNInitConfig()
     self.nn = NeuralNetwork()
     if networkFile == None:
         self.nn.createNew(**nnCreate)
     else:
         self.nn.load(networkFile)
Example #5
0
 def _initNeuralNetwork(self, networkFile):
     nn_conf = self.config.getNeuralNetworkConfig()
     self.nn = NeuralNetwork()
     if networkFile == None:
         self.nn.createNew(nn_conf["nInputs"], nn_conf["nHiddenLayers"], nn_conf["nOutput"], nn_conf["bias"])
     else:
         self.nn.load(networkFile)
 def setUp(self):
     self.nn = NeuralNetwork()
     self.config = {
         "learningrate": 0.01,
         "momentum": 0.1,
         "maxEpochs": 500,
         "continueEpochs": 15,
         "validationProportion": 0.25
     }
     self.nn.createNew(2, 4, 1, bias=True)
 def setUp(self):
     self.nn = NeuralNetwork()
     self.config = ConfigProvider().getNeuralNetworkConfig()
     self.nn.createNew(2, 4, 1, bias=True)
class TestNeuralNetwork(unittest.TestCase):


    def setUp(self):
        self.nn = NeuralNetwork()
        self.config = ConfigProvider().getNeuralNetworkConfig()
        self.nn.createNew(2, 4, 1, bias=True)

    def _createData(self, nInput, nTarget, values):
        ds = SupervisedDataSet(nInput, nTarget)
        for inp, target in values:
            ds.addSample(inp, target)
        
        return ds

    def createORData(self):
        values = [((0, 0), (0,)), ((0, 1), (1,)), ((1, 0), (1,)), ((1, 1), (1,))]
        return self._createData(2, 1, values)

    def createXORData(self):
        values = [((0, 0), (0,)), ((0, 1), (1,)), ((1, 0), (1,)), ((1, 1), (0,))]
        return self._createData(2, 1, values)

    def createANDData(self):
        values = [((0, 0), (0,)), ((0, 1), (0,)), ((1, 0), (0,)), ((1, 1), (1,))]
        return self._createData(2, 1, values)

    def removeFile(self):
        try:
            os.remove(self.nn.path + name + ".nn")
        except OSError as e:
            print e.message

    def test_xor(self):
        ds = self.createXORData()

        self.nn.train(ds, self.config["maxEpochs"], self.config["learningrate"], self.config["momentum"])

        #TODO may fail with delta 0.2
        for inpt, target in ds:
            self.assertAlmostEqual(self.nn.activate(inpt), target, delta=0.2)

    def test_and(self):
        ds = self.createANDData()
        self.nn.train(ds, self.config["maxEpochs"], self.config["learningrate"], self.config["momentum"])

        #TODO may fail with delta 0.2
        for inpt, target in ds:
            self.assertAlmostEqual(self.nn.activate(inpt), target, delta=0.2)

    def test_saveAndLoad(self):
        ds = self.createORData()
        self.nn.train(ds)
        self.nn.save(name)
        
        nn2 = NeuralNetwork()
        nn2.load(name)
        
        self.assertNotEqual(self.nn, nn2)
        
        self.assertTrue(sameEntries(self.nn.net.params, nn2.net.params))
        for inpt, _ in ds:
            self.assertEqual(self.nn.activate(inpt), nn2.activate(inpt))

        self.removeFile()

    def test_test(self):
        ds = self.createORData()
        self.nn.train(ds)
        self.nn.test(ds, False)

    def test_testBeforeTrain(self):
        with self.assertRaises(ValueError):
            self.nn.test(None, False)
Example #9
0
class PoSDBoS(object):

    def __init__(self, networkFile=None, demo=False, demoFile=None):
        '''Main class for drowsiness detection
        
        :param string networkFile: file name of the saved neural network (path: "/../../data/<networkFile>.nn")
        '''
        self.demo = demo
        self.running = True
        self.config = ConfigProvider()
        self._initPoSDBoS()
        self._initNeuralNetwork(networkFile)
        self._initFeatureExtractor(demoFile)
        self.dm = DrowsinessMonitor()
        self.fileUtil = EEGTableFileUtil()

    def _initPoSDBoS(self):
        posdbosConfig = self.config.getPoSDBoSConfig()
        self.drowsyMinCount = posdbosConfig.get("drowsyMinCount")
        self.awakeMinCount = posdbosConfig.get("awakeMinCount")
        self.classified = [0, 0]
        self.curClass = 0
        self.classCount = 0
        self.found = 0

    def _initNeuralNetwork(self, networkFile):
        nnCreate = self.config.getNNInitConfig()
        self.nn = NeuralNetwork()
        if networkFile == None:
            self.nn.createNew(**nnCreate)
        else:
            self.nn.load(networkFile)

    def _initFeatureExtractor(self, demoFile):
        self.demoFile = demoFile
        collector = self._initDataCollector(self.demoFile)
        self.fe = FeatureExtractor(collector)
        self.inputQueue = self.fe.extractQueue

    def _initDataCollector(self, demoFile):
        collectorConfig = self.config.getCollectorConfig()
        if self.demo:
            return DummyDataCollector(demoFile, **collectorConfig)
        else:
            return EEGDataCollector(None, **collectorConfig)

    def close(self):
        self.running = False

    def run(self):
        fet = threading.Thread(target=self.fe.start)
        fet.start()
        dmt = threading.Thread(target=self.dm.run)
        dmt.start()
        features = []
        total = 0
        start = time.time()
        c = []
        while self.running and dmt.is_alive():
            try:
                #awake = 0, drowsy = 1
                data = self.inputQueue.get(timeout=1)
                features.append(data)
                clazz = self.nn.activate(data, True)
                c.append([clazz, clazz])
                self.setStatus(clazz)
                total += 1
            except Empty:
                print "needed %sms for %d windows" % (time.time() - start, total) 
                pass
            except KeyboardInterrupt:
                self.close()
            except Exception as e:
                print e.message
                self.close()
        #self.writeFeature(c)
        self.fe.close()
        self.dm.close()
        dmt.join()

    def setStatus(self, clazz):
        self.classified[clazz] += 1
        if self.curClass == clazz:
            self.classCount += 1
        else:
            self.curClass = clazz
            self.classCount = 0

        info = "class %d row (%s)" % (clazz, str(self.classCount))
        if clazz == 1 and self.classCount >= self.drowsyMinCount:
            self.dm.setStatus(clazz, info)
            self.found += 1
        elif clazz == 0 and self.classCount >= self.awakeMinCount:
            self.dm.setStatus(clazz, info)

    def writeFeature(self, data):
        filePath = scriptPath + "/../data/" + "classes.csv"
        #filePath = scriptPath + "/../data/" + "drowsy_full_.csv"

        header = ["clazz", "clazz2"]
        #start = 4
        #end = start + len(data[0])/6
        #for field in self.config.getCollectorConfig().get("fields"):
        #    header.extend([str(x) + "Hz" + field for x in range(start, end)])
        self.fileUtil.writeFile(filePath, data, header)
Example #10
0
class PoSDBoS(object):
    
    def __init__(self, networkFile=None, demo=False, demoFile=None):
        '''Main class for drowsiness detection
        
        :param string networkFile: file name of the saved neural network (path: "/../../data/<networkFile>.nn")
        '''
        self.demo = demo
        self.running = True
        self.config = ConfigProvider()
        self._initNeuralNetwork(networkFile)
        self._initFeatureExtractor(demoFile)
        self.dm = DrowsinessMonitor()
        self.fileUtil = EEGTableFileUtil()

    def _initNeuralNetwork(self, networkFile):
        nn_conf = self.config.getNeuralNetworkConfig()
        self.nn = NeuralNetwork()
        if networkFile == None:
            self.nn.createNew(nn_conf["nInputs"], nn_conf["nHiddenLayers"], nn_conf["nOutput"], nn_conf["bias"])
        else:
            self.nn.load(networkFile)

    def _initFeatureExtractor(self, demoFile):
        collector = self._initDataCollector(demoFile)
        self.fe = FeatureExtractor(collector)
        self.inputQueue = self.fe.extractQueue

    def _initDataCollector(self, demoFile):
        collectorConfig = self.config.getCollectorConfig()
        if self.demo:
            return DummyDataCollector(demoFile, **collectorConfig)
        else:
            return EEGDataCollector(None, **collectorConfig)

    def close(self):
        self.running = False

    def run(self):
        fet = threading.Thread(target=self.fe.start)
        fet.start()
        dmt = threading.Thread(target=self.dm.run)
        dmt.start()
        features = []
        while self.running and dmt.is_alive():
            try:
                data = self.inputQueue.get(timeout=1)
                features.append(data)
                x = random.randint(1, 10)%2
                y = random.randint(1, 10)%2
                data = (x, y)
                
                clazz = self.nn.activate(data)
                info = "%d XOR %d is %d; queue: %d" % (x, y, clazz, self.inputQueue.qsize()) 
                self.dm.setStatus(clazz, info)
                #sleep(1)
            except Empty:
                pass
                #if self.demo:
                #    self.close()
            except KeyboardInterrupt:
                self.close()
            except Exception as e:
                print e.message
                self.close()
        self.writeFeature(features)
        self.fe.close()
        self.dm.close()
        dmt.join()

    def writeFeature(self, data):
        filePath = scriptPath + "/../data/" + "test.csv"
        header = []
        for field in ["F3", "F4", "F7", "F8"]:
            for i in range(1, 5):
                header.append("%s_%s" % (field ,str(i)))
        self.fileUtil.writeFile(filePath, data, header)