{"cells": [{"cell_type": "code", "execution_count": 1, "id": "732f09f4", "metadata": {}, "outputs": [], "source": ["# install fastkaggle if not available\n", "try: import fastkaggle\n", "except ModuleNotFoundError:\n", "    !pip install -Uq fastkaggle\n", "\n", "from fastkaggle import *"]}, {"cell_type": "markdown", "id": "f0f21256", "metadata": {}, "source": ["## Getting set up"]}, {"cell_type": "code", "execution_count": 2, "id": "70f2d75c", "metadata": {}, "outputs": [], "source": ["comp = 'clip-counting-competition-s23'\n", "path = setup_comp(comp, install='')"]}, {"cell_type": "code", "execution_count": 3, "id": "879f9000", "metadata": {}, "outputs": [], "source": ["path, path.ls()"]}, {"cell_type": "code", "execution_count": 4, "id": "d8d02d72-b6a2-4960-a9a1-188551be3557", "metadata": {}, "outputs": [], "source": ["import pandas as pd\n", "df=pd.read_csv(path/'train.csv')"]}, {"cell_type": "code", "execution_count": 5, "id": "d54ff571-5111-4f95-9d2a-23c90e3da358", "metadata": {}, "outputs": [], "source": ["df['id_tfm'] = df['id'].map(lambda o: 'clips-'+str(o)+'.png')"]}, {"cell_type": "code", "execution_count": 6, "id": "9a7d84c9-edd3-42ae-88ef-2e1364785f54", "metadata": {}, "outputs": [], "source": ["df"]}, {"cell_type": "markdown", "id": "b31685ce-fcf8-4fdd-8ff0-64b46fd380d2", "metadata": {}, "source": ["## Prepare data "]}, {"cell_type": "code", "execution_count": 7, "id": "7106d4ed-060a-4ca7-a6b6-eff64c3c2427", "metadata": {}, "outputs": [], "source": ["from fastai.vision.all import *"]}, {"cell_type": "code", "execution_count": 8, "id": "f105bb21-8803-4e1d-bd86-9fd42d1163da", "metadata": {}, "outputs": [], "source": ["trn_path = path/'train/train'\n", "files = get_image_files(trn_path).sorted()"]}, {"cell_type": "code", "execution_count": 9, "id": "37c410c0-9f4e-4d33-98fb-d260aec4ab16", "metadata": {}, "outputs": [], "source": ["files"]}, {"cell_type": "code", "execution_count": 10, "id": "d363b0ed-428a-435f-b45c-cb8f1a7446d8", "metadata": {}, "outputs": [], "source": ["img = PILImage.create(files[0])\n", "print(img.size)\n", "img.to_thumb(128)"]}, {"cell_type": "code", "execution_count": 11, "id": "84c748e1-2fad-4d09-9071-6b43668209e6", "metadata": {}, "outputs": [], "source": ["import os\n", "os.cpu_count()"]}, {"cell_type": "code", "execution_count": 12, "id": "7c5ac5d9-41b6-44cc-9a04-6aeb4a16cf50", "metadata": {}, "outputs": [], "source": ["# from fastcore.parallel import *\n", "\n", "# def f(o): return PILImage.create(o).size\n", "# sizes = parallel(f, files, n_workers=6)\n", "# pd.Series(sizes).value_counts()"]}, {"cell_type": "code", "execution_count": 13, "id": "c35507f3-6c8b-4b36-ba0a-a314093ac4fe", "metadata": {}, "outputs": [], "source": ["dls = DataBlock(\n", "    blocks=(ImageBlock, RegressionBlock()), \n", "    splitter=RandomSplitter(valid_pct=0.2),\n", "    get_x=ColReader(2, pref=trn_path),\n", "    get_y=ColReader(1)\n", ").dataloaders(df)\n", "\n", "dls.show_batch(max_n=6)"]}, {"cell_type": "code", "execution_count": 14, "id": "24ebc4a5-7c20-4dfb-b58d-bdecc112704a", "metadata": {}, "outputs": [], "source": ["learn = vision_learner(dls, 'resnet18', metrics=error_rate, path='.').to_fp16()"]}, {"cell_type": "code", "execution_count": null, "id": "eb603ff6-a9b2-4bd4-bb6e-6455f412bdbc", "metadata": {}, "outputs": [], "source": ["learn.lr_find(suggest_funcs=(valley, slide))"]}, {"cell_type": "code", "execution_count": null, "id": "46bbf92b-1015-42ab-b50d-ee0ad28fd037", "metadata": {}, "outputs": [], "source": []}, {"cell_type": "code", "execution_count": null, "id": "a3f457b3-35fd-49ec-ab54-723eceb400f8", "metadata": {}, "outputs": [], "source": []}, {"cell_type": "markdown", "id": "2ab26601", "metadata": {}, "source": ["## Submitting to Kaggle"]}, {"cell_type": "code", "execution_count": 1, "id": "ed52342c", "metadata": {}, "outputs": [], "source": ["# if not iskaggle:\n", "#     from kaggle import api\n", "#     api.competition_submit_cli('subm.csv', 'initial log tf HistGBR', comp)"]}, {"cell_type": "markdown", "id": "e04efcc0", "metadata": {}, "source": ["## Conclusion"]}, {"cell_type": "markdown", "id": "a972c10e", "metadata": {}, "source": ["## Addendum"]}, {"cell_type": "code", "execution_count": 15, "id": "bd2148d7", "metadata": {}, "outputs": [], "source": ["if not iskaggle:\n", "    push_notebook('xy', 'count-clip-regression',\n", "                  title='Counting numbers of clips as a regression problem',\n", "                  file='01-first-attempt-fastai.ipynb',\n", "                  competition=comp, private=False, gpu=True)"]}, {"cell_type": "code", "execution_count": null, "id": "29e901eb-2691-4216-a8f5-014ad9ea96f0", "metadata": {}, "outputs": [], "source": []}], "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.12"}}, "nbformat": 4, "nbformat_minor": 5}