Exemplo n.º 1
0
    def plot_network_architecture(self):
        try:
            from batchgenerators.utilities.file_and_folder_operations import join
            import hiddenlayer as hl
            if torch.cuda.is_available():
                g = hl.build_graph(self.network,
                                   torch.rand((1, self.num_input_channels,
                                               *self.patch_size)).cuda(),
                                   transforms=None)
            else:
                g = hl.build_graph(self.network,
                                   torch.rand((1, self.num_input_channels,
                                               *self.patch_size)),
                                   transforms=None)
            g.save(join(self.output_folder, "network_architecture.pdf"))
            del g
        except Exception as e:
            self.print_to_log_file("Unable to plot network architecture:")
            self.print_to_log_file(e)

            self.print_to_log_file("\nprinting the network instead:\n")
            self.print_to_log_file(self.network)
            self.print_to_log_file("\n")
        finally:
            if torch.cuda.is_available():
                torch.cuda.empty_cache()
Exemplo n.º 2
0
    def test_graph(self):
        model = torchvision.models.vgg16()
        g = hl.build_graph(model, torch.zeros([1, 3, 224, 224]))
        g.save(os.path.join(OUTPUT_DIR, "pytorch_vgg16.pdf"))

        model = torchvision.models.resnet50()
        g = hl.build_graph(model, torch.zeros([1, 3, 224, 224]))
        g.save(os.path.join(OUTPUT_DIR, "pytorch_resnet50.pdf"))

        # Clean up
        shutil.rmtree(OUTPUT_DIR)
Exemplo n.º 3
0
    def test_resnet_blocks(self):
        # Resnet101
        model = torchvision.models.resnet101()

        transforms = [
            # Fold Conv, BN, RELU layers into one
            ht.Fold("Conv > BatchNormalization > Relu", "ConvBnRelu"),
            # Fold Conv, BN layers together
            ht.Fold("Conv > BatchNormalization", "ConvBn"),
            # Fold bottleneck blocks
            ht.Fold(
                """
                ((ConvBnRelu > ConvBnRelu > ConvBn) | ConvBn) > Add > Relu
                """, "BottleneckBlock", "Bottleneck Block"),
            # Fold residual blocks
            ht.Fold("""ConvBnRelu > ConvBnRelu > ConvBn > Add > Relu""",
                    "ResBlock", "Residual Block"),
            # Fold repeated blocks
            ht.FoldDuplicates(),
        ]

        # Display graph using the transforms above
        g = hl.build_graph(model,
                           torch.zeros([1, 3, 224, 224]),
                           transforms=transforms)
        g.save(os.path.join(OUTPUT_DIR, "pytorch_resnet_bloks.pdf"))

        # Clean up
        shutil.rmtree(OUTPUT_DIR)
Exemplo n.º 4
0
def show_model2(model):
    """
    second way to show model by using Hiddenlayer module
    link: https://github.com/waleedka/hiddenlayer
    there is a warning, just ignore it
    :param module:
    :param model_name:
    :return:
    """
    try:
        import hiddenlayer as hl
    except ImportError:
        import os
        os.system('pip install hiddenlayer')
        import hiddenlayer as hl

    inp1 = torch.randn(1, 3, 300, 300)
    inp2 = torch.randn(1, 1, 300, 300)
    inputs = (Variable(inp1), Variable(inp2))
    # if model_name=='PSPNet':
    #     model = getattr(module, model_name)(sizes=(1, 2, 3, 6),
    #                                         psp_size=512, deep_features_size=256, backend='resnet18',pretrained=False)
    # else:
    #     model = getattr(module, model_name)(pretrained=False)
    print(model)
    g = hl.build_graph(model, inputs)
    g.save('./model_graph.pdf')
Exemplo n.º 5
0
 def __init__(self, path):
     # load pre-trained generator here
     self._model = custom_model.res18GConv(200)
     self._model.load_state_dict(torch.load(path)['model_state_dict'])
     self._model.eval()
     x = hl.build_graph(self._model, torch.zeros([1, 3, 64, 64]))
     print(type(x))
     x.save('./t.png')
Exemplo n.º 6
0
    def test_graph(self):
        m = TrafficSignsModel()

        dot = hl.build_graph(m.graph).build_dot()
        dot.format = 'pdf'
        dot.render("tf_traffic_signs", directory=OUTPUT_DIR, cleanup=True)

        # Import CIFAR from the demos folder
        sys.path.append("../demos")
        import tf_cifar10

        with tf.Session():
            with tf.Graph().as_default() as g:
                tf_cifar10.CIFAR10(data_dir=DATA_DIR).model(inputs=tf.placeholder(tf.float32, shape=(8, 32, 32, 3)))
                dot = hl.build_graph(g).build_dot()
                dot.format = 'pdf'
                dot.render("tf_cifar10", directory=OUTPUT_DIR, cleanup=True)
Exemplo n.º 7
0
def show_model(model: nn.Module):
    """
    Render graphviz representation of neural network model
    """
    hl_graph = hl.build_graph(model, torch.zeros([1, 3, 224, 224]))
    hl_graph.theme = hl.graph.THEMES["blue"].copy()

    return hl_graph
Exemplo n.º 8
0
    def visualization_compute_graph():
        import hiddenlayer as hl
        # VGG16 with BatchNorm
        model = torchvision.models.resnet18()

        # Build HiddenLayer graph
        # Jupyter Notebook renders it automatically
        g = hl.build_graph(model, torch.zeros([1, 3, 224, 224]))
        g.save("resnet18_compute_graph/resnet18", format='png')
Exemplo n.º 9
0
    def test_graph(self):
        with tf.Session():
            with tf.Graph().as_default() as g:
                nets.vgg.vgg_16(
                    tf.placeholder(tf.float32, shape=(1, 224, 224, 3)))
                dot = hl.build_graph(g).build_dot()
                dot.format = 'pdf'
                dot.render("tf_vgg16", directory=OUTPUT_DIR, cleanup=True)

        with tf.Session():
            with tf.Graph().as_default() as g:
                nets.resnet_v1.resnet_v1_50(
                    tf.placeholder(tf.float32, shape=(1, 224, 224, 3)))
                dot = hl.build_graph(g).build_dot()
                dot.format = 'pdf'
                dot.render("tf_resnet50", directory=OUTPUT_DIR, cleanup=True)

        with tf.Session():
            with tf.Graph().as_default() as g:
                nets.inception.inception_v1(
                    tf.placeholder(tf.float32, shape=(1, 224, 224, 3)))
                dot = hl.build_graph(g).build_dot()
                dot.format = 'pdf'
                dot.render("tf_inception", directory=OUTPUT_DIR, cleanup=True)

        with tf.Session():
            with tf.Graph().as_default() as g:
                nets.alexnet.alexnet_v2(
                    tf.placeholder(tf.float32, shape=(1, 224, 224, 3)))
                dot = hl.build_graph(g).build_dot()
                dot.format = 'pdf'
                dot.render("tf_alexnet", directory=OUTPUT_DIR, cleanup=True)

        with tf.Session():
            with tf.Graph().as_default() as g:
                nets.overfeat.overfeat(
                    tf.placeholder(tf.float32, shape=(1, 231, 231, 3)))
                dot = hl.build_graph(g).build_dot()
                dot.format = 'pdf'
                dot.render("tf_overfeat", directory=OUTPUT_DIR, cleanup=True)

        # Clean up
        shutil.rmtree(OUTPUT_DIR)
    def plot_graph(self):
        """ Plots network graph.

        If graph is too small, consider:

        from IPython.display import HTML
        style = "<style>svg{width:50% !important;height:50% !important;}</style>"
        HTML(style)
        """
        return hl.build_graph(
            self.module,
            torch.ones([1, self.feature_dimension + self.output_dimension * 2]),
        )
Exemplo n.º 11
0
 def _highlevel(self, model, trial):
     transforms = [
         hl.transforms.Rename(op = 'ATen', to = 'Norm'),
         hl.transforms.Fold("Norm > Elu > Conv", "PreActConvblock"),
         hl.transforms.Fold("BatchNorm > Elu > Conv", "PreActConvblock"),
         hl.transforms.Fold("BatchNorm > Relu > Conv", "PreActConvblock"),
         hl.transforms.Fold("Conv > BatchNorm > Relu", "Convblock"),
         hl.transforms.Fold("Conv > BatchNorm > Elu", "Convblock"),
         hl.transforms.FoldDuplicates(),
     ]
     hl_graph = hl.build_graph(model, trial, transforms = transforms)
     hl_graph.theme = hl.graph.THEMES["blue"].copy()  # Two options: basic and blue
     display(hl_graph)
Exemplo n.º 12
0
    train_data_loader = data.DataLoader(train_dataset, batch_size=xxxx, shuffle=True)
    validation_data_loader = data.DataLoader(
        validation_dataset, batch_size=xxxx, shuffle=True
    )

    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    # device_ids = [0, 1]

    # 自定义的model
    model = MyModel()
    model.to(device)
    model.apply(weight_init)
    # 并行运算,如果需要的话
    model = nn.DataParallel(model, device_ids=device_ids).to(device)
    summary(model, input_size=(channels, H, W))
    hl.build_graph(model, torch.zeros([1, 2, 3]))

    # loss function, 比如交叉熵
    criterion = nn.CrossEntropyLoss()
    criterion.to(device)
    # #自定义损失函数
    # class CustomLoss(nn.Module):
    #
    #     def __init__(self):
    #         super(CustomLoss, self).__init__()
    #
    #     def forward(self, x, y):
    #         loss = torch.mean((x - y) ** 2)
    #         return loss

    # optimizer,比如Adam
Exemplo n.º 13
0
    def _document_models(self) -> None:
        """Add model summaries to the traceability document.
        """
        with self.doc.create(Section("Models")):
            for model in humansorted(self.system.network.models,
                                     key=lambda m: m.model_name):
                if not isinstance(model, (tf.keras.Model, torch.nn.Module)):
                    continue
                self.doc.append(NoEscape(r'\FloatBarrier'))
                with self.doc.create(Subsection(f"{model.model_name}")):
                    if isinstance(model, tf.keras.Model):
                        # Text Summary
                        summary = []
                        model.summary(line_length=92,
                                      print_fn=lambda x: summary.append(x))
                        summary = "\n".join(summary)
                        self.doc.append(Verbatim(summary))
                        with self.doc.create(Center()):
                            self.doc.append(
                                HrefFEID(FEID(id(model)), model.model_name))

                        # Visual Summary
                        # noinspection PyBroadException
                        try:
                            file_path = os.path.join(
                                self.resource_dir,
                                "{}_{}.pdf".format(self.report_name,
                                                   model.model_name))
                            dot = tf.keras.utils.model_to_dot(
                                model, show_shapes=True, expand_nested=True)
                            # LaTeX \maxdim is around 575cm (226 inches), so the image must have max dimension less than
                            # 226 inches. However, the 'size' parameter doesn't account for the whole node height, so
                            # set the limit lower (100 inches) to leave some wiggle room.
                            dot.set('size', '100')
                            dot.write(file_path, format='pdf')
                        except Exception:
                            file_path = None
                            print(
                                f"FastEstimator-Warn: Model {model.model_name} could not be visualized by Traceability"
                            )
                    elif isinstance(model, torch.nn.Module):
                        if hasattr(model, 'fe_input_spec'):
                            # Text Summary
                            # noinspection PyUnresolvedReferences
                            inputs = model.fe_input_spec.get_dummy_input()
                            self.doc.append(
                                Verbatim(
                                    pms.summary(
                                        model.module if
                                        self.system.num_devices > 1 else model,
                                        inputs,
                                        print_summary=False)))
                            with self.doc.create(Center()):
                                self.doc.append(
                                    HrefFEID(FEID(id(model)),
                                             model.model_name))
                            # Visual Summary
                            # Import has to be done while matplotlib is using the Agg backend
                            old_backend = matplotlib.get_backend() or 'Agg'
                            matplotlib.use('Agg')
                            # noinspection PyBroadException
                            try:
                                # Fake the IPython import when user isn't running from Jupyter
                                sys.modules.setdefault('IPython', MagicMock())
                                sys.modules.setdefault('IPython.display',
                                                       MagicMock())
                                import hiddenlayer as hl
                                with Suppressor():
                                    graph = hl.build_graph(
                                        model.module if
                                        self.system.num_devices > 1 else model,
                                        inputs)
                                graph = graph.build_dot()
                                graph.attr(
                                    rankdir='TB'
                                )  # Switch it to Top-to-Bottom instead of Left-to-Right
                                # LaTeX \maxdim is around 575cm (226 inches), so the image must have max dimension less
                                # than 226 inches. However, the 'size' parameter doesn't account for the whole node
                                # height, so set the limit lower (100 inches) to leave some wiggle room.
                                graph.attr(size="100,100")
                                graph.attr(margin='0')
                                file_path = graph.render(
                                    filename="{}_{}".format(
                                        self.report_name, model.model_name),
                                    directory=self.resource_dir,
                                    format='pdf',
                                    cleanup=True)
                            except Exception:
                                file_path = None
                                print(
                                    "FastEstimator-Warn: Model {} could not be visualized by Traceability"
                                    .format(model.model_name))
                            finally:
                                matplotlib.use(old_backend)
                        else:
                            file_path = None
                            self.doc.append(
                                "This model was not used by the Network during training."
                            )
                    if file_path:
                        with self.doc.create(Figure(position='ht!')) as fig:
                            fig.append(
                                Label(
                                    Marker(name=str(FEID(id(model))),
                                           prefix="model")))
                            fig.add_image(
                                os.path.relpath(file_path,
                                                start=self.save_dir),
                                width=NoEscape(
                                    r'1.0\textwidth,height=0.95\textheight,keepaspectratio'
                                ))
                            fig.add_caption(
                                NoEscape(
                                    HrefFEID(FEID(id(model)),
                                             model.model_name).dumps()))
Exemplo n.º 14
0
    def forward(self, x):
        out = self.conv(x)
        out = self.bn(out)
        out = self.relu(out)
        out = self.layer1(out)
        out = self.layer2(out)
        out = self.layer3(out)
        out = self.avg_pool(out)
        out = out.view(out.size(0), -1)
        out = self.fc(out)
        return out


model = ResNet(ResidualBlock, [2, 2, 2, 2]).to(device)
h1 = hl.build_graph(model, torch.zeros(100, 3, 32, 32).to(device))
h1.save('images/resnet.png', format='png')

# Loss and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)


def update_lr(optimizer, lr):
    """For updating learning rate."""

    for param_group in optimizer.param_groups:
        param_group['lr'] = lr


# Train the model
Exemplo n.º 15
0
    def _document_models(self) -> None:
        """Add model summaries to the traceability document.
        """
        with self.doc.create(Section("Models")):
            for model in humansorted(self.system.network.models,
                                     key=lambda m: m.model_name):
                if not isinstance(model, (tf.keras.Model, torch.nn.Module)):
                    continue
                self.doc.append(NoEscape(r'\FloatBarrier'))
                with self.doc.create(Subsection(f"{model.model_name}")):
                    if isinstance(model, tf.keras.Model):
                        # Text Summary
                        summary = []
                        model.summary(line_length=92,
                                      print_fn=lambda x: summary.append(x))
                        summary = "\n".join(summary)
                        self.doc.append(Verbatim(summary))
                        with self.doc.create(Center()):
                            self.doc.append(
                                HrefFEID(FEID(id(model)), model.model_name))

                        # Visual Summary
                        # noinspection PyBroadException
                        try:
                            file_path = os.path.join(
                                self.figure_dir,
                                f"FE_Model_{model.model_name}.pdf")
                            tf.keras.utils.plot_model(model,
                                                      to_file=file_path,
                                                      show_shapes=True,
                                                      expand_nested=True)
                            # TODO - cap output image size like in the pytorch implementation in case of huge network
                            # TODO - save raw .dot file in case system lacks graphviz
                        except Exception:
                            file_path = None
                            print(
                                f"FastEstimator-Warn: Model {model.model_name} could not be visualized by Traceability"
                            )
                    elif isinstance(model, torch.nn.Module):
                        if hasattr(model, 'fe_input_spec'):
                            # Text Summary
                            # noinspection PyUnresolvedReferences
                            inputs = model.fe_input_spec.get_dummy_input()
                            self.doc.append(
                                Verbatim(pms.summary(model, inputs)))
                            with self.doc.create(Center()):
                                self.doc.append(
                                    HrefFEID(FEID(id(model)),
                                             model.model_name))

                            # Visual Summary
                            # Import has to be done while matplotlib is using the Agg backend
                            old_backend = matplotlib.get_backend() or 'Agg'
                            matplotlib.use('Agg')
                            # noinspection PyBroadException
                            try:
                                # Fake the IPython import when user isn't running from Jupyter
                                sys.modules.setdefault('IPython', MagicMock())
                                sys.modules.setdefault('IPython.display',
                                                       MagicMock())
                                import hiddenlayer as hl
                                with Suppressor():
                                    graph = hl.build_graph(model, inputs)
                                graph = graph.build_dot()
                                graph.attr(
                                    rankdir='TB'
                                )  # Switch it to Top-to-Bottom instead of Left-to-Right
                                graph.attr(
                                    size="200,200"
                                )  # LaTeX \maxdim is around 575cm (226 inches)
                                graph.attr(margin='0')
                                # TODO - save raw .dot file in case system lacks graphviz
                                file_path = graph.render(
                                    filename=f"FE_Model_{model.model_name}",
                                    directory=self.figure_dir,
                                    format='pdf',
                                    cleanup=True)
                            except Exception:
                                file_path = None
                                print(
                                    "FastEstimator-Warn: Model {} could not be visualized by Traceability"
                                    .format(model.model_name))
                            finally:
                                matplotlib.use(old_backend)
                        else:
                            self.doc.append(
                                "This model was not used by the Network during training."
                            )
                    if file_path:
                        with self.doc.create(Figure(position='ht!')) as fig:
                            fig.append(
                                Label(
                                    Marker(name=str(FEID(id(model))),
                                           prefix="model")))
                            fig.add_image(
                                os.path.relpath(file_path,
                                                start=self.save_dir),
                                width=NoEscape(
                                    r'1.0\textwidth,height=0.95\textheight,keepaspectratio'
                                ))
                            fig.add_caption(
                                NoEscape(
                                    HrefFEID(FEID(id(model)),
                                             model.model_name).dumps()))
Exemplo n.º 16
0
            if isinstance(m, nn.Conv2d):
                nn.init.kaiming_normal_(m.weight,
                                        mode='fan_out',
                                        nonlinearity='relu')
                if m.bias is not None:
                    nn.init.constant_(m.bias, 0)
            elif isinstance(m, nn.BatchNorm2d):
                nn.init.constant_(m.weight, 1)
                nn.init.constant_(m.bias, 0)
            elif isinstance(m, nn.Linear):
                nn.init.normal_(m.weight, 0, 0.01)
                nn.init.constant_(m.bias, 0)


model = VGG11(num_classes).to(device)
h1 = hl.build_graph(model, torch.zeros(64, 3, 224, 224).to(device))
h1.save('images/vgg11.png', format='png')

# Loss and optimizer
criterion = nn.NLLLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)

# Train the model
total_step = len(train_loader)
for epoch in range(num_epochs):
    for i, (images, labels) in enumerate(train_loader):
        images = images.to(device)
        labels = labels.to(device)

        # Forward pass
        outputs = model(images)
Exemplo n.º 17
0
# --- Source --- #
# https://github.com/waleedka/hiddenlayer

import time
import numpy as np
import torch
import torchvision.models
import hiddenlayer as hl  # Allows the creation of the model graphic
# import scipy.stats # # ImportError: No module named 'scipy._lib.decorator'

# VGG16 with BatchNorm
model = torchvision.models.vgg16()

# Build HiddenLayer graph
# Jupyter Notebook renders it automatically
hl.build_graph(model, torch.zeros([1, 3, 224,
                                   224]))  # correct torch.zeros size required

# save the graphic

# im.save(path="path_of_file/name_of_file" , format="jpg") # correct pathing

# -- scipy issue
# ImportError: No module named 'scipy._lib.decorator'

# -- uninstalled and reinstalled scipy and issue remains

#-- One Chart --#
# A History object to store metrics
history1 = hl.History()

# A Canvas object to draw the metrics
Exemplo n.º 18
0
from some_libs import *
import visdom
import time
import numpy as np
import torch
import pickle
import seaborn as sns
sns.set()
from matplotlib.colors import LogNorm

if False:  #hiddenlayer调用不便
    import hiddenlayer as hl
    import torchvision.models
    model = torchvision.models.vgg16()
    graph = hl.build_graph(model, torch.zeros([1, 3, 224, 224]))
    graph.save(path="vgg_hl", format="jpg")
    print("")


# https://seaborn.pydata.org/generated/seaborn.lineplot.html
def compare_loss_curve(files, hue_col, style_col=None):
    no, nPt = 0, sys.maxsize
    datas_0, datas_1 = [], []
    # style_col = 'Incident Angle'
    for path, bn, xita in files:
        with open(path, "rb") as fp:  # Pickling
            #legend = 'adaptive BN' if no==0 else 'standard BN'
            loss_curve = pickle.load(fp)
        nPt = min(nPt, len(loss_curve))
        assert nPt > 10
Exemplo n.º 19
0
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Creates an SVG file with the graph model of a network
"""

import torch
import model_zoo
import hiddenlayer as hl

# Model
# 1. Model design and GPU capability
model_name = 'simple_mlp'
model = getattr(model_zoo, model_name)()

# Build HiddenLayer graph
im = hl.build_graph(model, torch.zeros([1, 784]), transforms=[])

# Save graph as SVG file
im.save('./' + model_name + '.svg', 'svg')
Exemplo n.º 20
0
def visualize_model(model, inp_size=[1, 3, 64, 64], device="cuda:0"):
    return hl.build_graph(model, torch.zeros(inp_size).to(device))
Exemplo n.º 21
0
import torch
import torchvision.models
import hiddenlayer as hl
import model
import pdb
g = model.Generator(64, 10, 6)
d = model.Discriminator(128, 64, 10, 6)
#x=hl.build_graph(d, torch.zeros([16,1, 128, 128]))

x = hl.build_graph(g, (torch.zeros([1, 1, 128, 128]), torch.zeros([1, 10])))

pdb.set_trace()
Exemplo n.º 22
0
            'batch_dice': True,
            'smooth': 1e-5,
            'smooth_in_nom': True,
            'do_bg': False,
            'rebalance_weights': None,
            'background_weight': 1
        }, {})
    output = unet.decoder(skips)

    l = loss(output, dummy_gt)
    l.backward()

    optimizer.step()

    import hiddenlayer as hl
    g = hl.build_graph(unet, dummy_input)
    g.save("/home/fabian/test.pdf")
    """conv_op_kernel_sizes = ((3, 3, 3),
							(3, 3, 3),
							(3, 3, 3),
							(3, 3, 3),
							(3, 3, 3),
							(3, 3, 3))
	pool_op_kernel_sizes = ((1, 1, 1),
							(2, 2, 2),
							(2, 2, 2),
							(2, 2, 2),
							(2, 2, 2),
							(2, 2, 2))
	patch_size = (160, 128, 128)
	unet = PlainConvUNet(4, 32, (2, 2, 2, 2, 2, 2), 2, pool_op_kernel_sizes, conv_op_kernel_sizes,
Exemplo n.º 23
0
    def train(self,
              features,
              classifier,
              device,
              epochs,
              data_original,
              batch_size=32,
              round=-1,
              por=0.1):
        # def train(self, classifier, epochs, data_original, batch_size=32, round=-1 , por=0.1 ):
        c = h1.Canvas()
        hidden = h1.build_graph(self.D, torch.zeros([1, 542]).cuda())
        hidden.save('./discriminator.png')

        # hidden = h1.build_graph(self.G, torch.zeros([1, 642]).cuda())
        # hidden.save('./generator.png')

        (xmal, ymal), (xben, yben), (xtsmal, xtsben), (ytsmal, ytsben) = data_original[0], data_original[1], \
                                                                         data_original[2], data_original[3]

        xtrain = np.concatenate([xben, xmal])
        ytrain = np.concatenate([yben, ymal])

        #sampling for unbalanced data
        train_loader = sampling(xtrain, ytrain, batch_size)

        # classifier = train_target_model(classifier, train_loader, 100)

        print('\nTRAINING GAN...\n')
        print('=============data============= ')
        print('number of original malware = {0}\n'
              'number of original ben = {1}\n'
              'number of original test = {2}\n'.format(xmal.shape[0],
                                                       xben.shape[0],
                                                       xtsmal.shape[0]))
        print('==============================\n ')

        start_train = timer()
        Train_FNR, Test_FNR = [], []
        best_test_FNR, best_train_FNR = 0.0, 0.0

        self.gloss, self.dloss = [], []
        list_of_added_features = []
        g_grad_changes = []
        lb = preprocessing.LabelBinarizer()
        lb.fit([0, 1])

        for epoch in range(epochs):
            list_of_distortion = []
            batch_added_features = []
            for local_batch, local_lable in train_loader:
                # for step in range(xtrain.shape[0] // batch_size):
                xmal_batch = check_lenght(
                    local_batch[(local_lable != 0).nonzero()])
                xben_batch = check_lenght(
                    local_batch[(local_lable == 0).nonzero()])

                # xmal_batch = check_lenght(xtrain[(ytrain != 0).nonzero()])
                # xben_batch = check_lenght(xtrain[(ytrain == 0).nonzero()])
                # xmal_batch = torch.from_numpy(xmal_batch)

                noise = torch.rand(xmal_batch.shape[0], self.noise_size)
                yclassifierben_batch = classifier.predict(xben_batch)
                # yclassifierben_batch = classifier(xben_batch.float().cuda())

                # xben_batch= torch.from_numpy(xben_batch)

                # Generate a batch of new malware examples
                self.D.zero_grad()
                # gen_examples =binarize(self.G([xmal_batch.float().cuda(), noise.float().cuda()]).detach()))
                gen_examples = self.G(
                    [xmal_batch.float().cuda(),
                     noise.float().cuda()]).detach()

                # Check what features are added in this batch and add it to the list
                batch_added_features.append(
                    check_added_features(
                        binarize(gen_examples) - xmal_batch.cuda().float(),
                        features))

                # ---------------------
                #  Train Discriminator
                # ---------------------
                d_fake_decision = self.D(gen_examples)
                gen_examples_classifier_lable = classifier.predict(
                    binarize(gen_examples).cpu().detach().numpy())
                fake_data_lable = torch.cat(
                    (torch.zeros(gen_examples_classifier_lable.shape[0],
                                 1).cuda(),
                     torch.ones(gen_examples_classifier_lable.shape[0],
                                1).cuda()), 1)
                # fake_data_lable = torch.cat((torch.zeros(d_fake_decision.shape[0] ,1).cuda() , torch.ones(d_fake_decision.shape[0],1).cuda()) , 1)

                d_loss_fake = self.criterion(d_fake_decision, fake_data_lable)
                d_real_decision = self.D(xben_batch.float().cuda())
                # the disscriminator is using classifier predictions lables

                real_lable = np.hstack((1 - lb.transform(yclassifierben_batch),
                                        lb.transform(yclassifierben_batch)))
                # the disscriminator is just using true lables
                # real_lable = torch.cat((torch.ones(d_real_decision.shape[0], 1).cuda(), torch.zeros(d_real_decision.shape[0], 1).cuda()),
                #       1)
                # d_loss_real= self.criterion(d_real_decision,real_lable)
                d_loss_real = self.criterion(
                    d_real_decision,
                    torch.from_numpy(real_lable).float().cuda())
                # d_loss_real = self.criterion(d_real_decision,yclassifierben_batch.detach())
                # d_loss_real = self.criterion(d_real_decision,torch.zeros_like(d_real_decision))
                d_loss = 0.5 * torch.add(d_loss_real, d_loss_fake)
                d_loss_real.backward(retain_graph=True)
                d_loss_fake.backward(retain_graph=True)
                # plot_grad_flow(self.D.named_parameters())

                self.d_optimizer.step()

                # ---------------------
                #  Train Generator
                # ---------------------

                noise = torch.rand(xmal_batch.shape[0], self.noise_size)
                self.G.zero_grad()
                g_fake_data = self.G(
                    [xmal_batch.float().cuda(),
                     noise.float().cuda()])
                dg_fake_decision = self.D(g_fake_data)
                g_desired_lable = torch.cat(
                    (torch.ones(d_fake_decision.shape[0], 1).cuda(),
                     torch.zeros(d_fake_decision.shape[0], 1).cuda()), 1)
                # g_loss_samples = nn.functional.mse_loss(dg_fake_decision, g_desired_lable)
                g_loss_samples = self.criterion(dg_fake_decision,
                                                g_desired_lable)
                # g_loss_samples.reqiers_grad = True
                if self.losstype == 'limited_distortion':
                    orig_adv_dist = torch.sum(
                        g_fake_data - xmal_batch.float().cuda(), 1)
                    g_loss = g_loss_samples + 0.001 * torch.mean(
                        torch.norm(g_fake_data - xmal_batch.float().cuda(), 1,
                                   1))
                    list_of_distortion.append(
                        orig_adv_dist.cpu().detach().numpy())
                # if self.losstype == 'unlimited_distorion':
                # g_loss = g_loss_samples

                # g_loss_samples.backward(retain_graph=True)
                g_loss.backward(retain_graph=True)
                plot_grad_flow(self.G.named_parameters())
                g_grad_changes.append(grad_changes(self.G.named_parameters()))
                self.g_optimizer.step()

                torch.cuda.empty_cache()

            list_of_added_features.append(
                pd.DataFrame(batch_added_features).mean(axis=0).to_dict())
            self.gloss.append(g_loss)
            self.dloss.append(d_loss)

            # Compute attack success rate on train and test data
            train_FNR = self.FNR(xmal, classifier)
            test_FNR = self.FNR(xtsmal, classifier)

            if train_FNR > best_train_FNR:
                best_train_FNR = train_FNR
            Train_FNR.append(train_FNR)
            Test_FNR.append(test_FNR)
            if test_FNR > best_test_FNR:
                best_test_FNR = test_FNR
                print('saving mulgan weights at epoch: %d', epoch)

                # print("[D loss: %f]  [G loss: %f] \n" % ( self.dloss[-1],self.gloss[-1]))
                torch.save(
                    self.G,
                    '/home/maryam/Code/python/adversarial_training/torch_impl/_'
                    + str(por) + '/malgan{0}.pt'.format(round))

            print(
                "%d [D loss: %f] [G loss: %f] [train_FNR: %f] [test_FNR: %f] [distortion: %f]"
                % (epoch, d_loss, g_loss, train_FNR, test_FNR,
                   pd.DataFrame(batch_added_features).mean(axis=0).sum()))
        end_train = timer()
        del g_loss, d_loss_real, d_loss_fake  # d_loss

        print('\ntraining completed in %.3f seconds.\n' %
              (end_train - start_train))
        print('=============results ============= ')

        print(' attack success rate using train data: {0} \n'
              ' attack success rate using test data: {1}'.format(
                  best_train_FNR, best_test_FNR))
        print('==============================\n ')

        plot_added_featues(list_of_added_features)
        plot_added_featues(g_grad_changes)

        # Plot losses
        plt.figure()
        plt.plot(range(len(self.gloss)),
                 self.gloss,
                 c='r',
                 label='g_loss_rec',
                 linewidth=2)
        plt.plot(range(len(self.dloss)),
                 self.dloss,
                 c='g',
                 linestyle='--',
                 label='d_loss',
                 linewidth=2)
        plt.xlabel('Epoch')
        plt.ylabel('loss')
        plt.legend()
        plt.savefig(
            '/home/maryam/Code/python/adversarial_training/torch_impl/_' +
            str(por) + '/GAN_Epoch_loss({0}).png'.format(round))
        plt.show()
        plt.close()

        # Plot TPR
        plt.figure()
        plt.plot(range(len(Train_FNR)),
                 Train_FNR,
                 c='r',
                 label='Training Set',
                 linewidth=2)
        # plt.plot(range(len(Train_FNR)), Train_FNR, c='r', label='Attack success rate', linewidth=2)
        plt.plot(range(len(Test_FNR)),
                 Test_FNR,
                 c='g',
                 linestyle='--',
                 label='Test Set',
                 linewidth=2)
        plt.xlabel('Epoch')
        plt.ylabel('FNR')
        plt.legend()
        plt.savefig(
            '/home/maryam/Code/python/adversarial_training/torch_impl/_' +
            str(por) + '/Epoch_FNR({0}).png'.format(round))
        plt.show()
        plt.close()

        return [best_train_FNR, best_test_FNR, (end_train - start_train)]
Exemplo n.º 24
0
        net = RefinementStageBlock(net, 128, training=is_training)
        net = RefinementStageBlock(net, 128, training=is_training)
        heatmaps2 = conv(net, 128, kernel_size=1, bn=False)
        heatmaps2 = conv(heatmaps2,
                         num_joints,
                         kernel_size=1,
                         bn=False,
                         relu=False)
        pafs2 = conv(net, 128, kernel_size=1, bn=False)
        pafs2 = conv(pafs2, num_pafs, kernel_size=1, bn=False, relu=False)

    return heatmaps2, pafs2


if __name__ == '__main__':
    import hiddenlayer as hl
    import hiddenlayer.transforms as ht
    import os
    # Hide GPUs. Not needed for this demo.
    os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
    with tf.Session() as sess:
        with tf.Graph().as_default() as tf_graph:
            # Setup input placeholder
            inputs = tf.placeholder(tf.float32, shape=(1, 256, 256, 3))
            # Build model
            predictions, _ = lightweight_openpose(inputs, 14, 26, True)
            # Build HiddenLayer graph
            hl_graph = hl.build_graph(tf_graph)

    hl_graph.save('lightweight', format='png')
Exemplo n.º 25
0
# The graph is very large so building the graph will take a long time.
# Note that at the time of writing the graph algorithms can't handle multiplying by a constant.
# Instead, I added if statements that skip the weight component if it is in visualization mode.
#
# For progress bars go back to /hiddenlayer/hiddenlayer/pytorch_builder.py:
# at the top add:
#     import tqdm as tqdm
#
# Then in:
#
#     def import_graph()
#
# find all instances of:
#
#    torch_graph.nodes()
#
# and replace with:
#
#    tqdm(torch_graph.nodes())
#
if args.dataset == 'imagenet':
    input_batch = torch.zeros([2, 3, 224, 224])
elif args.dataset == 'cifar10':
    input_batch = torch.zeros([2, 3, 32, 32])
# print(input_batch)
cnn_graph = hl.build_graph(cnn_model, input_batch, transforms=transforms)
output_file = os.path.expanduser('~/src/darts/cnn/' + args.arch +
                                 '_network.pdf')
print('build complete, saving: ' + output_file)
cnn_graph.save(output_file)
print('save complete')
Exemplo n.º 26
0
    n_params, n_trainable_params, n_non_trainable_params = count_model_parameters(
        model)
    print('\t- Num of Parameters                : {:,}'.format(n_params))
    print('\t- Num of Trainable Parameters      : {:,}'.format(
        n_trainable_params))
    print('\t- Num of Non-Trainable Parameters  : {:,}'.format(
        n_non_trainable_params))
    print('==========================================================')
    # summary(model, [(1,args.vol_size_z,args.vol_size_x,args.vol_size_y), (3,args.img_size_x,args.img_size_y)])

    # write the dot graph to file
    dummy_x_lidar = torch.randn(1, 1, args.vol_size_z, args.vol_size_x,
                                args.vol_size_y).to(device)
    dummy_x_camera = torch.randn(1, 3, args.img_size_x,
                                 args.img_size_y).to(device)
    hl_graph = hl.build_graph(model, (dummy_x_lidar, dummy_x_camera))
    hl_graph = hl_graph.save(os.path.join(model_exp_dir, 'model'))
    # write training hyperparameters to yaml file
    with open(os.path.join(model_exp_dir, 'vr3dense_args.yaml'),
              'w') as outfile:
        yaml.dump(vr3dense_args, outfile, default_flow_style=False)

    # load weights
    best_ckpt_model = os.path.join(model_exp_dir, 'checkpoint_best.pt')
    if (args.use_pretrained_weights == True) and (args.pretrained_weights !=
                                                  'none') and os.path.exists(
                                                      args.pretrained_weights):
        model.load_state_dict(
            torch.load(args.pretrained_weights,
                       map_location=lambda storage, loc: storage))
        print('Loaded pre-trained weights: {}'.format(args.pretrained_weights))
Exemplo n.º 27
0
                nn.ReLU(inplace=True),
            ), *[
                nn.Sequential(
                    nn.Conv2d(out, out, kernel_size=3, padding=1, stride=1),
                    nn.ReLU(inplace=True),
                ) for _ in range(n - 1)
            ], nn.MaxPool2d(2, 2))

    def forward(self, x):
        self._initialize_weights()
        x = self.conv1(x)
        x = self.conv2(x)
        x = self.conv3(x)
        x = self.conv4(x)
        x = self.conv5(x)
        x = self.avgpool(x)
        x = torch.flatten(x, 1)
        x = self.classif(x)

        return x


import hiddenlayer as hl
from torch.autograd import Variable

x = Variable(torch.rand(1, 1, 28, 28))
n = Net()
n.eval()
h = hl.build_graph(n, x)
h.save('gp.png')
Exemplo n.º 28
0
load_model(model, 'cpu', best_epoch, model_name, models_path)


# ***
# # Plot the Model

# In[ ]:


# model.eval()


# In[ ]:


hl_graph = hl.build_graph(model, torch.zeros([1, 3, 600, 600]))


# In[ ]:


hl_graph


# ***
# # Dev

# In[ ]:


from common_code import *
Exemplo n.º 29
0
                         self.hidden_size).to(device)  # 2 for bidirection
        c0 = torch.zeros(self.num_layers*2, x.size(0),
                         self.hidden_size).to(device)

        # Forward propagate LSTM
        # out: tensor of shape (batch_size, seq_length, hidden_size*2)
        out, _ = self.lstm(x, (h0, c0))

        # Decode the hidden state of the last time step
        out = self.fc(out[:, -1, :])
        return out


model1 = BiRNN(input_size, hidden_size, num_layers, num_classes).to(device)
model = RNN(input_size, hidden_size, num_layers, num_classes).to(device)
h1 = hl.build_graph(model, torch.zeros(64, 28, 28).to(device))
h1.save('images/rnn1.png', format='png')
h2 = hl.build_graph(model1, torch.zeros(64, 28, 28).to(device))
h2.save('images/birnn1.png', format='png')
model = model1

# Loss and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)

# Train the model
total_step = len(train_loader)
for epoch in range(num_epochs):
    for i, (images, labels) in enumerate(train_loader):
        images = images.reshape(-1, sequence_length, input_size).to(device)
        labels = labels.to(device)
from inspect import trace
import torch
import hiddenlayer as hl

from model import TrackmaniaNet

trackmania_net = TrackmaniaNet()

visualize_graph = hl.build_graph(
    trackmania_net,
    torch.zeros([64, 1, 64, 64]),
    transforms=[
        hl.transforms.Fold("Conv > BatchNorm > Relu", "ConvBnRelu"),
        hl.transforms.Fold("Linear > Relu", "LinearRelu")
    ])
visualize_graph.theme = hl.graph.THEMES["blue"].copy()

dot = visualize_graph.build_dot()
dot.format = 'png'
dot.attr("graph", rankdir='TB')
dot.render('model_diagram', directory='docs/imgs', cleanup=True)

# visualize_graph.save('docs/imgs/model_diagram.png', 'png')