# Path: tensorboardX/torchvis.py
# class TorchVis:
#     def __init__(self, *args, **init_kwargs):
#         """
#         Args:
#             args (list of strings): The name of the visualization target(s).
#               Accepted targets are 'tensorboard' and 'visdom'.
#             init_kwargs: Additional keyword parameters for the visdom writer (For example, server IP).
#               See `visdom doc <https://github.com/facebookresearch/visdom/blob
#               /master/README.md#visdom-arguments-python-only>`_ for more.
#         """
#         self.subscribers = {}
#         self.register(*args, **init_kwargs)
# 
#     def register(self, *args, **init_kwargs):
#         # Sets tensorboard as the default visualization format if not specified
#         formats = ['tensorboard'] if not args else args
#         for format in formats:
#             if self.subscribers.get(format) is None and format in vis_formats.keys():
#                 self.subscribers[format] = vis_formats[format](**init_kwargs.get(format, {}))
# 
#     def unregister(self, *args):
#         for format in args:
#             self.subscribers[format].close()
#             del self.subscribers[format]
#             gc.collect()
# 
#     def __getattr__(self, attr):
#         for _, subscriber in six.iteritems(self.subscribers):
#             def wrapper(*args, **kwargs):
#                 for _, subscriber in six.iteritems(self.subscribers):
#                     if hasattr(subscriber, attr):
#                         getattr(subscriber, attr)(*args, **kwargs)
#             return wrapper
#         raise AttributeError
# 
#     # Handle writer management (open/close) for the user
#     def __del__(self):
#         for _, subscriber in six.iteritems(self.subscribers):
#             subscriber.close()


    import caffe2.python.predictor.predictor_exporter as pe
import numpy as np
import os
import shutil
    import requests
    import zipfile
from caffe2.python import core, model_helper, workspace, brew
from tensorboardX import TorchVis
    from six import BytesIO
if os.path.exists(os.path.join(data_folder, "mnist-test-nchw-lmdb")):
    print("lmdb test db found!")
else:
    db_missing = True

# attempt the download of the db if either was missing
if db_missing:
    print("one or both of the MNIST lmbd dbs not found!!")
    db_url = "http://download.caffe2.ai/databases/mnist-lmdb.zip"
    try:
        DownloadResource(db_url, data_folder)
    except Exception as ex:
        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

if os.path.exists(root_folder):
    print("Looks like you ran this before, so we need to cleanup those old files...")
    shutil.rmtree(root_folder)

os.makedirs(root_folder)
workspace.ResetWorkspace(root_folder)

print("training data folder:" + data_folder)
print("workspace root folder:" + root_folder)

# END DATA PREPARATION #

# Create TorchVis in preparation for writing. Default format is 'tensorboard'

# Path: tensorboardX/torchvis.py
# class TorchVis:
#     def __init__(self, *args, **init_kwargs):
#         """
#         Args:
#             args (list of strings): The name of the visualization target(s).
#               Accepted targets are 'tensorboard' and 'visdom'.
#             init_kwargs: Additional keyword parameters for the visdom writer (For example, server IP).
#               See `visdom doc <https://github.com/facebookresearch/visdom/blob
#               /master/README.md#visdom-arguments-python-only>`_ for more.
#         """
#         self.subscribers = {}
#         self.register(*args, **init_kwargs)
# 
#     def register(self, *args, **init_kwargs):
#         # Sets tensorboard as the default visualization format if not specified
#         formats = ['tensorboard'] if not args else args
#         for format in formats:
#             if self.subscribers.get(format) is None and format in vis_formats.keys():
#                 self.subscribers[format] = vis_formats[format](**init_kwargs.get(format, {}))
# 
#     def unregister(self, *args):
#         for format in args:
#             self.subscribers[format].close()
#             del self.subscribers[format]
#             gc.collect()
# 
#     def __getattr__(self, attr):
#         for _, subscriber in six.iteritems(self.subscribers):
#             def wrapper(*args, **kwargs):
#                 for _, subscriber in six.iteritems(self.subscribers):
#                     if hasattr(subscriber, attr):
#                         getattr(subscriber, attr)(*args, **kwargs)
#             return wrapper
#         raise AttributeError
# 
#     # Handle writer management (open/close) for the user
#     def __del__(self):
#         for _, subscriber in six.iteritems(self.subscribers):
#             subscriber.close()


# Path: examples/demo_caffe2.py
    import caffe2.python.predictor.predictor_exporter as pe
import numpy as np
import os
import shutil
    import requests
    import zipfile
from caffe2.python import core, model_helper, workspace, brew
from tensorboardX import TorchVis
    from six import BytesIO

if os.path.exists(os.path.join(data_folder, "mnist-test-nchw-lmdb")):
    print("lmdb test db found!")
else:
    db_missing = True

# attempt the download of the db if either was missing
if db_missing:
    print("one or both of the MNIST lmbd dbs not found!!")
    db_url = "http://download.caffe2.ai/databases/mnist-lmdb.zip"
    try:
        DownloadResource(db_url, data_folder)
    except Exception as ex:
        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

if os.path.exists(root_folder):
    print("Looks like you ran this before, so we need to cleanup those old files...")
    shutil.rmtree(root_folder)

os.makedirs(root_folder)
workspace.ResetWorkspace(root_folder)

print("training data folder:" + data_folder)
print("workspace root folder:" + root_folder)

# END DATA PREPARATION #

# Create TorchVis in preparation for writing. Default format is 'tensorboard'

tv = TorchVis()