class InceptionV3Class(nn.Module):
def __init__(
self,
num_classes = 5,
):
super().__init__()
self.model = torch.hub.load('pytorch/vision:v0.6.0', 'inception_v3', pretrained=True)
# model = models.inception_v3(pretrained=use_pretrained)
set_parameter_requires_grad(self.model, feature_extracting = True)
# Handle the auxilary net
num_ftrs = self.model.AuxLogits.fc.in_features
self.model.AuxLogits.fc = nn.Linear(num_ftrs, num_classes)
# Handle the primary net
num_ftrs = self.model.fc.in_features
self.model.fc = nn.Linear(num_ftrs, num_classes)
model = InceptionV3Model(
model_name=model_name,
num_classes=num_classes,
data_path=path,
lr=lr,
loss_fn=loss_fn,
)
trainer.fit(model=model, datamodule=data_module)
| Name | Type | Params
----------------------------------------------
0 | model | InceptionV3Class | 24.4 M
1 | accuracy | Accuracy | 0
----------------------------------------------
14.1 K Trainable params
24.3 M Non-trainable params
24.4 M Total params
Validation sanity check: 0it [00:00, ?it/s]
---------------------------------------------------------------------------
NotImplementedError Traceback (most recent call last)
<ipython-input-16-2cd966cc686d> in <module>
----> 1 trainer.fit(model=model, datamodule=data_module)
~/anaconda3/envs/torch/lib/python3.8/site-packages/pytorch_lightning/trainer/trainer.py in fit(self, model, train_dataloader, val_dataloaders, datamodule)
470 self.call_hook('on_fit_start')
471
--> 472 results = self.accelerator_backend.train()
473 self.accelerator_backend.teardown()
474
~/anaconda3/envs/torch/lib/python3.8/site-packages/pytorch_lightning/accelerators/gpu_accelerator.py in train(self)
66
67 # train or test
---> 68 results = self.train_or_test()
69 return results
70
~/anaconda3/envs/torch/lib/python3.8/site-packages/pytorch_lightning/accelerators/accelerator.py in train_or_test(self)
67 results = self.trainer.run_test()
68 else:
---> 69 results = self.trainer.train()
70 return results
71
~/anaconda3/envs/torch/lib/python3.8/site-packages/pytorch_lightning/trainer/trainer.py in train(self)
492
493 def train(self):
--> 494 self.run_sanity_check(self.get_model())
495
496 # set stage for logging
~/anaconda3/envs/torch/lib/python3.8/site-packages/pytorch_lightning/trainer/trainer.py in run_sanity_check(self, ref_model)
690
691 # run eval step
--> 692 _, eval_results = self.run_evaluation(test_mode=False, max_batches=self.num_sanity_val_batches)
693
694 # allow no returns from eval
~/anaconda3/envs/torch/lib/python3.8/site-packages/pytorch_lightning/trainer/trainer.py in run_evaluation(self, test_mode, max_batches)
606 # lightning module methods
607 with self.profiler.profile("evaluation_step_and_end"):
--> 608 output = self.evaluation_loop.evaluation_step(test_mode, batch, batch_idx, dataloader_idx)
609 output = self.evaluation_loop.evaluation_step_end(output)
610
~/anaconda3/envs/torch/lib/python3.8/site-packages/pytorch_lightning/trainer/evaluation_loop.py in evaluation_step(self, test_mode, batch, batch_idx, dataloader_idx)
176 else:
177 model_ref._current_fx_name = "validation_step"
--> 178 output = self.trainer.accelerator_backend.validation_step(args)
179
180 # capture any logged information
~/anaconda3/envs/torch/lib/python3.8/site-packages/pytorch_lightning/accelerators/gpu_accelerator.py in validation_step(self, args)
84
85 def validation_step(self, args):
---> 86 return self._step(self.trainer.model.validation_step, args)
87
88 def test_step(self, args):
~/anaconda3/envs/torch/lib/python3.8/site-packages/pytorch_lightning/accelerators/gpu_accelerator.py in _step(self, model_step, args)
74 if self.trainer.amp_backend == AMPType.NATIVE:
75 with torch.cuda.amp.autocast():
---> 76 output = model_step(*args)
77 else:
78 output = model_step(*args)
<ipython-input-10-2987c5363b3a> in validation_step(self, batch, batch_idx)
30 def validation_step(self, batch, batch_idx):
31 x, y = batch
---> 32 y_hat = self(x)
33 loss = self.loss_fn(y_hat, y)
34 self.log("valid_loss", loss, prog_bar=True)
~/anaconda3/envs/torch/lib/python3.8/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
725 result = self._slow_forward(*input, **kwargs)
726 else:
--> 727 result = self.forward(*input, **kwargs)
728 for hook in itertools.chain(
729 _global_forward_hooks.values(),
<ipython-input-10-2987c5363b3a> in forward(self, x)
19
20 def forward(self, x):
---> 21 return self.model(x)
22
23 def training_step(self, batch, batch_idx):
~/anaconda3/envs/torch/lib/python3.8/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
725 result = self._slow_forward(*input, **kwargs)
726 else:
--> 727 result = self.forward(*input, **kwargs)
728 for hook in itertools.chain(
729 _global_forward_hooks.values(),
~/anaconda3/envs/torch/lib/python3.8/site-packages/torch/nn/modules/module.py in _forward_unimplemented(self, *input)
173 registered hooks while the latter silently ignores them.
174 """
--> 175 raise NotImplementedError
176
177
NotImplementedError:
you need to wrap it up around LightningModule
. https://pytorch-lightning.readthedocs.io/en/latest/lightning_module.html
thanks, but I have this
class InceptionV3Model(pl.LightningModule):
def __init__(
self,
model_name: str = None,
num_classes: int = None,
data_path: Path = None,
loss_fn=F.cross_entropy,
lr=1e-4,
wd=1e-6,
):
super().__init__()
self.model = InceptionV3Class(num_classes=num_classes)
self.data_path = data_path
self.loss_fn = loss_fn
self.lr = lr
self.accuracy = pl.metrics.Accuracy()
self.wd = wd
def forward(self, x):
return self.model(x)
def training_step(self, batch, batch_idx):
x, y = batch
y_hat = self(x)
loss = self.loss_fn(y_hat, y)
self.log("train_loss", loss, prog_bar=True)
return loss
def validation_step(self, batch, batch_idx):
x, y = batch
y_hat = self(x)
loss = self.loss_fn(y_hat, y)
self.log("valid_loss", loss, prog_bar=True)
self.log("val_acc", self.accuracy(y_hat, y), prog_bar=True)
def configure_optimizers(self):
optimizer = optim.AdamW(
self.model.parameters(), lr=self.lr, weight_decay=self.wd
)
scheduler = optim.lr_scheduler.CosineAnnealingLR(
optimizer, self.trainer.max_epochs, 0
)
return [optimizer], [scheduler]
from here I call the inception class
you need to implement a forward method for this class too since PyTorch needs to know the definition of the forward pass if you are using self.model(x)
. Right now it just has the layers but no info about how they should be used.
Hi there thanks for the response, can you help me with the forward method?
Thanks!