{"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\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom matplotlib import style\nimport os\nimport cv2\nimport pydicom","metadata":{"execution":{"iopub.status.busy":"2021-06-26T06:37:21.931447Z","iopub.execute_input":"2021-06-26T06:37:21.931845Z","iopub.status.idle":"2021-06-26T06:37:27.603525Z","shell.execute_reply.started":"2021-06-26T06:37:21.931755Z","shell.execute_reply":"2021-06-26T06:37:27.602638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_study_level = pd.read_csv('../input/siim-covid19-detection/train_study_level.csv')\ntrain_study_level = train_study_level.set_index('id')\ntrain_study_level.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-26T05:50:06.31253Z","iopub.status.idle":"2021-06-26T05:50:06.313611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_image_level = pd.read_csv('../input/siim-covid19-detection/train_image_level.csv')\ntrain_image_level = train_image_level.set_index('id')\ntrain_image_level.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-26T05:50:06.315786Z","iopub.status.idle":"2021-06-26T05:50:06.316789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''def load_images(folder, data):\n    imgs, targets, j = [], [], 0\n    for foldername in os.listdir(folder):\n        print(len(os.listdir(folder)) , '/' , j)\n        j += 1\n        loc1 = folder + \"/\" + foldername #3eb5a506ccf3\n        for file in os.listdir(loc1):\n            loc2 = loc1 + \"/\" + file #\n            for filedcm in os.listdir(loc2):\n                filename = os.path.join(loc2, filedcm)\n                try:\n                    ds = pydicom.dcmread(filename)\n                    ds1 =ds.pixel_array\n                    #image_bytes = tf.io.read_file(os.path.join(loc2, filedcm))\n                    #image = tfio.image.decode_dicom_image(image_bytes, dtype=tf.uint16)\n                    image = cv2.resize(ds1, (300, 300))\n                    imgs.append(image.reshape(300, 300, 1))\n                    temp = data.loc[foldername + \"_study\"]\n                    print(temp, foldername+\"_study\")\n                    input()\n                    temp_targets = []\n                    for i in temp:\n                        temp_targets.append(i)\n                    targets.append(temp_targets)\n                except RuntimeError:\n                        continue\n        \n    imgs = np.array(imgs)\n    targets = np.array(targets)\n    return imgs, targets'''","metadata":{"execution":{"iopub.status.busy":"2021-06-26T05:31:13.085102Z","iopub.execute_input":"2021-06-26T05:31:13.085541Z","iopub.status.idle":"2021-06-26T05:31:13.096132Z","shell.execute_reply.started":"2021-06-26T05:31:13.085496Z","shell.execute_reply":"2021-06-26T05:31:13.094583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''X_train, y_train = load_images('../input/siim-covid19-detection/train', train_study_level)'''","metadata":{"_kg_hide-output":false,"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-06-26T05:31:14.117242Z","iopub.execute_input":"2021-06-26T05:31:14.11779Z","iopub.status.idle":"2021-06-26T05:31:14.138636Z","shell.execute_reply.started":"2021-06-26T05:31:14.117745Z","shell.execute_reply":"2021-06-26T05:31:14.135428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pickle as pkl\npickle_in = open('../input/images-in-numpy-array-for-siim-coivd19-prediction/training_images.pickle', 'rb')\nX_train = pkl.load(pickle_in)","metadata":{"execution":{"iopub.status.busy":"2021-06-26T06:37:27.605337Z","iopub.execute_input":"2021-06-26T06:37:27.605744Z","iopub.status.idle":"2021-06-26T06:37:38.70788Z","shell.execute_reply.started":"2021-06-26T06:37:27.605699Z","shell.execute_reply":"2021-06-26T06:37:38.707174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2021-06-26T06:37:38.709807Z","iopub.execute_input":"2021-06-26T06:37:38.710358Z","iopub.status.idle":"2021-06-26T06:37:38.718558Z","shell.execute_reply.started":"2021-06-26T06:37:38.710321Z","shell.execute_reply":"2021-06-26T06:37:38.717721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pickle_in = open('../input/images-in-numpy-array-for-siim-coivd19-prediction/training_lables.pickle', 'rb')\ny_train = pkl.load(pickle_in)","metadata":{"execution":{"iopub.status.busy":"2021-06-26T06:37:38.719765Z","iopub.execute_input":"2021-06-26T06:37:38.720082Z","iopub.status.idle":"2021-06-26T06:37:38.746995Z","shell.execute_reply.started":"2021-06-26T06:37:38.720056Z","shell.execute_reply":"2021-06-26T06:37:38.745657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.shape","metadata":{"execution":{"iopub.status.busy":"2021-06-26T06:37:38.747961Z","iopub.execute_input":"2021-06-26T06:37:38.748238Z","iopub.status.idle":"2021-06-26T06:37:38.753485Z","shell.execute_reply.started":"2021-06-26T06:37:38.748214Z","shell.execute_reply":"2021-06-26T06:37:38.752474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''import pickle as pkl\nwith open ('training_images.pickle', 'wb') as f:\n    pkl.dump(X_train, f)\n    f.close()\n\n    with open ('training_lables.pickle', 'wb') as f:\n        pkl.dump(y_train, f)\n        f.close'''","metadata":{"execution":{"iopub.status.busy":"2021-06-26T05:31:24.129963Z","iopub.execute_input":"2021-06-26T05:31:24.130637Z","iopub.status.idle":"2021-06-26T05:31:24.139921Z","shell.execute_reply.started":"2021-06-26T05:31:24.130589Z","shell.execute_reply":"2021-06-26T05:31:24.138322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rand_no = np.random.randint(0, len(X_train), 4)\nfig, axes = plt.subplots(1, 4, figsize=(20, 20))\nfor i in range(4):\n    axes[i].imshow(X_train[rand_no[i]].astype('uint16'), cmap='bone')\n    axes[i].set_title(y_train[rand_no[i]])","metadata":{"execution":{"iopub.status.busy":"2021-06-26T05:31:24.142528Z","iopub.execute_input":"2021-06-26T05:31:24.143145Z","iopub.status.idle":"2021-06-26T05:31:24.937923Z","shell.execute_reply.started":"2021-06-26T05:31:24.143098Z","shell.execute_reply":"2021-06-26T05:31:24.936925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig1, axes1 = plt.subplots(1, 2, figsize=(15, 15))\naxes1[0].imshow(X_train[0], cmap='bone')\naxes1[1].imshow(X_train[0].transpose(1, 0, 2), cmap='bone')","metadata":{"execution":{"iopub.status.busy":"2021-06-26T05:31:24.93926Z","iopub.execute_input":"2021-06-26T05:31:24.939677Z","iopub.status.idle":"2021-06-26T05:31:25.684568Z","shell.execute_reply.started":"2021-06-26T05:31:24.939629Z","shell.execute_reply":"2021-06-26T05:31:25.681076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#X_train = X_train.astype('uint8').astype('float32')\nprint(X_train.shape)\nprint(X_train.max())\nprint(y_train.shape)\nprint(X_train.min())","metadata":{"execution":{"iopub.status.busy":"2021-06-26T05:31:25.688229Z","iopub.execute_input":"2021-06-26T05:31:25.688674Z","iopub.status.idle":"2021-06-26T05:31:26.680727Z","shell.execute_reply.started":"2021-06-26T05:31:25.688634Z","shell.execute_reply":"2021-06-26T05:31:26.679583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(rescale=1./65535.0,\n                                  rotation_range=40,\n                                  width_shift_range=0.2,\n                                  height_shift_range=0.2,\n                                  zoom_range=0.2,\n                                  horizontal_flip=True,\n                                  fill_mode='nearest')","metadata":{"execution":{"iopub.status.busy":"2021-06-26T05:50:22.817346Z","iopub.execute_input":"2021-06-26T05:50:22.818397Z","iopub.status.idle":"2021-06-26T05:50:22.825516Z","shell.execute_reply.started":"2021-06-26T05:50:22.818185Z","shell.execute_reply":"2021-06-26T05:50:22.824046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"Transfer Learning\"\"\"\nbase_model = tf.keras.applications.EfficientNetB0(include_top=False)\n\nbase_model.trainable = False\n\ninputs = layers.Input(shape=(300, 300, 1), name='input_layer')\n\nx = base_model(inputs)\nprint(f\"shape after base model: {x.shape}\")\n\nx = layers.GlobalAveragePooling2D(name='GAP_layer')(x)\nprint(f'shape after GAP: {x.shape}')\n\noutputs = layers.Dense(4, activation='softmax', name='output_layer')(x)\n\nmodel_0 = keras.Model(inputs=inputs, outputs=outputs)\n\nmodel_0.compile(optimizer=keras.optimizers.Adam(),\n             loss=keras.losses.CategoricalCrossentropy(from_logits=False),\n             metrics=['accuracy'])\n\n\nclass Callbacks(tf.keras.callbacks.Callback):\n        def on_epoch_end(self, epoch, logs=None):\n            if logs.get('accuracy') > 0.99:\n                print(f'Stopping, due to accuracy value is above 99% ')\n                self.model.stop_training = True\n\n\ncallbacks = Callbacks()\n\n\nmodel_checkpoint_callbacks = tf.keras.callbacks.ModelCheckpoint('siim.h5',\n                                                                monitor='acc',\n                                                                verbose=1,\n                                                                save_weights_only=False,\n                                                                mode='max',\n                                                                save_freq='epoch'\n                                                                )\n\nhistory = model_0.fit_generator(generator=train_datagen.flow(X_train, y_train, batch_size=32),\n                       epochs=25,\n                       steps_per_epoch=int(len(X_train) / 32),\n                       callbacks=[callbacks])","metadata":{"execution":{"iopub.status.busy":"2021-06-26T05:31:26.692101Z","iopub.execute_input":"2021-06-26T05:31:26.692691Z","iopub.status.idle":"2021-06-26T05:32:18.886035Z","shell.execute_reply.started":"2021-06-26T05:31:26.692644Z","shell.execute_reply":"2021-06-26T05:32:18.88252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_model(X_train, y_train):\n    inputs = keras.Input(shape=(300, 300, 1))\n    x = layers.Conv2D(32, (3, 3), padding='valid', activation='relu')(inputs)\n    x = layers.MaxPooling2D((2, 2))(x)\n    x = layers.Conv2D(32, (3, 3), padding='valid', activation='relu')(x)\n    x = layers.MaxPooling2D((2, 2))(x)\n    x = layers.Conv2D(32, (3, 3), padding='valid', activation='relu')(x)\n    x = layers.MaxPooling2D((2, 2))(x)\n    x = layers.Flatten()(x)\n    x = layers.Dense(128, activation='relu')(x)\n    outputs = layers.Dense(4, activation='softmax')(x)\n    \n    model = keras.models.Model(inputs=inputs, outputs=outputs, name='conv')\n    \n    print(model.summary())\n    \n    class Callbacks(tf.keras.callbacks.Callback):\n        def on_epoch_end(self, epoch, logs=None):\n            if logs.get('accuracy') > 0.99:\n                print(f'Stopping, due to accuracy value is above 99% ')\n                self.model.stop_training = True\n\n\n    callbacks = Callbacks()\n\n    \n    model.compile(optimizer=keras.optimizers.Adam(),\n                 loss=keras.losses.CategoricalCrossentropy(from_logits=False),\n                 metrics=['accuracy'])\n    \n    history = model.fit_generator(generator=train_datagen.flow(X_train, y_train, batch_size=32),\n                       epochs=5,\n                       steps_per_epoch=int(len(X_train) / 32),\n                       )    \n    \n    return model, history","metadata":{"execution":{"iopub.status.busy":"2021-06-26T05:50:24.700713Z","iopub.execute_input":"2021-06-26T05:50:24.701167Z","iopub.status.idle":"2021-06-26T05:50:24.715954Z","shell.execute_reply.started":"2021-06-26T05:50:24.701137Z","shell.execute_reply":"2021-06-26T05:50:24.714616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model, history = create_model(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2021-06-26T05:50:26.065024Z","iopub.execute_input":"2021-06-26T05:50:26.065407Z","iopub.status.idle":"2021-06-26T05:52:39.941801Z","shell.execute_reply.started":"2021-06-26T05:50:26.065375Z","shell.execute_reply":"2021-06-26T05:52:39.938895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_acc = history.history['accuracy']\nprint(len(history.history['accuracy']), history.history['accuracy'])\nplt.plot(range(1, 6), training_acc)","metadata":{"execution":{"iopub.status.busy":"2021-06-26T05:48:33.259619Z","iopub.execute_input":"2021-06-26T05:48:33.260141Z","iopub.status.idle":"2021-06-26T05:48:33.43461Z","shell.execute_reply.started":"2021-06-26T05:48:33.260109Z","shell.execute_reply":"2021-06-26T05:48:33.433211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def convfilter(filter, inputs, stride=1):\n    i, j, temp = 0, 0, []\n    convolved_feature_shape = [int((inputs.shape[0] - filter.shape[0] + 1) / stride), int((inputs.shape[0] - filter.shape[0] + 1) / stride), inputs.shape[2]]  \n    convolved_features = np.ones((convolved_feature_shape))\n    print(f'convolved features shape: {convolved_features.shape}')\n    while inputs.shape[0] - (i + stride) > filter.shape[0]:\n        while inputs.shape[1] - (j + stride) > filter.shape[1]:\n            for k in range(inputs.shape[2]):\n                temp.append(np.sum(inputs[i:i + filter.shape[0], j:j + filter.shape[1], k] * filter))\n                \n            #print(f'j :{j}')\n            j += stride\n        j = 0\n        #print(f'j set to 0 again')\n        #print(f'i :{i}')\n        i += stride\n    \n    fig1, axes1 = plt.subplots(1, 2, figsize=(10, 10))\n    axes1[0].imshow(inputs, cmap='bone')\n    axes1[1].imshow(convolved_features, cmap='bone')\n            ","metadata":{"execution":{"iopub.status.busy":"2021-06-12T07:00:33.605341Z","iopub.status.idle":"2021-06-12T07:00:33.605996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Conv2D:\n    def __init__(self, n_kernels, kernel_size, stride=1):\n        self.n_kernels = n_kernels\n        self.kernel_size = kernel_size\n        self.stride = stride\n        #self.weights = np.random.randn(n_kernels, kernel_size[0], kernel_size[1])\n        self.weights = np.array([ [-1, -2, -1], [0, 0, 0], [1, 2, 1]]).reshape(1, 3, 3) * -1\n        self.bias = np.zeros((n_kernels, ))\n    \n    def call(self, input_tensor, labels):\n        self.labels = labels\n        self.input_image = input_tensor\n        self.output_dims = (int((input_tensor.shape[0] - self.kernel_size[0] + 1) / self.stride),\n                            int((input_tensor.shape[1] - self.kernel_size[1] + 1) / self.stride),\n                            self.n_kernels)\n        \n        self.output = np.ones((self.output_dims))\n        \n        for n in range(self.n_kernels):\n            i, j, temp, h = 0, 0, np.ones((self.output_dims[:2])), 0\n            while input_tensor.shape[1] - (j + self.stride) > self.kernel_size[1]:\n                while input_tensor.shape[0] - (i + self.stride) > self.kernel_size[0]:\n                    for channel in range(input_tensor.shape[-1]):\n                        temp1 = np.sum(input_tensor[i:i + self.kernel_size[0], j:j + self.kernel_size[1], channel] * self.weights[0])\n                        h += temp1\n                    temp[i, j] = h\n                    h = 0\n                    i += self.stride\n                i = 0\n                j += self.stride\n            j = 0\n            self.output[:, :, n] = temp\n            print(n)\n    \n    def visualizing_convs(self, conv_number=0, cmap_attribute='bone'):\n        img = np.expand_dims(self.output[:, :, conv_number], axis=2)\n        \n        fig1, axes1 = plt.subplots(1, 2, figsize=(10, 10))\n        \n        axes1[0].imshow(self.input_image, cmap=cmap_attribute)\n        axes1[0].set_title(f'Input Image')\n        \n        axes1[1].imshow(img, cmap=cmap_attribute)\n        axes1[1].set_title(f'Convolved feature number: {conv_number}')\n        \n        print(self.labels)","metadata":{"execution":{"iopub.status.busy":"2021-06-14T05:23:00.92912Z","iopub.execute_input":"2021-06-14T05:23:00.929556Z","iopub.status.idle":"2021-06-14T05:23:01.434618Z","shell.execute_reply.started":"2021-06-14T05:23:00.929516Z","shell.execute_reply":"2021-06-14T05:23:01.433404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class MaxPooling2D:\n    def __init__(self, pool_size):\n        self.pool_size = pool_size\n    \n    def call(self, input_tensor):\n        self.input_image = input_tensor\n        self.output_dims = (input_tensor[0] // 2, input_tensor[1] // 2, input_tensor[-1])\n        self.outputs = np.ones((self.output_dims))\n        \n        for n in range(self.output_dims[2]):\n            for j in range(self.output_dims[1]):\n                for i in range(self.output_dims[0]):\n                    self.outputs[i, j, n] = self.input_image[i:i + self.pool_size]","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"conv = Conv2D(1, (3, 3))\nconv.call(X_train[0], y_train[0])\nconv.visualizing_convs(conv_number=0, cmap_attribute='bone')\n","metadata":{"execution":{"iopub.status.busy":"2021-06-14T05:23:01.453682Z","iopub.execute_input":"2021-06-14T05:23:01.454034Z","iopub.status.idle":"2021-06-14T05:23:02.892897Z","shell.execute_reply.started":"2021-06-14T05:23:01.454003Z","shell.execute_reply":"2021-06-14T05:23:02.892117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_study_level = pd.read_csv('../input/siim-covid19-detection/train_study_level.csv')\ntrain_study_level = train_study_level.set_index('id')\ntrain_study_level.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-14T04:05:14.896839Z","iopub.execute_input":"2021-06-14T04:05:14.897246Z","iopub.status.idle":"2021-06-14T04:05:14.94757Z","shell.execute_reply.started":"2021-06-14T04:05:14.897213Z","shell.execute_reply":"2021-06-14T04:05:14.946677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filter_random = np.random.randint(-9, 9, (3, 3))","metadata":{"execution":{"iopub.status.busy":"2021-06-14T04:04:29.231792Z","iopub.execute_input":"2021-06-14T04:04:29.232189Z","iopub.status.idle":"2021-06-14T04:04:29.237431Z","shell.execute_reply.started":"2021-06-14T04:04:29.232151Z","shell.execute_reply":"2021-06-14T04:04:29.2364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"convfilter(filter_random, X_train1[0], 1)","metadata":{"execution":{"iopub.status.busy":"2021-06-12T07:00:33.609291Z","iopub.status.idle":"2021-06-12T07:00:33.610021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filter = np.array([[-1, 1, -1], [1, -4, 1], [-8, 1, 9]])\n","metadata":{"execution":{"iopub.status.busy":"2021-06-12T07:00:33.611213Z","iopub.status.idle":"2021-06-12T07:00:33.611864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"convfilter(filter, X_train1[0], 1)","metadata":{"execution":{"iopub.status.busy":"2021-06-12T07:00:33.613092Z","iopub.status.idle":"2021-06-12T07:00:33.613781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def pixel_altering(image):\n    for i in range(image.shape[2]):\n        for j in range(image.shape[1]):\n            for k in range(image.shape[0]):\n                if k < 60000:\n                    image[k, j, i] = 0\n    \n    plt.imshow(image, cmap='bone')","metadata":{"execution":{"iopub.status.busy":"2021-06-26T06:38:56.490134Z","iopub.execute_input":"2021-06-26T06:38:56.490638Z","iopub.status.idle":"2021-06-26T06:38:56.496754Z","shell.execute_reply.started":"2021-06-26T06:38:56.490593Z","shell.execute_reply":"2021-06-26T06:38:56.495956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pixel_altering(X_train[0])","metadata":{"execution":{"iopub.status.busy":"2021-06-26T06:38:39.495767Z","iopub.execute_input":"2021-06-26T06:38:39.496263Z","iopub.status.idle":"2021-06-26T06:38:39.642685Z","shell.execute_reply.started":"2021-06-26T06:38:39.496224Z","shell.execute_reply":"2021-06-26T06:38:39.642066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}