Fail to train cGAN - train_loader is not defined

Hello everyone,

I’m working on a Conditional GAN to estimate flow field data in a porous medium (here is the link for the notebook). In my research, I provide for the Generator a binary cropped mesh image (representing a section of the porous medium), and it will learn to estimate the flow field in the black region as an output image. The Discriminator will then tell if the provided image is real or fake. The model is still far from functional, as I’m struggling to generate new training data. I have the following case:

class adversarialModel(object):

    def __init__(self, num_epochs=500, samples=3, batch=5, betas=(0.5,0.5), g_lr = 0.001, d_lr = 0.001,

                 size = 64, data_path = './models', channels_img = 1, channels_noise =  64, features_g = 16,

                 features_d = 16, dataloader = train_loader, transforms = None):

# Get mesh and velocity data paths

mesh_paths = glob.glob('./mesh_data/*.tif')

vel_paths = glob.glob('./vel_data/*.tif')

total_samples = len(mesh_paths)

train_size = 0.85

# Separate training samples

train_mesh_paths = mesh_paths[:int(total_samples*train_size)]

train_vel_paths = vel_paths[:int(total_samples*train_size)]
train_data = mesh_vel_dataset(train_mesh_paths, train_vel_paths, train=True)

train_loader = DataLoader(train_data, batch_size = 5, shuffle = True)

test_data = mesh_vel_dataset(test_mesh_paths, test_vel_paths, train=False)

test_loader = DataLoader(test_data, batch_size = 5, shuffle = False)

So now I need to train the model.

model = adversarialModel(num_epochs=100, dataloader = train_loader)

However, after I run this cell, I get the following error:
Traceback (most recent call last):

>   File "d:/deep_learning/Computer_Vision/Projeto/", line 133, in <module>
>     class adversarialModel(object):
>   File "d:/deep_learning/Computer_Vision/Projeto/", line 136, in adversarialModel
>     features_d = 16, dataloader = train_loader, transforms = None):
> NameError: name 'train_loader' is not defined

This looks like a simple error, but I’m stuck in it for a week. It seems that the training data is not defined, but as I inspect the variable, It looks completely fine. If I am not asking for much, I would appreciate if someone could help me with that.

May be You’ve defined not train_loader in model arugment. I think you shouldn’t put train_loader directly in argument,before train_loader was defined , so only data_loader would be fine.

1 Like

Thanks for the reply, you have fixed my one-week-lasting error!

1 Like