{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Example of single image prediction" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%reload_ext autoreload\n", "%autoreload 2\n", "%matplotlib inline" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#export\n", "from fastai import *\n", "from fastai.vision import *\n", "import fastai" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Train a model" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "tfms = get_transforms()\n", "bs = 64\n", "path = untar_data(URLs.PETS)/'images'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "data = (ImageFileList.from_folder(path)\n", " .label_from_re(r'^(.*)_\\d+.jpg$')\n", " .random_split_by_pct(0.2)\n", " .datasets(ImageClassificationDataset)\n", " .transform(tfms, size=224)\n", " .databunch(bs=bs)\n", " .normalize(imagenet_stats))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "img = open_image(get_image_files(path)[0])\n", "img.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "data.show_batch(3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "learn = create_cnn(data, models.resnet34, metrics=accuracy)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Run this once to create your model\n", "# learn.fit_one_cycle(1)\n", "# learn.save('one-epoch')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "learn.load('one-epoch')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pred_class,pred_idx,outputs = learn.predict(img)\n", "pred_class" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Single image prediction" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fastai.defaults.device = torch.device('cpu')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# You wouldn't use data.classes really\n", "# - instead load your classes list from somewhere else\n", "data2 = ImageDataBunch.single_from_classes(\n", " path, data.classes, tfms=tfms, size=224).normalize(imagenet_stats)\n", "learn = create_cnn(data2, models.resnet34)\n", "learn.load('one-epoch')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pred_class,pred_idx,outputs = learn.predict(img)\n", "pred_class" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 2 }