def run_model(): mode = params.get('mode') if not mode: logger.warning( 'Не указан режим работы приложения! ключ --mode [load, prepare, train, predict, all] ' ) logger.info('Пример: python run_model.py --mode train ') logger.info('По умолчанию режим: predict ') mode = 'predict' logger.info(f'Приложение запущено в режиме: { mode } ') if mode == 'predict': predict() elif mode == 'prepare': prepare() elif mode == 'load': load() elif mode == 'train': train() elif mode == 'all': load() prepare() train() predict() else: logger.warning(f'Режим MODE = {mode} не распознан!') logger.info(f'Приложение успешно завершено в режиме: {mode} ')
def train_eval_model(args): dataset_name = args[0] model_name = args[1] model = models.module_from_name(model_name) param_dict = args[2] current_gpu = args[3] dir_name = os.path.join(FLAGS.models_dir, dataset_name) param_dict['batch_size'] = 128 param_dict['n_draws'] = 1 hps = models.params.HParams(**param_dict) if model_name == 'madry': madry.Model.maybe_download_and_extract(FLAGS.models_dir) else: print("Running on GPU {}\n\t{}".format(current_gpu, hps)) with tf.Graph().as_default(): train(hps, model, dataset=dataset_name, dir_name=dir_name, dev='/gpu:{}'.format(current_gpu)) compute_robustness = True if model_name == 'madry': compute_robustness = False param_dict['batch_size'] = 2000 param_dict['n_draws'] = 1 elif param_dict['noise_after_n_layers'] < 0: compute_robustness = False param_dict['batch_size'] = 100 param_dict['n_draws'] = 1 else: param_dict['batch_size'] = 1 param_dict['n_draws'] = 2000 hps = models.params.HParams(**param_dict) with tf.Graph().as_default(): evaluate(hps, model, dataset=dataset_name, dir_name=dir_name, compute_robustness=compute_robustness, dev='/gpu:{}'.format(current_gpu)) return hps, model_name
def main(_): config = Config(FLAGS) config.print_config() config.make_dirs() config_proto = tf.ConfigProto(allow_soft_placement=FLAGS.is_train, log_device_placement=False) config_proto.gpu_options.allow_growth = True with tf.Session(config=config_proto) as sess: model = MemoryGAN(config) if not FLAGS.is_train: test_generation(model, sess) else: train(model, sess)
def main(_): pp.pprint(flags.FLAGS.__flags) config = Config(FLAGS) config.print_config() config.make_dirs() config_proto = tf.ConfigProto(allow_soft_placement=FLAGS.is_train, log_device_placement=False) config_proto.gpu_options.allow_growth = True with tf.Session(config=config_proto) as sess: model = globals()[FLAGS.model](config) if not FLAGS.is_train: if not FLAGS.gen_exp: test_generation(model, sess) else: generalization_examples(model, sess) else: train(model, sess)
def cv(model, data, target, temperature, weight_decay, learning_rate, sparsity, sparsity_penalty, n_epochs, n_splits, seed, batch_size, shuffle): cv = KFold(n_splits=n_splits, shuffle=shuffle, random_state=seed) train_losses = [] test_losses = [] for cv_iter, (train_index, test_index) in enumerate(cv.split(data, target)): print('I: [cross_val_train] cv_iter = {:2}'.format(cv_iter)) model.apply(reset_weights) # reset model's weights, recursively train_data = data[train_index] train_target = target[train_index] train_dataloader = torch.utils.data.DataLoader(ZipDataset( train_data[:, :-1], train_target[:, 1:]), batch_size=batch_size, shuffle=shuffle) test_data = data[test_index] test_target = target[test_index] test_dataloader = torch.utils.data.DataLoader(ZipDataset( test_data[:, :-1], test_target[:, 1:]), batch_size=batch_size, shuffle=shuffle) cv_train_loss, cv_test_loss = train(model, train_dataloader, temperature=temperature, weight_decay=weight_decay, learning_rate=learning_rate, sparsity=sparsity, sparsity_penalty=sparsity_penalty, n_epochs=n_epochs, test_dataset=test_dataloader) # plot_loss(cv_train_loss, 'cv_train_loss:cv_iter_{}'.format(cv_iter)) # save(cv_train_loss, 'cv_train_loss:cv_iter_{}'.format(cv_iter)) train_losses.append(cv_train_loss) test_losses.append(cv_test_loss) mean = np.mean(test_losses[-1]) var = np.var(test_losses[-1]) save_txt(train_losses, 'train_loss') save_txt(test_losses, 'test_loss') print('I: [cross_val_train] mean of test losses = {:2}'.format(mean)) print('I: [cross_val_train] variance of test losses = {:2}'.format(var)) return mean
def test_train_dqn_delay(self): # Read train.py arguments from train.config file. train_config = configparser.ConfigParser() cfg_path = os.path.join(CONFIG_PATH, 'test_train_dqn_delay.config') train_config.read(cfg_path) path = train(cfg_path) # Load train_log.json data. with open(path + '/logs/train_log.json') as f: json_data_1 = json.load(f) # Load expected output. with open(OUTPUTS_PATH + '/test_train_dqn_delay.json') as f: json_data_2 = json.load(f) self.assertEqual(json_data_1, json_data_2)
def train_recognition(_argv): """ Train an image classifier. The images should be saved each in a folder with name the class labels. It will save a dictionary that maps each output neuron to the corresponding label as a pickle file named 'neuron_to_class_dict.p'. Divides de training set into train and validation sets and uses the test set to analyze the accuracy of the model. It saves the parameters passed to train the model in a file named 'train_info.txt' in the checkpoints directory. For the moment it has been checked only with the GTSR dataset. """ img_res = FLAGS.img_res gpu_aval = tf.test.is_gpu_available(cuda_only=True) model = Recognizer(FLAGS.dataset_name, img_res=img_res, train=True, model_name=FLAGS.model_name) gpus = 0 if gpu_aval: for x in device_lib.list_local_devices(): if x.device_type == "GPU": gpus += 1 print(constants.C_WARNING, "Are CUDA gpus available? \n\t-", (constants.C_OKBLUE + ('Yes, ' + str(gpus)) if gpu_aval else constants.C_FAIL + 'No.'), constants.C_ENDC) batch_size = FLAGS.batch_size optimizer = tf.keras.optimizers.Adam(lr=FLAGS.lr) if gpus > 1: strategy = tf.distribute.MirroredStrategy() # Here the batch size scales up by number of workers since # `tf.data.Dataset.batch` expects the global batch size. Previously we used 'batch_size', # and now this is multiplied by the number of workers. batch_size *= gpus with strategy.scope(): model.train_model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy']) else: model.train_model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy']) model.train_model.summary() train_info = FLAGS.extra.replace('\\n', '\n') + '\n' train_info += "Train model: {} For {} epochs and {} as Database. \nParameters used:\n " \ "- Frequency = {}\n " \ "- Batch size = {}\n " \ "- Image resolution = {}\n".format(model.model_description, FLAGS.epochs, FLAGS.dataset_name, FLAGS.save_freq, FLAGS.batch_size, FLAGS.img_res) print(constants.C_WARNING, FLAGS.extra.replace('\\n', '\n'), constants.C_ENDC) if model.dataset_name != 'FAKE': ds_directory = constants.DATASET_PATH.format(FLAGS.dataset_name) train_directory = ds_directory + "train" if FLAGS.simple_aug: generator = tf.keras.preprocessing.image.ImageDataGenerator( rescale=1. / 255.0, validation_split=0.1, rotation_range=20, height_shift_range=0.2, width_shift_range=0.2, zoom_range=0.3, shear_range=0.3, brightness_range=(0.2, 0.8) ) train_generator = generator.flow_from_directory( train_directory, target_size=(img_res, img_res), batch_size=batch_size, class_mode='categorical', subset='training' ) val_generator = generator.flow_from_directory( train_directory, target_size=(img_res, img_res), batch_size=batch_size, class_mode='categorical', subset='validation' ) class_to_neuron_dict = train_generator.class_indices else: generator = Generator(directory=train_directory, batch_size=FLAGS.batch_size, image_dimensions=(img_res, img_res), validation_split=0.1) train_generator = generator.train_generator val_generator = generator.val_generator class_to_neuron_dict = generator.class_indices neuron_to_class_dict = {} labels_dict = helpers.get_labels_dict(model.dataset_name) neuron_labels = [] for class_id, neuron in class_to_neuron_dict.items(): neuron_to_class_dict[str(neuron)] = str(class_id) neuron_labels.append(labels_dict[str(neuron)]) print('class_to_neuron_dict', class_to_neuron_dict) start = time.time() history, history_callback = train.train(model=model, epochs=FLAGS.epochs, train_data=train_generator, val_data=val_generator, save_freq=FLAGS.save_freq, initial_lr=FLAGS.lr, train_info=train_info, use_fit_generator=gpus <= 1, use_cosine_lr=FLAGS.use_cosine_lr) model.train_model.save_weights(model.checkpoints_path + 'weights.ckpt') test_acc = 100 * test_model(model, neuron_to_class_dict) history_callback.test_acc = test_acc print(constants.C_OKGREEN, "Test accuracy {:0.2f} %".format(test_acc), constants.C_ENDC) with open(model.checkpoints_path + 'neuron_to_class_dict.json', 'w') as fp: json.dump(neuron_to_class_dict, fp, indent=4) with open(model.checkpoints_path + 'train_info.txt', 'a') as t: t.write("Test accuracy {:0.2f} %".format(test_acc)) with open(model.checkpoints_path + 'neuron_labels.txt', 'a') as t: for label in neuron_labels: t.write(label + '\n') else: print("Train with fake data") train_data, val_data = helpers.load_fake_dataset_recognition(img_res) start = time.time() history, history_callback = train.train(model, FLAGS.epochs, train_data, val_data, FLAGS.save_freq, FLAGS.lr, 'Use FAKE DS\n', False, FLAGS.use_cosine_lr) helpers.save_history(FLAGS, model.model_name, model.dataset_name, history, start, 'recognition') if FLAGS.tflite: converter = tf.lite.TFLiteConverter.from_keras_model(model.inference_model) tflite_model = converter.convert() open(model.checkpoints_path + "model.tflite", "wb").write(tflite_model) return model, history, history_callback
def benchmarked_train(*args, **kwargs): return train(*args, **kwargs)
from models.train import train if __name__ == '__main__': train()
def train_detection(_argv): gpu_aval = tf.test.is_gpu_available(cuda_only=True) gpus = 0 if gpu_aval: for x in device_lib.list_local_devices(): if x.device_type == "GPU": gpus += 1 print(constants.C_WARNING, "Are CUDA gpus available? \n\t-", (constants.C_OKBLUE + ('Yes, ' + str(gpus)) if gpu_aval else constants.C_FAIL + 'No.'), constants.C_ENDC) batch_size = FLAGS.batch_size if gpus > 1: strategy = tf.distribute.MirroredStrategy() # Here the batch size scales up by number of workers since # `tf.data.Dataset.batch` expects the global batch size. Previously we used 'batch_size', # and now this is multiplied by the number of workers. batch_size *= gpus with strategy.scope(): dataset, model = load_model_and_db(batch_size) else: dataset, model = load_model_and_db(FLAGS.batch_size) if FLAGS.dataset_name == '': tfrecords_pattern_path = constants.PROCESSED_PROJECT_FOLDER_PATH + \ constants.TFRECORDS_PATH.format('COCO', 'val', '*-of-*') tensorboard_imgs = custom_callbacks.TensorBoardImagesDetection( inference_model=model.inference_model, tfrecords_pattern_path=tfrecords_pattern_path, dataset_name='COCO', model_input_size=model.image_res, freq=FLAGS.save_freq, logs_path=model.logs_path, n_images=5) start = time.time() history, history_callback = train.train(model, FLAGS.epochs, dataset.train_data, dataset.validation_data, FLAGS.save_freq, FLAGS.lr, 'Use fake DS\n', False, True, extra_callbacks=[tensorboard_imgs]) helpers.save_history(FLAGS, model.model_name, dataset.dataset_name, history, start, 'detection') return model, history train_info = FLAGS.extra.replace('\\n', '\n') + '\n' train_info += "Train model: {} For {} epochs and {} as Database. \nParameters used:\n " \ "- Checkpoints frequency = {}\n " \ "- Batch size = {}\n".format(model.model_name, FLAGS.epochs, FLAGS.dataset_name, FLAGS.save_freq, FLAGS.batch_size) if gpu_aval: train_info += " - {} gpu{} available for training\n".format(gpus, 's' if gpus > 1 else '') train_info += " - Use {} version of the model\n".format('tiny' if FLAGS.tiny else 'full') if FLAGS.trainable != 'none': train_info += " - Use transfer learning with trainable option: {} \n".format(FLAGS.trainable) else: train_info += " - Train from scratch\n" print(constants.C_WARNING, FLAGS.extra.replace('\\n', '\n'), constants.C_ENDC) tfrecords_pattern_path = dataset.tf_paths.format(dataset.dataset_name, 'val', '*-of-*') tensorboard_imgs = custom_callbacks.TensorBoardImagesDetection(inference_model=model.inference_model, tfrecords_pattern_path=tfrecords_pattern_path, dataset_name=dataset.dataset_name, model_input_size=model.image_res, freq=FLAGS.save_freq, logs_path=model.logs_path, n_images=10) start = time.time() history, history_callback = train.train(model=model, epochs=FLAGS.epochs, train_data=dataset.train_data, val_data=dataset.validation_data, save_freq=FLAGS.save_freq, initial_lr=FLAGS.lr, train_info=train_info, use_fit_generator=False, use_cosine_lr=FLAGS.use_cosine_lr, extra_callbacks=[tensorboard_imgs]) helpers.save_history(FLAGS, model.model_name, dataset.dataset_name, history, start, 'detection') return model, history
def run_one(): # Manual runs support cpu or 1 gpu if FLAGS.num_gpus == 0: dev = '/cpu:0' else: dev = '/gpu:0' if FLAGS.dataset == 'mnist': _model = pixeldp_cnn steps_num = 40000 eval_data_size = 10000 image_size = 28 n_channels = 1 num_classes = 10 relu_leakiness = 0.0 lrn_rate = 0.01 lrn_rte_changes = [30000] lrn_rte_vals = [0.01] if FLAGS.mode == 'train': batch_size = 128 n_draws = 1 elif FLAGS.mode == 'eval': batch_size = 25 n_draws = 2000 elif FLAGS.dataset == 'svhn': _model = pixeldp_resnet steps_num = 60000 eval_data_size = 26032 image_size = 32 n_channels = 3 num_classes = 10 relu_leakiness = 0.0 lrn_rate = 0.01 lrn_rte_changes = [20000, 40000, 50000] lrn_rte_vals = [0.01, 0.001, 0.0001] if FLAGS.mode == 'train': batch_size = 128 n_draws = 1 elif FLAGS.mode == 'eval': batch_size = 25 n_draws = 2000 else: steps_num = 90000 eval_data_size = 10000 lrn_rate = 0.1 lrn_rte_changes = [40000, 60000, 80000] lrn_rte_vals = [0.01, 0.001, 0.0001] if FLAGS.mode == 'train': batch_size = 128 n_draws = 1 elif FLAGS.mode == 'eval': batch_size = 1 n_draws = 2000 if FLAGS.dataset == 'cifar10': _model = pixeldp_resnet image_size = 32 n_channels = 3 num_classes = 10 relu_leakiness = 0.1 elif FLAGS.dataset == 'cifar100': _model = pixeldp_resnet image_size = 32 n_channels = 3 num_classes = 100 relu_leakiness = 0.1 if FLAGS.mode in ['attack', 'attack_eval', 'plot']: batch_size = 1 n_draws = 10 compute_robustness = True # See doc in ./models/params.py L = 0.1 hps = models.params.HParams( name_prefix="", batch_size=batch_size, num_classes=num_classes, image_size=image_size, n_channels=n_channels, lrn_rate=lrn_rate, lrn_rte_changes=lrn_rte_changes, lrn_rte_vals=lrn_rte_vals, num_residual_units=4, use_bottleneck=False, weight_decay_rate=0.0002, relu_leakiness=relu_leakiness, optimizer='mom', image_standardization=False, n_draws=n_draws, dp_epsilon=1.0, dp_delta=0.05, robustness_confidence_proba=0.05, attack_norm_bound=L, attack_norm='l2', sensitivity_norm='l2', sensitivity_control_scheme='bound', # bound or optimize noise_after_n_layers=1, layer_sensitivity_bounds=['l2_l2'], noise_after_activation=True, parseval_loops=10, parseval_step=0.0003, steps_num=steps_num, eval_data_size=eval_data_size, ) # atk = pgd atk = carlini # atk = carlini_robust_precision if atk == carlini_robust_precision: attack_params = attacks.params.AttackParamsPrec( restarts=1, n_draws_attack=20, n_draws_eval=500, attack_norm='l2', max_attack_size=5, num_examples=1000, attack_methodolody=attacks.name_from_module(atk), targeted=False, sgd_iterations=100, use_softmax=False, T=0.01) else: attack_params = attacks.params.AttackParams( restarts=1, n_draws_attack=20, n_draws_eval=500, attack_norm='l2', max_attack_size=5, num_examples=1000, attack_methodolody=attacks.name_from_module(atk), targeted=False, sgd_iterations=100, use_softmax=True) # _model = pixeldp_cnn # _model = pixeldp_resnet # _model = madry if _model == madry: madry.Model.maybe_download_and_extract(FLAGS.models_dir) hps = models.params.update(hps, 'batch_size', 200) hps = models.params.update(hps, 'n_draws', 1) attack_params = attacks.params.update(attack_params, 'n_draws_attack', 1) attack_params = attacks.params.update(attack_params, 'n_draws_eval', 1) compute_robustness = False if FLAGS.mode == 'train': train.train(hps, _model, dev=dev) elif FLAGS.mode == 'eval': evaluate.evaluate(hps, _model, compute_robustness=compute_robustness, dev=dev) elif FLAGS.mode == 'attack': train_attack.train_one(FLAGS.dataset, _model, hps, atk, attack_params, dev=dev) tf.reset_default_graph() elif FLAGS.mode == 'attack_eval': if attack_params.attack_methodolody == 'carlini_robust_precision': evaluate_attack_carlini_robust_prec.evaluate_one(FLAGS.dataset, _model, hps, atk, attack_params, dev=dev) else: evaluate_attack.evaluate_one(FLAGS.dataset, _model, hps, atk, attack_params, dev=dev) elif FLAGS.mode == 'plot': ms = [] ps = [] atks = [[]] robust_ms = [_model] robust_ps = [hps] robust_atks = [[attack_params]] # plots.plot_robust_accuracy.plot("test_robust_acc", None, None, ms, ps) plots.plot_accuracy_under_attack.plot( "test_acc_under_atk", robust_ms, robust_ps, robust_atks, x_ticks=[x / 10 for x in range(1, 16)])
def main(): parser = argparse.ArgumentParser() parser.add_argument('--dataset', type=str, default='synthetic_nonlinear', help='dataset to use') parser.add_argument('--fid', type=str, default=1, help='id for run') parser.add_argument('--data_path', type=str, default='data/', help='path to data files') parser.add_argument('--buffer_size', type=int, default=15000) parser.add_argument('--log_dir', type=str, default='test/', help='directory for logging') parser.add_argument('--img_size_w', type=int, default=28, help='width of input images') parser.add_argument('--img_size_h', type=int, default=28, help='height of input images') parser.add_argument('--n_channels', type=int, default=1, help='number of channels of input images') parser.add_argument('--n_input', type=int, default=2, help='dim of input') parser.add_argument('--num_classes', type=int, default=2, help='number of classes') parser.add_argument('--architecture', type=str, default='nonlinear', help='conv, nonlinear or linear') # batch norm parser.add_argument('--bn', dest='bn', action='store_true') parser.add_argument('--no-bn', dest='bn', action='store_false') parser.set_defaults(bn=False) parser.add_argument('--batch_size', type=int, default=120, help='minibatch size') parser.add_argument('--eval_batch_size', type=int, default=200, help='minibatch size evaluation') parser.add_argument('--num_epochs_class', type=int, default=25, help='number of epochs') parser.add_argument('--save_every', type=int, default=1000, help='save frequency') parser.add_argument('--learning_rate_class', type=float, default=0.01, help='learning rate') parser.add_argument('--decay_rate', type=float, default=0.9999, help='decay rate for learning rate') parser.add_argument('--h1', type=int, default=500, help='dim of hidden layer 1') parser.add_argument('--h2', type=int, default=500, help='dim of hidden layer 2') parser.add_argument('--img_data', dest='img_data', action='store_true') parser.add_argument('--no-img_data', dest='img_data', action='store_false') parser.set_defaults(img_data=False) parser.add_argument('--normalize', dest='normalize', action='store_true') parser.add_argument('--do-not-normalize', dest='normalize', action='store_false') parser.set_defaults(normalize=False) parser.add_argument('--classifier', type=str, default='counterfactual', help='counterfactual, max_loss, or standard') parser.add_argument('--weight_countfact_loss', type=float, default=1, help='weight counterfactual loss') parser.add_argument('--cfl_annealing', dest='cfl_annealing', action='store_true') parser.add_argument('--no-cfl_annealing', dest='cfl_annealing', action='store_false') parser.set_defaults(cfl_annealing=False) parser.add_argument('--cfl_rate_rise_factor', type=float, default=0.01, help='recon loss weight is increasd by this much \ every save_every steps') parser.add_argument('--cfl_rate_rise_time', type=int, default=30000, help='iterations before increasing cf loss term') parser.add_argument('--regression', dest='regression', action='store_true') parser.add_argument('--no-regression', dest='regression', action='store_false') parser.set_defaults(regression=False) parser.add_argument('--lambda_reg', type=float, default=1e-3, help='lambda_reg') parser.add_argument('--save_img_sums', dest='save_img_sums', action='store_true') parser.add_argument('--do-not-save_img_sums', dest='save_img_sums', action='store_false') parser.set_defaults(save_img_sums=False) parser.add_argument('--two_layer_CNN', dest='two_layer_CNN', action='store_true') parser.add_argument('--do-not-use-two_layer_CNN', dest='two_layer_CNN', action='store_false') parser.set_defaults(two_layer_CNN=True) args = parser.parse_args() args.time = datetime.datetime.now().strftime(r"%y%m%d_%H%M") modelparam = logging.make_modelparam_string_class(args) train.train(args, modelparam)
params['use_mask'] = True params['loadFiles'] = False params['generateMeanShape'] = True params['SelectedViews'] = [ 'view000', 'view001', 'view002', 'view010', 'view011', 'view012', 'view020', 'view021', 'view022', 'view030', 'view031', 'view032', 'view040', 'view041', 'view042', 'view050', 'view051', 'view052', 'view060', 'view061', 'view062', 'view070', 'view071', 'view072', 'view200', 'view201', 'view202', 'view210', 'view211', 'view212', 'view220', 'view221', 'view222', 'view230', 'view231', 'view232', 'view240', 'view241', 'view242', 'view250', 'view251', 'view252', 'view260', 'view261', 'view262', 'view270', 'view271', 'view272', 'view300', 'view301', 'view302', 'view310', 'view311', 'view312', 'view320', 'view321', 'view322', 'view330', 'view331', 'view332', 'view340', 'view341', 'view342', 'view350', 'view351', 'view352', 'view360', 'view361', 'view362', 'view370', 'view371', 'view372', 'view400', 'view401', 'view402', 'view410', 'view411', 'view412', 'view420', 'view421', 'view422', 'view430', 'view431', 'view432', 'view440', 'view441', 'view442', 'view450', 'view451', 'view452', 'view460', 'view461', 'view462', 'view470', 'view471', 'view472' ] # model specification params['lr'] = 0.0001 # lr = 0.0001 for airplane and 0.00001 for car params['batch_size'] = 64 params['epochs'] = 50 params['epochs_between_evals'] = 2 params['instances_between_checkpoints'] = 2000 loaders.save_params(params) train(model_fn, params)