Object has no attribute 'train'

Archy_dragon: <p>Hi just wonder do you know why this happen I defined the train  in begiining and when i want to try to use it error pop up</p>
<pre><code class="lang-auto">
class LFW1(object):#orginal
    def __init__(self, root, specific_folder, img_extension, preprocessing_method=None, crop_size=(96, 112),train = True):
        """
        Dataloader of the LFW dataset.

        root: path to the dataset to be used.
        specific_folder: specific folder inside the same dataset.
        img_extension: extension of the dataset images.
        preprocessing_method: string with the name of the preprocessing method.
        crop_size: retrieval network specific crop size.
        """

        self.preprocessing_method = preprocessing_method
        self.crop_size = crop_size
        self.imgl_list = []
        self.classes = []
        self.people = []
        self.model_align = None

        # read the file with the names and the number of images of each people in the dataset
        with open(os.path.join(root, 'people.txt')) as f:
            people = f.read().splitlines()[1:]

        # get only the people that have more than 20 images
        for p in people:
            p = p.split('\t')
            if len(p) &gt; 1:
                if int(p[1]) &gt;= 20:
                    for num_img in range(1, int(p[1]) + 1):
                        self.imgl_list.append(os.path.join(root, specific_folder, p[0], p[0] + '_' +
                                                           '{:04}'.format(num_img) + '.' + img_extension))
                        self.classes.append(p[0])
                        self.people.append(p[0])

        le = preprocessing.LabelEncoder()
        self.classes = le.fit_transform(self.classes)
        #print(len(self.imgl_list), len(self.classes), len(self.people))
        self.i_train,self.i_test,self.c_train,self.c_test=train_test_split(self.imgl_list,self.classes,test_size=0.2)
        print(len(self.i_train), len(self.i_test), len(self.c_train), len(self.c_test))
       
        

    def __getitem__(self, index):
      if self.train:
        imgl = imageio.imread(self.i_train[index])
        cl = self.c_train[index]

        # if image is grayscale, transform into rgb by repeating the image 3 times
        if len(imgl.shape) == 2:
            imgl = np.stack([imgl] * 3, 2)

        imgl, bb = preprocess(imgl, self.preprocessing_method, crop_size=self.crop_size,
                              is_processing_dataset=True, return_only_largest_bb=True, execute_default=True)

        # append image with its reverse
        imglist = [imgl, imgl[:, ::-1, :]]

        # normalization
        for i in range(len(imglist)):
            imglist[i] = (imglist[i] - 127.5) / 128.0
            imglist[i] = imglist[i].transpose(2, 0, 1)
        imgs = [torch.from_numpy(i).float() for i in imglist]
        
       

        return imgs, cl
        

      else:
        imgl1 = imageio.imread(self.i_test[index])
        cl1 = self.c_test[index]

        # if image is grayscale, transform into rgb by repeating the image 3 times
        if len(imgl1.shape) == 2:
            imgl1 = np.stack([imgl1] * 3, 2)

        imgl1, bb1 = preprocess(imgl1, self.preprocessing_method, crop_size=self.crop_size,
                              is_processing_dataset=True, return_only_largest_bb=True, execute_default=True)

        # append image with its reverse
        imglist1 = [imgl1, imgl1[:, ::-1, :]]

        # normalization
        for i in range(len(imglist1)):
            imglist1[i] = (imglist1[i] - 127.5) / 128.0
            imglist1[i] = imglist1[i].transpose(2, 0, 1)
        imgs1 = [torch.from_numpy(i).float() for i in imglist]
        
        return imgs1, cl1
        

    def __len__(self):
        return len((self.imgl_list),)
</code></pre>
<p>I defined that train = true for train set and train=False for test data when i try to load the data error up up</p>
<pre><code class="lang-auto">dataset1 = LFW1('/content/drive/My Drive/recfaces13/recfaces/datasets/LFW/try','lfw', 'jpg', 'sphereface', (96, 112),train=True)
dataloader1 = torch.utils.data.DataLoader(dataset1, batch_size=1, shuffle=False, num_workers=2, drop_last=False)
for i, data in enumerate(dataloader1):
  inps, labs = data
  print(len(inps))
</code></pre>
<p>it said unexpected keyword argument ‘train’’ im very confuse i defind the train since beginning did i do something wrong here ?</p>

Usama_Hasan: <p>Hy <a class="mention" href="/u/archy_dragon">@Archy_dragon</a>, why are you setting train = true or false, just call</p>
<pre><code class="lang-auto">#for training
model.train()
#for eval
model.eval()
</code></pre>

bsridatta: <p><a class="mention" href="/u/usama_hasan">@Usama_Hasan</a> I dont think it has anything to do with nn.Module() but how the dataset is initialized</p>
<p><a class="mention" href="/u/archy_dragon">@Archy_dragon</a>  It would be helpful if you could provide the error log. Why is the LFW1 dataset not inheriting the Dataset class?</p>

Archy_dragon: <p>this is full log error</p>
<p>AttributeError                            Traceback (most recent call last)<br>
 in ()<br>
1 x=[]<br>
----&gt; 2 for i, data in enumerate(dataloader1):<br>
3   inps, labs = data<br>
4   x.append<br>
5</p>
<p>3 frames<br>
/usr/local/lib/python3.6/dist-packages/torch/_utils.py in reraise(self)<br>
393             # (<a href="https://bugs.python.org/issue2651" rel="nofollow noopener">https://bugs.python.org/issue2651</a>), so we work around it.<br>
394             msg = KeyErrorMessage(msg)<br>
–&gt; 395         raise self.exc_type(msg)</p>
<p>AttributeError: Caught AttributeError in DataLoader worker process 0.<br>
Original Traceback (most recent call last):<br>
File “/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/worker.py”, line 185, in _worker_loop<br>
data = fetcher.fetch(index)<br>
File “/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/fetch.py”, line 44, in fetch<br>
data = [self.dataset[idx] for idx in possibly_batched_index]<br>
File “/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/fetch.py”, line 44, in <br>
data = [self.dataset[idx] for idx in possibly_batched_index]<br>
File “”, line 45, in <strong>getitem</strong><br>
if self.train:<br>
AttributeError: ‘LFW1’ object has no attribute ‘train’</p>
<p>so when it true it will be train and fauls will be test data, So i want to make a function that if i put data or path way in data will load</p>

Archy_dragon: <p>so when it true will be train and false will be test can you tell me how the model,train() and model,eval() work?</p>

Usama_Hasan: <p>Hy <a class="mention" href="/u/archy_dragon">@Archy_dragon</a>, Sorry for replying late. when we call model.eval() some layers like batch normalization etc behave differently. Also it’s a good practice to use it during evaluation.</p>
<aside class="quote no-group quote-modified" data-username="Archy_dragon" data-post="4" data-topic="92436">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/archy_dragon/48/27582_2.png" class="avatar"> Archy_dragon:</div>
<blockquote>
<p>1 x=<span class="chcklst-box fa fa-square-o fa-fw"></span><br>
----&gt; 2 for i, data in enumerate(dataloader1):<br>
3 inps, labs = data<br>
4 x.append<br>
5</p>
</blockquote>
</aside>
<p>Can you add your code inside ‘’’ ‘’’ . It will be easy to debug it then</p>

Usama_Hasan: <aside class="quote no-group" data-username="bsridatta" data-post="3" data-topic="92436">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/bsridatta/48/27176_2.png" class="avatar"> bsridatta:</div>
<blockquote>
<p><a class="mention" href="/u/usama_hasan">@Usama_Hasan</a> I dont think it has anything to do with nn.Module() but how the dataset is initialized</p>
</blockquote>
</aside>
<p>Hy <a class="mention" href="/u/bsridatta">@bsridatta</a>. You’re right Man, I made the comment considering the heading of the issue.</p>

bsridatta: <ol>
<li>Assume the dataset creation is wrong. Though I dont see why it says the argument is not valid</li>
<li>If its wrong then, the error should be in</li>
</ol>
<aside class="quote no-group" data-username="Archy_dragon" data-post="1" data-topic="92436">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/archy_dragon/48/27582_2.png" class="avatar"> Archy_dragon:</div>
<blockquote>
<p><code>dataset1 = LFW1('/content/drive/My Drive/recfaces13/recfaces/datasets/LFW/try','lfw', 'jpg', 'sphereface', (96, 112),train=True)</code></p>
</blockquote>
</aside>
<p>and not here</p>
<aside class="quote no-group" data-username="Archy_dragon" data-post="1" data-topic="92436">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/archy_dragon/48/27582_2.png" class="avatar"> Archy_dragon:</div>
<blockquote>
<p><code>for i, data in enumerate(dataloader1):</code></p>
</blockquote>
</aside>
<p>So I would it has something to do with the dataloader too. That is also the reason for me asking why does the dataset class not inherit torch Dataset class? Could you please change and she if the error still exists?</p>
<aside class="quote no-group" data-username="Archy_dragon" data-post="1" data-topic="92436">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/archy_dragon/48/27582_2.png" class="avatar"> Archy_dragon:</div>
<blockquote>
<p><code>class LFW1(object):#orginal</code></p>
</blockquote>
</aside>

Archy_dragon: <p>where to change? sorry pretty confuse</p>

bsridatta: <aside class="quote no-group" data-username="Archy_dragon" data-post="1" data-topic="92436">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/archy_dragon/48/27582_2.png" class="avatar"> Archy_dragon:</div>
<blockquote>
<p><code>class LFW1(object):#orginal</code></p>
</blockquote>
</aside>
<p>Here, just check the custom dataset tutorial, you can have it as an object but try,</p>
<pre><code class="lang-auto">from torch.utils.data import Dataset    
class LFW1(Dataset)
</code></pre>

Archy_dragon: <p>thank you will give it a try come back to you</p>