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

111401: <p>Hi, I have trouble. When I run my graph convolutional model, the pytorch will respond the error message like:<br>
AttributeError: Can’t get attribute ‘NusDatasetGCN’ on &lt;module ‘<strong>main</strong>’ (built-in)&gt;</p>
<p>#<span class="hashtag">#The</span> NusDatasetGCN code:</p>
<p>class NusDatasetGCN(Dataset):<br>
def <strong>init</strong>(self, data_path, anno_path, transforms, w2v_path):<br>
self.transforms = transforms</p>
<pre><code>    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)
</code></pre>
<p>###################################</p>
<h1>Run training.</h1>
<p>epoch = 0<br>
iteration = 0<br>
while True:<br>
batch_losses = []<br>
for batch_number, (imgs, targets, gcn_input) in enumerate(train_dataloader):<br>
imgs, targets, gcn_input = imgs.to(device), targets.to(device), gcn_input.to(device)<br>
optimizer.zero_grad()</p>
<pre><code>    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 &lt; epoch:
    break
</code></pre>
<p>when I run training, I will get an error message. How can solve this problem? Very thanks!!</p>

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

111401: <p>Thanks your reply <img src="https://discuss.pytorch.org/images/emoji/apple/grin.png?v=9" title=":grin:" class="emoji" alt=":grin:">. Yes, but I write the class in script, maybe I need to write NusDatasetGCN to another file</p>

111401: <p>The code is from here:</p>
<aside class="onebox githubblob">
  <header class="source">
      <a href="https://github.com/spmallick/learnopencv/blob/master/Graph-Convolutional-Networks-Model-Relations-In-Data/graph_convolutional_networks_model_relations_in_data.ipynb" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>
  <article class="onebox-body">
    <h4><a href="https://github.com/spmallick/learnopencv/blob/master/Graph-Convolutional-Networks-Model-Relations-In-Data/graph_convolutional_networks_model_relations_in_data.ipynb" target="_blank" rel="noopener nofollow ugc">spmallick/learnopencv/blob/master/Graph-Convolutional-Networks-Model-Relations-In-Data/graph_convolutional_networks_model_relations_in_data.ipynb</a></h4>
<pre><code class="lang-ipynb">{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# Install requirements\n",
    "!pip install numpy scikit-image scipy scikit-learn matplotlib tqdm tensorflow torch torchvision"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
</code></pre>

  This file has been truncated. <a href="https://github.com/spmallick/learnopencv/blob/master/Graph-Convolutional-Networks-Model-Relations-In-Data/graph_convolutional_networks_model_relations_in_data.ipynb" target="_blank" rel="noopener nofollow ugc">show original</a>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>