{"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":"markdown","source":"# Neuronal Cell Classification","metadata":{}},{"cell_type":"markdown","source":"We train a classification model for predicting the `cell_type` for Sartorius Cell Instance Segmentation Data. This could be helpful in the instance segmentation pipeline.","metadata":{}},{"cell_type":"code","source":"import fastai\nfrom fastai.vision.all import *","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-12-16T12:54:02.826390Z","iopub.execute_input":"2021-12-16T12:54:02.827191Z","iopub.status.idle":"2021-12-16T12:54:06.522214Z","shell.execute_reply.started":"2021-12-16T12:54:02.827070Z","shell.execute_reply":"2021-12-16T12:54:06.521228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"SEED = 3011\n\ndef fix_seeds(seed):\n    np.random.seed(seed)\n    random.seed(seed)\n    os.environ['PYTHONHASHSEED'] = str(seed)\n    torch.manual_seed(seed)\n    torch.cuda.manual_seed(seed)\n    torch.cuda.manual_seed_all(seed)\n    \nfix_seeds(SEED)","metadata":{"execution":{"iopub.status.busy":"2021-12-16T12:54:07.868627Z","iopub.execute_input":"2021-12-16T12:54:07.869419Z","iopub.status.idle":"2021-12-16T12:54:07.878122Z","shell.execute_reply.started":"2021-12-16T12:54:07.869384Z","shell.execute_reply":"2021-12-16T12:54:07.877018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TRAIN_CSV = \"../input/sartorius-cell-instance-segmentation/train.csv\"\nTRAIN_PATH = \"../input/sartorius-cell-instance-segmentation/train\"\nTEST_PATH = \"../input/sartorius-cell-instance-segmentation/test\"","metadata":{"execution":{"iopub.status.busy":"2021-12-16T12:54:09.633423Z","iopub.execute_input":"2021-12-16T12:54:09.633729Z","iopub.status.idle":"2021-12-16T12:54:09.638610Z","shell.execute_reply.started":"2021-12-16T12:54:09.633697Z","shell.execute_reply":"2021-12-16T12:54:09.637416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.read_csv(TRAIN_CSV)","metadata":{"execution":{"iopub.status.busy":"2021-12-16T12:54:11.021162Z","iopub.execute_input":"2021-12-16T12:54:11.021803Z","iopub.status.idle":"2021-12-16T12:54:11.527046Z","shell.execute_reply.started":"2021-12-16T12:54:11.021769Z","shell.execute_reply":"2021-12-16T12:54:11.526060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_df = df_train[['id', 'cell_type']].drop_duplicates().reset_index(drop = True)","metadata":{"execution":{"iopub.status.busy":"2021-12-16T12:54:13.165648Z","iopub.execute_input":"2021-12-16T12:54:13.166290Z","iopub.status.idle":"2021-12-16T12:54:13.222397Z","shell.execute_reply.started":"2021-12-16T12:54:13.166239Z","shell.execute_reply":"2021-12-16T12:54:13.221449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"With this, we have a dataframe containing information about the image id and it's class.","metadata":{}},{"cell_type":"code","source":"img_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-12-16T12:54:22.860125Z","iopub.execute_input":"2021-12-16T12:54:22.860484Z","iopub.status.idle":"2021-12-16T12:54:22.889840Z","shell.execute_reply.started":"2021-12-16T12:54:22.860443Z","shell.execute_reply":"2021-12-16T12:54:22.882459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We use `fastai`'s datablock API to quickly create the dataloader.","metadata":{}},{"cell_type":"code","source":"dblock = DataBlock(\n    blocks = (ImageBlock, CategoryBlock),\n    get_x = ColReader('id', pref = \"../input/sartorius-cell-instance-segmentation/train/\", suff = '.png'),\n    get_y = ColReader('cell_type')\n)\ndls = dblock.dataloaders(img_df, bs = 32, num_workers = 4)\ndls.show_batch(figsize = (30, 22))","metadata":{"execution":{"iopub.status.busy":"2021-12-16T12:54:56.269368Z","iopub.execute_input":"2021-12-16T12:54:56.269684Z","iopub.status.idle":"2021-12-16T12:54:59.503589Z","shell.execute_reply.started":"2021-12-16T12:54:56.269646Z","shell.execute_reply":"2021-12-16T12:54:59.502642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Using the default resnet18 model is enough for our needs here.","metadata":{}},{"cell_type":"code","source":"learn = cnn_learner(dls, resnet18, metrics = accuracy)","metadata":{"execution":{"iopub.status.busy":"2021-12-16T12:55:01.708923Z","iopub.execute_input":"2021-12-16T12:55:01.709439Z","iopub.status.idle":"2021-12-16T12:55:02.021223Z","shell.execute_reply.started":"2021-12-16T12:55:01.709388Z","shell.execute_reply":"2021-12-16T12:55:02.020108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.cuda();","metadata":{"execution":{"iopub.status.busy":"2021-12-16T12:55:04.095260Z","iopub.execute_input":"2021-12-16T12:55:04.095552Z","iopub.status.idle":"2021-12-16T12:55:04.121382Z","shell.execute_reply.started":"2021-12-16T12:55:04.095521Z","shell.execute_reply":"2021-12-16T12:55:04.120124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.lr_find()","metadata":{"execution":{"iopub.status.busy":"2021-12-16T12:55:04.882749Z","iopub.execute_input":"2021-12-16T12:55:04.883120Z","iopub.status.idle":"2021-12-16T12:55:54.870150Z","shell.execute_reply.started":"2021-12-16T12:55:04.883088Z","shell.execute_reply":"2021-12-16T12:55:54.868966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fine_tune(5, 1e-3)","metadata":{"execution":{"iopub.status.busy":"2021-12-16T12:56:21.338968Z","iopub.execute_input":"2021-12-16T12:56:21.339683Z","iopub.status.idle":"2021-12-16T12:57:23.096111Z","shell.execute_reply.started":"2021-12-16T12:56:21.339650Z","shell.execute_reply":"2021-12-16T12:57:23.095025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.export(\"cell-classification-learner.pkl\")","metadata":{"execution":{"iopub.status.busy":"2021-12-16T12:57:25.930676Z","iopub.execute_input":"2021-12-16T12:57:25.931006Z","iopub.status.idle":"2021-12-16T12:57:26.062300Z","shell.execute_reply.started":"2021-12-16T12:57:25.930961Z","shell.execute_reply":"2021-12-16T12:57:26.061304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn2 = load_learner(\"cell-classification-learner.pkl\")","metadata":{"execution":{"iopub.status.busy":"2021-12-16T12:57:29.455066Z","iopub.execute_input":"2021-12-16T12:57:29.455719Z","iopub.status.idle":"2021-12-16T12:57:29.498070Z","shell.execute_reply.started":"2021-12-16T12:57:29.455678Z","shell.execute_reply":"2021-12-16T12:57:29.497070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn2.cpu();","metadata":{"execution":{"iopub.status.busy":"2021-12-16T12:57:31.952257Z","iopub.execute_input":"2021-12-16T12:57:31.952578Z","iopub.status.idle":"2021-12-16T12:57:31.961677Z","shell.execute_reply.started":"2021-12-16T12:57:31.952546Z","shell.execute_reply":"2021-12-16T12:57:31.960503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## References\n\n`fastai` was used for creating and training the model, and it provides a quick and flexible way of creating dataloaders.  \nMore about `fastai` vision can be found [here](https://docs.fast.ai/tutorial.vision.html).","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}