So let’s just code the Unet architecture.
Full code : Github
1. Natural Language Generation:
The Commercial State of the Art in 2020
2. This Entire Article Was Written by Open AI’s GPT2
3. Learning To Classify Images Without Labels
4. Becoming a Data Scientist, Data Analyst, Financial Analyst and Research Analyst
As we have seen in the architecture a 3×3 double convolution layer followed by the Relu activation function is used on both right and left side.
a dual_conv() function is created with the in & out channel parameters.
inside the function, a Sequential layer of two convolution layers with kernel size 3 (3x3 conv) each followed by a relu activation is made.
dual_conv() returns the conv a sequential layer
We will create a class Unet() and make the layers of the left side and a maxpool layer(the red arrow in image). In each layer, we use dual_conv() as it uses dual convolution. Let's just name the layer as dwn_conv (5 layers are on the left side).
Make a forward() function inside the class in which we will forward pass the input(image) to the left side layers
WALAAHH… left side is complete
After passing the image from left side the interesting part came Right side of the architecture.So let’s just implement that.
Now let’s declare the 4 layers of the right side in the __init__() function of the class and the last 1×1 conv. output layer. 2×2 transpose convolution is used instead of maxpool as in the left side
As we seen in the architecture the input image on the right side is combination of the image from left side (grey arrow in image) and its previous layer. But for combining the imaged it has to be the same size images so lets make a function out side the class to crop the image.
what happens in crop_tensor() images = tensortensor = image from the left side which needs to be cropped
target_tensor = image on the right side whose size has to be matched by cropping left side imagetake the size of both the tensors in target_size and tensor_size .
 takes only last value of tensor width , as height and width are same eg:torch.Size([1, 512, 64, 64]) so it take  = 64now we got the size of both the images we will subtract the size of lower tensor'target_size' from bigger one 'ternsor_size' .Suppose target_size=56 and tensor_size=64 so delta(subtracted size) will be 8 but we will crop image from all corners 'height' * 'width' so we will divide the delta by 2 so that height and width can be cropped equally
8 =>h*W = 4*4
now return the cropped tensor[4:64-4, 4:64-4] => 4:60, 4:60 in above example we need 56x56 img
[:, :,] = all dimensions
[delta:tensor_size-delta, delta:tensor_size-delta] = cropped image
see the below image height as an example
Forward pass on the right side we will make this in the forward() function inside the class
first is transpose layer x takes input x9 the last layer of left side.
now combine the images of x and layer in front of it(left side), but wait the size of two images are different so we will crop the image using crop_tensor() function, size of x is smaller then x7 print(x7.size()) :torch.Size([1, 512, 64, 64])
print(x.size()) :torch.Size([1, 512, 56, 56])combine both the images using torch.cat() and pass it to up_conv()
Lets just make a image (as image is just a tensor ) by using torch.rand()
572 x572 image height x width as Unet take 572×572 image as input
and pass it to the model.
Full code : Github