AttributeError: module 'utils' has no attribute 'read'

13.8k Views Asked by At

I'm trying to train a model using PyTorch and I got this error AttributeError: module 'utils' has no attribute 'read'

In the main project I have the file utils.pyx and also I'm getting an error in this particular line for i, data in enumerate(trainloader):

Here is the code with the problem (if you have any idea about this issue just let me know Thank you)

import os
import copy
import torch
import torch.nn as nn
import torch.optim as optim
import torch.utils.data
import numpy as np
import matplotlib.pyplot as plt
#from dataset import NTUSkeletonDataset
from torch.utils.data import Dataset, DataLoader
#import GAN
from torch.autograd import Variable
import matplotlib.pyplot as plt
import time

# Root directory for dataset
dataroot = "Data/nturgb+d_skeletons"

# Batch size during training
batch_size = 5

# Size of z latent vector (i.e. size of generator input)
latent_dim = 20

# Number of training epochs
num_epochs = 200

# Learning rate for optimizers
lrG = 0.00005
lrD = 0.00005

clip_value = 0.01
n_critic = 20

trainset = NTUSkeletonDataset(root_dir=dataroot, pinpoint=10)
trainloader = DataLoader(trainset, batch_size=batch_size,
                         shuffle=True, num_workers=4)

cuda = torch.cuda.is_available()
device = torch.device("cuda:0" if cuda else "cpu")
Tensor = torch.cuda.FloatTensor if cuda else torch.FloatTensor

generator = Gen0(latent_dim).to(device)
discriminator = Dis0().to(device)

optimizer_G = torch.optim.RMSprop(generator.parameters(), lr=lrG)
optimizer_D = torch.optim.RMSprop(discriminator.parameters(), lr=lrD)

epoch_loss = np.zeros((num_epochs, 3, len(trainloader)//n_critic+1))

for epoch in range(num_epochs):
    j = 0
    print("Boucle 1")
    epoch_start = time.time()
    for i, data in enumerate(trainloader):
        print("something")
        size = (-1, data.size(-1))
        data = data.reshape(size)
        print
        optimizer_D.zero_grad()

        real_skeleton = Variable(data.type(Tensor)).to(device)

        critic_real = -torch.mean(discriminator(real_skeleton))
        # critic_real.backward()

        # sample noise as generator input
        z = torch.randn(real_skeleton.size(0), latent_dim).to(device)

        # Generate a batch of fake skeleton
        fake_skeleton = generator(z).detach()

        critic_fake = torch.mean(discriminator(fake_skeleton))
        # critic_fake.backward()

        loss_D = critic_real + critic_fake
        loss_D.backward()

        optimizer_D.step()

        # clip weights of discriminator
        for p in discriminator.parameters():
            p.data.clamp_(-clip_value, clip_value)

        # Train the generator every n_critic iterations:
        if i % n_critic == n_critic - 1:
            optimizer_G.zero_grad()

            # Generate a batch of
            gen_skeleton = generator(z)
            # adversarial loss
            loss_G = -torch.mean(discriminator(gen_skeleton))

            loss_G.backward()
            optimizer_G.step()

            for k, l in enumerate((loss_G, critic_real, critic_fake)):
                epoch_loss[epoch, k, j] = l.item()
            j += 1

    epoch_end = time.time()
    print('[%d] time eplased: %.3f' % (epoch, epoch_end-epoch_start))
    for k, l in enumerate(('G', 'critic real', 'critic fake')):
        print('\t', l, epoch_loss[epoch, k].mean(axis=-1))

    if epoch % 20 == 19:
        m = copy.deepcopy(generator.state_dict())
        torch.save(m, 'gen0_%d.pt' % epoch)

np.save('gen0_epoch_loss.npy', epoch_loss)

The full error is :

enter image description here

The utils.pyx

#cython: language_level=3
import numpy as np
cimport numpy as np
import os
cimport cython

def read(fname, max_bodies=2):
    with open(fname, 'r') as f:
        num_frames = int(f.readline())
        keypoints = np.zeros((2, num_frames, 25, 2), dtype=np.float64)

        for t in range(num_frames):
            num_bodies = int(f.readline())

            for m in range(num_bodies):
                f.readline() # Body info, skip
                num_keypoints = int(f.readline())
                for k in range(num_keypoints): # Read joints
                    x, y = f.readline().split()[:2]
                    if m >= max_bodies:
                        continue

                    keypoints[m, t, k, 0] = x
                    keypoints[m, t, k, 1] = y
    return keypoints


@cython.boundscheck(False)
@cython.wraparound(False)
@cython.initializedcheck(False)
@cython.cdivision(True)
cpdef void ins_frames(double[:,:,:,::1] buf, double[:,:,:,::1] data, int diff):
    cdef int n0 = data.shape[1]
    cdef int i = 0
    cdef int j = 0
    cdef int k = 0
    cdef int l = 0
    cdef double v = 0
    cdef int count = 0

    indices = np.linspace(1, n0, num=diff, endpoint=False, dtype=np.int32) \
              + np.arange(diff, dtype=np.int32)
    cdef np.ndarray[np.int32_t, ndim=1] to_ins = indices

    for i in range(to_ins.shape[0]):
        buf[0, to_ins[i], 0, 0] = -10000001 # Marker

    recur = 0

    for i in range(buf.shape[1]):
        if buf[0, i, 0, 0] == -10000001:
            recur += 1
            continue

        for j in range(2):
            for k in range(25):
                for l in range(2):
                    v = data[j, count, k, l]
                    buf[j, i, k, l] = v # Copy
                    if recur != 0: # Calculate the mean
                        buf[j, i-1, k, l] = (v + data[j, count-1, k, l]) * 0.5

        if recur > 0: recur -= 1 # Reset

        count += 1

To get a better idea about the whole code, here you can find my notebook. As well as the dataset.

1

There are 1 best solutions below

3
R. Marolahy On

When I did pip install utils and tried:

>> import utils
>> utils.read()

I got AttributeError: module 'utils' has no attribute 'read'. I think the same thing is happening to your code.

Try to rename your utils.pyx (e.g myutils.pyx) and change:

f = utils.read(...)

to:

f = myutils.read(...)

Don't forget to import it first.