{"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 numpy as np\nimport pandas as pd\nimport tensorflow as tf\nimport tensorflow.keras as keras","metadata":{"execution":{"iopub.status.busy":"2022-07-17T10:30:21.166301Z","iopub.execute_input":"2022-07-17T10:30:21.167046Z","iopub.status.idle":"2022-07-17T10:30:27.940357Z","shell.execute_reply.started":"2022-07-17T10:30:21.166950Z","shell.execute_reply":"2022-07-17T10:30:27.939019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Import data / split**","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.layers.experimental.preprocessing import Rescaling\nrescale = Rescaling(scale=1.0/255)","metadata":{"execution":{"iopub.status.busy":"2022-07-17T10:30:27.942267Z","iopub.execute_input":"2022-07-17T10:30:27.942923Z","iopub.status.idle":"2022-07-17T10:30:27.985532Z","shell.execute_reply.started":"2022-07-17T10:30:27.942884Z","shell.execute_reply":"2022-07-17T10:30:27.984543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = tf.keras.utils.image_dataset_from_directory(\n    \"../input/xray-bodypart/train\",\n    labels='inferred',\n    label_mode='categorical',\n    color_mode='grayscale',\n    batch_size=32,\n    image_size=(256, 256),\n    shuffle=True,\n    seed=42,\n    validation_split=.2,\n    subset=\"training\"\n)\ntrain = train.map(lambda image,label:(rescale(image),label))","metadata":{"execution":{"iopub.status.busy":"2022-07-17T10:30:27.986612Z","iopub.execute_input":"2022-07-17T10:30:27.986955Z","iopub.status.idle":"2022-07-17T10:30:31.281633Z","shell.execute_reply.started":"2022-07-17T10:30:27.986919Z","shell.execute_reply":"2022-07-17T10:30:31.280705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val = tf.keras.utils.image_dataset_from_directory(\n    \"../input/xray-bodypart/train\",\n    labels='inferred',\n    label_mode='categorical',\n    color_mode='grayscale',\n    batch_size=32,\n    image_size=(256, 256),\n    shuffle=True,\n    seed=42,\n    validation_split=.2,\n    subset=\"validation\"\n)\nval = val.map(lambda image,label:(rescale(image),label))","metadata":{"execution":{"iopub.status.busy":"2022-07-17T10:30:31.284385Z","iopub.execute_input":"2022-07-17T10:30:31.284734Z","iopub.status.idle":"2022-07-17T10:30:31.418359Z","shell.execute_reply.started":"2022-07-17T10:30:31.284699Z","shell.execute_reply":"2022-07-17T10:30:31.417387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow.keras.layers as layers\n\ndata_augmentation = keras.Sequential(\n  [\n    #layers.experimental.preprocessing.Rescaling(1./255),\n    layers.experimental.preprocessing.RandomFlip(\"horizontal\", \n                         input_shape=(256,256,1)),\n    layers.experimental.preprocessing.RandomRotation(0.1),\n    layers.experimental.preprocessing.RandomZoom(0.1),\n    tf.keras.layers.experimental.preprocessing.RandomContrast(factor=(0.8, 1.4))\n  ]\n)","metadata":{"execution":{"iopub.status.busy":"2022-07-17T10:30:31.419893Z","iopub.execute_input":"2022-07-17T10:30:31.420244Z","iopub.status.idle":"2022-07-17T10:30:31.568026Z","shell.execute_reply.started":"2022-07-17T10:30:31.420208Z","shell.execute_reply":"2022-07-17T10:30:31.567112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n#class_names = train.class_names\nplt.figure(figsize=(10, 10))\nfor images, labels in train.take(1):\n    for i in range(9):\n        augmented_images = data_augmentation(images)*255\n        ax = plt.subplot(3, 3, i + 1)\n        plt.imshow(augmented_images[i].numpy().astype(\"uint8\"))\n        #plt.title(class_names[labels[i]])\n        #plt.title(labels[i])\n        plt.axis(\"off\")","metadata":{"execution":{"iopub.status.busy":"2022-07-17T10:30:31.570437Z","iopub.execute_input":"2022-07-17T10:30:31.571197Z","iopub.status.idle":"2022-07-17T10:30:33.257994Z","shell.execute_reply.started":"2022-07-17T10:30:31.571133Z","shell.execute_reply":"2022-07-17T10:30:33.257144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Make model**","metadata":{}},{"cell_type":"code","source":"model = tf.keras.applications.efficientnet.EfficientNetB3(\n    include_top=True,\n    weights=None,\n    input_shape=(256,256,1),\n    classes=22,\n    classifier_activation='softmax'\n)\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-07-17T10:30:33.258985Z","iopub.execute_input":"2022-07-17T10:30:33.259309Z","iopub.status.idle":"2022-07-17T10:30:35.380758Z","shell.execute_reply.started":"2022-07-17T10:30:33.259278Z","shell.execute_reply":"2022-07-17T10:30:35.379752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow_addons as tfa\nf1m = tfa.metrics.F1Score(\n    num_classes= 22,\n    average= \"micro\",\n    name = 'f1_micro',\n)\nf1M = tfa.metrics.F1Score(\n    num_classes= 22,\n    average= \"macro\",\n    name = 'f1_macro',\n)\nmodel.compile(optimizer=\"adam\",metrics=[f1m,f1M,\"accuracy\"],loss=\"categorical_crossentropy\")","metadata":{"execution":{"iopub.status.busy":"2022-07-17T10:30:35.382508Z","iopub.execute_input":"2022-07-17T10:30:35.383143Z","iopub.status.idle":"2022-07-17T10:30:35.547131Z","shell.execute_reply.started":"2022-07-17T10:30:35.383107Z","shell.execute_reply":"2022-07-17T10:30:35.546218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Training**","metadata":{}},{"cell_type":"code","source":"earlystopping = tf.keras.callbacks.EarlyStopping(\n    monitor='val_loss',\n    patience=25,\n    verbose=1,\n    mode='auto',\n    restore_best_weights=True\n)","metadata":{"execution":{"iopub.status.busy":"2022-07-17T10:30:35.548716Z","iopub.execute_input":"2022-07-17T10:30:35.549044Z","iopub.status.idle":"2022-07-17T10:30:35.555048Z","shell.execute_reply.started":"2022-07-17T10:30:35.549009Z","shell.execute_reply":"2022-07-17T10:30:35.554168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(train,validation_data=val,epochs=200,callbacks=[earlystopping])","metadata":{"execution":{"iopub.status.busy":"2022-07-17T10:30:35.559147Z","iopub.execute_input":"2022-07-17T10:30:35.559571Z","iopub.status.idle":"2022-07-17T10:31:37.568809Z","shell.execute_reply.started":"2022-07-17T10:30:35.559529Z","shell.execute_reply":"2022-07-17T10:31:37.566745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Prediction**","metadata":{}},{"cell_type":"code","source":"test = tf.keras.utils.image_dataset_from_directory(\n    \"../input/xray-bodypart/test\",\n    labels=None,\n    color_mode='grayscale',\n    batch_size=32,\n    image_size=(256, 256),\n    shuffle=False,\n    seed=42,\n)\nlist_path = test.file_paths\nindex = [int(x.split(\"/\")[-1].split(\".\")[0]) for x in list_path]\ntest = test.map(lambda image:(rescale(image)))","metadata":{"execution":{"iopub.status.busy":"2022-07-17T10:31:42.526332Z","iopub.execute_input":"2022-07-17T10:31:42.526714Z","iopub.status.idle":"2022-07-17T10:31:42.774622Z","shell.execute_reply.started":"2022-07-17T10:31:42.526677Z","shell.execute_reply":"2022-07-17T10:31:42.773700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction = model.predict(test)\npred = np.argmax(prediction,axis=-1)\npred_right_index = np.zeros_like(pred)\nfor i,idx in enumerate(index):\n    pred_right_index[idx] = pred[i]\npred_right_index","metadata":{"execution":{"iopub.status.busy":"2022-07-17T10:31:43.116160Z","iopub.execute_input":"2022-07-17T10:31:43.117208Z","iopub.status.idle":"2022-07-17T10:31:47.009700Z","shell.execute_reply.started":"2022-07-17T10:31:43.117136Z","shell.execute_reply":"2022-07-17T10:31:47.008702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Submission**","metadata":{}},{"cell_type":"code","source":"test_ds = pd.read_csv(\"../input/unifesp-x-ray-body-part-classifier/sample_submission.csv\")\ntest_ds.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-17T10:31:47.011746Z","iopub.execute_input":"2022-07-17T10:31:47.012353Z","iopub.status.idle":"2022-07-17T10:31:47.040277Z","shell.execute_reply.started":"2022-07-17T10:31:47.012314Z","shell.execute_reply":"2022-07-17T10:31:47.039237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_ds[\"Target\"] = pred_right_index\ntest_ds.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-17T10:31:49.298049Z","iopub.execute_input":"2022-07-17T10:31:49.299488Z","iopub.status.idle":"2022-07-17T10:31:49.312780Z","shell.execute_reply.started":"2022-07-17T10:31:49.299437Z","shell.execute_reply":"2022-07-17T10:31:49.311577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_ds.to_csv(\"./submission.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-17T10:31:50.381265Z","iopub.execute_input":"2022-07-17T10:31:50.382294Z","iopub.status.idle":"2022-07-17T10:31:50.393260Z","shell.execute_reply.started":"2022-07-17T10:31:50.382250Z","shell.execute_reply":"2022-07-17T10:31:50.392228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}