Hello.
I am trying too train a CNN.
The CNN can be found here:
  
      github.com
  
  
    amadeuzou/vrn-pytorch/blob/master/vrn_unguided.py 2
import torch
import torch.nn as nn
import torch.legacy.nn as lnn

from functools import reduce
from torch.autograd import Variable

class LambdaBase(nn.Sequential):
    def __init__(self, fn, *args):
        super(LambdaBase, self).__init__(*args)
        self.lambda_func = fn

    def forward_prepare(self, input):
        output = []
        for module in self._modules.values():
            output.append(module(input))
        return output if output else input

class Lambda(LambdaBase):
    def forward(self, input):


  This file has been truncated. show original

  
  
    
    
  
  


however i dont get it too really run.
I want too use CrossEntropyLoss as my lossfunction. Since this is the function which is used in the model here too.
The reason why i want too retrain is, that i need to mod it a little bit for a bigger Model and a bigger picture.
(384 * 384 image → 384 * 384 * 384 Voxel Space)
I have only 1 Dataset, this means one image and the corresponding voxel-model.
I know that this never ever is going too work-out for usability but it has too be done, just too proove that it is possible. (Huge Overfitting is okay, basically i wanted too run it on 5 Datasets later and make the CNN overfit for those 5 images)
Now i wrote some script which should train my CNN. However, some Loss-Functions let it run but the loss never gets better or worse. And some others, like CrossEntropy dont work at all.
Can someone help me and say what i am doing wrong?
My Script for the CNN is following:
VRN = vrn.vrn_unguided
VRN.load_state_dict(torch.load('vrn-unguided-selfmade.pth'))
# telling the model it will be trained
VRN.train()
# lossfct to calculate error while training -> sigmoid cross entropy loss same as in original vrn.
lossfct = torch.nn.modules.loss.CrossEntropyLoss()
# optimizer settings
optimizer = torch.optim.SGD(VRN.parameters(), lr=0.1)

# between the #-lines is just data-loading 
#################################################################
# file format .txt
voxel_array = np.zeros((1, 384, 384, 384), dtype=float)
file = open("sample_1.txt","r")
for line in file:
    if not "#" in line:
        x = line.partition(" ")[0]
        y = line.partition(" ")[2].partition(" ")[0]
        z = line.partition(" ")[2].partition(" ")[2].partition(" ")[0]
        voxel_array[0, int(x), int(y), int(z)] = 1
file.close()

###############
# here we load the image too train with
### get landmarks from test image
image_file = 'rsz_pic_zu_sample.jpg'
image = cv.imread(image_file)
try:
    image_height, image_width, image_depth = image.shape
except:
    print('cannot load image:', image_file)
# vrn output
inp = torch.from_numpy(image.transpose((2, 0, 1))).float().unsqueeze_(0)

###############################################################

voxel_array = torch.from_numpy(voxel_array)


# the actual training takes place here. Right now it just iterates 20 times over the same data.
for epoch in range(20):
    print(epoch)
    VRN.train()
    predicted_model = VRN(inp)[-1].data.cpu()
    vol = predicted_model.numpy()

    optimizer.zero_grad()
    vol = torch.from_numpy(vol)

    print(vol.shape)
    print(voxel_array.shape)
    loss = lossfct(vol, voxel_array)
    print(loss)
    loss.backward()
    optimizer.step()

torch.save(VRN.state_dict(), "vrn-unguided-selfmade.pth")
print("Done, saved CNN")