예제 #1
0
 async def load_classifier(self, sid, args):
     if self.users.get(sid) is not None:
         try:
             self.clf[sid] = ml.load(self.users[sid]["weights"])
             return sid, True
         except FileNotFoundError:
             raise Exception(f'Cannot load classifier')
     else:
         raise Exception(f'User not logged in!')
예제 #2
0
    async def retrieve_prediction_results(self, sid, args):
        if self.users.get(sid) is not None:
            uuid, data = args
            data = np.array(data)
            data = np.expand_dims(data, axis=0)

            # load classifier if not already loaded
            if self.clf.get(sid) is None:
                self.clf[sid] = ml.load(f'clfs/{self.users[sid]["username"]}')
            p300 = self.clf[sid].predict(data)
            p300 = p300[0]

            score = 1
            results = (uuid, p300, score)
            return sid, results
        else:
            raise Exception(f'User not logged in!')
예제 #3
0
    def _loop_worker(self):
        """The main loop for performing real time analysis.
        Takes items from an analysis queue sequentially, forms mne epochs, and either uses the data for real time
        training or to predict the letter that was mind-typed.
        Structure is adapted from rteeg.rteeg.analysis._loop_worker.
        """
        sleep_time = 0.01  # Time to sleep between queries.

        while not self._kill_signal.is_set():
            # when items exist in the marker analysis queue
            if not self.m_stream.analyze.empty():
                print('Began analyzing data...')

                trial_num, ts, marker_end = self.m_stream.remove_analysis()
                self.data_duration = trial_num * self.event_time + self.analysis_time
                tmp = np.array(self.eeg_stream.data)
                # get analysis_time seconds of data (in terms of the end_index) after the event
                end_index = int((np.abs(tmp[:, -1] - ts)).argmin() +
                                self.analysis_time /
                                (1 / self.eeg_stream.info['sfreq']))

                # ensure there is enough eeg data before analyzing; wait if there isn't
                while len(self.eeg_stream.data) < end_index:
                    time.sleep(sleep_time)

                # Make an MNE epoch from channels 0-3 (EEG), decim = keep every nth sample
                epochs, identities, targets = self.eeg_stream.make_epochs(
                    marker_stream=self.m_stream,
                    end_index=end_index,
                    marker_end=marker_end,
                    trial_num=trial_num,
                    data_duration=self.data_duration,
                    picks=[0, 1, 2, 3],
                    tmin=0.0,
                    tmax=1,
                    decim=3)
                # get input to classifier
                print('Formatting data for classifier...')
                data = np.array(epochs.get_data())
                # since the sample frequency is 220 Hz/3 = 73.33 Hz, indexes 8 and 55 is approximately 0.100 - 0.750 s
                data = data[:, :, 8:56]
                print('size of classifier-input: {}'.format(data.shape))
                print('size of identities: {}'.format(identities.shape))
                print('size of targets: {}'.format(targets.shape))

                # If training classifier, send data to classifier with ground truth targets
                if self.train:
                    self.train_number += data.shape[0]
                    if self.train_number < self.train_epochs:
                        self.train_data.extend(data)
                        self.train_targets.extend(targets)
                    else:
                        print('Training ml classifier with {} epochs'.format(
                            self.train_number))
                        package = zip(self.train_targets, self.train_data)
                        if self.get_test:
                            ml.save_test_data(self.test_path, package)
                            print("test set created!")
                            self._kill_signal.set()
                        else:
                            i, t = ml.create_input_target(package)
                            classifier = ml.ml_classifier(i, t)
                            print("Finished training.")
                            ml.save(self.classifier_path, classifier)
                            self.train_number = 0

                            # Get accuracy of classifier based on test set
                            score = classifier.score(self.inputs_test,
                                                     self.targets_test)
                            print('Test Set Accuracy: {}%'.format(score * 100))
                # else do a prediction
                else:
                    classifier = ml.load(self.classifier_path)
                    i, t = ml.create_input_target(zip(targets, data))
                    prediction = ml.predict(i, classifier)
                    intermediate = 0
                    for index, item in enumerate(prediction):
                        # To account for the fact that every marker is associated with 4 channels, average the output
                        # of each channel (or apply specific weights to each channel, to possibly implement in future).
                        # Predictions for a single event based on 4 channels is appended to a list.
                        if (index + 1) % 4 == 0:
                            intermediate += item / 4
                            self.predictions.append(intermediate)
                            intermediate = 0
                        else:
                            intermediate += item / 4
                        #TODO: create method to return a predictions with events
            time.sleep(sleep_time)
예제 #4
0
파일: test_ml.py 프로젝트: nealith/IA_Fonts
from ml import train, save, load, predict

#model = train('deep_fonts/data.csv')
#save(model,'test_model')

model = load('test_model')
nothing = 0
predict(model, 'test.jpg', nothing)
예제 #5
0
または
python3 tests/eval_mlp3d.py dropout
と実行
"""

import ml, numpy as np, sys

(X_train, T_train), (X_test, T_test) = ml.load_data()

fnames = {
    'no_dropout' : 'pkl/mlp3d.pkl',
    'dropout'    : 'pkl/mlp3d_dropout_weight_decay.pkl'
}

which = fnames[sys.argv[1]]
net = ml.load(which)

net(X_train[:10000]) 
X = net[-2].z.copy() 
labels = T_train[:10000]
 
ev_train = ml.evaluate(ml.cluster.as_cluster(X, labels))
print(ev_train)
ml.save(ev_train, f'pkl/eval_mlp3d_{sys.argv[1]}_train')

net(X_test) 
X = net[-2].z.copy() 
labels = T_test

ev_test = ml.evaluate(ml.cluster.as_cluster(X, labels))
print(ev_test)
예제 #6
0
"""test_ae.pyで得られたオートエンコーダにより次元圧縮をおこない、それを用いてMLPを学習させてみる"""

import numpy as np
import ml

(X_train, T_train), (X_test, T_test) = ml.load_data('mnist')

ae = ml.load('pkl/ae')
ae.forward_prop(X_train)
X_train_encoded = ae[3].z.copy()
ae.forward_prop(X_test)
X_test_encoded = ae[3].z.copy()

net = ml.nn.mlp_classifier.from_shape([32, 20, 10])

net.train(X_train=X_train_encoded[:-6000],
          T_train=T_train[:-6000],
          X_val=X_train_encoded[-6000:],
          T_val=T_train[-6000:],
          eta0=1e-2,
          optimizer='SGD',
          tol=0)