Skip to content

Commit

Permalink
Apply PEP 8 formatting (#235)
Browse files Browse the repository at this point in the history
* autopep8 formatting for examples

Formats all demo_*.py files for better readability and code style

* autopep8 format all files in tensorboardX/

* autopep8 for examples/ again

* fix py2.7 encoding error for CI
  • Loading branch information
orionr authored and lanpa committed Oct 5, 2018
1 parent 8a7925b commit 5406e95
Show file tree
Hide file tree
Showing 21 changed files with 308 additions and 189 deletions.
62 changes: 34 additions & 28 deletions demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,44 +11,50 @@
sample_rate = 44100
freqs = [262, 294, 330, 349, 392, 440, 440, 440, 440, 440, 440]

true_positive_counts=[75, 64, 21, 5, 0]
false_positive_counts=[150, 105, 18, 0, 0]
true_negative_counts=[0, 45, 132, 150, 150]
false_negative_counts=[0, 11, 54, 70, 75]
precision=[0.3333333, 0.3786982, 0.5384616, 1.0, 0.0]
recall=[1.0, 0.8533334, 0.28, 0.0666667, 0.0]
true_positive_counts = [75, 64, 21, 5, 0]
false_positive_counts = [150, 105, 18, 0, 0]
true_negative_counts = [0, 45, 132, 150, 150]
false_negative_counts = [0, 11, 54, 70, 75]
precision = [0.3333333, 0.3786982, 0.5384616, 1.0, 0.0]
recall = [1.0, 0.8533334, 0.28, 0.0666667, 0.0]


for n_iter in range(100):
s1 = torch.rand(1) # value to keep
s1 = torch.rand(1) # value to keep
s2 = torch.rand(1)
writer.add_scalar('data/scalar_systemtime', s1[0], n_iter) # data grouping by `slash`
writer.add_scalar('data/scalar_customtime', s1[0], n_iter, walltime=n_iter) # data grouping by `slash`
writer.add_scalars('data/scalar_group', {"xsinx":n_iter*np.sin(n_iter),
"xcosx":n_iter*np.cos(n_iter),
# data grouping by `slash`
writer.add_scalar('data/scalar_systemtime', s1[0], n_iter)
# data grouping by `slash`
writer.add_scalar('data/scalar_customtime', s1[0], n_iter, walltime=n_iter)
writer.add_scalars('data/scalar_group', {"xsinx": n_iter * np.sin(n_iter),
"xcosx": n_iter * np.cos(n_iter),
"arctanx": np.arctan(n_iter)}, n_iter)
x = torch.rand(32, 3, 64, 64) # output from network
if n_iter%10==0:
x = torch.rand(32, 3, 64, 64) # output from network
if n_iter % 10 == 0:
x = vutils.make_grid(x, normalize=True, scale_each=True)
writer.add_image('Image', x, n_iter) # Tensor
#writer.add_image('astronaut', skimage.data.astronaut(), n_iter) # numpy
#writer.add_image('imread', skimage.io.imread('screenshots/audio.png'), n_iter) # numpy
x = torch.zeros(sample_rate*2)
writer.add_image('Image', x, n_iter) # Tensor
# writer.add_image('astronaut', skimage.data.astronaut(), n_iter) # numpy
# writer.add_image('imread',
# skimage.io.imread('screenshots/audio.png'), n_iter) # numpy
x = torch.zeros(sample_rate * 2)
for i in range(x.size(0)):
x[i] = np.cos(freqs[n_iter//10]*np.pi*float(i)/float(sample_rate)) # sound amplitude should in [-1, 1]
# sound amplitude should in [-1, 1]
x[i] = np.cos(freqs[n_iter // 10] * np.pi *
float(i) / float(sample_rate))
writer.add_audio('myAudio', x, n_iter)
writer.add_text('Text', 'text logged at step:'+str(n_iter), n_iter)
writer.add_text('Text', 'text logged at step:' + str(n_iter), n_iter)
writer.add_text('markdown Text', '''a|b\n-|-\nc|d''', n_iter)
for name, param in resnet18.named_parameters():
if 'bn' not in name:
if 'bn' not in name:
writer.add_histogram(name, param, n_iter)
writer.add_pr_curve('xoxo', np.random.randint(2, size=100), np.random.rand(100), n_iter) #needs tensorboard 0.4RC or later
writer.add_pr_curve('xoxo', np.random.randint(2, size=100), np.random.rand(
100), n_iter) # needs tensorboard 0.4RC or later
writer.add_pr_curve_raw('prcurve with raw data', true_positive_counts,
false_positive_counts,
true_negative_counts,
false_negative_counts,
precision,
recall, n_iter)
false_positive_counts,
true_negative_counts,
false_negative_counts,
precision,
recall, n_iter)
# export scalar data to JSON for external processing
writer.export_scalars_to_json("./all_scalars.json")

Expand All @@ -66,14 +72,14 @@
all_features = torch.cat((features, features_train))
all_labels = torch.cat((label, labels_train))
all_images = torch.cat((images, images_train))
dataset_label = ['test']*100 + ['train']*100
dataset_label = ['test'] * 100 + ['train'] * 100
all_labels = list(zip(all_labels, dataset_label))

writer.add_embedding(all_features, metadata=all_labels, label_img=all_images.unsqueeze(1),
metadata_header=['digit', 'dataset'], global_step=2)

# VIDEO
vid_images = dataset.train_data[:16*48]
vid_images = dataset.train_data[:16 * 48]
vid = vid_images.view(16, 1, 48, 28, 28) # BxCxTxHxW

writer.add_video('video', vid_tensor=vid)
Expand Down
2 changes: 2 additions & 0 deletions examples/chainer/extension_logger/train_dcgan.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
from visualize import out_generated_image
from tensorboardX import SummaryWriter
from writetnesorboard import LogTensorboard


def main():
parser = argparse.ArgumentParser(description='Chainer example: DCGAN')
parser.add_argument('--batchsize', '-b', type=int, default=50,
Expand Down
3 changes: 2 additions & 1 deletion examples/chainer/extension_logger/writetnesorboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ def __call__(self, trainer):
summary.add({k: observation[k] for k in keys if k in observation})
for k, v in observation.items():
#self._logger.add_scalar(k, chainer.cuda.to_cpu(observation[k].data), trainer.updater.iteration)
self._logger.add_scalar(k, observation[k], trainer.updater.iteration)
self._logger.add_scalar(
k, observation[k], trainer.updater.iteration)
if self._trigger(trainer):
# output the result
stats = self._summary.compute_mean()
Expand Down
15 changes: 9 additions & 6 deletions examples/chainer/plain_logger/train_vae.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@
(model.loss, ))
o.write(g.dump())
print('graph generated')
writer.add_scalar('train/loss', model.loss, epoch*N+i)
writer.add_scalar('train/rec_loss', model.rec_loss, epoch*N+i)
writer.add_scalar('train/loss', model.loss, epoch * N + i)
writer.add_scalar('train/rec_loss', model.rec_loss, epoch * N + i)
sum_loss += float(model.loss.data) * len(x.data)
sum_rec_loss += float(model.rec_loss.data) * len(x.data)

Expand All @@ -123,10 +123,13 @@
loss_func(x)
sum_loss += float(model.loss.data) * len(x.data)
sum_rec_loss += float(model.rec_loss.data) * len(x.data)
writer.add_scalar('test/loss', model.loss, epoch*N_test+i)
writer.add_scalar('test/rec_loss', model.rec_loss, epoch*N_test+i)
writer.add_image('reconstructed', model(x).reshape(-1,1,28,28), epoch*N_test+i)
writer.add_image('input', x.reshape(-1,1,28,28), epoch*N_test+i)
writer.add_scalar('test/loss', model.loss, epoch * N_test + i)
writer.add_scalar('test/rec_loss', model.rec_loss,
epoch * N_test + i)
writer.add_image('reconstructed', model(
x).reshape(-1, 1, 28, 28), epoch * N_test + i)
writer.add_image('input', x.reshape(-1, 1, 28, 28),
epoch * N_test + i)
del model.loss
print('test mean loss={}, mean reconstruction loss={}'
.format(sum_loss / N_test, sum_rec_loss / N_test))
Expand Down
18 changes: 8 additions & 10 deletions examples/demo_beholder.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,23 @@
from __future__ import division
from __future__ import print_function

import argparse
import sys
import torch
import numpy as np
import tensorboardX.beholder as beholder_lib
from collections import namedtuple
import time

LOG_DIRECTORY = '/tmp/beholder-demo'
from collections import namedtuple


import time
LOG_DIRECTORY = '/tmp/beholder-demo'
tensor_and_name = namedtuple('tensor_and_name', 'tensor, name')


def beholder_pytorch():
for i in range(1000):
fake_param = [tensor_and_name(np.random.randn(128, 768, 3), 'test'+str(i)) for i in range(5)]
arrays = [tensor_and_name(np.random.randn(128, 768, 3), 'test'+str(i)) for i in range(5)]
fake_param = [tensor_and_name(np.random.randn(128, 768, 3), 'test' + str(i))
for i in range(5)]
arrays = [tensor_and_name(np.random.randn(128, 768, 3), 'test' + str(i))
for i in range(5)]
beholder = beholder_lib.Beholder(logdir=LOG_DIRECTORY)
beholder.update(
trainable=fake_param,
Expand All @@ -46,7 +46,6 @@ def beholder_pytorch():
)
time.sleep(0.1)
print(i)



if __name__ == '__main__':
Expand All @@ -55,4 +54,3 @@ def beholder_pytorch():
os.makedirs(LOG_DIRECTORY)
print(LOG_DIRECTORY)
beholder_pytorch()

18 changes: 11 additions & 7 deletions examples/demo_caffe2.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ def DownloadResource(url, path):
print("Your data folder was not found!! This was generated: {}".format(data_folder))

# Look for existing database: lmdb
if os.path.exists(os.path.join(data_folder,"mnist-train-nchw-lmdb")):
if os.path.exists(os.path.join(data_folder, "mnist-train-nchw-lmdb")):
print("lmdb train db found!")
else:
db_missing = True

if os.path.exists(os.path.join(data_folder,"mnist-test-nchw-lmdb")):
if os.path.exists(os.path.join(data_folder, "mnist-test-nchw-lmdb")):
print("lmdb test db found!")
else:
db_missing = True
Expand All @@ -64,7 +64,8 @@ def DownloadResource(url, path):
try:
DownloadResource(db_url, data_folder)
except Exception as ex:
print("Failed to download dataset. Please download it manually from {}".format(db_url))
print(
"Failed to download dataset. Please download it manually from {}".format(db_url))
print("Unzip it and place the two database folders here: {}".format(data_folder))
raise ex

Expand Down Expand Up @@ -92,7 +93,7 @@ def AddInput(model, batch_size, db, db_type):
# cast the data to float
data = model.Cast(data_uint8, "data", to=core.DataType.FLOAT)
# scale data from [0,255] down to [0,1]
data = model.Scale(data, data, scale=float(1./256))
data = model.Scale(data, data, scale=float(1. / 256))
# don't need the gradient for the backward pass
data = model.StopGradient(data, data)
return data, label
Expand All @@ -117,7 +118,8 @@ def AddLeNetModel(model, data):
conv2 = brew.conv(model, pool1, 'conv2', dim_in=20, dim_out=100, kernel=5)
# Image size: 8 x 8 -> 4 x 4
pool2 = brew.max_pool(model, conv2, 'pool2', kernel=2, stride=2)
# 50 * 4 * 4 stands for dim_out from previous layer multiplied by the image size
# 50 * 4 * 4 stands for dim_out from previous layer multiplied by the
# image size
fc3 = brew.fc(model, pool2, 'fc3', dim_in=100 * 4 * 4, dim_out=500)
relu = brew.relu(model, fc3, fc3)
pred = brew.fc(model, relu, 'pred', 500, 10)
Expand All @@ -138,13 +140,14 @@ def AddTrainingOperators(model, softmax, label):
loss = model.AveragedLoss(xent, "loss")
# track the accuracy of the model
AddAccuracy(model, softmax, label)
# use the average loss we just computed to add gradient operators to the model
# use the average loss we just computed to add gradient operators to the
# model
model.AddGradientOperators([loss])
# do a simple stochastic gradient descent
ITER = brew.iter(model, "iter")
# set the learning rate schedule
LR = model.LearningRate(
ITER, "LR", base_lr=-0.1, policy="step", stepsize=1, gamma=0.999 )
ITER, "LR", base_lr=-0.1, policy="step", stepsize=1, gamma=0.999)
# ONE is a constant value that is used in the gradient update. We only need
# to create it once, so it is explicitly placed in param_init_net.
ONE = model.param_init_net.ConstantFill([], "ONE", shape=[1], value=1.0)
Expand Down Expand Up @@ -179,6 +182,7 @@ def AddBookkeepingOperators(model):
# demo, we will only show how to summarize the parameters and their
# gradients.


arg_scope = {"order": "NCHW"}
train_model = model_helper.ModelHelper(name="mnist_train", arg_scope=arg_scope)
data, label = AddInput(
Expand Down
18 changes: 10 additions & 8 deletions examples/demo_custom_scalars.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
from numpy.random import rand
from tensorboardX import SummaryWriter
import time


with SummaryWriter() as writer:
for n_iter in range(100):
writer.add_scalar('twse/0050', rand(), n_iter)
writer.add_scalar('twse/2330', rand(), n_iter)
t = rand()
writer.add_scalar('dow/aaa', t, n_iter)
writer.add_scalar('dow/bbb', t-1, n_iter)
writer.add_scalar('dow/ccc', t+1, n_iter)
writer.add_scalar('dow/bbb', t - 1, n_iter)
writer.add_scalar('dow/ccc', t + 1, n_iter)
writer.add_scalar('nasdaq/aaa', rand(), n_iter)
writer.add_scalar('nasdaq/bbb', rand(), n_iter)
writer.add_scalar('nasdaq/ccc', rand(), n_iter)

layout = {'Taiwan':{'twse':['Multiline',['twse/0050', 'twse/2330']]},
'USA':{'dow':['Margin',['dow/aaa', 'dow/bbb', 'dow/ccc']],
'nasdaq':['Margin',['nasdaq/aaa', 'nasdaq/bbb', 'nasdaq/ccc']]}}
layout = {'Taiwan': {'twse': ['Multiline', ['twse/0050', 'twse/2330']]},
'USA': {'dow': ['Margin', ['dow/aaa', 'dow/bbb', 'dow/ccc']],
'nasdaq': ['Margin', ['nasdaq/aaa', 'nasdaq/bbb', 'nasdaq/ccc']]}}
writer.add_custom_scalars(layout)
# writer.add_custom_scalars(layout) second call has no effect

Expand All @@ -34,7 +36,7 @@
for n_iter in range(100):
t = rand()
writer.add_scalar('dow/aaa', t, n_iter)
writer.add_scalar('dow/bbb', t-1, n_iter)
writer.add_scalar('dow/ccc', t+1, n_iter)
writer.add_scalar('dow/bbb', t - 1, n_iter)
writer.add_scalar('dow/ccc', t + 1, n_iter)

writer.add_custom_scalars_marginchart(['dow/aaa', 'dow/bbb', 'dow/ccc'])
writer.add_custom_scalars_marginchart(['dow/aaa', 'dow/bbb', 'dow/ccc'])
Loading

0 comments on commit 5406e95

Please sign in to comment.