def train_model(grade_mode):
    # Check that function parameters are valid
    if not grade_mode in ['no_grade', 'post_grade', 'pre_grade']:
        raise ValueError(
            'Invalid grade_mode for model training. Use no_grade, post_grade or pre_grade as the grade_mode parameter.'
        )

    # Find directories
    base_save_dir = '{}/data/lstm_files/{}/'.format(script_parent_directory,
                                                    grade_mode)

    # Train the model
    train.build_model(base_save_dir)
Beispiel #2
0
def main():
    digit2idx, idx2digit = load_vocab()
    X, I = load_test_data()
    model = build_model(Hyperparams.ctxlen)
    latest_ckpt = get_the_latest_ckpt()
    model.load_weights(latest_ckpt)

    with open('../result.csv', 'w') as fout:
        fout.write("Id, Last\n")
        for step in range(0, len(X), Hyperparams.batch_size):
            xs = X[step:step + Hyperparams.batch_size]
            ids = I[step:step + Hyperparams.batch_size]
            _preds = []
            for _ in range(10):
                preds = model.predict(xs)
                preds = preds[:, -1, :]
                preds = np.argmax(preds, axis=-1)
                _preds.append(preds)
                preds = np.expand_dims(preds, -1)
                xs = np.concatenate((xs, preds), 1)[:, 1:]
            _preds = np.array(_preds).transpose()
            for p, id in zip(_preds, ids):
                p = "".join(idx2digit[idx] for idx in p).split(",")[0]
                fout.write("{},{}\n".format(id, p))
                fout.flush()
def main(path: str):
    _configs = load_configs(path)

    _batch_size = _configs['hyperparams']['batch_size']
    _epochs = _configs['hyperparams']['epochs']
    _embedding_size = _configs['hyperparams']['embedding_size']
    _conv_dim = _configs['hyperparams']['convolution_dims']
    _eval_steps = _configs['hyperparams']['evaluation_steps']

    print('Getting dataset...')
    _data_manager = prepare_dataset(_configs)
    _vocab_size = _data_manager.get_vocabulary_size()
    _pretrained_embeddings = _data_manager.get_pretrained_word_embedding()

    print('Now build model...')
    model = build_model(_vocab_size, _embedding_size, _conv_dim, embeddings=_pretrained_embeddings)

    train_dataloader = _data_manager.get_train_data_loader(_batch_size)
    test_dataloader = _data_manager.get_test_data_loader(_batch_size)

    print('Train start!')
    train_manager = TrainManager(train_dataloader,
                                 test_dataloader,
                                 model,
                                 _epochs,
                                 _eval_steps)

    train_manager.train()
def test_train_pipeline(fix_seed, config, gpus):
    config = OmegaConf.create(config)

    train_dataloader, test_dataloader = get_data_loaders(config=config)
    lr_logger = LearningRateLogger()
    model = build_model(model_conf=config.model)
    runner = Runner(model=model, config=config.runner)

    trainer = Trainer(
        distributed_backend=config.runner.trainer.distributed_backend,
        fast_dev_run=True,
        gpus=gpus,
        amp_level="O2",
        row_log_interval=10,
        callbacks=[lr_logger],
        max_epochs=1,
        weights_summary="top",
        reload_dataloaders_every_epoch=False,
        resume_from_checkpoint=None,
        benchmark=False,
        deterministic=True,
        num_sanity_val_steps=5,
        overfit_batches=0.0,
        precision=32,
        profiler=True,
    )

    trainer.fit(model=runner,
                train_dataloader=train_dataloader,
                val_dataloaders=test_dataloader)
Beispiel #5
0
def train_selected_model(activation,
                         learning_rate,
                         momentum,
                         n_points,
                         n_epochs,
                         batch_size,
                         plot_points=False):
    train_data, test_data = data.generate_data(n_points)

    model = train.build_model(activation)
    optimizer = optim.SGD(model.parameters(),
                          lr=learning_rate,
                          momentum=momentum)
    criterion = framework.MSELoss()

    t0 = time.perf_counter()
    history = train.train_model(model, optimizer, criterion, train_data,
                                test_data, n_epochs, batch_size)
    t1 = time.perf_counter()

    result = {
        'train_loss': train.compute_loss(model, criterion, train_data,
                                         batch_size),
        'test_loss': train.compute_error(model, train_data, batch_size) * 100,
        'train_err': train.compute_loss(model, criterion, test_data,
                                        batch_size),
        'test_err': train.compute_error(model, test_data, batch_size) * 100,
        'time': t1 - t0
    }

    if plot_points:
        plot.plot_points(test_data, train_data, model, plot_points)

    return history, result
Beispiel #6
0
def eval_one_mol(smiles, model_save_dir_root, gpu):
    # smiles = Chem.MolToSmiles(mol, isomericSmiles=True)
    featurizer = dc.feat.CircularFingerprint(size=1024)
    loader = dc.data.CSVLoader(tasks=[],
                               smiles_field='smiles',
                               featurizer=featurizer)

    test_file = io.StringIO(f"smiles\n{smiles}\n")
    test_dataset = loader.create_dataset(test_file, shard_size=8096)
    device = f"/gpu:{gpu}"
    pred_list = []
    config = tf.compat.v1.ConfigProto()
    config.gpu_options.per_process_gpu_memory_fraction = 0.1  # 程序最多只能占用指定gpu50%的显存
    config.gpu_options.allow_growth = True  # 不全部占满显存, 按需分配
    sess = tf.compat.v1.Session(config=config)
    backend.set_session(sess)

    for m in ['seed0', 'seed1', 'seed2']:
        model_save_dir = f"{model_save_dir_root}/{m}"

        checkpoint = find_best_model_checkpoint(model_save_dir)
        with tf.device(device):
            model = build_model(model_save_dir)
            model.restore(checkpoint)
            model.batch_size = 2
            # model.to(device)
            result = model.predict(test_dataset)
            if result.shape[-1] == 1:
                result = result.squeeze(-1)
            pred_list.append(result[0][55])
    return mean(pred_list)
  def load(self, checkpoint_path, fast=True):
    # Presets
    if hparams.preset is not None and hparams.preset != "":
        preset = hparams.presets[hparams.preset]
        import json
        hparams.parse_json(json.dumps(preset))
        print("Override hyper parameters with preset \"{}\": {}".format(
            hparams.preset, json.dumps(preset, indent=4)))

    self._frontend = getattr(frontend, hparams.frontend)
    import train
    train._frontend = self._frontend
    from train import build_model

    # Model
    self.model = build_model()

    # Load checkpoints separately
    checkpoint = torch.load(checkpoint_path)
    self.model.load_state_dict(checkpoint["state_dict"])
    #model.seq2seq.decoder.max_decoder_steps = max_decoder_steps

    self.use_cuda = torch.cuda.is_available()
    if self.use_cuda:
        self.model = self.model.cuda()
    self.model.eval()
    if fast:
        self.model.make_generation_fast_()
def main(path: str):
    _configs = load_configs(path)

    _batch_size = _configs['hyperparams']['batch_size']
    _epochs = _configs['hyperparams']['epochs']
    _eval_steps = _configs['hyperparams']['evaluation_steps']

    _model_params = _configs['hyperparams']['model']

    print('Getting dataset...')
    _data_manager = prepare_dataset(_configs)
    _vocab_size = _data_manager.get_vocabulary_size()

    _model_params['vocab_size'] = _vocab_size

    print('Now build model...')
    model = build_model(_model_params)

    train_dataloader = _data_manager.get_train_data_loader(_batch_size)
    test_dataloader = _data_manager.get_test_data_loader(100)

    print(model)

    print('Train start!')
    train_manager = TrainManager(train_dataloader, test_dataloader, model,
                                 _epochs, _eval_steps)

    train_manager.train()
 def build(self):
     self.model_file = tf.train.get_checkpoint_state(
         './out').model_checkpoint_path
     graph = tf.Graph()
     with graph.as_default():
         self.source_ids = tf.placeholder(
             tf.int32,
             [self.options.batch_size, self.options.max_source_length])
         self.target_ids = tf.placeholder(
             tf.int32,
             [self.options.batch_size, self.options.max_target_length])
         self.source_mask = tf.placeholder(
             tf.int32,
             [self.options.batch_size, self.options.max_source_length])
         self.target_mask = tf.placeholder(
             tf.int32,
             [self.options.batch_size, self.options.max_target_length])
         self.ouputs = train.build_model(self.source_ids, self.target_ids,
                                         self.source_mask, self.target_mask,
                                         False, self.options)
         sess_config = tf.ConfigProto()
         sess_config.gpu_options.allow_growth = True
         self.sess = tf.Session(graph=graph, config=sess_config)
         tf_vars_dict = {i.name: i for i in tf.trainable_variables()}
         a_map = get_assign_map(tf_vars_dict)
         tf.train.init_from_checkpoint(self.model_file, a_map)
         self.sess.run(tf.global_variables_initializer())
Beispiel #10
0
def damage_by_segmentation(path):
    """
    Generate solution .png files, using a single multiclass segmentation
    model to do so.
    """
    model = train.build_model(classes=6, damage=True)
    model = train.load_weights(model,
                               "damage-motokimura-mobilenetv2-best.hdf5")
    #model.load_individual_weights()# = train.load_weights(model, S.DMG_MODELSTRING_BEST)
    df = flow.Dataflow(files=flow.get_test_files(),
                       transform=False,
                       batch_size=1,
                       buildings_only=False,
                       shuffle=False,
                       return_postmask=False,
                       return_stacked=True,
                       return_average=False)
    pbar = tqdm.tqdm(total=len(df))

    for image, filename in df:
        filename = os.path.basename(filename)
        filename = filename.replace("pre", "localization").replace(
            ".png", "_prediction.png")
        #if os.path.exists(os.path.join("solution", filename)):
        #    continue

        # localization (segmentation)
        pred = model.predict([image])
        mask = infer.convert_prediction(pred)
        write_solution(names=[filename], images=[mask], path=path)

        filename = filename.replace("localization", "damage")
        write_solution(names=[filename], images=[mask], path=path)

        pbar.update(1)
Beispiel #11
0
def damage_random(path):
    """
    Generate solution .png files using random damage.
    """
    model = train.build_model(
        train=False)  #, save_path="motokimura-stacked-2.hdf5")
    model = train.load_weights(model, S.MODELSTRING_BEST)
    df = flow.Dataflow(files=flow.get_test_files(),
                       transform=False,
                       batch_size=1,
                       buildings_only=False,
                       shuffle=False,
                       return_postmask=False,
                       return_stacked=True,
                       return_average=False)
    pbar = tqdm.tqdm(total=len(df))

    for image, filename in df:
        filename = os.path.basename(filename)
        filename = filename.replace("pre", "localization").replace(
            ".png", "_prediction.png")
        #if os.path.exists(os.path.join("solution", filename)):
        #    continue

        # localization (segmentation)
        pred = model.predict([image])
        mask = infer.convert_prediction(pred)
        write_solution(names=[filename], images=[mask], path=path)

        mask = randomize_damage(mask)
        filename = filename.replace("localization", "damage")
        write_solution(names=[filename], images=[mask], path=path)

        pbar.update(1)
Beispiel #12
0
def single_worker(device, num_jobs, args, idx_beg=0):

    if idx_beg > 0 and num_jobs == 1:
        local_writers = [open(f"{args.output_dir}/decode.{args.nj}.ark", 'wb')]
    else:
        local_writers = [open(f"{args.output_dir}/decode.{i+1}.ark", 'wb')
                         for i in range(num_jobs)]

    inferset = InferDataset(args.input_scp)
    inferset.dataset = inferset.dataset[idx_beg:]

    testloader = DataLoader(
        inferset, batch_size=1, shuffle=False,
        num_workers=args.workers, pin_memory=True)

    with open(args.config, 'r') as fi:
        configures = json.load(fi)

    model = build_model(args, configures, train=False)

    model = model.to(device)
    model.load_state_dict(torch.load(
        args.resume, map_location=device))
    model.eval()

    print("> Model built.")
    print("  Model size:{:.2f}M".format(
        utils.count_parameters(model)/1e6))

    cal_logit(model, testloader, device, local_writers)
Beispiel #13
0
def predict(img):
    inputs, pred = train.build_model()
    model = keras.Model(inputs, pred)
    model.load_weights('model/result.h5')
    y = model.predict(img)

    return y
Beispiel #14
0
def eval_test(model_save_dir_root, test_file, pred_path, gpu):
    featurizer = dc.feat.CircularFingerprint(size=1024)
    loader = dc.data.CSVLoader(tasks=[],
                               smiles_field='smiles',
                               featurizer=featurizer)
    test_dataset = loader.create_dataset(test_file, shard_size=8096)
    device = f"cuda:{gpu}"
    pred_list = []
    for m in ['seed0', 'seed1', 'seed2']:
        model_save_dir = f"{model_save_dir_root}/{m}"
        model = build_model(model_save_dir)
        checkpoint = find_best_model_checkpoint(model_save_dir)
        model.restore(checkpoint)
        # model.to(device)
        result = model.predict(test_dataset)
        if result.shape[-1] == 1:
            pred = pd.DataFrame(result.squeeze(-1))
        else:
            pred = pd.DataFrame(result)
        pred['smiles'] = test_dataset.ids.tolist()
        # dummmy column for next pipeline
        pred['label'] = 0
        pred_list.append(pred)
    pred = pd.concat(pred_list)
    score_colnames = [c for c in pred.columns]
    score_colnames.remove('smiles')
    score_colnames.remove('label')
    # average the values from 3 models as the score
    pred.groupby(['smiles'],
                 sort=False)[score_colnames].apply(lambda x: mean(x))
    # pred = pred.reset_index()
    cols = ['smiles', 'label']
    cols.extend(score_colnames)
    pred.to_csv(pred_path, index=False, columns=cols)
Beispiel #15
0
def load_model(checkpoint):
    """
    This funtion builds the NN model with its associated weights from the 
    checkpoint folder. It returns this model and the class to name dictionary for
    later use in the identification of the flower type.
    """
    # Checks that directory exists. If not, uses the default directory
    if os.path.isdir(checkpoint) == False:
        print(
            "Checkpoint path does not exist. Using the default checkpoint folder.\n"
        )
        checkpoint = 'checkpoint'
    # Loads the number of hidden neurons
    hidden = torch.load(checkpoint + '/hidden.pth')
    # Loads the VGG type used
    arch = torch.load(checkpoint + '/vgg_ver.pth')
    # Builds the initial model
    model, hidden = build_model(arch, hidden)
    # Converts to the cpu to upload
    model.to('cpu')
    # Loads the state dictionary and weights from checkpoint.pth file
    state_dict = torch.load(checkpoint + '/checkpoint.pth')
    # Loads the state dictionary into the model
    model.load_state_dict(state_dict)
    # Loads the class to index map into the class_to_idx dictionary
    class_to_idx = torch.load(checkpoint + '/class_to_idx.pth')
    return model, class_to_idx
Beispiel #16
0
def freezon_graph(params):
    # from tensorflow.python.framework.graph_util import convert_variables_to_constants
    # https://github.com/tensorflow/tensorflow/issues/31331
    import tensorflow as tf
    import tensorflow.compat.v1.keras.backend as K
    from tensorflow.python.tools import freeze_graph

    K.set_learning_phase(0)

    model = build_model(params)

    sess = K.get_session()
    graph = sess.graph
    pb_file = "test.pb"
    outdir = "./test/"

    print(model.outputs, model.inputs)
    # unfriendly ops: tf.newaxis, dims more than 4
    mid, lge = model.outputs
    if params.tflite:
        mid = tf.reshape(mid, (
            tf.shape(mid)[0],
            -1,
            tf.shape(mid)[-1],
        ))
        lge = tf.reshape(lge, (
            tf.shape(lge)[0],
            -1,
            tf.shape(lge)[-1],
        ))
        merge_branch = tf.concat([mid, lge], axis=1)

        model.inputs[0].set_shape([1, params.test_input, params.test_input, 3])
        converter = tf.compat.v1.lite.TFLiteConverter.from_session(
            sess, model.inputs, [merge_branch])
        converter.optimizations = [tf.lite.Optimize.DEFAULT]
        #converter.target_spec.supported_types = [tf.compat.v1.lite.constants.FLOAT16]
        tflite_model = converter.convert()
        open("gesture.tflite", "wb").write(tflite_model)
        return

    tf.compat.v1.saved_model.simple_save(K.get_session(),
                                         outdir,
                                         inputs={"input": model.inputs[0]},
                                         outputs={
                                             "output0": mid,
                                             "output1": lge
                                         })

    freeze_graph.freeze_graph(None,
                              None,
                              None,
                              None,
                              mid.op.name + "," + lge.op.name,
                              None,
                              None,
                              os.path.join(outdir, pb_file),
                              False,
                              "",
                              input_saved_model_dir=outdir)
Beispiel #17
0
def load_model(model_name):
    from train import build_model
    from train import restore_parts, load_checkpoint

    checkpoint_path = model_name
    model = build_model()
    model = load_checkpoint(checkpoint_path, model, None, True)
    return model
def main():

    model = build_model('resnet34', 'square_crop', pretrained=True)
    print(model)

    input1 = torch.zeros(32, 1, 137, 236)
    input2 = torch.zeros(32, 1, 128, 128)
    out = model.forward((input1, input2))
    print(out.shape)
Beispiel #19
0
def init_model():
    model = build_model(3)
    device = torch.device('cpu')
    checkpoint = torch.load("./checkpoint/chkpoint_9.pt",
                            map_location={'cuda:0':
                                          'cpu'})  #read from last checkpoint
    model.load_state_dict(checkpoint['state_dict'])
    model.eval()  #evaluation mode
    return model
Beispiel #20
0
  def test_train_success(self):
    train_root_dir = self._config['train_root_dir']
    if not tf.gfile.Exists(train_root_dir):
      tf.gfile.MakeDirs(train_root_dir)

    for stage_id in train.get_stage_ids(**self._config):
      tf.reset_default_graph()
      real_images = provide_random_data()
      model = train.build_model(stage_id, real_images, **self._config)
      train.add_model_summaries(model, **self._config)
      train.train(model, **self._config)
Beispiel #21
0
def damage_by_building_classification(path):
    """
    Generate solution .png files, classifying damage using contiguous
    regions in the segmentation model's predicted masks in order to extract
    individual building polygons from pre-disaster and post-disaster images.
    """
    # load the localization (segmentation) model
    S.BATCH_SIZE = 1
    model = train.build_model(architecture=S.ARCHITECTURE, train=True)
    model = train.load_weights(
        model, S.MODELSTRING_BEST)  #.replace(".hdf5", "-best.hdf5"))

    # load the damage classification model
    dmg_model = damage.build_model()
    dmg_model = damage.load_weights(dmg_model, S.DMG_MODELSTRING_BEST)

    # get a dataflow for the test files
    df = flow.Dataflow(files=flow.get_test_files(),
                       transform=False,
                       shuffle=False,
                       buildings_only=False,
                       batch_size=1,
                       return_stacked=True)
    i = 0
    pbar = tqdm.tqdm(total=len(df))
    # x = pre-disaster image, y = post-disaster image
    for stacked, filename in df:
        filename = os.path.basename(filename)
        x = stacked
        #filename = os.path.basename(df.samples[i][0].img_name)
        filename = filename.replace("pre", "localization").replace(
            ".png", "_prediction.png")
        #if os.path.exists(os.path.join("solution", filename)):
        #    continue

        # localization (segmentation)
        pred = model.predict(x)
        mask = infer.convert_prediction(pred)
        write_solution(names=[filename], images=[mask], path=path)

        # damage classification
        filename = filename.replace("localization", "damage")
        pre, post = stacked[..., :3], stacked[
            ..., 3:]  #df.samples[i][0].image(), df.samples[i][1].image()
        boxes, coords = flow.Building.get_all_in(pre, post, mask)
        if len(boxes) > 0:
            labels = dmg_model.predict(boxes)
            for k, c in enumerate(coords):
                x, y, w, h = c
                mask[y:y + h, x:x + w] = np.argmax(labels[k]) + 1

        write_solution(names=[filename], images=[mask], path=path)
        pbar.update(1)
        i += 1
    def test_train_success(self):
        train_root_dir = self._config['train_root_dir']
        if not tf.gfile.Exists(train_root_dir):
            tf.gfile.MakeDirs(train_root_dir)

        for stage_id in train.get_stage_ids(**self._config):
            tf.reset_default_graph()
            real_images = provide_random_data()
            model = train.build_model(stage_id, real_images, **self._config)
            train.add_model_summaries(model, **self._config)
            train.train(model, **self._config)
def apply(beginning, num_of_chars, save_to_JS=False):
    # load model

    # model = keras.models.load_model(os.path.join(MODEL_PATH, 'model.h5'))
    with open(os.path.join(MODEL_PATH, 'text_to_int.pickle'), 'rb') as handle:
        text_to_int = pickle.load(handle)
    with open(os.path.join(MODEL_PATH, 'int_to_text.pickle'), 'rb') as handle:
        int_to_text = pickle.load(handle)
    with open(os.path.join(MODEL_PATH, 'vocab_size.pickle'), 'rb') as handle:
        vocab_size = pickle.load(handle)

    model = build_model(vocab_size=vocab_size,
                        embedding_dim=EMBEDDING_DIM,
                        rnn_units=RNN_UNITS,
                        batch_size=1)

    model.load_weights(os.path.join(MODEL_PATH, 'model'))

    print('Load model successfully.')

    # for Tensorflowjs
    if save_to_JS:
        if Path(JS_PATH).is_dir():
            shutil.rmtree(JS_PATH)

        data = {'text_to_int': text_to_int, 'int_to_text': int_to_text}
        with open('data.json', 'w', encoding='utf-8') as f:
            f.write(json.dumps(data))
            print('Dict written to data.json.')

        tfjs.converters.save_keras_model(model, JS_PATH)
        print('Model for JS saved to %s.' % JS_PATH)

    input_seq_jieba = [l for l in list(jieba.cut(beginning)) if l != ' ']
    input_seq_int = [
        text_to_int[w] for w in input_seq_jieba if w in text_to_int
    ]
    if len(input_seq_int) == 0:
        input_seq_int = [text_to_int['<br>']]
    input_seq = tf.expand_dims(input_seq_int, axis=0)  # Add a dim

    text_generated = ''
    model.reset_states()  # Add this because 'stateful=True'
    while len(text_generated) < num_of_chars:
        predictions = model.predict(input_seq)
        predictions = tf.squeeze(predictions, axis=0)
        predictions /= TEMPERATURE
        predicted_id = tf.random.categorical(predictions,
                                             num_samples=1).numpy()[-1][0]
        input_seq = tf.expand_dims([predicted_id], 0)  # Add a dim
        text_generated += int_to_text[
            predicted_id] if int_to_text[predicted_id] != '<br>' else '\n'

    return text_generated
Beispiel #24
0
    def __init__(self, config, **opt):
        # Load config used for training and merge with testing options
        self.config = yaml.load(open(config, "r"))
        self.config = Namespace(**{**self.config, **opt})

        # Load training data.pkl for src and tgt vocabs
        self.data = load_data(self.config)

        # Load trained model checkpoints
        device, devices_ids = misc_utils.set_cuda(self.config)
        self.model, _ = build_model(None, self.config, device)
        self.model.eval()
Beispiel #25
0
def load_model(checkpoint_path):
    ''' Load the model from checkpoint
    '''
    checkpoint = torch.load(checkpoint_path)

    # Get model from checkpoint
    model = build_model(architecture=checkpoint['model'])

    ## Build classifier architecture
    # Define first layer
    architecture = OrderedDict([
        ('fc1', nn.Linear(checkpoint['input'], checkpoint['hidden'][0])),
        ('re1', nn.ReLU()),
        ('dr1', nn.Dropout(p=checkpoint['p_drop'])),
    ])
    ## If 1+ hidden layers
    if len(checkpoint['hidden']) > 1:
        # Define the hidden layer(s)
        for index, layer_size in enumerate(
                zip(checkpoint['hidden'][:-1], checkpoint['hidden'][1:])):
            architecture.update({
                'fc{}'.format(index + 2):
                nn.Linear(layer_size[0], layer_size[1])
            })
            architecture.update({'re{}'.format(index + 2): nn.ReLU()})
            architecture.update(
                {'dr{}'.format(index + 2): nn.Dropout(p=p_drop)})

        # Define the last layer
        architecture.update({
            'fc{}'.format(index + 3):
            nn.Linear(checkpoint['hidden'][-1], checkpoint['output'])
        })
        architecture.update({'log': nn.LogSoftmax(dim=1)})
    ## If no hidden layers
    if len(checkpoint['hidden']) == 1:
        architecture.update(
            {'fc2': nn.Linear(checkpoint['hidden'][0], checkpoint['output'])})
        architecture.update({'log': nn.LogSoftmax(dim=1)})

    classifier = nn.Sequential(architecture)
    model.classifier = classifier

    # Load state dict
    model.load_state_dict(checkpoint['state_dict'])

    # Load classifier
    optimizer = optim.SGD(params=model.classifier.parameters(),
                          lr=checkpoint['lr'],
                          momentum=checkpoint['momentum'])
    optimizer.load_state_dict(checkpoint['optimizer'])

    return model, optimizer
def load(checkpoint_path):
    checkpoint = torch.load(checkpoint_path)
    model = build_model(checkpoint['architecture'],
                        checkpoint['hidden_layers'], checkpoint['output_size'],
                        checkpoint['class_to_idx'])
    model.load_state_dict(checkpoint['state_dict'])
    #     optimizer.load_state_dict(checkpoint['optimizer'])
    #     print("Loaded '{}' (arch={}, hidden_layers={})".format(
    #     checkpoint_path,
    #     checkpoint['architecture'],
    #     checkpoint['hidden_layers']))
    return model
Beispiel #27
0
    def test_inference_vs_train(self):
        self.assertTrue(False) # disable and auto fail this test for now
        tf.reset_default_graph()
        with tf.Session() as sess:
            conf = config.generate_config(keep_prob=1.0)
            conf['batch_size'] = 1
            data = data_pipe.Data('./example_data/', conf['batch_size'], conf['max_word_len'], conf['max_line_len'])
            model, free_model = train.build_model(data, conf)
            data.initialize(sess, data.datadir + '*')
            sess.run(tf.tables_initializer())
            sess.run(tf.global_variables_initializer())
            (out_logits_4,
             src_sentence_3,
             src_sent_len_1,
             trg_sentence_3,
             trg_sent_len_1) = sess.run([model.out_logits_4,
                                         data.src.to_tensor(-1),
                                         data.src_sentence_len,
                                         data.trg.to_tensor(-1),
                                         data.trg_sentence_len])
            src = data.array_to_strings(src_sentence_3)[0].replace(data.go_stop_token, '')
            trg = data.array_to_strings(trg_sentence_3)[0].replace(data.go_stop_token, '')
            # trg is the concatenation of itself with src. Restore the stop word that delimits them
            trg = trg[len(src):]
            trg = src + ' ' + data.go_stop_token + ' ' + trg.strip() # recombine src and trg
            print src
            print trg
            feed = {data.src_place:src, data.trg_place:trg}
            (free_logits_4,
             src_sentence_inference,
             trg_sentence_inference) = sess.run([free_model.out_logits_4,
                                                 data.src_inference.to_tensor(-1),
                                                 data.trg_inference.to_tensor(-1)], feed_dict=feed)
            # Get the fist batch line and trim potential batch padding from the model's logits
            out_logits_3 = out_logits_4[0, :free_logits_4.shape[1], :free_logits_4.shape[2], :]
            # Check that the model's outputs are the same regardless of what data pipeline is used
            self.assertTrue((np.abs(out_logits_3 - free_logits_4[0]) < 1e-5).all())
            # Run the inference model as though generating one char at time, and check the outputs
            feed = {data.src_place:src, data.trg_place:''} # Start with no input
            free_logits_4 = sess.run(free_model.out_logits_4, feed_dict=feed)
            self.assertTrue((np.abs(free_logits_4[0,0,0,:] - out_logits_3[0,0,:]) <= 1e-5).all()) 
            trg = trg.split()
            trg_so_far = ''
            for word_idx, trg_word in enumerate(trg):
                for chr_num in range(len(trg_word)):
                    trg_so_far += trg_word[chr_num]
                    feed = {data.src_place:src, data.trg_place:trg_so_far}
                    free_logits_4 = sess.run(free_model.out_logits_4, feed_dict=feed)
#                    print (free_logits_4[0, word_idx, chr_num + 1,:] - out_logits_3[word_idx, chr_num + 1, :]) < 1e-4
                    self.assertTrue((np.abs(free_logits_4[0, word_idx, chr_num + 1,:] - out_logits_3[word_idx, chr_num + 1, :]) <= 1e-5).all())
                trg_so_far += ' '
Beispiel #28
0
def main():
    args = get_args()
    weight_path = args.weight_path
    if not os.path.exists(RESPATH):
        os.makedirs(RESPATH)
    viddata, auddata = train.load_data(DATAPATH)
    net_out = auddata.shape[1]
    viddata, auddata_norm, auddata_means, auddata_stds = standardize_data(
        viddata, auddata)
    model = train.build_model(net_out)
    model.compile(loss='mse', optimizer='adam')
    model.load_weights(weight_path)
    aud_pred = train.predict(model, viddata, auddata_means, auddata_stds)
    np.save(join(RESPATH, 'aud_pred.npy'), aud_pred)
def load_checkpoint(checkpoint):
    state = torch.load(checkpoint)

    arch = state['arch']
    lr = float(state['learning_rate'])
    hidden_units = int(state['hidden_units'])

    model, optimizer, criterion = \
        train.build_model(arch, hidden_units, lr)

    model.class_to_idx = state['class_to_idx']
    model.load_state_dict(state['state_dict'])
    optimizer.load_state_dict(state['optimizer'])
    return model
Beispiel #30
0
def main():
    args = get_args()
    weight_path = args.weight_path
    if not os.path.exists(RESPATH):
        os.makedirs(RESPATH)
    (Xtr, Ytr), (Xte, Yte) = train.load_data(DATAPATH)
    net_out = Ytr.shape[1]
    Xtr, Ytr_norm, Xte, Yte_norm, Y_means, Y_stds = train.standardize_data(
        Xtr, Ytr, Xte, Yte)
    model = train.build_model(net_out)
    model.compile(loss='mse', optimizer='adam')
    model.load_weights(weight_path)
    Ytr_pred, Yte_pred = train.predict(model, Xtr, Xte, Y_means, Y_stds)
    train.savedata(Ytr, Ytr_pred, Yte, Yte_pred, respath=RESPATH)
Beispiel #31
0
def setup():
    os.chdir(ROOT)
    if not exists(NAME):
        url = "https://github.com/r9y9/" + NAME
        os.system("git clone %s" % url)
    os.chdir(NAME)
    os.system("pip install -q -e \'.[bin]\'")  # THIS MIGHT NOT WORK: TEST
    #os.system("./dependency_scipt.sh") # Install python dependcies
    #os.system("./dependency_scipt.sh") # Install bash dependcies
    os.system("python -m nltk.downloader cmudict")  # English text processing

    # Get the model
    os.system("git checkout %s --quiet" % BRANCH)

    if not exists(PRESET):
        url = "https://www.dropbox.com/s/0ck82unm0bo0rxd/" + PRESET
        os.system("curl -O -L %s" % url)
    if not exists(CHECKPOINT):
        url = "https://www.dropbox.com/s/5ucl9remrwy5oeg/" + CHECKPOINT
        os.system("curl -O -L %s" % url)

    # Hyper parameters
    import hparams
    import json

    # Load parameters from preset
    with open(PRESET) as f:
        hparams.hparams.parse_json(f.read())

    # Inject frontend text processor
    import synthesis
    import train
    from deepvoice3_pytorch import frontend
    synthesis._frontend = getattr(frontend, "en")
    train._frontend = getattr(frontend, "en")

    # alises
    fs = hparams.hparams.sample_rate
    hop_length = hparams.hparams.hop_size
    print(os.path.dirname(os.path.realpath(__file__)))

    # Load model
    from train import build_model
    from train import restore_parts, load_checkpoint

    print("Building model")
    model = build_model()
    model = load_checkpoint(CHECKPOINT, model, None, True)
    return model
Beispiel #32
0
def main(_):
  if not tf.gfile.Exists(FLAGS.train_root_dir):
    tf.gfile.MakeDirs(FLAGS.train_root_dir)

  config = _make_config_from_flags()
  logging.info('\n'.join(['{}={}'.format(k, v) for k, v in config.iteritems()]))

  for stage_id in train.get_stage_ids(**config):
    tf.reset_default_graph()
    with tf.device(tf.train.replica_device_setter(FLAGS.ps_tasks)):
      real_images = None
      with tf.device('/cpu:0'), tf.name_scope('inputs'):
        real_images = _provide_real_images(**config)
      model = train.build_model(stage_id, real_images, **config)
      train.add_model_summaries(model, **config)
      train.train(model, **config)
Beispiel #33
0
def yield_chars():
    text, char_indices, indices_char = load_data()
    model = build_model(indices_char)
    load_latest_model(model)

    start_index = random.randint(0, len(text) - MAXLEN - 1)
    gen = sample_from_model(text, start_index, char_indices, model, indices_char)

    next_letter_upper = False
    for c in gen:
        if next_letter_upper and c.upper() != c:
            yield c.upper()
            next_letter_upper = False
        else:
            yield c
        if c == '.':
            next_letter_upper = True
Beispiel #34
0
import train

udp_host = "localhost"
udp_port = 4000
sock     = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

key_wait_time = 10
input_size = 128

capture = cv2.VideoCapture(0)

# video_size = (int(capture.get(cv2.CAP_PROP_FRAME_WIDTH)),
#               int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT)))
# print "Video width, height: " + str(video_size)

model = train.build_model()
model.load('checkpoints/road_model1-72')

def process_frame(frame):
  pr = model.predict(frame[np.newaxis, :, :, np.newaxis])
  return pr[0][0]

while capture.isOpened():
  success, frame = capture.read()

  if success:
    time_now = time()
    if time_now >= time_next:
      time_next = time_now + time_delay

      frame_gray  = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)