{"cells":[{"metadata":{},"cell_type":"markdown","source":"+ https://www.kaggle.com/yangsaewon/ka-kr-sillim-pytorch-baseline-updated-7-01\n+ 위 커널에 이어서 inference 커널을 작성해보았습니다. keras도 똑같이 적용 가능합니다."},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","collapsed":true,"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":false},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom pathlib import Path","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df = pd.read_csv(\"../input/test.csv\")\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-output":true,"trusted":true},"cell_type":"code","source":"weights_path = Path('../input/../..')\nweight_list = os.listdir(weights_path)","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-output":true,"trusted":true},"cell_type":"code","source":"batch_size = 1\ntta = 3\ntest_dataset = TestDataset(...)\ntest_loader = DataLoader(...)\ntotal_num_models = len(weight_list)*tta \n\nmodel = ...\nmodel.cuda()\n\n\nall_prediction = np.zeros((len(test_dataset), num_classes))\n\nfor i, weight in enumerate(weight_list):\n    print(\"fold {} prediction starts\".format(i+1))\n    \n    for _ in range(tta):\n        print(\"tta {}\".format(_+1))\n\n        model.load_state_dict(torch.load(weights_path / weight))\n\n        model.eval()\n        \n        prediction = np.zeros((len(test_dataset), num_classes)) # num_classes=196\n        with torch.no_grad():\n            for i, images in enumerate(test_loader):\n                images = images.cuda()\n\n                preds = model(images).detach()\n                prediction[i * batch_size: (i+1) * batch_size] = preds.cpu().numpy()\n                all_prediction = all_prediction + prediction\n    \nall_prediction /= total_num_models\n","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-output":true,"trusted":true},"cell_type":"code","source":"result = np.argmax(all_prediction, axis=1)\n\nsubmission = pd.read_csv('../input/2019-3rd-ml-month-with-kakr/sample_submission.csv')\nsubmission[\"class\"] = result\nsubmission.to_csv(\"submission.csv\", index=False)\nsubmission.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"+ ** 도움 되셨으면 upvote 부탁드립니다!**"}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}