{"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":"# Ignore  the warnings\nimport warnings\nwarnings.filterwarnings('always')\nwarnings.filterwarnings('ignore')\n\n# data visualisation and manipulation\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom matplotlib import style\nimport seaborn as sns\n \n#configure\n# sets matplotlib to inline and displays graphs below the corressponding cell.\n%matplotlib inline  \nstyle.use('fivethirtyeight')\nsns.set(style='whitegrid',color_codes=True)\n\n#model selection\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.model_selection import KFold\nfrom sklearn.metrics import accuracy_score,precision_score,recall_score,confusion_matrix,roc_curve,roc_auc_score\nfrom sklearn.model_selection import GridSearchCV\nfrom sklearn.preprocessing import LabelEncoder\n\n#preprocess.\nfrom keras.preprocessing.image import ImageDataGenerator\n\n#dl libraraies\nfrom keras import backend as K\nfrom keras.models import Sequential\nfrom keras.layers import Dense\nfrom keras.optimizers import Adam,SGD,Adagrad,Adadelta,RMSprop\nfrom keras.utils import to_categorical\nfrom keras.utils.vis_utils import model_to_dot\nfrom keras.utils.vis_utils import plot_model\n\n# specifically for cnn\nfrom keras.applications.inception_v3 import InceptionV3, preprocess_input\nfrom keras.layers import Dropout, Flatten,Activation\nfrom keras.layers import Conv2D, MaxPooling2D, BatchNormalization,GlobalAveragePooling2D\nfrom keras.callbacks import ModelCheckpoint,EarlyStopping,TensorBoard,CSVLogger,ReduceLROnPlateau,LearningRateScheduler\n    \nimport tensorflow as tf\nimport random as rn\n\n# specifically for manipulating zipped images and getting numpy arrays of pixel values of images.\nimport cv2                  \nimport numpy as np  \nfrom tqdm import tqdm\nimport os                   \nfrom random import shuffle  \nfrom zipfile import ZipFile\nfrom PIL import Image\n\nprint(os.listdir(\"../input\"))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-04-20T10:11:11.725176Z","iopub.execute_input":"2022-04-20T10:11:11.725565Z","iopub.status.idle":"2022-04-20T10:11:11.756577Z","shell.execute_reply.started":"2022-04-20T10:11:11.725499Z","shell.execute_reply":"2022-04-20T10:11:11.755168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X=[]\nZ=[]\nIMG_SIZE=150\nTRAIN_DIR='../input/aptos2019-blindness-detection/train_images'","metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","execution":{"iopub.status.busy":"2022-04-20T10:11:11.759369Z","iopub.execute_input":"2022-04-20T10:11:11.760086Z","iopub.status.idle":"2022-04-20T10:11:11.771804Z","shell.execute_reply.started":"2022-04-20T10:11:11.760011Z","shell.execute_reply":"2022-04-20T10:11:11.770638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def make_train_data(label,path):\n    img = cv2.imread(path,cv2.IMREAD_COLOR)\n    img = cv2.resize(img, (IMG_SIZE,IMG_SIZE))\n\n    X.append(np.array(img))\n    Z.append(str(label))","metadata":{"execution":{"iopub.status.busy":"2022-04-20T10:11:11.773595Z","iopub.execute_input":"2022-04-20T10:11:11.774147Z","iopub.status.idle":"2022-04-20T10:11:11.788431Z","shell.execute_reply.started":"2022-04-20T10:11:11.774088Z","shell.execute_reply":"2022-04-20T10:11:11.786899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/aptos2019-blindness-detection/train.csv')\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-20T10:11:11.790115Z","iopub.execute_input":"2022-04-20T10:11:11.790444Z","iopub.status.idle":"2022-04-20T10:11:11.836007Z","shell.execute_reply.started":"2022-04-20T10:11:11.790379Z","shell.execute_reply":"2022-04-20T10:11:11.835107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = df['id_code']\ny = df['diagnosis']","metadata":{"execution":{"iopub.status.busy":"2022-04-20T10:11:11.837574Z","iopub.execute_input":"2022-04-20T10:11:11.838077Z","iopub.status.idle":"2022-04-20T10:11:11.842827Z","shell.execute_reply.started":"2022-04-20T10:11:11.838024Z","shell.execute_reply":"2022-04-20T10:11:11.841561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for id_code,diagnosis in tqdm(zip(x,y)):\n    path = os.path.join(TRAIN_DIR,'{}.png'.format(id_code))\n    make_train_data(diagnosis,path)","metadata":{"execution":{"iopub.status.busy":"2022-04-20T10:11:11.84698Z","iopub.execute_input":"2022-04-20T10:11:11.847582Z","iopub.status.idle":"2022-04-20T10:17:47.249794Z","shell.execute_reply.started":"2022-04-20T10:11:11.847506Z","shell.execute_reply":"2022-04-20T10:17:47.248681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# check some image\nfig,ax=plt.subplots(5,2)\nfig.set_size_inches(15,15)\nfor i in range(5):\n    for j in range (2):\n        l=rn.randint(0,len(Z))\n        ax[i,j].imshow(X[l])\n        ax[i,j].set_title(Z[l])\n        \nplt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2022-04-20T10:17:47.252176Z","iopub.execute_input":"2022-04-20T10:17:47.252498Z","iopub.status.idle":"2022-04-20T10:17:49.757934Z","shell.execute_reply.started":"2022-04-20T10:17:47.25244Z","shell.execute_reply":"2022-04-20T10:17:49.756743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y=to_categorical(Z)\nX=np.array(X)\nX=X/255","metadata":{"execution":{"iopub.status.busy":"2022-04-20T10:17:49.759646Z","iopub.execute_input":"2022-04-20T10:17:49.760037Z","iopub.status.idle":"2022-04-20T10:17:53.588887Z","shell.execute_reply.started":"2022-04-20T10:17:49.759977Z","shell.execute_reply":"2022-04-20T10:17:53.587902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train,x_valid,y_train,y_valid = train_test_split(X,Y,test_size=0.2,random_state=42)\ndel X\ndel Y\ndel Z","metadata":{"execution":{"iopub.status.busy":"2022-04-20T10:17:53.590473Z","iopub.execute_input":"2022-04-20T10:17:53.590828Z","iopub.status.idle":"2022-04-20T10:17:56.488842Z","shell.execute_reply.started":"2022-04-20T10:17:53.59077Z","shell.execute_reply":"2022-04-20T10:17:56.487691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"augs_gen = ImageDataGenerator(\n        featurewise_center=False,  \n        samplewise_center=False, \n        featurewise_std_normalization=False,  \n        samplewise_std_normalization=False,  \n        zca_whitening=False,  \n        rotation_range=10,  \n        zoom_range = 0.1, \n        width_shift_range=0.2,  \n        height_shift_range=0.2, \n        horizontal_flip=True,  \n        vertical_flip=False) \n\naugs_gen.fit(x_train)","metadata":{"execution":{"iopub.status.busy":"2022-04-20T10:17:56.490741Z","iopub.execute_input":"2022-04-20T10:17:56.4912Z","iopub.status.idle":"2022-04-20T10:17:57.835727Z","shell.execute_reply.started":"2022-04-20T10:17:56.491117Z","shell.execute_reply":"2022-04-20T10:17:57.834585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # modelling starts using a CNN.\n\nmodel = Sequential()\nmodel.add(Conv2D(filters = 32, kernel_size = (5,5),padding = 'Same',activation ='relu', input_shape = (150,150,3)))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\n\n\nmodel.add(Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same',activation ='relu'))\nmodel.add(MaxPooling2D(pool_size=(2,2), strides=(2,2)))\n \n\nmodel.add(Conv2D(filters =96, kernel_size = (3,3),padding = 'Same',activation ='relu'))\nmodel.add(MaxPooling2D(pool_size=(2,2), strides=(2,2)))\n\nmodel.add(Conv2D(filters = 96, kernel_size = (3,3),padding = 'Same',activation ='relu'))\nmodel.add(MaxPooling2D(pool_size=(2,2), strides=(2,2)))\n\nmodel.add(Flatten())\nmodel.add(Dense(512))\nmodel.add(Activation('relu'))\nmodel.add(Dense(5, activation = \"softmax\"))","metadata":{"execution":{"iopub.status.busy":"2022-04-20T10:17:57.837595Z","iopub.execute_input":"2022-04-20T10:17:57.838002Z","iopub.status.idle":"2022-04-20T10:17:57.968778Z","shell.execute_reply.started":"2022-04-20T10:17:57.837926Z","shell.execute_reply":"2022-04-20T10:17:57.967444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# set callbacks\ncheckpoint = ModelCheckpoint(\n    './base.model',\n    monitor='val_loss',\n    verbose=1,\n    save_best_only=True,\n    mode='min',\n    save_weights_only=False,\n    period=1\n)\nearlystop = EarlyStopping(\n    monitor='val_loss',\n    min_delta=0.001,\n    patience=30,\n    verbose=1,\n    mode='auto'\n)\ntensorboard = TensorBoard(\n    log_dir = './logs',\n    histogram_freq=0,\n    batch_size=16,\n    write_graph=True,\n    write_grads=True,\n    write_images=False,\n)\n\ncsvlogger = CSVLogger(\n    filename= \"training_csv.log\",\n    separator = \",\",\n    append = False\n)\n\nreduce = ReduceLROnPlateau(\n    monitor='val_loss',\n    factor=0.1,\n    patience=5,\n    min_lr=1e-6,\n    verbose=1, \n    mode='auto'\n)\n\ncallbacks = [checkpoint,tensorboard,csvlogger,reduce]","metadata":{"execution":{"iopub.status.busy":"2022-04-20T10:17:57.970939Z","iopub.execute_input":"2022-04-20T10:17:57.971414Z","iopub.status.idle":"2022-04-20T10:17:57.982329Z","shell.execute_reply.started":"2022-04-20T10:17:57.971326Z","shell.execute_reply":"2022-04-20T10:17:57.9811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size=64\nepochs=20","metadata":{"execution":{"iopub.status.busy":"2022-04-20T10:17:57.98406Z","iopub.execute_input":"2022-04-20T10:17:57.984438Z","iopub.status.idle":"2022-04-20T10:17:58.000628Z","shell.execute_reply.started":"2022-04-20T10:17:57.984372Z","shell.execute_reply":"2022-04-20T10:17:57.99931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer=Adam(lr=0.001),loss='categorical_crossentropy',metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-04-20T10:17:58.001993Z","iopub.execute_input":"2022-04-20T10:17:58.002859Z","iopub.status.idle":"2022-04-20T10:17:58.066101Z","shell.execute_reply.started":"2022-04-20T10:17:58.002257Z","shell.execute_reply":"2022-04-20T10:17:58.065222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"History = model.fit_generator(augs_gen.flow(x_train,y_train, batch_size=batch_size),\n                              epochs = epochs, validation_data = (x_valid,y_valid),\n                              verbose = 1, steps_per_epoch=x_train.shape[0] // batch_size,\n                              callbacks=callbacks)","metadata":{"execution":{"iopub.status.busy":"2022-04-20T10:17:58.067362Z","iopub.execute_input":"2022-04-20T10:17:58.067811Z","iopub.status.idle":"2022-04-20T11:13:51.940234Z","shell.execute_reply.started":"2022-04-20T10:17:58.067756Z","shell.execute_reply":"2022-04-20T11:13:51.938256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.read_csv('../input/aptos2019-blindness-detection/test.csv')\ntest_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-20T11:13:51.944245Z","iopub.execute_input":"2022-04-20T11:13:51.944888Z","iopub.status.idle":"2022-04-20T11:13:51.986687Z","shell.execute_reply.started":"2022-04-20T11:13:51.944799Z","shell.execute_reply":"2022-04-20T11:13:51.985464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = test_df['id_code']","metadata":{"execution":{"iopub.status.busy":"2022-04-20T11:13:51.988371Z","iopub.execute_input":"2022-04-20T11:13:51.989009Z","iopub.status.idle":"2022-04-20T11:13:51.996146Z","shell.execute_reply.started":"2022-04-20T11:13:51.988939Z","shell.execute_reply":"2022-04-20T11:13:51.994503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TEST_X = []\ndef make_test_data(path):\n    img = cv2.imread(path,cv2.IMREAD_COLOR)\n    img = cv2.resize(img, (IMG_SIZE,IMG_SIZE))\n\n    TEST_X.append(np.array(img))","metadata":{"execution":{"iopub.status.busy":"2022-04-20T11:13:51.997796Z","iopub.execute_input":"2022-04-20T11:13:51.998404Z","iopub.status.idle":"2022-04-20T11:13:52.01209Z","shell.execute_reply.started":"2022-04-20T11:13:51.998341Z","shell.execute_reply":"2022-04-20T11:13:52.010758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TEST_DIR='../input/aptos2019-blindness-detection/test_images'\nfor id_code in tqdm(x):\n    path = os.path.join(TEST_DIR,'{}.png'.format(id_code))\n    make_test_data(path)","metadata":{"execution":{"iopub.status.busy":"2022-04-20T11:13:52.014012Z","iopub.execute_input":"2022-04-20T11:13:52.014561Z","iopub.status.idle":"2022-04-20T11:15:19.13903Z","shell.execute_reply.started":"2022-04-20T11:13:52.014482Z","shell.execute_reply":"2022-04-20T11:15:19.138067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TEST_X=np.array(TEST_X)\nTEST_X=TEST_X/255\npred=model.predict(TEST_X)","metadata":{"execution":{"iopub.status.busy":"2022-04-20T11:15:19.140565Z","iopub.execute_input":"2022-04-20T11:15:19.140899Z","iopub.status.idle":"2022-04-20T11:15:57.436968Z","shell.execute_reply.started":"2022-04-20T11:15:19.14082Z","shell.execute_reply":"2022-04-20T11:15:57.435983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred=np.argmax(pred,axis=1)\npred","metadata":{"execution":{"iopub.status.busy":"2022-04-20T11:15:57.438144Z","iopub.execute_input":"2022-04-20T11:15:57.438399Z","iopub.status.idle":"2022-04-20T11:15:57.44787Z","shell.execute_reply.started":"2022-04-20T11:15:57.438362Z","shell.execute_reply":"2022-04-20T11:15:57.446518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_df = pd.read_csv('../input/aptos2019-blindness-detection/sample_submission.csv')\nsub_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-20T11:15:57.449875Z","iopub.execute_input":"2022-04-20T11:15:57.450451Z","iopub.status.idle":"2022-04-20T11:15:57.483929Z","shell.execute_reply.started":"2022-04-20T11:15:57.450399Z","shell.execute_reply":"2022-04-20T11:15:57.482976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_df.diagnosis = pred\nsub_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-20T11:15:57.485178Z","iopub.execute_input":"2022-04-20T11:15:57.485594Z","iopub.status.idle":"2022-04-20T11:15:57.502408Z","shell.execute_reply.started":"2022-04-20T11:15:57.485552Z","shell.execute_reply":"2022-04-20T11:15:57.501068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_df.to_csv(\"submission.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2022-04-20T11:15:57.50451Z","iopub.execute_input":"2022-04-20T11:15:57.505294Z","iopub.status.idle":"2022-04-20T11:15:57.854866Z","shell.execute_reply.started":"2022-04-20T11:15:57.505206Z","shell.execute_reply":"2022-04-20T11:15:57.853897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**If you like it , please upvote :)**","metadata":{}},{"cell_type":"code","source":"# serialize model to JSON\nfrom keras.models import model_from_json\nmodel_json = classifier.to_json()\nwith open(\"model.json\", \"w\") as json_file:\n    json_file.write(model_json)","metadata":{"execution":{"iopub.status.busy":"2022-04-20T11:15:57.856253Z","iopub.execute_input":"2022-04-20T11:15:57.856526Z","iopub.status.idle":"2022-04-20T11:15:57.929062Z","shell.execute_reply.started":"2022-04-20T11:15:57.856477Z","shell.execute_reply":"2022-04-20T11:15:57.927667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# serialize weights to HDF5\nclassifier.save_weights(\"model.h5\")\nprint(\"Saved model to disk\")","metadata":{"execution":{"iopub.status.busy":"2022-04-20T11:15:57.930174Z","iopub.status.idle":"2022-04-20T11:15:57.930696Z"},"trusted":true},"execution_count":null,"outputs":[]}]}