Hi,
I am trying to run an image regression task.
dls = ImageDataLoaders.from_df(train, path = Path('./train'),fn_col='fn',label_col='wind_speed',y_block=RegressionBlock,seed=2020,item_tfms=Resize(224),batch_tfms=aug_transforms(size=224),bs=15) dls.show_batch()
Above code works fine.
But when I run the below code, training finishes smoothly but start of validation causes the error -
grid_sampler(): expected input and grid to have same dtype, but input has float and grid has c10::Half
May be related to issues with pytorch
`archs = ['efficientnet']
prediction_array = np.zeros(shape=(test.shape[0],1))
for arch in archs:
print('Model ',arch)
cbs=SaveModelCallback(every_epoch=False,fname='/content/drive/My Drive/wind-speed/models/effnet')
learn = timm_learner(dls, 'efficientnet_b3',loss_func=MSELossFlat(),metrics=[rmse],cbs=cbs)
#learn = cnn_learner(dls,arch,loss_func=MSELossFlat(),metrics=[rmse],cbs=cbs).to_fp16()
learn.fine_tune(5)
tdl = learn.dls.test_dl(test)
preds = learn.get_preds(dl=tdl)
prediction_array += preds[0].numpy()
print('Prediction completed !!!')`
Model efficientnet
0.00% [0/1 00:00<00:00]
epoch train_loss valid_loss _rmse time
0.00% [0/937 00:00<00:00]
RuntimeError Traceback (most recent call last)
in ()
10 learn = timm_learner(dls, 'efficientnet_b3',loss_func=MSELossFlat(),metrics=[rmse],cbs=cbs)
11 #learn = cnn_learner(dls,arch,loss_func=MSELossFlat(),metrics=[rmse],cbs=cbs).to_fp16()
---> 12 learn.fine_tune(5)
13 tdl = learn.dls.test_dl(test)
14 preds = learn.get_preds(dl=tdl)
28 frames
/usr/local/lib/python3.6/dist-packages/fastai/callback/schedule.py in fine_tune(self, epochs, base_lr, freeze_epochs, lr_mult, pct_start, div, **kwargs)
155 "Fine tune with freeze
for freeze_epochs
then with unfreeze
from epochs
using discriminative LR"
156 self.freeze()
--> 157 self.fit_one_cycle(freeze_epochs, slice(base_lr), pct_start=0.99, **kwargs)
158 base_lr /= 2
159 self.unfreeze()
/usr/local/lib/python3.6/dist-packages/fastai/callback/schedule.py in fit_one_cycle(self, n_epoch, lr_max, div, div_final, pct_start, wd, moms, cbs, reset_opt)
110 scheds = {'lr': combined_cos(pct_start, lr_max/div, lr_max, lr_max/div_final),
111 'mom': combined_cos(pct_start, *(self.moms if moms is None else moms))}
--> 112 self.fit(n_epoch, cbs=ParamScheduler(scheds)+L(cbs), reset_opt=reset_opt, wd=wd)
113
114 # Cell
/usr/local/lib/python3.6/dist-packages/fastai/learner.py in fit(self, n_epoch, lr, wd, cbs, reset_opt)
209 self.opt.set_hypers(lr=self.lr if lr is None else lr)
210 self.n_epoch = n_epoch
--> 211 self._with_events(self._do_fit, 'fit', CancelFitException, self._end_cleanup)
212
213 def _end_cleanup(self): self.dl,self.xb,self.yb,self.pred,self.loss = None,(None,),(None,),None,None
/usr/local/lib/python3.6/dist-packages/fastai/learner.py in with_events(self, f, event_type, ex, final)
158
159 def with_events(self, f, event_type, ex, final=noop):
--> 160 try: self(f'before{event_type}'); f()
161 except ex: self(f'after_cancel{event_type}')
162 self(f'after_{event_type}'); final()
/usr/local/lib/python3.6/dist-packages/fastai/learner.py in _do_fit(self)
200 for epoch in range(self.n_epoch):
201 self.epoch=epoch
--> 202 self._with_events(self._do_epoch, 'epoch', CancelEpochException)
203
204 def fit(self, n_epoch, lr=None, wd=None, cbs=None, reset_opt=False):
/usr/local/lib/python3.6/dist-packages/fastai/learner.py in with_events(self, f, event_type, ex, final)
158
159 def with_events(self, f, event_type, ex, final=noop):
--> 160 try: self(f'before{event_type}'); f()
161 except ex: self(f'after_cancel{event_type}')
162 self(f'after_{event_type}'); final()
/usr/local/lib/python3.6/dist-packages/fastai/learner.py in _do_epoch(self)
195 def _do_epoch(self):
196 self._do_epoch_train()
--> 197 self._do_epoch_validate()
198
199 def _do_fit(self):
/usr/local/lib/python3.6/dist-packages/fastai/learner.py in _do_epoch_validate(self, ds_idx, dl)
191 if dl is None: dl = self.dls[ds_idx]
192 self.dl = dl
--> 193 with torch.no_grad(): self._with_events(self.all_batches, 'validate', CancelValidException)
194
195 def _do_epoch(self):
/usr/local/lib/python3.6/dist-packages/fastai/learner.py in with_events(self, f, event_type, ex, final)
158
159 def with_events(self, f, event_type, ex, final=noop):
--> 160 try: self(f'before{event_type}'); f()
161 except ex: self(f'after_cancel{event_type}')
162 self(f'after_{event_type}'); final()
/usr/local/lib/python3.6/dist-packages/fastai/learner.py in all_batches(self)
164 def all_batches(self):
165 self.n_iter = len(self.dl)
--> 166 for o in enumerate(self.dl): self.one_batch(*o)
167
168 def _do_one_batch(self):
/usr/local/lib/python3.6/dist-packages/fastai/data/load.py in iter(self)
101 for b in _loadersself.fake_l.num_workers==0:
102 if self.device is not None: b = to_device(b, self.device)
--> 103 yield self.after_batch(b)
104 self.after_iter()
105 if hasattr(self, 'it'): del(self.it)
/usr/local/lib/python3.6/dist-packages/fastcore/transform.py in call(self, o)
196 self.fs.append(t)
197
--> 198 def call(self, o): return compose_tfms(o, tfms=self.fs, split_idx=self.split_idx)
199 def repr(self): return f"Pipeline: {' -> '.join([f.name for f in self.fs if f.name != 'noop'])}"
200 def getitem(self,i): return self.fs[i]
/usr/local/lib/python3.6/dist-packages/fastcore/transform.py in compose_tfms(x, tfms, is_enc, reverse, **kwargs)
148 for f in tfms:
149 if not is_enc: f = f.decode
--> 150 x = f(x, **kwargs)
151 return x
152
/usr/local/lib/python3.6/dist-packages/fastai/vision/augment.py in call(self, b, split_idx, **kwargs)
33 def call(self, b, split_idx=None, **kwargs):
34 self.before_call(b, split_idx=split_idx)
---> 35 return super().call(b, split_idx=split_idx, **kwargs) if self.do else b
36
37 # Cell
/usr/local/lib/python3.6/dist-packages/fastcore/transform.py in call(self, x, **kwargs)
71 @property
72 def name(self): return getattr(self, '_name', _get_name(self))
---> 73 def call(self, x, **kwargs): return self._call('encodes', x, **kwargs)
74 def decode (self, x, **kwargs): return self._call('decodes', x, **kwargs)
75 def repr(self): return f'{self.name}:\nencodes: {self.encodes}decodes: {self.decodes}'
/usr/local/lib/python3.6/dist-packages/fastcore/transform.py in _call(self, fn, x, split_idx, **kwargs)
81 def _call(self, fn, x, split_idx=None, **kwargs):
82 if split_idx!=self.split_idx and self.split_idx is not None: return x
---> 83 return self._do_call(getattr(self, fn), x, **kwargs)
84
85 def _do_call(self, f, x, **kwargs):
/usr/local/lib/python3.6/dist-packages/fastcore/transform.py in do_call(self, f, x, **kwargs)
88 ret = f.returns(x) if hasattr(f,'returns') else None
89 return retain_type(f(x, **kwargs), x, ret)
---> 90 res = tuple(self.do_call(f, x, **kwargs) for x in x)
91 return retain_type(res, x)
92
/usr/local/lib/python3.6/dist-packages/fastcore/transform.py in (.0)
88 ret = f.returns(x) if hasattr(f,'returns') else None
89 return retain_type(f(x, **kwargs), x, ret)
---> 90 res = tuple(self.do_call(f, x, **kwargs) for x_ in x)
91 return retain_type(res, x)
92
/usr/local/lib/python3.6/dist-packages/fastcore/transform.py in do_call(self, f, x, **kwargs)
87 if f is None: return x
88 ret = f.returns(x) if hasattr(f,'returns') else None
---> 89 return retain_type(f(x, **kwargs), x, ret)
90 res = tuple(self.do_call(f, x, **kwargs) for x in x)
91 return retain_type(res, x)
/usr/local/lib/python3.6/dist-packages/fastcore/dispatch.py in call(self, *args, **kwargs)
116 elif self.inst is not None: f = MethodType(f, self.inst)
117 elif self.owner is not None: f = MethodType(f, self.owner)
--> 118 return f(*args, **kwargs)
119
120 def get(self, inst, owner):
/usr/local/lib/python3.6/dist-packages/fastai/vision/augment.py in encodes(self, x)
397 return x.affine_coord(self.mat, coord_func, sz=self.size, mode=mode, pad_mode=self.pad_mode, align_corners=self.align_corners)
398
--> 399 def encodes(self, x:TensorImage): return self._encode(x, self.mode)
400 def encodes(self, x:TensorMask): return self._encode(x, self.mode_mask)
401 def encodes(self, x:(TensorPoint, TensorBBox)): return self._encode(x, self.mode, reverse=True)
/usr/local/lib/python3.6/dist-packages/fastai/vision/augment.py in _encode(self, x, mode, reverse)
395 def _encode(self, x, mode, reverse=False):
396 coord_func = None if len(self.coord_fs)==0 or self.split_idx else partial(compose_tfms, tfms=self.coord_fs, reverse=reverse)
--> 397 return x.affine_coord(self.mat, coord_func, sz=self.size, mode=mode, pad_mode=self.pad_mode, align_corners=self.align_corners)
398
399 def encodes(self, x:TensorImage): return self._encode(x, self.mode)
/usr/local/lib/python3.6/dist-packages/fastai/vision/augment.py in affine_coord(x, mat, coord_tfm, sz, mode, pad_mode, align_corners)
319 coords = affine_grid(mat, x.shape[:2] + size, align_corners=align_corners)
320 if coord_tfm is not None: coords = coord_tfm(coords)
--> 321 return TensorImage(_grid_sample(x, coords, mode=mode, padding_mode=pad_mode, align_corners=align_corners))
322
323 @patch
/usr/local/lib/python3.6/dist-packages/fastai/vision/augment.py in _grid_sample(x, coords, mode, padding_mode, align_corners)
304 else:
305 x = F.interpolate(x, scale_factor=1/d, mode='area')
--> 306 return F.grid_sample(x, coords, mode=mode, padding_mode=padding_mode, align_corners=align_corners)
307
308 # Cell
/usr/local/lib/python3.6/dist-packages/torch/nn/functional.py in grid_sample(input, grid, mode, padding_mode, align_corners)
3361 return handle_torch_function(
3362 grid_sample, tens_ops, input, grid, mode=mode, padding_mode=padding_mode,
-> 3363 align_corners=align_corners)
3364 if mode != 'bilinear' and mode != 'nearest':
3365 raise ValueError("nn.functional.grid_sample(): expected mode to be "
/usr/local/lib/python3.6/dist-packages/torch/overrides.py in handle_torch_function(public_api, relevant_args, *args, **kwargs)
1061 # Use public_api
instead of implementation
so torch_function
1062 # implementations can do equality/identity comparisons.
-> 1063 result = overloaded_arg.torch_function(public_api, types, args, kwargs)
1064
1065 if result is not NotImplemented:
/usr/local/lib/python3.6/dist-packages/fastai/torch_core.py in torch_function(self, func, types, args, kwargs)
323 convert=False
324 if _torch_handled(args, self._opt, func): convert,types = type(self),(torch.Tensor,)
--> 325 res = super().torch_function(func, types, args=args, kwargs=kwargs)
326 if convert: res = convert(res)
327 if isinstance(res, TensorBase): res.set_meta(self, as_copy=True)
/usr/local/lib/python3.6/dist-packages/torch/tensor.py in torch_function(cls, func, types, args, kwargs)
993
994 with _C.DisableTorchFunction():
--> 995 ret = func(*args, **kwargs)
996 return _convert(ret, cls)
997
/usr/local/lib/python3.6/dist-packages/torch/nn/functional.py in grid_sample(input, grid, mode, padding_mode, align_corners)
3389 align_corners = False
3390
-> 3391 return torch.grid_sampler(input, grid, mode_enum, padding_mode_enum, align_corners)
3392
3393
RuntimeError: grid_sampler(): expected input and grid to have same dtype, but input has float and grid has c10::Half