-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodels.py
70 lines (58 loc) · 2.43 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
'''
Author: Ambareesh Ravi
Date: July 19, 2021
Title: models.py
Description:
Contains the PyTorch CNN model for rooftop pixel classification/segmentation
'''
# imports
import torch
import torch.nn as nn
import torch.nn.functional as F
# PyTorch model for Pixelwise rooftop classifcation
class PixelClassifier_CNN(nn.Module):
def __init__(self, in_channels = 3):
'''
Overrides nn.Module to create a PyTorch model for classifying pixels with roofs in buildings
Args:
in_channels - number of input channels as <int> [3 for RGB, 1 for Grayscale]
Returns:
-
Exception:
-
'''
super().__init__()
# Class variables
self.in_channels = in_channels
# Given Layers
self.conv1 = nn.Conv2d(in_channels = self.in_channels, out_channels = 16, kernel_size = 3, padding = 1)
self.conv2 = nn.Conv2d(in_channels = 16, out_channels = 16, kernel_size = 5, padding = 2)
self.conv3 = nn.Conv2d(in_channels = 16, out_channels = 32, kernel_size = 3, padding = 1)
self.tr_conv1 = nn.ConvTranspose2d(in_channels=32, out_channels=32, kernel_size=2, padding = 0)
self.conv4 = nn.Conv2d(in_channels = 32, out_channels = 16, kernel_size = 3, padding = 0)
self.max_pool = nn.MaxPool2d(2, 2)
# Extra layers
self.output_layer = nn.Sequential(
nn.ConvTranspose2d(in_channels=16, out_channels=16, kernel_size=3, stride = 2, padding=0),
nn.ReLU(),
nn.Conv2d(in_channels=16, out_channels=1, kernel_size=2, padding=1),
nn.Sigmoid()
)
def forward(self, input_images):
'''
Processes the input images through the CNN
Args:
input_images - batch of input images as <torch.Tensor>
Returns:
output_masks - grayscale mask containing white pixels for roofs and black pixels for others as <torch.Tensor>
Exception:
-
'''
conv1_out = F.relu(self.conv1(input_images))
conv2_out = F.relu(self.conv2(conv1_out))
max_pool1_out = self.max_pool(conv2_out)
conv3_out = F.relu(self.conv3(max_pool1_out))
tr_conv1_out = self.tr_conv1(conv3_out)
conv4_out = self.conv4(tr_conv1_out)
output_masks = self.output_layer(conv4_out)
return output_masks