{ "cells": [ { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import timm\n", "from fastai.vision.all import *\n", "import gradio as gr\n", "import os\n", "\n", "\n", "import platform\n", "if platform.system() == 'Windows':\n", " import pathlib\n", " temp = pathlib.PosixPath\n", " pathlib.PosixPath = pathlib.WindowsPath" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "themes = sorted(('City', 'Technic', 'Star-Wars', 'Creator', 'Ninjago', 'Architecture', 'Duplo', 'Friends', 'DC-Comics-Super-Heroes'))\n", "learn_color = load_learner('models/lego_convnext_small_4ep_sets05-19.pkl')\n", "learn_gray = load_learner('models/lego_convnext_small_4ep_grayscale.pkl')\n", "\n", "def classify(img, *args):\n", " if args[-1] == 'Color mode':\n", " _, _, probs = learn_color.predict(img)\n", " else:\n", " _, _, probs = learn_gray.predict(img)\n", " return dict(zip(themes, map(float, probs)))\n", "\n", "\n", "img = gr.components.Image(shape=(192, 192), label=\"Input image\")\n", "is_color = gr.components.Radio(['Color mode', 'Grayscale mode'], value='Color mode', show_label=False)\n", "real_label = gr.components.Textbox(\"\", label='Theme', interactive=False)\n", "year = gr.components.Textbox(\"\", label='Release year', visible=False)\n", "\n", "label = gr.components.Label(label='Predictions')\n", "examples = [[f'test_images/{img_name}', img_name.split('2', 1)[0].capitalize(), img_name.split('.', 1)[0][-4:]] for img_name in os.listdir('test_images')]\n", "\n", "# gr.Interface(fn=classify, inputs=[img, real_label, year, is_color], outputs=label, examples=examples).launch(\n", "# # inline=False\n", "# )" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "title = 'LEGO sets&creations theme classifier'\n", "description = f'''\n", "# {title}\n", "This demo showcases the LEGO theme classifier built with the help of fast.ai. A model was trained using over 1800 images of sets released in 2005-19 scraped from the Brickset LEGO database.\n", "To test how much overfitting might be present due to the model memorizing the color(s) associated with a particular theme, I ran the training again using the same set of images, but in grayscale. Hence two available models.\n", "\n", "I was especially intrested in how the model will do on MOCS a.k.a. community creations, since the boundries between themes are not well-defined. Enjoy!\n", "'''" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "c:\\Users\\ewafa\\anaconda3\\envs\\ml\\lib\\site-packages\\gradio\\deprecation.py:43: UserWarning: You have unused kwarg parameters in Row, please remove them: {'equal_height': True}\n", " warnings.warn(\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Running on local URL: http://127.0.0.1:7860\n", "\n", "To create a public link, set `share=True` in `launch()`.\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "