{"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\n\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":"os.chdir(r'/kaggle/input/aptos2019-blindness-detection')\npath=os.getcwd()\nprint(path)\nprint(os.listdir())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"path_train=os.path.join(path,\"train_images\")\nprint(\"train_path: \"+path_train)\npath_test=os.path.join(path,\"test_images\")\nprint(\"train_path: \"+path_test)\npath_train_csv=os.path.join(path,\"train.csv\")\nprint(\"train_path: \"+path_train_csv)\npath_sample_csv=os.path.join(path,\"sample_submission.csv\")\nprint(\"train_path: \"+path_sample_csv)\nroot_path = '/kaggle/input'\nos.mkdir(os.path.join(root_path,\"Train_new\"))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"os.chdir(r'/kaggle/input')\npath=os.getcwd()\nprint(path)\nprint(os.listdir())\nos.chdir(r'/kaggle/input/Train_new')\npath_Train_new=os.getcwd()\nprint(path_Train_new)\n                         ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train=pd.read_csv(path_train_csv)\nprint(train.head(3))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"a=train['diagnosis'].value_counts()\nb=train['id_code'].value_counts()\nprint(\"Unique Id: \"+str(len(a)))\nprint(\"total images: \"+str(len(b)))\nprint(\"train_shape: \"+str(train.shape))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_images_name=train['id_code']\ntrain_images_name=train_images_name.to_list()\n\ntrain_name=[]\nfor name in train_images_name:\n    train_name.append(name+'.png')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_name=pd.DataFrame(train_name)\ntrain=pd.concat([train,train_name],axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.drop(['id_code'],axis=1,inplace=True)\ntrain.columns=['diagnosis','id_code']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train[:3]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# filtering images w.r.t ID\nlabel=train['diagnosis'].unique()\nlabel_list=[]\nfor i in label:\n    label_list.append(str(i))\nprint(\"Classes: \"+str(len(label_list)))\nd={}\nfor name in label_list:\n    index=train['diagnosis']==int(name)\n    a=train[index]\n    d[name]=a['id_code'].tolist()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"root_path = path_Train_new\nos.mkdir(os.path.join(root_path,\"0\"))\nos.mkdir(os.path.join(root_path,\"1\"))\nos.mkdir(os.path.join(root_path,\"2\"))\nos.mkdir(os.path.join(root_path,\"3\"))\nos.mkdir(os.path.join(root_path,\"4\"))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(\"Sub Directories of Train_New: \"+str(os.listdir()))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"os.chdir(path)\nprint(os.listdir())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# copying images to their respected ID's\nos.chdir(path_train)\nimport shutil\nfor name in label_list:\n    list=d[name]\n    for f in list:\n        path=os.path.join(path_Train_new,name)\n        shutil.copy(f,path)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import keras\nfrom keras.preprocessing.image import ImageDataGenerator\ntrain_gen=ImageDataGenerator(rescale=1/255,rotation_range=40,width_shift_range=0.3,height_shift_range=0.2,shear_range=0.2,zoom_range=0.2,\n                            fill_mode='nearest',validation_split=0.1)\ntrain_data=train_gen.flow_from_directory(path_Train_new,subset='training',batch_size=50,target_size=(224,224))\ntest_data=train_gen.flow_from_directory(path_Train_new,subset='validation',batch_size=10,target_size=(224,224))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.layers import Dense\nfrom keras.models import load_model,Model\nfrom keras import layers\nn_classes=5\nfrom keras.optimizers import RMSprop\nin_layer = layers.Input((224,224,3))\n\nconv1 = layers.Conv2D(96, 11, strides=4, activation='relu')(in_layer)\npool1 = layers.MaxPooling2D(3, 2)(conv1)\nconv2 = layers.Conv2D(256, 5, strides=1, padding='same', activation='relu')(pool1)\npool2 = layers.MaxPooling2D(3, 2)(conv2)\nconv3 = layers.Conv2D(384, 3, strides=1, padding='same', activation='relu')(pool2)\nconv4 = layers.Conv2D(256, 3, strides=1, padding='same', activation='relu')(conv3)\npool3 = layers.MaxPooling2D(3, 2)(conv4)\nflattened = layers.Flatten()(pool3)\ndense1 = layers.Dense(4096, activation='relu')(flattened)\ndrop1 = layers.Dropout(0.5)(dense1)\ndense2 = layers.Dense(4096, activation='relu')(drop1)\ndrop2 = layers.Dropout(0.5)(dense2)\npreds = layers.Dense(n_classes, activation='softmax')(drop2)\n\n\nmodel=Model(inputs=in_layer,outputs=preds)                                  # new model's summary\nmodel.compile(loss=\"categorical_crossentropy\", optimizer='RMSprop',metrics=[\"accuracy\"])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\nimport tensorflow as tf\n\nclass myCallback(tf.keras.callbacks.Callback):     # customized Callback class\n    def on_epoch_end(self,epoch,logs={}):\n        if(logs.get('val_accuracy')>0.70):\n            print('cancelling since validation accuracy has been reached to 70%')\n            self.model.stop_training=True\ncallbacks=myCallback()   ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"history=model.fit_generator(train_data,epochs=10,validation_data=test_data,callbacks=[callbacks])\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#os.chdir(r'/kaggle/working')\n#model_1.save(\"blind01.h5\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#os.chdir(r'/kaggle/input/blind01')\n#os.listdir()\n#from keras.models import load_model\n#model=load_model(\"blind01.h5\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from PIL import Image\nfrom numpy import asarray\ntest_images=os.listdir(path_test)\nprint(len(test_images))\n\ny_pred=[]\n\nfor file in test_images:\n    path_file=os.path.join(path_test,file)\n    img=Image.open(path_file)\n    img=asarray(img.resize((224,224)))\n    img=img.reshape(1,224,224,3)\n    y_pred.append(model.predict(img))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_pred_np=np.array(y_pred)\nprint(y_pred_np.shape)\ny_pred_np=y_pred_np.reshape(1928,5)\nimport pandas as pd\ny_pred_final=y_pred_np.argmax(axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_pred_final[10:20]\ny_pred_df=pd.DataFrame(y_pred_final)\nsubmission=pd.read_csv(path_sample_csv)\nprint(submission.head())\nsubmission_n=pd.concat([submission,y_pred_df],axis=1)\nsubmission_n.drop(['diagnosis'],axis=1,inplace=True)\nsubmission_n.columns=['id_code','diagnosis']\n\nos.chdir('/kaggle/working')\nsubmission_n.to_csv(\"submission.csv\",index=False)","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}