Spaces:
Running
Running
Jason Adrian
commited on
Commit
•
62248f2
1
Parent(s):
3858779
update on app
Browse files
app.py
CHANGED
@@ -2,8 +2,133 @@ import gradio as gr
|
|
2 |
import torch
|
3 |
from torchvision.transforms import transforms
|
4 |
import numpy as np
|
|
|
|
|
5 |
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
model = ResNet18(1, 5)
|
9 |
|
|
|
2 |
import torch
|
3 |
from torchvision.transforms import transforms
|
4 |
import numpy as np
|
5 |
+
from typing import Optional
|
6 |
+
import torch.nn as nn
|
7 |
|
8 |
+
class BasicBlock(nn.Module):
|
9 |
+
"""ResNet Basic Block.
|
10 |
+
|
11 |
+
Parameters
|
12 |
+
----------
|
13 |
+
in_channels : int
|
14 |
+
Number of input channels
|
15 |
+
out_channels : int
|
16 |
+
Number of output channels
|
17 |
+
stride : int, optional
|
18 |
+
Convolution stride size, by default 1
|
19 |
+
identity_downsample : Optional[torch.nn.Module], optional
|
20 |
+
Downsampling layer, by default None
|
21 |
+
"""
|
22 |
+
|
23 |
+
def __init__(self,
|
24 |
+
in_channels: int,
|
25 |
+
out_channels: int,
|
26 |
+
stride: int = 1,
|
27 |
+
identity_downsample: Optional[torch.nn.Module] = None):
|
28 |
+
super(BasicBlock, self).__init__()
|
29 |
+
self.conv1 = nn.Conv2d(in_channels,
|
30 |
+
out_channels,
|
31 |
+
kernel_size = 3,
|
32 |
+
stride = stride,
|
33 |
+
padding = 1)
|
34 |
+
self.bn1 = nn.BatchNorm2d(out_channels)
|
35 |
+
self.relu = nn.ReLU()
|
36 |
+
self.conv2 = nn.Conv2d(out_channels,
|
37 |
+
out_channels,
|
38 |
+
kernel_size = 3,
|
39 |
+
stride = 1,
|
40 |
+
padding = 1)
|
41 |
+
self.bn2 = nn.BatchNorm2d(out_channels)
|
42 |
+
self.identity_downsample = identity_downsample
|
43 |
+
|
44 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
45 |
+
"""Apply forward computation."""
|
46 |
+
identity = x
|
47 |
+
x = self.conv1(x)
|
48 |
+
x = self.bn1(x)
|
49 |
+
x = self.relu(x)
|
50 |
+
x = self.conv2(x)
|
51 |
+
x = self.bn2(x)
|
52 |
+
|
53 |
+
# Apply an operation to the identity output.
|
54 |
+
# Useful to reduce the layer size and match from conv2 output
|
55 |
+
if self.identity_downsample is not None:
|
56 |
+
identity = self.identity_downsample(identity)
|
57 |
+
x += identity
|
58 |
+
x = self.relu(x)
|
59 |
+
return x
|
60 |
+
|
61 |
+
class ResNet18(nn.Module):
|
62 |
+
"""Construct ResNet-18 Model.
|
63 |
+
|
64 |
+
Parameters
|
65 |
+
----------
|
66 |
+
input_channels : int
|
67 |
+
Number of input channels
|
68 |
+
num_classes : int
|
69 |
+
Number of class outputs
|
70 |
+
"""
|
71 |
+
|
72 |
+
def __init__(self, input_channels, num_classes):
|
73 |
+
|
74 |
+
super(ResNet18, self).__init__()
|
75 |
+
self.conv1 = nn.Conv2d(input_channels,
|
76 |
+
64, kernel_size = 7,
|
77 |
+
stride = 2, padding=3)
|
78 |
+
self.bn1 = nn.BatchNorm2d(64)
|
79 |
+
self.relu = nn.ReLU()
|
80 |
+
self.maxpool = nn.MaxPool2d(kernel_size = 3,
|
81 |
+
stride = 2,
|
82 |
+
padding = 1)
|
83 |
+
|
84 |
+
self.layer1 = self._make_layer(64, 64, stride = 1)
|
85 |
+
self.layer2 = self._make_layer(64, 128, stride = 2)
|
86 |
+
self.layer3 = self._make_layer(128, 256, stride = 2)
|
87 |
+
self.layer4 = self._make_layer(256, 512, stride = 2)
|
88 |
+
|
89 |
+
# Last layers
|
90 |
+
self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
|
91 |
+
self.fc = nn.Linear(512, num_classes)
|
92 |
+
|
93 |
+
def identity_downsample(self, in_channels: int, out_channels: int) -> nn.Module:
|
94 |
+
"""Downsampling block to reduce the feature sizes."""
|
95 |
+
return nn.Sequential(
|
96 |
+
nn.Conv2d(in_channels,
|
97 |
+
out_channels,
|
98 |
+
kernel_size = 3,
|
99 |
+
stride = 2,
|
100 |
+
padding = 1),
|
101 |
+
nn.BatchNorm2d(out_channels)
|
102 |
+
)
|
103 |
+
|
104 |
+
def _make_layer(self, in_channels: int, out_channels: int, stride: int) -> nn.Module:
|
105 |
+
"""Create sequential basic block."""
|
106 |
+
identity_downsample = None
|
107 |
+
|
108 |
+
# Add downsampling function
|
109 |
+
if stride != 1:
|
110 |
+
identity_downsample = self.identity_downsample(in_channels, out_channels)
|
111 |
+
|
112 |
+
return nn.Sequential(
|
113 |
+
BasicBlock(in_channels, out_channels, identity_downsample=identity_downsample, stride=stride),
|
114 |
+
BasicBlock(out_channels, out_channels)
|
115 |
+
)
|
116 |
+
|
117 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
118 |
+
x = self.conv1(x)
|
119 |
+
x = self.bn1(x)
|
120 |
+
x = self.relu(x)
|
121 |
+
x = self.maxpool(x)
|
122 |
+
|
123 |
+
x = self.layer1(x)
|
124 |
+
x = self.layer2(x)
|
125 |
+
x = self.layer3(x)
|
126 |
+
x = self.layer4(x)
|
127 |
+
|
128 |
+
x = self.avgpool(x)
|
129 |
+
x = x.view(x.shape[0], -1)
|
130 |
+
x = self.fc(x)
|
131 |
+
return x
|
132 |
|
133 |
model = ResNet18(1, 5)
|
134 |
|