class TbLogger():
    def __init__(self, logdir, unique_dir=False, restart=False):
        if restart:
            subdirectories = [f.path for f in os.scandir(logdir) if f.is_dir()]
            for dir in subdirectories:
                shutil.rmtree(dir)
        if unique_dir:
            logdir = os.path.join(
                logdir, str(datetime.datetime.today().strftime('%d. %B %Y')))
            print(logdir)
        self.writer = SummaryWriter(logdir)

    def add_image(self, title, image, global_step=None):
        self.writer.add_image(title, image, global_step)

    def add_graph(self, net, image, verbose=False):
        self.writer.add_graph(net, image, verbose)

    def add_embedding(self, features, metadata, label_img, global_step=None):
        self.writer.add_embedding(features,
                                  metadata,
                                  label_img,
                                  global_step=global_step)

    def add_scalar(self, var, val, global_step=None):
        self.writer.add_scalar(var, val, global_step)

    def add_figure(self, var, image, global_step=None):
        self.writer.add_figure(var, image, global_step)

    def close(self):
        self.writer.close()
def create_3d_projector(loader, num, net, logdir, cm):

    try:
        classes = list(loader.dataset.class_to_idx.keys())
    except:
        classes = list(loader.dataset.dataset.class_to_idx.keys())

    with torch.no_grad():
        for data in loader:
            images, labels = data
            images = images.to(device)
            outputs = net(images)
            _, predictions = torch.max(outputs.to('cpu').data, 1)
            class_labels = [classes[lab] for lab in labels]
            features = net.output_final_layer.to('cpu')
            break

    images = images.to('cpu')
    # print(images.shape)
    try:
        _, _, _, _ = images.shape
        # images_new = images.permute(0,3,1,2)
        images_new = images
    except:
        images_new = images.unsqueeze(1)
    if cm == True:
        create_image_confusion_matrix(logdir, labels.numpy(),
                                      predictions.numpy(), classes)

    writer = SummaryWriter(logdir)
    writer.add_embedding(features, metadata=class_labels, label_img=images_new)
    writer.close()
def add_embedding_visualization(writer: SummaryWriter, metrics, global_step):
    all_embeddings, all_image_data, all_targets = metrics['embedding_data']
    writer.add_embedding(all_embeddings,
                         metadata=all_targets,
                         label_img=all_image_data,
                         global_step=global_step,
                         tag="coord2vec")
def test(args, mnasnet, regressor, data_loader, show_image_on_board=True):
    writer = SummaryWriter()
    weights = []
    images = []
    labels = []
    result_labels = []
    with torch.no_grad():
        for i, (image, label) in enumerate(data_loader):
            image = image.to(device)
            images.append(image.squeeze(0).numpy())
            label = label.squeeze(0).numpy()[0]
            labels.append(label)

            x = mnasnet(image)
            out_cl, emb = regressor.predict(x)
            out_label = out_cl.argmax(dim=1, keepdim=True)
            # visualize(args, image, predict, i, label, out_label)
            result_labels.append(out_label)
            weights.append(emb.squeeze(0).numpy())

    weights = torch.FloatTensor(weights)
    images = torch.FloatTensor(images)
    if show_image_on_board:
        writer.add_embedding(weights, label_img=images)
    else:
        writer.add_embedding(weights, metadata=labels)
    print_result(labels, result_labels)
    print("done")
Exemple #5
0
class _Visualizer():
    """
    Class that define utility functions to use TensorBoard as visualization debug platform
    """
    def __init__(self,
                 log_dir="./visualizer_logs",
                 flush_secs=120,
                 filename_suffix='adaptation_'):
        self.timestamp = datetime.now()
        self.writer = SummaryWriter(log_dir=log_dir,
                                    flush_secs=flush_secs,
                                    filename_suffix=filename_suffix,
                                    comment=str(self.timestamp))

    def add_embedding(self, vectors, metadata, metadata_header, tag=None):
        """
        Append given embeddings and informations to SummaryWriter
        @param vectors: list of embeddings' matrix
        @param metadata: list of records that describe the correspondent vector
        @param metadata_header: header of metadata columns
        @return None
        """
        if not tag:
            tag = "main_" + str(datetime.now())
        vectors = np.array(vectors)
        self.writer.add_embedding(vectors,
                                  metadata,
                                  metadata_header=metadata_header,
                                  global_step=tag)

    def close(self):
        self.writer.close()
Exemple #6
0
class Tensorboard:
    def __init__(self, config: ConfigFactory) -> None:
        self.config = config
        self.writer = SummaryWriter(self.config.tensorboard_path())

    def write_scalar(self, title, value, iteration) -> None:
        self.writer.add_scalar(title, value, iteration)

    def write_video(self, title, value, iteration) -> None:
        self.writer.add_video(title,
                              value,
                              global_step=iteration,
                              fps=self.config.fps)

    def write_image(self, title, value, iteration) -> None:
        self.writer.add_image(title,
                              value,
                              global_step=iteration,
                              dataformats='CHW')

    def write_histogram(self, title, value, iteration) -> None:
        self.writer.add_histogram(title, value, iteration)

    def write_embedding(self, all_embeddings, metadata, images) -> None:
        self.writer.add_embedding(all_embeddings,
                                  metadata=metadata,
                                  label_img=images)

    def write_embedding_no_labels(self, all_embeddings, images) -> None:
        self.writer.add_embedding(all_embeddings, label_img=images)
def evaluate_encoder(encoder: nn.Module,
                     test_loader: torch.utils.data.DataLoader,
                     loss_fn: nn.TripletMarginLoss = None,
                     writer: SummaryWriter = None,
                     epoch: int = -1):
    with torch.no_grad():
        encoder.eval()
        searcher = Searcher.get_simple_index(encoder.embedding_dim)
        embeddings_x = []
        embeddings_y = []
        epoch_losses = []
        for step, (x, y_pos, y_neg) in enumerate(test_loader):
            x, y_pos, y_neg = x.cuda(), y_pos.cuda(), y_neg.cuda()
            x_enc = encoder(x)
            y_pos_enc = encoder(y_pos)
            y_neg_enc = encoder(y_neg)
            if loss_fn:
                loss_val = loss_fn(x_enc, y_pos_enc, y_neg_enc)
                epoch_losses.append(loss_val.item())
            embeddings_x.append(x_enc)
            embeddings_y.append(y_pos_enc)
            print('    Test batch {} of {}'.format(step + 1, len(test_loader)),
                  file=sys.stderr)

        embeddings_x = torch.cat(embeddings_x, dim=0)
        embeddings_y = torch.cat(embeddings_y, dim=0)
        searcher.add(embeddings_x)
        lookup = searcher.search(embeddings_y, 100)
        correct_100 = sum(y in x
                          for y, x in enumerate(lookup[1])) / len(lookup[1])
        correct_50 = sum(y in x[:50]
                         for y, x in enumerate(lookup[1])) / len(lookup[1])
        correct_10 = sum(y in x[:10]
                         for y, x in enumerate(lookup[1])) / len(lookup[1])
        correct_1 = sum(y == x[0]
                        for y, x in enumerate(lookup[1])) / len(lookup[1])
        print(f'Test loss: {np.mean(epoch_losses):.4f}')
        print(
            'Test accuracy:\n    top1 {}\n    top10 {}\n    top50 {}\n    top100 {}'
            .format(correct_1, correct_10, correct_50, correct_100))
        if writer:
            writer.add_scalars('Accuracy', {
                'top1': correct_1,
                'top10': correct_10,
                'top50': correct_50,
                'top100': correct_100,
            },
                               global_step=epoch)
            writer.add_scalar('Loss/test',
                              np.mean(epoch_losses),
                              global_step=epoch)
            if epoch == -1 or epoch % 5 == 1:
                mat = torch.cat([embeddings_x[:1000], embeddings_y[:1000]],
                                dim=0)
                labels = list(range(1000)) + list(range(1000))
                writer.add_embedding(mat,
                                     labels,
                                     tag='Embeddings',
                                     global_step=epoch)
        return correct_1 * 100, correct_100 * 100, lookup, embeddings_x, embeddings_y
Exemple #8
0
def make_embeddings(args):
    """
    Make embeddings from 'args.wav_dir', save them into Annoy and TF Projector and JIT-compile their model.
    """
    coubs = sorted(args.wav_dir.glob('*.wav'))
    coub_dataset = ConcatDataset(
        [boiler.dataset.WavFile(wav) for wav in coubs])
    coub_loader = DataLoader(coub_dataset,
                             batch_size=64,
                             shuffle=False,
                             num_workers=4)

    model = getattr(boiler.encoder, args.encoder)(args.pt_path, args.device)

    embeddings = []
    for batch in tqdm(coub_loader):
        embeddings.append(model(batch.to(args.device)).detach().cpu())
        gc.collect()
        torch.cuda.empty_cache()
    embeddings = torch.cat(embeddings)

    model_dir = args.pt_path.with_suffix('') / str(args.encoder)
    model_dir.mkdir(parents=True, exist_ok=True)
    torch.jit.script(model).save(str(model_dir / 'encoder.pt'))

    writer = SummaryWriter(log_dir=model_dir)
    writer.add_embedding(
        embeddings,
        global_step=None,
        metadata=[f'https://coub.com/view/{p.stem}' for p in coubs])

    return model_dir, embeddings
Exemple #9
0
def main(sigs_path, images_path, samples_per_person=16):
    # Read the imagenet signatures from file
    paths, signatures = read_signatures(sigs_path)
    # Enumerate the frame paths based on person and video
    person_ids, video_ids = enumerate_paths(paths)
    # Sample "samples_per_person" images from each person
    sampled_indices = [
        pid for pp in split_by(range(len(paths)), person_ids)
        for pid in sorted(np.random.choice(pp, samples_per_person).tolist())
    ]
    sampled_paths = [paths[idx] for idx in sampled_indices]
    sampled_labels = np.mgrid[:len(sampled_indices
                                   ), :samples_per_person][0].ravel()
    # Get images of sampled data points
    with Images(images_path) as images:
        sampled_images = [images[path] for path in sampled_paths]
    sampled_images = np.concatenate([sampled_images]).transpose([0, 3, 1, 2])
    # Get normalized signatures of sampled data points
    sampled_sigs = signatures[sampled_indices]
    sampled_sigs /= np.sqrt(
        np.sum(np.square(sampled_sigs), axis=1, keepdims=True))
    # Write data to tensorboard projector
    writer = SummaryWriter()
    meta_data = [sp.split('/')[0] for sp in sampled_paths]
    label_img = torch.from_numpy(sampled_images).float() / 255
    writer.add_embedding(torch.from_numpy(sampled_sigs),
                         metadata=meta_data,
                         label_img=label_img)
    print('Visualization ready')
    print('run: \t tensorboard --logdir=runs')
Exemple #10
0
def main():
    # transforms
    transform = transforms.Compose(
        [transforms.ToTensor(),
         transforms.Normalize((0.5, ), (0.5, ))])

    # datasets
    trainset = torchvision.datasets.FashionMNIST('./data',
                                                 download=True,
                                                 train=True,
                                                 transform=transform)
    testset = torchvision.datasets.FashionMNIST('./data',
                                                download=True,
                                                train=False,
                                                transform=transform)

    # dataloaders
    trainloader = torch.utils.data.DataLoader(trainset,
                                              batch_size=4,
                                              shuffle=True,
                                              num_workers=2)

    testloader = torch.utils.data.DataLoader(testset,
                                             batch_size=4,
                                             shuffle=False,
                                             num_workers=2)

    # constant for classes
    classes = ('T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal',
               'Shirt', 'Sneaker', 'Bag', 'Ankle Boot')

    writer = SummaryWriter('runs/fashion_mnist_experiment_1')
    dataiter = iter(trainloader)
    images, labels = dataiter.next()

    # create grid of images
    img_grid = torchvision.utils.make_grid(images)

    # show images
    matplotlib_imshow(img_grid, one_channel=True)

    # write to tensorboard
    writer.add_image('four_fashion_mnist_images', img_grid)

    net = Net()
    writer.add_graph(net, images)

    # select random images and their target indices
    images, labels = select_n_random(trainset.data, trainset.targets)

    # get the class labels for each image
    class_labels = [classes[lab] for lab in labels]

    # log embeddings
    features = images.view(-1, 28 * 28)
    writer.add_embedding(features,
                         metadata=class_labels,
                         label_img=images.unsqueeze(1))
    writer.close()
Exemple #11
0
class TensorboardWriter:
    def __init__(self, outdir):
        assert (os.path.isdir(outdir))
        self.outdir = outdir
        self.writer = SummaryWriter(self.outdir, flush_secs=10)

    def save_scalar(self, tag, scalar_value, global_step=None):
        self.writer.add_scalar(tag, scalar_value, global_step)

    def save_scalars(self, main_tag, scalars_dict, global_step=None):
        self.writer.add_scalars(main_tag, scalars_dict, global_step)

    def save_image(self, tag, image, global_step=None, dataformats='CHW'):
        self.writer.add_image(tag=tag,
                              img_tensor=image,
                              global_step=global_step,
                              dataformats=dataformats)

    def save_figure(self, tag, figure, global_step=None, close=False):
        self.writer.add_figure(tag=tag,
                               figure=figure,
                               global_step=global_step,
                               close=close)

    def save_graph(self, model, inputs_to_model=None):
        """
        Saves graph to the tensorboard. Ideally call once.
        :param model: The torch.nn.Module object
        :param inputs_to_model: tensor or a list of tensor(batch) will also be showed.
        :return: None
        """
        try:
            self.writer.add_graph(model, inputs_to_model)
        except Exception as e:
            logger.exception(
                'Check this for fix: https://github.com/lanpa/tensorboardX/issues/389#issuecomment-475879228'
            )

    def save_embedding(self,
                       mat,
                       metadata=None,
                       label_img=None,
                       global_step=None,
                       tag='default',
                       metadata_header=None):
        self.writer.add_embedding(mat, metadata, label_img, global_step, tag,
                                  metadata_header)

    def flush(self):
        """
        If you need to flush all data immediately.
        """
        self.writer._get_file_writer().flush()

    def close(self):
        """
        To be called in the end
        """
        self.writer.close()
Exemple #12
0
def make_pytorch_projector(log_dir, embeddings, global_step):
    ''' Exports PyTorch projector '''
    writer = SummaryWriter(log_dir)
    writer.add_embedding(embeddings['mat'],
                         metadata=embeddings['labels'],
                         tag=embeddings['tag'],
                         global_step=global_step)
    writer.close()
Exemple #13
0
def log_embeddings_to_tensorboard(loader: DataLoader, model: nn.Module,
                                  device: torch.device, writer: SummaryWriter,
                                  tag: str) -> None:
    if tag == "train":
        if hasattr(loader.sampler, "sequential_sampling"):
            loader.sampler.sequential_sampling = True
    # Calculating embedding of training set for visualization
    embeddings, labels = get_embeddings_from_dataloader(loader, model, device)
    writer.add_embedding(embeddings, metadata=labels.tolist(), tag=tag)
Exemple #14
0
def evaluate_encoder(encoder: nn.Module, test_loader: torch.utils.data.DataLoader, writer: SummaryWriter = None,
                     epoch: int = -1, final=False):
    with torch.no_grad():
        encoder.eval()
        searcher = Searcher.get_simple_index(encoder.embedding_dim)
        embeddings = []
        targets = []
        for step, (x, target) in enumerate(test_loader):
            x = x.cuda()
            x_enc = encoder(x)
            embeddings.append(x_enc.cpu().numpy())
            targets.append(target.numpy())
            print('    Test batch {} of {}'.format(step + 1, len(test_loader)), file=sys.stderr)

        embeddings = np.concatenate(embeddings, axis=0)
        targets = np.concatenate(targets, axis=0)
        searcher.add(embeddings)
        lookup = searcher.search(embeddings, 100)[1]
        lookup = [[x for x in l if x != i] for i, l in enumerate(lookup)]
        correct_10 = sum(targets[y] in targets[x[:10]] for y, x in enumerate(lookup)) / len(lookup)
        correct_1 = sum(targets[y] == targets[x[0]] for y, x in enumerate(lookup)) / len(lookup)
        class_embeds = defaultdict(list)
        for embed, tgt in zip(embeddings, targets):
            class_embeds[tgt.item()].append(embed)
        class_means = {k: np.mean(v, axis=0) for k, v in class_embeds.items()}
        mean_correct_1 = 0
        for embed, tgt in zip(embeddings, targets):
            class_mean_without_x = np.mean([x for x in class_embeds[tgt.item()] if not np.array_equal(x, embed)],
                                           axis=0)
            best_other_val = 9999999999
            for k, v in class_means.items():
                if k == tgt.item():
                    continue
                dist = ((embed - v) ** 2).sum()
                if best_other_val > ((embed - v) ** 2).sum():
                    best_other_val = dist
            if ((embed - class_mean_without_x) ** 2).sum() < best_other_val:
                mean_correct_1 += 1
        mean_correct_1 /= len(embeddings)
        print(
            'Test accuracy:\n    top1 {}\n    top10 {}\n    mean1 {}'.format(
                correct_1, correct_10, mean_correct_1))
        if writer and not final:
            writer.add_scalar('Accuracy/Test_top1', correct_1, global_step=epoch)
            writer.add_scalar('Accuracy/Test_top10', correct_10, global_step=epoch)
            writer.add_scalar('Accuracy/Test_mean1', mean_correct_1, global_step=epoch)
            if epoch == -1 or epoch % 5 == 1:
                mat = embeddings[:1000]
                labels = targets[:1000]
                writer.add_embedding(mat, labels, tag='Embeddings', global_step=epoch)
        if writer and final:
            writer.add_text('FinalResults',
                            'top_1: {}, top_2: {}, mean1: {}'.format(correct_1, correct_10, mean_correct_1))
            mat = embeddings
            labels = targets
            writer.add_embedding(mat, labels, tag='EmbeddingsAll', global_step=epoch+1)
        return correct_1 * 100, correct_10 * 100
Exemple #15
0
def embed_map(embeddings, images, exp):
    import tensorflow
    from torch.utils.tensorboard import SummaryWriter
    import tensorboard

    tensorflow.io.gfile = tensorboard.compat.tensorflow_stub.io.gfile
    writer = SummaryWriter(log_dir=os.path.join("./results", exp))
    writer.add_embedding(embeddings, label_img=images)
    writer.close()
Exemple #16
0
    def save_to_tensorboard(self, embeddings, save_dir):
        metadata, embeds = [], []
        for (name, category), person_embeds in embeddings.items():
            metadata += [f'{category}: {name}'] * person_embeds.shape[0]
            embeds.append(person_embeds)
        embeds = np.concatenate(embeds, axis=0)

        writer = SummaryWriter(log_dir=save_dir)
        writer.add_embedding(embeds, metadata=metadata)
        writer.close()
Exemple #17
0
def train_word2vec(base_dir, train_file, embedding_dim, num_iters):
    train_data_raw = pd.read_csv(train_file,
                                 usecols=["movieId", "userId"],
                                 na_filter=False)
    try:
        max_id = read_info_file(train_file.parent).get("num_items") + 1
    except FileNotFoundError:
        max_id = read_info_file(train_file.parent.parent).get("num_items") + 1

    max_id = int(max_id)

    train_data = train_data_raw.groupby("userId").agg(list).squeeze()

    # Learn an unknown token embedding. Therefore we sample 50 items randomly from the long tail and mask them to one vector
    items_train = np.unique(train_data_raw.movieId.values.ravel())
    candidates = items_train[:int(len(items_train) / 2)]
    unk_items = np.random.choice(candidates, 50, replace=False)
    print("Replacing {} items with the unknown token symbol".format(unk_items))
    unk_id = max_id + 1
    fn = np.vectorize(lambda x: str(x) if x not in unk_items else str(unk_id))
    train_data = train_data.map(lambda x: fn(x).tolist())
    string_corpus = train_data.values.tolist()
    print(len(list(filter(lambda x: str(unk_id) in x, string_corpus))))
    logging.info("Pretraining the word2vec model")

    word2vec_dir = base_dir / "word2vec"
    word2vec_dir.mkdir(exist_ok=True)
    for dim in embedding_dim:
        dim_dir = word2vec_dir / str(dim)
        dim_dir.mkdir(exist_ok=True)
        writer = SummaryWriter(dim_dir)
        w2v = Word2Vec(string_corpus,
                       size=dim,
                       compute_loss=True,
                       callbacks=[
                           Word2VecCallback(writer),
                           WordVectorDumpCallback(dim_dir, embedding_dim)
                       ],
                       min_count=1,
                       workers=32,
                       iter=num_iters,
                       window=3)
        wv = w2v.wv
        embedding_tensor = torch.zeros(max_id, dim, dtype=torch.float32)
        for str_id in map(str, items_train):
            if str_id in wv:
                embedding_tensor[int(str_id)] = torch.as_tensor(wv[str_id])
        empty_indices = set(range(1, max_id)) - set(items_train)
        embedding_tensor[torch.as_tensor(list(empty_indices),
                                         dtype=torch.long)] = torch.as_tensor(
                                             wv[str(unk_id)])
        torch.save(embedding_tensor,
                   word2vec_dir / "tensor_{}.pkl".format(dim))
        writer.add_embedding(embedding_tensor, tag=str(dim))
def writer_init(embedding_files,
                image=None,
                visual_name='embedding_discipline'):
    writer = SummaryWriter('runs/' + visual_name)
    embedding = np.load(embedding_files)
    if image is not None:
        image_label = np.repeat(image, embedding.shape[0]).reshape(
            (-1, image.shape[0], image.shape[1], image.shape[2]))
    else:
        image_label = None
    print(image_label.shape)
    writer.add_embedding(embedding, label_img=torch.from_numpy(image_label))
Exemple #19
0
def projector_word_embeddings(learn=None, layer=None, vocab=None, limit=-1, start=0, log_dir=None):
    "Extracts and exports word embeddings from language models embedding layers"
    if not layer:
        if   isinstance(learn, LMLearner):   layer = learn.model[0].encoder
        elif isinstance(learn, TextLearner): layer = learn.model[0].module.encoder
    emb = layer.weight
    img = torch.full((len(emb),3,8,8), 0.7)
    vocab = learn.dls.vocab[0] if vocab == None else vocab
    vocab = list(map(lambda x: f'{x}_', vocab))
    writer = SummaryWriter(log_dir=log_dir)
    end = start + limit if limit >= 0 else -1
    writer.add_embedding(emb[start:end], metadata=vocab[start:end], label_img=img[start:end])
    writer.close()
Exemple #20
0
def magic():
    writer = SummaryWriter()
    all_files = os.listdir("backup/")
    for file_ in tqdm(all_files):
        pos = file_.find(".pth")
        if pos > 0:
            name_of_epoch = file_[:pos]
            word_vectors = process_file(file_)
            writer.add_embedding(word_vectors,
                                 metadata=words,
                                 tag=name_of_epoch)

    writer.close()
Exemple #21
0
def write_summary(data, output_dir):
    os.system('rm -rf ' + output_dir)
    os.system('mkdir ' + output_dir)

    writer = SummaryWriter(output_dir)
    writer.add_embedding(data['embeddings'], metadata=data['labels'])
    writer.close()

    # fix metadata manually
    metadata_file = os.path.join(output_dir, '00000/default/metadata.tsv')
    with open(metadata_file, 'w') as f:
        f.write('label\ttext\n')
        for label, event_text in zip(data['labels'], data['event_texts']):
            f.write(str(label.item()) + '\t' + event_text + '\n')
def wordEmbedding_demo():
    # 实例化写入对象
    writer = SummaryWriter()
    # 随机初始化一个100*5的矩阵用于演示
    embedded = torch.randn(100, 5)

    # 导入准备好的100个词汇
    meta = list(
        map(
            lambda x: x.strip(),
            fileinput.FileInput(
                "/Users/songqinghu/Desktop/baiduyun/data/nlp/proprecess/vocab100.csv"
            )))
    print(meta)
    writer.add_embedding(embedded, metadata=meta)
    writer.close()
class VisualizerTensorboard:
    def __init__(self, opts):
        self.dtype = {}
        self.iteration = 1
        self.writer = SummaryWriter(opts.logs_dir)

    def register(self, modules):
        # here modules are assumed to be a dictionary
        for key in modules:
            self.dtype[key] = modules[key]['dtype']

    def update(self, modules):
        for key, value in modules:
            if self.dtype[key] == 'scalar':
                self.writer.add_scalar(key, value, self.iteration)
            elif self.dtype[key] == 'scalars':
                self.writer.add_scalars(key, value, self.iteration)
            elif self.dtype[key] == 'histogram':
                self.writer.add_histogram(key, value, self.iteration)
            elif self.dtype[key] == 'image':
                self.writer.add_image(key, value, self.iteration)
            elif self.dtype[key] == 'images':
                self.writer.add_images(key, value, self.iteration)
            elif self.dtype[key] == 'figure':
                self.writer.add_figure(key, value, self.iteration)
            elif self.dtype[key] == 'video':
                self.writer.add_video(key, value, self.iteration)
            elif self.dtype[key] == 'audio':
                self.writer.add_audio(key, value, self.iteration)
            elif self.dtype[key] == 'text':
                self.writer.add_text(key, value, self.iteration)
            elif self.dtype[key] == 'embedding':
                self.writer.add_embedding(key, value, self.iteration)
            elif self.dtype[key] == 'pr_curve':
                self.writer.pr_curve(key, value['labels'],
                                     value['predictions'], self.iteration)
            elif self.dtype[key] == 'mesh':
                self.writer.add_audio(key, value, self.iteration)
            elif self.dtype[key] == 'hparams':
                self.writer.add_hparams(key, value['hparam_dict'],
                                        value['metric_dict'], self.iteration)
            else:
                raise Exception(
                    'Data type not supported, please update the visualizer plugin and rerun !!'
                )

        self.iteration = self.iteration + 1
Exemple #24
0
class TensorboardLogger:
    def __init__(self, log_dir):
        self.writer = SummaryWriter(log_dir=log_dir)
        self.supported_types = [
            'scalar', 'histogram', 'image', 'graph', 'embedding'
        ]

    def save_dict(self, kvs, step):
        for key in kvs.keys():
            if key not in self.supported_types:
                raise TypeError(f'Unsupported data type in tensorboard: {key}')

        for tp in self.supported_types[:3]:
            if tp in kvs:
                for k, v in kvs[tp].items():
                    getattr(self, f'save_{tp}')(k, v, step)
        for tp in self.supported_types[3:]:
            if tp in kvs:
                getattr(self, f'save_{tp}')(**kvs[tp])

    def save_scalar(self, tag, scalar, step):
        self.writer.add_scalar(tag, scalar, step)

    def save_histogram(self, tag, tensor, step):
        self.writer.add_histogram(tag, tensor, step)

    def save_image(self, tag, tensor, step):
        self.writer.add_image(tag, tensor, step)

    def save_graph(self, model, input_to_model=None):
        self.writer.add_graph(model, input_to_model)

    def save_embedding(self,
                       tensor,
                       metadata=None,
                       label_img=None,
                       step=None,
                       tag='default'):
        self.writer.add_embedding(tensor,
                                  metadata=metadata,
                                  label_img=label_img,
                                  global_step=step,
                                  tag=tag)

    def __del__(self):
        self.writer.flush()
        self.writer.close()
Exemple #25
0
class Logger(object):
    def __init__(self, path):
        self.writer = SummaryWriter(os.path.join(path, 'runs'))

    def show_img_grid(self, images, info='pictures'):
        """
        show a batch of images
        images:a batch of tensors which are a batch of images
        info:the information about the pictures
        """
        img_grid = torchvision.utils.make_grid(images)
        self.writer.add_image(info, img_grid)

    def add_projector(self, feature, label=None, img=None):
        """
        visualize the lower dimensional representation of higher dimensional data
        feature:a matrix which each row is the feature vector of the data point
        label:label
        img:label_img
        """
        self.writer.add_embedding(mat=feature, metadata=label, label_img=img)

    def draw_scalars(self, tag, scalars, start_iteration=0):
        """
         draw the list curve,such as loss accuracy
         list:the list of values
         start_iteration:the start iteration of training,default is 0,means train from scratch
        """
        for i, item in enumerate(scalars):
            self.writer.add_scalar(tag, item, start_iteration + i)

    def add_pr_curve_tensorboard(self,
                                 tag,
                                 labels,
                                 predictions,
                                 global_step=0):
        """
        Takes in a class index and plots the corresponding
        precision-recall curve
        """
        self.writer.add_pr_curve(tag,
                                 labels,
                                 predictions,
                                 global_step=global_step)

    def close(self):
        self.writer.close()
Exemple #26
0
        def test_embedding_64(self):
            w = SummaryWriter()
            all_features = torch.Tensor([[1, 2, 3], [5, 4, 1], [3, 7, 7]])
            all_labels = torch.Tensor([33, 44, 55])
            all_images = torch.zeros((3, 3, 5, 5), dtype=torch.float64)

            w.add_embedding(all_features,
                            metadata=all_labels,
                            label_img=all_images,
                            global_step=2)

            dataset_label = ['test'] * 2 + ['train'] * 2
            all_labels = list(zip(all_labels, dataset_label))
            w.add_embedding(all_features,
                            metadata=all_labels,
                            label_img=all_images,
                            metadata_header=['digit', 'dataset'],
                            global_step=2)
Exemple #27
0
def embeddings(
    dataset_name: str, embeddings_name: str, metadata: str, tag: List[str]
) -> None:
    """Add the embeddings in the given path to Tensorboard."""
    from pandas import read_csv
    from vscvs.utils import sprite_tensor

    classes = read_csv(metadata, delimiter="\t")["class"]
    embeddings_tensor = load_embeddings(embeddings_name)
    writer = SummaryWriter(get_path("tensorboard", "embeddings", embeddings_name))
    writer.add_embedding(
        embeddings_tensor,
        metadata=classes,
        tag="/".join((embeddings_name,) + tag),
        label_img=sprite_tensor(dataset_name),
    )
    writer.close()
    click.echo("Embeddings added to Tensorboard: {}".format(embeddings_name))
Exemple #28
0
class EmbeddingVisualiser:
    """Visualize an embedding in tensorboard
    """
    def __init__(self, df, label_cols, feat_cols, session_name=""):
        self.session_name = session_name
        self.labels = df[label_cols]
        self.features = df[feat_cols]
        self.writer = SummaryWriter(
            log_dir=os.path.join('.', 'embedding', self.session_name))

    def visualize(self, embeddings_folder):
        self.writer.add_embedding(self.features.values,
                                  metadata=self.labels["colors"].values,
                                  tag="colors")

        self.writer.add_embedding(self.features.values,
                                  metadata=self.labels["is_reference"].values,
                                  tag="Munsell points highlight")
class TensorboardWriter:
    def __init__(self):
        writer_path = os.path.join(config.tensorboard.tensorboard_path, config.tensorboard.experiment_name)
        self.writer = SummaryWriter(writer_path)

    def add_scalar(self, tag: str, x: int, y: float):
        self.writer.add_scalar(tag=tag, scalar_value=y, global_step=x)
        self.writer.flush()

    def add_embedding(self, tag: str, features: np.ndarray, labels: np.ndarray):
        assert features.shape[0] == labels.shape[0]
        assert features.ndim == 2

        self.writer.add_embedding(mat=features, metadata=labels, tag=tag)
        self.writer.flush()

    def close(self):
        self.writer.close()
def test(args, mnasnet, model, data_loader, show_image_on_board=True):
    writer = SummaryWriter()
    images = []
    labels = []
    weights = []    
    with torch.no_grad():
        for i, (image, label) in enumerate(data_loader):
            image = image.to(device)
            images.append(image.squeeze(0).numpy())
            label = label.squeeze(0).numpy()[0]
            labels.append(label)

            x = mnasnet(image)
            recon_x = model(x)
            weights.append(x.squeeze(0).numpy())

    weights = torch.FloatTensor(weights)
    writer.add_embedding(weights, metadata=labels)
    print("done")