{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"tpu1vmV38","dataSources":[{"sourceId":14774,"databundleVersionId":875431,"sourceType":"competition"}],"dockerImageVersionId":30747,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"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\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 read-only \"../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#for dirname, _, filenames in os.walk('/kaggle/input'):\n#    for filename in filenames:\n#        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-07-23T08:33:30.368857Z","iopub.execute_input":"2024-07-23T08:33:30.369280Z","iopub.status.idle":"2024-07-23T08:33:32.092769Z","shell.execute_reply.started":"2024-07-23T08:33:30.369218Z","shell.execute_reply":"2024-07-23T08:33:32.091808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/input/aptos2019-blindness-detection/train.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-07-23T08:33:33.421751Z","iopub.execute_input":"2024-07-23T08:33:33.422209Z","iopub.status.idle":"2024-07-23T08:33:33.437842Z","shell.execute_reply.started":"2024-07-23T08:33:33.422177Z","shell.execute_reply":"2024-07-23T08:33:33.437045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2024-07-23T08:33:34.360556Z","iopub.execute_input":"2024-07-23T08:33:34.360943Z","iopub.status.idle":"2024-07-23T08:33:34.376152Z","shell.execute_reply.started":"2024-07-23T08:33:34.360912Z","shell.execute_reply":"2024-07-23T08:33:34.375257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.diagnosis.unique()","metadata":{"execution":{"iopub.status.busy":"2024-07-23T08:33:36.106367Z","iopub.execute_input":"2024-07-23T08:33:36.106731Z","iopub.status.idle":"2024-07-23T08:33:36.112837Z","shell.execute_reply.started":"2024-07-23T08:33:36.106700Z","shell.execute_reply":"2024-07-23T08:33:36.112079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(df)","metadata":{"execution":{"iopub.status.busy":"2024-07-23T08:33:36.114202Z","iopub.execute_input":"2024-07-23T08:33:36.114501Z","iopub.status.idle":"2024-07-23T08:33:36.123994Z","shell.execute_reply.started":"2024-07-23T08:33:36.114475Z","shell.execute_reply":"2024-07-23T08:33:36.123199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.diagnosis.value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-07-23T08:33:36.891548Z","iopub.execute_input":"2024-07-23T08:33:36.891848Z","iopub.status.idle":"2024-07-23T08:33:36.898443Z","shell.execute_reply.started":"2024-07-23T08:33:36.891820Z","shell.execute_reply":"2024-07-23T08:33:36.897768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2024-07-23T08:33:37.626268Z","iopub.execute_input":"2024-07-23T08:33:37.626565Z","iopub.status.idle":"2024-07-23T08:33:37.633361Z","shell.execute_reply.started":"2024-07-23T08:33:37.626536Z","shell.execute_reply":"2024-07-23T08:33:37.632746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# x = [[],[],[]]\n# y = [1,2,3] = df.diagnosis","metadata":{"execution":{"iopub.status.busy":"2024-07-23T08:33:38.854944Z","iopub.execute_input":"2024-07-23T08:33:38.855289Z","iopub.status.idle":"2024-07-23T08:33:38.858944Z","shell.execute_reply.started":"2024-07-23T08:33:38.855258Z","shell.execute_reply":"2024-07-23T08:33:38.858202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.makedirs(\"/kaggle/working/0\")\nos.makedirs(\"/kaggle/working/1\")\nos.makedirs(\"/kaggle/working/2\")\nos.makedirs(\"/kaggle/working/3\")\nos.makedirs(\"/kaggle/working/4\")","metadata":{"execution":{"iopub.status.busy":"2024-07-23T08:33:38.980598Z","iopub.execute_input":"2024-07-23T08:33:38.981242Z","iopub.status.idle":"2024-07-23T08:33:38.984922Z","shell.execute_reply.started":"2024-07-23T08:33:38.981199Z","shell.execute_reply":"2024-07-23T08:33:38.984355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_images = 5 folders\nimport shutil\nfor a,b in zip(df.id_code,df.diagnosis):\n    shutil.copy(os.path.join(\"/kaggle/input/aptos2019-blindness-detection/train_images\",a+\".png\"),\n               os.path.join(\"/kaggle/working/\",str(b),a+\".png\"))","metadata":{"execution":{"iopub.status.busy":"2024-07-23T08:34:37.345163Z","iopub.execute_input":"2024-07-23T08:34:37.345535Z","iopub.status.idle":"2024-07-23T08:36:58.756217Z","shell.execute_reply.started":"2024-07-23T08:34:37.345503Z","shell.execute_reply":"2024-07-23T08:36:58.755488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(os.listdir(\"/kaggle/working/0\"))","metadata":{"execution":{"iopub.status.busy":"2024-07-23T08:36:58.757548Z","iopub.execute_input":"2024-07-23T08:36:58.757787Z","iopub.status.idle":"2024-07-23T08:36:58.763243Z","shell.execute_reply.started":"2024-07-23T08:36:58.757762Z","shell.execute_reply":"2024-07-23T08:36:58.762593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\ndata_gen = ImageDataGenerator(rescale=1/255,\n#                             featurewise_center=False,\n    rotation_range=45,\n    width_shift_range=0.2,\n    height_shift_range=0.2,\n    shear_range=0.2,\n    zoom_range=0.2,\n    fill_mode='nearest',\n    horizontal_flip=True,\n    vertical_flip=True,\n    validation_split=0.25\n                             )","metadata":{"execution":{"iopub.status.busy":"2024-07-23T08:36:58.764139Z","iopub.execute_input":"2024-07-23T08:36:58.764409Z","iopub.status.idle":"2024-07-23T08:37:11.862915Z","shell.execute_reply.started":"2024-07-23T08:36:58.764382Z","shell.execute_reply":"2024-07-23T08:37:11.862145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shutil.rmtree(os.path.join(\"/kaggle/working/\",\".virtual_documents\"))","metadata":{"execution":{"iopub.status.busy":"2024-07-23T08:37:11.864604Z","iopub.execute_input":"2024-07-23T08:37:11.865089Z","iopub.status.idle":"2024-07-23T08:37:11.869259Z","shell.execute_reply.started":"2024-07-23T08:37:11.865057Z","shell.execute_reply":"2024-07-23T08:37:11.868665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = data_gen.flow_from_directory(directory=\"/kaggle/working/\",\n                                             target_size=(300,300),\n                                             batch_size=64,\n                                             class_mode=\"categorical\")","metadata":{"execution":{"iopub.status.busy":"2024-07-23T08:37:11.870010Z","iopub.execute_input":"2024-07-23T08:37:11.870257Z","iopub.status.idle":"2024-07-23T08:37:12.303738Z","shell.execute_reply.started":"2024-07-23T08:37:11.870216Z","shell.execute_reply":"2024-07-23T08:37:12.303004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def model():\n    from tensorflow.keras.models import Sequential\n    from tensorflow.keras.layers import Conv2D, MaxPooling2D,Dense,Flatten,Dropout, GlobalAveragePooling2D\n    model = Sequential()\n    model.add(Conv2D(filters=32, kernel_size=(3,3), strides=1, padding='same', activation='relu', input_shape=[300, 400, 3]))\n    model.add(MaxPooling2D(2,2))\n    model.add(Conv2D(filters=64, kernel_size=(3,3), strides=1, padding='same', activation='relu'))\n    model.add(MaxPooling2D(2,2))\n    model.add(Conv2D(filters=128, kernel_size=(3,3), strides=1, padding='same', activation='relu'))\n    model.add(MaxPooling2D(2,2))\n    model.add(Conv2D(filters=128, kernel_size=(3,3), strides=1, padding='same', activation='relu'))\n    model.add(MaxPooling2D(2,2))\n    model.add(Conv2D(filters=256, kernel_size=(3,3), strides=1, padding='same', activation='relu'))\n    model.add(MaxPooling2D(2,2))\n    model.add(GlobalAveragePooling2D())\n    model.add(Dense(128, activation='relu'))\n    model.add(Dense(5, activation='softmax'))\n    model.compile(loss='categorical_crossentropy',\n              optimizer='adam',\n              metrics=['acc'])\n    return model","metadata":{"execution":{"iopub.status.busy":"2024-07-23T08:37:12.304713Z","iopub.execute_input":"2024-07-23T08:37:12.305051Z","iopub.status.idle":"2024-07-23T08:37:12.313146Z","shell.execute_reply.started":"2024-07-23T08:37:12.305021Z","shell.execute_reply":"2024-07-23T08:37:12.312400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\n\ndevice_name = tf.test.gpu_device_name()\n\nif \"GPU\" not in device_name:\n    print(\"GPU device not found\")\n    \nprint('Found GPU at: {}'.format(device_name))\n\nprint(\"GPU\", \"available (YESS!!!!)\" if tf.config.list_physical_devices(\"GPU\") else \"not available :(\")","metadata":{"execution":{"iopub.status.busy":"2024-07-23T08:52:08.905328Z","iopub.execute_input":"2024-07-23T08:52:08.905853Z","iopub.status.idle":"2024-07-23T08:52:08.916734Z","shell.execute_reply.started":"2024-07-23T08:52:08.905809Z","shell.execute_reply":"2024-07-23T08:52:08.915609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\ntpu = tf.distribute.cluster_resolver.TPUClusterResolver()\n\ntf.tpu.experimental.initialize_tpu_system(tpu)\ntpu_strategy = tf.distribute.TPUStrategy(tpu)\n\nwith tpu_strategy.scope():\n    model = model()\nmodel.fit(data, epochs=14)","metadata":{"execution":{"iopub.status.busy":"2024-07-23T08:37:12.314009Z","iopub.execute_input":"2024-07-23T08:37:12.314275Z","iopub.status.idle":"2024-07-23T08:47:17.525499Z","shell.execute_reply.started":"2024-07-23T08:37:12.314247Z","shell.execute_reply":"2024-07-23T08:47:17.523784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(data, epochs=2)","metadata":{"execution":{"iopub.status.busy":"2024-07-23T08:34:09.284051Z","iopub.status.idle":"2024-07-23T08:34:09.284354Z","shell.execute_reply.started":"2024-07-23T08:34:09.284189Z","shell.execute_reply":"2024-07-23T08:34:09.284203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_gen = ImageDataGenerator(rescale = 1.0/255.0) # Normalise the data\ntest_data = test_gen.flow_from_directory(\n                                            \"/kaggle/input/aptos2019-blindness-detection/test_images\",\n                                            target_size=(300, 400),\n                                            batch_size=32)","metadata":{"execution":{"iopub.status.busy":"2024-07-23T08:34:09.285172Z","iopub.status.idle":"2024-07-23T08:34:09.285465Z","shell.execute_reply.started":"2024-07-23T08:34:09.285329Z","shell.execute_reply":"2024-07-23T08:34:09.285343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Make predictions on the test data\npredictions = model.predict(test_data)","metadata":{"execution":{"iopub.status.busy":"2024-07-23T08:34:09.286209Z","iopub.status.idle":"2024-07-23T08:34:09.286499Z","shell.execute_reply.started":"2024-07-23T08:34:09.286358Z","shell.execute_reply":"2024-07-23T08:34:09.286372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.read_csv(\"/kaggle/input/aptos2019-blindness-detection/sample_submission.csv\")\n\nte = pd.read_csv(\"/kaggle/input/aptos2019-blindness-detection/test.csv\")\nid = te.iloc[:,0]\n\nsubmission = pd.DataFrame({\"id_code\":id,\"diagnosis\":p3})\n\nsubmission.to_csv(\"Submission.csv\",index=False)\n\nsubmission=pd.read_csv(\"Submission.csv\")\nsubmission\n\n","metadata":{"execution":{"iopub.status.busy":"2024-07-23T08:34:09.287264Z","iopub.status.idle":"2024-07-23T08:34:09.287533Z","shell.execute_reply.started":"2024-07-23T08:34:09.287397Z","shell.execute_reply":"2024-07-23T08:34:09.287411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2024-07-23T08:34:09.288334Z","iopub.status.idle":"2024-07-23T08:34:09.288619Z","shell.execute_reply.started":"2024-07-23T08:34:09.288481Z","shell.execute_reply":"2024-07-23T08:34:09.288498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(train,y,epochs=500)\n\npr = model.predict(test)\n\npr = [a[0] for a in pr]\nt2 = pd.read_csv(\"/content/test (1).csv\")\nid = t2.iloc[:,0]\nsubmission = pd.DataFrame({\"Id\":id,\"SalePrice\":pr})\nsubmission.to_csv(\"Submission.csv\",index=False)\npd.read_csv(\"Submission.csv\")\n\n","metadata":{"execution":{"iopub.status.busy":"2024-07-23T08:34:09.289604Z","iopub.status.idle":"2024-07-23T08:34:09.289876Z","shell.execute_reply.started":"2024-07-23T08:34:09.289741Z","shell.execute_reply":"2024-07-23T08:34:09.289755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}