AttributeError: Can't get attribute 'NusDatasetGCN' on <module '__main__' (built-in)>

Hi, I have trouble. When I run my graph convolutional model, the pytorch will respond the error message like:
AttributeError: Can’t get attribute ‘NusDatasetGCN’ on <module ‘main’ (built-in)>

##The NusDatasetGCN code:

class NusDatasetGCN(Dataset):
def init(self, data_path, anno_path, transforms, w2v_path):
self.transforms = transforms

    with open(anno_path) as fp:
        json_data = json.load(fp)
   # with open('small_train.json',encoding="utf-8") as fp:
        #json_data = json.loads(fp.read())
    samples = json_data['samples']
    self.classes = json_data['labels']

    self.imgs = []
    self.annos = []
    self.data_path = data_path
    print('loading', anno_path)
    for sample in samples:
        self.imgs.append(sample['image_name'])
        self.annos.append(sample['image_labels'])
    for item_id in range(len(self.annos)):
        item = self.annos[item_id]
        vector = [cls in item for cls in self.classes]
        self.annos[item_id] = np.array(vector, dtype=float)
    # Load vectorized labels for GCN from json.    
    with open(w2v_path) as fp:
        self.gcn_inp = np.array(json.load(fp)['vect_labels'], dtype=float)

def __getitem__(self, item):
    anno = self.annos[item]
    img_path = os.path.join(self.data_path, self.imgs[item])
    img = Image.open(img_path)
    if self.transforms is not None:
        img = self.transforms(img)
    return img, anno, self.gcn_inp

def __len__(self):
    return len(self.imgs)

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

Run training.

epoch = 0
iteration = 0
while True:
batch_losses = []
for batch_number, (imgs, targets, gcn_input) in enumerate(train_dataloader):
imgs, targets, gcn_input = imgs.to(device), targets.to(device), gcn_input.to(device)
optimizer.zero_grad()

    model_result = model(imgs, gcn_input)
    loss = criterion(model_result, targets.type(torch.float))

    batch_loss_value = loss.item()
    loss.backward()
    torch.nn.utils.clip_grad_norm(model.parameters(), 10.0)
    
    optimizer.step()

    logger.add_scalar('train_loss', batch_loss_value, iteration)
    batch_losses.append(batch_loss_value)
    with torch.no_grad():
        result = calculate_metrics(model_result.cpu().numpy(), targets.cpu().numpy())
        for metric in result:
            logger.add_scalar('train/' + metric, result[metric], iteration)

    if iteration % test_freq == 0:
        model.eval()
        with torch.no_grad():
            model_result = []
            targets = []
            for imgs, batch_targets, gcn_input in test_dataloader:
                gcn_input = gcn_input.to(device)
                imgs = imgs.to(device)
                model_batch_result = model(imgs, gcn_input)
                model_result.extend(model_batch_result.cpu().numpy())
                targets.extend(batch_targets.cpu().numpy())

        result = calculate_metrics(np.array(model_result), np.array(targets))
        for metric in result:
            logger.add_scalar('test/' + metric, result[metric], iteration)
        print("epoch:{:2d} iter:{:3d} test: "
              "micro f1: {:.3f} "
              "macro f1: {:.3f} "
              "samples f1: {:.3f}".format(epoch, iteration,
                                          result['micro/f1'],
                                          result['macro/f1'],
                                          result['samples/f1']))

        model.train()
    iteration += 1

loss_value = np.mean(batch_losses)
print("epoch:{:2d} iter:{:3d} train: loss:{:.3f}".format(epoch, iteration, loss_value))
if epoch % save_freq == 0:
    checkpoint_save(model, save_path, epoch)
epoch += 1
if max_epoch_number < epoch:
    break

when I run training, I will get an error message. How can solve this problem? Very thanks!!

This error seems to be raised by Python’s import system and apparently Python isn’t able to find the definition of NusDatasetGCN. Are you importing it from another file or define everything in a single script?

1 Like

Thanks your reply :grin:. Yes, but I write the class in script, maybe I need to write NusDatasetGCN to another file

The code is from here: