def test_networks(): dirname = Path('results_networks') dirname.mkdir(exist_ok=True) image = np.array(Image.open('lena.png').convert('L')) image_cuda = torch.tensor(image).float().cuda()[None, None, ...] config = Config() config.kn_kernel_size = 2 config.kn_num_channels = 256 config.kn_num_convs = 3 kn = KernelNet().cuda() print(kn) assert kn.kernel_cuda.shape == (1, 1, 19, 1) assert kn(image_cuda).shape == (1, 1, 494, 512) assert torch.isclose(torch.sum(kn.kernel_cuda), torch.tensor(1).float()) kn_dot = make_dot(image_cuda, kn) kn_dot.render(dirname.joinpath('kn')) fig = plt.figure() kernel = kn.kernel.numpy().squeeze() plt.plot(kernel) fig.savefig(dirname.joinpath('kernel.png')) lrd = LowResDiscriminator().cuda() print(lrd) assert lrd(image_cuda).shape == (1, 1, 502, 502) lrd_dot = make_dot(image_cuda, lrd) lrd_dot.render(dirname.joinpath('lrd'))
def test_double_input_model(): di = DoubleInputModel() x1 = torch.randn(1, 8, 16, 16) x2 = torch.randn(1, 8, 16, 16) dot = make_dot((x1, x2), di) dot.render('double_input', format='png')
def test_lstm(): x = torch.randn(1, 128) lstm_cell = torch.nn.LSTMCell(128, 128) dot = make_dot(x, lstm_cell) dot.render('lstm')
def test_double_backprop_model(): x = torch.randn(1, 8) dp = DoublePropModel() dot = make_dot(x, dp) dot.render('double_backprop')
def test_mlp(): x = torch.randn(1, 8) mlp = MLP() dot = make_dot(x, mlp) dot.render('mlp')
test_acc += torch.sum(y_pred == acc_test) test_loss += loss.data[0] test_loss /= test_size test_acc /= test_size print('\tVal Loss: {:.4f} \t Acc: {:.4f}'.format(test_loss, test_acc)) print('-' * 100) if __name__ == '__main__': # main() # train() x = Variable(torch.rand(1, 3, 224, 224)) # resNet18 = ResNet(BasicBlock,[2, 2, 2, 2]) # resNet34 = ResNet(BasicBlock,[3,4,6,3]) resNet50 = ResNet(Bottleneck, [3, 4, 6, 3]) # resNet101 = ResNet(Bottleneck, [3, 4, 23, 3]) # with SummaryWriter(log_dir="./logs/troch",comment='resNet101') as w: # w.add_graph(resNet101, (x, )) y = resNet50(x) # model = tv.models.resnet50() # y = model(x) print(y) dot = make_dot(y, name="./images/resNet50",params=dict(list(resNet50.named_parameters()) + [('x', x)])) # dot = make_dot(y, name="./images/resNet50",params=dict(list(model.named_parameters()) + [('x', x)])) dot.format = "pdf" dot.render()
#!/usr/bin/env python import torch from pytorchviz import make_dot # from torchviz import make_dot x = torch.randn(1, 2, 3) class Minimum(torch.nn.Module): def forward(self, x): return x + 2 x.requires_grad = True y = Minimum()(x) print(y.grad_fn.next_functions) dot = make_dot(x, Minimum()) # x.requires_grad = True # model = Minimum() # params = dict(model.named_parameters()) # params['x'] = x # dot = make_dot(model(x), params) dot.render('minimum')
#!/usr/bin/env python import torch from pytorch_unet import UNet from pytorchviz import make_dot class Wrapper(torch.nn.Module): def __init__(self, net): super().__init__() self.net = net def forward(self, x): y = self.net(x) if isinstance(y, dict): y = tuple(y.values()) return y x = torch.rand(1, 1, 16, 16, 16).cuda().float() unet = UNet(1, 4, 3, 8, output_levels=[3, 0, 1]).cuda() unet = Wrapper(unet) print(unet) y = unet(x) dot = make_dot(x, unet) dot.format = 'pdf' dot.render('unet')