def initialize_lists( extract_hyperparams , reconstruct_hyperparams , cross_pairs ): ##Make lists of hyperparams for use in model later extract_hyperparams_list = [ params for params in ParameterGrid( extract_hyperparams ) ] reconstruct_hyperparams_list = [ params for params in ParameterGrid( reconstruct_hyperparams ) ] ##Initialize dictionary with keys lists = [ "extract_names" , "extract_list" , "reconstruct_names" , "reconstruct_list" , "summary_sizes_list" , "accuracies_mean" , "accuracies_std_dev" , "accuracies_95_ci" , "accuracies_raw" , "values_mean" , "values_std_dev" , "values_95_ci" , "values_raw" ] data = { list_name : [] for list_name in lists } ##Put together arrays to save data for each run for extract_hyperparams_choice in extract_hyperparams_list: if not cross_pairs: ##If hyperparam search, if extract_hyperparams_choice[ "algorithm" ] == "random": ##Try all reconstructions on random summaries!! reconstruct_hyperparams_choices = reconstruct_hyperparams_list else: reconstruct_hyperparams_choices = [ utils.find_corresponding_reconstruction( extract_hyperparams_choice , reconstruct_hyperparams_list ) ] else: ##If full table, all combinations reconstruct_hyperparams_choices = reconstruct_hyperparams_list for reconstruct_hyperparams_choice in reconstruct_hyperparams_choices: data[ "extract_names" ].append( utils.get_model_name( extract_hyperparams_choice ) ) data[ "reconstruct_names" ].append( utils.get_model_name( reconstruct_hyperparams_choice ) ) data[ "extract_list" ].append( { key : [ value ] for key , value in extract_hyperparams_choice.items() } ) data[ "reconstruct_list" ].append( { key: [ value ] for key , value in reconstruct_hyperparams_choice.items() } ) for list_name in lists[ 4: ]: data[ list_name ].append( [] ) return data
def train(config_name, device): config, learning_rate, batch_size, max_epochs = load_config(config_name) train_data_loader, test_data_loader = get_data_loader( batch_size=batch_size, use_npy=config['use_npy'], frame_range=config['frame_range']) net, criterion, optimizer, scheduler = build_model(config, device, train=True) if config['resume_training']: saved_ckpt_path = get_model_name(config['old_ckpt_name']) net.load_state_dict(torch.load(saved_ckpt_path, map_location=device)) print("Successfully loaded trained ckpt at {}".format(saved_ckpt_path)) net.train() #net.backbone.conv1.register_forward_hook(printnorm) #net.backbone.conv2.register_backward_hook(printgradnorm) start_time = time.time() for epoch in range(max_epochs): train_loss = 0 num_samples = 0 scheduler.step() print("Learning Rate for Epoch {} is {} ".format( epoch + 1, scheduler.get_lr())) for i, (input, label_map) in enumerate(train_data_loader): input = input.to(device) label_map = label_map.to(device) optimizer.zero_grad() # Forward predictions = net(input) loss = criterion(predictions, label_map) loss.backward() optimizer.step() train_loss += float(loss) num_samples += label_map.shape[0] train_loss = train_loss * batch_size / num_samples val_loss = validate_batch(net, criterion, batch_size, test_data_loader, device) print("Epoch {}|Time {:.3f}|Training Loss: {}|Validation Loss: {}". format(epoch + 1, time.time() - start_time, train_loss, val_loss)) if (epoch + 1) == max_epochs or (epoch + 1) % config['save_every'] == 0: model_path = get_model_name(config['name'] + '__epoch{}'.format(epoch + 1)) torch.save(net.state_dict(), model_path) print("Checkpoint saved at {}".format(model_path)) print('Finished Training') end_time = time.time() elapsed_time = end_time - start_time print("Total time elapsed: {:.2f} seconds".format(elapsed_time))
def experiment(exp_name, device, eval_range='all', plot=True): config, _, _, _ = load_config(exp_name) net, loss_fn = build_model(config, device, train=False) state_dict = torch.load(get_model_name(config), map_location=device) if config['mGPUs']: net.module.load_state_dict(state_dict) else: net.load_state_dict(state_dict) train_loader, val_loader = get_data_loader( config['batch_size'], config['use_npy'], geometry=config['geometry'], frame_range=config['frame_range']) #Train Set train_metrics, train_precisions, train_recalls, _ = eval_batch( config, net, loss_fn, train_loader, device, eval_range) print("Training mAP", train_metrics['AP']) fig_name = "PRCurve_train_" + config['name'] legend = "AP={:.1%} @IOU=0.5".format(train_metrics['AP']) plot_pr_curve(train_precisions, train_recalls, legend, name=fig_name) # Val Set val_metrics, val_precisions, val_recalls, _ = eval_batch( config, net, loss_fn, val_loader, device, eval_range) print("Validation mAP", val_metrics['AP']) print("Net Fwd Pass Time on average {:.4f}s".format( val_metrics['Forward Pass Time'])) print("Nms Time on average {:.4f}s".format( val_metrics['Postprocess Time'])) fig_name = "PRCurve_val_" + config['name'] legend = "AP={:.1%} @IOU=0.5".format(val_metrics['AP']) plot_pr_curve(val_precisions, val_recalls, legend, name=fig_name)
def pred_pano_labels(pano_id, path_to_gsv_scrapes, GSV_IMAGE_WIDTH, GSV_IMAGE_HEIGHT, model_dir, num_threads=4, save_labeled_pano=True, verbose=False): ''' takes a panorama id and returns a dict of the filtered predictions''' path_to_folder = os.path.join(path_to_gsv_scrapes, pano_id[:2], pano_id) path_to_xml = path_to_folder + ".xml" (GSV_IMAGE_WIDTH, GSV_IMAGE_HEIGHT) = utils.extract_width_and_height(path_to_xml) now = time.time() temp = os.path.join('temp', 'crops') if not os.path.exists(temp): os.makedirs(temp) if not os.path.exists('viz'): os.makedirs('viz') utils.clear_dir(temp) make_sliding_window_crops(pano_id, GSV_IMAGE_WIDTH, GSV_IMAGE_HEIGHT, path_to_gsv_scrapes, num_threads=num_threads, verbose=verbose) model_name = utils.get_model_name() model_path = os.path.join(model_dir, model_name + '.pt') preds = predict_from_crops("temp", model_path, verbose=verbose) preds_loc = write_predictions_for_every_pano(path_to_gsv_scrapes, preds, verbose=verbose) if (len(preds_loc) == 0): return None pred = read_predictions_from_file(preds_loc) pred_dict = non_max_sup(pred, radius=150, clip_val=4.5, ignore_ind=1, verbose=verbose) if save_labeled_pano: pano_root = os.path.join(path_to_gsv_scrapes, pano_id[:2], pano_id) out_img = os.path.join("viz", pano_id + "_viz.jpg") show_predictions_on_image(pano_root, GSV_IMAGE_WIDTH, GSV_IMAGE_HEIGHT, pred_dict, out_img, show_coords=False, show_box=True, verbose=verbose) utils.clear_dir(temp) if verbose: print("{} took {} seconds".format(pano_id, time.time() - now)) return pred_dict
def log_model(self, model, **keywords): params = model.get_params() name = get_model_name(model) dt = datetime.datetime.utcnow() model = {'name': name, 'parameters': params, 'datetime': dt} model.update(keywords) inserted_id = self.collection.insert_one(model).inserted_id return str(inserted_id)
def send_event(self, event): data = {} data.update({'item': utils.get_serializer_class( self.__class__.__name__)(instance=self).data}) data.update( {'model_name': utils.get_model_name(self.__class__.__name__)}) send_event(event, UnicodeJSONRenderer() .render(data=data), channel="stream")
def add (self, index_model, force=False) : if self.is_lock() : return name = utils.get_model_name(index_model.Meta.model) if not force and self.has_key(name) : return self.candidate.setdefault(name, list()) self.candidate[name].append(index_model)
def update_parameters(self, summary_length, extract_hyperparams, reconstruct_hyperparams): self.summary_length = summary_length self.extract_hyperparams_list = [ params for params in ParameterGrid(extract_hyperparams) ] self.reconstruct_hyperparams_list = [ params for params in ParameterGrid(reconstruct_hyperparams) ] self.extract_names = [ utils.get_model_name(hyperparams) for hyperparams in self.extract_hyperparams_list ] self.reconstruct_names = [ utils.get_model_name(hyperparams) for hyperparams in self.reconstruct_hyperparams_list ]
def inference(): config_name = 'config.json' config, _, _, _ = load_config(config_name) if torch.cuda.is_available(): device = 'cuda' net = LDOPC(config['use_bn']).cuda() else: device = 'cpu' net = LDOPC(config['use_bn']).cpu() net.load_state_dict( torch.load(get_model_name(config['name']), map_location=device)) net.set_decode(True) loader, _ = get_data_loader(batch_size=1, use_npy=config['use_npy'], frame_range=config['frame_range']) net.eval() image_id = 25 threshold = config['cls_threshold'] with torch.no_grad(): pc_feature, label_map = loader.dataset[image_id] pc_feature = pc_feature.to(device) label_map = label_map.to(device) label_map_unnorm, label_list = loader.dataset.get_label(image_id) t_start = time.time() pred = net(pc_feature.unsqueeze(0)).squeeze_(0) print("Forward pass time", time.time() - t_start) cls_pred = pred[..., 0] activation = cls_pred > threshold num_boxes = int(activation.sum()) if num_boxes == 0: print("No bounding box found") return corners = torch.zeros((num_boxes, 8)) for i in range(1, 9): corners[:, i - 1] = torch.masked_select(pred[..., i], activation) corners = corners.view(-1, 4, 2).numpy() scores = (torch.masked_select(pred[..., 0], activation)).cpu().numpy() t_start = time.time() selected_ids = non_max_suppression(corners, scores, config['nms_iou_threshold']) corners = corners[selected_ids] scores = scores[selected_ids] print("Non max suppression time:", time.time() - t_start) pc_feature = pc_feature.cpu().numpy() plot_bev(pc_feature, label_list, window_name='GT') plot_bev(pc_feature, corners, window_name='Prediction') plot_label_map(cls_pred.cpu().numpy()) cv2.waitKey(0)
def add(self, index_model, force=False): if self.is_lock(): return name = utils.get_model_name(index_model.Meta.model) if not force and self.has_key(name): return self.candidate.setdefault(name, list()) self.candidate[name].append(index_model)
def single_crops(crop_dir, path_dir, model_dir, verbose=False): model_name = utils.get_model_name() model_path = os.path.join(model_dir, model_name + '.pt') preds = predict_from_crops(crop_dir, model_path, verbose=verbose) #print(preds) #utils.clear_dir("temp/crops") preds_loc = write_predictions_to_file(preds, path_dir, "completelabels.csv", verbose=verbose)
def experiment(config_name, device): config, _, _, _ = load_config(config_name) net, criterion = build_model(config, device, train=False) net.load_state_dict( torch.load(get_model_name(config['name']), map_location=device)) net.set_decode(True) loader, _ = get_data_loader(batch_size=1, use_npy=config['use_npy'], frame_range=config['frame_range']) net.eval() image_id = 25 threshold = config['cls_threshold'] with torch.no_grad(): input, label_map = loader.dataset[image_id] input = input.to(device) label_map = label_map.to(device) label_map_unnorm, label_list = loader.dataset.get_label(image_id) # Forward Pass t_start = time.time() pred = net(input.unsqueeze(0)).squeeze_(0) print("Forward pass time", time.time() - t_start) # Select all the bounding boxes with classification score above threshold cls_pred = pred[..., 0] activation = cls_pred > threshold # Compute (x, y) of the corners of selected bounding box num_boxes = int(activation.sum()) if num_boxes == 0: print("No bounding box found") return corners = torch.zeros((num_boxes, 8)) for i in range(1, 9): corners[:, i - 1] = torch.masked_select(pred[..., i], activation) corners = corners.view(-1, 4, 2).numpy() scores = torch.masked_select(pred[..., 0], activation).numpy() # NMS t_start = time.time() selected_ids = non_max_suppression(corners, scores, config['nms_iou_threshold']) corners = corners[selected_ids] scores = scores[selected_ids] print("Non max suppression time:", time.time() - t_start) # Visualization input_np = input.cpu().numpy() plot_bev(input_np, label_list, window_name='GT') plot_bev(input_np, corners, window_name='Prediction') plot_label_map(cls_pred.numpy())
def log_model(self, model, **keywords): ''' Log a model to the database ''' params = model.get_params() name = get_model_name(model) dt = datetime.datetime.utcnow() model = {'name': name, 'parameters': params, 'datetime': dt} model.update(keywords) inserted_id = self.collection.insert_one(model).inserted_id return str(inserted_id)
def training_curves(models, y_data, settings, histories, smoothing=1): """function for building training curves""" epochs = np.arange(settings['epochs']) markers = ['.', '^'] # ok for two outputs only if len(models) == 1: f = plt.figure(figsize=(4 * len(models), 4 * len(models))) else: f = plt.figure(figsize=(10 * len(models) / 3, 10 * len(models))) for i, mod in enumerate(models): model_name = utils.get_model_name(mod, settings['dataset']) plt.subplot(1, len(models), i + 1) for j in range(len(y_data)): plt.plot(np.convolve(np.log( histories[model_name].history[y_data[j] + '_mse']), np.ones(smoothing) / smoothing, mode='valid'), 'k--', alpha=0.5) plt.plot(np.convolve(np.log( histories[model_name].history['val_' + y_data[j] + '_mse']), np.ones(smoothing) / smoothing, mode='valid'), 'r--', alpha=0.5) plt.plot(epochs[::smoothing], np.convolve(np.log( histories[model_name].history[y_data[j] + '_mse']), np.ones(smoothing) / smoothing, mode='valid')[::smoothing], 'k' + markers[j], label=y_data[j], alpha=0.5) plt.plot(epochs[::smoothing], np.convolve(np.log( histories[model_name].history['val_' + y_data[j] + '_mse']), np.ones(smoothing) / smoothing, mode='valid')[::smoothing], 'r' + markers[j], alpha=0.5) if i == 0: plt.legend(frameon=False) plt.xlabel('epochs') plt.ylabel('log MSE') plt.title(model_name + ' ({} params)'.format(utils.count_parameters(mod))) plt.gca().set_aspect(1. / plt.gca().get_data_ratio()) plt.tight_layout() return f
def get_new_index_model(model, local_attrs=dict(), meta=None, name=None): if not meta: meta = new.classobj("Meta", (object, ), { "model": model, "exclude": tuple(), }) return new.classobj( name and name or "%s" % utils.get_model_name(model), (IndexModel, ), { "Meta": meta, "local_attrs": local_attrs, }, )
def log_model(self, model, **keywords): ''' Log a model to the database ''' params = model.get_params() name = get_model_name(model) dt = datetime.datetime.utcnow() model = {'name': name, 'parameters': params, 'datetime': dt} for mym in keywords['config']['models']: if type(mym) == dict: old_key = mym.keys()[0] new_key = old_key.replace('.','_') mym[new_key] = mym.pop(old_key) model.update(keywords) inserted_id = self.collection.insert_one(model).inserted_id return str(inserted_id)
def post_save (self, instance=None, sender=None, created=False, **kwargs) : import core core.initialize() index_models = sys.MODELS_REGISTERED.get(utils.get_model_name(instance), None) for index_model in index_models : if created : sys.INDEX_MANAGER.index( instance, analyzer=index_model._meta.analyzer, field_analyzers=index_model._meta.field_analyzers, ) else : sys.INDEX_MANAGER.index_update( instance, analyzer=index_model._meta.analyzer, field_analyzers=index_model._meta.field_analyzers, )
def plot_confusion_matrix(confusion_matrix_array): print('###### Start Confusion Matrix ####') print(confusion_matrix_array) save_report_to_csv( REPORT_FOLDER + get_model_name_by_file(VALIDATION_FILE) + '_confusion_report.csv', [ get_model_name(MODEL_FILE), get_model_name_by_file(MODEL_FILE), confusion_matrix_array[0][0], confusion_matrix_array[0][1], confusion_matrix_array[1][0], confusion_matrix_array[1][1] ]) print('###### End Confusion Matrix ####') df_cm = pd.DataFrame(confusion_matrix_array, range(2), range(2)) #plt.figure(figsize = (10,7)) plot = df_cm.plot() fig = plot.get_figure() ax = plt.subplot() sn.heatmap(df_cm, annot=True, fmt='g', ax=ax, annot_kws={"size": 16}) # font size # labels, title and ticks ax.set_xlabel('Predicted') ax.set_ylabel('Real') ax.yaxis.set_ticklabels(['Non Political', 'Political']) ax.xaxis.set_ticklabels(['Non Political', 'Political']) model_name = MODEL_FILE.replace(SKL_FOLDER, '') model_name = model_name.replace('.politics_ben.skl', '') ax.set_title(model_name.replace('_', ' ').upper()) fig.add_subplot(ax) fig.savefig(PLOT_FOLDER + 'confusion_matrix_' + model_name + '.png', dpi=400)
def test(exp_name, device, image_id): config, _, _, _ = load_config(exp_name) net, loss_fn = build_model(config, device, train=False) net.load_state_dict(torch.load(get_model_name(config), map_location=device)) net.set_decode(True) train_loader, val_loader = get_data_loader( 1, config['use_npy'], geometry=config['geometry'], frame_range=config['frame_range']) net.eval() with torch.no_grad(): num_gt, num_pred, scores, pred_image, pred_match, loss, t_forward, t_nms = \ eval_one(net, loss_fn, config, train_loader, image_id, device, plot=True) TP = (pred_match != -1).sum() print("Loss: {:.4f}".format(loss)) print("Precision: {:.2f}".format(TP / num_pred)) print("Recall: {:.2f}".format(TP / num_gt)) print("forward pass time {:.3f}s".format(t_forward)) print("nms time {:.3f}s".format(t_nms))
def get_tag(cls): return "/%s/"%get_model_name(cls)
def build_model(params): def _create_cell(model, id): with tf.variable_scope('cell_%d' % id, reuse=tf.AUTO_REUSE): cell_type = (tf.nn.rnn_cell.GRUCell if params.cell == 'GRUCell' else tf.nn.rnn_cell.LSTMCell) if cell_type is tf.nn.rnn_cell.GRUCell: cell = cell_type(params.state_size) else: cell = cell_type(params.state_size, state_is_tuple=True, use_peepholes=params.peep) cell = tf.nn.rnn_cell.DropoutWrapper( cell, output_keep_prob=model.dropout) return cell def _create_network(dataset_it, name=None): with tf.variable_scope("network", reuse=tf.AUTO_REUSE) as vs: model.set_prefix(name) model.dropout = tf.placeholder(tf.float32, shape=[]) model.batch_inputs, model.batch_labels = dataset_it.get_next() model.embedded_batch_inputs = tf.nn.embedding_lookup( model['embeddings'], model.batch_inputs) model.cell = tf.nn.rnn_cell.MultiRNNCell( cells=[_create_cell(model, i) for i in range(params.layers)], state_is_tuple=True) model.tuples_states = model.cell.zero_state(params.batch_size, dtype=tf.float32) model.states, model.current_state = tf.nn.dynamic_rnn( model.cell, model.embedded_batch_inputs, initial_state=model.tuples_states, scope=vs) model.states = model.states[:, params.suffix:, :] model.Wproj = tf.tile( tf.expand_dims( tf.get_variable("Wproj", shape=(params.state_size, num_classes)), 0), [params.batch_size, 1, 1]) model.bproj = tf.tile( tf.expand_dims( tf.get_variable("bproj", shape=(1, num_classes)), 0), [params.batch_size, 1, 1]) model.logits = tf.matmul(model.states, model.Wproj) + model.bproj model.logits = tf.reduce_mean(model.logits, axis=1) model.predictions = tf.argmax(model.logits, axis=1) model.labels = tf.argmax(model.batch_labels, axis=1) model.correct_predictions = tf.reduce_sum( tf.cast(tf.equal(model.predictions, model.labels), tf.int32)) model.loss = tf.nn.softmax_cross_entropy_with_logits( logits=model.logits, labels=model.batch_labels) model.total_loss = tf.reduce_mean(model.loss) if params.decay: step = tf.get_variable("step", shape=[], initializer=tf.constant_initializer(0), trainable=False) model.lr = tf.train.exponential_decay(params.lr, step, 1, decay_step) else: model.lr = params.lr all_optimizers = { 'GD': tf.train.GradientDescentOptimizer, 'Adagrad': tf.train.AdagradOptimizer, 'Adam': tf.train.AdamOptimizer, } model.optimizer = all_optimizers[params.optimizer](model.lr) if params.decay: model.opt = model.optimizer.minimize(model.total_loss, global_step=step) else: model.opt = model.optimizer.minimize(model.total_loss) tf.summary.scalar('correct_predictions', model.correct_predictions) model.all_summaries = tf.summary.merge_all() model.clear_prefix() tf.reset_default_graph() model = SmartDict() model.params = params model.params.name = model.name = params.name if params.name else utils.get_model_name( params) model.path = 'models/%s' % model.name print("Path: %s" % model.path) model.embeddings = tf.get_variable("embeddings", shape=[num_words, embedding_size]) model.train_dataset = input_fn('train', data['train'].num_examples, True, params) model.valid_dataset = input_fn('valid', valid_size, False, params) _create_network(model.train_dataset.iterator) _create_network(model.valid_dataset.iterator, name='validation') model.writer = tf.summary.FileWriter('models/%s/checkpoints/' % model.name, tf.get_default_graph(), flush_secs=10) model.saver = tf.train.Saver( var_list=tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES)) utils.describe_params(params) return model
y_test_hat_all = np.zeros(y.shape) num_folders = 10 recalls = np.zeros(num_folders) precisions = np.zeros(num_folders) auprc = np.zeros(num_folders) auroc = np.zeros(num_folders) thresholds = np.zeros(num_folders) skf = StratifiedKFold(n_splits=num_folders, random_state=1001, shuffle=True) X_np = X.to_numpy() y_np = y.to_numpy() # Use Scikit-Learn clf = get_clf() model_name = get_model_name(clf) print("Model = {}".format(model_name)) for idx, (train_index, test_index) in enumerate(skf.split(X, y)): print("Crossvalidation Iter: {} / {}".format(idx+1, num_folders)) X_train_np, X_test_np = X_np[train_index, :], X_np[test_index, :] X_train = pd.DataFrame(X_train_np, columns=X.columns) X_test = pd.DataFrame(X_test_np, columns=X.columns) y_train_np, y_test_np = y_np[train_index], y_np[test_index] y_train = pd.Series(y_train_np) y_test = pd.Series(y_test_np) X_train, imputer = perform_imputation(X_train, imputer=None) X_test, _ = perform_imputation(X_test, imputer=imputer) # Normalizing data
def is_added(self, f): name = utils.get_model_name(f.Meta.model) if not self.has_key(name): return False return self.get(name).count(f) > 0
def train(exp_name, device): # Load Hyperparameters config, learning_rate, batch_size, max_epochs = load_config(exp_name) # Dataset and DataLoader train_data_loader, test_data_loader = get_data_loader( batch_size, config['use_npy'], geometry=config['geometry'], frame_range=config['frame_range']) # Model net, loss_fn, optimizer, scheduler = build_model(config, device, train=True) # Tensorboard Logger train_logger = get_logger(config, 'train') val_logger = get_logger(config, 'val') if config['resume_training']: saved_ckpt_path = get_model_name(config) if config['mGPUs']: net.module.load_state_dict( torch.load(saved_ckpt_path, map_location=device)) else: net.load_state_dict( torch.load(saved_ckpt_path, map_location=device)) print("Successfully loaded trained ckpt at {}".format(saved_ckpt_path)) st_epoch = config['resume_from'] else: # writefile(config, 'train_loss.csv', 'iteration, cls_loss, loc_loss\n') # writefile(config, 'val_loss.csv', 'epoch, cls_loss, loc_loss\n') st_epoch = 0 step = 1 + st_epoch * len(train_data_loader) cls_loss = 0 loc_loss = 0 for epoch in range(st_epoch, max_epochs): start_time = time.time() train_loss = 0 net.train() if config['mGPUs']: net.module.set_decode(False) else: net.set_decode(False) scheduler.step() for input, label_map, image_id in train_data_loader: tic = time.time() #print('step', step) input = input.to(device) label_map = label_map.to(device) optimizer.zero_grad() # Forward predictions = net(input) loss, cls, loc = loss_fn(predictions, label_map) loss.backward() optimizer.step() cls_loss += cls loc_loss += loc train_loss += loss.item() if step % config['log_every'] == 0: cls_loss = cls_loss / config['log_every'] loc_loss = loc_loss / config['log_every'] train_logger.scalar_summary('cls_loss', cls_loss, step) train_logger.scalar_summary('loc_loss', loc_loss, step) cls_loss = 0 loc_loss = 0 #for tag, value in net.named_parameters(): # tag = tag.replace('.', '/') # train_logger.histo_summary(tag, value.data.cpu().numpy(), step) # train_logger.histo_summary(tag + '/grad', value.grad.data.cpu().numpy(), step) step += 1 #print(time.time() - tic) # Record Training Loss train_loss = train_loss / len(train_data_loader) train_logger.scalar_summary('loss', train_loss, epoch + 1) print("Epoch {}|Time {:.3f}|Training Loss: {:.5f}".format( epoch + 1, time.time() - start_time, train_loss)) # Run Validation # if (epoch +1) % 2 == 0: # tic = time.time() # val_metrics, _, _, log_images = eval_batch(config, net, loss_fn, test_data_loader, device) # for tag, value in val_metrics.items(): # val_logger.scalar_summary(tag, value, epoch + 1) # val_logger.image_summary('Predictions', log_images, epoch + 1) # print("Epoch {}|Time {:.3f}|Validation Loss: {:.5f}".format( # epoch + 1, time.time() - tic, val_metrics['loss'])) # Save Checkpoint if (epoch + 1) == max_epochs or (epoch + 1) % config['save_every'] == 0: model_path = get_model_name(config, epoch + 1) if config['mGPUs']: torch.save(net.module.state_dict(), model_path) else: torch.save(net.state_dict(), model_path) print("Checkpoint saved at {}".format(model_path)) print('Finished Training')
def __repr__(self): return '{}(robot={}, gripper={}, movable={}, surfaces={})'.format( self.__class__.__name__, get_model_name(self.mbp, self.robot), get_model_name(self.mbp, self.gripper), [get_model_name(self.mbp, model) for model in self.movable], self.surfaces)
#! /usr/bin/env python3 import sys import json import shutil import subprocess from urllib.parse import quote from utils import pdf_path_to_doc_name, doc_name_to_fragment_id, get_model_name with open(sys.argv[1]) as f: doc_topics = json.load(f) model = get_model_name(sys.argv[1]) pdf_path = sys.argv[2] host = sys.argv[3] port = sys.argv[4] doc_name = pdf_path_to_doc_name(pdf_path) fragment = quote(doc_name_to_fragment_id(doc_name)) if doc_name in doc_topics: open_cmd = shutil.which('open') if open_cmd is None: open_cmd = shutil.which('xdg-open') for p, t in doc_topics[doc_name]: url = f'http://{host}:{port}/topdocs/{model}/{t}.html#{fragment}' print(url) if open_cmd is not None: subprocess.run([open_cmd, url])
def __init__(self, *args, **kwargs): scope_prefix = utils.get_model_name(self._meta.model.__name__) kwargs.update({'scope_prefix': scope_prefix}) super(FormHelperMixin, self).__init__(*args, **kwargs) self.helper = TweetHelper(form=self) self.helper.form_id = utils.get_form_id(self)
def __call__(self, trained_model): #Read md template and compile to html pkg = os.path.dirname(os.path.abspath(__file__)) filepath = os.path.join(pkg, 'templates', 'classification_default.md') f = open(filepath, 'r') md = f.read() markdown = mistune.Markdown() html = markdown(md) #Add css filepath = os.path.join(pkg, 'templates', 'github.css') f = open(filepath, 'r') css = f.read() html = '<style>'+css+'</style>'+html t = Template(html) #Get the placeholders in the template #so things that the user do not want are not computed #Get name for the model model_class_name = get_model_name(trained_model.model) #If trained_model does not have a model_name, use model_class_name model_name = model_class_name if trained_model.model_name is None else trained_model.model_name #Feature importance try: fi = p.feature_importance(trained_model.model, trained_model.feature_names) fi_content = figure2html(fi) except AttributeError: fi_content = '%s does not support feature importances' % (model_class_name) except TypeError: fi_content = 'To compute this plot you need to provide a model and a feature list' except: fi_content = 'An unkwown error happened while computing feature importances plot' #Confusion matrix try: cm = p.confusion_matrix(trained_model.y_true, trained_model.y_pred, trained_model.target_names) cm_content = figure2html(cm) except TypeError: cm_content = 'To compute this plot you need to provide y_true, y_pred and target_names' except: cm_content = 'An unkwown error happened while computing the confusion matrix' #ROC roc = p.roc(trained_model.y_true, trained_model.y_score) roc_base64 = figure2base64(roc) #Precision-Recall pr = p.precision_recall(trained_model.y_true, trained_model.y_score) pr_base64 = figure2base64(pr) d = {'model_name': model_name, 'date': datetime.now().strftime('%B %d %Y %H:%M'), 'model_properties': prettify_dict(trained_model.model.get_params()), 'feature_names': prettify_list(trained_model.feature_names), 'feature_importance_plot': fi_content, 'confusion_matrix': cm_content, 'roc': base64_2_html(roc_base64), 'precision_recall': base64_2_html(pr_base64), } #Replace values in template t = t.substitute(d) #If path is provided, save report to disk if self.savepath is not None: path_to_report = os.path.join(self.savepath, model_name+'.html') report_file = open(path_to_report, 'w') report_file.write(t) report_file.close() else: return t
def __str__(self): return self.archive_string @classmethod def from_ini(cls, archive, ini): model = super(ArchivedDAQModel, cls).from_ini(archive.modelname, ini) model._archive_string = str(archive) return model if __name__ == '__main__': import sys import os if len(sys.argv) > 1: filename = sys.argv[1] else: filename = 'examples/G2SSM.ini' with open(filename) as ini: dm = DAQModel.from_ini(utils.get_model_name(filename), ini) for chan in dm.channels: print "{c.name}: {c.datarate}Hz, enabled={c.enabled}, acquire={c.acquire}".format( c=chan) chan = dm.find_channel('G2:SSM-AUXSUS_LEFT_EXC_DQ') print chan.name with open('out.ini', 'wb') as ini: dm.to_ini(ini)
def __str__(self): return self.archive_string @classmethod def from_ini(cls, archive, ini): model = super(ArchivedDAQModel, cls).from_ini(archive.modelname, ini) model._archive_string = str(archive) return model if __name__ == '__main__': import sys import os if len(sys.argv) > 1: filename = sys.argv[1] else: filename = 'examples/G2SSM.ini' with open(filename) as ini: dm = DAQModel.from_ini(utils.get_model_name(filename), ini) for chan in dm.channels: print "{c.name}: {c.datarate}Hz, enabled={c.enabled}, acquire={c.acquire}".format(c=chan) chan = dm.find_channel('G2:SSM-AUXSUS_LEFT_EXC_DQ') print chan.name with open('out.ini', 'wb') as ini: dm.to_ini(ini)
], 'RNN_FREEZE_EMDEDDINGS': [True], # freeze 'RNN_HIDDEN_DIM': [256], # 128 tai 256 'RNN_N_LAYERS': [1], # 3 layers in Howard et. al (2018) 'RNN_DROPOUT': [0.4], # 0.4put 'RNN_USE_GRU': [False], # True: use GRU, False: use LSTM 'RNN_BATCH_SIZE': [128], # Kagglessa käytettiin 1024 'RNN_EPOCHS': [10] # onko riittävä? }] param_grid = list(ParameterGrid(params)) print(f"Number of items in parameter grid {len(param_grid)}") test_accs = [] for i, param in enumerate(param_grid): print(f"params {param}") model_name = get_model_name(param) print(f"{i+1}/{len(param_grid)} testing {model_name}") start_time = time.time() test_loss, test_acc = analyse_sentiments(params=param, model_name=model_name, training_mode=training_mode) end_time = time.time() print(f"Training lasted for {round((end_time - start_time) / 60, 1)} min") test_accs.append(test_acc) for i, param in enumerate(param_grid): print(f"param {param}") print(f"test accuracy: {test_accs[i]}")
def post_delete (self, instance=None, sender=None, **kwargs) : index_models = sys.MODELS_REGISTERED.get(utils.get_model_name(instance), None) for index_model in index_models : if index_model._meta.casecade_delete : sys.INDEX_MANAGER.unindex(instance)
# model = models.DeepCoral(num_classes=3) # model.sharedNet.classifier = torch.nn.Linear(in_features=2048, out_features=3, bias=True) # print(model) class Model(torch.nn.Module): def __init__(self): super().__init__() self.model = model = tvmodels.resnet101(pretrained=False) self.model.conv1 = torch.nn.Conv2d(1, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False) self.model.fc = torch.nn.Linear(2048, 2, bias=True) def forward(self, source, target=None): source = self.model(source) return source, None # model = Model() # model = SuperSimple() model_name = utils.get_model_name(model) print(utils.get_model_name(model)) if torch.cuda.is_available(): if torch.cuda.device_count() > 1: print(f'{torch.cuda.device_count()} GPUs used') model = torch.nn.DataParallel(model) model = model.to(device) # -> Optimizer print(f'Optimizer: {settings.opt}') if settings.opt == 'SGD': optimizer = torch.optim.SGD([ {'params': model.parameters()} ], lr=settings.lr, momentum=settings.momentum, weight_decay=settings.l2_decay, nesterov=True) elif settings.opt == 'Adam':
def __call__(self, trained_model): #Read md template and compile to html pkg = os.path.dirname(os.path.abspath(__file__)) filepath = os.path.join(pkg, 'templates', 'classification_default.md') f = open(filepath, 'r') md = f.read() markdown = mistune.Markdown() html = markdown(md) #Add css filepath = os.path.join(pkg, 'templates', 'github.css') f = open(filepath, 'r') css = f.read() html = '<style>' + css + '</style>' + html t = Template(html) #Get the placeholders in the template #so things that the user do not want are not computed #Get name for the model model_class_name = get_model_name(trained_model.model) #If trained_model does not have a model_name, use model_class_name model_name = model_class_name if trained_model.model_name is None else trained_model.model_name #Feature importance try: fi = p.feature_importance(trained_model.model, trained_model.feature_names) fi_content = figure2html(fi) except AttributeError: fi_content = '%s does not support feature importances' % ( model_class_name) except TypeError: fi_content = 'To compute this plot you need to provide a model and a feature list' except: fi_content = 'An unkwown error happened while computing feature importances plot' #Confusion matrix try: cm = p.confusion_matrix(trained_model.y_true, trained_model.y_pred, trained_model.target_names) cm_content = figure2html(cm) except TypeError: cm_content = 'To compute this plot you need to provide y_true, y_pred and target_names' except: cm_content = 'An unkwown error happened while computing the confusion matrix' #ROC roc = p.roc(trained_model.y_true, trained_model.y_score) roc_base64 = figure2base64(roc) #Precision-Recall pr = p.precision_recall(trained_model.y_true, trained_model.y_score) pr_base64 = figure2base64(pr) d = { 'model_name': model_name, 'date': datetime.now().strftime('%B %d %Y %H:%M'), 'model_properties': prettify_dict(trained_model.model.get_params()), 'feature_names': prettify_list(trained_model.feature_names), 'feature_importance_plot': fi_content, 'confusion_matrix': cm_content, 'roc': base64_2_html(roc_base64), 'precision_recall': base64_2_html(pr_base64), } #Replace values in template t = t.substitute(d) #If path is provided, save report to disk if self.savepath is not None: path_to_report = os.path.join(self.savepath, model_name + '.html') report_file = open(path_to_report, 'w') report_file.write(t) report_file.close() else: return t
def generate_report(self, model, y_true, y_pred, y_score, feature_list, target_names, name): # Read md template and compile to html pkg = os.path.dirname(os.path.abspath(__file__)) filepath = os.path.join(pkg, "templates", "classification_default.md") f = open(filepath, "r") md = f.read() markdown = mistune.Markdown() html = markdown(md) # Add css filepath = os.path.join(pkg, "templates", "github.css") f = open(filepath, "r") css = f.read() html = "<style>" + css + "</style>" + html t = Template(html) # Get the placeholders in the template # so things that the user do not want are not computed # Get name for the model model_name = get_model_name(model) # Feature importance try: fi = p.feature_importance(model, feature_list) fi_content = figure2html(fi) except AttributeError: fi_content = "%s does not support feature importances" % (model_name) except TypeError: fi_content = "To compute this plot you need to provide a model and a feature list" except: fi_content = "An unkwown error happened while computing feature importances plot" # Confusion matrix try: cm = p.confusion_matrix_(y_true, y_pred, target_names) cm_content = figure2html(cm) except TypeError: cm_content = "To compute this plot you need to provide y_true, y_pred and target_names" except: cm_content = "An unkwown error happened while computing the confusion matrix" # ROC roc = p.roc(y_true, y_score) roc_base64 = figure2base64(roc) # Precision-Recall pr = p.precision_recall(y_true, y_score) pr_base64 = figure2base64(pr) d = { "model_name": model_name, "date": datetime.now().strftime("%B %d %Y %H:%M"), "model_properties": prettify_dict(model.get_params()), "feature_list": prettify_list(feature_list), "feature_importance_plot": fi_content, "confusion_matrix": cm_content, "roc": base64_2_html(roc_base64), "precision_recall": base64_2_html(pr_base64), } # Replace values in template t = t.substitute(d) # If path is provided, save report to disk if self.savepath is not None: name = d["model_name"] + ".html" if name == None else name report_file = open(self.savepath + name, "w") report_file.write(t) report_file.close() else: return t
def inference(): # evaluation config_name = 'config.json' config, _, _, _ = load_config(config_name) if torch.cuda.is_available(): device = 'cuda' net = PIXOR(config['use_bn']).cuda() else: device = 'cpu' net = PIXOR(config['use_bn']).cpu() # net, criterion = build_model(config, device, train=False) net.load_state_dict( torch.load(get_model_name(config['name']), map_location=device)) net.set_decode(True) # loader, _ = get_data_loader(batch_size=1, use_npy=config['use_npy'], frame_range=config['frame_range']) loader, _ = get_data_loader(batch_size=1) net.eval() # image_ids = [8, 3, 15, 27] image_id = 4 threshold = config['cls_threshold'] with torch.no_grad(): # for image_id in image_ids: pc_feature, label_map = loader.dataset[image_id] pc_feature = pc_feature.to(device) label_map = label_map.to(device) label_map_unnorm, label_list = loader.dataset.get_label(image_id) # Forward Pass t_start = time.time() pred = net(pc_feature.unsqueeze(0)).squeeze_(0) print("Forward pass time", time.time() - t_start) # Select all the bounding boxes with classification score above threshold cls_pred = pred[..., 0] activation = cls_pred > threshold # Compute (x, y) of the corners of selected bounding box num_boxes = int(activation.sum()) if num_boxes == 0: print("No bounding box found") return corners = torch.zeros((num_boxes, 8)) for i in range(1, 9): corners[:, i - 1] = torch.masked_select(pred[..., i], activation) corners = corners.view(-1, 4, 2).numpy() scores = (torch.masked_select(pred[..., 0], activation)).cpu().numpy() # NMS t_start = time.time() # these are some problems. ???? selected_ids = non_max_suppression(corners, scores, config['nms_iou_threshold']) corners = corners[selected_ids] scores = scores[selected_ids] print("Non max suppression time:", time.time() - t_start) # Visualization pc_feature = pc_feature.cpu().numpy() # (800, 700, 36) plot_bev(pc_feature, corners, label_list, window_name='predict_gt')
import torch import time from external import * import envs # registers the environment from utils import get_model_name import matplotlib.pyplot as plt fig, ax = plt.subplots(1, 4, sharey=True) args = get_args() if args.model_name is None: args.model_name = [0, 1, 2, 3, 4, 5, 6, 7, 8] model_name = get_model_name(args) expdir = args.save_dir + model_name + '/' # expdir = args.save_dir + 'no-stop-and-go/curriculum/' + model_name + '/' args.log_dir = expdir + 'logs/' args.model_dir = expdir + 'model/' args.det = not args.non_det args.eval = True device = torch.device("cuda:0" if args.cuda else "cpu") env = make_vec_envs(args, device, allow_early_resets=True) action_space_hi = env.action_space.high action_space_lo = env.action_space.low # Get a render function render_func = get_render_func(env) timestr = time.strftime("%Y%m%d-%H%M%S")
def get_pddlstream_problem(task, context, collisions=True): domain_pddl = read(get_file_path(__file__, 'domain.pddl')) stream_pddl = read(get_file_path(__file__, 'stream.pddl')) constant_map = {} plant = task.mbp robot = task.robot robot_name = get_model_name(plant, robot) world = plant.world_frame() # mbp.world_body() robot_joints = get_movable_joints(plant, robot) robot_conf = Conf(robot_joints, get_configuration(plant, context, robot)) init = [ ('Robot', robot_name), ('CanMove', robot_name), ('Conf', robot_name, robot_conf), ('AtConf', robot_name, robot_conf), ('HandEmpty', robot_name), ] goal_literals = [] if task.reset_robot: goal_literals.append(('AtConf', robot_name, robot_conf),) for obj in task.movable: obj_name = get_model_name(plant, obj) obj_pose = Pose(plant, world, obj, get_world_pose(plant, context, obj)) init += [('Graspable', obj_name), ('Pose', obj_name, obj_pose), ('AtPose', obj_name, obj_pose)] for surface in task.surfaces: init += [('Stackable', obj_name, surface)] for surface in task.surfaces: surface_name = get_model_name(plant, surface.model_index) if 'sink' in surface_name: init += [('Sink', surface)] if 'stove' in surface_name: init += [('Stove', surface)] for door in task.doors: door_body = plant.tree().get_body(door) door_name = door_body.name() door_joints = get_parent_joints(plant, door_body) door_conf = Conf(door_joints, get_joint_positions(door_joints, context)) init += [ ('Door', door_name), ('Conf', door_name, door_conf), ('AtConf', door_name, door_conf), ] for positions in [get_door_positions(door_body, DOOR_OPEN)]: conf = Conf(door_joints, positions) init += [('Conf', door_name, conf)] if task.reset_doors: goal_literals += [('AtConf', door_name, door_conf)] for obj, transform in task.goal_poses.items(): obj_name = get_model_name(plant, obj) obj_pose = Pose(plant, world, obj, transform) init += [('Pose', obj_name, obj_pose)] goal_literals.append(('AtPose', obj_name, obj_pose)) for obj in task.goal_holding: goal_literals.append(('Holding', robot_name, get_model_name(plant, obj))) for obj, surface in task.goal_on: goal_literals.append(('On', get_model_name(plant, obj), surface)) for obj in task.goal_cooked: goal_literals.append(('Cooked', get_model_name(plant, obj))) goal = And(*goal_literals) print('Initial:', init) print('Goal:', goal) stream_map = { 'sample-grasp': from_gen_fn(get_grasp_gen_fn(task)), 'plan-ik': from_gen_fn(get_ik_gen_fn(task, context, collisions=collisions)), 'plan-motion': from_fn(get_motion_fn(task, context, collisions=collisions)), 'plan-pull': from_gen_fn(get_pull_fn(task, context, collisions=collisions)), 'TrajPoseCollision': get_collision_test(task, context, collisions=collisions), 'TrajConfCollision': get_collision_test(task, context, collisions=collisions), } #stream_map = 'debug' # Runs PDDLStream with "placeholder streams" for debugging return PDDLProblem(domain_pddl, constant_map, stream_pddl, stream_map, init, goal)
def is_added (self, f) : name = utils.get_model_name(f.Meta.model) if not self.has_key(name) : return False return self.get(name).count(f) > 0
def __repr__(self): return '{}({}->{})'.format(self.__class__.__name__, get_model_name(self.mbp, self.child), self.parent.name())