{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from fastai import *\n", "from fastbook import *" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!kaggle datasets download -d gpiosenka/musical-instruments-image-classification" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!ls -l" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!unzip -d images musical-instruments-image-classification.zip" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "path = Path('images')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "path.absolute()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from fastai.vision.all import *\n", "from fastai.vision.widgets import *" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "csv_path = Path('instruments.csv')\n", "csv_path.absolute()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dls = ImageDataLoaders.from_csv(path=path,csv_fname='instruments.csv')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "learner = vision_learner(dls=dls,arch=resnet18,metrics=error_rate)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "learner.fine_tune(2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "interp = ClassificationInterpretation.from_learner(learner)\n", "interp.plot_confusion_matrix()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "interp.plot_top_losses(10,nrows=5, figsize=(15,10))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cleaner = ImageClassifierCleaner(learner)\n", "cleaner" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for idx in cleaner.delete(): cleaner.fns[idx].unlink()\n", "for idx,cat in cleaner.change(): shutil.move(str(cleaner.fns[idx]),path/changed)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "uploader=SimpleNamespace(data=['images/6 test samples/1.jpg'])\n", "img = (PILImage.create(uploader.data[0])).to_thumb(224)\n", "img" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "learner.predict(img)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "learner.export('model.pkl')" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "total 46032\n", "-rwxrwxrwx 1 tux tux 243 Jun 22 21:11 README.md\n", "-rwxrwxrwx 1 tux tux 3162 Jun 29 16:56 app.ipynb\n", "-rwxrwxrwx 1 tux tux 970 Jun 22 21:45 app.py\n", "-rwxrwxrwx 1 tux tux 37139 Jun 22 21:35 banjo.jpg\n", "-rwxrwxrwx 1 tux tux 47081297 Jun 22 21:08 model.pkl\n", "-rwxrwxrwx 1 tux tux 6 Jun 22 23:35 requirements.txt\n", "-rwxrwxrwx 1 tux tux 3774 Jun 29 16:56 train.ipynb\n" ] } ], "source": [ "!ls -l" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.10.10" } }, "nbformat": 4, "nbformat_minor": 0 }