{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":21154,"databundleVersionId":1243559,"sourceType":"competition"},{"sourceId":948457,"sourceType":"datasetVersion","datasetId":514569},{"sourceId":13484849,"sourceType":"datasetVersion","datasetId":8561311}],"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings(\"ignore\", category=UserWarning, module=\"pydantic\")\nimport os\nimport fastai\nfrom fastcore.all import *\nfrom fastai.vision.all import *","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-24T06:59:41.539835Z","iopub.execute_input":"2025-10-24T06:59:41.540701Z","iopub.status.idle":"2025-10-24T06:59:41.547619Z","shell.execute_reply.started":"2025-10-24T06:59:41.540672Z","shell.execute_reply":"2025-10-24T06:59:41.546660Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"im = Image.open('/kaggle/input/104-flowers-garden-of-eden/jpeg-311x311/train/alpine sea holly/10512.jpeg')\nim.to_thumb(500,500)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-24T06:59:41.549165Z","iopub.execute_input":"2025-10-24T06:59:41.549440Z","iopub.status.idle":"2025-10-24T06:59:41.590496Z","shell.execute_reply.started":"2025-10-24T06:59:41.549415Z","shell.execute_reply":"2025-10-24T06:59:41.589835Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"LABELS = [\n    \"pink primrose\",\n    \"hard-leaved pocket orchid\",\n    \"canterbury bells\",\n    \"sweet pea\",\n    \"wild geranium\",\n    \"tiger lily\",\n    \"moon orchid\",\n    \"bird of paradise\",\n    \"monkshood\",\n    \"globe thistle\",  # 00 - 09\n    \"snapdragon\",\n    \"colt's foot\",\n    \"king protea\",\n    \"spear thistle\",\n    \"yellow iris\",\n    \"globe-flower\",\n    \"purple coneflower\",\n    \"peruvian lily\",\n    \"balloon flower\",\n    \"giant white arum lily\",  # 10 - 19\n    \"fire lily\",\n    \"pincushion flower\",\n    \"fritillary\",\n    \"red ginger\",\n    \"grape hyacinth\",\n    \"corn poppy\",\n    \"prince of wales feathers\",\n    \"stemless gentian\",\n    \"artichoke\",\n    \"sweet william\",  # 20 - 29\n    \"carnation\",\n    \"garden phlox\",\n    \"love in the mist\",\n    \"cosmos\",\n    \"alpine sea holly\",\n    \"ruby-lipped cattleya\",\n    \"cape flower\",\n    \"great masterwort\",\n    \"siam tulip\",\n    \"lenten rose\",  # 30 - 39\n    \"barberton daisy\",\n    \"daffodil\",\n    \"sword lily\",\n    \"poinsettia\",\n    \"bolero deep blue\",\n    \"wallflower\",\n    \"marigold\",\n    \"buttercup\",\n    \"daisy\",\n    \"common dandelion\",  # 40 - 49\n    \"petunia\",\n    \"wild pansy\",\n    \"primula\",\n    \"sunflower\",\n    \"lilac hibiscus\",\n    \"bishop of llandaff\",\n    \"gaura\",\n    \"geranium\",\n    \"orange dahlia\",\n    \"pink-yellow dahlia\",  # 50 - 59\n    \"cautleya spicata\",\n    \"japanese anemone\",\n    \"black-eyed susan\",\n    \"silverbush\",\n    \"californian poppy\",\n    \"osteospermum\",\n    \"spring crocus\",\n    \"iris\",\n    \"windflower\",\n    \"tree poppy\",  # 60 - 69\n    \"gazania\",\n    \"azalea\",\n    \"water lily\",\n    \"rose\",\n    \"thorn apple\",\n    \"morning glory\",\n    \"passion flower\",\n    \"lotus\",\n    \"toad lily\",\n    \"anthurium\",  # 70 - 79\n    \"frangipani\",\n    \"clematis\",\n    \"hibiscus\",\n    \"columbine\",\n    \"desert-rose\",\n    \"tree mallow\",\n    \"magnolia\",\n    \"cyclamen \",\n    \"watercress\",\n    \"canna lily\",  # 80 - 89\n    \"hippeastrum \",\n    \"bee balm\",\n    \"pink quill\",\n    \"foxglove\",\n    \"bougainvillea\",\n    \"camellia\",\n    \"mallow\",\n    \"mexican petunia\",\n    \"bromelia\",\n    \"blanket flower\",  # 90 - 99\n    \"trumpet creeper\",\n    \"blackberry lily\",\n    \"common tulip\",\n    \"wild rose\",\n]  # 100 - 102","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-24T06:59:41.591037Z","iopub.execute_input":"2025-10-24T06:59:41.591319Z","iopub.status.idle":"2025-10-24T06:59:41.600795Z","shell.execute_reply.started":"2025-10-24T06:59:41.591299Z","shell.execute_reply":"2025-10-24T06:59:41.600027Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 이미지가 저장된 상위 경로\npath = Path('/kaggle/input/104-flowers-garden-of-eden/jpeg-311x311/train')\n\n# 각 카테고리별 이미지 개수 확인\nfor category in LABELS:\n    category_path = path/category\n    # jpg, png 등 일반적인 이미지 확장자 포함\n    num_images = len(list(category_path.glob('*.jpeg'))) + len(list(category_path.glob('*.png')))\n    print(f\"{category}: {num_images} images\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-24T06:59:41.602553Z","iopub.execute_input":"2025-10-24T06:59:41.602811Z","iopub.status.idle":"2025-10-24T06:59:41.848065Z","shell.execute_reply.started":"2025-10-24T06:59:41.602791Z","shell.execute_reply":"2025-10-24T06:59:41.847109Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"path = Path('/kaggle/input/104-flowers-garden-of-eden/jpeg-311x311')\n\ndls = DataBlock(\n    blocks=(ImageBlock, CategoryBlock),\n    get_items=get_image_files,\n    get_y=parent_label,\n    splitter=GrandparentSplitter(train_name='train', valid_name='val'),\n    item_tfms=[Resize(311, method='squish')]\n).dataloaders(path, bs=32)\n\ndls.show_batch(max_n=6)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-24T06:59:41.848942Z","iopub.execute_input":"2025-10-24T06:59:41.849417Z","iopub.status.idle":"2025-10-24T06:59:52.562289Z","shell.execute_reply.started":"2025-10-24T06:59:41.849390Z","shell.execute_reply":"2025-10-24T06:59:52.561451Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"learn = vision_learner(dls, resnet18, metrics=error_rate)\nlearn.fine_tune(3)\nlearn.recorder.plot_loss()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-24T06:59:52.562940Z","iopub.execute_input":"2025-10-24T06:59:52.563252Z","iopub.status.idle":"2025-10-24T07:03:25.576753Z","shell.execute_reply.started":"2025-10-24T06:59:52.563230Z","shell.execute_reply":"2025-10-24T07:03:25.575841Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from pathlib import Path\n\ntest_path = Path(\"/kaggle/input/104-flowers-garden-of-eden/jpeg-311x311/test\")  # test 이미지가 있는 폴더\ntest_files = get_image_files(test_path)\nprint(f\"Number of test images: {len(test_files)}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-24T07:03:25.578086Z","iopub.execute_input":"2025-10-24T07:03:25.579091Z","iopub.status.idle":"2025-10-24T07:03:27.984067Z","shell.execute_reply.started":"2025-10-24T07:03:25.579054Z","shell.execute_reply":"2025-10-24T07:03:27.983148Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_dl = learn.dls.test_dl(test_files)\npreds, _ = learn.get_preds(dl=test_dl)  # preds = 확률\npred_labels = preds.argmax(dim=1)\npred_classes = [learn.dls.vocab[i] for i in pred_labels]\n\nfor f, c in zip(test_files[:5], pred_labels[:5]):\n    print(f\"{f.name} -> {c}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-24T07:03:27.985183Z","iopub.execute_input":"2025-10-24T07:03:27.985562Z","iopub.status.idle":"2025-10-24T07:03:47.468168Z","shell.execute_reply.started":"2025-10-24T07:03:27.985537Z","shell.execute_reply":"2025-10-24T07:03:47.466984Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 이미지 이름만\nimage_ids = [f.name for f in test_files]\n\n# 예측 클래스\nlabels = pred_labels ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-24T07:03:47.469366Z","iopub.execute_input":"2025-10-24T07:03:47.470392Z","iopub.status.idle":"2025-10-24T07:03:47.480730Z","shell.execute_reply.started":"2025-10-24T07:03:47.470350Z","shell.execute_reply":"2025-10-24T07:03:47.479730Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission_df = pd.DataFrame({\n    \"id\": image_ids,\n    \"label\": labels\n})\n\nsubmission_df.head()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-24T07:03:47.483864Z","iopub.execute_input":"2025-10-24T07:03:47.484863Z","iopub.status.idle":"2025-10-24T07:03:47.513443Z","shell.execute_reply.started":"2025-10-24T07:03:47.484835Z","shell.execute_reply":"2025-10-24T07:03:47.511776Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission_path = \"submission.csv\"\nsubmission_df.to_csv(submission_path, index=False)\nprint(f\"Saved submission file to {submission_path}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-24T07:03:47.515312Z","iopub.execute_input":"2025-10-24T07:03:47.515632Z","iopub.status.idle":"2025-10-24T07:03:47.546361Z","shell.execute_reply.started":"2025-10-24T07:03:47.515602Z","shell.execute_reply":"2025-10-24T07:03:47.545544Z"}},"outputs":[],"execution_count":null}]}