From a87c70b2bed2501c054a6fa7554286ff3cf9c2b0 Mon Sep 17 00:00:00 2001 From: CHIRANJEET1729DAS <126919390+CHIRANJEET1729DAS@users.noreply.github.com> Date: Tue, 21 May 2024 13:51:51 +0530 Subject: [PATCH] Update model.py In this model the linear layers and conv layers are changed to the lazy version ,it reduces and provides flexibility in terms of memory consumption,explained below: Deferred Initialization: Lazy Layers: Unlike regular layers (nn.Linear, nn.Conv2d), lazy layers do not require the specification of input dimensions at the time of model construction. Instead, they infer the input shape the first time data passes through them. Memory Allocation: Memory for weights and biases is not allocated when the lazy layer is instantiated. It is only allocated when the layer processes the first batch of data during the forward pass. Flexibility: Dynamic Shape Inference: Lazy layers are useful when input dimensions might change or are not known in advance. This can be particularly beneficial in complex architectures or during rapid prototyping. Model Adaptability: They allow for building more adaptable models that can handle varying input sizes without requiring redefinition or adjustment of the layer parameters. Memory Efficiency: Deferred Memory Allocation: By deferring memory allocation until the forward pass, unnecessary memory usage during model instantiation is avoided. This can reduce the initial memory footprint, especially in large models or when using multiple layers with unknown dimensions. --- model.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/model.py b/model.py index 9d92299..8c6b4c8 100644 --- a/model.py +++ b/model.py @@ -21,9 +21,9 @@ def __init__(self, features, path=None): super(Darknet, self).__init__() self.features = features self.classifier = nn.Sequential( - nn.Linear(1024 * S * S, 4096), + nn.LazyLinear(4096,bias=True), nn.LeakyReLU(0.1), - nn.Linear(4096, S * S * (B * 5 + C)), + nn.LazyLinear(S * S * (B * 5 + C),bias=True), ) if path is None: @@ -81,11 +81,11 @@ def make_layers(cfg): layers += [nn.MaxPool2d(kernel_size=2, stride=2)] elif isinstance(v, tuple): if len(v) == 3: - # Conv (kernel_size, out_channels, stride) - layers += [nn.Conv2d(in_channels, out_channels=v[1], kernel_size=v[0], stride=2)] + # LazyConv (out_channels,kernel,stride) + layers += [nn.LazyConv2d(out_channels=v[1], kernel_size=v[0], stride=2)] else: - # Conv (kernel_size, out_channels) - layers += [nn.Conv2d(in_channels, out_channels=v[1], kernel_size=v[0])] + # LazyConv (out_channels,kernel) + layers += [nn.LazyConv2d(out_channels=v[1], kernel_size=v[0])] layers += [nn.BatchNorm2d(num_features=v[1])] # BN print('[new] BN is added.')