TypeError: add(): argument 'other' (position 1) must be Tensor, not numpy.ndarray

Jemmy picture Jemmy · Dec 27, 2018 · Viewed 9.7k times · Source

I am testing a ResNet-34 trained_model using Pytorch and fastai on a linux system with the latest anaconda3. To run it as a batch job, I commented out the gui related lines. It started to run for a few hrs, then stopped in the Validation step, the error message is as below.

...
^M100%|█████████▉| 452/453 [1:07:07<00:08,  8.75s/it, 
loss=1.23]^[[A^[[A^[[A

^MValidation:   0%|          | 0/40 [00:00<?, ?it/s]^[[A^[[A^[[ATraceback 
(most recent call last):
  File "./resnet34_pretrained_PNG_nogui_2.py", line 279, in <module>
    learner.fit(lr,1,callbacks=[f1_callback])
  File "/project/6000192/jemmyhu/resnet_png/fastai/learner.py", line 302, 
in fit
    return self.fit_gen(self.model, self.data, layer_opt, n_cycle, 
**kwargs)
  File "/project/6000192/jemmyhu/resnet_png/fastai/learner.py", line 249, 
in fit_gen
    swa_eval_freq=swa_eval_freq, **kwargs)
  File "/project/6000192/jemmyhu/resnet_png/fastai/model.py", line 162, in 
fit
    vals = validate(model_stepper, cur_data.val_dl, metrics, epoch, 
seq_first=seq_first, validate_skip = validate_skip)
  File "/project/6000192/jemmyhu/resnet_png/fastai/model.py", line 241, in 
validate
    res.append([to_np(f(datafy(preds), datafy(y))) for f in metrics])
  File "/project/6000192/jemmyhu/resnet_png/fastai/model.py", line 241, in 
<listcomp>
    res.append([to_np(f(datafy(preds), datafy(y))) for f in metrics])
  File "./resnet34_pretrained_PNG_nogui_2.py", line 237, in __call__
    self.TP += (preds*targs).float().sum(dim=0)
TypeError: add(): argument 'other' (position 1) must be Tensor, not 
numpy.ndarray

The link for the original code is https://www.kaggle.com/iafoss/pretrained-resnet34-with-rgby-0-460-public-lb

lines 279 and 237 in my copy are shown below:

226 class F1:
227     __name__ = 'F1 macro'
228     def __init__(self,n=28):
229         self.n = n
230         self.TP = np.zeros(self.n)
231         self.FP = np.zeros(self.n)
232         self.FN = np.zeros(self.n)
233
234     def __call__(self,preds,targs,th=0.0):
235         preds = (preds > th).int()
236         targs = targs.int()
237         self.TP += (preds*targs).float().sum(dim=0)
238         self.FP += (preds > targs).float().sum(dim=0)
239         self.FN += (preds < targs).float().sum(dim=0)
240         score = (2.0*self.TP/(2.0*self.TP + self.FP + self.FN + 1e-6)).mean()
241         return score

276 lr = 0.5e-2
277 with warnings.catch_warnings():
278     warnings.simplefilter("ignore")
279     learner.fit(lr,1,callbacks=[f1_callback])

Could anyone have a clue for the issue?

Many thanks, Jemmy

Answer

Jemmy picture Jemmy · Dec 31, 2018

Ok, the error seems be for the latest pytorch-1.0.0, when degrade pytorch to pytorch-0.4.1, the code seems work (passed the error lines at this point). Still have no idea to make the code work with pytorch-1.0.0