Files
fastai--fastai/nbs/98_test_model_export.ipynb
2026-07-13 13:21:43 +08:00

63 lines
1.4 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"id": "330ad5b0",
"metadata": {},
"source": [
"# test model export\n",
"\n",
"> Test the Learner.export feature"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b176eb92",
"metadata": {},
"outputs": [],
"source": [
"from tempfile import TemporaryDirectory\n",
"from fastai.vision.all import *\n",
"from fastcore.test import *"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "37601436",
"metadata": {},
"outputs": [],
"source": [
"#| cuda\n",
"def label_func(f): return f[0].isupper()\n",
"path = untar_data(URLs.PETS)\n",
"files = get_image_files(path/\"images\")\n",
"dls = ImageDataLoaders.from_name_func(path, files, label_func, item_tfms=Resize(32))\n",
"\n",
"with TemporaryDirectory() as td:\n",
" learn = vision_learner(dls, resnet18, metrics=error_rate, path=td)\n",
" learn.fine_tune(1,base_lr=0.00001)\n",
" learn.export(\"model.pkl\")\n",
" \n",
" learn2 = load_learner(Path(td) / \"model.pkl\", cpu=False)\n",
"\n",
"o1 = learn.predict(files[0])\n",
"o2 = learn2.predict(files[0])\n",
"\n",
"test_eq(o1[:2],o2[:2])\n",
"test_close(o1[-1], o2[-1])"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "python3",
"language": "python",
"name": "python3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}