{"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":"markdown","source":"## Importing Libraries","metadata":{}},{"cell_type":"code","source":"from kaggle_datasets import KaggleDatasets\n\nimport cv2\nimport json\nimport datetime\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport tensorflow as tf\nimport matplotlib.pyplot as plt\n\nfrom sklearn.metrics import accuracy_score\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import MultiLabelBinarizer","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:42:28.982627Z","iopub.execute_input":"2021-05-20T13:42:28.983091Z","iopub.status.idle":"2021-05-20T13:42:36.144768Z","shell.execute_reply.started":"2021-05-20T13:42:28.982984Z","shell.execute_reply":"2021-05-20T13:42:36.143404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Initializing the validating the allocation of TPU (And the no of replicas)","metadata":{}},{"cell_type":"code","source":"try:\n    tpu = tf.distribute.cluster_resolver.TPUClusterResolver()\n    tf.config.experimental_connect_to_cluster(tpu)\n    tf.tpu.experimental.initialize_tpu_system(tpu)\n    strategy = tf.distribute.experimental.TPUStrategy(tpu)\n    print(\"Running on TPU:\", tpu.master())\n    \nexcept ValueError:\n    strategy = tf.distribute.get_strategy()\n    \nprint(f\"Running on {strategy.num_replicas_in_sync} replicas\")","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:42:36.146507Z","iopub.execute_input":"2021-05-20T13:42:36.146836Z","iopub.status.idle":"2021-05-20T13:42:42.313693Z","shell.execute_reply.started":"2021-05-20T13:42:36.146804Z","shell.execute_reply":"2021-05-20T13:42:42.312396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Functions for minor Data Augmentations and Image Resizing","metadata":{}},{"cell_type":"code","source":"def decode_image(path, label = None, target_size = (512, 512)):\n    \n    img = tf.image.decode_jpeg(tf.io.read_file(path), channels = 3)\n    img = tf.cast(img, tf.float32) / 255.0\n    img = tf.image.resize_with_pad(img, target_width=target_size[1], target_height=target_size[0])\n    \n    output_path = label['decoder_output']\n    output_img = tf.image.decode_jpeg(tf.io.read_file(output_path), channels = 3)\n    output_img = tf.cast(output_img, tf.float32) / 255.0\n    output_img = tf.image.resize_with_pad(output_img, target_width=546, target_height=546)\n    output_img = tf.image.per_image_standardization(output_img)\n    label['decoder_output'] = output_img\n    return img if label is None else img, label\n\ndef data_augment(img, label = None):\n    \n    img = tf.image.random_flip_left_right(img)\n    img = tf.image.random_flip_up_down(img)\n    #img = tf.image.random_brightness(img, max_delta = 0.5)\n    img = tf.image.random_crop(img, size = [512, 512, 3])\n\n    return img if label is None else img, label","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:42:42.315731Z","iopub.execute_input":"2021-05-20T13:42:42.316588Z","iopub.status.idle":"2021-05-20T13:42:42.328037Z","shell.execute_reply.started":"2021-05-20T13:42:42.316468Z","shell.execute_reply":"2021-05-20T13:42:42.326896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## TPU-specific Hyperparameters","metadata":{}},{"cell_type":"code","source":"#Get your credentials\nfrom kaggle_secrets import UserSecretsClient\nuser_secrets = UserSecretsClient()\nuser_credential = user_secrets.get_gcloud_credential()\n\n#Set the credentials\nuser_secrets.set_tensorflow_credential(user_credential)","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:42:42.329649Z","iopub.execute_input":"2021-05-20T13:42:42.329979Z","iopub.status.idle":"2021-05-20T13:42:42.550547Z","shell.execute_reply.started":"2021-05-20T13:42:42.329947Z","shell.execute_reply":"2021-05-20T13:42:42.549502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"AUTO = tf.data.experimental.AUTOTUNE\nBATCH_SIZE = strategy.num_replicas_in_sync * 16\nGCS_DS_PATH = KaggleDatasets().get_gcs_path('plant-pathology-2021-fgvc8')\n#GCS_DS_PATH = KaggleDatasets().get_gcs_path('m1-aug-final')","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:42:42.552882Z","iopub.execute_input":"2021-05-20T13:42:42.553183Z","iopub.status.idle":"2021-05-20T13:42:42.849428Z","shell.execute_reply.started":"2021-05-20T13:42:42.553156Z","shell.execute_reply":"2021-05-20T13:42:42.848360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Loading the data","metadata":{}},{"cell_type":"code","source":"load_dir = \"../input/plant-pathology-2021-fgvc8/\"\ndf = pd.read_csv(load_dir + 'train.csv')\ndf['paths'] = GCS_DS_PATH + \"/train_images/\" + df.image#\"../input/plant-pathology-2021-fgvc8/train_images/\" + df.image#G\nsub_df = pd.read_csv(load_dir + 'sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:42:42.850694Z","iopub.execute_input":"2021-05-20T13:42:42.850977Z","iopub.status.idle":"2021-05-20T13:42:43.098536Z","shell.execute_reply.started":"2021-05-20T13:42:42.850950Z","shell.execute_reply":"2021-05-20T13:42:43.097488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['labels'] = df['labels'].apply(lambda x: x.split(\" \"))\ndf['complex'] = df['labels']\ndf['complex'] = df['complex'].apply(lambda x: 1 if 'complex' in x else 0)","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:42:43.099728Z","iopub.execute_input":"2021-05-20T13:42:43.099997Z","iopub.status.idle":"2021-05-20T13:42:43.133484Z","shell.execute_reply.started":"2021-05-20T13:42:43.099971Z","shell.execute_reply":"2021-05-20T13:42:43.132407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scab_counter = 0\nhealthy_counter = 0\nfgls_counter = 0\nrust_counter = 0\npowdery_mildew_counter = 0\nfor i in df.index:\n    if 'scab' in df['labels'][i]:\n        scab_counter+=1\n    if 'healthy' in df['labels'][i]:\n        healthy_counter+=1\n    if 'frog_eye_leaf_spot' in df['labels'][i]:\n        fgls_counter+=1\n    if 'rust' in df['labels'][i]:\n        rust_counter+=1\n    if 'powdery_mildew' in df['labels'][i]:\n        powdery_mildew_counter+=1\n        \nprint(\"Scab -> {}\".format(scab_counter))\nprint(\"Healthy -> {}\".format(healthy_counter))\nprint(\"Grog Eye Leaf Spot -> {}\".format(fgls_counter))\nprint(\"Rust -> {}\".format(rust_counter))\nprint(\"Powdery Mildew -> {}\".format(powdery_mildew_counter))","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:42:43.135384Z","iopub.execute_input":"2021-05-20T13:42:43.135678Z","iopub.status.idle":"2021-05-20T13:42:43.911396Z","shell.execute_reply.started":"2021-05-20T13:42:43.135650Z","shell.execute_reply":"2021-05-20T13:42:43.910557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scab_weight = len(df['labels']) / (6 * scab_counter)\nhealthy_weight = len(df['labels']) / (6 * healthy_counter)\nfgls_weight = len(df['labels']) / (6 * fgls_counter)\nrust_weight = len(df['labels']) / (6 * rust_counter)\npowder_mildew_weight = len(df['labels']) / (6 * powdery_mildew_counter)\n\n\nn_scab_weight = len(df['labels']) / (6 * (len(df['labels']) - scab_counter))\nn_healthy_weight = len(df['labels']) / (6 * (len(df['labels']) - healthy_counter))\nn_fgls_weight = len(df['labels']) / (6 * (len(df['labels']) - fgls_counter))\nn_rust_weight = len(df['labels']) / (6 * (len(df['labels']) - rust_counter))\nn_powder_mildew_weight = len(df['labels']) / (6 * (len(df['labels']) - powdery_mildew_counter))","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:42:43.913174Z","iopub.execute_input":"2021-05-20T13:42:43.913530Z","iopub.status.idle":"2021-05-20T13:42:43.922565Z","shell.execute_reply.started":"2021-05-20T13:42:43.913497Z","shell.execute_reply":"2021-05-20T13:42:43.921470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in df.index:\n    if 'complex' in df['labels'][i]:\n        df['labels'][i].remove('complex')","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:42:43.924316Z","iopub.execute_input":"2021-05-20T13:42:43.924743Z","iopub.status.idle":"2021-05-20T13:42:44.114738Z","shell.execute_reply.started":"2021-05-20T13:42:43.924697Z","shell.execute_reply":"2021-05-20T13:42:44.113739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Encoding Labels","metadata":{}},{"cell_type":"code","source":"mlb = MultiLabelBinarizer()\ndf['class_labels'] = list(mlb.fit_transform(df['labels']))\nmlb.classes_","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:42:45.654148Z","iopub.execute_input":"2021-05-20T13:42:45.654520Z","iopub.status.idle":"2021-05-20T13:42:45.696311Z","shell.execute_reply.started":"2021-05-20T13:42:45.654490Z","shell.execute_reply":"2021-05-20T13:42:45.695173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_weights = {'positive_weights' : {0: fgls_weight,\n                                       1: healthy_weight,\n                                       2: powder_mildew_weight,\n                                       3: rust_weight,\n                                       4: scab_weight},\n                'negative_weights': {0: n_fgls_weight,\n                                     1: n_healthy_weight,\n                                     2: n_powder_mildew_weight,\n                                     3: n_rust_weight,\n                                     4: n_scab_weight}}\n\nprint(class_weights)","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:42:46.221754Z","iopub.execute_input":"2021-05-20T13:42:46.222117Z","iopub.status.idle":"2021-05-20T13:42:46.228480Z","shell.execute_reply.started":"2021-05-20T13:42:46.222087Z","shell.execute_reply":"2021-05-20T13:42:46.227495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.utils import class_weight\nclass_weights_cmplx = class_weight.compute_class_weight('balanced',\n                                                 np.unique(df['complex']),\n                                                 df['complex'])\nclass_weights_cmplx","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:42:47.290200Z","iopub.execute_input":"2021-05-20T13:42:47.290594Z","iopub.status.idle":"2021-05-20T13:42:47.309148Z","shell.execute_reply.started":"2021-05-20T13:42:47.290561Z","shell.execute_reply":"2021-05-20T13:42:47.308060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_weights_complex = {'complex_output': {0: 0.56525696, 1: 4.33100883}}\ndf['class_labels'] = df['class_labels'].apply(lambda x: np.float32(x))","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:42:48.074925Z","iopub.execute_input":"2021-05-20T13:42:48.075311Z","iopub.status.idle":"2021-05-20T13:42:48.107207Z","shell.execute_reply.started":"2021-05-20T13:42:48.075281Z","shell.execute_reply":"2021-05-20T13:42:48.106113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:42:49.233301Z","iopub.execute_input":"2021-05-20T13:42:49.233661Z","iopub.status.idle":"2021-05-20T13:42:49.258410Z","shell.execute_reply.started":"2021-05-20T13:42:49.233630Z","shell.execute_reply":"2021-05-20T13:42:49.257313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in df.index:\n    if set(df['class_labels'][i]) == set([0.0, 0.0, 0.0, 0.0, 0.0]):\n        #print(df['labels'][i])","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:42:49.959882Z","iopub.execute_input":"2021-05-20T13:42:49.960252Z","iopub.status.idle":"2021-05-20T13:42:49.965897Z","shell.execute_reply.started":"2021-05-20T13:42:49.960222Z","shell.execute_reply":"2021-05-20T13:42:49.964806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Train Test Split","metadata":{}},{"cell_type":"code","source":"train_df, valid_df = train_test_split(df, test_size = 0.2, random_state = 1, stratify = df['labels'])\n\ntrain_dataset = (tf.data.Dataset\n                 .from_tensor_slices((train_df.paths, {\"other_disease_output\": list(train_df['class_labels']),\n                                                        \"complex_output\": list(train_df['complex']),\n                                                        \"decoder_output\": train_df.paths}))\n                 .map(decode_image, num_parallel_calls = AUTO).cache().repeat()\n                 #.map(data_augment, num_parallel_calls = AUTO).repeat()\n                 .shuffle(1024).batch(BATCH_SIZE).prefetch(AUTO))\n\nvalid_dataset = (tf.data.Dataset\n                 .from_tensor_slices((valid_df.paths, {\"other_disease_output\": list(valid_df['class_labels']),\n                                                        \"complex_output\": list(valid_df['complex']),\n                                                        \"decoder_output\": valid_df.paths}))\n                 .map(decode_image, num_parallel_calls=AUTO).cache().repeat()\n                 #.map(data_augment, num_parallel_calls = AUTO).repeat()\n                 .batch(BATCH_SIZE).prefetch(AUTO))","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:44:11.763855Z","iopub.execute_input":"2021-05-20T13:44:11.764229Z","iopub.status.idle":"2021-05-20T13:44:12.940542Z","shell.execute_reply.started":"2021-05-20T13:44:11.764198Z","shell.execute_reply":"2021-05-20T13:44:12.939370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show(objs, titles=None, structure='LR', figsize=(5, 5), clims=False, axes=False, xlabels=False, title_colors=False):\n    '''\n    utility function to show plots (subplots)\n    '''\n    plt.style.use('default')\n    plt.figure(figsize=figsize)\n    for i, obj in enumerate(objs):\n        if structure == 'LR': plt.subplot(1, len(objs), i+1)\n        elif structure == 'TB': plt.subplot(len(objs), 1, i+1)\n        plt.imshow(obj)\n        #\n        if axes: plt.axis(axes[i])\n        else: plt.axis('off')\n        #\n        if xlabels: plt.xlabel(xlabels[i])\n        #\n        if clims: plt.clim(clims[i])\n        #\n        if titles:\n            plt.title(titles[i], color=title_colors[i] if title_colors else 'teal')\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:44:14.673280Z","iopub.execute_input":"2021-05-20T13:44:14.673659Z","iopub.status.idle":"2021-05-20T13:44:14.682413Z","shell.execute_reply.started":"2021-05-20T13:44:14.673623Z","shell.execute_reply":"2021-05-20T13:44:14.681545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for x,y in train_dataset.as_numpy_iterator():\n    for i in range(5):\n        label_img = mlb.classes_[np.argmax(y['other_disease_output'][i])]\n        show(\n            [x[i], y['decoder_output'][i]],\n            [label_img, y['other_disease_output'][i]],\n            clims=[[0, 1], [0, 1]],\n            axes=['image', 'image'],\n            figsize=((10, 5))\n        )\n        #show(x[:5], titles = np.argmax(y['other_disease_output']), figsize=(20,5))\n    #show(y['decoder_output'][:5], figsize=(20,5))\n    break","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:44:15.661473Z","iopub.execute_input":"2021-05-20T13:44:15.662066Z","iopub.status.idle":"2021-05-20T13:45:27.916710Z","shell.execute_reply.started":"2021-05-20T13:44:15.662034Z","shell.execute_reply":"2021-05-20T13:45:27.915717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Build Model","metadata":{}},{"cell_type":"code","source":"#He Uniform Initializer for Dense Layer\nimport tensorflow as tf\ndef my_init(shape, dtype=None):\n    initializer = tf.keras.initializers.he_uniform(seed = 1)\n    return initializer(shape, dtype=dtype)\n\ndef conv2d(x, filters, filter_size, stride=(1,1), pad='same', name=None):\n    x = tf.keras.layers.Conv2D(filters, filter_size, strides = stride, padding=pad, activation = 'relu', name=name, kernel_initializer = my_init)(x)\n    x = tf.keras.layers.BatchNormalization(name = name+'_bn')(x)\n    #x = tf.keras.layers.Activation('relu', name = name+'_activation')(x)\n    return x","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:45:54.563871Z","iopub.execute_input":"2021-05-20T13:45:54.564428Z","iopub.status.idle":"2021-05-20T13:45:54.570600Z","shell.execute_reply.started":"2021-05-20T13:45:54.564385Z","shell.execute_reply":"2021-05-20T13:45:54.569850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def inception_b(x, name=None):\n    #Branch 0\n    # Conv2D(1,1) -> Conv2D(7,1) -> Conv2D(1,7) -> -> Conv2D(7,1) -> Conv2D(1,7)\n    branch0 = conv2d(x, 192, (1,1), name = name+'_b0_0')\n    branch0 = conv2d(branch0, 192, (7,1), name = name+'_b0_1')\n    branch0 = conv2d(branch0, 224, (1,7), name = name+'_b0_2')\n    branch0 = conv2d(branch0, 224, (7,1), name = name+'_b0_3')\n    branch0 = conv2d(branch0, 256, (1,7), name = name+'_b0_4')\n    \n    #Branch 1\n    # Conv2D(1,1) -> Conv2D(7,1) -> Conv2D(1,7)\n    branch1 = conv2d(x, 192, (1,1), name = name+'_b1_0')\n    branch1 = conv2d(branch1, 224, (7,1), name = name+'_b1_1')\n    branch1 = conv2d(branch1, 256, (1,7), name = name+'_b1_2')\n    \n    #Branch 2\n    # AvgPool(3,3) -> Conv2D(1,1)\n    branch2 = tf.keras.layers.AveragePooling2D((1,1), padding='same', name = name+'_b3_0')(x)\n    branch2 = conv2d(branch2, 128, (1,1), name = name+'_b3_1')\n    \n    #Branch 3\n    # Conv2D(1,1)\n    branch3 = conv2d(x, 384, (1,1), name = name+'_b4_0')\n    \n    #Concatenation\n    x = tf.keras.layers.Concatenate(axis=3, name = name+'_concat')([branch0, branch1, branch2, branch3])\n    \n    return x","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:45:55.065302Z","iopub.execute_input":"2021-05-20T13:45:55.065696Z","iopub.status.idle":"2021-05-20T13:45:55.076231Z","shell.execute_reply.started":"2021-05-20T13:45:55.065661Z","shell.execute_reply":"2021-05-20T13:45:55.075130Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def reduction_b(x, name=None):\n    #Branch 0\n    # Conv2D(1,1) -> Conv2D(1,7) -> Conv2D(7,1) -> -> Conv2D(3,3)\n    branch0 = conv2d(x, 256, (1,1), name = name+'_b0_0')\n    branch0 = conv2d(branch0, 256, (1,7), name = name+'_b0_1')\n    branch0 = conv2d(branch0, 320, (7,1), name = name+'_b0_2')\n    branch0 = conv2d(branch0, 320, (3,3), stride = (2,2), pad = 'valid', name = name+'_b0_3')\n    \n    #Branch 1\n    # Conv2D(1,1) -> Conv2D(3,3)\n    branch1 = conv2d(x, 192, (1,1), name = name+'_b1_0')\n    branch1 = conv2d(branch1, 192, (3,3), stride = (2,2), pad = 'valid', name = name+'_b1_1')\n    \n    #Branch 2\n    # AvgPool(3,3) -> Conv2D(1,1)\n    branch2 = tf.keras.layers.MaxPooling2D((3,3), strides = (2,2), padding='valid', name = name+'_b3_0')(x)\n\n    #Concatenation\n    x = tf.keras.layers.Concatenate(axis=3, name = name+'_concat')([branch0, branch1, branch2])\n    \n    return x","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:45:57.169902Z","iopub.execute_input":"2021-05-20T13:45:57.170291Z","iopub.status.idle":"2021-05-20T13:45:57.179917Z","shell.execute_reply.started":"2021-05-20T13:45:57.170244Z","shell.execute_reply":"2021-05-20T13:45:57.178769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_model_x(input_shape):\n    \n    x = tf.keras.applications.Xception(include_top = False, weights = 'imagenet', input_shape = input_shape)\n    \n    #Branch Complex\n    cmplex = inception_b(x.output, name = 'complex_inception_c')\n    cmplex = reduction_b(cmplex, name = 'complex_reduction')\n    cmplex_pool = tf.keras.layers.GlobalAveragePooling2D(name = 'complex_avg_pooling')(cmplex)\n    cmplex = tf.keras.layers.Dense(512, activation = 'relu', kernel_initializer = my_init, name = 'complex_dense0')(cmplex_pool)\n    cmplex = tf.keras.layers.Dense(16, activation = 'relu', kernel_initializer = my_init, name = 'complex_dense1')(cmplex)\n    cmplex = tf.keras.layers.Dense(1, activation = 'sigmoid', name = 'complex_output')(cmplex)\n    \n    #Branch Others\n    other_disease = reduction_b(x.output, name = 'other_disease_reduction')\n    other_disease = tf.keras.layers.GlobalAveragePooling2D(name = 'other_disease_avg_pooling')(other_disease)\n    other_disease = tf.keras.layers.Concatenate(axis  = 1, name = 'complex_other_disease_concat')([other_disease, cmplex_pool])\n    other_disease = tf.keras.layers.Dense(512, activation = 'relu', kernel_initializer = my_init, name = 'other_disease_dense0')(other_disease)\n    other_disease = tf.keras.layers.Dense(32, activation = 'relu', kernel_initializer = my_init, name = 'other_disease_dense1')(other_disease)\n    other_disease = tf.keras.layers.Dense(5, activation = 'sigmoid', name = 'other_disease_output')(other_disease)\n    \n    #Branch Decoder\n    #fgls = inception_c(x.output, name = 'fgls_inception_c')\n    #decoder = tf.keras.layers.Conv2DTranspose(1024, (3,3),  strides=2, activation='relu', padding='same')(x.output)\n    #decoder = tf.keras.layers.Conv2DTranspose(512, (3,3),  strides=2, activation='relu', padding='same')(decoder)\n    #decoder = tf.keras.layers.Conv2DTranspose(128, (3,3),  strides=2, activation='relu', padding='same')(decoder)\n    #decoder = tf.keras.layers.Conv2DTranspose(16, (3,3),  strides=2, activation='relu', padding='same')(decoder)\n    #decoder = tf.keras.layers.Conv2DTranspose(3, (3,3),  strides=2, activation='sigmoid', padding='same', name = 'decoder_output')(decoder)\n    # Decoder\n    dec = tf.keras.layers.Conv2D(168, (1, 1), activation='relu', kernel_initializer = my_init)(x.output)\n    dec = tf.keras.layers.Conv2DTranspose(144, (3, 3), strides=(2, 2), activation='relu', kernel_initializer = my_init)(dec)\n    dec = tf.keras.layers.Conv2DTranspose(144, (3, 3), strides=(2, 2), activation='relu', kernel_initializer = my_init)(dec)\n    dec = tf.keras.layers.Conv2DTranspose(112, (3, 3), strides=(2, 2), activation='relu', kernel_initializer = my_init)(dec)\n    dec = tf.keras.layers.Conv2DTranspose(112, (3, 3), strides=(2, 2), activation='relu', kernel_initializer = my_init)(dec)\n    dec = tf.keras.layers.Conv2DTranspose(56, (3, 3), strides=(2, 2), activation='relu', kernel_initializer = my_init)(dec)\n    dec = tf.keras.layers.Conv2DTranspose(28, (3, 3), activation='relu', kernel_initializer = my_init)(dec)\n    dec = tf.keras.layers.Conv2DTranspose(14, (2, 2), activation='relu', kernel_initializer = my_init)(dec)\n    dec = tf.keras.layers.Conv2DTranspose(3, (1, 1), activation='tanh', name='decoder_output')(dec)\n    \n    model = tf.keras.models.Model(inputs = x.input, outputs = [other_disease, cmplex, dec])\n    return model","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:49:06.854483Z","iopub.execute_input":"2021-05-20T13:49:06.854861Z","iopub.status.idle":"2021-05-20T13:49:06.873699Z","shell.execute_reply.started":"2021-05-20T13:49:06.854830Z","shell.execute_reply":"2021-05-20T13:49:06.872387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with strategy.scope():\n    model = get_model_x((512, 512, 3))\n#model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:49:09.338852Z","iopub.execute_input":"2021-05-20T13:49:09.339198Z","iopub.status.idle":"2021-05-20T13:49:18.856443Z","shell.execute_reply.started":"2021-05-20T13:49:09.339168Z","shell.execute_reply":"2021-05-20T13:49:18.855214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#tf.keras.utils.plot_model(model, expand_nested=True)","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:46:19.435563Z","iopub.execute_input":"2021-05-20T13:46:19.435946Z","iopub.status.idle":"2021-05-20T13:46:19.440684Z","shell.execute_reply.started":"2021-05-20T13:46:19.435916Z","shell.execute_reply":"2021-05-20T13:46:19.439338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def custom_loss_(x_labels, y_labels):\n        _mse_loss = tf.keras.losses.mean_squared_error(x_labels, y_labels)\n        _mlse_loss = tf.keras.losses.mean_squared_logarithmic_error(x_labels, y_labels)\n        _mae_loss = tf.keras.losses.mean_absolute_error(x_labels, y_labels)\n        return 1. * _mse_loss + .1 * _mae_loss  + .1 * _mlse_loss","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:46:20.592375Z","iopub.execute_input":"2021-05-20T13:46:20.592733Z","iopub.status.idle":"2021-05-20T13:46:20.598567Z","shell.execute_reply.started":"2021-05-20T13:46:20.592700Z","shell.execute_reply":"2021-05-20T13:46:20.597398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras import backend as K\n\nWp = class_weights['positive_weights']\nWn = class_weights['negative_weights']\n\ndef custom_loss(y_true, y_logit):\n    '''\n    Multi-label cross-entropy\n    * Required \"Wp\", \"Wn\" as positive & negative class-weights\n    y_true: true value\n    y_logit: predicted value\n    '''\n    loss = 0.#float(0)\n    \n    for i, key in enumerate(Wp.keys()):\n        first_term = Wp[key] * y_true[i] * K.log(y_logit[i] + K.epsilon())\n        second_term = Wn[key] * (1 - y_true[i]) * K.log(1 - y_logit[i] + K.epsilon())\n        loss -= (first_term + second_term)\n    return loss","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:46:21.861000Z","iopub.execute_input":"2021-05-20T13:46:21.861410Z","iopub.status.idle":"2021-05-20T13:46:21.868778Z","shell.execute_reply.started":"2021-05-20T13:46:21.861374Z","shell.execute_reply":"2021-05-20T13:46:21.867604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow_addons.metrics.f_scores import F1Score, FBetaScore\nwith strategy.scope():\n    model.compile(loss = {\"other_disease_output\": custom_loss, #tf.keras.losses.BinaryCrossentropy(), \n                          \"complex_output\": tf.keras.losses.BinaryCrossentropy(), \n                          \"decoder_output\": tf.keras.losses.MeanAbsoluteError()},\n                  loss_weights = {\"other_disease_output\": 1.0, #tf.keras.losses.BinaryCrossentropy(), \n                          \"complex_output\": 0.5, \n                          \"decoder_output\": 5.0},\n                  optimizer = tf.keras.optimizers.Adam(lr = 0.001, decay = 1e-5),\n                  metrics = {\"other_disease_output\": ['acc', F1Score(len(mlb.classes_))], #tf.keras.losses.BinaryCrossentropy(), \n                          \"complex_output\": ['acc', F1Score(1)]})\n\ncheckpoint = tf.keras.callbacks.ModelCheckpoint('custom.h5', save_best_only = True, monitor = 'val_loss', mode = 'min')\nearly_stop = tf.keras.callbacks.EarlyStopping(monitor = 'val_loss', min_delta = 0.0001, patience = 3, mode = 'min', verbose = 1, restore_best_weights = True)\nreduce_lr = tf.keras.callbacks.ReduceLROnPlateau(monitor = 'val_loss', factor = 0.3, patience = 1, min_delta = 0.001, mode = 'min')\n\nsteps_per_epoch = train_df.shape[0] // BATCH_SIZE\nvalid_steps = valid_df.shape[0] //BATCH_SIZE","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:52:49.904662Z","iopub.execute_input":"2021-05-20T13:52:49.905025Z","iopub.status.idle":"2021-05-20T13:52:50.581183Z","shell.execute_reply.started":"2021-05-20T13:52:49.904994Z","shell.execute_reply":"2021-05-20T13:52:50.580193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"h = model.fit(train_dataset, \n              epochs = 15, \n              validation_data = valid_dataset, \n              callbacks = [checkpoint, early_stop, reduce_lr], \n              steps_per_epoch = steps_per_epoch, \n              validation_steps = valid_steps)","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:52:52.092758Z","iopub.execute_input":"2021-05-20T13:52:52.093119Z","iopub.status.idle":"2021-05-20T14:36:23.056283Z","shell.execute_reply.started":"2021-05-20T13:52:52.093086Z","shell.execute_reply":"2021-05-20T14:36:23.055105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('decoder_multi_label_v5.h5')","metadata":{"execution":{"iopub.status.busy":"2021-05-20T14:36:27.209432Z","iopub.execute_input":"2021-05-20T14:36:27.209853Z","iopub.status.idle":"2021-05-20T14:36:32.033071Z","shell.execute_reply.started":"2021-05-20T14:36:27.209819Z","shell.execute_reply":"2021-05-20T14:36:32.031891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Visualising Learning Plots","metadata":{}},{"cell_type":"code","source":"plt.style.use(\"ggplot\")\nplt.figure()\n\nplt.plot(h.history[\"loss\"][1:], label=\"train_loss\")\nplt.plot(h.history[\"val_loss\"][1:], label=\"val_loss\")\nplt.title(\"Loss\")\nplt.xlabel(\"Epoch\")\nplt.ylabel(\"Loss\")\nplt.legend(loc=\"upper left\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-05-20T14:36:33.693407Z","iopub.execute_input":"2021-05-20T14:36:33.693817Z","iopub.status.idle":"2021-05-20T14:36:33.889259Z","shell.execute_reply.started":"2021-05-20T14:36:33.693780Z","shell.execute_reply":"2021-05-20T14:36:33.888134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.style.use(\"ggplot\")\nplt.figure()\n\nplt.plot(h.history[\"complex_output_loss\"], label=\"train_loss\")\nplt.plot(h.history[\"val_complex_output_loss\"], label=\"val_loss\")\nplt.title(\"Loss\")\nplt.xlabel(\"Epoch\")\nplt.ylabel(\"Loss\")\nplt.legend(loc=\"upper left\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-05-20T14:36:37.800132Z","iopub.execute_input":"2021-05-20T14:36:37.800557Z","iopub.status.idle":"2021-05-20T14:36:38.002744Z","shell.execute_reply.started":"2021-05-20T14:36:37.800522Z","shell.execute_reply":"2021-05-20T14:36:38.001678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.style.use(\"ggplot\")\nplt.figure()\n\nplt.plot(h.history[\"other_disease_output_loss\"][1:], label=\"train_loss\")\nplt.plot(h.history[\"val_other_disease_output_loss\"][1:], label=\"val_loss\")\nplt.title(\"Loss\")\nplt.xlabel(\"Epoch\")\nplt.ylabel(\"Loss\")\nplt.legend(loc=\"upper left\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-05-20T14:36:39.479368Z","iopub.execute_input":"2021-05-20T14:36:39.479739Z","iopub.status.idle":"2021-05-20T14:36:39.670653Z","shell.execute_reply.started":"2021-05-20T14:36:39.479707Z","shell.execute_reply":"2021-05-20T14:36:39.669822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(h.history[\"decoder_output_loss\"][1:], label=\"train_loss\")\nplt.plot(h.history[\"val_decoder_output_loss\"][1:], label=\"val_loss\")\nplt.title(\"Loss\")\nplt.xlabel(\"Epoch\")\nplt.ylabel(\"Loss\")\nplt.legend(loc=\"upper left\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-05-20T14:36:43.972726Z","iopub.execute_input":"2021-05-20T14:36:43.973236Z","iopub.status.idle":"2021-05-20T14:36:44.165540Z","shell.execute_reply.started":"2021-05-20T14:36:43.973203Z","shell.execute_reply":"2021-05-20T14:36:44.164433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(h.history[\"complex_output_acc\"], label=\"train_acc\")\nplt.plot(h.history[\"val_complex_output_acc\"], label=\"val_acc\")\nplt.title(\"Accuracy\")\nplt.xlabel(\"Epoch\")\nplt.ylabel(\"Accuracy\")\nplt.legend(loc=\"upper left\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-05-20T14:36:46.819500Z","iopub.execute_input":"2021-05-20T14:36:46.819873Z","iopub.status.idle":"2021-05-20T14:36:47.022935Z","shell.execute_reply.started":"2021-05-20T14:36:46.819841Z","shell.execute_reply":"2021-05-20T14:36:47.022148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(h.history[\"other_disease_output_acc\"], label=\"train_acc\")\nplt.plot(h.history[\"val_other_disease_output_acc\"], label=\"val_acc\")\nplt.title(\"Accuracy\")\nplt.xlabel(\"Epoch\")\nplt.ylabel(\"Accuracy\")\nplt.legend(loc=\"upper left\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-05-20T14:36:55.594366Z","iopub.execute_input":"2021-05-20T14:36:55.595065Z","iopub.status.idle":"2021-05-20T14:36:55.800841Z","shell.execute_reply.started":"2021-05-20T14:36:55.595018Z","shell.execute_reply":"2021-05-20T14:36:55.799982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(h.history[\"other_disease_output_f1_score\"], label=\"train_F1_Score\")\nplt.plot(h.history[\"val_other_disease_output_f1_score\"], label=\"val_F1_Score\")\nplt.title(\"F1 Score\")\nplt.xlabel(\"Epoch\")\nplt.ylabel(\"F1 Score\")\nplt.legend(loc=\"lower right\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-05-20T14:37:44.224798Z","iopub.execute_input":"2021-05-20T14:37:44.225170Z","iopub.status.idle":"2021-05-20T14:37:44.494686Z","shell.execute_reply.started":"2021-05-20T14:37:44.225139Z","shell.execute_reply":"2021-05-20T14:37:44.493827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(h.history[\"complex_output_f1_score\"], label=\"train_F1_Score\")\nplt.plot(h.history[\"val_complex_output_f1_score\"], label=\"val_F1_Score\")\nplt.title(\"F1 Score\")\nplt.xlabel(\"Epoch\")\nplt.ylabel(\"F1 Score\")\nplt.legend(loc=\"upper left\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-05-20T14:38:03.679384Z","iopub.execute_input":"2021-05-20T14:38:03.679770Z","iopub.status.idle":"2021-05-20T14:38:03.878171Z","shell.execute_reply.started":"2021-05-20T14:38:03.679737Z","shell.execute_reply":"2021-05-20T14:38:03.877096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Confusion Matrix","metadata":{}},{"cell_type":"code","source":"#model = tf.keras.models.load_model('../input/decoder-branch-model-v4/decoder_multi_label_v3.h5', compile=False, custom_objects={'my_init':my_init})#, 'custom_loss': custom_loss})","metadata":{"execution":{"iopub.status.busy":"2021-05-20T08:19:10.759839Z","iopub.execute_input":"2021-05-20T08:19:10.760208Z","iopub.status.idle":"2021-05-20T08:19:16.946746Z","shell.execute_reply.started":"2021-05-20T08:19:10.760178Z","shell.execute_reply":"2021-05-20T08:19:16.945566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_steps = valid_df.shape[0] //BATCH_SIZE\nprint(valid_steps)","metadata":{"execution":{"iopub.status.busy":"2021-05-20T14:38:26.662630Z","iopub.execute_input":"2021-05-20T14:38:26.663037Z","iopub.status.idle":"2021-05-20T14:38:26.667921Z","shell.execute_reply.started":"2021-05-20T14:38:26.663003Z","shell.execute_reply":"2021-05-20T14:38:26.667101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\nimport matplotlib.pyplot as plt\nfrom sklearn.metrics import classification_report, confusion_matrix, multilabel_confusion_matrix","metadata":{"execution":{"iopub.status.busy":"2021-05-20T14:38:27.018327Z","iopub.execute_input":"2021-05-20T14:38:27.018836Z","iopub.status.idle":"2021-05-20T14:38:27.023324Z","shell.execute_reply.started":"2021-05-20T14:38:27.018805Z","shell.execute_reply":"2021-05-20T14:38:27.022229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_df = valid_df.reset_index()","metadata":{"execution":{"iopub.status.busy":"2021-05-20T14:38:27.272800Z","iopub.execute_input":"2021-05-20T14:38:27.273195Z","iopub.status.idle":"2021-05-20T14:38:27.291299Z","shell.execute_reply.started":"2021-05-20T14:38:27.273158Z","shell.execute_reply":"2021-05-20T14:38:27.290290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df.reset_index()","metadata":{"execution":{"iopub.status.busy":"2021-05-20T14:38:29.761809Z","iopub.execute_input":"2021-05-20T14:38:29.762373Z","iopub.status.idle":"2021-05-20T14:38:29.770321Z","shell.execute_reply.started":"2021-05-20T14:38:29.762328Z","shell.execute_reply":"2021-05-20T14:38:29.769560Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#for i in df.index:\n#    if df['complex'][i] == 1.0:\n#        print(df['class_labels'][i], '\\t', df['labels'][i])","metadata":{"execution":{"iopub.status.busy":"2021-05-20T08:20:39.359164Z","iopub.execute_input":"2021-05-20T08:20:39.359597Z","iopub.status.idle":"2021-05-20T08:20:39.365598Z","shell.execute_reply.started":"2021-05-20T08:20:39.359565Z","shell.execute_reply":"2021-05-20T08:20:39.363887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output_other, output_complx,output_img = model.predict(valid_dataset, steps = valid_steps, verbose = 1)","metadata":{"execution":{"iopub.status.busy":"2021-05-20T14:40:52.077826Z","iopub.execute_input":"2021-05-20T14:40:52.078411Z","iopub.status.idle":"2021-05-20T14:40:52.161779Z","shell.execute_reply.started":"2021-05-20T14:40:52.078376Z","shell.execute_reply":"2021-05-20T14:40:52.159422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"copy_other_disease = output_other.copy()\ncopy_output_complx = output_complx.copy()","metadata":{"execution":{"iopub.status.busy":"2021-05-20T08:47:22.409071Z","iopub.execute_input":"2021-05-20T08:47:22.409782Z","iopub.status.idle":"2021-05-20T08:47:22.417975Z","shell.execute_reply.started":"2021-05-20T08:47:22.409740Z","shell.execute_reply":"2021-05-20T08:47:22.416420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i, x in enumerate(output_other):\n    #num_output = np.round(x)\n    if x[4] >= 0.5:\n        output_other[i][4] = 1.0\n    else:\n        output_other[i][4] = 0.0\n        \n    if x[1] >= 0.5:\n        output_other[i][1] = 1.0\n    else:\n        output_other[i][1] = 0.0\n            \n    if x[0] >= 0.5:\n        output_other[i][0] = 1.0\n    else:\n        output_other[i][0] = 0.0\n            \n    if x[3] >= 0.5:\n        output_other[i][3] = 1.0\n    else:\n        output_other[i][3] = 0.0\n            \n    if x[2] >= 0.5:\n        output_other[i][2] = 1.0\n    else:\n        output_other[i][2] = 0.0\n\n        \nfor i, x in enumerate(output_complx):\n    if x >= 0.5:\n        output_complx[i] = 1.0\n    else:\n        output_complx[i] = 0.0","metadata":{"execution":{"iopub.status.busy":"2021-05-20T14:40:58.808471Z","iopub.execute_input":"2021-05-20T14:40:58.808853Z","iopub.status.idle":"2021-05-20T14:40:58.846627Z","shell.execute_reply.started":"2021-05-20T14:40:58.808816Z","shell.execute_reply":"2021-05-20T14:40:58.845081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"---Predicted--- \\t ---True Label---\\tComplex Predicition\")\nfor i, x in enumerate(output_other):\n    if valid_df['class_labels'][i].sum() == 0.0:\n        pass\n        #print(output_other[i], '\\t',  valid_df['class_labels'][i], '\\t', output_complx[i])","metadata":{"execution":{"iopub.status.busy":"2021-05-20T08:43:39.527083Z","iopub.execute_input":"2021-05-20T08:43:39.527502Z","iopub.status.idle":"2021-05-20T08:43:39.558957Z","shell.execute_reply.started":"2021-05-20T08:43:39.527469Z","shell.execute_reply":"2021-05-20T08:43:39.557885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_df['complex'][:176][0]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"---Predicted--- \\t ---True Label---\\tComplex Predicition\")\nfor i, x in enumerate(output_other):\n    if valid_df['complex'][i] == 1.0:\n        pass\n        #print(output_other[i], '\\t',  valid_df['class_labels'][i], '\\t', output_complx[i])","metadata":{"execution":{"iopub.status.busy":"2021-05-20T08:43:32.383760Z","iopub.execute_input":"2021-05-20T08:43:32.384750Z","iopub.status.idle":"2021-05-20T08:43:32.409019Z","shell.execute_reply.started":"2021-05-20T08:43:32.384629Z","shell.execute_reply":"2021-05-20T08:43:32.407695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"true_other = 0\nfalse_other = 0\ntrue_cmplx = 0\nfalse_cmplx = 0\nfor i in range(len(output_other)):\n    #print(output_other[i], '\\t',  valid_df['class_labels'][i], '\\t', output_complx[i])\n    if set(output_other[i]) == set(valid_df['class_labels'][i]):\n        true_other += 1\n    else:\n        false_other += 1\n    if output_complx[i][0] == valid_df['complex'][i]:\n        true_cmplx += 1\n    else:\n        false_cmplx += 1","metadata":{"execution":{"iopub.status.busy":"2021-05-20T08:43:23.164582Z","iopub.execute_input":"2021-05-20T08:43:23.165120Z","iopub.status.idle":"2021-05-20T08:43:23.209146Z","shell.execute_reply.started":"2021-05-20T08:43:23.165079Z","shell.execute_reply":"2021-05-20T08:43:23.208061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_df.to_csv('valid_df.csv')","metadata":{"execution":{"iopub.status.busy":"2021-05-20T08:48:51.298024Z","iopub.execute_input":"2021-05-20T08:48:51.298431Z","iopub.status.idle":"2021-05-20T08:48:51.846791Z","shell.execute_reply.started":"2021-05-20T08:48:51.298395Z","shell.execute_reply":"2021-05-20T08:48:51.845782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(true_other)\nprint(false_other)\nprint(true_cmplx)\nprint(false_cmplx)","metadata":{"execution":{"iopub.status.busy":"2021-05-20T08:34:48.787718Z","iopub.execute_input":"2021-05-20T08:34:48.788094Z","iopub.status.idle":"2021-05-20T08:34:48.799500Z","shell.execute_reply.started":"2021-05-20T08:34:48.788061Z","shell.execute_reply":"2021-05-20T08:34:48.798204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(output_complx)","metadata":{"execution":{"iopub.status.busy":"2021-05-20T08:45:11.059708Z","iopub.execute_input":"2021-05-20T08:45:11.060075Z","iopub.status.idle":"2021-05-20T08:45:11.068629Z","shell.execute_reply.started":"2021-05-20T08:45:11.060042Z","shell.execute_reply":"2021-05-20T08:45:11.067213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_matrix(cm, class_names):\n    plt.style.use('default')\n    #cm = confusion_matrix(x_labels, y_labels)\n    fig, ax = plt.subplots(figsize=(5, 5))\n    im = ax.imshow(cm, interpolation='nearest', cmap=plt.cm.Blues)\n    ax.set(xticks=np.arange(cm.shape[1]),\n            yticks=np.arange(cm.shape[0]),\n            xticklabels=class_names,\n            yticklabels=class_names,\n            title='Confusion matrix',\n            ylabel='True label',\n            xlabel='Predicted label')\n    plt.setp(ax.get_xticklabels(), rotation=45, ha='right', rotation_mode='anchor')\n    for i in range(cm.shape[0]):\n        for j in range(cm.shape[1]):\n            ax.text(j, i, format(cm[i, j], 'd'),\n                    ha='center', va='center',\n                    color='white' if cm[i, j] > cm.max() / 2. else 'black')\n    fig.tight_layout()\n    plt.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"otehr_cm = multilabel_confusion_matrix(valid_df['class_labels'][:928].to_list(), output_other)","metadata":{"execution":{"iopub.status.busy":"2021-05-20T08:43:09.294224Z","iopub.execute_input":"2021-05-20T08:43:09.294614Z","iopub.status.idle":"2021-05-20T08:43:09.309986Z","shell.execute_reply.started":"2021-05-20T08:43:09.294583Z","shell.execute_reply":"2021-05-20T08:43:09.308604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#cm = np.asarray(cm, dtype=np.uint8)\nplot_matrix(otehr_cm[0], ['Frog Eye', 'Not Frog Eye'])","metadata":{"execution":{"iopub.status.busy":"2021-05-20T08:43:55.850390Z","iopub.execute_input":"2021-05-20T08:43:55.850768Z","iopub.status.idle":"2021-05-20T08:43:56.113751Z","shell.execute_reply.started":"2021-05-20T08:43:55.850735Z","shell.execute_reply":"2021-05-20T08:43:56.112655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_matrix(otehr_cm[1], ['Heathly', 'Not Heathly'])","metadata":{"execution":{"iopub.status.busy":"2021-05-20T08:44:01.057455Z","iopub.execute_input":"2021-05-20T08:44:01.057838Z","iopub.status.idle":"2021-05-20T08:44:01.259971Z","shell.execute_reply.started":"2021-05-20T08:44:01.057806Z","shell.execute_reply":"2021-05-20T08:44:01.258830Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_matrix(otehr_cm[2], ['Powdery Mildew', 'Not Powdery Mildew'])","metadata":{"execution":{"iopub.status.busy":"2021-05-20T08:44:04.585907Z","iopub.execute_input":"2021-05-20T08:44:04.586262Z","iopub.status.idle":"2021-05-20T08:44:04.789615Z","shell.execute_reply.started":"2021-05-20T08:44:04.586231Z","shell.execute_reply":"2021-05-20T08:44:04.788447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_matrix(otehr_cm[3], ['Rust', 'Not Rust '])","metadata":{"execution":{"iopub.status.busy":"2021-05-20T08:44:05.979608Z","iopub.execute_input":"2021-05-20T08:44:05.979965Z","iopub.status.idle":"2021-05-20T08:44:06.185203Z","shell.execute_reply.started":"2021-05-20T08:44:05.979934Z","shell.execute_reply":"2021-05-20T08:44:06.184103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_matrix(otehr_cm[4], ['Scab', 'Not Scab'])","metadata":{"execution":{"iopub.status.busy":"2021-05-20T08:44:07.469821Z","iopub.execute_input":"2021-05-20T08:44:07.470187Z","iopub.status.idle":"2021-05-20T08:44:07.672036Z","shell.execute_reply.started":"2021-05-20T08:44:07.470155Z","shell.execute_reply":"2021-05-20T08:44:07.670987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cmplx_cm = confusion_matrix(valid_df['complex'][:928], output_complx)","metadata":{"execution":{"iopub.status.busy":"2021-05-20T08:44:18.424912Z","iopub.execute_input":"2021-05-20T08:44:18.425304Z","iopub.status.idle":"2021-05-20T08:44:18.438697Z","shell.execute_reply.started":"2021-05-20T08:44:18.425254Z","shell.execute_reply":"2021-05-20T08:44:18.437513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_matrix(cmplx_cm, ['Complex', 'Not Complex'])","metadata":{"execution":{"iopub.status.busy":"2021-05-20T08:44:20.868948Z","iopub.execute_input":"2021-05-20T08:44:20.869351Z","iopub.status.idle":"2021-05-20T08:44:21.076484Z","shell.execute_reply.started":"2021-05-20T08:44:20.869313Z","shell.execute_reply":"2021-05-20T08:44:21.075389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2021-05-20T08:43:47.620213Z","iopub.execute_input":"2021-05-20T08:43:47.620620Z","iopub.status.idle":"2021-05-20T08:43:47.630722Z","shell.execute_reply.started":"2021-05-20T08:43:47.620588Z","shell.execute_reply":"2021-05-20T08:43:47.629502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}