Skip to content
This repository has been archived by the owner on Oct 9, 2023. It is now read-only.

Commit

Permalink
update notebooks
Browse files Browse the repository at this point in the history
  • Loading branch information
tchaton authored and Borda committed Jan 28, 2021
1 parent 7b9102d commit bb558b2
Show file tree
Hide file tree
Showing 10 changed files with 1,134 additions and 0 deletions.
167 changes: 167 additions & 0 deletions flash_notebooks/finetuning/finetuning_image_classification.ipynb
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
}
Loading

0 comments on commit bb558b2

Please sign in to comment.