Skip to content

Commit

Permalink
make visdom optional, port modifiable, and print visdom hostname
Browse files Browse the repository at this point in the history
Former-commit-id: c85cbfed67945582a9be470f296cdbaa8c330e99
  • Loading branch information
Javi Ribera committed Dec 5, 2018
1 parent 05f9531 commit 2b3ce85
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 11 deletions.
7 changes: 5 additions & 2 deletions object-locator/argparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,13 @@ def parse_command_args(training_or_testing):
metavar='NAME',
help='name of the environment in Visdom')
parser.add_argument('--visdom-server',
default='http://localhost',
type=str,
default=None,
metavar='SRV',
help='Hostname of the Visdom server')
parser.add_argument('--visdom-port',
default=8989,
metavar='PRT',
help='Port of the Visdom server')
parser.add_argument('--optimizer', '--optim',
default='sgd',
type=str.lower,
Expand Down
47 changes: 40 additions & 7 deletions object-locator/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,49 @@
import visdom
import torch
import numbers
from . import utils

from torch.autograd import Variable

class Logger():
def __init__(self,
env_name='main',
server='http://localhost'):
server=None,
port=8989,
env_name='main'):
"""
Logger that connects to a Visdom server
and sends training losses/metrics and images of any kind.
:param server: Host name of the server (e.g, http://localhost),
without the port number. If None,
this Logger will do nothing at all
(it will not connect to any server,
and the functions here will do nothing).
:param port: Port number of the Visdom server.
:param env_name: Name of the environment within the Visdom
server where everything you sent to it will go.
:param terms_legends: Legend of each term.
"""

# Visdom setup
self.client = visdom.Visdom(server=server,
env=env_name,
port=8989)
if server is None:
self.train_losses = utils.nothing
self.val_losses = utils.nothing
self.image = utils.nothing
print('W: Not connected to any Visdom server. '
'You will not visualize any training/validation plot '
'or intermediate image')
else:
# Connect to Visdom
self.client = visdom.Visdom(server=server,
env=env_name,
port=port)
if self.client.check_connection():
print(f'Connected to Visdom server '
f'{server}:{port}')
else:
print(f'E: cannot connect to Visdom server '
f'{server}:{port}')
exit(-1)

# Each of the 'windows' in visdom web panel
self.viz_train_input_win = None
Expand All @@ -35,8 +66,10 @@ def __init__(self,
# Visdom only supports CPU Tensors
self.device = torch.device("cpu")


def train_losses(self, terms, iteration_number, terms_legends=None):
"""Plot a new point of the training losses (scalars) to Visdom.
"""
Plot a new point of the training losses (scalars) to Visdom.
All losses will be plotted in the same figure/window.
:param terms: List of scalar losses.
Expand Down
5 changes: 3 additions & 2 deletions object-locator/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@
torch.cuda.manual_seed_all(args.seed)

# Visdom setup
log = logger.Logger(env_name=args.visdom_env,
server=args.visdom_server)
log = logger.Logger(server=args.visdom_server,
port=args.visdom_port,
env_name=args.visdom_env)

# Data loading code
training_transforms = []
Expand Down
6 changes: 6 additions & 0 deletions object-locator/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,3 +317,9 @@ def paint_circles(img, points, color='red', crosshair=False):
img = np.moveaxis(img, 2, 0)

return img


def nothing(*args, **kwargs):
""" A useless function that does nothing at all. """
pass

0 comments on commit 2b3ce85

Please sign in to comment.