def display_page(pathname): if pathname == '/training': return Training() elif pathname == '/eda': return EDA() else: return Homepage()
def main(): args = parser.parse_args() args = vars(args) if args['data_set'] == 'UCF101': print('UCF101 data set') name_list = 'ucfTrainTestlist' num_classes = 101 elif args['data_set'] == 'Breakfast': print("breakfast data set") num_classes = 37 name_list = 'breakfastTrainTestList' else: print('Merl data set') num_classes = 5 name_list = 'merlTrainTestList' if args['test']: Testing(name_list=name_list, num_classes=num_classes, modality='RGB', **args) else: Training(name_list=name_list, num_classes=num_classes, modality='RGB', **args)
def display_page(pathname): if pathname == '/training': return Training() elif pathname == '/training_perm': return Training_perm() elif pathname == '/eda': return EDA() elif pathname == '/eda_perm': return EDA_perm() elif pathname == '/userguide': return userGuide() elif pathname == '/buildmodel': return buildModel() elif pathname == '/h1bmodel': return h1bModel() elif pathname == '/permmodel': return permModel() elif pathname == '/aboutEDA': return aboutEDA() elif pathname == '/contactus': return contactus() elif pathname == '/documents': return documents() else: return Homepage()
from keras.preprocessing.text import Tokenizer from train import Training import sys import argparse # parser = argparse.ArgumentParser(description='Train the word model.') # parser.add_argument('-num_words', dest='n_words', type=str, required=True, help='Number of words to predict.') # parser.add_argument('-load_model', dest='load_model', type=str, required=True, help='Path to save the model.') # args = parser.parse_args() file_path = input("Enter the path of corpus (include file extension): ") model_path = input("Enter the path to your pretrained model (Leave blank if none): ") valid = {"yes": True, "y": True, "ye": True, "no": False, "n": False} model = Sequential() train = Training(str(file_path)) try: model = models.load_model(model_path) except ValueError: isTrain = input("Model not found! Do you want to train a new model? (yes/no): ") if valid[isTrain]: model = train.train_model() print("Training Done!") else: print("Thank you for using this tool.") sys.exit() print() seed_words = input("Enter your input: ") num_words = int(input("Enter number of words to predict: ")) print() result = train.generate_seq(model, seed_words, num_words)
from utils import div_list import tensorflow as tf import numpy as np from train import Training if __name__ == "__main__": # Initial model gcn = Training() # Set random seed seed = 123 np.random.seed(seed) tf.compat.v1.set_random_seed(seed) labels = np.loadtxt("data/adj.txt") reorder = np.arange(labels.shape[0]) np.random.shuffle(reorder) cv_num = 5 order = div_list(reorder.tolist(), cv_num) for i in range(cv_num): print("cross_validation:", '%01d' % (i)) test_arr = order[i] arr = list(set(reorder).difference(set(test_arr))) np.random.shuffle(arr) train_arr = arr scores = gcn.train(train_arr, test_arr)
train_info = pickle.load(fp) with open(main_path_to_data + "/val_info", 'rb') as fp: valid_info = pickle.load(fp) with open(main_path_to_data + "/test_info", 'rb') as fp: test_info = pickle.load(fp) # Nastavimo hiperparametre v slovarju hyperparameters = {} hyperparameters['learning_rate'] = 0.2e-3 # learning rate hyperparameters['weight_decay'] = 0.0001 # weight decay hyperparameters['total_epoch'] = 20 # total number of epochs hyperparameters[ 'multiplicator'] = 0.95 # each epoch learning rate is decreased on LR*multiplicator # Ustvarimo ucni in testni razred TrainClass = Training(main_path_to_data, unique_name=unique_name_for_this_run) TestClass = Testing(main_path_to_data) # Naucimo model za izbrane hiperparametre aucs, losses, path_to_model = TrainClass.train(train_info, valid_info, hyperparameters) # Najboljsi model glede na validacijsko mnozico (zadnji je /LAST_model.pth) best_model = path_to_model + '/BEST_model.pth' # Testiramo nas model na testni mnozici auc, fpr, tpr, thresholds, trues, predictions = TestClass.test( test_info, best_model) print("Test set AUC result: ", auc)
def objective(trial: optuna.trial.Trial): """ Optuna objective function. """ # Parse CLI arguments. args = parse_cli_args() config = HyperparamConfig() # Prepare dataset class. train_ds = Dataset(args.train_path) valid_ds = Dataset(args.valid_path) # Suggestions. # >>> TODO >>> optimizer_name = trial.suggest_categorical("optimizer", ["Adam", "RMSProp", "SGD"]) learning_rate = trial.suggest_float("learning_rate", low=1e-6, hight=1e-1, log=True) n_layer = trial.suggest_int("n_layer", 32, 1024, step=32) # <<< TODO <<< model = make_model(n_layer=n_layer) optimizer = getattr(optim, optimizer_name)(model.parameters(), lr=learning_rate) training = Training( train_ds, valid_ds, model, optimizer, config.batch_size, config.epochs, ) accuracy_list = [] with mlflow.start_run(run_name=study.study_name): mlflow.log_params(trial.params) for epoch in range(config.epochs): # Training phase. training.model.train() train_loss, train_accuracy = training.train_epoch() mlflow.log_metrics( { "train_loss": train_loss, "train_accuracy": train_accuracy }, step=epoch + 1, ) # Validation phase. training.model.eval() valid_loss, valid_accuracy = training.validate() mlflow.log_metrics( { "valid_loss": valid_loss, "valid_accuracy": valid_accuracy }, step=epoch + 1, ) accuracy_list.append(valid_accuracy.item()) trial.report(valid_accuracy.item(), epoch) if trial.should_prune(): print("Pruned with epoch {}".format(epoch)) raise optuna.exceptions.TrialPruned() print( "Epoch {}: TrainLoss: {:.5f}, ValidLoss: {:.5f}, ValidAcc: {:.5f}" .format(epoch + 1, train_loss, valid_loss, valid_accuracy)) accuracy_list.sort() accuracy = np.mean(accuracy_list[-10:]) mlflow.log_metrics({"top10_avg_accuracy": accuracy}) return accuracy
import pickle as pkl import sys from multiprocessing import Value import numpy as np from train import Training, parse_params p = pkl.load(open(sys.argv[1] + "/model.pkl", "rb")) args = pkl.load(open(sys.argv[1] + "/params.pkl", "rb")) temp_p = parse_params() for a in args.__dict__: setattr(temp_p, a, args.__dict__[a]) args = temp_p print args print args.testing = True setattr(args, "init_num_moves", 2) args.fps = 60 t = Training(np.random.RandomState(), 0, p, Value("i", 0, lock=False), args)
nargs=1, action="store", default=0, dest="limit", help= "Limit genes in all datasets, it speeds up on data pre-processing development." ) parser.add_argument("-t", "--train", action="store_true", dest="train", help="When the flag is activated, it performs training.") parser.add_argument( "-e", "--evaluate", action="store_true", dest="evaluate", help="When the flag is activated, it predicts test classes.") args = parser.parse_args() if args.data_proc: dp = DataPreprocess("data", [2, 4, 6, 8, 10, 12, 15, 20, 25, 30], int(args.limit[0])) if args.train: tr = Training([2, 4, 6, 8, 10, 12, 15, 20, 25, 30]) if args.evaluate: evaluate()
else: loss_function = nn.CrossEntropyLoss() train_name += '_entp' # Validation loss val_loss = DiceLoss() # Set logs folder logger = Logger('../logs/' + train_name + '/') # Run training training = Training(model, device, dataset_train, dataset_val, optmizer, loss_function, eval_loss=val_loss, target=target, train_with_targets=train_with_targets, logger=logger, train_name=train_name, arch=net_type) training.train(epochs=n_epochs, batch_size=batch_size) print('------------- END OF TRAINING -------------') print(' ') # Test network model print('Testing') print('') weights_path = '../weights/' + train_name + '_weights.pth.tar' # Output folder out_folder = '../predictions/' + train_name + '/'
from predict import Predict from rabbitmq.publisher import Publisher from rabbitmq.consumer import Consumer import json import os from mongodb.dao import MongoDBDAO import logging logging.basicConfig(level=logging.INFO) from analytics.chatbasepublisher import ChatBasePublisher publisher = Publisher() chatbasepublisher = ChatBasePublisher() ERROR_THRESHOLD = 0.70 training = Training() predict = Predict(ERROR_THRESHOLD) def processTraining(): mongoDBDAO = MongoDBDAO() kbItems = mongoDBDAO.selectAllKBItems() kbArray = json.loads(kbItems) print("KB items",kbArray['kbItems']) if len(kbArray['kbItems']) > 0: training.train(kbItems) predict.loadModel() print("[INFO] Training Completed") else: print("[INFO] No KB to train")
from train import Training if __name__ == "__main__": gcn = Training() gcn.train(train_data,test_data)
class TrainTab(ttk.Frame): def __init__(self, *arg, **kw): super(TrainTab, self).__init__(*arg, **kw) self.pack(fill='x') # Stage 5 (sub_train -> fd1 - fd2 - trainButton) self.label1 = ttk.Label( self, text="Select folder containing training images") self.label1.pack(anchor='w') self.fd1 = FileDialog(self, mode='folder') self.fd1.filenameText.set('data/train') self.fd1.pack(fill='both', anchor='w') self.empty_space1 = ttk.Label(self, text="") self.empty_space1.pack() self.label2 = ttk.Label(self, text="Select folder containing testing images") self.label2.pack(anchor='w') self.fd2 = FileDialog(self, mode='folder') self.fd2.filenameText.set('data/test') self.fd2.pack(fill='both', anchor='w') self.empty_space2 = ttk.Label(self, text="") self.empty_space2.pack() self.trainButton = ttk.Button(self, text="Train!", padding=PADDING, command=self.train_model) self.trainButton.pack(fill='both') def train_model(self): nbof_epochs = 30 batch_size = 64 nbof_images = 2000 nbof_validation_images = 1000 nbof_steps = nbof_images // batch_size nbof_validation_steps = nbof_validation_images // batch_size # TODO: check validity of training and testing folders # - same number of classes # TODO: save class names # TODO: display training infos # "Training..." label self.label_progress_bar_steps = ttk.Label( self, text="Training...\nStep {:d}/{:d}".format(0, nbof_steps)) self.label_progress_bar_steps.pack(anchor='w') # Create a progress bar for steps self.progress_bar_steps = ttk.Progressbar(self, orient=HORIZONTAL, maximum=nbof_steps, mode='determinate') self.progress_bar_steps.pack(fill='both') # Epoch label self.label_progress_bar_epochs = ttk.Label( self, text="Epoch {:d}/{:d}".format(0, nbof_epochs)) self.label_progress_bar_epochs.pack(anchor='w') # Create a progress bar for epochs self.progress_bar_epochs = ttk.Progressbar(self, orient=HORIZONTAL, maximum=nbof_epochs, mode='determinate') self.progress_bar_epochs.pack(fill='both') # Create display for training info self.display_train_info = ttk.Label( self, font=("Courier", 10), text="{:<20}{:<20}{:<20}{:<20}\n{:<20.4f}{:<20.4f}{:<20.4f}{:<20.4f}" .format("Training Loss", "Training Accuracy", "Testing Loss", "Testing Accuracy", 0, 0, 0, 0)) self.display_train_info.pack(fill='both') # Update the frame self.update() # Initiate the training self.train = Training(self.fd1.filenameText.get(), self.fd2.filenameText.get(), batch_size) train_loss = 0 train_acc = 0 test_loss = 0 test_acc = 0 def update_display(train_loss, train_acc, test_loss, test_acc): self.display_train_info[ 'text'] = "{:<20}{:<20}{:<20}{:<20}\n{:<20.4f}{:<20.4f}{:<20.4f}{:<20.4f}".format( "Training Loss", "Training Accuracy", "Testing Loss", "Testing Accuracy", train_loss, train_acc, test_loss, test_acc) # Run the training for epoch in range(nbof_epochs): # Train on one epoch self.label_progress_bar_steps[ 'text'] = "Training...\nStep {:d}/{:d}".format(0, nbof_steps) self.progress_bar_steps['value'] = 0 self.progress_bar_steps['maximum'] = nbof_steps self.progress_bar_steps.update() for step in range(nbof_steps): train_loss, train_acc = self.train.train_on_batch() self.label_progress_bar_steps[ 'text'] = "Training...\nStep {:d}/{:d}".format( step + 1, nbof_steps) self.progress_bar_steps['value'] = step + 1 update_display(train_loss, train_acc, test_loss, test_acc) self.progress_bar_steps.update() # Validate self.label_progress_bar_steps[ 'text'] = "Validation...\nStep {:d}/{:d}".format( 0, nbof_validation_steps) self.progress_bar_steps['value'] = 0 self.progress_bar_steps['maximum'] = nbof_validation_steps self.progress_bar_steps.update() test_loss = 0 test_acc = 0 for step in range(nbof_validation_steps): crt_test_loss, crt_test_acc = self.train.test_on_batch() test_loss, test_acc = test_loss + crt_test_loss, test_acc + crt_test_acc self.label_progress_bar_steps[ 'text'] = "Validation...\nStep {:d}/{:d}".format( step + 1, nbof_validation_steps) self.progress_bar_steps['value'] = step + 1 self.progress_bar_steps.update() test_loss /= nbof_validation_steps test_acc /= nbof_validation_steps update_display(train_loss, train_acc, test_loss, test_acc) self.label_progress_bar_epochs['text'] = "Epoch {:d}/{:d}".format( epoch + 1, nbof_epochs) self.progress_bar_epochs['value'] = epoch + 1 self.progress_bar_epochs.update() self.train.save_model(model_name='models/two_try.h5') self.label_end = ttk.Label( self, text= "Training done! The model can now be tested in the \"Test\" tab.") self.label_end.pack(anchor='w')
def train_model(self): nbof_epochs = 30 batch_size = 64 nbof_images = 2000 nbof_validation_images = 1000 nbof_steps = nbof_images // batch_size nbof_validation_steps = nbof_validation_images // batch_size # TODO: check validity of training and testing folders # - same number of classes # TODO: save class names # TODO: display training infos # "Training..." label self.label_progress_bar_steps = ttk.Label( self, text="Training...\nStep {:d}/{:d}".format(0, nbof_steps)) self.label_progress_bar_steps.pack(anchor='w') # Create a progress bar for steps self.progress_bar_steps = ttk.Progressbar(self, orient=HORIZONTAL, maximum=nbof_steps, mode='determinate') self.progress_bar_steps.pack(fill='both') # Epoch label self.label_progress_bar_epochs = ttk.Label( self, text="Epoch {:d}/{:d}".format(0, nbof_epochs)) self.label_progress_bar_epochs.pack(anchor='w') # Create a progress bar for epochs self.progress_bar_epochs = ttk.Progressbar(self, orient=HORIZONTAL, maximum=nbof_epochs, mode='determinate') self.progress_bar_epochs.pack(fill='both') # Create display for training info self.display_train_info = ttk.Label( self, font=("Courier", 10), text="{:<20}{:<20}{:<20}{:<20}\n{:<20.4f}{:<20.4f}{:<20.4f}{:<20.4f}" .format("Training Loss", "Training Accuracy", "Testing Loss", "Testing Accuracy", 0, 0, 0, 0)) self.display_train_info.pack(fill='both') # Update the frame self.update() # Initiate the training self.train = Training(self.fd1.filenameText.get(), self.fd2.filenameText.get(), batch_size) train_loss = 0 train_acc = 0 test_loss = 0 test_acc = 0 def update_display(train_loss, train_acc, test_loss, test_acc): self.display_train_info[ 'text'] = "{:<20}{:<20}{:<20}{:<20}\n{:<20.4f}{:<20.4f}{:<20.4f}{:<20.4f}".format( "Training Loss", "Training Accuracy", "Testing Loss", "Testing Accuracy", train_loss, train_acc, test_loss, test_acc) # Run the training for epoch in range(nbof_epochs): # Train on one epoch self.label_progress_bar_steps[ 'text'] = "Training...\nStep {:d}/{:d}".format(0, nbof_steps) self.progress_bar_steps['value'] = 0 self.progress_bar_steps['maximum'] = nbof_steps self.progress_bar_steps.update() for step in range(nbof_steps): train_loss, train_acc = self.train.train_on_batch() self.label_progress_bar_steps[ 'text'] = "Training...\nStep {:d}/{:d}".format( step + 1, nbof_steps) self.progress_bar_steps['value'] = step + 1 update_display(train_loss, train_acc, test_loss, test_acc) self.progress_bar_steps.update() # Validate self.label_progress_bar_steps[ 'text'] = "Validation...\nStep {:d}/{:d}".format( 0, nbof_validation_steps) self.progress_bar_steps['value'] = 0 self.progress_bar_steps['maximum'] = nbof_validation_steps self.progress_bar_steps.update() test_loss = 0 test_acc = 0 for step in range(nbof_validation_steps): crt_test_loss, crt_test_acc = self.train.test_on_batch() test_loss, test_acc = test_loss + crt_test_loss, test_acc + crt_test_acc self.label_progress_bar_steps[ 'text'] = "Validation...\nStep {:d}/{:d}".format( step + 1, nbof_validation_steps) self.progress_bar_steps['value'] = step + 1 self.progress_bar_steps.update() test_loss /= nbof_validation_steps test_acc /= nbof_validation_steps update_display(train_loss, train_acc, test_loss, test_acc) self.label_progress_bar_epochs['text'] = "Epoch {:d}/{:d}".format( epoch + 1, nbof_epochs) self.progress_bar_epochs['value'] = epoch + 1 self.progress_bar_epochs.update() self.train.save_model(model_name='models/two_try.h5') self.label_end = ttk.Label( self, text= "Training done! The model can now be tested in the \"Test\" tab.") self.label_end.pack(anchor='w')