{"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":"from fastai.vision.all import *\nimport pandas as pd\n%matplotlib inline\nset_seed(3865)","metadata":{"execution":{"iopub.status.busy":"2022-06-28T16:08:45.623839Z","iopub.execute_input":"2022-06-28T16:08:45.624308Z","iopub.status.idle":"2022-06-28T16:08:49.117204Z","shell.execute_reply.started":"2022-06-28T16:08:45.624209Z","shell.execute_reply":"2022-06-28T16:08:49.115837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path='../input/resized-2015-2019-diabetic-retinopathy-detection/resized_traintest15_train19'\ntrain_df = pd.read_csv('../input/resized-2015-2019-diabetic-retinopathy-detection/labels/traintestLabels15_trainLabels19.csv')\n# for interactive DEBUG: reduce amount of train images : train_df = train_df[:1024]","metadata":{"execution":{"iopub.status.busy":"2022-06-28T16:08:49.118892Z","iopub.execute_input":"2022-06-28T16:08:49.119415Z","iopub.status.idle":"2022-06-28T16:08:49.21196Z","shell.execute_reply.started":"2022-06-28T16:08:49.119383Z","shell.execute_reply":"2022-06-28T16:08:49.210656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df","metadata":{"execution":{"iopub.status.busy":"2022-06-28T16:08:49.214Z","iopub.execute_input":"2022-06-28T16:08:49.214814Z","iopub.status.idle":"2022-06-28T16:08:49.369552Z","shell.execute_reply.started":"2022-06-28T16:08:49.21475Z","shell.execute_reply":"2022-06-28T16:08:49.368491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import albumentations as Alb\nclass AlbTransform(Transform):\n    def __init__(self, aug): self.aug = aug\n    def encodes(self, img: PILImage):\n        aug_img = self.aug(image=np.array(img))['image']\n        return PILImage.create(aug_img)\n    \ndef get_augs(): return Alb.Compose([\n    Alb.ShiftScaleRotate(rotate_limit=20, border_mode=0, value=(0,0,0) ),\n    # Alb.Transpose(),\n    Alb.HorizontalFlip(),\n    # Alb.RandomRotate90(),\n    Alb.RandomBrightnessContrast(),\n    Alb.HueSaturationValue(\n      hue_shift_limit=5, \n      sat_shift_limit=5, \n      val_shift_limit=5 ),\n])\n\nitem_tfms = [Resize(224), AlbTransform(get_augs())] \nbatch_tfms = Normalize.from_stats(*imagenet_stats) ","metadata":{"execution":{"iopub.status.busy":"2022-06-28T16:08:49.371559Z","iopub.execute_input":"2022-06-28T16:08:49.371913Z","iopub.status.idle":"2022-06-28T16:08:50.712465Z","shell.execute_reply.started":"2022-06-28T16:08:49.37188Z","shell.execute_reply":"2022-06-28T16:08:50.711165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls = ImageDataLoaders.from_df(train_df, path=train_path, \n    fn_col='image', label_col='level', suff='.jpg', valid_pct=0.1, bs=32,\n    item_tfms=item_tfms, batch_tfms=batch_tfms, shuffle=True)","metadata":{"execution":{"iopub.status.busy":"2022-06-28T16:08:50.713975Z","iopub.execute_input":"2022-06-28T16:08:50.714413Z","iopub.status.idle":"2022-06-28T16:09:00.782731Z","shell.execute_reply.started":"2022-06-28T16:08:50.714369Z","shell.execute_reply":"2022-06-28T16:09:00.781284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# uncomment to test data loaders\ndls.train.show_batch(max_n=12)\nprint('train items:', len(dls.train.items), 'validation items:', len(dls.valid.items))\n# dls.valid.show_batch(max_n=12)","metadata":{"execution":{"iopub.status.busy":"2022-06-28T16:09:00.785229Z","iopub.execute_input":"2022-06-28T16:09:00.785848Z","iopub.status.idle":"2022-06-28T16:09:03.921905Z","shell.execute_reply.started":"2022-06-28T16:09:00.785769Z","shell.execute_reply":"2022-06-28T16:09:03.920777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls.vocab\n# dls.c","metadata":{"execution":{"iopub.status.busy":"2022-06-28T16:09:27.07198Z","iopub.execute_input":"2022-06-28T16:09:27.072518Z","iopub.status.idle":"2022-06-28T16:09:27.080896Z","shell.execute_reply.started":"2022-06-28T16:09:27.072468Z","shell.execute_reply":"2022-06-28T16:09:27.079694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = vision_learner(dls, resnet34, path='.', \n    loss_func=FocalLoss(),  \n    metrics=[accuracy]  )","metadata":{"execution":{"iopub.status.busy":"2022-06-28T16:13:12.253659Z","iopub.execute_input":"2022-06-28T16:13:12.254198Z","iopub.status.idle":"2022-06-28T16:13:12.880378Z","shell.execute_reply.started":"2022-06-28T16:13:12.254153Z","shell.execute_reply":"2022-06-28T16:13:12.879307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# lrs = learn.lr_find(suggest_funcs=(minimum, steep, valley, slide))\n# print('lrs.valley=', lrs.valley)","metadata":{"execution":{"iopub.status.busy":"2022-06-28T16:13:12.882376Z","iopub.execute_input":"2022-06-28T16:13:12.883346Z","iopub.status.idle":"2022-06-28T16:26:14.211367Z","shell.execute_reply.started":"2022-06-28T16:13:12.883289Z","shell.execute_reply":"2022-06-28T16:26:14.210281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fine_tune(4, cbs=[ShowGraphCallback()])","metadata":{"execution":{"iopub.status.busy":"2022-06-28T16:26:14.212843Z","iopub.execute_input":"2022-06-28T16:26:14.213734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.save('retinopathy201519-rn34-1') ","metadata":{},"execution_count":null,"outputs":[]}]}