Error when trying to evaluate a pretranined model

I am trying to evaluate a pretranined model created by EpicKitchens. But I stuck at an error when I create my own data.
Link of pretrained model is indicated below.

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}>= start_frame and {frame_number}<=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}")

And I get an error
RuntimeError: shape '[-1, 8, 125]' is invalid for input of size 125
I can’t find its solution what should I do?

Here is an example of correct input shape

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)

Hi,

Could you please use triple backticks before and after code to make it more readable!

But I couldn’t load my data appropriately.

What do you mean? The reshape method you show at the beginning should work if your input is indeed of size [1, 8, 1, 3, 224, 224].

I have editted my question, can you look again please?

Thanks, much easier to read ! :slight_smile:

Where does the error happen? Do you have the full stack trace please?

inputs.shape = torch.Size([1, 3, 224, 224])
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-11-f63fb1f7f96f> in <module>()
      6 #   print(inputs.shape)
      7   output = tsn.features(inputs)
----> 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(
--> 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

Does that view in tsn.py looks good to you?

Wait. Why is there a -1 in the shape. Are there negative shapes?

When you reshape or view, you are allowed to have a single -1 in the size. This will be automatically computed to make sure that the number of total elements did not change.

1 Like

Oh wait I see it is doing x.view. I understand why there is a -1

sorry I couldn’t understand.


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 num_segment or segment_count to my inputs