{"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":"import tensorflow as tf\nfrom tensorflow.keras import layers\nimport tensorflow_addons as tfa\n\nfrom kaggle_datasets import KaggleDatasets\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\n\nfrom plotly.subplots import make_subplots\nimport plotly.express as px\n\nAUTO=tf.data.AUTOTUNE\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-08-21T17:02:01.123573Z","iopub.execute_input":"2021-08-21T17:02:01.124133Z","iopub.status.idle":"2021-08-21T17:02:08.408828Z","shell.execute_reply.started":"2021-08-21T17:02:01.124010Z","shell.execute_reply":"2021-08-21T17:02:08.407745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resolver = tf.distribute.cluster_resolver.TPUClusterResolver(tpu='')\ntf.config.experimental_connect_to_cluster(resolver)\ntf.tpu.experimental.initialize_tpu_system(resolver)\nstrategy = tf.distribute.TPUStrategy(resolver)\n","metadata":{"execution":{"iopub.status.busy":"2021-08-21T17:02:16.435539Z","iopub.execute_input":"2021-08-21T17:02:16.435902Z","iopub.status.idle":"2021-08-21T17:02:24.471602Z","shell.execute_reply.started":"2021-08-21T17:02:16.435872Z","shell.execute_reply":"2021-08-21T17:02:24.470527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gcsPath=KaggleDatasets().get_gcs_path()\n","metadata":{"execution":{"iopub.status.busy":"2021-08-21T17:02:24.473058Z","iopub.execute_input":"2021-08-21T17:02:24.473360Z","iopub.status.idle":"2021-08-21T17:02:24.844859Z","shell.execute_reply.started":"2021-08-21T17:02:24.473333Z","shell.execute_reply":"2021-08-21T17:02:24.844050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Train=tf.data.TFRecordDataset(tf.io.gfile.glob(gcsPath+'/tfrecords-jpeg-512x512/train/*'),num_parallel_reads=AUTO)\nVal=tf.data.TFRecordDataset(tf.io.gfile.glob(gcsPath+'/tfrecords-jpeg-512x512/val/*'),num_parallel_reads=AUTO)\nTest=tf.data.TFRecordDataset(tf.io.gfile.glob(gcsPath+'/tfrecords-jpeg-512x512/test/*'),num_parallel_reads=AUTO)\n","metadata":{"execution":{"iopub.status.busy":"2021-08-21T17:02:24.846380Z","iopub.execute_input":"2021-08-21T17:02:24.846895Z","iopub.status.idle":"2021-08-21T17:02:25.120472Z","shell.execute_reply.started":"2021-08-21T17:02:24.846847Z","shell.execute_reply":"2021-08-21T17:02:25.119557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"FullTrain=Train.concatenate(Val)\n","metadata":{"execution":{"iopub.status.busy":"2021-08-21T17:02:25.121837Z","iopub.execute_input":"2021-08-21T17:02:25.122165Z","iopub.status.idle":"2021-08-21T17:02:25.128350Z","shell.execute_reply.started":"2021-08-21T17:02:25.122129Z","shell.execute_reply":"2021-08-21T17:02:25.127310Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"temp=[]\nfor i,data in enumerate(FullTrain):\n    temp.append(i)","metadata":{"execution":{"iopub.status.busy":"2021-08-21T17:02:25.129547Z","iopub.execute_input":"2021-08-21T17:02:25.129845Z","iopub.status.idle":"2021-08-21T17:02:42.685005Z","shell.execute_reply.started":"2021-08-21T17:02:25.129818Z","shell.execute_reply":"2021-08-21T17:02:42.683961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"for i in FullTrain.take(1):\n    example=tf.train.Example()\n    example.ParseFromString(i.numpy())\n    print(example)\n","metadata":{"execution":{"iopub.status.busy":"2021-08-21T13:47:19.857632Z","iopub.execute_input":"2021-08-21T13:47:19.858256Z","iopub.status.idle":"2021-08-21T13:47:20.276509Z","shell.execute_reply.started":"2021-08-21T13:47:19.858201Z","shell.execute_reply":"2021-08-21T13:47:20.275504Z"}}},{"cell_type":"code","source":"def parseImage(EagerTensor):\n    FeatureMap={\n    'image':tf.io.FixedLenFeature([],tf.string),\n    }\n    Features=tf.io.parse_single_example(EagerTensor,FeatureMap)\n    return tf.reshape(tf.image.decode_jpeg(Features['image']),(512,512,3))/255\n\ndef parseLabel(EagerTensor):\n    FeatureMap={\n    \"class\":tf.io.FixedLenFeature([], tf.int64),\n    }\n    Features=tf.io.parse_single_example(EagerTensor,FeatureMap)\n    return tf.one_hot(Features['class'],104)\n\ndef parseId(EagerTensor):\n    FeatureMap={\n    \"id\":tf.io.FixedLenFeature([], tf.string),\n    }\n    Features=tf.io.parse_single_example(EagerTensor,FeatureMap)\n    return Features['id']\n","metadata":{"execution":{"iopub.status.busy":"2021-08-21T17:02:42.687408Z","iopub.execute_input":"2021-08-21T17:02:42.687872Z","iopub.status.idle":"2021-08-21T17:02:42.696936Z","shell.execute_reply.started":"2021-08-21T17:02:42.687829Z","shell.execute_reply":"2021-08-21T17:02:42.695712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"plt.imshow(next(iter(Train.map(parseImage))))","metadata":{"execution":{"iopub.status.busy":"2021-08-21T02:17:20.355175Z","iopub.execute_input":"2021-08-21T02:17:20.355676Z","iopub.status.idle":"2021-08-21T02:17:22.852853Z","shell.execute_reply.started":"2021-08-21T02:17:20.355639Z","shell.execute_reply":"2021-08-21T02:17:22.8518Z"}}},{"cell_type":"markdown","source":"plt.imshow(next(iter(HighResolution.map(parse512Image))))","metadata":{"execution":{"iopub.status.busy":"2021-08-21T02:17:22.854494Z","iopub.execute_input":"2021-08-21T02:17:22.8548Z","iopub.status.idle":"2021-08-21T02:17:25.177085Z","shell.execute_reply.started":"2021-08-21T02:17:22.854771Z","shell.execute_reply":"2021-08-21T02:17:25.176045Z"}}},{"cell_type":"code","source":"\nwith strategy.scope():\n    Densenet201=tf.keras.applications.DenseNet201(\n    weights='imagenet',\n    include_top=False,\n    input_shape=(512,512,3)\n    )\n    Densenet201.trainalbe=True\n    for layer in Densenet201.layers[:100]:\n        layer.trainable=False\n\n    model=tf.keras.Sequential([\n        Densenet201,\n        layers.Dropout(0.2),\n        layers.GlobalAveragePooling2D(),\n        layers.Dense(104,activation='softmax')\n    ])\n    model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy',tfa.metrics.F1Score(104)])\n    \nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-08-21T17:02:42.698755Z","iopub.execute_input":"2021-08-21T17:02:42.699194Z","iopub.status.idle":"2021-08-21T17:03:19.333910Z","shell.execute_reply.started":"2021-08-21T17:02:42.699150Z","shell.execute_reply":"2021-08-21T17:03:19.332696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BATCHSIZE=16*strategy.num_replicas_in_sync\n\nTrainImage=FullTrain.map(parseImage)\nTrainLabel=FullTrain.map(parseLabel)\n\n# ValImage=Val.map(parseImage)\n# ValLabel=Val.map(parseLabel)\n\nTrainDS=tf.data.Dataset.zip((TrainImage,TrainLabel)).repeat().batch(BATCHSIZE).prefetch(AUTO)\n# ValDS=tf.data.Dataset.zip((ValImage,ValLabel)).batch(BATCHSIZE).prefetch(AUTO)","metadata":{"execution":{"iopub.status.busy":"2021-08-21T17:03:19.335395Z","iopub.execute_input":"2021-08-21T17:03:19.335984Z","iopub.status.idle":"2021-08-21T17:03:19.482070Z","shell.execute_reply.started":"2021-08-21T17:03:19.335937Z","shell.execute_reply":"2021-08-21T17:03:19.481024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ReduceLR = tf.keras.callbacks.ReduceLROnPlateau(monitor='accuracy', factor=0.2, patience=3,verbose=True)\n\nclass MyCallBack(tf.keras.callbacks.Callback):\n    def on_epoch_end(self,epoch,log):\n        if log['accuracy']>0.9995:\n            self.stop_training=True\n            \nStop=MyCallBack()","metadata":{"execution":{"iopub.status.busy":"2021-08-21T17:03:19.483750Z","iopub.execute_input":"2021-08-21T17:03:19.484051Z","iopub.status.idle":"2021-08-21T17:03:19.489208Z","shell.execute_reply.started":"2021-08-21T17:03:19.484021Z","shell.execute_reply":"2021-08-21T17:03:19.488520Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Epochs=20\nHistory=model.fit(TrainDS,epochs=Epochs,steps_per_epoch=temp[-1]//BATCHSIZE,callbacks=[ReduceLR,Stop],verbose=1)\n","metadata":{"execution":{"iopub.status.busy":"2021-08-21T14:14:32.904871Z","iopub.execute_input":"2021-08-21T14:14:32.90525Z","iopub.status.idle":"2021-08-21T14:40:49.466253Z","shell.execute_reply.started":"2021-08-21T14:14:32.905214Z","shell.execute_reply":"2021-08-21T14:40:49.464333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Loss=History.history['loss']\nAcc=History.history['accuracy']\nF1=History.history['f1_score']\n\n# ValLoss=History.history['val_loss']\n# ValAcc=History.history['val_accuracy']\n# ValF1=History.history['val_f1_score']\n","metadata":{"execution":{"iopub.status.busy":"2021-08-21T14:40:56.4321Z","iopub.execute_input":"2021-08-21T14:40:56.432454Z","iopub.status.idle":"2021-08-21T14:40:56.459414Z","shell.execute_reply.started":"2021-08-21T14:40:56.432423Z","shell.execute_reply":"2021-08-21T14:40:56.45789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig=make_subplots(rows=1, cols=3)\nfig.add_scatter(x=History.epoch,y=Acc,name='TrainAcc',row=1, col=1)\n# fig.add_scatter(x=History.epoch,y=ValAcc,name='ValAcc',row=1, col=1)\nfig.add_scatter(x=History.epoch,y=Loss,name='TrainLoss',row=1, col=2)\n# fig.add_scatter(x=History.epoch,y=ValLoss,name='ValLoss',row=1, col=2)\nfig.add_scatter(x=History.epoch,y=Loss,name='TrainF1',row=1, col=3)\n# fig.add_scatter(x=History.epoch,y=ValLoss,name='ValF1',row=1, col=3)\nfig.update_xaxes(title_text=\"Epoch\", row=1, col=1)\nfig.update_xaxes(title_text=\"Epoch\", row=1, col=2)\nfig.update_xaxes(title_text=\"Epoch\", row=1, col=3)\nfig.update_yaxes(title_text=\"Accuracy\", row=1, col=1)\nfig.update_yaxes(title_text=\"Loss\", row=1, col=2)\nfig.update_yaxes(title_text=\"F1 Score\", row=1, col=3)\n","metadata":{"execution":{"iopub.status.busy":"2021-08-21T13:36:37.78469Z","iopub.execute_input":"2021-08-21T13:36:37.785107Z","iopub.status.idle":"2021-08-21T13:36:37.859255Z","shell.execute_reply.started":"2021-08-21T13:36:37.785077Z","shell.execute_reply":"2021-08-21T13:36:37.858123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TestImage=Test.map(parseImage)\nTestId=Test.map(parseId)\nTestDS=tf.data.Dataset.zip((TestImage)).batch(BATCHSIZE).prefetch(AUTO)\nresult=model.predict(TestDS)\n","metadata":{"execution":{"iopub.status.busy":"2021-08-21T17:03:26.420037Z","iopub.execute_input":"2021-08-21T17:03:26.420631Z","iopub.status.idle":"2021-08-21T17:03:58.227899Z","shell.execute_reply.started":"2021-08-21T17:03:26.420597Z","shell.execute_reply":"2021-08-21T17:03:58.226033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Id=[]\nfor i in TestId:\n    Id.append(str(i.numpy(),encoding='utf-8'))\n    ","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission=pd.read_csv(r'../input/tpu-getting-started/sample_submission.csv')\nsubmission.id=Id\nsubmission.label=np.argmax(result,axis=1)\nsubmission.to_csv(r'./submission.csv',index=False)\n","metadata":{"execution":{"iopub.status.busy":"2021-08-21T14:46:45.189601Z","iopub.execute_input":"2021-08-21T14:46:45.189955Z","iopub.status.idle":"2021-08-21T14:46:45.23122Z","shell.execute_reply.started":"2021-08-21T14:46:45.189925Z","shell.execute_reply":"2021-08-21T14:46:45.230225Z"},"trusted":true},"execution_count":null,"outputs":[]}]}