PPPW / deep-learning-random-explore

194 stars 34 forks source link

Can we include pytorch models from pytorchcv ? #10

Closed saurabh502 closed 5 years ago

saurabh502 commented 5 years ago

Hi ,

I found this wonderful github repo containing many models: https://github.com/osmr/imgclsmob/blob/master/pytorch/README.md

Can we include all here, so that we can integrate them with Fastai ?

Thanks in advance for your help!

PPPW commented 5 years ago

Hi @saurabh502,

This repo looks great! I have added some examples here, hope it helps!

saurabh502 commented 5 years ago

Thank you @PPPW, When I tried implementing pnasnet5large example as per your suggestion, I am getting below error: RuntimeError: Given input size: (4320x10x10). Calculated output size: (4320x0x0). Output size is too small at /opt/conda/conda-bld/pytorch_1556653099582/work/aten/src/THCUNN/generic/SpatialAveragePooling.cu:47

Below is my code: def pnasnet5large(pretrained=False): return ptcv_get_model("pnasnet5large", pretrained=False).features

m = ptcv_get_model("pnasnet5large", pretrained=False) output_size = m.output[1].out_features

learn = cnn_learner(data, pnasnet5large, pretrained=False, cut=noop, split_on=lambda m: (m[0][3], m[1]), custom_head=nn.Linear(output_size, data.c), model_dir=MODEL_PATH,metrics=accuracy)

learn.fit_one_cycle(4,max_lr=slice(1e-3,1e-1))

PPPW commented 5 years ago

Hmm, thanks for checking, I used the wrong number for the custom head feature size. Just fixed it

saurabh502 commented 5 years ago

Thanks for looking into this, what has changed ? I don't see any difference here: https://github.com/PPPW/deep-learning-random-explore/blob/63a434a7a513ab3d63a4d6e8571a456ad3b21609/CNN_archs/cnn_archs_more.ipynb

PPPW commented 5 years ago

Hi @saurabh502, That link is the old commit. Check this.

saurabh502 commented 5 years ago

Hi PPPW, thank you for looking into this, unfortunately I am getting the same error, below is the updated code and error message

`m = ptcv_get_model("pnasnet5large", pretrained=False) output_size = m.output[1].in_features

def pnasnet5large(pretrained=False): return ptcv_get_model("pnasnet5large", pretrained=False).features

learn = cnn_learner(data, pnasnet5large, pretrained=False, cut=noop, split_on=lambda m: (m[0][3], m[1]), custom_head=nn.Sequential(Flatten(), nn.Linear(output_size, data.c)), model_dir=MODEL_PATH,metrics=accuracy)

learn.fit_one_cycle(4,max_lr=slice(1e-3,1e-1))`

error:

`--------------------------------------------------------------------------- RuntimeError Traceback (most recent call last)

in ----> 1 learn.fit_one_cycle(4,max_lr=slice(1e-3,1e-1)) 2 preds, _ = learn.TTA(ds_type=DatasetType.Test) /opt/conda/lib/python3.6/site-packages/fastai/train.py in fit_one_cycle(learn, cyc_len, max_lr, moms, div_factor, pct_start, final_div, wd, callbacks, tot_epochs, start_epoch) 20 callbacks.append(OneCycleScheduler(learn, max_lr, moms=moms, div_factor=div_factor, pct_start=pct_start, 21 final_div=final_div, tot_epochs=tot_epochs, start_epoch=start_epoch)) ---> 22 learn.fit(cyc_len, max_lr, wd=wd, callbacks=callbacks) 23 24 def lr_find(learn:Learner, start_lr:Floats=1e-7, end_lr:Floats=10, num_it:int=100, stop_div:bool=True, wd:float=None): /opt/conda/lib/python3.6/site-packages/fastai/basic_train.py in fit(self, epochs, lr, wd, callbacks) 198 callbacks = [cb(self) for cb in self.callback_fns + listify(defaults.extra_callback_fns)] + listify(callbacks) 199 if defaults.extra_callbacks is not None: callbacks += defaults.extra_callbacks --> 200 fit(epochs, self, metrics=self.metrics, callbacks=self.callbacks+callbacks) 201 202 def create_opt(self, lr:Floats, wd:Floats=0.)->None: /opt/conda/lib/python3.6/site-packages/fastai/basic_train.py in fit(epochs, learn, callbacks, metrics) 99 for xb,yb in progress_bar(learn.data.train_dl, parent=pbar): 100 xb, yb = cb_handler.on_batch_begin(xb, yb) --> 101 loss = loss_batch(learn.model, xb, yb, learn.loss_func, learn.opt, cb_handler) 102 if cb_handler.on_batch_end(loss): break 103 /opt/conda/lib/python3.6/site-packages/fastai/basic_train.py in loss_batch(model, xb, yb, loss_func, opt, cb_handler) 24 if not is_listy(xb): xb = [xb] 25 if not is_listy(yb): yb = [yb] ---> 26 out = model(*xb) 27 out = cb_handler.on_loss_begin(out) 28 /opt/conda/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs) 491 result = self._slow_forward(*input, **kwargs) 492 else: --> 493 result = self.forward(*input, **kwargs) 494 for hook in self._forward_hooks.values(): 495 hook_result = hook(self, input, result) /opt/conda/lib/python3.6/site-packages/torch/nn/modules/container.py in forward(self, input) 90 def forward(self, input): 91 for module in self._modules.values(): ---> 92 input = module(input) 93 return input 94 /opt/conda/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs) 491 result = self._slow_forward(*input, **kwargs) 492 else: --> 493 result = self.forward(*input, **kwargs) 494 for hook in self._forward_hooks.values(): 495 hook_result = hook(self, input, result) /opt/conda/lib/python3.6/site-packages/pytorchcv/models/common.py in forward(self, x1, x2) 867 for i, module in enumerate(self._modules.values()): 868 if (i < self.first_ordinals) or (i >= length - self.last_ordinals): --> 869 x1, x2 = self.dual_path_scheme_ordinal(module, x1, x2) 870 else: 871 x1, x2 = self.dual_path_scheme(module, x1, x2) /opt/conda/lib/python3.6/site-packages/pytorchcv/models/nasnet.py in nasnet_dual_path_scheme_ordinal(module, x, _) 80 Current processed tensor. 81 """ ---> 82 return module(x), x 83 84 /opt/conda/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs) 491 result = self._slow_forward(*input, **kwargs) 492 else: --> 493 result = self.forward(*input, **kwargs) 494 for hook in self._forward_hooks.values(): 495 hook_result = hook(self, input, result) /opt/conda/lib/python3.6/site-packages/torch/nn/modules/pooling.py in forward(self, input) 561 def forward(self, input): 562 return F.avg_pool2d(input, self.kernel_size, self.stride, --> 563 self.padding, self.ceil_mode, self.count_include_pad) 564 565 RuntimeError: Given input size: (4320x10x10). Calculated output size: (4320x0x0). Output size is too small at /opt/conda/conda-bld/pytorch_1556653099582/work/aten/src/THCUNN/generic/SpatialAveragePooling.cu:47`
PPPW commented 5 years ago

Can you run the following without error?

sz = # your image size
with torch.no_grad():
    learn.model.eval()
    print(learn.model(torch.randn(1,3,sz,sz))
saurabh502 commented 5 years ago

Hi, no when I am using size 300 , it is giving me below error:

`-------------------------------------------------------------------------- RuntimeError Traceback (most recent call last)

in 2 with torch.no_grad(): 3 learn.model.eval() ----> 4 print(learn.model(torch.randn(1,3,sz,sz))) /opt/conda/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs) 491 result = self._slow_forward(*input, **kwargs) 492 else: --> 493 result = self.forward(*input, **kwargs) 494 for hook in self._forward_hooks.values(): 495 hook_result = hook(self, input, result) /opt/conda/lib/python3.6/site-packages/torch/nn/modules/container.py in forward(self, input) 90 def forward(self, input): 91 for module in self._modules.values(): ---> 92 input = module(input) 93 return input 94 /opt/conda/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs) 491 result = self._slow_forward(*input, **kwargs) 492 else: --> 493 result = self.forward(*input, **kwargs) 494 for hook in self._forward_hooks.values(): 495 hook_result = hook(self, input, result) /opt/conda/lib/python3.6/site-packages/pytorchcv/models/common.py in forward(self, x1, x2) 867 for i, module in enumerate(self._modules.values()): 868 if (i < self.first_ordinals) or (i >= length - self.last_ordinals): --> 869 x1, x2 = self.dual_path_scheme_ordinal(module, x1, x2) 870 else: 871 x1, x2 = self.dual_path_scheme(module, x1, x2) /opt/conda/lib/python3.6/site-packages/pytorchcv/models/nasnet.py in nasnet_dual_path_scheme_ordinal(module, x, _) 80 Current processed tensor. 81 """ ---> 82 return module(x), x 83 84 /opt/conda/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs) 491 result = self._slow_forward(*input, **kwargs) 492 else: --> 493 result = self.forward(*input, **kwargs) 494 for hook in self._forward_hooks.values(): 495 hook_result = hook(self, input, result) /opt/conda/lib/python3.6/site-packages/pytorchcv/models/nasnet.py in forward(self, x) 1029 1030 def forward(self, x): -> 1031 x = self.conv(x) 1032 x = self.bn(x) 1033 return x /opt/conda/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs) 491 result = self._slow_forward(*input, **kwargs) 492 else: --> 493 result = self.forward(*input, **kwargs) 494 for hook in self._forward_hooks.values(): 495 hook_result = hook(self, input, result) /opt/conda/lib/python3.6/site-packages/torch/nn/modules/conv.py in forward(self, input) 336 _pair(0), self.dilation, self.groups) 337 return F.conv2d(input, self.weight, self.bias, self.stride, --> 338 self.padding, self.dilation, self.groups) 339 340 RuntimeError: Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor) should be the same`
PPPW commented 5 years ago

Maybe you need to resize your image to 331 (the size they use in their model)

saurabh502 commented 5 years ago

Thanks resizing image to 331 worked for me. Though I am not getting good results :)