mirror of
https://github.com/wassname/pytorch-lightning.git
synced 2026-09-12 12:40:20 +08:00
Docs5 (#1033)
* changed path * changed path * changed path * changed path * changed path * changed path * changed path * changed path * changed path * changed path * changed path * added cv * added cv * added cv * added cv * added cv * added cv * added cv * added cv * added cv * added cv * added cv * added cv * added cv * added cv * added cv * added cv * added cv
This commit is contained in:
@@ -7,6 +7,8 @@ Using Pretrained Models
|
||||
Sometimes we want to use a LightningModule as a pretrained model. This is fine because
|
||||
a LightningModule is just a `torch.nn.Module`!
|
||||
|
||||
.. note:: Remember that a pl.LightningModule is EXACTLY a torch.nn.Module but with more capabilities.
|
||||
|
||||
Let's use the `AutoEncoder` as a feature extractor in a separate model.
|
||||
|
||||
|
||||
@@ -27,7 +29,7 @@ Let's use the `AutoEncoder` as a feature extractor in a separate model.
|
||||
self.feature_extractor.freeze()
|
||||
|
||||
# the autoencoder outputs a 100-dim representation and CIFAR-10 has 10 classes
|
||||
self.classifier = nn.Liner(100, 10)
|
||||
self.classifier = nn.Linear(100, 10)
|
||||
|
||||
def forward(self, x):
|
||||
representations = self.feature_extractor(x)
|
||||
@@ -36,11 +38,58 @@ Let's use the `AutoEncoder` as a feature extractor in a separate model.
|
||||
|
||||
We used our pretrained Autoencoder (a LightningModule) for transfer learning!
|
||||
|
||||
Example: BERT (transformers)
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
Lightning is completely agnostic to what's used for tranfer learning so long
|
||||
Example: Imagenet (computer Vision)
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
import torchvision.models as models
|
||||
|
||||
class ImagenetTranferLearning(pl.LightingModule):
|
||||
def __init__(self):
|
||||
# init a pretrained resnet
|
||||
num_target_classes = 10
|
||||
self.feature_extractor = model.resnet50(
|
||||
pretrained=True,
|
||||
num_classes=num_target_classes)
|
||||
self.feature_extractor.eval()
|
||||
|
||||
# use the pretrained model to classify cifar-10 (10 image classes)
|
||||
self.classifier = nn.Linear(2048, num_target_classes)
|
||||
|
||||
def forward(self, x):
|
||||
representations = self.feature_extractor(x)
|
||||
x = self.classifier(representations)
|
||||
...
|
||||
|
||||
Finetune
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
model = ImagenetTranferLearning()
|
||||
trainer = Trainer()
|
||||
trainer.fit(model)
|
||||
|
||||
And use it to predict your data of interest
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
model = ImagenetTranferLearning.load_from_checkpoint(PATH)
|
||||
model.freeze()
|
||||
|
||||
x = some_images_from_cifar10()
|
||||
predictions = model(x)
|
||||
|
||||
We used a pretrained model on imagenet, finetuned on CIFAR-10 to predict on CIFAR-10.
|
||||
In the non-academic world we would finetune on a tiny dataset you have and predict on your dataset.
|
||||
|
||||
Example: BERT (NLP)
|
||||
^^^^^^^^^^^^^^^^^^^
|
||||
Lightning is completely agnostic to what's used for transfer learning so long
|
||||
as it is a `torch.nn.Module` subclass.
|
||||
|
||||
Here's a model that uses `Huggingface transformers <https://github.com/huggingface/transformers>`_.
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
from transformers import BertModel
|
||||
|
||||
Reference in New Issue
Block a user