{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import fastai\nimport torch\nfrom fastai.vision.all import *\nfrom tqdm import tqdm\nfrom torch.utils.data import dataloader","metadata":{"execution":{"iopub.status.busy":"2022-07-07T20:17:08.320659Z","iopub.execute_input":"2022-07-07T20:17:08.322272Z","iopub.status.idle":"2022-07-07T20:17:08.330214Z","shell.execute_reply.started":"2022-07-07T20:17:08.322212Z","shell.execute_reply":"2022-07-07T20:17:08.328983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = Path('../input/pnevmoniya/train')\nfls = get_image_files(path)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T20:17:12.289630Z","iopub.execute_input":"2022-07-07T20:17:12.290224Z","iopub.status.idle":"2022-07-07T20:17:14.748422Z","shell.execute_reply.started":"2022-07-07T20:17:12.290171Z","shell.execute_reply":"2022-07-07T20:17:14.747352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Datablock\npnevmoniya = DataBlock(\n    blocks=(ImageBlock,CategoryBlock),\n    get_items = get_image_files,\n    splitter = RandomSplitter(valid_pct=0.1, seed=7),\n    get_y = parent_label,\n    item_tfms = Resize(128)\n)\n\n# Data loaders\ndls = pnevmoniya.dataloaders(path)\n\n# Train\nlearn = cnn_learner(dls, resnet18, metrics=accuracy)\nlearn.fine_tune(7) # 7 martta validation qiladi.","metadata":{"execution":{"iopub.status.busy":"2022-07-07T21:22:04.866373Z","iopub.execute_input":"2022-07-07T21:22:04.867156Z","iopub.status.idle":"2022-07-07T21:43:28.447203Z","shell.execute_reply.started":"2022-07-07T21:22:04.867107Z","shell.execute_reply":"2022-07-07T21:43:28.446167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"interp = ClassificationInterpretation.from_learner(learn)\ninterp.plot_confusion_matrix()","metadata":{"execution":{"iopub.status.busy":"2022-07-07T21:44:12.007563Z","iopub.execute_input":"2022-07-07T21:44:12.008082Z","iopub.status.idle":"2022-07-07T21:44:45.579153Z","shell.execute_reply.started":"2022-07-07T21:44:12.008032Z","shell.execute_reply":"2022-07-07T21:44:45.578030Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"interp.plot_top_losses(7)","metadata":{"execution":{"iopub.status.busy":"2022-07-03T05:35:39.138795Z","iopub.execute_input":"2022-07-03T05:35:39.13947Z","iopub.status.idle":"2022-07-03T05:35:40.612396Z","shell.execute_reply.started":"2022-07-03T05:35:39.139403Z","shell.execute_reply":"2022-07-03T05:35:40.611361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"files = !ls \"../input/pnevmoniya/test\"\npreds = []\nnumbers = []\nfor file in tqdm(files):\n    number, n_th, probs = learn.predict(f\"../input/pnevmoniya/test/{file}\")\n    preds.append(n_th)\n    numbers.append(number)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T21:44:59.591717Z","iopub.execute_input":"2022-07-07T21:44:59.592216Z","iopub.status.idle":"2022-07-07T21:45:50.855985Z","shell.execute_reply.started":"2022-07-07T21:44:59.592170Z","shell.execute_reply":"2022-07-07T21:45:50.854995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict = []\nfor num in numbers:\n    if num == 'NORMAL':\n        predict.append(0)\n    elif num == 'PNEUMONIA':\n        predict.append(1)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T21:47:07.349542Z","iopub.execute_input":"2022-07-07T21:47:07.350171Z","iopub.status.idle":"2022-07-07T21:47:07.357727Z","shell.execute_reply.started":"2022-07-07T21:47:07.350126Z","shell.execute_reply":"2022-07-07T21:47:07.356434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/pnevmoniya/sample_solution.csv')\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-07T21:47:08.794428Z","iopub.execute_input":"2022-07-07T21:47:08.794985Z","iopub.status.idle":"2022-07-07T21:47:08.815671Z","shell.execute_reply.started":"2022-07-07T21:47:08.794895Z","shell.execute_reply":"2022-07-07T21:47:08.814843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(predict)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T21:47:10.099819Z","iopub.execute_input":"2022-07-07T21:47:10.100280Z","iopub.status.idle":"2022-07-07T21:47:10.107237Z","shell.execute_reply.started":"2022-07-07T21:47:10.100239Z","shell.execute_reply":"2022-07-07T21:47:10.106243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['labels'] = predict\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-07T21:47:10.920015Z","iopub.execute_input":"2022-07-07T21:47:10.920451Z","iopub.status.idle":"2022-07-07T21:47:10.933765Z","shell.execute_reply.started":"2022-07-07T21:47:10.920415Z","shell.execute_reply":"2022-07-07T21:47:10.932953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.to_csv('pnevmoniya3.2.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T21:47:27.060518Z","iopub.execute_input":"2022-07-07T21:47:27.061011Z","iopub.status.idle":"2022-07-07T21:47:27.075647Z","shell.execute_reply.started":"2022-07-07T21:47:27.060967Z","shell.execute_reply":"2022-07-07T21:47:27.074943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}