{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import fastbook\n", "fastbook.setup_book()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "from fastbook import *\n", "from fastai.vision.all import *\n", "import os" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pattern_get_class = re.compile(r'PetImages/(\\w+)/\\d+.jpg')\n", "path = '../kagglecatsanddogs_5340/'\n", "fnames = get_image_files(path)\n", "\n", "dls = ImageDataLoaders.from_path_re(\n", " path, fnames, pattern_get_class, valid_pct=0.2, seed=42, item_tfms=Resize(224))\n", "dls.show_batch()" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "learn = vision_learner(dls, resnet34, metrics=error_rate)\n", "learn.fine_tune(1)\n", "learn.export('model.pkl')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "path = 'cat.jpg'\n", "img = PILImage.create(path)\n", "img.to_thumb(192)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "is_cat,_,probs = learn.predict(img)\n", "print(f\"Is this a cat?: {is_cat}.\")\n", "print(f\"Probability it's a cat: {probs[0].item():.6f}\")" ] } ], "metadata": { "kernelspec": { "display_name": "tesseract", "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.9.15" }, "orig_nbformat": 4, "vscode": { "interpreter": { "hash": "c8334dffe72b6a881969c3515475442b0cf3f3c8c06d8151aebf952bb4134fbe" } } }, "nbformat": 4, "nbformat_minor": 2 }