This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
1,134 additions
and
0 deletions.
There are no files selected for viewing
167 changes: 167 additions & 0 deletions
167
flash_notebooks/finetuning/finetuning_image_classification.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"id": "fatal-doctor", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import flash\n", | ||
"from flash.core.data import download_data\n", | ||
"from flash.vision import ImageClassificationData, ImageClassifier" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "important-underground", | ||
"metadata": {}, | ||
"source": [ | ||
"# 1. Download the data" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "celtic-publisher", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"download_data(\"https://pl-flash-data.s3.amazonaws.com/hymenoptera_data.zip\", 'data/')" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "atmospheric-batch", | ||
"metadata": {}, | ||
"source": [ | ||
"# 2. Load the data" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "broadband-massachusetts", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"datamodule = ImageClassificationData.from_folders(\n", | ||
" train_folder=\"data/hymenoptera_data/train/\",\n", | ||
" valid_folder=\"data/hymenoptera_data/val/\",\n", | ||
" test_folder=\"data/hymenoptera_data/test/\",\n", | ||
")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "biblical-boundary", | ||
"metadata": {}, | ||
"source": [ | ||
"# 3. Build the model" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "upper-staff", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"model = ImageClassifier(num_classes=datamodule.num_classes)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "sixth-lancaster", | ||
"metadata": {}, | ||
"source": [ | ||
"# 4. Create the trainer. Run once on data" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "generous-paraguay", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"trainer = flash.Trainer(max_epochs=1)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "physical-prophet", | ||
"metadata": {}, | ||
"source": [ | ||
"# 5. Train the model" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "oriented-hudson", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"trainer.finetune(model, datamodule=datamodule, unfreeze_milestones=(0, 1))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "sexual-puzzle", | ||
"metadata": {}, | ||
"source": [ | ||
"# 6. Test the model" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "eight-russell", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"trainer.test()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "ultimate-halloween", | ||
"metadata": {}, | ||
"source": [ | ||
"# 7. Save it!" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "literary-establishment", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"trainer.save_checkpoint(\"image_classification_model.pt\")" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.6.9" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
Oops, something went wrong.