ajbrock:

Brock* Can you post your full code? It’s not clear exactly what you’re passing from the forward() method, but just based on that snippet it looks like you might be passing a list somehow?


Sorry for the typo. Its a list where i am storing the output… Here’s the bottleneck code:

class Bottleneck(nn.Module):
out_saved = [0] * 900
def __init__(self, nChannels, growthRate,layer_index, train_size, test_size, batch_sz):
    super(Bottleneck, self).__init__()
    interChannels = 4*growthRate
    self.bn1 = nn.BatchNorm2d(nChannels)
    self.conv1 = nn.Conv2d(nChannels, interChannels, kernel_size=1,
                           bias=False)
    self.bn2 = nn.BatchNorm2d(interChannels)
    self.conv2 = nn.Conv2d(interChannels, growthRate, kernel_size=3,
                           padding=1, bias=False)

    # If the layer is still being trained
    self.active=True
    
    # The index of this layer relative to the overall net
    self.layer_index=layer_index

    #Change
    self.counter = 0
    self.train_size = train_size
    self.test_size = test_size
    self.batch_sz = batch_sz
    #self.outList = []
    remainder = train_size % batch_sz
    print("remainder = ", remainder)


    self.maxCounter = train_size//batch_sz

    print("self.maxCounter", self.maxCounter)

    if(remainder != 0):
        self.maxCounter += 1
        
    out_saved = [0] * (self.maxCounter)
    #print(out_saved)

def forward(self, x):
    test = DenseNet.test
    # If we're not training this layer, set to eval mode so that we use
    # running batchnorm stats (both for time-saving and to avoid updating
    # said stats).
    if not self.active:
        self.eval()
    # While Validation , Return the original Output 
    if test:
        #print test
        
        out = self.conv1(F.relu(self.bn1(x)))
        out = self.conv2(F.relu(self.bn2(out)))
        out = torch.cat((x, out), 1) 
        return out

    # If we're  active, return a detached output to prevent backprop.
    if self.active and  not test: 
        out = self.conv1(F.relu(self.bn1(x)))
        out = self.conv2(F.relu(self.bn2(out)))
    
        out = torch.cat((x, out), 1)

         #   print out
        if(self.layer_index < DenseNet.freezeLayerIndex):
            #Store The detach instead of output as we will be returning that.
            detach = out.detach()
            detach.volatile = False

            #print('Inside ',self.counter)
            Bottleneck.out_saved[self.counter] = detach
            self.counter+=1
            if(self.counter >= self.maxCounter):
                self.counter = 0
                self.active = False
            
        return out

    elif not test:
        detach = Bottleneck.out_saved[self.counter]
        self.counter+=1
        if(self.counter >= self.maxCounter):
            self.counter = 0
        return detach