{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"from fastai.vision import *\nfrom pathlib import Path\nimport cv2","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import os\nprint(os.listdir(\"../input/recursion-cellular-image-classification/\"))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"path = Path('../input/recursion-cellular-image-classification/')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train = pd.read_csv(f'{path}/train.csv')\ntrain.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pixel_stats = pd.read_csv(f'{path}/pixel_stats.csv')\npixel_stats.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.style.use('ggplot')\nplt.figure(figsize=(15,5))\ntrain.experiment.value_counts().plot.barh()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(15,5))\ntrain.sirna.value_counts()\ntrain.sirna.value_counts().plot.bar()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"len(train.sirna.unique())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.isnull().sum().sort_index()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"smpl=1\ntrain['path'] = train['experiment']+'/Plate'+train['plate'].astype(str)+'/'+train['well'].astype(str)+'_s'+str(smpl)+'_w'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\nimg = cv2.imread(f\"{path}/train/HUVEC-06/Plate1/B02_s1_w1.png\")\nplt.imshow(img)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"gray_img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)\nplt.imshow(gray_img)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(10,5))\n[[plt.subplot(1,6,i+1),plt.imshow(cv2.imread(f\"{path}/train/{train.path[0]}{str(i+1)}.png\")), plt.grid(False), plt.yticks([]),  plt.xticks([])] for i in range(6)];\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(10,5))\n[[plt.subplot(1,6,i+1),plt.imshow(cv2.cvtColor(cv2.imread(f\"{path}/train/{train.path[0]}{str(i+1)}.png\"),cv2.COLOR_RGB2GRAY)), plt.grid(False), plt.yticks([]),  plt.xticks([])] for i in range(6)];\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"source https://www.kaggle.com/tanlikesmath/rcic-fastai-starter\ndef opening_file(fn):\n    return Image(pil2tensor(np.dstack([cv2.cvtColor(cv2.imread(f\"{fn}{str(i+1)}.png\"),cv2.COLOR_RGB2GRAY) for i in range(6)]), np.float32).div_(255))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"source https://www.kaggle.com/tanlikesmath/rcic-fastai-starter"},{"metadata":{"trusted":true},"cell_type":"code","source":"class MultiChannelImageList(ImageList):\n     def open(self, fn):\n        return opening_file(fn)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"image_list_df = train.copy()\n\nimage_list_df.drop(['id_code', 'experiment', 'plate', 'well'], axis=1, inplace = True)\n\nimage_list_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dat = MultiChannelImageList.from_df(df=image_list_df, path=r'train/',cols='path')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":""},{"metadata":{"trusted":true},"cell_type":"code","source":"#copied from source https://www.kaggle.com/tanlikesmath/rcic-fastai-starter\ndef image2np(image:Tensor)->np.ndarray:\n    \"Convert from torch style `image` to numpy/matplotlib style.\"\n    res = image.cpu().permute(1,2,0).numpy()\n    if res.shape[2]==1:\n        return res[...,0]  \n    elif res.shape[2]>3:\n        return res[...,:3]\n    else:\n        return res\n\nvision.image.image2np = image2np","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data = (MultiChannelImageList.from_df(df=image_list_df,path=f'{path}/train/', cols = 'path')\n        .split_by_rand_pct(0.1)\n        .label_from_df(cols ='sirna')\n        .transform(get_transforms(),size=128)\n        .databunch(bs=8,num_workers=0)\n        .normalize(imagenet_stats)\n       )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"arch = models.resnet34","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":1}