Runtime error when training the first model - 01_intro

I chose AWS sage maker for running the Jupiter notebooks however when I tried to train the first model I get this error. I’m not quite sure what’s going on here or how to trace it so any help would be much appreciated, thanks!

Could not do one pass in your dataloader, there is something wrong in it

RuntimeError Traceback (most recent call last)
in ()
11
12 learn = cnn_learner(dls, resnet34, metrics=error_rate)
—> 13 learn.fine_tune(1)

/home/ec2-user/SageMaker/.env/fastai/lib/python3.6/site-packages/fastai/callback/schedule.py in fine_tune(self, epochs, base_lr, freeze_epochs, lr_mult, pct_start, div, **kwargs)
156 “Fine tune with freeze for freeze_epochs then with unfreeze from epochs using discriminative LR”
157 self.freeze()
→ 158 self.fit_one_cycle(freeze_epochs, slice(base_lr), pct_start=0.99, **kwargs)
159 base_lr /= 2
160 self.unfreeze()

/home/ec2-user/SageMaker/.env/fastai/lib/python3.6/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)
111 scheds = {‘lr’: combined_cos(pct_start, lr_max/div, lr_max, lr_max/div_final),
112 ‘mom’: combined_cos(pct_start, *(self.moms if moms is None else moms))}
→ 113 self.fit(n_epoch, cbs=ParamScheduler(scheds)+L(cbs), reset_opt=reset_opt, wd=wd)
114
115 # Cell

/home/ec2-user/SageMaker/.env/fastai/lib/python3.6/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

/home/ec2-user/SageMaker/.env/fastai/lib/python3.6/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()

/home/ec2-user/SageMaker/.env/fastai/lib/python3.6/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):

/home/ec2-user/SageMaker/.env/fastai/lib/python3.6/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()

/home/ec2-user/SageMaker/.env/fastai/lib/python3.6/site-packages/fastai/learner.py in _do_epoch(self)
204
205 def _do_epoch(self):
→ 206 self._do_epoch_train()
207 self._do_epoch_validate()
208

/home/ec2-user/SageMaker/.env/fastai/lib/python3.6/site-packages/fastai/learner.py in _do_epoch_train(self)
196 def _do_epoch_train(self):
197 self.dl = self.dls.train
→ 198 self._with_events(self.all_batches, ‘train’, CancelTrainException)
199
200 def _do_epoch_validate(self, ds_idx=1, dl=None):

/home/ec2-user/SageMaker/.env/fastai/lib/python3.6/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()

/home/ec2-user/SageMaker/.env/fastai/lib/python3.6/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):

/home/ec2-user/SageMaker/.env/fastai/lib/python3.6/site-packages/fastai/data/load.py in iter(self)
107 self.before_iter()
108 self.__idxs=self.get_idxs() # called in context of main process (not workers/subprocesses)
→ 109 for b in _loadersself.fake_l.num_workers==0:
110 if self.device is not None: b = to_device(b, self.device)
111 yield self.after_batch(b)

/home/ec2-user/SageMaker/.env/fastai/lib/python3.6/site-packages/torch/utils/data/dataloader.py in next(self)
519 if self._sampler_iter is None:
520 self._reset()
→ 521 data = self._next_data()
522 self._num_yielded += 1
523 if self._dataset_kind == _DatasetKind.Iterable and \

/home/ec2-user/SageMaker/.env/fastai/lib/python3.6/site-packages/torch/utils/data/dataloader.py in _next_data(self)
1201 else:
1202 del self._task_info[idx]
→ 1203 return self._process_data(data)
1204
1205 def _try_put_index(self):

/home/ec2-user/SageMaker/.env/fastai/lib/python3.6/site-packages/torch/utils/data/dataloader.py in _process_data(self, data)
1227 self._try_put_index()
1228 if isinstance(data, ExceptionWrapper):
→ 1229 data.reraise()
1230 return data
1231

/home/ec2-user/SageMaker/.env/fastai/lib/python3.6/site-packages/torch/_utils.py in reraise(self)
423 # have message field
424 raise self.exc_type(message=msg)
→ 425 raise self.exc_type(msg)
426
427

RuntimeError: Caught RuntimeError in DataLoader worker process 0.
Original Traceback (most recent call last):
File “/home/ec2-user/SageMaker/.env/fastai/lib/python3.6/site-packages/torch/utils/data/_utils/worker.py”, line 287, in _worker_loop
data = fetcher.fetch(index)
File “/home/ec2-user/SageMaker/.env/fastai/lib/python3.6/site-packages/torch/utils/data/_utils/fetch.py”, line 34, in fetch
data = next(self.dataset_iter)
File “/home/ec2-user/SageMaker/.env/fastai/lib/python3.6/site-packages/fastai/data/load.py”, line 118, in create_batches
yield from map(self.do_batch, self.chunkify(res))
File “/home/ec2-user/SageMaker/.env/fastai/lib/python3.6/site-packages/fastcore/basics.py”, line 216, in chunked
res = list(itertools.islice(it, chunk_sz))
File “/home/ec2-user/SageMaker/.env/fastai/lib/python3.6/site-packages/fastai/data/load.py”, line 133, in do_item
try: return self.after_item(self.create_item(s))
File “/home/ec2-user/SageMaker/.env/fastai/lib/python3.6/site-packages/fastcore/transform.py”, line 200, in call
def call(self, o): return compose_tfms(o, tfms=self.fs, split_idx=self.split_idx)
File “/home/ec2-user/SageMaker/.env/fastai/lib/python3.6/site-packages/fastcore/transform.py”, line 150, in compose_tfms
x = f(x, **kwargs)
File “/home/ec2-user/SageMaker/.env/fastai/lib/python3.6/site-packages/fastcore/transform.py”, line 73, in call
def call(self, x, **kwargs): return self._call(‘encodes’, x, **kwargs)
File “/home/ec2-user/SageMaker/.env/fastai/lib/python3.6/site-packages/fastcore/transform.py”, line 83, in _call
return self.do_call(getattr(self, fn), x, **kwargs)
File “/home/ec2-user/SageMaker/.env/fastai/lib/python3.6/site-packages/fastcore/transform.py”, line 90, in do_call
res = tuple(self.do_call(f, x, **kwargs) for x
in x)
File “/home/ec2-user/SageMaker/.env/fastai/lib/python3.6/site-packages/fastcore/transform.py”, line 90, in
res = tuple(self.do_call(f, x, **kwargs) for x
in x)
File “/home/ec2-user/SageMaker/.env/fastai/lib/python3.6/site-packages/fastcore/transform.py”, line 89, in _do_call
return retain_type(f(x, **kwargs), x, ret)
File “/home/ec2-user/SageMaker/.env/fastai/lib/python3.6/site-packages/fastcore/dispatch.py”, line 118, in call
return f(*args, **kwargs)
File “/home/ec2-user/SageMaker/.env/fastai/lib/python3.6/site-packages/fastai/vision/core.py”, line 223, in encodes
def encodes(self, o:PILBase): return o._tensor_cls(image2tensor(o))
File “/home/ec2-user/SageMaker/.env/fastai/lib/python3.6/site-packages/fastai/vision/core.py”, line 93, in image2tensor
res = tensor(img)
File “/home/ec2-user/SageMaker/.env/fastai/lib/python3.6/site-packages/fastai/torch_core.py”, line 134, in tensor
else as_tensor(x, **kwargs) if hasattr(x, ‘array’) or is_iter(x)
RuntimeError: Could not infer dtype of PILImage

I ran into the same error - it’s incredibly demoralising when learning something new, and out of the box the very first thing you do doesn’t work.

I found my answer here

ssh into your box, and run pip install pillow==8.2. Then try and run the code again.