Example #1
0
 def setUp(self):
     # Set up the objects needed to skin a poseinfer model
     self.pi_model = poseinfer.Model(self.model_path)
     self.pi_pose_params = poseinfer.PoseParams(22)
     
     # And those needed to skin a python model
     self.py_model = load_model(self.model_path)
Example #2
0
 def build(self, includes):
     """
     Build up a model object and attach it to the request object.
     
     The goal is that all database activity will be concentrated here.
     """
     cherrypy.request.model = load_model(includes)
Example #3
0
def score_images(image_files, outstream):
    _log.info('Opening model %s' % options.model_file)
    mod = model.load_model(options.model_file)

    results = [] # list of (filename, score)
    for imfile in image_files:
        imfile = imfile.strip()
        if imfile == '':
            continue

        image = cv2.imread(imfile)
        if image is None:
            _log.warn('Could not open %s' % imfile)
            continue
        score, attr = mod.score(image, do_filtering=False)
        results.append((imfile, score))

        if len(results) % 1000 == 0:
            _log.info('Scored %i images' % len(results))

    results = sorted(results, key=lambda x: -x[1])

    outstream.write('\n'.join(['%s,%.5f' % x for x in results]))
Example #4
0
def train(output_directory, log_directory, checkpoint_path, warm_start, warm_start_force, n_gpus,
          rank, group_name, hparams):
    """Training and validation logging results to tensorboard and stdout

    Params
    ------
    output_directory (string): directory to save checkpoints
    log_directory (string) directory to save tensorboard logs
    checkpoint_path(string): checkpoint path
    n_gpus (int): number of gpus
    rank (int): rank of current gpu
    hparams (object): comma separated list of "name=value" pairs.
    """
    # setup distributed
    hparams.n_gpus = n_gpus
    hparams.rank = rank
    if hparams.distributed_run:
        init_distributed(hparams, n_gpus, rank, group_name)
    
    # reproducablilty stuffs
    torch.manual_seed(hparams.seed)
    torch.cuda.manual_seed(hparams.seed)
    
    # initialize blank model
    model = load_model(hparams)
    model.eval()
    learning_rate = hparams.learning_rate
    
    # (optional) show the names of each layer in model, mainly makes it easier to copy/paste what you want to adjust
    if hparams.print_layer_names_during_startup:
        print(*[f"Layer{i} = "+str(x[0])+" "+str(x[1].shape) for i,x in enumerate(list(model.named_parameters()))], sep="\n")
    
    # (optional) Freeze layers by disabling grads
    if len(hparams.frozen_modules):
        for layer, params in list(model.named_parameters()):
            if any(layer.startswith(module) for module in hparams.frozen_modules):
                params.requires_grad = False
                print(f"Layer: {layer} has been frozen")
    
    # define optimizer (any params without requires_grad are ignored)
    optimizer = torch.optim.Adam(filter(lambda p: p.requires_grad, model.parameters()), lr=learning_rate, weight_decay=hparams.weight_decay)
    #optimizer = apexopt.FusedAdam(model.parameters(), lr=learning_rate, weight_decay=hparams.weight_decay)
    
    if hparams.fp16_run:
        model, optimizer = amp.initialize(model, optimizer, opt_level='O2')
    
    if hparams.distributed_run:
        model = apply_gradient_allreduce(model)
    
    criterion = Tacotron2Loss(hparams)
    
    logger = prepare_directories_and_logger(
        output_directory, log_directory, rank)
    
    # Load checkpoint if one exists
    best_validation_loss = 0.8 # used to see when "best_model" should be saved, default = 0.4, load_checkpoint will update to last best value.
    iteration = 0
    epoch_offset = 0
    _learning_rate = 1e-3
    saved_lookup = None
    if checkpoint_path is not None:
        if warm_start:
            model, iteration, saved_lookup = warm_start_model(
                checkpoint_path, model, hparams.ignore_layers)
        elif warm_start_force:
            model, iteration, saved_lookup = warm_start_force_model(
                checkpoint_path, model)
        else:
            model, optimizer, _learning_rate, iteration, best_validation_loss, saved_lookup = load_checkpoint(
                checkpoint_path, model, optimizer)
            if hparams.use_saved_learning_rate:
                learning_rate = _learning_rate
        iteration += 1  # next iteration is iteration + 1
        print('Model Loaded')
    
    # define datasets/dataloaders
    train_loader, valset, collate_fn, train_sampler, trainset = prepare_dataloaders(hparams, saved_lookup)
    epoch_offset = max(0, int(iteration / len(train_loader)))
    speaker_lookup = trainset.speaker_ids
    
    # load and/or generate global_mean
    if hparams.drop_frame_rate > 0.:
        if rank != 0: # if global_mean not yet calcuated, wait for main thread to do it
            while not os.path.exists(hparams.global_mean_npy): time.sleep(1)
        global_mean = calculate_global_mean(train_loader, hparams.global_mean_npy, hparams)
        hparams.global_mean = global_mean
        model.global_mean = global_mean
    
    # define scheduler
    use_scheduler = 0
    if use_scheduler:
        scheduler = ReduceLROnPlateau(optimizer, factor=0.1**(1/5), patience=10)
    
    model.train()
    is_overflow = False
    validate_then_terminate = 0
    if validate_then_terminate:
        val_loss = validate(model, criterion, valset, iteration,
            hparams.batch_size, n_gpus, collate_fn, logger,
            hparams.distributed_run, rank)
        raise Exception("Finished Validation")
    
    for param_group in optimizer.param_groups:
        param_group['lr'] = learning_rate
    
    rolling_loss = StreamingMovingAverage(min(int(len(train_loader)), 200))
    # ================ MAIN TRAINNIG LOOP! ===================
    for epoch in tqdm(range(epoch_offset, hparams.epochs), initial=epoch_offset, total=hparams.epochs, desc="Epoch:", position=1, unit="epoch"):
        tqdm.write("Epoch:{}".format(epoch))
        
        if hparams.distributed_run: # shuffles the train_loader when doing multi-gpu training
            train_sampler.set_epoch(epoch)
        start_time = time.time()
        # start iterating through the epoch
        for i, batch in tqdm(enumerate(train_loader), desc="Iter:  ", smoothing=0, total=len(train_loader), position=0, unit="iter"):
            # run external code every epoch, allows the run to be adjusting without restarts
            if (iteration % 1000 == 0 or i==0):
                try:
                    with open("run_every_epoch.py") as f:
                        internal_text = str(f.read())
                        if len(internal_text) > 0:
                            print(internal_text)
                            #code = compile(internal_text, "run_every_epoch.py", 'exec')
                            ldict = {'iteration': iteration}
                            exec(internal_text, globals(), ldict)
                            print("Custom code excecuted\nPlease remove code if it was intended to be ran once.")
                        else:
                            print("No Custom code found, continuing without changes.")
                except Exception as ex:
                    print(f"Custom code FAILED to run!\n{ex}")
                globals().update(ldict)
                locals().update(ldict)
                print("decay_start is ",decay_start)
                print("A_ is ",A_)
                print("B_ is ",B_)
                print("C_ is ",C_)
                print("min_learning_rate is ",min_learning_rate)
                print("epochs_between_updates is ",epochs_between_updates)
                print("drop_frame_rate is ",drop_frame_rate)
                print("p_teacher_forcing is ",p_teacher_forcing)
                print("teacher_force_till is ",teacher_force_till)
                print("val_p_teacher_forcing is ",val_p_teacher_forcing)
                print("val_teacher_force_till is ",val_teacher_force_till)
                print("grad_clip_thresh is ",grad_clip_thresh)
                if epoch % epochs_between_updates == 0 or epoch_offset == epoch:
                #if None:
                    tqdm.write("Old learning rate [{:.6f}]".format(learning_rate))
                    if iteration < decay_start:
                        learning_rate = A_ + C_
                    else:
                        iteration_adjusted = iteration - decay_start
                        learning_rate = (A_*(e**(-iteration_adjusted/B_))) + C_
                    learning_rate = max(min_learning_rate, learning_rate) # output the largest number
                    tqdm.write("Changing Learning Rate to [{:.6f}]".format(learning_rate))
                    for param_group in optimizer.param_groups:
                        param_group['lr'] = learning_rate
            # /run external code every epoch, allows the run to be adjusting without restarts/
            
            model.zero_grad()
            x, y = model.parse_batch(batch) # move batch to GPU (async)
            y_pred = model(x, teacher_force_till=teacher_force_till, p_teacher_forcing=p_teacher_forcing, drop_frame_rate=drop_frame_rate)
            
            loss, gate_loss = criterion(y_pred, y)
            
            if hparams.distributed_run:
                reduced_loss = reduce_tensor(loss.data, n_gpus).item()
                reduced_gate_loss = reduce_tensor(gate_loss.data, n_gpus).item()
            else:
                reduced_loss = loss.item()
                reduced_gate_loss = gate_loss.item()
            
            if hparams.fp16_run:
                with amp.scale_loss(loss, optimizer) as scaled_loss:
                    scaled_loss.backward()
            else:
                loss.backward()
            
            if hparams.fp16_run:
                grad_norm = torch.nn.utils.clip_grad_norm_(
                    amp.master_params(optimizer), grad_clip_thresh)
                is_overflow = math.isinf(grad_norm) or math.isnan(grad_norm)
            else:
                grad_norm = torch.nn.utils.clip_grad_norm_(
                    model.parameters(), grad_clip_thresh)
            
            optimizer.step()
            
            for j, param_group in enumerate(optimizer.param_groups):
                learning_rate = (float(param_group['lr'])); break
            
            if iteration < decay_start:
                learning_rate = A_ + C_
            else:
                iteration_adjusted = iteration - decay_start
                learning_rate = (A_*(e**(-iteration_adjusted/B_))) + C_
            learning_rate = max(min_learning_rate, learning_rate) # output the largest number
            for param_group in optimizer.param_groups:
                param_group['lr'] = learning_rate
            
            if not is_overflow and rank == 0:
                duration = time.time() - start_time
                average_loss = rolling_loss.process(reduced_loss)
                tqdm.write("{} [Train_loss {:.4f} Avg {:.4f}] [Gate_loss {:.4f}] [Grad Norm {:.4f}] "
                      "[{:.2f}s/it] [{:.3f}s/file] [{:.7f} LR]".format(
                    iteration, reduced_loss, average_loss, reduced_gate_loss, grad_norm, duration, (duration/(hparams.batch_size*n_gpus)), learning_rate))
                if iteration % 20 == 0:
                    diagonality, avg_prob = alignment_metric(x, y_pred)
                    logger.log_training(
                        reduced_loss, grad_norm, learning_rate, duration, iteration, teacher_force_till, p_teacher_forcing, diagonality=diagonality, avg_prob=avg_prob)
                else:
                    logger.log_training(
                        reduced_loss, grad_norm, learning_rate, duration, iteration, teacher_force_till, p_teacher_forcing)
                start_time = time.time()
            if is_overflow and rank == 0:
                tqdm.write("Gradient Overflow, Skipping Step")
            
            if not is_overflow and ((iteration % (hparams.iters_per_checkpoint/1) == 0) or (os.path.exists(save_file_check_path))):
                # save model checkpoint like normal
                if rank == 0:
                    checkpoint_path = os.path.join(
                        output_directory, "checkpoint_{}".format(iteration))
                    save_checkpoint(model, optimizer, learning_rate, iteration, hparams, best_validation_loss, average_loss, speaker_lookup, checkpoint_path)
            
            if not is_overflow and ((iteration % int((hparams.iters_per_validation)/1) == 0) or (os.path.exists(save_file_check_path)) or (iteration < 1000 and (iteration % 250 == 0))):
                if rank == 0 and os.path.exists(save_file_check_path):
                    os.remove(save_file_check_path)
                # perform validation and save "best_model" depending on validation loss
                val_loss = validate(model, criterion, valset, iteration,
                         hparams.val_batch_size, n_gpus, collate_fn, logger,
                         hparams.distributed_run, rank, val_teacher_force_till, val_p_teacher_forcing, teacher_force=1) #teacher_force
                val_loss = validate(model, criterion, valset, iteration,
                         hparams.val_batch_size, n_gpus, collate_fn, logger,
                         hparams.distributed_run, rank, val_teacher_force_till, val_p_teacher_forcing, teacher_force=2) #infer
                val_loss = validate(model, criterion, valset, iteration,
                         hparams.val_batch_size, n_gpus, collate_fn, logger,
                         hparams.distributed_run, rank, val_teacher_force_till, val_p_teacher_forcing, teacher_force=0) #validate (0.8 forcing)
                if use_scheduler:
                    scheduler.step(val_loss)
                if (val_loss < best_validation_loss):
                    best_validation_loss = val_loss
                    if rank == 0:
                        checkpoint_path = os.path.join(output_directory, "best_model")
                        save_checkpoint(model, optimizer, learning_rate, iteration, hparams, best_validation_loss, average_loss, speaker_lookup, checkpoint_path)
            
            iteration += 1
def run_TED1104(
    model_dir,
    fp16: bool,
    enable_evasion: bool,
    show_current_control: bool,
    num_parallel_sequences: int = 1,
    evasion_score=1000,
    enable_segmentation: bool = False,
) -> None:
    """
    Generate dataset exampled from a human playing a videogame
    HOWTO:
        Set your game in windowed mode
        Set your game to 1600x900 resolution
        Move the game window to the top left corner, there should be a blue line of 1 pixel in the left bezel of your
         screen and the window top bar should start in the top bezel of your screen.
        Let the AI play the game!
    Controls:
        Push QE to exit
        Push L to see the input images
        Push and hold J to use to use manual control

    Input:
    - model_dir: Directory where the model to use is stored (model.bin and model_hyperparameters.json files)
    - enable_evasion: automatic evasion maneuvers when the car gets stuck somewhere. Note: It adds computation time
    - show_current_control: Show a window with text that indicates if the car is currently being driven by
      the AI or a human
    - num_parallel_sequences: num_parallel_sequences to record, is the number is larger the recorded sequence of images
      will be updated faster and the model  will use more recent images as well as being able to do more iterations
      per second. However if num_parallel_sequences is too high it wont be able to update the sequences with 1/10 secs
      between images (default capturate to generate training examples).
    -evasion_score: Mean squared error value between images to activate the evasion maneuvers
    -enable_segmentation: Image segmentation will be performed using a pretrained model. Cars, persons, bikes.. will be
     highlighted to help the model to identify them.

    Output:

    """
    if enable_segmentation:
        image_segmentation = ImageSegmentation(model_name="fcn_resnet101",
                                               device=device,
                                               fp16=fp16)
    else:
        image_segmentation = None

    show_what_ai_sees: bool = False
    fp16: bool
    model: TEDD1104
    model = load_model(save_dir=model_dir, device=device, fp16=fp16)
    model.eval()
    stop_recording: threading.Event = threading.Event()

    th_img: threading.Thread = threading.Thread(
        target=screen_recorder.img_thread, args=[stop_recording])
    th_seq: threading.Thread = threading.Thread(
        target=screen_recorder.multi_image_sequencer_thread,
        args=[stop_recording, num_parallel_sequences],
    )
    th_img.setDaemon(True)
    th_seq.setDaemon(True)
    th_img.start()
    # Wait to launch the image_sequencer_thread, it needs the img_thread to be running
    time.sleep(5)
    th_seq.start()

    if show_current_control:
        root = Tk()
        var = StringVar()
        var.set("T.E.D.D. 1104 Driving")
        l = Label(root, textvariable=var, fg="green", font=("Courier", 44))
        l.pack()

    last_time: float = time.time()
    model_prediction: np.ndarray = np.asarray([0])
    score: np.float = np.float(0)
    last_num: int = 0
    while True:
        while (
                last_num == screen_recorder.num
        ):  # Don't run the same sequence again, the resulted key will be the same
            time.sleep(0.0001)
        last_num = screen_recorder.num
        if enable_segmentation:
            img_seq: np.ndarray = image_segmentation.add_segmentation(
                np.copy(screen_recorder.seq))
        else:
            img_seq: np.ndarray = np.copy(screen_recorder.seq)

        keys = key_check()
        if not "J" in keys:
            X: torch.Tensor = torch.from_numpy(
                reshape_x(np.array([img_seq]), fp=16 if fp16 else 32))
            model_prediction = model.predict(X.to(device)).cpu().numpy()
            select_key(int(model_prediction[0]))

            if show_current_control:
                var.set("T.E.D.D. 1104 Driving")
                l.config(fg="green")
                root.update()

            if enable_evasion:
                score = mse(img_seq[0], img_seq[4])
                if score < evasion_score:
                    if show_current_control:
                        var.set("Evasion maneuver")
                        l.config(fg="blue")
                        root.update()
                    select_key(4)
                    time.sleep(1)
                    if np.random.rand() > 0.5:
                        select_key(6)
                    else:
                        select_key(8)
                    time.sleep(0.2)
                    if show_current_control:
                        var.set("T.E.D.D. 1104 Driving")
                        l.config(fg="green")
                        root.update()

        else:
            if show_current_control:
                var.set("Manual Control")
                l.config(fg="red")
                root.update()

        if show_what_ai_sees:
            cv2.imshow("window1", img_seq[0])
            cv2.waitKey(1)
            cv2.imshow("window2", img_seq[1])
            cv2.waitKey(1)
            cv2.imshow("window3", img_seq[2])
            cv2.waitKey(1)
            cv2.imshow("window4", img_seq[3])
            cv2.waitKey(1)
            cv2.imshow("window5", img_seq[4])
            cv2.waitKey(1)

        if "Q" in keys and "E" in keys:
            print("\nStopping...")
            stop_recording.set()
            th_seq.join()
            th_img.join()
            if show_what_ai_sees:
                cv2.destroyAllWindows()

            break

        if "L" in keys:
            time.sleep(0.1)  # Wait for key release
            if show_what_ai_sees:
                cv2.destroyAllWindows()
                show_what_ai_sees = False
            else:
                show_what_ai_sees = True

        time_it: float = time.time() - last_time
        print(
            f"Recording at {screen_recorder.fps} FPS\n"
            f"Actions per second {None if time_it==0 else 1/time_it}\n"
            f"Key predicted by nn: {key_press(model_prediction[0])}\n"
            f"Difference from img 1 to img 5 {None if not enable_evasion else score}\n"
            f"Push QE to exit\n"
            f"Push L to see the input images\n"
            f"Push J to use to use manual control\n",
            end="\r",
        )

        last_time = time.time()
Example #6
0
    def __init__(
        self,
        config,
        data_loader,
        val_data_loader=None,
    ):
        # occupancy only for 3D Match dataset. For ScanNet, use RGB 3 channels.
        num_feats = 1

        # Model initialization
        Model = load_model(config.model)
        model = Model(
            num_feats,
            config.model_n_out,
            bn_momentum=config.bn_momentum,
            normalize_feature=config.normalize_feature,
            conv1_kernel_size=config.conv1_kernel_size,
            D=3)

        if config.weights:
            checkpoint = torch.load(config.weights)
            model.load_state_dict(checkpoint['state_dict'])

        logging.info(model)

        self.config = config
        self.model = model
        self.max_epoch = config.max_epoch
        self.save_freq = config.save_freq_epoch
        self.val_max_iter = config.val_max_iter
        self.val_epoch_freq = config.val_epoch_freq

        self.best_val_metric = config.best_val_metric
        self.best_val_epoch = -np.inf
        self.best_val = -np.inf

        if config.use_gpu and not torch.cuda.is_available():
            logging.warning('Warning: There\'s no CUDA support on this machine, '
                            'training is performed on CPU.')
            raise ValueError('GPU not available, but cuda flag set')

        self.device = torch.device(
            'cuda' if torch.cuda.is_available() else 'cpu')

        self.optimizer = getattr(optim, config.optimizer)(
            model.parameters(),
            lr=config.lr,
            momentum=config.momentum,
            weight_decay=config.weight_decay)

        self.scheduler = optim.lr_scheduler.ExponentialLR(
            self.optimizer, config.exp_gamma)

        self.start_epoch = 1
        self.checkpoint_dir = config.out_dir

        ensure_dir(self.checkpoint_dir)
        json.dump(
            config,
            open(os.path.join(self.checkpoint_dir, 'config.json'), 'w'),
            indent=4,
            sort_keys=False)

        self.iter_size = config.iter_size
        self.batch_size = data_loader.batch_size
        self.data_loader = data_loader
        self.val_data_loader = val_data_loader

        self.test_valid = True if self.val_data_loader is not None else False
        self.log_step = int(np.sqrt(self.config.batch_size))
        self.model = self.model.to(self.device)
        self.writer = SummaryWriter(logdir=config.out_dir)

        if config.resume is not None:
            if osp.isfile(config.resume):
                logging.info(
                    "=> loading checkpoint '{}'".format(config.resume))
                state = torch.load(config.resume)
                self.start_epoch = state['epoch']
                model.load_state_dict(state['state_dict'])
                self.scheduler.load_state_dict(state['scheduler'])
                self.optimizer.load_state_dict(state['optimizer'])

                if 'best_val' in state.keys():
                    self.best_val = state['best_val']
                    self.best_val_epoch = state['best_val_epoch']
                    self.best_val_metric = state['best_val_metric']
            else:
                raise ValueError(f"=> no checkpoint found at '{config.resume}'")
Example #7
0
def main(config):
  test_loader = make_data_loader(
      config, config.test_phase, 1, num_threads=config.test_num_thread, shuffle=True)

  num_feats = 1

  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

  Model = load_model(config.model)
  model = Model(num_feats, config.model_n_out, config=config)
  checkpoint = torch.load(config.save_dir + '/checkpoint.pth')
  model.load_state_dict(checkpoint['state_dict'])
  model = model.to(device)
  model.eval()

  success_meter, loss_meter, rte_meter, rre_meter = AverageMeter(), AverageMeter(
  ), AverageMeter(), AverageMeter()
  data_timer, feat_timer, reg_timer = Timer(), Timer(), Timer()

  test_iter = test_loader.__iter__()
  N = len(test_iter)
  n_gpu_failures = 0

  # downsample_voxel_size = 2 * config.voxel_size

  for i in range(len(test_iter)):

    data_timer.tic()
    try:
      xyz0, xyz1, coord0, coord1, feats0, feats1, matching01, T_gth, len_01 = test_iter.next(
      )
    except ValueError:
      n_gpu_failures += 1
      logging.info(f"# Erroneous GPU Pair {n_gpu_failures}")
      continue
    data_timer.toc()

    xyz0np, xyz1np = xyz0.numpy(), xyz1.numpy()

    pcd0 = make_open3d_point_cloud(xyz0np)
    pcd1 = make_open3d_point_cloud(xyz1np)

    with torch.no_grad():
      feat_timer.tic()
      sinput0 = ME.SparseTensor(feats0, coords=coord0).to(device)
      F0 = model(sinput0).F.detach()
      sinput1 = ME.SparseTensor(feats1, coords=coord1).to(device)
      F1 = model(sinput1).F.detach()
      feat_timer.toc()

    feat0 = make_open3d_feature(F0, 32, F0.shape[0])
    feat1 = make_open3d_feature(F1, 32, F1.shape[0])

    reg_timer.tic()
    distance_threshold = config.voxel_size * 1.0
    ransac_result = o3d.registration_ransac_based_on_feature_matching(
        pcd0, pcd1, feat0, feat1, distance_threshold,
        o3d.TransformationEstimationPointToPoint(False), 4, [
            o3d.CorrespondenceCheckerBasedOnEdgeLength(0.9),
            o3d.CorrespondenceCheckerBasedOnDistance(distance_threshold)
        ], o3d.RANSACConvergenceCriteria(4000000, 10000))
    T_ransac = torch.from_numpy(ransac_result.transformation.astype(np.float32))
    reg_timer.toc()

    loss_ransac = corr_dist(T_ransac, T_gth, xyz0, xyz1, weight=None, max_dist=1)

    # Translation error
    rte = np.linalg.norm(T_ransac[:3, 3] - T_gth[:3, 3])
    rre = np.arccos((np.trace(T_ransac[:3, :3].t() @ T_gth[:3, :3]) - 1) / 2)

    # Check if the ransac was successful. successful if rte < 2m and rre < 5◦
    # http://openaccess.thecvf.com/content_ECCV_2018/papers/Zi_Jian_Yew_3DFeat-Net_Weakly_Supervised_ECCV_2018_paper.pdf
    if rte < 2:
      rte_meter.update(rte)

    if not np.isnan(rre) and rre < np.pi / 180 * 5:
      rre_meter.update(rre)

    if rte < 2 and not np.isnan(rre) and rre < np.pi / 180 * 5:
      success_meter.update(1)
    else:
      success_meter.update(0)
      logging.info(f"Failed with RTE: {rte}, RRE: {rre}")

    loss_meter.update(loss_ransac)

    if i % 10 == 0:
      logging.info(
          f"{i} / {N}: Data time: {data_timer.avg}, Feat time: {feat_timer.avg}," +
          f" Reg time: {reg_timer.avg}, Loss: {loss_meter.avg}, RTE: {rte_meter.avg}," +
          f" RRE: {rre_meter.avg}, Success: {success_meter.sum} / {success_meter.count}" +
          f" ({success_meter.avg * 100} %)"
      )
      data_timer.reset()
      feat_timer.reset()
      reg_timer.reset()

  logging.info(
      f"Total loss: {loss_meter.avg}, RTE: {rte_meter.avg}, var: {rte_meter.var}," +
      f" RRE: {rre_meter.avg}, var: {rre_meter.var}, Success: {success_meter.sum} " +
      f"/ {success_meter.count} ({success_meter.avg * 100} %)"
  )
Example #8
0
def measure(output_directory, log_directory, checkpoint_path, warm_start,
            n_gpus, rank, group_name, hparams):
    """Handles all the validation scoring and printing"""
    stft = TacotronSTFT(hparams.filter_length, hparams.hop_length,
                        hparams.win_length, hparams.n_mel_channels,
                        hparams.sampling_rate, hparams.mel_fmin,
                        hparams.mel_fmax)

    mellotron = load_model(hparams).cuda().eval()
    mellotron.load_state_dict(torch.load(checkpoint_path)['state_dict'])

    waveglow_path = '/media/arsh/New Volume/Models/speech/waveglow_256channels_v4.pt'
    waveglow = torch.load(waveglow_path)['model'].cuda().eval()
    denoiser = Denoiser(waveglow).cuda().eval()

    arpabet_dict = cmudict.CMUDict('data/cmu_dictionary')
    audio_paths = 'filelists/libritts_train_clean_100_audiopath_text_sid_atleast5min_val_filelist.txt'
    dataloader = TextMelLoader(audio_paths, hparams)
    datacollate = TextMelCollate(1)

    speaker_ids = TextMelLoader(
        "filelists/libritts_train_clean_100_audiopath_text_sid_shorterthan10s_atleast5min_train_filelist.txt",
        hparams).speaker_ids
    speakers = pd.read_csv('filelists/libritts_speakerinfo.txt',
                           engine='python',
                           header=None,
                           comment=';',
                           sep=' *\| *',
                           names=['ID', 'SEX', 'SUBSET', 'MINUTES', 'NAME'])
    speakers['MELLOTRON_ID'] = speakers['ID'].apply(
        lambda x: speaker_ids[x] if x in speaker_ids else -1)
    female_speakers = cycle(
        speakers.query("SEX == 'F' and MINUTES > 20 and MELLOTRON_ID >= 0")
        ['MELLOTRON_ID'].sample(frac=1).tolist())
    male_speakers = cycle(
        speakers.query("SEX == 'M' and MINUTES > 20 and MELLOTRON_ID >= 0")
        ['MELLOTRON_ID'].sample(frac=1).tolist())

    file_idx = 0
    MEL_DTW = []
    TPP_DTW = []
    RAND_DTW = []
    logSpecDbConst = 10.0 / math.log(10.0) * math.sqrt(2.0)
    while file_idx < len(dataloader):
        audio_path, text, sid = dataloader.audiopaths_and_text[file_idx]

        # get audio path, encoded text, pitch contour and mel for gst
        text_encoded = torch.LongTensor(
            text_to_sequence(text, hparams.text_cleaners,
                             arpabet_dict))[None, :].cuda()
        pitch_contour = dataloader[file_idx][3][None].cuda()
        mel = load_mel(audio_path, stft)
        fs, audio = read(audio_path)

        # load source data to obtain rhythm using tacotron 2 as a forced aligner
        x, y = mellotron.parse_batch(datacollate([dataloader[file_idx]]))

        with torch.no_grad():
            # get rhythm (alignment map) using tacotron 2
            mel_outputs, mel_outputs_postnet, gate_outputs, rhythm, gst, tpse_gst = mellotron.forward(
                x)
            rhythm = rhythm.permute(1, 0, 2)
        speaker_id = next(female_speakers) if np.random.randint(2) else next(
            male_speakers)
        speaker_id = torch.LongTensor([speaker_id]).cuda()

        with torch.no_grad():
            mel_outputs, mel_outputs_postnet, gate_outputs, _ = mellotron.inference_noattention(
                (text_encoded, mel, speaker_id, pitch_contour, rhythm),
                with_tpse=False)
        with torch.no_grad():
            audio_mel = denoiser(
                waveglow.infer(mel_outputs_postnet, sigma=0.8), 0.01)[:, 0]

        with torch.no_grad():
            mel_outputs, mel_outputs_postnet, gate_outputs, _ = mellotron.inference_noattention(
                (text_encoded, mel, speaker_id, pitch_contour, rhythm),
                with_tpse=True)
        with torch.no_grad():
            audio_tpp = denoiser(
                waveglow.infer(mel_outputs_postnet, sigma=0.8), 0.01)[:, 0]

        with torch.no_grad():
            mel_outputs, mel_outputs_postnet, gate_outputs, _ = mellotron.inference_noattention(
                (text_encoded, np.random.randint(
                    0, 9), speaker_id, pitch_contour, rhythm),
                with_tpse=False)
        with torch.no_grad():
            audio_rand = denoiser(
                waveglow.infer(mel_outputs_postnet, sigma=0.8), 0.01)[:, 0]
        audio = np.pad(audio, 128)

        MEL_DTW.append(
            logSpecDbConst *
            np.log(dtw(audio_mel.data.cpu().numpy(), audio, eucCepDist)[0]))
        TPP_DTW.append(
            logSpecDbConst *
            np.log(dtw(audio_tpp.data.cpu().numpy(), audio, eucCepDist)[0]))
        RAND_DTW.append(
            logSpecDbConst *
            np.log(dtw(audio_rand.data.cpu().numpy(), audio, eucCepDist)[0]))
        print(MEL_DTW[-1], TPP_DTW[-1], RAND_DTW[-1])
        print("MEL DTW, Mean: ", np.mean(MEL_DTW), " SD: ", np.std(MEL_DTW))
        print("TPP DTW, Mean: ", np.mean(TPP_DTW), " SD: ", np.std(TPP_DTW))
        print("RAND DTW, Mean: ", np.mean(RAND_DTW), " SD: ", np.std(RAND_DTW))
        file_idx += 1
"""
from __future__ import print_function
import os
import sys

import chainer
import numpy

from argument import get_args
from model import load_model
from files import load_image, save_features, mkdir, grep_images


if __name__ == '__main__':
    args, xp = get_args()
    forward, in_size, mean_image = load_model(args)
    mkdir(args.dst)

    msg = True
    ps = []
    path_list = grep_images(args.src)
    total = len(path_list)
    x_batch = numpy.ndarray((args.batchsize, 3, in_size, in_size), dtype=numpy.float32)

    for i, path in enumerate(path_list):
        image = load_image(path, in_size, mean_image)
        x_batch[i % args.batchsize] = image
        ps.append(path)

        if i == 0 and i != total - 1:
            continue
Example #10
0
"""

# API Dependencies
import pickle
import json
import numpy as np
from model import load_model, make_prediction
from flask import Flask, request, jsonify

# Application definition
app = Flask(__name__)

# Load our model into memory.
# Please update this path to reflect your own trained model.
static_model = load_model(
    path_to_model='assets/trained-models/multiple_linear_regression2.pkl')

print('-' * 40)
print('Model succesfully loaded')
print('-' * 40)


# Define the API's interface.
# Here the 'model_prediction()' function will be called when a POST request
# is sent to our interface located at:
# http:{Host-machine-ip-address}:5000/api_v0.1
@app.route('/api_v0.1', methods=['POST'])
def model_prediction():
    # We retrieve the data payload of the POST request
    data = request.get_json(force=True)
    # We then preprocess our data, and use our pretrained model to make a
Example #11
0
from flask import Flask, jsonify, request

from model import load_model

app = Flask(__name__)
model = load_model()


@app.route("/")
def index():
    return f"Serving a {model.__class__.__name__} model using Flask."


@app.route("/predict", methods=["POST"])
def predict():
    body = request.get_json(force=True)
    prediction = model.predict(body["X"])
    return jsonify({"y": prediction.tolist()})


if __name__ == "__main__":
    app.run(host="0.0.0.0", port="8000")
    print('Step1: Dataset is loaded successfully!')

    preprocessed_data = preprocessing(data)
    print('Step2: Data preprocessing done successfully!')

    train, test = train_test_split(preprocessed_data)
    print('Step3: Data splitted into train and test successfully!')

    train_X, train_Y, test_X, test_Y, vectorizer = feature_extraction(
        train, test)

    trained_model = model_training(train_X, train_Y)
    print('Step4: Model trained successfully successfully!')

    accuracy = model_testing(test_X, test_Y, trained_model)

    vec_classifier = Pipeline([('vectorizer', vectorizer),
                               ('classifier', trained_model)])

    save_model(vec_classifier)
    print('Step5: Model is deployed successfully')

    loaded_model = load_model('saved_models/model.pkl')

    data = ["Aortic aneurysm", "AR", "SAS", "HBP", "hipertynson"]
    res = predict(data, loaded_model)
    print(res)

except Exception as e:
    print('Error occured in the main loop:  ', e)
Example #13
0
def main(_run, _log):
    args = argparse.Namespace(**_run.config)
    args = post_config_hook(args, _run)

    args.device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

    root = "./datasets"
    simclr_model = load_model(args, reload_model=True)
    simclr_model = simclr_model.to(args.device)
    simclr_model.eval()


    ## Logistic Regression
    n_classes = 10 # stl-10
    model = LogisticRegression(simclr_model.n_features, n_classes)
    model = model.to(args.device)

    optimizer = torch.optim.Adam(model.parameters(), lr=3e-4)
    criterion = torch.nn.CrossEntropyLoss()

    transform = transforms.Compose([
        transforms.ToTensor(),
        transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])

    if args.dataset == "STL10":
        train_dataset = torchvision.datasets.STL10(
            root, split="train", download=True, transform=torchvision.transforms.ToTensor()
        )
        test_dataset = torchvision.datasets.STL10(
            root, split="test", download=True, transform=torchvision.transforms.ToTensor()
        )
    elif args.dataset == "CIFAR10":
        train_dataset = torchvision.datasets.CIFAR10(
            root, train=True, download=True, transform=transform
        )
        test_dataset = torchvision.datasets.CIFAR10(
            root, train=False, download=True, transform=transform
        )
    else:
        raise NotImplementedError

    train_loader = torch.utils.data.DataLoader(
        train_dataset,
        batch_size=args.logistic_batch_size,
        shuffle=True,
        drop_last=True,
        num_workers=args.workers,
    )

    test_loader = torch.utils.data.DataLoader(
        test_dataset,
        batch_size=args.logistic_batch_size,
        shuffle=False,
        drop_last=True,
        num_workers=args.workers,
    )

    for epoch in range(args.logistic_epochs):
        loss_epoch, accuracy_epoch = train(args, train_loader, simclr_model, model, criterion, optimizer)
        print(f"Epoch [{epoch}/{args.logistic_epochs}]\t Loss: {loss_epoch / len(train_loader)}\t Accuracy: {accuracy_epoch / len(train_loader)}")

    # final testing
    loss_epoch, accuracy_epoch = test(args, test_loader, simclr_model, model, criterion, optimizer)
    print(f"[FINAL]\t Loss: {loss_epoch / len(test_loader)}\t Accuracy: {accuracy_epoch / len(test_loader)}")
Example #14
0
def run():
    model = load_model(arch, pretrained, use_gpu=use_gpu, num_classes=30,  AdaptiveAvgPool=AdaptiveAvgPool, SPP=SPP, num_levels=num_levels, pool_type=pool_type, bilinear=bilinear, stage=stage, SENet=SENet,se_stage=se_stage,se_layers=se_layers, threshold_before_avg = threshold_before_avg)
                                
    if use_gpu:
        if arch.lower().startswith('alexnet') or arch.lower().startswith('vgg'):
            model.features = nn.DataParallel(model.features)
            model.cuda()
        else:
            model = nn.DataParallel(model).cuda()#
            #model = nn.DataParallel(model, device_ids=[2]).cuda()

    best_prec3 = 0
    best_loss1 = 10000

    if try_resume:
        if os.path.isfile(latest_check):
            print("=> loading checkpoint '{}'".format(latest_check))
            checkpoint = torch.load(latest_check)
            global start_epoch 
            start_epoch = checkpoint['epoch']
            best_prec3 = checkpoint['best_prec3']
            best_loss1 = checkpoint['loss1']
            model.load_state_dict(checkpoint['state_dict'])
            print("=> loaded checkpoint '{}'"
                  .format(latest_check))
        else:
            print("=> no checkpoint found at '{}'".format(latest_check))

    cudnn.benchmark = True


    if class_aware:
        train_set = td.ImageFolder(TRAIN_ROOT, data_transforms(train_transform,input_size, train_scale, test_scale))
        train_loader = torch.utils.data.DataLoader(
                train_set,
                batch_size=BATCH_SIZE, shuffle=False,
                sampler=ClassAwareSampler.ClassAwareSampler(train_set),
                num_workers=INPUT_WORKERS, pin_memory=use_gpu)
    else:
        train_loader = torch.utils.data.DataLoader(
                td.ImageFolder(TRAIN_ROOT, data_transforms(train_transform,input_size, train_scale, test_scale)),
                batch_size=BATCH_SIZE, shuffle=True,
                num_workers=INPUT_WORKERS, pin_memory=use_gpu)
        
    val_loader = torch.utils.data.DataLoader(
            td.ImageFolder(VALIDATION_ROOT, data_transforms('validation',input_size, train_scale, test_scale)),
            batch_size=BATCH_SIZE, shuffle=False,
            num_workers=INPUT_WORKERS, pin_memory=use_gpu)


    criterion = nn.CrossEntropyLoss().cuda() if use_gpu else nn.CrossEntropyLoss()

    if if_fc:
        if pretrained == 'imagenet' or arch == 'resnet50' or arch == 'resnet18':
            ignored_params = list(map(id, model.module.fc.parameters()))
            base_params = filter(lambda p: id(p) not in ignored_params,
                                 model.module.parameters())
            lr_dicts = [{'params': base_params, 'lr':lr1}, 
                         {'params': model.module.fc.parameters(), 'lr':lr2}]
            
        elif pretrained =='places':
            if arch == 'preact_resnet50':
                lr_dicts = list()
                lr_dicts.append({'params': model.module._modules['12']._modules['1'].parameters(), 'lr':lr2})
                for _, index in enumerate(model.module._modules):
                    if index != '12':
                        lr_dicts.append({'params': model.module._modules[index].parameters(), 'lr':lr1})
                    else:
                        for index2,_ in enumerate(model.module._modules[index]):
                            if index2 !=1:
                                lr_dicts.append({'params': model.module._modules[index]._modules[str(index2)].parameters(), 'lr':lr1})
            elif arch == 'resnet152':
                lr_dicts = list()
                lr_dicts.append({'params': model.module._modules['10']._modules['1'].parameters(), 'lr':lr2})
                for _, index in enumerate(model.module._modules):
                    if index != '10':
                        lr_dicts.append({'params': model.module._modules[index].parameters(), 'lr':lr1})
                    else:
                        for index2,_ in enumerate(model.module._modules[index]):
                            if index2 !=1:
                                lr_dicts.append({'params': model.module._modules[index]._modules[str(index2)].parameters(), 'lr':lr1})

        if optim_type == 'Adam':
                optimizer = optim.Adam(lr_dicts,
                                         betas=betas, eps=eps, weight_decay=weight_decay) 
        elif optim_type == 'SGD':
                optimizer = optim.SGD(lr_dicts, 
                                         momentum=momentum, weight_decay=weight_decay)
    else:
        if optim_type == 'Adam':
            if stage == 1 or se_stage == 1:
#                optimizer = optim.Adam(model.module.fc.parameters(), lr=lr, betas=betas, eps=eps, weight_decay=weight_decay) 
                optimizer = optim.Adam(filter(lambda p: p.requires_grad, model.parameters()), lr=lr, betas=betas, eps=eps, weight_decay=weight_decay)
            else:
                optimizer = optim.Adam(model.parameters(), lr=lr, betas=betas, eps=eps, weight_decay=weight_decay) 
        elif optim_type == 'SGD':
            if stage == 1 or se_stage == 1:
#                if pretrained == 'places' and arch == 'preact_resnet50':
#                    optimizer = optim.SGD(model.module._modules['12']._modules['1'].parameters(), lr=lr, momentum=momentum, weight_decay=weight_decay)
#                elif pretrained =='places' and arch == 'resnet152':
#                    optimizer = optim.SGD(model.module._modules['10']._modules['1'].parameters(), lr=lr, momentum=momentum, weight_decay=weight_decay)
#                else:
#                    optimizer = optim.SGD(model.module.fc.parameters(), lr=lr, momentum=momentum, weight_decay=weight_decay)
                optimizer = optim.SGD(filter(lambda p: p.requires_grad, model.parameters()), lr=lr, momentum=momentum, weight_decay=weight_decay)
            else:
                optimizer = optim.SGD(model.parameters(), lr=lr, momentum=momentum, weight_decay=weight_decay)
    
    if evaluate:
        validate(val_loader, model, criterion)

    else:

        for epoch in range(start_epoch, epochs):

            # train for one epoch
            prec3, loss1 = train(train_loader, model, criterion, optimizer, epoch)

            # evaluate on validation set
            if VALIDATION_ROOT != None:
                prec3, loss1 = validate(val_loader, model, criterion, epoch)

            # remember best 
            is_best = loss1 <= best_loss1
            best_loss1 = min(loss1, best_loss1)
            if epoch % save_freq == 0:
                save_checkpoint_epoch({
                        'epoch': epoch + 1,
                        'arch': arch,
                        'state_dict': model.state_dict(),
                        'best_prec3': best_prec3,
                        'loss1': loss1
                        },  epoch+1)
            save_checkpoint({
                        'epoch': epoch + 1,
                        'arch': arch,
                        'state_dict': model.state_dict(),
                        'best_prec3': best_prec3,
                        'loss1': loss1
                        }, is_best)
_log = logging.getLogger(__name__)

def choose_thumbnails(video):
    video = cv2.VideoCapture(options.video)
    mod.choose_thumbnails(video, n=5)

if __name__ == '__main__':
    parser = OptionParser()

    parser.add_option('--model', default=None,
                      help='File containing the model')
    parser.add_option('--video', default=None,
                      help='The video to process')
    
    options, args = parser.parse_args()

    logging.basicConfig(level=logging.INFO)

    _log.info('Loading model')
    mod = model.load_model(options.model)

    gc.collect()
    objgraph.show_growth()
    for i in range(10):
        choose_thumbnails(options.video)
    gc.collect()
    objgraph.show_growth()

    _log.info('Done')
Example #16
0
from model.operation import Operation
from model.couch import LegoDocument


# TODO: Move this from here
couchdb = Server()
try:
    database = couchdb['lego_erp_test']
except:
    database = couchdb.create('lego_erp_test')

if not len(couchdb['lego_erp_test']):
    from model import load_model
    from examples import simplecrm

    load_model(database, simplecrm.model)


def index(request):
    '''
    index.html display the view list and each view operations.
    '''

    # Get all perspectives
    perspectives = {}
    for view in Perspective().list(database):
        perspectives[view.id] = view

    return render_to_response('explorer/index.html', { 'perspectives' : perspectives })

def operation(request, viewid, id):
Example #17
0
    registration_data = {'ip': ip, 'port': port}
    try:
        resp = requests.post(REGISTER_URL, data=registration_data)
        return resp.status_code == 200
    except requests.ConnectionError:
        return False


if __name__ == '__main__':
    # Setup database connection
    client = MongoClient()
    db = client.fraud_prediction_service
    print "Connected to Mongo database"

    # Load model
    model = load_model()
    print "Loaded predictive model"

    # Register for pinging service
    ip_address = socket.gethostbyname(socket.gethostname())
    print "attempting to register %s:%d" % (ip_address, PORT)
    if register_for_ping(ip_address, str(PORT)):
        print "Registered with pinging service"
    else:
        print "Registration with pinging service failed."

    # Start Flask app
    # 0.0.0.0 is so that the Flask app can receive requests from external
    # sources
    app.run(host='0.0.0.0', port=PORT, debug=True)
Example #18
0
def main(args):
    vocab, vocab_inv = load_vocab(args.model_dir)
    vocab_source, vocab_target = vocab
    vocab_inv_source, vocab_inv_target = vocab_inv

    source_dataset, target_dataset = read_data(vocab_source,
                                               vocab_target,
                                               args.source_train,
                                               args.target_train,
                                               args.source_dev,
                                               args.target_dev,
                                               args.source_test,
                                               args.target_test,
                                               reverse_source=True)

    source_dataset_train, source_dataset_dev, source_dataset_test = source_dataset
    target_dataset_train, target_dataset_dev, target_dataset_test = target_dataset
    printb("data	#")
    if len(source_dataset_train) > 0:
        print("train	{}".format(len(source_dataset_train)))
    if len(source_dataset_dev) > 0:
        print("dev	{}".format(len(source_dataset_dev)))
    if len(source_dataset_test) > 0:
        print("test	{}".format(len(source_dataset_test)))

    print("vocab	{}	(source)".format(len(vocab_source)))
    print("vocab	{}	(target)".format(len(vocab_target)))

    # split into buckets
    source_buckets_train = None
    if len(source_dataset_train) > 0:
        printb("buckets 	#data	(train)")
        source_buckets_train, target_buckets_train = make_buckets(
            source_dataset_train, target_dataset_train)
        if args.buckets_slice is not None:
            source_buckets_train = source_buckets_train[:args.buckets_slice +
                                                        1]
            target_buckets_train = target_buckets_train[:args.buckets_slice +
                                                        1]
        for size, data in zip(bucket_sizes, source_buckets_train):
            print("{} 	{}".format(size, len(data)))

    source_buckets_dev = None
    if len(source_dataset_dev) > 0:
        printb("buckets 	#data	(dev)")
        source_buckets_dev, target_buckets_dev = make_buckets(
            source_dataset_dev, target_dataset_dev)
        if args.buckets_slice is not None:
            source_buckets_dev = source_buckets_dev[:args.buckets_slice + 1]
            target_buckets_dev = target_buckets_dev[:args.buckets_slice + 1]
        for size, data in zip(bucket_sizes, source_buckets_dev):
            print("{} 	{}".format(size, len(data)))

    source_buckets_test = None
    if len(source_dataset_test) > 0:
        printb("buckets		#data	(test)")
        source_buckets_test, target_buckets_test = make_buckets(
            source_dataset_test, target_dataset_test)
        if args.buckets_slice is not None:
            source_buckets_test = source_buckets_test[:args.buckets_slice + 1]
            target_buckets_test = target_buckets_test[:args.buckets_slice + 1]
        for size, data in zip(bucket_sizes, source_buckets_test):
            print("{} 	{}".format(size, len(data)))

    model = load_model(args.model_dir)
    assert model is not None
    if args.gpu_device >= 0:
        cuda.get_device(args.gpu_device).use()
        model.to_gpu()

    def mean(l):
        return sum(l) / len(l)

    with chainer.using_config("train", False):
        if source_buckets_train is not None:
            printb("WER (train)")
            wer_train = compute_error_rate_buckets(model, source_buckets_train,
                                                   target_buckets_train,
                                                   len(vocab_target),
                                                   args.beam_width, args.alpha)
            print(mean(wer_train), wer_train)

        if source_buckets_dev is not None:
            printb("WER (dev)")
            wer_dev = compute_error_rate_buckets(model, source_buckets_dev,
                                                 target_buckets_dev,
                                                 len(vocab_target),
                                                 args.beam_width, args.alpha)
            print(mean(wer_dev), wer_dev)

        if source_buckets_test is not None:
            printb("WER (test)")
            wer_test = compute_error_rate_buckets(model, source_buckets_test,
                                                  target_buckets_test,
                                                  len(vocab_target),
                                                  args.beam_width, args.alpha)
            print(mean(wer_test), wer_test)
Example #19
0
    args = parser.parse_args()

    # load env
    import json
    from env import make_env
    file_args = json.load(open(args.argfile))
    env_name = file_args["env_name"]
    env_args = file_args["env_args"]
    env_args["enable_draw"] = True
    test_env = make_env(env_name, env_args)

    # load model
    import torch
    from model import load_model
    ckpt = args.ckpt
    if args.ckpt:
        ckpt = args.ckpt
    elif "ckpt" in file_args:
        ckpt = file_args["ckpt"]
    else:
        print("please specify checkpoint")
        assert (False)

    model = load_model(ckpt)
    data = torch.load(ckpt)
    if "select_set" in data.keys():
        select_set = data["select_set"]
    else:
        select_set = None
    test_model(test_env, model, select_set, args.record, args.random)
Example #20
0
def train(output_directory, log_directory, checkpoint_path, warm_start,
          warm_start_force, n_gpus, rank, group_name, hparams):
    """Training and validation logging results to tensorboard and stdout

    Params
    ------
    output_directory (string): directory to save checkpoints
    log_directory (string) directory to save tensorboard logs
    checkpoint_path(string): checkpoint path
    n_gpus (int): number of gpus
    rank (int): rank of current gpu
    hparams (object): comma separated list of "name=value" pairs.
    """
    # setup distributed
    hparams.n_gpus = n_gpus
    hparams.rank = rank
    if hparams.distributed_run:
        init_distributed(hparams, n_gpus, rank, group_name)

    # reproducablilty stuffs
    torch.manual_seed(hparams.seed)
    torch.cuda.manual_seed(hparams.seed)

    # initialize blank model
    print('Initializing UnTTS...')
    model = load_model(hparams)
    print('Done')
    model.eval()
    learning_rate = hparams.learning_rate

    # (optional) show the names of each layer in model, mainly makes it easier to copy/paste what you want to adjust
    if hparams.print_layer_names_during_startup:
        print(*[
            f"Layer{i} = " + str(x[0]) + " " + str(x[1].shape)
            for i, x in enumerate(list(model.named_parameters()))
        ],
              sep="\n")

    # (optional) Freeze layers by disabling grads
    if len(hparams.frozen_modules):
        for layer, params in list(model.named_parameters()):
            if any(
                    layer.startswith(module)
                    for module in hparams.frozen_modules):
                params.requires_grad = False
                print(f"Layer: {layer} has been frozen")

    # define optimizer (any params without requires_grad are ignored)
    #optimizer = torch.optim.Adam(filter(lambda p: p.requires_grad, model.parameters()), lr=learning_rate, weight_decay=hparams.weight_decay)
    optimizer = apexopt.FusedAdam(filter(lambda p: p.requires_grad,
                                         model.parameters()),
                                  lr=learning_rate,
                                  weight_decay=hparams.weight_decay)

    if True and rank == 0:
        pytorch_total_params = sum(p.numel() for p in model.parameters())
        print("{:,} total parameters in model".format(pytorch_total_params))
        pytorch_total_params = sum(p.numel() for p in model.parameters()
                                   if p.requires_grad)
        print("{:,} trainable parameters.".format(pytorch_total_params))

    if hparams.fp16_run:
        model, optimizer = amp.initialize(
            model, optimizer, opt_level=f'O{hparams.fp16_run_optlvl}')

    if hparams.distributed_run:
        model = apply_gradient_allreduce(model)

    criterion = Tacotron2Loss(hparams)

    logger = prepare_directories_and_logger(hparams, output_directory,
                                            log_directory, rank)

    # Load checkpoint if one exists
    best_validation_loss = 1e3  # used to see when "best_model" should be saved

    n_restarts = 0
    checkpoint_iter = 0
    iteration = 0
    epoch_offset = 0
    _learning_rate = 1e-3
    saved_lookup = None

    global best_val_loss_dict
    best_val_loss_dict = None
    global best_loss_dict
    best_loss_dict = None
    global expavg_loss_dict
    expavg_loss_dict = None
    expavg_loss_dict_iters = 0  # initial iters expavg_loss_dict has been fitted
    loss_dict_smoothness = 0.95  # smoothing factor

    if checkpoint_path is not None:
        if warm_start:
            model, iteration, saved_lookup = warm_start_model(
                checkpoint_path, model, hparams.ignore_layers)
        elif warm_start_force:
            model, iteration, saved_lookup = warm_start_force_model(
                checkpoint_path, model)
        else:
            _ = load_checkpoint(checkpoint_path, model, optimizer,
                                best_val_loss_dict, best_loss_dict)
            model, optimizer, _learning_rate, iteration, best_validation_loss, saved_lookup, best_val_loss_dict, best_loss_dict = _
            if hparams.use_saved_learning_rate:
                learning_rate = _learning_rate
        checkpoint_iter = iteration
        iteration += 1  # next iteration is iteration + 1
        print('Model Loaded')

    # define datasets/dataloaders
    train_loader, valset, collate_fn, train_sampler, trainset = prepare_dataloaders(
        hparams, saved_lookup)
    epoch_offset = max(0, int(iteration / len(train_loader)))
    speaker_lookup = trainset.speaker_ids

    # load and/or generate global_mean
    if hparams.drop_frame_rate > 0.:
        if rank != 0:  # if global_mean not yet calcuated, wait for main thread to do it
            while not os.path.exists(hparams.global_mean_npy):
                time.sleep(1)
        global_mean = calculate_global_mean(train_loader,
                                            hparams.global_mean_npy, hparams)
        hparams.global_mean = global_mean
        model.global_mean = global_mean

    # define scheduler
    use_scheduler = 0
    if use_scheduler:
        scheduler = ReduceLROnPlateau(optimizer,
                                      factor=0.1**(1 / 5),
                                      patience=10)

    model.train()
    is_overflow = False
    validate_then_terminate = 0
    if validate_then_terminate:
        val_loss = validate(model, criterion, valset, iteration,
                            hparams.batch_size, n_gpus, collate_fn, logger,
                            hparams.distributed_run, rank)
        raise Exception("Finished Validation")

    for param_group in optimizer.param_groups:
        param_group['lr'] = learning_rate

    rolling_loss = StreamingMovingAverage(min(int(len(train_loader)), 200))
    # ================ MAIN TRAINNIG LOOP! ===================
    training = True
    while training:
        try:
            for epoch in tqdm(range(epoch_offset, hparams.epochs),
                              initial=epoch_offset,
                              total=hparams.epochs,
                              desc="Epoch:",
                              position=1,
                              unit="epoch"):
                tqdm.write("Epoch:{}".format(epoch))

                if hparams.distributed_run:  # shuffles the train_loader when doing multi-gpu training
                    train_sampler.set_epoch(epoch)
                start_time = time.time()
                # start iterating through the epoch
                for i, batch in tqdm(enumerate(train_loader),
                                     desc="Iter:  ",
                                     smoothing=0,
                                     total=len(train_loader),
                                     position=0,
                                     unit="iter"):
                    # run external code every iter, allows the run to be adjusted without restarts
                    if (i == 0 or iteration % param_interval == 0):
                        try:
                            with open("run_every_epoch.py") as f:
                                internal_text = str(f.read())
                                if len(internal_text) > 0:
                                    #code = compile(internal_text, "run_every_epoch.py", 'exec')
                                    ldict = {
                                        'iteration': iteration,
                                        'checkpoint_iter': checkpoint_iter,
                                        'n_restarts': n_restarts
                                    }
                                    exec(internal_text, globals(), ldict)
                                else:
                                    print(
                                        "[info] tried to execute 'run_every_epoch.py' but it is empty"
                                    )
                        except Exception as ex:
                            print(
                                f"[warning] 'run_every_epoch.py' FAILED to execute!\nException:\n{ex}"
                            )
                        globals().update(ldict)
                        locals().update(ldict)
                        if show_live_params:
                            print(internal_text)
                    n_restarts = n_restarts_override if (
                        n_restarts_override is not None) else n_restarts or 0
                    # Learning Rate Schedule
                    if custom_lr:
                        if iteration < warmup_start:
                            learning_rate = warmup_start_lr
                        elif iteration < warmup_end:
                            learning_rate = (iteration - warmup_start) * (
                                (A_ + C_) - warmup_start_lr
                            ) / (
                                warmup_end - warmup_start
                            ) + warmup_start_lr  # learning rate increases from warmup_start_lr to A_ linearly over (warmup_end-warmup_start) iterations.
                        else:
                            if iteration < decay_start:
                                learning_rate = A_ + C_
                            else:
                                iteration_adjusted = iteration - decay_start
                                learning_rate = (
                                    A_ * (e**(-iteration_adjusted / B_))) + C_
                        assert learning_rate > -1e-8, "Negative Learning Rate."
                        if decrease_lr_on_restart:
                            learning_rate = learning_rate / (2**(n_restarts /
                                                                 3))
                        for param_group in optimizer.param_groups:
                            param_group['lr'] = learning_rate
                    # /run external code every epoch, allows the run to be adjusting without restarts/
                    model.zero_grad()
                    x, y = model.parse_batch(
                        batch)  # move batch to GPU (async)
                    y_pred = model(x)

                    loss_scalars = {
                        "MelGlow_ls": MelGlow_ls,
                        "DurGlow_ls": DurGlow_ls,
                        "VarGlow_ls": VarGlow_ls,
                        "Sylps_ls": Sylps_ls,
                    }
                    loss_dict = criterion(y_pred, y, loss_scalars)
                    loss = loss_dict['loss']

                    if hparams.distributed_run:
                        reduced_loss_dict = {
                            k: reduce_tensor(v.data, n_gpus).item()
                            if v is not None else 0.
                            for k, v in loss_dict.items()
                        }
                    else:
                        reduced_loss_dict = {
                            k: v.item() if v is not None else 0.
                            for k, v in loss_dict.items()
                        }
                    reduced_loss = reduced_loss_dict['loss']

                    if hparams.fp16_run:
                        with amp.scale_loss(loss, optimizer) as scaled_loss:
                            scaled_loss.backward()
                    else:
                        loss.backward()

                    if grad_clip_thresh:
                        if hparams.fp16_run:
                            grad_norm = torch.nn.utils.clip_grad_norm_(
                                amp.master_params(optimizer), grad_clip_thresh)
                            is_overflow = math.isinf(grad_norm) or math.isnan(
                                grad_norm)
                        else:
                            grad_norm = torch.nn.utils.clip_grad_norm_(
                                model.parameters(), grad_clip_thresh)
                    else:
                        grad_norm = 0.0

                    optimizer.step()

                    # get current Loss Scale of first optimizer
                    loss_scale = amp._amp_state.loss_scalers[
                        0]._loss_scale if hparams.fp16_run else 32768.

                    # restart if training/model has collapsed
                    if (iteration > 1e3 and
                        (reduced_loss > LossExplosionThreshold)) or (
                            math.isnan(reduced_loss)) or (loss_scale < 1 / 4):
                        raise LossExplosion(
                            f"\nLOSS EXPLOSION EXCEPTION ON RANK {rank}: Loss reached {reduced_loss} during iteration {iteration}.\n\n\n"
                        )

                    if expavg_loss_dict is None:
                        expavg_loss_dict = reduced_loss_dict
                    else:
                        expavg_loss_dict = {
                            k: (reduced_loss_dict[k] *
                                (1 - loss_dict_smoothness)) +
                            (expavg_loss_dict[k] * loss_dict_smoothness)
                            for k in expavg_loss_dict.keys()
                        }
                        expavg_loss_dict_iters += 1

                    if expavg_loss_dict_iters > 100:
                        if best_loss_dict is None:
                            best_loss_dict = expavg_loss_dict
                        else:
                            best_loss_dict = {
                                k: min(best_loss_dict[k], expavg_loss_dict[k])
                                for k in best_loss_dict.keys()
                            }

                    if rank == 0:
                        duration = time.time() - start_time
                        if not is_overflow:
                            average_loss = rolling_loss.process(reduced_loss)
                            tqdm.write(
                                "{} [Train_loss:{:.4f} Avg:{:.4f}] [Grad Norm {:.4f}] "
                                "[{:.2f}s/it] [{:.3f}s/file] [{:.7f} LR] [{} LS]"
                                .format(iteration, reduced_loss, average_loss,
                                        grad_norm, duration,
                                        (duration /
                                         (hparams.batch_size * n_gpus)),
                                        learning_rate, round(loss_scale)))
                            logger.log_training(reduced_loss_dict,
                                                expavg_loss_dict,
                                                best_loss_dict, grad_norm,
                                                learning_rate, duration,
                                                iteration)
                        else:
                            tqdm.write("Gradient Overflow, Skipping Step")
                        start_time = time.time()

                    if not is_overflow and (
                        (iteration %
                         (hparams.iters_per_checkpoint / 1) == 0) or
                        (os.path.exists(save_file_check_path))):
                        # save model checkpoint like normal
                        if rank == 0:
                            checkpoint_path = os.path.join(
                                output_directory,
                                "checkpoint_{}".format(iteration))
                            save_checkpoint(model, optimizer, learning_rate,
                                            iteration, hparams,
                                            best_validation_loss, average_loss,
                                            best_val_loss_dict, best_loss_dict,
                                            speaker_lookup, checkpoint_path)

                    if not is_overflow and (
                        (iteration % int(validation_interval) == 0) or
                        (os.path.exists(save_file_check_path)) or
                        (iteration < 1000 and (iteration % 250 == 0))):
                        if rank == 0 and os.path.exists(save_file_check_path):
                            os.remove(save_file_check_path)
                        # perform validation and save "best_model" depending on validation loss
                        val_loss, best_val_loss_dict = validate(
                            model, criterion, valset, loss_scalars,
                            best_val_loss_dict, iteration,
                            hparams.val_batch_size, n_gpus, collate_fn, logger,
                            hparams.distributed_run,
                            rank)  #validate (0.8 forcing)
                        if use_scheduler:
                            scheduler.step(val_loss)
                        if (val_loss < best_validation_loss):
                            best_validation_loss = val_loss
                            if rank == 0:
                                checkpoint_path = os.path.join(
                                    output_directory, "best_model")
                                save_checkpoint(model, optimizer,
                                                learning_rate, iteration,
                                                hparams, best_validation_loss,
                                                average_loss,
                                                best_val_loss_dict,
                                                best_loss_dict, speaker_lookup,
                                                checkpoint_path)

                    iteration += 1
                    # end of iteration loop
                # end of epoch loop
            training = False  # exit the While loop

        #except Exception as ex: # print Exception and continue from checkpoint. (turns out it takes < 4 seconds to restart like this, f*****g awesome)
        except LossExplosion as ex:  # print Exception and continue from checkpoint. (turns out it takes < 4 seconds to restart like this, f*****g awesome)
            print(ex)  # print Loss
            checkpoint_path = os.path.join(output_directory, "best_model")
            assert os.path.exists(
                checkpoint_path
            ), "best_model checkpoint must exist for automatic restarts"

            if hparams.fp16_run:
                amp._amp_state.loss_scalers[0]._loss_scale = 32768

            # clearing VRAM for load checkpoint
            model.zero_grad()
            x = y = y_pred = loss = len_loss = loss_z = loss_w = loss_s = loss_att = dur_loss_z = dur_loss_w = dur_loss_s = None
            torch.cuda.empty_cache()

            model.eval()
            model, optimizer, _learning_rate, iteration, best_validation_loss, saved_lookup = load_checkpoint(
                checkpoint_path, model, optimizer)
            learning_rate = optimizer.param_groups[0]['lr']
            epoch_offset = max(0, int(iteration / len(train_loader)))
            model.train()
            checkpoint_iter = iteration
            iteration += 1
            n_restarts += 1
def train(
    task: str = Task.AgeC,  # 수행할 태스크(분류-메인 태스크, 마스크 상태, 연령대, 성별, 회귀-나이)
    model_type: str = Config.VanillaEfficientNet,  # 불러올 모델명
    load_state_dict: str = None,  # 학습 이어서 할 경우 저장된 파라미터 경로
    train_root: str = Config.TrainS,  # 데이터 경로
    valid_root: str = Config.ValidS,
    transform_type: str = Aug.BaseTransform,  # 적용할 transform
    epochs: int = Config.Epochs,
    batch_size: int = Config.Batch32,
    optim_type: str = Config.Adam,
    loss_type: str = Loss.CE,
    lr: float = Config.LRBase,
    lr_scheduler: str = Config.CosineScheduler,
    save_path: str = Config.ModelPath,
    seed: int = Config.Seed,
):
    set_seed(seed)
    trainloader = get_dataloader(task, "train", train_root, transform_type, batch_size)
    validloader = get_dataloader(
        task, "valid", valid_root, transform_type, 1024, shuffle=False, drop_last=False
    )

    model = load_model(model_type, task, load_state_dict)
    model.cuda()
    model.train()

    optimizer = get_optim(model, optim_type=optim_type, lr=lr)
    criterion = get_criterion(loss_type=loss_type, task=task)

    if lr_scheduler is not None:
        scheduler = get_scheduler(scheduler_type=lr_scheduler, optimizer=optimizer)

    best_f1 = 0

    if task != Task.Age:  # classification(main, ageg, mask, gender)
        for epoch in range(epochs):
            print(f"Epoch: {epoch}")

            # F1, ACC
            pred_list = []
            true_list = []

            # CE Loss
            total_loss = 0
            num_samples = 0

            for idx, (imgs, labels) in tqdm(enumerate(trainloader), desc="Train"):
                imgs = imgs.cuda()
                labels = labels.cuda()

                output = model(imgs)
                loss = criterion(output, labels)
                _, preds = torch.max(output.data, dim=1)

                pred_list.append(preds.data.cpu().numpy())
                true_list.append(labels.data.cpu().numpy())

                total_loss += loss
                num_samples += imgs.size(0)

                optimizer.zero_grad()
                loss.backward()
                optimizer.step()
                if lr_scheduler is not None:
                    scheduler.step()

                train_loss = total_loss / num_samples

                pred_arr = np.hstack(pred_list)
                true_arr = np.hstack(true_list)
                train_acc = (true_arr == pred_arr).sum() / len(true_arr)
                train_f1 = f1_score(y_true=true_arr, y_pred=pred_arr, average="macro")

                if epoch == 0:  # logs during just first epoch

                    wandb.log(
                        {
                            f"Ep{epoch:0>2d} Train F1": train_f1,
                            f"Ep{epoch:0>2d} Train ACC": train_acc,
                            f"Ep{epoch:0>2d} Train Loss": train_loss,
                        }
                    )

                if idx != 0 and idx % VALID_CYCLE == 0:
                    valid_f1, valid_acc, valid_loss = validate(
                        task, model, validloader, criterion
                    )

                    print(
                        f"[Valid] F1: {valid_f1:.4f} ACC: {valid_acc:.4f} Loss: {valid_loss:.4f}"
                    )
                    print(
                        f"[Train] F1: {train_f1:.4f} ACC: {train_acc:.4f} Loss: {train_loss:.4f}"
                    )
                    if epoch == 0:
                        # logs during one epoch
                        wandb.log(
                            {
                                f"Ep{epoch:0>2d} Valid F1": valid_f1,
                                f"Ep{epoch:0>2d} Valid ACC": valid_acc,
                                f"Ep{epoch:0>2d} Valid Loss": valid_loss,
                            }
                        )

            # logs for one epoch in total
            wandb.log(
                {
                    "Train F1": train_f1,
                    "Valid F1": valid_f1,
                    "Train ACC": train_acc,
                    "Valid ACC": valid_acc,
                    "Train Loss": train_loss,
                    "Valid Loss": valid_loss,
                }
            )

            if save_path and valid_f1 >= best_f1:
                name = f"{model_type}_task({task})ep({epoch:0>2d})f1({valid_f1:.4f})bs({batch_size})loss({valid_loss:.4f})lr({lr})trans({transform_type})optim({optim_type})crit({loss_type})seed({seed}).pth"
                best_f1 = valid_f1
                torch.save(model.state_dict(), os.path.join(save_path, name))

    # regression(age)
    else:
        for epoch in range(epochs):
            print(f"Epoch: {epoch}")

            pred_list = []
            true_list = []

            mse_raw = 0
            rmse_raw = 0
            num_samples = 0

            for idx, (imgs, labels) in tqdm(enumerate(trainloader), desc="Train"):
                imgs = imgs.cuda()

                # regression(age)
                labels_reg = labels.float().cuda()
                output = model(imgs)
                loss = criterion(output, labels_reg.unsqueeze(1))

                mse_raw += loss.item() * len(labels_reg)
                rmse_raw += loss.item() * len(labels_reg)
                num_samples += len(labels_reg)

                # backward
                optimizer.zero_grad()
                loss.backward()
                optimizer.step()
                scheduler.step()

                # classification(ageg)
                labels_clf = age2ageg(labels.data.numpy())
                preds_clf = age2ageg(output.data.cpu().numpy().flatten())
                pred_list.append(preds_clf)
                true_list.append(labels_clf)

                train_rmse = math.sqrt(rmse_raw / num_samples)
                train_mse = mse_raw / num_samples

                # eval for clf(ageg)
                pred_arr = np.hstack(pred_list)
                true_arr = np.hstack(true_list)

                train_acc = (true_arr == pred_arr).sum() / len(true_arr)
                train_f1 = f1_score(y_true=true_arr, y_pred=pred_arr, average="macro")

                # logs during one epoch
                # wandb.log(
                #     {
                #         f"Ep{epoch:0>2d} Train F1": train_f1,
                #         f"Ep{epoch:0>2d} Train ACC": train_acc,
                #         f"Ep{epoch:0>2d} Train RMSE": train_rmse,
                #         f"Ep{epoch:0>2d} Train MSE": train_mse,
                #     }
                # )

                if idx != 0 and idx % VALID_CYCLE == 0:
                    valid_f1, valid_acc, valid_rmse, valid_mse = validate(
                        task, model, validloader, criterion
                    )
                    print(
                        f"[Valid] F1: {valid_f1:.4f} ACC: {valid_acc:.4f} RMSE: {valid_rmse:.4f} MSE: {valid_mse:.4f}"
                    )
                    print(
                        f"[Train] F1: {train_f1:.4f} ACC: {train_acc:.4f} RMSE: {train_rmse:.4f} MSE: {train_mse:.4f}"
                    )
                    # wandb.log(
                    #     {
                    #         "Valid F1": valid_f1,
                    #         "Valid ACC": valid_acc,
                    #         "Valid RMSE": valid_rmse,
                    #         "Valid MSE": valid_mse,
                    #     }
                    # )
            wandb.log(
                {
                    "Train F1": train_f1,
                    "Valid F1": valid_f1,
                    "Train ACC": train_acc,
                    "Valid ACC": valid_acc,
                    "Train RMSE": train_rmse,
                    "Valid RMSE": valid_rmse,
                    "Train MSE": train_mse,
                    "Valid MSE": valid_mse,
                }
            )

            if save_path:
                name = f"{model_type}_task({task})ep({epoch:0>2d})f1({valid_f1:.4f})bs({batch_size})loss({valid_mse:.4f})lr({lr})trans({transform_type})optim({optim_type})crit({loss_type})seed({seed}).pth"
                torch.save(model.state_dict(), os.path.join(save_path, name))
Example #22
0
    audio_norm = audio.unsqueeze(0)
    audio_norm = torch.autograd.Variable(audio_norm, requires_grad=False)
    melspec = stft.mel_spectrogram(audio_norm)
    melspec = melspec.cuda()
    return melspec


hparams = create_hparams()

stft = TacotronSTFT(hparams.filter_length, hparams.hop_length,
                    hparams.win_length, hparams.n_mel_channels,
                    hparams.sampling_rate, hparams.mel_fmin, hparams.mel_fmax)

# Load Models
checkpoint_path = "pretrained_models/mellotron_libritts.pt"
mellotron = load_model(hparams).cuda().eval()
mellotron.load_state_dict(torch.load(checkpoint_path)['state_dict'])

waveglow_path = '/home/keon/contextron/pretrained_models/waveglow_256channels_universal_v5.pt'
waveglow = torch.load(waveglow_path)['model'].cuda().eval()
denoiser = Denoiser(waveglow).cuda().eval()

# Setup dataloaders
arpabet_dict = cmudict.CMUDict('data/cmu_dictionary')
audio_paths = 'data/refs/refs.txt'  # 'data/refs/refs.txt'
dataloader = TextMelLoader(audio_paths, hparams)
datacollate = TextMelCollate(1)

#Load data
file_idx = 0
audio_path, text, sid = dataloader.audiopaths_and_text[file_idx]
def train_cv(
    task: str = Task.AgeC,  # 수행할 태스크(분류-메인 태스크, 마스크 상태, 연령대, 성별, 회귀-나이)
    model_type: str = Config.VanillaEfficientNet,  # 불러올 모델명
    load_state_dict: str = None,  # 학습 이어서 할 경우 저장된 파라미터 경로
    train_root: str = Config.TrainS,  # 데이터 경로
    valid_root: str = Config.ValidS,
    transform_type: str = Aug.BaseTransform,  # 적용할 transform
    age_filter: int=58,
    epochs: int = Config.Epochs,
    cv: int = 5,
    batch_size: int = Config.Batch32,
    optim_type: str = Config.Adam,
    loss_type: str = Loss.CE,
    lr: float = Config.LRBase,
    lr_scheduler: str = Config.CosineScheduler,
    save_path: str = Config.ModelPath,
    seed: int = Config.Seed,
):
    if save_path:
        kfold_dir = f"kfold_{model_type}_" + get_timestamp()
        if kfold_dir not in os.listdir(save_path):
            os.mkdir(os.path.join(save_path, kfold_dir))
        print(f'Models will be saved in {os.path.join(save_path, kfold_dir)}.')

    set_seed(seed)
    transform = configure_transform(phase="train", transform_type=transform_type)
    trainset = TrainDataset(root=train_root, transform=transform, task=task, age_filter=age_filter, meta_path=Config.Metadata)
    validloader = get_dataloader(
        task, "valid", valid_root, transform_type, 1024, shuffle=False, drop_last=False
    )

    kfold = KFold(n_splits=cv, shuffle=True)

    for fold_idx, (train_indices, _) in enumerate(
        kfold.split(trainset)
    ):  # 앙상블이 목적이므로 test 인덱스는 따로 사용하지 않고, validloader를 통해 성능 검증
        if fold_idx == 0 or fold_idx == 1 or fold_idx == 2 or fold_idx == 3: continue
        print(f"Train Fold #{fold_idx}")
        train_sampler = SubsetRandomSampler(train_indices)
        trainloader = DataLoader(
            trainset, batch_size=batch_size, sampler=train_sampler, drop_last=True
        )

        model = load_model(model_type, task, load_state_dict)
        model.cuda()
        model.train()

        optimizer = get_optim(model, optim_type=optim_type, lr=lr)
        criterion = get_criterion(loss_type=loss_type, task=task)

        if lr_scheduler is not None:
            scheduler = get_scheduler(scheduler_type=lr_scheduler, optimizer=optimizer)

        best_f1 = 0

        if task != Task.Age:  # classification(main, ageg, mask, gender)
            for epoch in range(epochs):
                print(f"Epoch: {epoch}")

                # F1, ACC
                pred_list = []
                true_list = []

                # CE Loss
                total_loss = 0
                num_samples = 0

                for idx, (imgs, labels) in tqdm(enumerate(trainloader), desc="Train"):
                    imgs = imgs.cuda()
                    labels = labels.cuda()

                    output = model(imgs)
                    loss = criterion(output, labels)
                    _, preds = torch.max(output.data, dim=1)

                    pred_list.append(preds.data.cpu().numpy())
                    true_list.append(labels.data.cpu().numpy())

                    total_loss += loss
                    num_samples += imgs.size(0)

                    optimizer.zero_grad()
                    loss.backward()
                    optimizer.step()
                    if lr_scheduler is not None:
                        scheduler.step()

                    train_loss = total_loss / num_samples

                    pred_arr = np.hstack(pred_list)
                    true_arr = np.hstack(true_list)
                    train_acc = (true_arr == pred_arr).sum() / len(true_arr)
                    train_f1 = f1_score(
                        y_true=true_arr, y_pred=pred_arr, average="macro"
                    )

                    if epoch == 0:  # logs during just first epoch

                        wandb.log(
                            {
                                f"Fold #{fold_idx} Ep{epoch:0>2d} Train F1": train_f1,
                                f"Fold #{fold_idx} Ep{epoch:0>2d} Train ACC": train_acc,
                                f"Fold #{fold_idx} Ep{epoch:0>2d} Train Loss": train_loss,
                            }
                        )

                    if idx != 0 and idx % VALID_CYCLE == 0:
                        valid_f1, valid_acc, valid_loss = validate(
                            task, model, validloader, criterion
                        )

                        print(
                            f"[Valid] F1: {valid_f1:.4f} ACC: {valid_acc:.4f} Loss: {valid_loss:.4f}"
                        )
                        print(
                            f"[Train] F1: {train_f1:.4f} ACC: {train_acc:.4f} Loss: {train_loss:.4f}"
                        )
                        if epoch == 0:
                            # logs during one epoch
                            wandb.log(
                                {
                                    f"Fold #{fold_idx} Ep{epoch:0>2d} Valid F1": valid_f1,
                                    f"Fold #{fold_idx} Ep{epoch:0>2d} Valid ACC": valid_acc,
                                    f"Fold #{fold_idx} Ep{epoch:0>2d} Valid Loss": valid_loss,
                                }
                            )

                # logs for one epoch in total
                wandb.log(
                    {
                        f"Fold #{fold_idx} Train F1": train_f1,
                        f"Fold #{fold_idx} Valid F1": valid_f1,
                        f"Fold #{fold_idx} Train ACC": train_acc,
                        f"Fold #{fold_idx} Valid ACC": valid_acc,
                        f"Fold #{fold_idx} Train Loss": train_loss,
                        f"Fold #{fold_idx} Valid Loss": valid_loss,
                    }
                )

                if save_path and valid_f1 >= best_f1:
                    name = f"Fold{fold_idx:0>2d}_{model_type}_task({task})ep({epoch:0>2d})f1({valid_f1:.4f})bs({batch_size})loss({valid_loss:.4f})lr({lr})trans({transform_type})optim({optim_type})crit({loss_type})seed({seed}).pth"
                    best_f1 = valid_f1
                    torch.save(
                        model.state_dict(), os.path.join(save_path, kfold_dir, name)
                    )

        # regression(age)
        else:
            for epoch in range(epochs):
                print(f"Epoch: {epoch}")

                pred_list = []
                true_list = []

                mse_raw = 0
                rmse_raw = 0
                num_samples = 0

                for idx, (imgs, labels) in tqdm(enumerate(trainloader), desc="Train"):
                    imgs = imgs.cuda()

                    # regression(age)
                    labels_reg = labels.float().cuda()
                    output = model(imgs)
                    loss = criterion(output, labels_reg.unsqueeze(1))

                    mse_raw += loss.item() * len(labels_reg)
                    rmse_raw += loss.item() * len(labels_reg)
                    num_samples += len(labels_reg)

                    # backward
                    optimizer.zero_grad()
                    loss.backward()
                    optimizer.step()
                    scheduler.step()

                    # classification(ageg)
                    labels_clf = age2ageg(labels.data.numpy())
                    preds_clf = age2ageg(output.data.cpu().numpy().flatten())
                    pred_list.append(preds_clf)
                    true_list.append(labels_clf)

                    train_rmse = math.sqrt(rmse_raw / num_samples)
                    train_mse = mse_raw / num_samples

                    # eval for clf(ageg)
                    pred_arr = np.hstack(pred_list)
                    true_arr = np.hstack(true_list)

                    train_acc = (true_arr == pred_arr).sum() / len(true_arr)
                    train_f1 = f1_score(
                        y_true=true_arr, y_pred=pred_arr, average="macro"
                    )

                    if idx != 0 and idx % VALID_CYCLE == 0:
                        valid_f1, valid_acc, valid_rmse, valid_mse = validate(
                            task, model, validloader, criterion
                        )
                        print(
                            f"[Valid] F1: {valid_f1:.4f} ACC: {valid_acc:.4f} RMSE: {valid_rmse:.4f} MSE: {valid_mse:.4f}"
                        )
                        print(
                            f"[Train] F1: {train_f1:.4f} ACC: {train_acc:.4f} RMSE: {train_rmse:.4f} MSE: {train_mse:.4f}"
                        )

                wandb.log(
                    {
                        f"Fold #{fold_idx} Train F1": train_f1,
                        f"Fold #{fold_idx} Valid F1": valid_f1,
                        f"Fold #{fold_idx} Train ACC": train_acc,
                        f"Fold #{fold_idx} Valid ACC": valid_acc,
                        f"Fold #{fold_idx} Train RMSE": train_rmse,
                        f"Fold #{fold_idx} Valid RMSE": valid_rmse,
                        f"Fold #{fold_idx} Train MSE": train_mse,
                        f"Fold #{fold_idx} Valid MSE": valid_mse,
                    }
                )

                if save_path:
                    name = f"Fold{fold_idx:0>2d}_{model_type}_task({task})ep({epoch:0>2d})f1({valid_f1:.4f})bs({batch_size})loss({valid_mse:.4f})lr({lr})trans({transform_type})optim({optim_type})crit({loss_type})seed({seed}).pth"
                    torch.save(
                        model.state_dict(), os.path.join(save_path, kfold_dir, name)
                    )
        model.cpu()
    return "\n\n".join("\n".join(p) for p in paragraphs)


if __name__ == "__main__":
    bookshelf = Bookshelf((
        "/dataset_archive/gutenburg/pgsfcd-032007/data/",
    ))
    book_title = "Edison's Conquest of Mars"
    book = find_book(bookshelf, book_title)
    words_per_chapter = math.ceil(50000.0 / len(book))
    print("Generating {} words per chapter for the {} chapters of '{}'".
          format(words_per_chapter, len(book), book_title))

    print("Loading Model")
    if 'char_model' not in globals():
        char_model = model.load_model("language_model")
    diversities = (0.4, 0.5, 0.6)
    punct = '".!?\'\n'
    max_iter = words_per_chapter * 15
    generated_chapters = []
    for i, chapter in enumerate(book.chapters):
        title = chapter['title']
        chapter_data = {}
        seed = ''
        for paragraph in chapter['content']:
            seed += paragraph[:model.maxlen]
            if len(seed) >= model.maxlen:
                break
        print("\nSeed: ", seed)
        text_generator = model.sample_model2(
            char_model,
    category = args.category

    manualSeed = random.randint(1, 10000)
    random.seed(manualSeed)
    torch.manual_seed(manualSeed)
    torch.cuda.manual_seed_all(manualSeed)

    source, target = analysis_dir(category)
    SourceTrainDataLoader, SourceTestDataLoader = load_data(source, batch_size)
    TargetTrainDataLoader, TargetTestDataLoader = load_data(target, batch_size)
    transform = transforms.Compose([
        transforms.ToPILImage(),
        transforms.Grayscale(num_output_channels=3),
        transforms.Pad(padding=2),
        transforms.ToTensor(),
        transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
    ])
    netG = load_model(category).to(device)
    optimizerG = optim.Adam(netG.parameters(), lr=lrG)
    loss_class = torch.nn.CrossEntropyLoss()
    loss_domain = torch.nn.CrossEntropyLoss()

    exp = 'check'
    trainLogger = open('./%s/%s/train.log' % (exp, category), 'w')
    validLogger = open('./%s/%s/valid.log' % (exp, category), 'w')
    targetLogger = open('./%s/%s/target.log' % (exp, category), 'w')
    train()
    trainLogger.close()
    validLogger.close()
    targetLogger.close()
Example #26
0
    model_path = config.model_path
    result_path = config.result_path

    parser = argparse.ArgumentParser()
    parser.add_argument('-i', '--input', help='input path')
    parser.add_argument('-o', '--output', help='output path')
    parser.add_argument('-m',
                        '--model',
                        help='model path',
                        default='CNN_p10_e2000.h5')
    args = parser.parse_args()

    data_filename = args.input
    data_path = os.path.join(data_root, data_filename)

    result_filename = args.output if args.output else '{}_detect_by_heatmap.nii'.format(
        data_filename.split('/')[-1].split('.')[-2])

    result_path = os.path.join(result_path, result_filename)

    model_name = args.model
    unet_path = os.path.join(model_path, 'unet_pm25_yuzq.hdf5')
    clf_path = os.path.join(model_path, model_name)
    bet_net = unet(pretrained_weights=unet_path)
    model = load_model(clf_path)

    mask, mask_roi = detect_file(data_path, model, bet_net)

    nib.save(mask, result_path)
    nib.save(mask_roi, './mask/roi_by_heatmap.nii')
Example #27
0
train_data_dir = FLAGS.data_dir
image_filelist = []
for file in os.listdir(train_data_dir + '/image/'):
    if FLAGS.image_filename in file:
        image_filelist.append(os.path.join(train_data_dir, 'image', file))
print(image_filelist)

num_labels = 3
num_channels = 1
input_shape = (None, None, None, num_channels)
output_shape = (None, None, None, num_labels)
model = load_model(input_shape=input_shape,
                   num_labels=num_labels,
                   base_filter=32,
                   depth_size=3,
                   se_res_block=True,
                   se_ratio=16,
                   last_relu=False)
print(model)

model.compile(optimizer=Adam(lr=1e-4),
              loss=average_dice_coef_loss,
              metrics=[average_dice_coef])

df = MyDataFlow(train_data_dir,
                FLAGS.image_filename,
                FLAGS.label_filename,
                shuffle=True)
df = MapDataComponent(df, process.simple_preprocess_img, index=0)
df = MapDataComponent(df, process.sample_z_norm, index=0)
Example #28
0
import torch

from model import load_model, move_model_to_GPU
from dataset import create_data_loader

##############
model_num = 0
##############

test_loader = create_data_loader("test")

model = load_model("model_" + str(model_num) + "_epoch_219", isTrain=True) # Optimize et
#model = model.netG
#model.eval()
#print(model.loss_history_G)

model, device = move_model_to_GPU(model)

print("Testing the model...")

for iter, batch in enumerate(test_loader):
	batch = batch.to(device)

	with torch.no_grad():
		model.set_input(batch)
		model.forward()
		model.save_images(iter, len(batch))

print("Testing done.")
Example #29
0
config.gpu_options.allow_growth = True

parser = argparse.ArgumentParser(description='Calculate model adversarial lower bound by PGD')
parser.add_argument('--dataset')
parser.add_argument('--eps', type=float)
parser.add_argument('--model')
parser.add_argument('--attack')
parser.add_argument('--batch_size', type=int, default=128)
parser.add_argument('--cuda_ids', type=int, default=0)
if __name__ == '__main__':
    args = parser.parse_args()

    os.environ['CUDA_VISIBLE_DEVICES'] = str(args.cuda_ids)

    # load model
    model = load_model(args.model)

    eps = args.eps
    if args.dataset == 'mnist':
        mean = normalization['mnist'][0]
        std = [normalization['mnist'][1] for _ in mean]
        x = input_shape['mnist']
        x_op = tf.placeholder(tf.float32, shape=(None, x[0], x[1], x[2],))
    elif args.dataset == 'cifar':
        mean = normalization['cifar'][0]
        std = [normalization['cifar'][1] for _ in mean]
        x = input_shape['cifar']
        x_op = tf.placeholder(tf.float32, shape=(None, x[0], x[1], x[2],))
    train_loader, test_loader = data_loaders(args.dataset, args.batch_size,
                                             shuffle_test=False, norm_mean=mean, norm_std=std)
from selective_search import find_region_proposals
import cv2
import numpy as np
from model import initialize_model, load_model
from dataset import prepare_image
import torch
import matplotlib.pyplot as plt
import torchvision
from tqdm import tqdm

NUM_CLASSES = 21
image_size = (224, 224)
MODEL_PATH = './checkpoints/regression 12-03-2021 22-38-46 epoch-2.pth'
model = initialize_model(NUM_CLASSES)
model, _, _ = load_model(model, MODEL_PATH)
DEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'
model = model.to(DEVICE)
print("Model Loaded...")
for param in model.parameters():
    param.requires_grad = False
torch.no_grad()
class_list = [
    'background', 'person', 'bird', 'cat', 'cow', 'dog', 'horse', 'sheep',
    'aeroplane', 'bicycle', 'boat', 'bus', 'car', 'motorbike', 'train',
    'bottle', 'chair', 'diningtable', 'pottedplant', 'sofa', 'tvmonitor'
]


def draw_boxes(image, boxes):
    new_image = image.copy()
    for box in boxes:
Example #31
0
def train(output_directory, log_directory, checkpoint_path, warm_start, n_gpus,
          rank, group_name, hparams):
    """Training and validation logging results to tensorboard and stdout

    Params
    ------
    output_directory (string): directory to save checkpoints
    log_directory (string) directory to save tensorboard logs
    checkpoint_path(string): checkpoint path
    n_gpus (int): number of gpus
    rank (int): rank of current gpu
    hparams (object): comma separated list of "name=value" pairs.
    """
    if hparams.distributed_run:
        init_distributed(hparams, n_gpus, rank, group_name)

    torch.manual_seed(hparams.seed)
    # torch.cuda.manual_seed(hparams.seed)

    model = load_model(hparams)
    learning_rate = hparams.learning_rate
    optimizer = torch.optim.Adam(model.parameters(),
                                 lr=learning_rate,
                                 weight_decay=hparams.weight_decay)

    if hparams.fp16_run:
        from apex import amp
        model, optimizer = amp.initialize(model, optimizer, opt_level='O2')

    if hparams.distributed_run:
        model = apply_gradient_allreduce(model)

    criterion = Tacotron2Loss()

    logger = prepare_directories_and_logger(output_directory, log_directory,
                                            rank)

    train_loader, valset, collate_fn, train_sampler = prepare_dataloaders(
        hparams)

    # Load checkpoint if one exists
    iteration = 0
    epoch_offset = 0
    if checkpoint_path is not None:
        if warm_start:
            model = warm_start_model(checkpoint_path, model,
                                     hparams.ignore_layers)
        else:
            model, optimizer, _learning_rate, iteration = load_checkpoint(
                checkpoint_path, model, optimizer)
            if hparams.use_saved_learning_rate:
                learning_rate = _learning_rate
            iteration += 1  # next iteration is iteration + 1
            epoch_offset = max(0, int(iteration / len(train_loader)))

    model.train()
    is_overflow = False
    # ================ MAIN TRAINNIG LOOP! ===================
    for epoch in range(epoch_offset, hparams.epochs):
        print("Epoch: {}".format(epoch))
        if train_sampler is not None:
            train_sampler.set_epoch(epoch)
        for i, batch in enumerate(train_loader):
            start = time.perf_counter()
            if iteration > 0 and iteration % hparams.learning_rate_anneal == 0:
                learning_rate = max(hparams.learning_rate_min,
                                    learning_rate * 0.5)
                for param_group in optimizer.param_groups:
                    param_group['lr'] = learning_rate

            model.zero_grad()
            x, y = model.parse_batch(batch)
            y_pred = model(x)

            loss = criterion(y_pred, y)
            if hparams.distributed_run:
                reduced_loss = reduce_tensor(loss.data, n_gpus).item()
            else:
                reduced_loss = loss.item()

            if hparams.fp16_run:
                with amp.scale_loss(loss, optimizer) as scaled_loss:
                    scaled_loss.backward()
            else:
                loss.backward()

            if hparams.fp16_run:
                grad_norm = torch.nn.utils.clip_grad_norm_(
                    amp.master_params(optimizer), hparams.grad_clip_thresh)
                is_overflow = math.isnan(grad_norm)
            else:
                grad_norm = torch.nn.utils.clip_grad_norm_(
                    model.parameters(), hparams.grad_clip_thresh)

            optimizer.step()

            if not is_overflow and rank == 0:
                duration = time.perf_counter() - start
                print(
                    "Train loss {} {:.6f} Grad Norm {:.6f} {:.2f}s/it".format(
                        iteration, reduced_loss, grad_norm, duration))
                logger.log_training(reduced_loss, grad_norm, learning_rate,
                                    duration, iteration)

            if not is_overflow and (iteration % hparams.iters_per_checkpoint
                                    == 0):
                validate(model, criterion, valset, iteration,
                         hparams.batch_size, n_gpus, collate_fn, logger,
                         hparams.distributed_run, rank)
                if rank == 0:
                    checkpoint_path = os.path.join(
                        output_directory, "checkpoint_{}".format(iteration))
                    save_checkpoint(model, optimizer, learning_rate, iteration,
                                    checkpoint_path)

            iteration += 1
from video_interpolation_utilities import *
from model import load_model
import config as config
import glob
import os
import numpy as np
from cv2 import imread,imwrite
import shutil
if __name__ == "__main__":
    loaded_model = load_model('./pre-trained-models/model_adaSepConv.json',"./pre-trained-models/model_adaSepConv.h5")

    videofile = input("enter path to video file : ")

    print("Writing video frames in folder  .....")
    fps = capture_frames_from_video(videofile)
    print("Written all frames successfully ...")
    print("Interpolating frames please wait ...")
    video_filename  = os.path.basename(os.path.splitext(videofile)[0])
    print(video_filename)
    create_interpolated_frames(glob.glob("./frames_"+ video_filename+"/*"),loaded_model,config.PREDICTION_BATCH,video_filename)
    
    convert_frames_to_video(sorted(glob.glob("./frames_"+ video_filename+"/*")),'./'+video_filename+'.avi',fps*2)
    shutil.rmtree("./frames_"+ video_filename)
    print("Video generated with double FPS  successfully please check current directory")
def remove_disk_cache(obj):
    '''Recusively removes disk caches from the object.'''
    for name, val in obj.__dict__.items():
        if isinstance(val, features.DiskCachedFeatures):
            obj.__dict__[name] = (
                features.MemCachedFeatures.create_shared_cache(
                val.feature_generator))
        else:
            try:
                remove_disk_cache(val)
            except AttributeError:
                pass

    return obj
    

if __name__ == '__main__':
    parser = OptionParser()

    parser.add_option('--output', '-o', default='neon.model',
                      help='File to output the model definition')
    parser.add_option('--input', '-i', default='neon.model',
                      help='File to input the model definition')
    
    options, args = parser.parse_args()

    model.save_model(
        remove_disk_cache(model.load_model(options.input)),
        options.output)
Example #34
0
"""

# API Dependencies
import pickle
import json
import numpy as np
from model import load_model, make_prediction
from flask import Flask, request, jsonify

# Application definition
app = Flask(__name__)

# Load our model into memory.
# Please update this path to reflect your own trained model.
static_model = load_model(path_to_model='assets/trained-models/xgbmodel.pkl')

print('-' * 40)
print('Model succesfully loaded')
print('-' * 40)


# Define the API's interface.
# Here the 'model_prediction()' function will be called when a POST request
# is sent to our interface located at:
# http:{Host-machine-ip-address}:5000/api_v0.1
@app.route('/api_v0.1', methods=['POST'])
def model_prediction():
    # We retrieve the data payload of the POST request
    data = request.get_json(force=True)
    # We then preprocess our data, and use our pretrained model to make a
Example #35
0
""" Vietnamese Accent Predictor demo """
import io
import os

import hug

import model

MODEL_DIR = os.environ.get('MODELS_DIR', 'models')
MODEL_VERSION = os.environ.get('MODEL_VERSION', 'v2')
the_model = model.load_model(os.path.join(MODEL_DIR, MODEL_VERSION))

with io.open('static/index.html', encoding='utf-8') as f:
    index_page = f.read()


@hug.request_middleware()
def enable_cors_all(request, response):
    response.set_header('Access-Control-Allow-Origin', '*')


@hug.get('/', output=hug.output_format.html)
def index():
    """ Demo homepage """
    return index_page


@hug.get(examples='text=co%20gai%20den%20tu%20hom%20qua')
def accented(text: hug.types.text, hug_timer=3):
    """ Add accent to given plain text """
    return {
Example #36
0
"""

# API Dependencies
import pickle
import json
import numpy as np
from model import load_model, make_prediction
from flask import Flask, request, jsonify

# Application definition
app = Flask(__name__)

# Load our model into memory.
# Please update this path to reflect your own trained model.
static_model = load_model(path_to_model='assets/trained-models/rfmodel_2.pkl')
print('-' * 40)
print('Model succesfully loaded')
print('-' * 40)


# Define the API's interface.
# Here the 'model_prediction()' function will be called when a POST request
# is sent to our interface located at:
# http:{Host-machine-ip-address}:5000/api_v0.1
@app.route('/api_v0.1', methods=['POST'])
def model_prediction():
    # We retrieve the data payload of the POST request
    data = request.get_json(force=True)
    # We then preprocess our data, and use our pretrained model to make a
    # prediction.
Example #37
0
def call_method(choice):
    #global variables
    global data, data10to14, data15to16, analysis_data, community_area, primary_type, data_enriched_dist, X, primaryType_onehot, test_data_X, primaryType_onehot_test
    global list_of_lr_models, list_of_nn_models, list_of_knn_models, list_of_dt_models, list_of_nb_models
    global data10to16, data15to16_enriched_dist
    if (choice == 1):
        #Reads in the data
        data = dec.read_data(dec.query, dec.conn)
    elif (choice == 2):
        #Runs preliminary analysis and totals
        dec.time_series_graph_month(data)
        dec.time_series_graph_year(data)
        dec.table_primary_type_volume(data)
        dec.table_primary_type_volume05to09(data)
        dec.table_primary_type_volume10to14(data)
    elif (choice == 3):
        #Cuts down the dataset to just the years we are interested
        data10to14, community_area, primary_type = dec.cut_down_data(data)
    elif (choice == 4):
        #production of charts and analysis
        analysis_data = dec.get_analysis(data10to14)
        dec.chart_data(analysis_data, community_area)
        print(data10to14.shape)
        dec.time_series_community_area_graph_year(data10to14, community_area)
        dec.chart_primary_type(data10to14)
        dec.time_series_year_primary_type(data10to14)
        dec.rolling_yearly_primary_type(data, community_area, primary_type)
        dec.calculate_percentage_arrests(data10to14, community_area)

    elif (choice == 5):
        #production of heat maps
        dec.heatmap_primary_type_location_desc(data10to14)
        dec.heatmap_year_community_area(data10to14)
        dec.heatmap_month_community_area(data10to14)
        dec.heatmap_weekday_community_area(data10to14)
        dec.heatmap_hour_community_area(data10to14)
        dec.heatmap_hour_primary_type(data10to14)
        dec.heatmap_month_primary_type(data10to14)
        dec.heatmap_weekday_primary_type(data10to14)

    elif (choice == 6):
        #Enrich the geographic data where the longitude / latitude values are null
        data_enriched = dec.geographical_data_analysis(data10to14, data10to14)
        data_enriched.shape
        data_enriched_dist = dec.calculate_distance_from_mean(data_enriched)
        data_enriched_dist.shape
    elif (choice == 7):
        #create the model data pre processing
        model_data = data_enriched_dist
        model_data = model_data.dropna()  #drop the 11 rows containing nulls
        model_data['District'] = model_data.District.astype(float)
        model_data['Ward'] = model_data.Ward.astype(float)
        model_data['Beat'] = model_data.Beat.astype(float)
        communityArea_onehot, month_onehot, weekday_onehot, domestic_onehot, distance_band_onehot, primaryType_onehot, arrest_onehot = m.generate_one_hot_data(
            model_data)
        X = m.append_one_hot_datasets(model_data, communityArea_onehot,
                                      month_onehot, weekday_onehot,
                                      distance_band_onehot, arrest_onehot)

    elif (choice == 8):
        #build logisitic regression models
        type_model = 'logisitic_regression_model'
        list_of_lr_models = m.primary_type_model(X, primary_type,
                                                 primaryType_onehot,
                                                 type_model)
        m.save_model(list_of_lr_models, type_model)
        m.save_test_results(type_model, list_of_lr_models)
        print(list_of_lr_models)
    elif (choice == 9):
        #build neural network models
        type_model = 'neural_network_model'
        list_of_nn_models = m.primary_type_model(X, primary_type,
                                                 primaryType_onehot,
                                                 type_model)
        m.save_model(list_of_nn_models, type_model)
        m.save_test_results(type_model, list_of_nn_models)
        print(list_of_nn_models)
    elif (choice == 10):
        #build K Nearest Neighbour models
        print('build started', datetime.datetime.now())
        type_model = 'knn_classifier_model'
        list_of_knn_models = m.primary_type_model(X, primary_type,
                                                  primaryType_onehot,
                                                  type_model)
        m.save_model(list_of_knn_models, type_model)
        m.save_test_results(type_model, list_of_knn_models)
        print(list_of_knn_models)
        print('build finished', datetime.datetime.now())
    elif (choice == 11):
        #Build Decision Tree models
        type_model = 'decision_tree_model'
        list_of_dt_models = m.primary_type_model(X, primary_type,
                                                 primaryType_onehot,
                                                 type_model)
        m.save_model(list_of_dt_models, type_model)
        m.save_test_results(type_model, list_of_dt_models)
        print(list_of_dt_models)
    elif (choice == 12):
        #Build naive bayes models
        type_model = 'naive_baye_model'
        list_of_nb_models = m.primary_type_model(X, primary_type,
                                                 primaryType_onehot,
                                                 type_model)
        m.save_model(list_of_nb_models, type_model)
        m.save_test_results(type_model, list_of_nb_models)
        print(list_of_nb_models)
    elif (choice == 13):
        #Extract data for 2015/16
        print('Step 1: Get Test Data 2015 and 2016')
        data15to16 = m.fetch_new_data(m.sql, m.conn)

        print('Data read in ', data15to16.shape)
        data15to16 = m.cut_down_test_data(data15to16, community_area,
                                          primary_type)
        print('cut down the dataset', data15to16.shape)

        data15to16_enriched = dec.geographical_data_analysis(
            data15to16, data10to14)
        print(data15to16_enriched.shape)

        print('Add the calculated distances to the dataset')
        data15to16_enriched_dist = dec.calculate_distance_from_mean(
            data15to16_enriched)
        print('Create the one hot datasets')
        communityArea_onehot_test, month_onehot_test, weekday_onehot_test, domestic_onehot_test, distance_band_onehot_test, primaryType_onehot_test, arrest_onehot_test = m.generate_one_hot_data(
            data15to16_enriched_dist)
        test_data_X = m.append_one_hot_datasets(data15to16_enriched_dist,
                                                communityArea_onehot_test,
                                                month_onehot_test,
                                                weekday_onehot_test,
                                                distance_band_onehot_test,
                                                arrest_onehot_test)
        print(len(test_data_X))

    elif (choice == 14):
        #Test Logistic Regression model
        print('14 running')
        type_model = 'logisitic_regression_model'
        model_score = m.test_new_data(list_of_lr_models, test_data_X,
                                      primary_type, primaryType_onehot_test)
        m.save_results(type_model, model_score)
    elif (choice == 15):
        #Test KNN model
        print('15 running')
        print('Read data started', datetime.datetime.now())
        type_model = 'knn_classifier_model'
        model_score = m.test_new_data(list_of_knn_models, test_data_X,
                                      primary_type, primaryType_onehot_test)
        m.save_results(type_model, model_score)
        print('Finished ', datetime.datetime.now())
    elif (choice == 16):
        #Test neural networks
        print('16 running')
        type_model = 'neural_network_model'
        model_score = m.test_new_data(list_of_nn_models, test_data_X,
                                      primary_type, primaryType_onehot_test)
        m.save_results(type_model, model_score)
    elif (choice == 17):
        #Test Decision Tree
        type_model = 'decision_tree_model'
        model_score = m.test_new_data(list_of_dt_models, test_data_X,
                                      primary_type, primaryType_onehot_test)
        m.save_results(type_model, model_score)
    elif (choice == 18):
        #Test naive baye model
        print('you chose 18')
        type_model = 'naive_baye_model'
        model_score = m.test_new_data(list_of_nb_models, test_data_X,
                                      primary_type, primaryType_onehot_test)
        m.save_results(type_model, model_score)
    elif (choice == 19):
        #load models from directory
        print('load models')

        list_of_lr_models = []
        type_model = 'logisitic_regression_model'
        list_of_lr_models = m.load_model(primary_type, type_model)

        list_of_nn_models = []
        type_model = 'neural_network_model'
        list_of_nn_models = m.load_model(primary_type, type_model)

        list_of_knn_models = []
        type_model = 'knn_classifier_model'
        list_of_knn_models = m.load_model(primary_type, type_model)

        list_of_dt_models = []
        type_model = 'decision_tree_model'
        list_of_dt_models = m.load_model(primary_type, type_model)

        list_of_nb_models = []
        type_model = 'naive_baye_model'
        list_of_nb_models = m.load_model(primary_type, type_model)

    elif (choice == 0):
        #Read in data for 10 to 16
        data10to16 = m.pre_processing(m.sql_pre_proc, dec.conn, primary_type,
                                      community_area)
        #If you are interested in seeing the correlation matrices
        m.feature_selection(data10to16)

    elif (choice == 20):
        exit()

    else:
        exit()
Example #38
0
        line_list_map = [mapping_rev[x] for x in line_list]
        out_1.append(line_list_map)
    out_1 = np.asarray(out_1)
    out1_onehot = np.eye(nc, dtype='uint8')[out_1]
    out1_onehot = np.rollaxis(out1_onehot, 2, 0)

    out1_onehot = out1_onehot[None, :, :]

    data_1 = torch.FloatTensor(out1_onehot)
    z_1, _, _ = model.encode(data_1)

    return z_1


path = 'vae_ng_64_final.pth'
model = load_model(nc, nz)


def compute_density(z):
    z_decoded = model.decode(z)
    level = z_decoded.data.cpu().numpy()
    level = np.argmax(level, axis=1)
    total = 0
    for line in level[0]:
        total += len(line[line == 0])  # X
        total += len(line[line == 1])  # S
        total += len(line[line == 3])  # Q
        total += len(line[line == 4])  # ?
        total += len(line[line == 11])  # T
        total += len(line[line == 12])  # M
        total += len(line[line == 14])  # #
Example #39
0
import model
import time
from random import random
from math import sin, cos, pi

mod = model.load_model('../openpixelcontrol/layouts/my_sphere.json')

totalLife = 50  # how long to keep the dots there
numParticles = 10
zoom = 0.998
intensity = -1.2

backgroundHue = random()

# Start out with just a background
particles = [] 

def newBloomTimer():
    return random() * 100               # how often and fast new things show

def randomPoint():
    #A random point somewhere relevant on our model
    return [
        4 * (random() - 0.5),
        1 * (random() - 0.5),
        4 * (random() - 0.5)
    ]

# Create other nascent particles that 'bloom' once a timer runs out
for i in range(numParticles):
    particles.append({
Example #40
0
def main(_run, _log):
    args = argparse.Namespace(**_run.config)
    args = post_config_hook(args, _run)

    args.device = torch.device(
        "cuda:0" if torch.cuda.is_available() else "cpu")
    args.n_gpu = torch.cuda.device_count()

    root = "./datasets"

    train_sampler = None

    if args.dataset == "STL10":
        train_dataset = torchvision.datasets.STL10(
            root,
            split="unlabeled",
            download=True,
            transform=TransformsSimCLR(size=96))
    elif args.dataset == "CIFAR10":
        train_dataset = torchvision.datasets.CIFAR10(
            root, download=True, transform=TransformsSimCLR(size=32))
    else:
        raise NotImplementedError

    train_loader = torch.utils.data.DataLoader(
        train_dataset,
        batch_size=args.batch_size,
        shuffle=(train_sampler is None),
        drop_last=True,
        num_workers=args.workers,
        sampler=train_sampler,
    )

    model, optimizer, scheduler = load_model(args, train_loader)

    print(f"Using {args.n_gpu}'s")
    if args.n_gpu > 1:
        model = torch.nn.DataParallel(model)
        model = convert_model(model)
        model = model.to(args.device)

    print(model)

    tb_dir = os.path.join(args.out_dir, _run.experiment_info["name"])
    os.makedirs(tb_dir)
    writer = SummaryWriter(log_dir=tb_dir)

    criterion = NT_Xent(args.batch_size, args.temperature, args.device)

    args.global_step = 0
    args.current_epoch = 0
    for epoch in range(args.start_epoch, args.epochs):
        lr = optimizer.param_groups[0]['lr']
        loss_epoch = train(args, train_loader, model, criterion, optimizer,
                           writer)

        if scheduler:
            scheduler.step()

        if epoch % 10 == 0:
            save_model(args, model, optimizer)

        writer.add_scalar("Loss/train", loss_epoch / len(train_loader), epoch)
        writer.add_scalar("Misc/learning_rate", lr, epoch)
        print(
            f"Epoch [{epoch}/{args.epochs}]\t Loss: {loss_epoch / len(train_loader)}\t lr: {round(lr, 5)}"
        )
        args.current_epoch += 1

    ## end training
    save_model(args, model, optimizer)
Example #41
0
    for c in str(mod):
        if c == '{' or c == '[':
            indent += 2
            outString += '%s\n%s' % (c,' '*indent)
            brackets.append(c)
            continue
        elif c == '}' or c == ']':
            indent -= 2
            outString += '\n%s' % (' '*indent)
            brackets.pop()
        elif c == '(':
            brackets.append(c)
        elif c == ')':
            brackets.pop()
        elif c == ',':
            if brackets[-1] <> '(':
                outString += '\n%s' % (' '*indent)
                continue
        outString += c

    print outString        

if __name__ == '__main__':
    parser = OptionParser(usage=USAGE)
    
    options, args = parser.parse_args()

    mod = model.load_model(args[0])

    PrettyPrintModel(mod)