Hi ,
I’m getting this error and I don’t know how to fix it
Screenshot from 2018-05-28 12-52-17.png1854×588 104 KB

import os
import sys
import ast
import numpy as np

import time
import logging
import argparse   
import json
import math
from tqdm import tqdm

import torch
import torch.nn as nn
import torch.optim as optim
from torch.optim.lr_scheduler import MultiStepLR
from torch.autograd import Variable
import torchnet as tnt

from dataset import create_dataset
from mynetwork import mynetwork

#sys.path.append(OTHER FOLDER)


def main():
    parser = argparse.ArgumentParser(description='Large-scale Point Cloud Semantic Segmentation with Superpoint Graphs')
    # GENERAL PARAMETER
    parser.add_argument('--ROOT_PATH', default='./data')
    parser.add_argument('--suffix', default='first_try', help='suffix to all folder, used for benchmarking')
    parser.add_argument('--odir', default='results', help='folder for saving the trained model')
    parser.add_argument('--resume', default='')
    parser.add_argument('--cuda', default=0)
    parser.add_argument('--epochs', default=200, type=int)
    parser.add_argument('--test_nth_epoch', default=10)
    parser.add_argument('--batch_size', default=1, type=int, help='Batch size')

    # NETWORK PARAMETER
    parser.add_argument('--width', default='[8,16,8]', help='shape of model')
    #OPTIMIZER PARAMETER
    parser.add_argument('--wd', default=0, type=float, help='Weight decay')
    parser.add_argument('--lr', default=1e-1, type=float, help='Initial learning rate')
    parser.add_argument('--lr_decay', default=0.7, type=float, help='Multiplicative factor used on learning rate at `lr_steps`')
    parser.add_argument('--lr_steps', default='[150,180]', help='List of epochs where the learning rate is decreased by `lr_decay`')
 


    args = parser.parse_args()

    args.width = ast.literal_eval(args.width)
    args.lr_steps = ast.literal_eval(args.lr_steps)

    args.start_epoch = 0
    
    if not os.path.exists(args.odir):
        os.makedirs(args.odir)
    
    if args.ROOT_PATH[-1]=='/':
        root = args.ROOT_PATH
    else:
        root = args.ROOT_PATH+'/'

    if len(args.suffix)>0 and not args.suffix[0]=='_':
        args.suffix =  '_' + args.suffix
    
    print('Will save embeddings to ' + root + 'results' + args.suffix)
    
    if not os.path.exists(root + 'results' + args.suffix):
        os.makedirs(root + 'results' + args.suffix)
        
    with open(os.path.join(args.odir, 'cmdline.txt'), 'w') as f:
        f.write(" ".join(["'"+a+"'" if (len(a)==0 or a[0]!='-') else a for a in sys.argv]))
    
    logging.getLogger().setLevel(logging.INFO)  #set to logging.DEBUG to allow for more prints

    train_dataset, validation_dataset, nfeats = create_dataset(args)

    args.nfeats = nfeats
    
        # Create model and optimizer
    if args.resume != '':
        if args.resume=='RESUME': args.resume = args.odir + '/model.pth.tar'
        model, optimizer, stats = resume(args)
    else:
        model = create_model(args)
        optimizer = create_optimizer(args, model)
        stats = []
    
    scheduler = MultiStepLR(optimizer, milestones=args.lr_steps, gamma=args.lr_decay, last_epoch=args.start_epoch-1)
    
    def train():
        """ Trains for one epoch """
        
        model.train()
        
        loader = torch.utils.data.DataLoader(train_dataset, batch_size=args.batch_size, shuffle=True, drop_last=True)
            
        loss_meter = tnt.meter.AverageValueMeter()
        acc_meter = tnt.meter.ClassErrorMeter(accuracy=True)

        t0 = time.time()
        
        if logging.getLogger().getEffectiveLevel() > logging.DEBUG: loader = tqdm(loader, ncols=100)
    
        for bidx, (features, objective) in enumerate(loader):
            t_loader = 1000*(time.time()-t0)
            optimizer.zero_grad()
            t0 = time.time() #optimizer.step())
        
            prediction = model.network.forward(Variable(features))
                        
            loss = nn.functional.cross_entropy(prediction, Variable(objective.squeeze()))
            
            loss.backward()

            optimizer.step()

            t_trainer = 1000*(time.time()-t0)

            loss_meter.add(loss.data[0])
            acc_meter.add(prediction.data.cpu().numpy(), objective.cpu().numpy().squeeze())

            logging.debug('Batch loss %f, Loader time %f ms, Trainer time %f ms.', loss.data[0], t_loader, t_trainer)
            
            t0 = time.time()

        return loss_meter.value()[0], acc_meter.value()[0]
    
    def evaluate():
        """ Evaluated model on test set """
        model.eval()
        loader = torch.utils.data.DataLoader(validation_dataset , batch_size=1)
        
        loss_meter = tnt.meter.AverageValueMeter()
        acc_meter = tnt.meter.ClassErrorMeter(accuracy=True)
        
        if logging.getLogger().getEffectiveLevel() > logging.DEBUG: loader = tqdm(loader, ncols=100)

        # iterate over dataset in batches
        for bidx, (features, objective) in enumerate(loader):
            
            prediction = model.network.forward(Variable(features))
                        
            loss = nn.functional.cross_entropy(prediction, Variable(objective.squeeze()))
            
            loss_meter.add(loss.data[0])
            acc_meter.add(prediction.data.cpu().numpy(), objective.cpu().numpy().squeeze())
            
        return loss_meter.value()[0], acc_meter.value()[0]
    # Training loop
    #
    for epoch in range(args.start_epoch, args.epochs):
        print('Epoch {}/{} ({}):'.format(epoch, args.epochs, args.odir))
        scheduler.step()

        loss, acc = train()

        if (epoch+1) % args.test_nth_epoch == 0 or epoch+1==args.epochs:
           loss_test, acc_test = evaluate()
           print('-> Train Loss: %1.5f, \t Test Loss: %1.5f \t Train acc: %3.2f %%, \t Test acc: %3.2f %%' % (loss, loss_test, acc, acc_test))
        else:
            print('-> Train loss: %2.4f Train acc %3.2f %%' % (loss, acc))

        stats.append({'epoch': epoch, 'loss': loss, 'acc' : acc})

        print("saving")
        with open(os.path.join(args.odir, 'trainlog.txt'), 'w') as outfile:
            json.dump(stats, outfile)
            torch.save({'epoch': epoch + 1, 'args': args, 'state_dict': model.state_dict(), 'optimizer' : optimizer.state_dict()},
                       os.path.join(args.odir, 'model.pth.tar'))

        if math.isnan(loss): break
    
def create_model(args):
    """ Creates model """
    model = nn.Module()

    model.network = mynetwork(args.nfeats, args.width)
   
    print('Total number of parameters: {}'.format(sum([p.numel() for p in model.parameters()])))
    print(model)    
    if args.cuda: 
        model.cuda()
    return model

def resume(args):
    """ Loads model and optimizer state from a previous checkpoint. """
    print("=> loading checkpoint '{}'".format(args.resume))
    checkpoint = torch.load(args.resume)
    model = create_model(checkpoint['args']) #use original arguments, architecture can't change
    optimizer = create_optimizer(args, model)
    
    model.load_state_dict(checkpoint['state_dict'])
    if 'optimizer' in checkpoint: optimizer.load_state_dict(checkpoint['optimizer'])
    for group in optimizer.param_groups: group['initial_lr'] = args.lr
    args.start_epoch = checkpoint['epoch']
    try:
        stats = json.loads(open(os.path.join(os.path.dirname(args.resume), 'trainlog.txt')).read())
    except:
        stats = []
    return model, optimizer, stats


def create_optimizer(args, model):
        return optim.Adam(model.parameters(), lr=args.lr, weight_decay=args.wd)
    
if __name__ == "__main__": 
    main()


MYNETWORK

import torch
import torch.nn as nn
import torch.nn.init as init


class mynetwork(nn.Module):

    def __init__(self, nfeats, width):
        super(mynetwork, self).__init__()
        
        self.width = width 
        self.nfeats = nfeats 
        
        mlp_modules = []
        for k in range(len(width)):
            mlp_modules.append(nn.Linear(width[k-1] if k>0 else nfeats, width[k]))
            mlp_modules.append(nn.ReLU(True))
        mlp_modules.append(nn.Linear(width[-1], 10))
        self.mlp = nn.Sequential(*mlp_modules)

    def forward(self, data):
        out = self.mlp(data).squeeze()        
        return nn.functional.softmax(out)