Error when trying to evaluate a pretranined model

username: sevdaasyn
cooked: <p>I am trying to evaluate a pretranined model created by EpicKitchens. But I stuck at an error when I create my own data.<br>
Link of pretrained model is indicated below.</p>
<aside class="onebox whitelistedgeneric">
  <header class="source">
      <img src="https://github.githubassets.com/favicon.ico" class="site-icon" width="32" height="32">
      <a href="https://github.com/epic-kitchens/action-models" target="_blank" rel="nofollow noopener">GitHub</a>
  </header>
  <article class="onebox-body">
    <img src="https://avatars1.githubusercontent.com/u/32941131?s=400&amp;v=4" class="thumbnail onebox-avatar" width="400" height="400">

<h3><a href="https://github.com/epic-kitchens/action-models" target="_blank" rel="nofollow noopener">epic-kitchens/action-models</a></h3>

<p>EPIC-Kitchens baselines for Action Recognition. Contribute to epic-kitchens/action-models development by creating an account on GitHub.</p>


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

<pre><code class="lang-auto">class OwnDataset(Dataset):
    running_loss = 0.0
    running_corrects = 0
    
    def __init__(self, image_paths,transform, length):
        self.image_paths = image_paths
        self.transform = transform
        self.length = length
        
    def __getitem__(self, index):
        dimensions =[]
        image_path_at_index = self.image_paths[index]

        video_name = image_path_at_index.split('/')[-2] # P01_01
        frame_name = image_path_at_index.split('/')[-1] # frame_0000000001.jpg
        frame_number = int(frame_name.split('_')[-1][:-4]) # 1
#         print(frame_number)
        #frame_number = 9

        selected_row = df_train_action_labels.query(f"video_id == \"{video_name}\" and {frame_number}&gt;= start_frame and {frame_number}&lt;=stop_frame")
        selected_row = selected_row.drop(columns=['uid','participant_id','start_timestamp', 'stop_timestamp', 'start_frame', 'stop_frame', 'all_nouns', 'all_noun_classes'])
        row_list = []			
        for index, rows in selected_row.iterrows(): 
          my_list =[rows.video_id ,rows.verb, rows.verb_class, rows.noun, rows.noun_class] 
          row_list.append(my_list)

#         print(row_list)

        # print("image_path_at_index: ", image_path_at_index) image_path_at_index:  /content/drive/My Drive/Videos/train/P01_01/frame_0000000001.jpg
        image = Image.open(image_path_at_index) 
        
        if self.transform:
          image = self.transform(image)
       
        # train_video_names
        # df_train_action_labels[df_train_action_labels.video_id == "P01_01"]

        x = image
        y = row_list


        return x, y
    
    
    def __len__(self):
        return self.length
      
      

df_train_action_labels = pd.read_csv(annotations_dir +'/EPIC_train_action_labels.csv')


class_paths_train = [d.path for d in os.scandir(train_dir) if d.is_dir]
whole_train_image_paths = []

train_video_names = [path.split('/')[-1] for path in class_paths_train]


for c, class_path in enumerate(class_paths_train):
        paths = sorted(glob.glob(os.path.join(class_path, '*.jpg')))  
        whole_train_image_paths.extend(paths)

# print(whole_train_image_paths)  ['/content/drive/My Drive/Videos/test/P01_12/0000000001.jpg', '/content/drive/My Drive/Videos/test/P01_12/0000000031.jpg', '/....]


transform = transforms.Compose([
    transforms.Resize(size=(224, 224)),
    transforms.ToTensor(),
    transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
])
dataset_train = OwnDataset(
    image_paths = whole_train_image_paths,
    transform = transform,
    length = len(whole_train_image_paths)
)
loader_train = DataLoader(
    dataset_train,
    batch_size=1,
    num_workers=1
)

for i, data in enumerate(loader_train):

  inputs,labels = data
  output = tsn.features(inputs)
  verb_logits, noun_logits = tsn.logits(features)
  
  if isinstance(output, tuple):
      print(f"Outputsssss shape: {[o.shape for o in output]}")
  else:
      print(f"Output shape: {output.shape}")

</code></pre>
<p>And I get an error<br>
<code>RuntimeError: shape '[-1, 8, 125]' is invalid for input of size 125</code><br>
I can’t find its solution what should I do?</p>
<p>Here is an example of correct input shape</p>
<pre><code class="lang-auto">batch_size = 1
segment_count = 8
snippet_length = 1  # Number of frames composing the snippet, 1 for RGB, 5 for optical flow
snippet_channels = 3  # Number of channels in a frame, 3 for RGB, 2 for optical flow
height, width = 224, 224

inputs = torch.randn(
    [batch_size, segment_count, snippet_length, snippet_channels, height, width]
)
# The segment and snippet length and channel dimensions are collapsed into the channel
# dimension
# Input shape: N x TC x H x W
print("b1" , inputs.shape  #[1, 8, 1, 3 224, 224]
inputs = inputs.reshape((batch_size, -1, height, width))
print( inputs.shape)  #[1, 24, 224, 224]
for model in [tsn]:
    # You can get features out of the models
    features = model.features(inputs)
    # and then classify those features
    verb_logits, noun_logits = model.logits(features)
    
    # or just call the object to classify inputs in a single forward pass
    verb_logits, noun_logits = model(inputs)
    print(verb_logits.shape, noun_logits.shape)
</code></pre>

username: albanD
cooked: <p>Hi,</p>
<p>Could you please use triple backticks before and after code to make it more readable!</p>
<blockquote>
<p>But I couldn’t load my data appropriately.</p>
</blockquote>
<p>What do you mean? The reshape method you show at the beginning should work if your input is indeed of size <code>[1, 8, 1, 3, 224, 224]</code>.</p>

username: sevdaasyn
cooked: <p>I have editted my question, can you look again please?</p>

username: albanD
cooked: <p>Thanks, much easier to read ! <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>
<p>Where does the error happen? Do you have the full stack trace please?</p>

username: sevdaasyn
cooked: <pre><code class="lang-auto">inputs.shape = torch.Size([1, 3, 224, 224])
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
&lt;ipython-input-11-f63fb1f7f96f&gt; in &lt;module&gt;()
      6 #   print(inputs.shape)
      7   output = tsn.features(inputs)
----&gt; 8   verb_logits, noun_logits = tsn.logits(output)
      9 
     10 

/root/.cache/torch/hub/epic-kitchens_action-models_master/tsn.py in logits(self, features)
    412             if self.reshape:
    413                 logits_verb = logits_verb.view(
--&gt; 414                     (-1, self.num_segments) + logits_verb.size()[1:]
    415                 )
    416             output_verb = self.consensus(logits_verb)

RuntimeError: shape '[-1, 8, 125]' is invalid for input of size 125
</code></pre>

username: albanD
cooked: <p>Does that <code>view</code> in <code>tsn.py</code> looks good to you?</p>

username: raghavendragaleppa
cooked: <p>Wait. Why is there a -1 in the shape. Are there negative shapes?</p>

username: albanD
cooked: <p>When you reshape or view, you are allowed to have a single <code>-1</code> in the size. This will be automatically computed to make sure that the number of total elements did not change.</p>

username: raghavendragaleppa
cooked: <p>Oh wait I see it is doing x.view. I understand why there is a -1</p>

username: sevdaasyn
cooked: <p>sorry I couldn’t understand.</p>
<p><aside class="onebox githubblob">
  <header class="source">
      <a href="https://github.com/epic-kitchens/action-models/blob/master/tsn.py" target="_blank" rel="nofollow noopener">github.com</a>
  </header>
  <article class="onebox-body">
    <h4><a href="https://github.com/epic-kitchens/action-models/blob/master/tsn.py" target="_blank" rel="nofollow noopener">epic-kitchens/action-models/blob/master/tsn.py</a></h4>
<pre><code class="lang-py">#!/usr/bin/env python
# Copyright (c) 2016, Multimedia Laboratory, The Chinese University of Hong Kong
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
#   list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
#   this list of conditions and the following disclaimer in the documentation
#   and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
</code></pre>

  This file has been truncated. <a href="https://github.com/epic-kitchens/action-models/blob/master/tsn.py" target="_blank" rel="nofollow noopener">show original</a>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>
<br>
I found 414th line in this file. But still don’t understand what my problem is. I think it is because of that I couldn’t include <code>num_segment </code> or <code>segment_count</code> to my inputs</p>