{"metadata":{"colab":{"provenance":[]},"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":8089,"databundleVersionId":44321,"sourceType":"competition"},{"sourceId":8465479,"sourceType":"datasetVersion","datasetId":5046967},{"sourceId":8470077,"sourceType":"datasetVersion","datasetId":5050409}],"dockerImageVersionId":30698,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\n\ndef pad_with_zeros(array):\n\n    original_shape = array.shape\n    new_shape = (2 * original_shape[0] + 1, 2 * original_shape[1] + 1)\n    padded_array = np.zeros(new_shape, dtype=array.dtype)\n    padded_array[1:-1:2, 1:-1:2] = array\n    return padded_array","metadata":{"id":"7BCA4JEhYp_R","execution":{"iopub.status.busy":"2024-05-21T18:18:22.127502Z","iopub.execute_input":"2024-05-21T18:18:22.128134Z","iopub.status.idle":"2024-05-21T18:18:22.144019Z","shell.execute_reply.started":"2024-05-21T18:18:22.128100Z","shell.execute_reply":"2024-05-21T18:18:22.142235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def convolve2d(X, W, stride=1):\n    X_shape = X.shape\n    W_shape = W.shape\n    output_shape = (\n        (X_shape[0] - W_shape[0]) // stride + 1,\n        (X_shape[1] - W_shape[1]) // stride + 1\n    )\n    output = np.zeros(output_shape)\n    for i in range(0, output_shape[0] * stride, stride):\n        for j in range(0, output_shape[1] * stride, stride):\n            if i + W_shape[0] <= X.shape[0] and j + W_shape[1] <= X.shape[1]:\n                output[i // stride, j // stride] = np.sum(X[i:i + W_shape[0], j:j + W_shape[1]] * W)\n    return output","metadata":{"id":"iVeDJ1CacIDl","execution":{"iopub.status.busy":"2024-05-21T18:18:24.341452Z","iopub.execute_input":"2024-05-21T18:18:24.342534Z","iopub.status.idle":"2024-05-21T18:18:24.348717Z","shell.execute_reply.started":"2024-05-21T18:18:24.342493Z","shell.execute_reply":"2024-05-21T18:18:24.347980Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class UpConv:\n    def __init__(self, kernel_size, learning_rate=0.005, beta1=0.9, beta2=0.999, epsilon=1e-8):\n        self.kernel_size = kernel_size\n        self.kernel = np.random.randn(*kernel_size) * np.sqrt(2. / 4.)\n        self.learning_rate = learning_rate\n        self.beta1 = beta1\n        self.beta2 = beta2\n        self.epsilon = epsilon\n        self.m_dw = np.zeros_like(self.kernel)\n        self.v_dw = np.zeros_like(self.kernel)\n\n    def forward(self, x):\n        in_height, in_width, in_channels = x.shape\n        out_height = in_height * 2\n        out_width = in_width * 2\n        out_channels = in_channels\n        self.input = x\n        output = np.zeros((out_height, out_width, out_channels))\n        rot_filter = np.rot90(self.kernel, 2)\n        for c in range(out_channels):\n            x_ = pad_with_zeros(x[:,:,c])\n            output[:,:,c] = convolve2d(x_, rot_filter)\n\n        return output\n\n    def backward(self, grad_output):\n        out_height, out_width, out_channels = grad_output.shape\n        in_height = out_height // 2\n        in_width = out_width // 2\n        in_channels = out_channels//2\n\n        grad_input = np.zeros((in_height, in_width, in_channels))\n        grad_kernel = np.zeros(self.kernel_size)\n\n        for c in range(in_channels):\n            grad_input[:,:,c] = convolve2d(grad_output[:,:,c], self.kernel, stride=2)\n\n        grad_kernel[0, 0] = np.sum(grad_output[0::2, 0::2, :in_channels] * self.input[:, :, :])\n        grad_kernel[0, 1] = np.sum(grad_output[1::2, 0::2, :in_channels] * self.input[:, :, :])\n        grad_kernel[1, 0] = np.sum(grad_output[0::2, 1::2, :in_channels] * self.input[:, :, :])\n        grad_kernel[1, 1] = np.sum(grad_output[1::2, 1::2, :in_channels] * self.input[:, :, :])\n\n        # Update Adam optimizer parameters\n        self.m_dw = self.beta1 * self.m_dw + (1 - self.beta1) * grad_kernel\n        self.v_dw = self.beta2 * self.v_dw + (1 - self.beta2) * (grad_kernel ** 2)\n\n        # Bias correction\n        m_dw_corrected = self.m_dw / (1 - self.beta1)\n        v_dw_corrected = self.v_dw / (1 - self.beta2)\n\n        # Update kernel\n        self.kernel -= self.learning_rate * m_dw_corrected / (np.sqrt(v_dw_corrected) + self.epsilon)\n\n        return grad_input","metadata":{"id":"HIe_3XMIR6UE","execution":{"iopub.status.busy":"2024-05-21T18:18:24.551535Z","iopub.execute_input":"2024-05-21T18:18:24.551936Z","iopub.status.idle":"2024-05-21T18:18:24.568142Z","shell.execute_reply.started":"2024-05-21T18:18:24.551887Z","shell.execute_reply":"2024-05-21T18:18:24.566833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\n\nclass conv1x1:\n    def __init__(self, in_channels, out_channels, learning_rate=0.005, beta1=0.9, beta2=0.999,epsilon=1e-8):\n        self.weights = np.random.randn(out_channels, in_channels) * np.sqrt(2. / in_channels)\n        self.biases = np.zeros(out_channels)\n        self.learning_rate = learning_rate\n        self.beta1 = beta1\n        self.beta2 = beta2\n        self.epsilon = epsilon\n\n        # Initialize Adam optimizer parameters\n        self.m_dw = np.zeros_like(self.weights)\n        self.v_dw = np.zeros_like(self.weights)\n        self.m_db = np.zeros_like(self.biases)\n        self.v_db = np.zeros_like(self.biases)\n\n    def forward(self, input):\n        self.input = input\n        height, width, in_channels = input.shape\n        input_reshaped = input.reshape(-1, in_channels)\n        output_reshaped = input_reshaped @ self.weights.T + self.biases\n        output = output_reshaped.reshape(height, width, -1)\n        self.output = output\n        return self.output\n\n    def backward(self, dL_dout):\n\n        height, width, out_channels = dL_dout.shape\n        in_channels = self.input.shape[2]\n\n        dL_dout_reshaped = dL_dout.reshape(-1, out_channels)\n        input_reshaped = self.input.reshape(-1, in_channels)\n\n        dL_dweights = dL_dout_reshaped.T @ input_reshaped\n        dL_dbiases = dL_dout_reshaped.sum(axis=0)\n        dL_dinput = dL_dout_reshaped @ self.weights\n\n        # Update Adam optimizer parameters\n        self.m_dw = self.beta1 * self.m_dw + (1 - self.beta1) * dL_dweights\n        self.v_dw = self.beta2 * self.v_dw + (1 - self.beta2) * (dL_dweights ** 2)\n        self.m_db = self.beta1 * self.m_db + (1 - self.beta1) * dL_dbiases\n        self.v_db = self.beta2 * self.v_db + (1 - self.beta2) * (dL_dbiases ** 2)\n\n        # Bias correction\n        m_dw_corrected = self.m_dw / (1 - self.beta1)\n        v_dw_corrected = self.v_dw / (1 - self.beta2)\n        m_db_corrected = self.m_db / (1 - self.beta1)\n        v_db_corrected = self.v_db / (1 - self.beta2)\n\n        # Update weights and biases\n        self.weights -= self.learning_rate * m_dw_corrected / (np.sqrt(v_dw_corrected) + self.epsilon)\n        self.biases -= self.learning_rate * m_db_corrected / (np.sqrt(v_db_corrected) + self.epsilon)\n\n        dL_dweights = dL_dweights.reshape(self.weights.shape)\n        dL_dinput = dL_dinput.reshape(height, width, in_channels)\n\n        return dL_dinput\n","metadata":{"id":"-arCF6WrnOXK","execution":{"iopub.status.busy":"2024-05-21T18:18:24.778586Z","iopub.execute_input":"2024-05-21T18:18:24.779052Z","iopub.status.idle":"2024-05-21T18:18:24.793553Z","shell.execute_reply.started":"2024-05-21T18:18:24.779005Z","shell.execute_reply":"2024-05-21T18:18:24.792241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class conv3x3:\n    def __init__(self, out_channels, in_channels, padding=1, learning_rate=0.005, beta1=0.9, beta2=0.999, epsilon=1e-8):\n        self.in_channels = in_channels\n        self.out_channels = out_channels\n        self.padding = padding\n        self.learning_rate = learning_rate\n        self.beta1 = beta1\n        self.beta2 = beta2\n        self.epsilon = epsilon\n\n        self.depthwise_weights = np.random.randn(in_channels, 3, 3) * np.sqrt(2. / (in_channels * 9))\n        self.depthwise_biases = np.zeros(in_channels)\n\n        # Initialize Adam optimizer parameters\n        self.m_dw = np.zeros_like(self.depthwise_weights)\n        self.v_dw = np.zeros_like(self.depthwise_weights)\n        self.m_db = np.zeros_like(self.depthwise_biases)\n        self.v_db = np.zeros_like(self.depthwise_biases)\n\n        # 1x1 convolution for pointwise convolution\n        self.pointwise_conv = conv1x1(in_channels, out_channels, learning_rate = learning_rate)\n\n    def forward(self, input):\n        self.input = input\n        height, width, in_channels = input.shape\n\n        # Apply padding\n        if self.padding > 0:\n            padded_input = np.pad(input, ((self.padding, self.padding), (self.padding, self.padding), (0, 0)), mode='constant')\n        else:\n            padded_input = input\n\n        out_height = height + 2 * self.padding - 2\n        out_width = width + 2 * self.padding - 2\n\n        depthwise_output = np.zeros((out_height, out_width, in_channels))\n\n        # Depthwise convolution\n        for i in range(in_channels):\n            for h in range(out_height):\n                for w in range(out_width):\n                    input_patch = padded_input[h:h+3, w:w+3, i]\n                    depthwise_output[h, w, i] = np.sum(input_patch * self.depthwise_weights[i]) + self.depthwise_biases[i]\n\n        output = self.pointwise_conv.forward(depthwise_output)\n\n        return output\n\n    def backward(self, dL_dout):\n        height, width, out_channels = dL_dout.shape\n        in_channels = self.input.shape[2]\n        dL_ddepthwise = self.pointwise_conv.backward(dL_dout)\n\n        padded_input = np.pad(self.input, ((self.padding, self.padding), (self.padding, self.padding), (0, 0)), mode='constant')\n        padded_dL_dinput = np.zeros_like(padded_input, dtype=\"float64\")\n        dL_ddepthwise_weights = np.zeros_like(self.depthwise_weights)\n        dL_ddepthwise_biases = np.zeros_like(self.depthwise_biases)\n\n        for i in range(in_channels):\n            for h in range(dL_ddepthwise.shape[0]):\n                for w in range(dL_ddepthwise.shape[1]):\n                    input_patch = padded_input[h:h+3, w:w+3, i]\n                    dL_ddepthwise_weights[i] += dL_ddepthwise[h, w, i] * input_patch\n                    dL_ddepthwise_biases[i] += dL_ddepthwise[h, w, i]\n                    padded_dL_dinput[h:h+3, w:w+3, i] += dL_ddepthwise[h, w, i] * self.depthwise_weights[i]\n\n        # Remove padding from dL_dinput\n        if self.padding > 0:\n            dL_dinput = padded_dL_dinput[self.padding:-self.padding, self.padding:-self.padding, :]\n        else:\n            dL_dinput = padded_dL_dinput\n\n        # Update Adam optimizer parameters\n        self.m_dw = self.beta1 * self.m_dw + (1 - self.beta1) * dL_ddepthwise_weights\n        self.v_dw = self.beta2 * self.v_dw + (1 - self.beta2) * (dL_ddepthwise_weights ** 2)\n        self.m_db = self.beta1 * self.m_db + (1 - self.beta1) * dL_ddepthwise_biases\n        self.v_db = self.beta2 * self.v_db + (1 - self.beta2) * (dL_ddepthwise_biases ** 2)\n\n        # Bias correction\n        m_dw_corrected = self.m_dw / (1 - self.beta1)\n        v_dw_corrected = self.v_dw / (1 - self.beta2)\n        m_db_corrected = self.m_db / (1 - self.beta1)\n        v_db_corrected = self.v_db / (1 - self.beta2)\n\n        # Update depthwise weights and biases\n        self.depthwise_weights -= self.learning_rate * m_dw_corrected / (np.sqrt(v_dw_corrected) + self.epsilon)\n        self.depthwise_biases -= self.learning_rate * m_db_corrected / (np.sqrt(v_db_corrected) + self.epsilon)\n\n        return dL_dinput\n","metadata":{"id":"piMLyNh6WCw-","execution":{"iopub.status.busy":"2024-05-21T18:18:25.011219Z","iopub.execute_input":"2024-05-21T18:18:25.012423Z","iopub.status.idle":"2024-05-21T18:18:25.032843Z","shell.execute_reply.started":"2024-05-21T18:18:25.012379Z","shell.execute_reply":"2024-05-21T18:18:25.030768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class maxpool2:\n\n    def forward(self,A_prev):\n\n        ( n_H_prev, n_W_prev, n_C_prev) = A_prev.shape\n        self.last_input = A_prev\n        f = 2\n        stride = 2\n        m=1\n        #print(\"dfa\",self.last_input.shape)\n        n_H = int(1 + (n_H_prev - f) / stride)\n        n_W = int(1 + (n_W_prev - f) / stride)\n        n_C = n_C_prev\n\n        A = np.zeros((n_H, n_W, n_C))\n\n        for i in range(m):\n            for h in range(n_H):\n                for w in range(n_W):\n                    for c in range (n_C):\n\n\n                        vert_start = h * stride\n                        vert_end = h * stride+ f\n                        horiz_start = w * stride\n                        horiz_end = w * stride + f\n\n\n                        a_prev_slice = A_prev[vert_start:vert_end, horiz_start:horiz_end,c]\n\n\n\n                        A[h, w, c] = np.max(a_prev_slice)\n        return A\n    def iterate_region(self,image):\n\n        h,w,_=image.shape\n        new_h=h//2\n        new_w=w//2\n\n        for i in range(new_h):\n            for j in range(new_w):\n                im_region = image[(i*2):(i*2+2),(j*2):(j*2+2)]\n                yield im_region,i,j\n\n    def backward(self,d_l_d_out):\n        d_l_d_input=np.zeros(self.last_input.shape)\n\n        for im_region,i,j in self.iterate_region(self.last_input):\n            h,w,f = im_region.shape\n            amax = np.max(im_region,axis=(0,1))\n\n            for i2 in range(h):\n                for j2 in range(w):\n                    for f2 in range(f):\n                        if im_region[i2,j2,f2] == amax[f2]:\n\n\n                            d_l_d_input[i*2+i2,j*2+j2,f2] = d_l_d_out[i,j,f2]\n\n        return d_l_d_input","metadata":{"id":"RYuHE2bOQbv0","execution":{"iopub.status.busy":"2024-05-21T18:18:25.518016Z","iopub.execute_input":"2024-05-21T18:18:25.518951Z","iopub.status.idle":"2024-05-21T18:18:25.531111Z","shell.execute_reply.started":"2024-05-21T18:18:25.518884Z","shell.execute_reply":"2024-05-21T18:18:25.529881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\n\ndef resize(image, target_size):\n\n    height, width, channels = image.shape\n    new_height, new_width = target_size\n\n    # Calculate scaling factors for each axis\n    y_scale = new_height / height\n    x_scale = new_width / width\n\n    # Interpolation\n    resized_image = np.zeros((new_height, new_width, channels))\n\n    for i in range(new_height):\n        for j in range(new_width):\n            y, x = i / y_scale, j / x_scale\n            y_low, x_low = int(np.floor(y)), int(np.floor(x))\n            y_high, x_high = min(y_low + 1, height - 1), min(x_low + 1, width - 1)\n            y_fraction, x_fraction = y - y_low, x - x_low\n\n            # Bilinear interpolation for each channel\n            for c in range(channels):\n                top_left = image[y_low, x_low, c]\n                top_right = image[y_low, x_high, c]\n                bottom_left = image[y_high, x_low, c]\n                bottom_right = image[y_high, x_high, c]\n\n                top = top_left * (1 - x_fraction) + top_right * x_fraction\n                bottom = bottom_left * (1 - x_fraction) + bottom_right * x_fraction\n\n                resized_image[i, j, c] = top * (1 - y_fraction) + bottom * y_fraction\n\n    return resized_image\n","metadata":{"execution":{"iopub.status.busy":"2024-05-21T18:18:26.178832Z","iopub.execute_input":"2024-05-21T18:18:26.179827Z","iopub.status.idle":"2024-05-21T18:18:26.191295Z","shell.execute_reply.started":"2024-05-21T18:18:26.179776Z","shell.execute_reply":"2024-05-21T18:18:26.189379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class LeakyReLU:\n    def __init__(self, alpha=0.01):\n        self.alpha = alpha\n\n    def forward(self, input):\n        self.input = input\n        return np.where(input > 0, input, self.alpha * input)\n\n    def backward(self, dL_dout):\n        dL_dinput = np.where(self.input > 0, 1, self.alpha)\n        return dL_dout * dL_dinput\n\nclass Sigmoid:\n    def forward(self, input):\n        self.output = 1 / (1 + np.exp(-input))\n        return self.output\n\n    def backward(self, dL_dout):\n        sigmoid_derivative = self.output * (1 - self.output)\n        return dL_dout * sigmoid_derivative","metadata":{"execution":{"iopub.status.busy":"2024-05-21T18:18:26.700678Z","iopub.execute_input":"2024-05-21T18:18:26.701091Z","iopub.status.idle":"2024-05-21T18:18:26.709431Z","shell.execute_reply.started":"2024-05-21T18:18:26.701060Z","shell.execute_reply":"2024-05-21T18:18:26.708109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Encoder:\n    def __init__(self, num_filters, n_c_prev,learning_rate = 0.005):\n        self.conv1 = conv3x3(num_filters, n_c_prev,learning_rate = learning_rate)\n        self.conv2 = conv3x3(num_filters, num_filters,learning_rate = learning_rate)\n        self.pool = maxpool2()\n        self.activation1 = LeakyReLU()  # Using Leaky ReLU\n        self.activation2 = LeakyReLU()\n\n    def forward(self, input):\n        conv1_out = self.activation1.forward(self.conv1.forward(input))\n        conv2_out = self.activation2.forward(self.conv2.forward(conv1_out))\n        pool_out = self.pool.forward(conv2_out)\n        return pool_out, conv2_out\n\n    def backward(self, d_l_d_pool):\n        d_l_d_conv2 = self.pool.backward(d_l_d_pool)\n        d_l_d_conv2 = self.activation2.backward(d_l_d_conv2)\n        d_l_d_conv1 = self.conv2.backward(d_l_d_conv2)\n        d_l_d_conv1 = self.activation1.backward(d_l_d_conv1)\n        d_l_d_input = self.conv1.backward(d_l_d_conv1)\n        return d_l_d_input\n\nclass Decoder:\n    def __init__(self, num_filters, n_c_prev,learning_rate = 0.005):\n        self.upconv = UpConv((2, 2),learning_rate = learning_rate)\n        self.conv1 = conv3x3(num_filters, 2 * n_c_prev,learning_rate = learning_rate)\n        self.conv2 = conv3x3(num_filters, num_filters,learning_rate = learning_rate)\n        self.activation1 = LeakyReLU()  # Using Leaky ReLU\n        self.activation2 = LeakyReLU()\n\n    def forward(self, input, skip_connection):\n        upconv_out = self.upconv.forward(input)\n        skip_connection = resize(skip_connection, upconv_out.shape[:2])\n        merged = np.concatenate((upconv_out, skip_connection), axis=-1)\n        conv1_out = self.activation1.forward(self.conv1.forward(merged))\n        conv2_out = self.activation2.forward(self.conv2.forward(conv1_out))\n        return conv2_out\n\n    def backward(self, d_l_d_conv2):\n        d_l_d_conv2 = self.activation2.backward(d_l_d_conv2)\n        d_l_d_conv2 = self.conv2.backward(d_l_d_conv2)\n        d_l_d_conv1 = self.activation1.backward(d_l_d_conv2)\n        d_l_d_conv1 = self.conv1.backward(d_l_d_conv1)\n        d_l_d_skip = d_l_d_conv1[:, :, d_l_d_conv1.shape[2] // 2:]\n        d_l_d_input = self.upconv.backward(d_l_d_conv1)\n        return d_l_d_input, d_l_d_skip","metadata":{"id":"OxxebkjEWHKT","execution":{"iopub.status.busy":"2024-05-21T18:18:27.325682Z","iopub.execute_input":"2024-05-21T18:18:27.326109Z","iopub.status.idle":"2024-05-21T18:18:27.340877Z","shell.execute_reply.started":"2024-05-21T18:18:27.326076Z","shell.execute_reply":"2024-05-21T18:18:27.339420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class UNet:\n    def __init__(self, num_filters, num_levels,learning_rate = 0.005):\n\n        self.encoders = [Encoder(num_filters, 3,learning_rate = learning_rate)]\n        for _ in range(num_levels - 1):\n            self.encoders.append(Encoder(num_filters * 2, num_filters,learning_rate = learning_rate))\n            num_filters *= 2\n\n        self.bottleneck_conv1 = conv3x3(num_filters, num_filters,learning_rate = learning_rate)\n        self.bottleneck_activation1 = LeakyReLU()\n        self.bottleneck_conv2 = conv3x3(num_filters, num_filters,learning_rate = learning_rate)\n        self.bottleneck_activation2 = LeakyReLU()\n\n        self.decoders = []\n        for i in range(num_levels - 1, 0, -1):\n            self.decoders.append(Decoder(num_filters // 2, num_filters,learning_rate = learning_rate))\n            num_filters //= 2\n        self.decoders.append(Decoder(num_filters, num_filters,learning_rate = learning_rate))\n\n        self.conv1x1 = conv1x1(num_filters,1,learning_rate = learning_rate)\n        self.sigmoid = Sigmoid()\n\n    def forward(self, input):\n\n        skip_connections = []\n        x = input\n\n        #encoders\n        for encoder in self.encoders:\n            x, skip = encoder.forward(x)\n            skip_connections.append(skip)\n\n        skip_connections.reverse()\n\n        x = self.bottleneck_activation1.forward(self.bottleneck_conv1.forward(x))\n        x = self.bottleneck_activation2.forward(self.bottleneck_conv2.forward(x))\n\n        #decoders\n        for decoder, skip in zip(self.decoders, skip_connections):\n            x = decoder.forward(x, skip)\n\n        x = self.conv1x1.forward(x)\n        x = self.sigmoid.forward(x)\n        return x\n\n    def backward(self, d_loss):\n        d_out = d_loss\n        d_skips = []\n\n        d_out = self.sigmoid.backward(d_out)\n        d_out = self.conv1x1.backward(d_out)\n\n        #decoders\n        for decoder in reversed(self.decoders):\n            d_out,d_skip = decoder.backward(d_out)\n            d_skips.append(d_skip)\n\n        # bottleneck\n        d_out = self.bottleneck_activation2.backward(d_out)\n        d_out = self.bottleneck_conv2.backward(d_out)\n        d_out = self.bottleneck_activation1.backward(d_out)\n        d_out = self.bottleneck_conv1.backward(d_out)\n\n        d_skips.reverse()\n        #encoders\n        for i,encoder in enumerate(reversed(self.encoders)):\n            d_out = encoder.backward(d_out+resize(d_skips[i],d_out.shape[:2]))","metadata":{"id":"KhGhHBFUWe_W","execution":{"iopub.status.busy":"2024-05-21T18:18:28.322907Z","iopub.execute_input":"2024-05-21T18:18:28.323544Z","iopub.status.idle":"2024-05-21T18:18:28.336930Z","shell.execute_reply.started":"2024-05-21T18:18:28.323511Z","shell.execute_reply":"2024-05-21T18:18:28.335367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mynet = UNet(num_filters=64, num_levels=4,learning_rate = 0.005)","metadata":{"id":"ts2rX5UldXGO","execution":{"iopub.status.busy":"2024-05-21T18:18:29.234220Z","iopub.execute_input":"2024-05-21T18:18:29.234659Z","iopub.status.idle":"2024-05-21T18:18:29.318767Z","shell.execute_reply.started":"2024-05-21T18:18:29.234622Z","shell.execute_reply":"2024-05-21T18:18:29.317434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from zipfile import ZipFile\nwith ZipFile(\"../input/data-science-bowl-2018/stage1_train.zip\",\"r\") as zip_ref:\n    zip_ref.extractall(\"./stage1_train\")","metadata":{"execution":{"iopub.status.busy":"2024-05-21T18:18:38.660625Z","iopub.execute_input":"2024-05-21T18:18:38.661009Z","iopub.status.idle":"2024-05-21T18:18:44.639975Z","shell.execute_reply.started":"2024-05-21T18:18:38.660979Z","shell.execute_reply":"2024-05-21T18:18:44.638303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Imports\nimport os\nimport sys\nimport random\n\nimport numpy as np\nimport cv2\nimport matplotlib.pyplot as plt\n\nimport tensorflow as tf\nfrom tensorflow import keras","metadata":{"execution":{"iopub.status.busy":"2024-05-21T18:18:47.468996Z","iopub.execute_input":"2024-05-21T18:18:47.469351Z","iopub.status.idle":"2024-05-21T18:18:51.372375Z","shell.execute_reply.started":"2024-05-21T18:18:47.469322Z","shell.execute_reply":"2024-05-21T18:18:51.371466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Seeding\nseed = 42\nrandom.seed = seed\nnp.random.seed = seed\ntf.seed = seed","metadata":{"execution":{"iopub.status.busy":"2024-05-21T18:18:51.373771Z","iopub.execute_input":"2024-05-21T18:18:51.374757Z","iopub.status.idle":"2024-05-21T18:18:51.378705Z","shell.execute_reply.started":"2024-05-21T18:18:51.374725Z","shell.execute_reply":"2024-05-21T18:18:51.377903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class DataGen(keras.utils.Sequence):\n    def __init__(self, ids, path, batch_size=8, image_size=128):\n        self.ids = ids\n        self.path = path\n        self.batch_size = batch_size\n        self.image_size = image_size\n        self.on_epoch_end()\n        \n    \n        \n    def __load__(self, id_name):\n        ## Path\n        image_path = os.path.join(self.path, id_name, \"images\", id_name) + \".png\"\n        mask_path = os.path.join(self.path, id_name, \"masks/\")\n        all_masks = os.listdir(mask_path)\n        \n        ## Reading Image\n        image = cv2.imread(image_path, 1)\n        image = cv2.resize(image, (self.image_size, self.image_size))\n        \n        mask = np.zeros((self.image_size, self.image_size, 1))\n        \n        ## Reading Masks\n        for name in all_masks:\n            _mask_path = mask_path + name\n            _mask_image = cv2.imread(_mask_path, -1)\n            _mask_image = cv2.resize(_mask_image, (self.image_size, self.image_size)) #128x128\n            _mask_image = np.expand_dims(_mask_image, axis=-1)\n            mask = np.maximum(mask, _mask_image)\n            \n        ## Normalizing \n        image = image/255.0\n        mask = mask/255.0\n        \n        return image, mask\n    \n    def __getitem__(self, index):\n        if(index+1)*self.batch_size > len(self.ids):\n            self.batch_size = len(self.ids) - index*self.batch_size\n        \n        files_batch = self.ids[index*self.batch_size : (index+1)*self.batch_size]\n        \n        image = []\n        mask  = []\n        \n        for id_name in files_batch:\n            _img, _mask = self.__load__(id_name)\n            image.append(_img)\n            mask.append(_mask)\n            \n        image = np.array(image)\n        mask  = np.array(mask)\n        \n        return image, mask\n    \n    def on_epoch_end(self):\n        # Shuffle data at the end of each epoch to reduce bias\n        random.shuffle(self.ids)\n    \n    def __len__(self):\n        return int(np.ceil(len(self.ids)/float(self.batch_size)))","metadata":{"execution":{"iopub.status.busy":"2024-05-21T18:18:51.381750Z","iopub.execute_input":"2024-05-21T18:18:51.382220Z","iopub.status.idle":"2024-05-21T18:18:51.396651Z","shell.execute_reply.started":"2024-05-21T18:18:51.382181Z","shell.execute_reply":"2024-05-21T18:18:51.395380Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_size = 128\n\ntrain_path = \"stage1_train/\"\nbatch_size = 5\n\n## Training Ids\ntrain_ids = next(os.walk(train_path))[1]\n\n## Validation Data Size\nval_data_size = 10\n\nvalid_ids = train_ids[:val_data_size]\ntrain_ids = train_ids[val_data_size:]","metadata":{"execution":{"iopub.status.busy":"2024-05-21T18:18:51.399449Z","iopub.execute_input":"2024-05-21T18:18:51.399839Z","iopub.status.idle":"2024-05-21T18:18:51.415113Z","shell.execute_reply.started":"2024-05-21T18:18:51.399804Z","shell.execute_reply":"2024-05-21T18:18:51.414167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_gen = DataGen(train_ids, train_path, image_size=image_size, batch_size=batch_size)\nvalid_gen = DataGen(valid_ids, train_path, image_size=image_size, batch_size=batch_size)","metadata":{"execution":{"iopub.status.busy":"2024-05-21T18:18:51.416394Z","iopub.execute_input":"2024-05-21T18:18:51.416910Z","iopub.status.idle":"2024-05-21T18:18:51.432922Z","shell.execute_reply.started":"2024-05-21T18:18:51.416873Z","shell.execute_reply":"2024-05-21T18:18:51.431974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_num=0\nj=2\nx,y = train_gen.__getitem__(batch_num)\nplt.subplot(121)\nplt.imshow(x[j])\nplt.subplot(122)\nplt.imshow(y[j],cmap = \"gray\")","metadata":{"execution":{"iopub.status.busy":"2024-05-21T18:18:51.434540Z","iopub.execute_input":"2024-05-21T18:18:51.437486Z","iopub.status.idle":"2024-05-21T18:18:52.078710Z","shell.execute_reply.started":"2024-05-21T18:18:51.437441Z","shell.execute_reply":"2024-05-21T18:18:52.077393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_num=2\nj=2\nx,y = train_gen.__getitem__(batch_num)\nvalidation_x,validation_y = x[j],y[j]\nplt.subplot(121)\nplt.imshow(x[j])\nplt.subplot(122)\nplt.imshow(y[j],cmap = \"gray\")","metadata":{"execution":{"iopub.status.busy":"2024-05-21T18:18:52.080172Z","iopub.execute_input":"2024-05-21T18:18:52.080607Z","iopub.status.idle":"2024-05-21T18:18:52.723527Z","shell.execute_reply.started":"2024-05-21T18:18:52.080568Z","shell.execute_reply":"2024-05-21T18:18:52.722373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\n\nepochs = 12\nbatch_size = 8\nepsilon = 1e-12  # to avoid log(0)\n\nfor epoch in range(epochs):\n    num_batches = 2\n    train_loss=0\n    for _ in range(num_batches):\n        batch_num = np.random.randint(num_batches, size=1)[0]\n        x, y = train_gen.__getitem__(batch_num)\n        \n        dl = np.zeros_like(y[0])\n        \n        shuffled_x = np.arange(len(x))\n        np.random.shuffle(shuffled_x)\n        \n        for i in shuffled_x:\n            forward_pass = mynet.forward(x[i])\n            dl += ((forward_pass - y[i]) / (forward_pass * (1 - forward_pass) + epsilon)) / y[i].size\n            train_loss += -np.mean(y[i] * np.log(forward_pass + epsilon) + (1 - y[i]) * np.log(1 - forward_pass + epsilon))\n        \n        mynet.backward(dl / len(x))\n    \n    # Validation after each epoch\n    forward_pass = mynet.forward(validation_x)\n    loss = -np.mean(validation_y * np.log(forward_pass + epsilon) + (1 - validation_y) * np.log(1 - forward_pass + epsilon))\n    print(\"Epoch:\", epoch + 1,\"training_loss\",train_loss/15,\"Validation_Loss:\", loss)\n    \n    # Plotting\n    plt.figure(figsize=(20, 30))\n    plt.subplot(131)\n    plt.imshow(validation_x)\n    plt.title(\"gt\")\n    plt.subplot(132)\n    plt.imshow(validation_y, cmap=\"gray\")\n    plt.title(\"mask_gt\")\n    plt.subplot(133)\n    plt.imshow(forward_pass, cmap=\"gray\")\n    plt.title(\"mask_prediction\")\n    plt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-05-21T18:18:52.725065Z","iopub.execute_input":"2024-05-21T18:18:52.725392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}