def get_data(args, dataset, split="train"): """ Adapted from original multimodal transformer code """ alignment = "a" if args.aligned else "na" data_path = os.path.join(args.data_path, dataset) + f"_{split}_{alignment}.dt" if not os.path.exists(data_path): logger.info(f" - Creating new {split} data") data = DataSets(args.data_path, dataset, split, args.aligned) torch.save(data, data_path) else: logger.info(f" - Found cached {split} data") data = torch.load(data_path) return data
def __init__(self, img_id, settings): self.settings = settings self.datasets = DataSets(img_id, settings) self.classifier = None
class PredictionModule(object): #------------------------------------------------------------------- # Main constructor of PredictionModule #------------------------------------------------------------------- def __init__(self, img_id, settings): self.settings = settings self.datasets = DataSets(img_id, settings) self.classifier = None def report(self, name, dims): msg = '%s' % (name) status = '[' p_dim = None for dim in dims: if p_dim == None: status = '%s%s' % (status, dim) else: status = '%s x %s' % (status, dim) p_dim = dim status = '%s]' % status Utility.report_status(msg, status) #------------------------------------------------------------------- # Main constructor of MembraneModel # Initialize vari #------------------------------------------------------------------- def predict(self): Utility.report_status('starting prediction', '') msg = 'loading prediction data' if not self.datasets.load_test(): Utility.report_status(msg, 'fail') return False Utility.report_status(msg, 'done') self.report( 'test set x', \ self.datasets.test_set_x.shape.eval()) self.report( 'test set y', \ self.datasets.test_set_y.shape.eval()) path = '%s/best_model.pkl' % (self.settings.params) msg = 'loading best model' if not os.path.exists(path): Utility.report_status(msg, 'fail') return False n_hidden = self.settings.n_hidden n_classes = self.datasets.n_classes n_features = self.datasets.n_features w_val = np.random.normal(0, 0.1, (n_features, n_hidden)) b_val = np.zeros(n_hidden) w = theano.shared(value=w_val, name='W', borrow=True) b = theano.shared(value=b_val, name='b', borrow=True) save_file = open(path) w_val, b_val = cPickle.load(save_file) w.set_value(w_val) #cPickle.load(save_file), borrow=True) b.set_value(b_val) #cPickle.load(save_file), borrow=True) print 'w:', w.shape.eval() print 'b:', b.shape.eval() Utility.report_status(msg, 'done') test_set_x = self.datasets.test_set_x test_set_y = self.datasets.test_set_y rng = np.random.RandomState(42) # Step 1. Declare Theano variables x = T.fmatrix() y = T.ivector() index = T.iscalar() path = '%s/%s' % (self.settings.models, 'best_model_mlp.pkl') print 'loading %' % (path) classifier = MLP_Ex(path) if True: return True classifier = MLP(rng=rng, input=x, n_in=n_features, n_hidden=n_hidden, n_out=n_classes) classifier.params = [] classifier.params.extend([w, b]) predict_model = theano.function( inputs=[index], outputs=classifier.logRegressionLayer.y_pred, givens={ x: test_set_x, y: test_set_y }, on_unused_input='ignore') predicted_values = predict_model(1) print 'size predicted:', len(predicted_values) msg = 'image segmentation' Utility.report_status(msg, 'done') return True
def main(): start_time = time.time() # clocking start # Dataset dataset = DataSets(height=64, width=64, channel=3, ds_path='D:/DataSets/pix2pix/', ds_name="vangogh2photo") config = tf.ConfigProto() config.gpu_options.allow_growth = True with tf.Session(config=config) as s: # DiscoGAN model model = discogan.DiscoGAN(s) # load model & graph & weight global_step = 0 ckpt = tf.train.get_checkpoint_state('./model/') if ckpt and ckpt.model_checkpoint_path: # Restores from checkpoint model.saver.restore(s, ckpt.model_checkpoint_path) global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1] print("[+] global step : %s" % global_step, " successfully loaded") else: print('[-] No checkpoint file found') # initializing variables tf.global_variables_initializer().run() d_overpowered = False # G loss > D loss * 2 for epoch in range(paras['epoch']): for step in range(1000): offsetA = (step * paras['batch_size']) % (dataset.img_A.shape[0] - paras['batch_size']) offsetB = (step * paras['batch_size']) % (dataset.img_B.shape[0] - paras['batch_size']) # batch data set batch_A = dataset.img_A[offsetA:(offsetA + paras['batch_size']), :] batch_B = dataset.img_B[offsetB:(offsetB + paras['batch_size']), :] # update D network if not d_overpowered: s.run(model.d_op, feed_dict={model.A: batch_A}) # update G network s.run(model.g_op, feed_dict={model.B: batch_B}) if epoch % paras['logging_interval'] == 0: d_loss, g_loss, summary = s.run([ model.d_loss, model.g_loss, model.merged ], feed_dict={ model.A: batch_A, model.B: batch_B }) # print loss print("[+] Epoch %03d Step %04d => " % (epoch, global_step), " D loss : {:.8f}".format(d_loss), " G loss : {:.8f}".format(g_loss)) # update overpowered d_overpowered = d_loss < g_loss / 2 # training G model with sample image and noise AB_samples = s.run(model.G_s2b, feed_dict={model.A: batch_A}) BA_samples = s.run(model.G_b2s, feed_dict={model.B: batch_B}) # summary saver model.writer.add_summary(summary, global_step=global_step) # export image generated by model G sample_image_height = model.sample_size sample_image_width = model.sample_size sample_AB_dir = results['sample_output'] + 'train_A_{0}_{1}.png'.format(epoch, global_step) sample_BA_dir = results['sample_output'] + 'train_B_{0}_{1}.png'.format(epoch, global_step) # Generated image save iu.save_images(AB_samples, size=[sample_image_height, sample_image_width], image_path=sample_AB_dir) iu.save_images(BA_samples, size=[sample_image_height, sample_image_width], image_path=sample_BA_dir) # model save model.saver.save(s, results['model'], global_step=global_step) end_time = time.time() - start_time # elapsed time print("[+] Elapsed time {:.8f}s".format(end_time)) # close tf.Session s.close()
def __init__(self): super().__init__() self.datasets = DataSets() self._max_recent = 6 # maximum number of recent files self.history = [] # command history settings = self._read_settings() self.recent = settings["recent"] if settings["recent"] else [] self.setGeometry(300, 300, 800, 600) self.setWindowTitle("MNELAB") menubar = self.menuBar() file_menu = menubar.addMenu("&File") file_menu.addAction("&Open...", self.open_file, QKeySequence.Open) self.recent_menu = file_menu.addMenu("Open Recent") self.recent_menu.aboutToShow.connect(self._update_recent_menu) self.recent_menu.triggered.connect(self._load_recent) if not self.recent: self.recent_menu.setEnabled(False) self.close_file_action = file_menu.addAction("&Close", self.close_file, QKeySequence.Close) file_menu.addSeparator() file_menu.addAction("&Quit", self.close, QKeySequence.Quit) plot_menu = menubar.addMenu("&Plot") self.plot_raw_action = plot_menu.addAction("&Raw data", self.plot_raw) tools_menu = menubar.addMenu("&Tools") self.filter_action = tools_menu.addAction("&Filter data...", self.filter_data) self.run_ica_action = tools_menu.addAction("&Run ICA...") self.import_ica_action = tools_menu.addAction("&Load ICA...", self.load_ica) view_menu = menubar.addMenu("&View") view_menu.addAction("Show/hide statusbar", self._toggle_statusbar) help_menu = menubar.addMenu("&Help") help_menu.addAction("&About", self.show_about) help_menu.addAction("About &Qt", self.show_about_qt) self.names = QStringListModel() splitter = QSplitter() self.sidebar = QListView() self.sidebar.setFocusPolicy(0) self.sidebar.setFrameStyle(0) self.sidebar.setModel(self.names) self.sidebar.clicked.connect(self._update_data) splitter.addWidget(self.sidebar) self.infowidget = InfoWidget() splitter.addWidget(self.infowidget) width = splitter.size().width() splitter.setSizes((width * 0.25, width * 0.75)) self.setCentralWidget(splitter) self.status_label = QLabel() self.statusBar().addPermanentWidget(self.status_label) if settings["statusbar"]: self.statusBar().show() else: self.statusBar().hide() self._toggle_actions(False) self.show()
class MainWindow(QMainWindow): """MNELAB main window. """ def __init__(self): super().__init__() self.datasets = DataSets() self._max_recent = 6 # maximum number of recent files self.history = [] # command history settings = self._read_settings() self.recent = settings["recent"] if settings["recent"] else [] self.setGeometry(300, 300, 800, 600) self.setWindowTitle("MNELAB") menubar = self.menuBar() file_menu = menubar.addMenu("&File") file_menu.addAction("&Open...", self.open_file, QKeySequence.Open) self.recent_menu = file_menu.addMenu("Open Recent") self.recent_menu.aboutToShow.connect(self._update_recent_menu) self.recent_menu.triggered.connect(self._load_recent) if not self.recent: self.recent_menu.setEnabled(False) self.close_file_action = file_menu.addAction("&Close", self.close_file, QKeySequence.Close) file_menu.addSeparator() file_menu.addAction("&Quit", self.close, QKeySequence.Quit) plot_menu = menubar.addMenu("&Plot") self.plot_raw_action = plot_menu.addAction("&Raw data", self.plot_raw) tools_menu = menubar.addMenu("&Tools") self.filter_action = tools_menu.addAction("&Filter data...", self.filter_data) self.run_ica_action = tools_menu.addAction("&Run ICA...") self.import_ica_action = tools_menu.addAction("&Load ICA...", self.load_ica) view_menu = menubar.addMenu("&View") view_menu.addAction("Show/hide statusbar", self._toggle_statusbar) help_menu = menubar.addMenu("&Help") help_menu.addAction("&About", self.show_about) help_menu.addAction("About &Qt", self.show_about_qt) self.names = QStringListModel() splitter = QSplitter() self.sidebar = QListView() self.sidebar.setFocusPolicy(0) self.sidebar.setFrameStyle(0) self.sidebar.setModel(self.names) self.sidebar.clicked.connect(self._update_data) splitter.addWidget(self.sidebar) self.infowidget = InfoWidget() splitter.addWidget(self.infowidget) width = splitter.size().width() splitter.setSizes((width * 0.25, width * 0.75)) self.setCentralWidget(splitter) self.status_label = QLabel() self.statusBar().addPermanentWidget(self.status_label) if settings["statusbar"]: self.statusBar().show() else: self.statusBar().hide() self._toggle_actions(False) self.show() def open_file(self): """Open file. """ fname = QFileDialog.getOpenFileName(self, "Open file", filter="*.bdf *.edf")[0] if fname: self.load_file(fname) def load_file(self, fname): raw = mne.io.read_raw_edf(fname, stim_channel=None, preload=True) name, _ = splitext(split(fname)[-1]) self.history.append("raw = mne.io.read_raw_edf('{}', " "stim_channel=None, preload=True)".format(fname)) self.datasets.insert_data(DataSet(name=name, fname=fname, raw=raw)) self._update_sidebar() self._update_main() self._add_recent(fname) self._update_statusbar() self._toggle_actions() def close_file(self): """Close current file. """ self.datasets.remove_data() self._update_sidebar() self._update_main() self._update_statusbar() if not self.datasets: self.infowidget.clear() self._toggle_actions(False) self.status_label.clear() def get_info(self): """Get basic information on current file. """ raw = self.datasets.current.raw fname = self.datasets.current.fname nchan = raw.info["nchan"] chans = Counter([channel_type(raw.info, i) for i in range(nchan)]) return {"File name": fname if fname else "-", "Number of channels": raw.info["nchan"], "Channels": ", ".join( [" ".join([str(v), k.upper()]) for k, v in chans.items()]), "Samples": raw.n_times, "Sampling frequency": str(raw.info["sfreq"]) + " Hz", "Length": str(raw.n_times / raw.info["sfreq"]) + " s", "Size in memory": "{:.2f} MB".format( raw._data.nbytes / 1024 ** 2), "Size on disk": "-" if not fname else "{:.2f} MB".format( getsize(fname) / 1024 ** 2)} def plot_raw(self): """Plot raw data. """ events = self.datasets.current.events self.datasets.current.raw.plot(events=events) def load_ica(self): """Load ICA solution from a file. """ fname = QFileDialog.getOpenFileName(self, "Load ICA", filter="*.fif *.fif.gz") if fname[0]: self.state.ica = mne.preprocessing.read_ica(fname[0]) def filter_data(self): dialog = FilterDialog() if dialog.exec_(): low, high = dialog.low, dialog.high self.datasets.current.raw.filter(low, high) self.history.append("raw.filter({}, {})".format(low, high)) if QMessageBox.question(self, "Add new data set", "Store the current signals in a new data " "set?") == QMessageBox.Yes: new = DataSet(name="NEW", fname="", raw=self.datasets.current.raw) self.datasets.insert_data(new) self._update_sidebar() self._update_main() self._update_statusbar() def show_about(self): """Show About dialog. """ QMessageBox.about(self, "About MNELAB", "Licensed under the BSD 3-clause license.\n" "Copyright 2017 by Clemens Brunner.") def show_about_qt(self): """Show About Qt dialog. """ QMessageBox.aboutQt(self, "About Qt") def _update_sidebar(self): self.names.setStringList(self.datasets.names) self.sidebar.setCurrentIndex(self.names.index(self.datasets.index)) def _update_main(self): if self.datasets: self.infowidget.set_values(self.get_info()) else: self.infowidget.clear() def _update_statusbar(self): if self.datasets: mb = self.datasets.nbytes / 1024 ** 2 self.status_label.setText("Total Memory: {:.2f} MB".format(mb)) else: self.status_label.clear() def _toggle_actions(self, enabled=True): """Toggle actions. """ self.close_file_action.setEnabled(enabled) self.plot_raw_action.setEnabled(enabled) self.filter_action.setEnabled(enabled) self.run_ica_action.setEnabled(enabled) self.import_ica_action.setEnabled(enabled) def _add_recent(self, fname): if fname in self.recent: # avoid duplicates self.recent.remove(fname) self.recent.insert(0, fname) while len(self.recent) > self._max_recent: # prune list self.recent.pop() self._write_settings() if not self.recent_menu.isEnabled(): self.recent_menu.setEnabled(True) def _write_settings(self): settings = QSettings() if self.recent: settings.setValue("recent", self.recent) settings.setValue("statusbar", not self.statusBar().isHidden()) def _read_settings(self): settings = QSettings() recent = settings.value("recent") statusbar = settings.value("statusbar") if (statusbar is None) or (statusbar == "true"): statusbar = True else: statusbar = False return {"recent": recent, "statusbar": statusbar} @pyqtSlot(QModelIndex) def _update_data(self, selected): """Update index and information based on the state of the sidebar. """ if selected.row() != self.datasets.index: self.datasets.index = selected.row() self.datasets.update_current() self._update_main() @pyqtSlot() def _update_recent_menu(self): self.recent_menu.clear() for recent in self.recent: self.recent_menu.addAction(recent) @pyqtSlot(QAction) def _load_recent(self, action): self.load_file(action.text()) @pyqtSlot() def _toggle_statusbar(self): if self.statusBar().isHidden(): self.statusBar().show() else: self.statusBar().hide() self._write_settings() def closeEvent(self, event): print("\nCommand History") print("===============") print("\n".join(self.history)) event.accept()
def __init__(self, classifier, projectId): Task.__init__(self, classifier, projectId, 'training', 2) self.dataset = DataSets(projectId)
class TrainingTask(Task): #------------------------------------------------------------------- # Reads the input image and normalize it. Also creates # a version of the input image with corner pixels # mirrored based on the sample size # arguments: - path : directory path where image is located # - id : the unique identifier of the image # - pad : the amount to pad the image by. # return : returns original image and a padded version #------------------------------------------------------------------- def __init__(self, classifier, projectId): Task.__init__(self, classifier, projectId, 'training', 2) self.dataset = DataSets(projectId) #------------------------------------------------------------------- # Reads the input image and normalize it. Also creates # a version of the input image with corner pixels # mirrored based on the sample size # arguments: - path : directory path where image is located # - id : the unique identifier of the image # - pad : the amount to pad the image by. # return : returns original image and a padded version #------------------------------------------------------------------- def work(self): print 'training......running....' project = Database.getProject(self.projectId) sample_size = project['sample_size'] n_hidden = [h['units'] for h in project["hidden_layers"]] learning_rate = project['learning_rate'] momentum = project['momentum'] batch_size = project['batch_size'] epochs = project['epochs'] print '---------------------' print n_hidden print '---------------------' # no training until all labels are annotated labels = Database.getLabels(self.projectId) # must have training and hidden layer units to train if len(labels) == 0 or len(project['hidden_layers']) == 0: print 'no labels or hidden layers....' return # check for new data #new_data = Database.hasTrainingTasks( self.projectId ) or not self.dataset.valid() #if new_data: new_data = self.dataset.load_training(sample_size) # cache the dataset if not self.dataset.valid(): print 'invalid data....' return # train the classifier self.classifier.train(self.dataset.x, self.dataset.y, self.dataset.p, new_data, sample_size**2, len(labels), n_hidden, learning_rate, momentum, batch_size, epochs) # save statistics self.dataset.save_stats()
180, 240, 300, 360, 420, 480, 540, 600, 660, 720, 780, 840, 900, 960, 1020, 1080, 1140, ], # main "num_epochs": 1201, # datasets "dataset_builder": lambda batch_size, device: DataSets.load_csv( batch_size, device, "datasets/flights_filtered.csv"), "emd_test": False, "goal2_test": False, }
def get_data(args, dataset, label_list, split="train"): """ Adapted from original multimodal transformer code """ data_path = os.path.join(args.data_path, f"{split}.pkl") return DataSets(data_path, label_list, dataset, split)
def __init__(self, img_id, settings): self.settings = settings self.datasets = DataSets( img_id, settings ) self.classifier = None
class PredictionModule (object): #------------------------------------------------------------------- # Main constructor of PredictionModule #------------------------------------------------------------------- def __init__(self, img_id, settings): self.settings = settings self.datasets = DataSets( img_id, settings ) self.classifier = None def report(self, name, dims): msg = '%s'%(name) status = '[' p_dim = None for dim in dims: if p_dim == None: status = '%s%s'%(status, dim) else: status = '%s x %s'%(status, dim) p_dim = dim status = '%s]'%status Utility.report_status( msg, status) #------------------------------------------------------------------- # Main constructor of MembraneModel # Initialize vari #------------------------------------------------------------------- def predict(self): Utility.report_status('starting prediction', '') msg = 'loading prediction data' if not self.datasets.load_test(): Utility.report_status( msg, 'fail') return False Utility.report_status( msg, 'done' ) self.report( 'test set x', \ self.datasets.test_set_x.shape.eval()) self.report( 'test set y', \ self.datasets.test_set_y.shape.eval()) path = '%s/best_model.pkl'%(self.settings.params) msg = 'loading best model' if not os.path.exists(path): Utility.report_status( msg, 'fail' ) return False n_hidden = self.settings.n_hidden n_classes = self.datasets.n_classes n_features = self.datasets.n_features w_val = np.random.normal(0, 0.1, (n_features, n_hidden)) b_val = np.zeros( n_hidden ) w = theano.shared(value=w_val, name='W', borrow=True) b = theano.shared(value=b_val, name='b', borrow=True) save_file = open(path) w_val, b_val = cPickle.load(save_file) w.set_value(w_val) #cPickle.load(save_file), borrow=True) b.set_value(b_val) #cPickle.load(save_file), borrow=True) print 'w:', w.shape.eval() print 'b:', b.shape.eval() Utility.report_status( msg, 'done' ) test_set_x = self.datasets.test_set_x test_set_y = self.datasets.test_set_y rng = np.random.RandomState(42) # Step 1. Declare Theano variables x = T.fmatrix() y = T.ivector() index = T.iscalar() path = '%s/%s'%(self.settings.models, 'best_model_mlp.pkl') print 'loading %'%(path) classifier = MLP_Ex( path ) if True: return True classifier = MLP( rng=rng, input=x, n_in=n_features, n_hidden=n_hidden, n_out=n_classes ) classifier.params = [] classifier.params.extend([ w, b ]) predict_model = theano.function( inputs=[index], outputs=classifier.logRegressionLayer.y_pred, givens={ x: test_set_x, y: test_set_y }, on_unused_input='ignore' ) predicted_values = predict_model( 1 ) print 'size predicted:', len( predicted_values ) msg = 'image segmentation' Utility.report_status( msg, 'done' ) return True
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") # If using a generated dataset. if "dataset_builder" not in experiment: # Use coprime numbers to prevent any matching points between train and test. TRAIN_SIZE = 31013 # TRAIN_SIZE = 9611 TEST_SIZE = 5007 # TEST_SIZE = 1001 datasets = DataSets.generated_dataset( experiment=experiment, train_size=TRAIN_SIZE, test_size=TEST_SIZE, batch_size=BATCH_SIZE, device=device, ) # If using a real data dataset. else: datasets = experiment["dataset_builder"](BATCH_SIZE, device) # Create the z-samples set. z_samples = ZSamples( experiment=experiment, device=device, ) # Create the trainable model. model = Model(