List index out of range

class TripletNet(nn.Module):
     
    def __init__(self, embedding_net):
         super(TripletNet, self).__init__()
         self.embedding_net = embedding_net

    def forward(self,inputs):
        if type(inputs) in (tuple,list):
             x1,x2,x3 = inputs

            output1 = self.embedding_net(x1)
            output2 = self.embedding_net(x2)
            output3 = self.embedding_net(x3)
            return output1, output2, output3
        else:
            return self.embedding_net(inputs)
    def get_embedding(self, x):

           return self.embedding_net(x)


from datasets import TripleHTRU as TripletHTRU
from losses import TripletLoss

triplet_train_dataset = TripletHTRU(train_dataset) # Returns triplets of images
triplet_test_dataset = TripletHTRU(test_dataset)
batch_size = 4
kwargs = {'num_workers': 2, 'pin_memory': True} if cuda else {}
triplet_train_loader = DataLoader(triplet_train_dataset, 
batch_size=batch_size, shuffle=True, **kwargs)
triplet_test_loader = DataLoader(triplet_test_dataset, 
batch_size=batch_size, shuffle=False, **kwargs)

# Set up the network and training parameters
trunk = torchvision.models.resnet18(pretrained=True)
trunk_output_size = trunk.fc.in_features
trunk.fc = common_functions.Identity()
margin = 1.
model = TripletNet(trunk)
if cuda:
    model.cuda()
def triple_loss_func(loss_func):
    def loss(embeddings,labels):
        return loss_func(*embeddings)
    return loss
my_loss = triple_loss_func(loss_func=TripletLoss(margin))
tri_learn = Learner(dls,model,loss_func=my_loss,opt_func=SGD)

dls = DataLoaders(triplet_test_loader,triplet_test_loader)

IndexError Traceback (most recent call last)
in
----> 1 tri_learn.fit_one_cycle(2,slice(8e-2))

~/anaconda3/lib/python3.8/site-packages/fastai/callback/schedule.py in fit_one_cycle(self, n_epoch, lr_max, div, div_final, pct_start, wd, moms, cbs, reset_opt)
114 scheds = {‘lr’: combined_cos(pct_start, lr_max/div, lr_max, lr_max/div_final),
115 ‘mom’: combined_cos(pct_start, *(self.moms if moms is None else moms))}
→ 116 self.fit(n_epoch, cbs=ParamScheduler(scheds)+L(cbs), reset_opt=reset_opt, wd=wd)
117
118 # Cell

~/anaconda3/lib/python3.8/site-packages/fastai/learner.py in fit(self, n_epoch, lr, wd, cbs, reset_opt)
219 self.opt.set_hypers(lr=self.lr if lr is None else lr)
220 self.n_epoch = n_epoch
→ 221 self._with_events(self._do_fit, ‘fit’, CancelFitException, self._end_cleanup)
222
223 def _end_cleanup(self): self.dl,self.xb,self.yb,self.pred,self.loss = None,(None,),(None,),None,None

~/anaconda3/lib/python3.8/site-packages/fastai/learner.py in with_events(self, f, event_type, ex, final)
161
162 def with_events(self, f, event_type, ex, final=noop):
→ 163 try: self(f’before
{event_type}’); f()
164 except ex: self(f’after_cancel
{event_type}’)
165 self(f’after_{event_type}’); final()

~/anaconda3/lib/python3.8/site-packages/fastai/learner.py in _do_fit(self)
210 for epoch in range(self.n_epoch):
211 self.epoch=epoch
→ 212 self._with_events(self._do_epoch, ‘epoch’, CancelEpochException)
213
214 def fit(self, n_epoch, lr=None, wd=None, cbs=None, reset_opt=False):

~/anaconda3/lib/python3.8/site-packages/fastai/learner.py in with_events(self, f, event_type, ex, final)
161
162 def with_events(self, f, event_type, ex, final=noop):
→ 163 try: self(f’before
{event_type}’); f()
164 except ex: self(f’after_cancel
{event_type}’)
165 self(f’after_{event_type}’); final()

~/anaconda3/lib/python3.8/site-packages/fastai/learner.py in _do_epoch(self)
205 def _do_epoch(self):
206 self._do_epoch_train()
→ 207 self._do_epoch_validate()
208
209 def _do_fit(self):

~/anaconda3/lib/python3.8/site-packages/fastai/learner.py in _do_epoch_validate(self, ds_idx, dl)
201 if dl is None: dl = self.dls[ds_idx]
202 self.dl = dl
→ 203 with torch.no_grad(): self._with_events(self.all_batches, ‘validate’, CancelValidException)
204
205 def _do_epoch(self):

~/anaconda3/lib/python3.8/site-packages/fastai/learner.py in with_events(self, f, event_type, ex, final)
161
162 def with_events(self, f, event_type, ex, final=noop):
→ 163 try: self(f’before
{event_type}’); f()
164 except ex: self(f’after_cancel
{event_type}’)
165 self(f’after_{event_type}’); final()

~/anaconda3/lib/python3.8/site-packages/fastai/learner.py in all_batches(self)
167 def all_batches(self):
168 self.n_iter = len(self.dl)
→ 169 for o in enumerate(self.dl): self.one_batch(*o)
170
171 def _do_one_batch(self):

~/anaconda3/lib/python3.8/site-packages/fastai/learner.py in one_batch(self, i, b)
192 b = self._set_device(b)
193 self._split(b)
→ 194 self._with_events(self._do_one_batch, ‘batch’, CancelBatchException)
195
196 def _do_epoch_train(self):

~/anaconda3/lib/python3.8/site-packages/fastai/learner.py in with_events(self, f, event_type, ex, final)
163 try: self(f’before
{event_type}’); f()
164 except ex: self(f’after_cancel_{event_type}’)
→ 165 self(f’after_{event_type}’); final()
166
167 def all_batches(self):

~/anaconda3/lib/python3.8/site-packages/fastai/learner.py in call(self, event_name)
139
140 def ordered_cbs(self, event): return [cb for cb in self.cbs.sorted(‘order’) if hasattr(cb, event)]
→ 141 def call(self, event_name): L(event_name).map(self._call_one)
142
143 def _call_one(self, event_name):

~/anaconda3/lib/python3.8/site-packages/fastcore/foundation.py in map(self, f, gen, *args, **kwargs)
152 def range(cls, a, b=None, step=None): return cls(range_of(a, b=b, step=step))
153
→ 154 def map(self, f, *args, gen=False, **kwargs): return self._new(map_ex(self, f, *args, gen=gen, **kwargs))
155 def argwhere(self, f, negate=False, **kwargs): return self._new(argwhere(self, f, negate, **kwargs))
156 def argfirst(self, f, negate=False): return first(i for i,o in self.enumerate() if f(o))

~/anaconda3/lib/python3.8/site-packages/fastcore/basics.py in map_ex(iterable, f, gen, *args, **kwargs)
664 res = map(g, iterable)
665 if gen: return res
→ 666 return list(res)
667
668 # Cell

~/anaconda3/lib/python3.8/site-packages/fastcore/basics.py in call(self, *args, **kwargs)
649 if isinstance(v,_Arg): kwargs[k] = args.pop(v.i)
650 fargs = [args[x.i] if isinstance(x, _Arg) else x for x in self.pargs] + args[self.maxi+1:]
→ 651 return self.func(*fargs, **kwargs)
652
653 # Cell

~/anaconda3/lib/python3.8/site-packages/fastai/learner.py in _call_one(self, event_name)
143 def _call_one(self, event_name):
144 if not hasattr(event, event_name): raise Exception(f’missing {event_name}’)
→ 145 for cb in self.cbs.sorted(‘order’): cb(event_name)
146
147 def _bn_bias_state(self, with_bias): return norm_bias_params(self.model, with_bias).map(self.opt.state)

~/anaconda3/lib/python3.8/site-packages/fastai/callback/core.py in call(self, event_name)
43 (self.run_valid and not getattr(self, ‘training’, False)))
44 res = None
—> 45 if self.run and _run: res = getattr(self, event_name, noop)()
46 if event_name==‘after_fit’: self.run=True #Reset self.run to True at each end of fit
47 return res

~/anaconda3/lib/python3.8/site-packages/fastai/learner.py in after_batch(self)
502 if len(self.yb) == 0: return
503 mets = self._train_mets if self.training else self._valid_mets
→ 504 for met in mets: met.accumulate(self.learn)
505 if not self.training: return
506 self.lrs.append(self.opt.hypers[-1][‘lr’])

~/anaconda3/lib/python3.8/site-packages/fastai/learner.py in accumulate(self, learn)
436 def reset(self): self.total,self.count = 0.,0
437 def accumulate(self, learn):
→ 438 bs = find_bs(learn.yb)
439 self.total += learn.to_detach(learn.loss.mean())*bs
440 self.count += bs

~/anaconda3/lib/python3.8/site-packages/fastai/torch_core.py in find_bs(b)
567 def find_bs(b):
568 “Recursively search the batch size of b.”
→ 569 return item_find(b).shape[0]
570
571 # Cell

~/anaconda3/lib/python3.8/site-packages/fastai/torch_core.py in item_find(x, idx)
553 def item_find(x, idx=0):
554 "Recursively takes the idx-th element of x"
→ 555 if is_listy(x): return item_find(x[idx])
556 if isinstance(x,dict):
557 key = list(x.keys())[idx] if isinstance(idx, int) else idx

~/anaconda3/lib/python3.8/site-packages/fastai/torch_core.py in item_find(x, idx)
553 def item_find(x, idx=0):
554 "Recursively takes the idx-th element of x"
→ 555 if is_listy(x): return item_find(x[idx])
556 if isinstance(x,dict):
557 key = list(x.keys())[idx] if isinstance(idx, int) else idx

IndexError: list index out of range

It seems the error arise in the process of calculating the metric ,but I didn’t call any metric.
New to this forum,thanks in advance.

I have solved this problem.It was because my TripletHTRU dataset return (img1,img2,img3),[] . The target is a empty list.