{"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 import keras\nimport keras.layers as layers\nimport os\nimport matplotlib.pyplot as plt\nimport glob\nimport numpy as np","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-11-02T19:52:20.581326Z","iopub.execute_input":"2021-11-02T19:52:20.581622Z","iopub.status.idle":"2021-11-02T19:52:20.587177Z","shell.execute_reply.started":"2021-11-02T19:52:20.581591Z","shell.execute_reply":"2021-11-02T19:52:20.586029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"try: # detect TPUs\n    tpu = tf.distribute.cluster_resolver.TPUClusterResolver.connect() # TPU detection\n    strategy = tf.distribute.TPUStrategy(tpu)\nexcept ValueError: # detect GPUs\n    strategy = tf.distribute.get_strategy() # default strategy that works on CPU and single GPU\nprint(\"Number of accelerators: \", strategy.num_replicas_in_sync)","metadata":{"execution":{"iopub.status.busy":"2021-11-02T19:00:06.190434Z","iopub.execute_input":"2021-11-02T19:00:06.191628Z","iopub.status.idle":"2021-11-02T19:00:06.202973Z","shell.execute_reply.started":"2021-11-02T19:00:06.191584Z","shell.execute_reply":"2021-11-02T19:00:06.201518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"strategy","metadata":{"execution":{"iopub.status.busy":"2021-11-02T19:00:06.205111Z","iopub.execute_input":"2021-11-02T19:00:06.205813Z","iopub.status.idle":"2021-11-02T19:00:06.220378Z","shell.execute_reply.started":"2021-11-02T19:00:06.205769Z","shell.execute_reply":"2021-11-02T19:00:06.219282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TRAIN_FILES = glob.glob('../input/tpu-getting-started/tfrecords-jpeg-512x512/train/*.tfrec')\nVAL_FILES = glob.glob('../input/tpu-getting-started/tfrecords-jpeg-512x512/val/*.tfrec')\nTEST_FILES = glob.glob('../input/tpu-getting-started/tfrecords-jpeg-512x512/test/*.tfrec')","metadata":{"execution":{"iopub.status.busy":"2021-11-02T19:00:06.223313Z","iopub.execute_input":"2021-11-02T19:00:06.224223Z","iopub.status.idle":"2021-11-02T19:00:06.243825Z","shell.execute_reply.started":"2021-11-02T19:00:06.224179Z","shell.execute_reply":"2021-11-02T19:00:06.242900Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def decode_image(raw_image):\n    image = tf.image.decode_jpeg(raw_image,channels = 3)\n    image = tf.cast(image,dtype = tf.float64)   \n    return image\n\ndef parse_labeled(item):\n    feature_desc = {\n        'class':tf.io.FixedLenFeature([],tf.int64),\n        'image':tf.io.FixedLenFeature([],tf.string)\n    }\n    parsed_example = tf.io.parse_single_example(item,feature_desc)\n    image = decode_image(parsed_example['image'])\n    return image,parsed_example['class']\n\ndef parse_unlabeled(item):\n    feature_desc = {\n        'id':tf.io.FixedLenFeature([],tf.string),\n        'image':tf.io.FixedLenFeature([],tf.string)\n    }\n    parsed_example = tf.io.parse_single_example(item,feature_desc)\n    image = decode_image(parsed_example['image'])\n    return [parsed_example['id']],image\n    ","metadata":{"execution":{"iopub.status.busy":"2021-11-02T19:56:47.681949Z","iopub.execute_input":"2021-11-02T19:56:47.682256Z","iopub.status.idle":"2021-11-02T19:56:47.690942Z","shell.execute_reply.started":"2021-11-02T19:56:47.682226Z","shell.execute_reply":"2021-11-02T19:56:47.689968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ds_unparsed = tf.data.TFRecordDataset(TRAIN_FILES)\nval_ds_unparsed = tf.data.TFRecordDataset(VAL_FILES)\ntest_ds_unparsed = tf.data.TFRecordDataset(TEST_FILES)","metadata":{"execution":{"iopub.status.busy":"2021-11-02T19:00:06.261375Z","iopub.execute_input":"2021-11-02T19:00:06.262261Z","iopub.status.idle":"2021-11-02T19:00:06.294562Z","shell.execute_reply.started":"2021-11-02T19:00:06.262121Z","shell.execute_reply":"2021-11-02T19:00:06.293250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"AUTO = tf.data.experimental.AUTOTUNE\ntrain_ds = train_ds_unparsed.map(parse_labeled,AUTO).batch(32).prefetch(AUTO)\nval_ds = val_ds_unparsed.map(parse_labeled,AUTO).batch(32).prefetch(AUTO)","metadata":{"execution":{"iopub.status.busy":"2021-11-02T19:00:06.296487Z","iopub.execute_input":"2021-11-02T19:00:06.297084Z","iopub.status.idle":"2021-11-02T19:00:06.432690Z","shell.execute_reply.started":"2021-11-02T19:00:06.296998Z","shell.execute_reply":"2021-11-02T19:00:06.431578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preprocess = keras.applications.resnet.preprocess_input\nresnet_head = keras.applications.ResNet50(\n    include_top=False,\n    weights=\"imagenet\",\n    input_shape=(512,512,3)\n)\nresnet_head.trainable = False","metadata":{"execution":{"iopub.status.busy":"2021-11-02T19:00:06.434600Z","iopub.execute_input":"2021-11-02T19:00:06.434919Z","iopub.status.idle":"2021-11-02T19:00:08.180250Z","shell.execute_reply.started":"2021-11-02T19:00:06.434877Z","shell.execute_reply":"2021-11-02T19:00:08.179235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"early_stopping = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=5)\nwith strategy.scope():\n    inp = keras.Input(shape = (512,512,3),dtype = tf.float64)\n    x = preprocess(inp)\n    x = resnet_head(x)\n    x = layers.GlobalAveragePooling2D()(x)\n    x = layers.Dense(104,activation = 'softmax')(x)\n    model = keras.Model(inp,x)\nmodel.compile(optimizer = 'adam',\n              loss = 'sparse_categorical_crossentropy',\n              metrics = ['sparse_categorical_accuracy']\n            )\n","metadata":{"execution":{"iopub.status.busy":"2021-11-02T19:03:57.363883Z","iopub.execute_input":"2021-11-02T19:03:57.364203Z","iopub.status.idle":"2021-11-02T19:03:58.356636Z","shell.execute_reply.started":"2021-11-02T19:03:57.364172Z","shell.execute_reply":"2021-11-02T19:03:58.355712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"keras.utils.plot_model(model,show_shapes = True)","metadata":{"execution":{"iopub.status.busy":"2021-11-02T19:04:00.625301Z","iopub.execute_input":"2021-11-02T19:04:00.625665Z","iopub.status.idle":"2021-11-02T19:04:00.849773Z","shell.execute_reply.started":"2021-11-02T19:04:00.625635Z","shell.execute_reply":"2021-11-02T19:04:00.848802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(\n    train_ds,\n    validation_data = val_ds,\n    epochs = 30,\n    callbacks = [early_stopping]\n)","metadata":{"execution":{"iopub.status.busy":"2021-11-02T19:04:06.412733Z","iopub.execute_input":"2021-11-02T19:04:06.413131Z","iopub.status.idle":"2021-11-02T19:46:50.035346Z","shell.execute_reply.started":"2021-11-02T19:04:06.413088Z","shell.execute_reply":"2021-11-02T19:46:50.033094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_ds = test_ds_unparsed.map(parse_unlabeled,AUTO)","metadata":{"execution":{"iopub.status.busy":"2021-11-02T19:56:55.942453Z","iopub.execute_input":"2021-11-02T19:56:55.942836Z","iopub.status.idle":"2021-11-02T19:56:56.060806Z","shell.execute_reply.started":"2021-11-02T19:56:55.942767Z","shell.execute_reply":"2021-11-02T19:56:56.059824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_images_ds = test_ds.map(lambda idnum,image: image).batch(1)\nprobabilities = model.predict(test_images_ds)\npredictions = np.argmax(probabilities, axis=-1)","metadata":{"execution":{"iopub.status.busy":"2021-11-02T19:53:36.558041Z","iopub.execute_input":"2021-11-02T19:53:36.558350Z","iopub.status.idle":"2021-11-02T19:55:59.512374Z","shell.execute_reply.started":"2021-11-02T19:53:36.558305Z","shell.execute_reply":"2021-11-02T19:55:59.511302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_ids_ds = test_ds.map(lambda idnum,image: idnum).unbatch()\ntest_ids = next(iter(test_ids_ds.batch(7382))).numpy().astype('U')\n\n\nnp.savetxt(\n    'submission.csv',\n    np.rec.fromarrays([test_ids, predictions]),\n    fmt=['%s', '%d'],\n    delimiter=',',\n    header='id,label',\n    comments='',\n)","metadata":{"execution":{"iopub.status.busy":"2021-11-02T19:56:59.366971Z","iopub.execute_input":"2021-11-02T19:56:59.367234Z","iopub.status.idle":"2021-11-02T19:57:23.702562Z","shell.execute_reply.started":"2021-11-02T19:56:59.367205Z","shell.execute_reply":"2021-11-02T19:57:23.701616Z"},"trusted":true},"execution_count":null,"outputs":[]}]}