Upload
4
.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
dataset
|
||||
*.pth
|
||||
.vscode
|
||||
__pycache__
|
80
model.py
Normal file
@ -0,0 +1,80 @@
|
||||
import torch.nn as nn
|
||||
import torch.nn.functional as F
|
||||
|
||||
|
||||
class CNNv1(nn.Module):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.conv1 = nn.Conv2d(1, 16, 3, 1)
|
||||
self.conv2 = nn.Conv2d(16, 32, 3, 1)
|
||||
self.fc1 = nn.Linear(32 * 54 * 54, 128)
|
||||
self.fc2 = nn.Linear(128, 2)
|
||||
|
||||
def forward(self, x):
|
||||
x = F.relu(self.conv1(x))
|
||||
x = F.max_pool2d(x, 2, 2)
|
||||
x = F.relu(self.conv2(x))
|
||||
x = F.max_pool2d(x, 2, 2)
|
||||
x = x.view(-1, 32 * 54 * 54)
|
||||
x = F.relu(self.fc1(x))
|
||||
x = self.fc2(x)
|
||||
return x
|
||||
|
||||
|
||||
class CNNv2(nn.Module):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.conv1 = nn.Conv2d(1, 32, 3, 1)
|
||||
self.bn1 = nn.BatchNorm2d(32)
|
||||
self.conv2 = nn.Conv2d(32, 64, 3, 1)
|
||||
self.bn2 = nn.BatchNorm2d(64)
|
||||
self.conv3 = nn.Conv2d(64, 128, 3, 1)
|
||||
self.bn3 = nn.BatchNorm2d(128)
|
||||
self.fc1 = nn.Linear(128 * 26 * 26, 256)
|
||||
self.dropout = nn.Dropout(0.5)
|
||||
self.fc2 = nn.Linear(256, 2)
|
||||
|
||||
def forward(self, x):
|
||||
x = F.relu(self.bn1(self.conv1(x)))
|
||||
x = F.max_pool2d(x, 2, 2)
|
||||
x = F.relu(self.bn2(self.conv2(x)))
|
||||
x = F.max_pool2d(x, 2, 2)
|
||||
x = F.relu(self.bn3(self.conv3(x)))
|
||||
x = F.max_pool2d(x, 2, 2)
|
||||
x = x.view(-1, 128 * 26 * 26)
|
||||
x = F.relu(self.fc1(x))
|
||||
x = self.dropout(x)
|
||||
x = self.fc2(x)
|
||||
return x
|
||||
|
||||
|
||||
class CNNv3(nn.Module):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.conv1 = nn.Conv2d(1, 32, 3, 1)
|
||||
self.bn1 = nn.BatchNorm2d(32)
|
||||
self.conv2 = nn.Conv2d(32, 64, 3, 1)
|
||||
self.bn2 = nn.BatchNorm2d(64)
|
||||
self.conv3 = nn.Conv2d(64, 128, 3, 1)
|
||||
self.bn3 = nn.BatchNorm2d(128)
|
||||
self.conv4 = nn.Conv2d(128, 256, 3, 1)
|
||||
self.bn4 = nn.BatchNorm2d(256)
|
||||
self.global_avg_pool = nn.AdaptiveAvgPool2d((1, 1))
|
||||
self.fc1 = nn.Linear(256, 128)
|
||||
self.dropout = nn.Dropout(0.5)
|
||||
self.fc2 = nn.Linear(128, 2)
|
||||
|
||||
def forward(self, x):
|
||||
x = F.relu(self.bn1(self.conv1(x)))
|
||||
x = F.max_pool2d(x, 2, 2)
|
||||
x = F.relu(self.bn2(self.conv2(x)))
|
||||
x = F.max_pool2d(x, 2, 2)
|
||||
x = F.relu(self.bn3(self.conv3(x)))
|
||||
x = F.max_pool2d(x, 2, 2)
|
||||
x = F.relu(self.bn4(self.conv4(x)))
|
||||
x = self.global_avg_pool(x)
|
||||
x = x.view(-1, 256)
|
||||
x = F.relu(self.fc1(x))
|
||||
x = self.dropout(x)
|
||||
x = self.fc2(x)
|
||||
return x
|
28
predict.py
Normal file
@ -0,0 +1,28 @@
|
||||
from sys import argv
|
||||
import torch
|
||||
from PIL import Image
|
||||
|
||||
from preprocess import TRANSFORM
|
||||
from settings import MODEL_FILENAME, CLASSES, OurCNN
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
if len(argv) < 2:
|
||||
print("Usage: predict.py <path>")
|
||||
exit(1)
|
||||
|
||||
model = OurCNN()
|
||||
model.load_state_dict(torch.load(MODEL_FILENAME))
|
||||
|
||||
# Load the image
|
||||
image = Image.open(argv[1])
|
||||
|
||||
# Preprocess the image
|
||||
tensor: torch.Tensor = TRANSFORM(image) # type: ignore
|
||||
|
||||
# get the prediction
|
||||
with torch.no_grad():
|
||||
output = model(tensor.unsqueeze(0))
|
||||
_, predicted = torch.max(output, 1)
|
||||
|
||||
print(CLASSES[predicted])
|
13
preprocess.py
Normal file
@ -0,0 +1,13 @@
|
||||
from torchvision import transforms
|
||||
from settings import (
|
||||
IMAGE_TARGET_SIZE,
|
||||
)
|
||||
|
||||
|
||||
TRANSFORM = transforms.Compose(
|
||||
[
|
||||
transforms.Resize((IMAGE_TARGET_SIZE, IMAGE_TARGET_SIZE)),
|
||||
transforms.Grayscale(num_output_channels=1),
|
||||
transforms.ToTensor(),
|
||||
]
|
||||
)
|
BIN
raw/test/civilian/1000.jpg
Normal file
After Width: | Height: | Size: 13 KiB |
BIN
raw/test/civilian/1002.jpg
Normal file
After Width: | Height: | Size: 11 KiB |
BIN
raw/test/civilian/1003.jpg
Normal file
After Width: | Height: | Size: 12 KiB |
BIN
raw/test/civilian/1006.jpg
Normal file
After Width: | Height: | Size: 9.0 KiB |
BIN
raw/test/civilian/1008.jpg
Normal file
After Width: | Height: | Size: 7.0 KiB |
BIN
raw/test/civilian/101.jpg
Normal file
After Width: | Height: | Size: 8.4 KiB |
BIN
raw/test/civilian/1012.jpg
Normal file
After Width: | Height: | Size: 11 KiB |
BIN
raw/test/civilian/102.jpg
Normal file
After Width: | Height: | Size: 11 KiB |
BIN
raw/test/civilian/1020.jpg
Normal file
After Width: | Height: | Size: 6.3 KiB |
BIN
raw/test/civilian/1022.jpg
Normal file
After Width: | Height: | Size: 11 KiB |
BIN
raw/test/civilian/1025.jpg
Normal file
After Width: | Height: | Size: 9.7 KiB |
BIN
raw/test/civilian/1027.jpg
Normal file
After Width: | Height: | Size: 6.5 KiB |
BIN
raw/test/civilian/1030.jpg
Normal file
After Width: | Height: | Size: 10 KiB |
BIN
raw/test/civilian/1032.jpg
Normal file
After Width: | Height: | Size: 7.0 KiB |
BIN
raw/test/civilian/1037.jpg
Normal file
After Width: | Height: | Size: 7.5 KiB |
BIN
raw/test/civilian/1038.jpg
Normal file
After Width: | Height: | Size: 8.5 KiB |
BIN
raw/test/civilian/104.jpg
Normal file
After Width: | Height: | Size: 8.5 KiB |
BIN
raw/test/civilian/1043.jpg
Normal file
After Width: | Height: | Size: 9.8 KiB |
BIN
raw/test/civilian/1051.jpg
Normal file
After Width: | Height: | Size: 8.5 KiB |
BIN
raw/test/civilian/1053.jpg
Normal file
After Width: | Height: | Size: 6.3 KiB |
BIN
raw/test/civilian/1055.jpg
Normal file
After Width: | Height: | Size: 8.1 KiB |
BIN
raw/test/civilian/1057.jpg
Normal file
After Width: | Height: | Size: 13 KiB |
BIN
raw/test/civilian/1059.jpg
Normal file
After Width: | Height: | Size: 5.9 KiB |
BIN
raw/test/civilian/1060.jpg
Normal file
After Width: | Height: | Size: 6.2 KiB |
BIN
raw/test/civilian/1064.jpg
Normal file
After Width: | Height: | Size: 11 KiB |
BIN
raw/test/civilian/1066.jpg
Normal file
After Width: | Height: | Size: 6.7 KiB |
BIN
raw/test/civilian/1072.jpg
Normal file
After Width: | Height: | Size: 7.3 KiB |
BIN
raw/test/civilian/1073.jpg
Normal file
After Width: | Height: | Size: 9.9 KiB |
BIN
raw/test/civilian/1075.jpg
Normal file
After Width: | Height: | Size: 7.6 KiB |
BIN
raw/test/civilian/1077.jpg
Normal file
After Width: | Height: | Size: 15 KiB |
BIN
raw/test/civilian/1080.jpg
Normal file
After Width: | Height: | Size: 12 KiB |
BIN
raw/test/civilian/1082.jpg
Normal file
After Width: | Height: | Size: 8.4 KiB |
BIN
raw/test/civilian/1084.jpg
Normal file
After Width: | Height: | Size: 7.9 KiB |
BIN
raw/test/civilian/1085.jpg
Normal file
After Width: | Height: | Size: 7.1 KiB |
BIN
raw/test/civilian/1086.jpg
Normal file
After Width: | Height: | Size: 12 KiB |
BIN
raw/test/civilian/109.jpg
Normal file
After Width: | Height: | Size: 9.5 KiB |
BIN
raw/test/civilian/1092.jpg
Normal file
After Width: | Height: | Size: 9.0 KiB |
BIN
raw/test/civilian/1093.jpg
Normal file
After Width: | Height: | Size: 8.0 KiB |
BIN
raw/test/civilian/1094.jpg
Normal file
After Width: | Height: | Size: 7.0 KiB |
BIN
raw/test/civilian/110.jpg
Normal file
After Width: | Height: | Size: 5.7 KiB |
BIN
raw/test/civilian/1100.jpg
Normal file
After Width: | Height: | Size: 7.5 KiB |
BIN
raw/test/civilian/1101.jpg
Normal file
After Width: | Height: | Size: 9.8 KiB |
BIN
raw/test/civilian/1103.jpg
Normal file
After Width: | Height: | Size: 8.7 KiB |
BIN
raw/test/civilian/1105.jpg
Normal file
After Width: | Height: | Size: 5.5 KiB |
BIN
raw/test/civilian/1106.jpg
Normal file
After Width: | Height: | Size: 4.7 KiB |
BIN
raw/test/civilian/1107.jpg
Normal file
After Width: | Height: | Size: 7.6 KiB |
BIN
raw/test/civilian/111.jpg
Normal file
After Width: | Height: | Size: 6.0 KiB |
BIN
raw/test/civilian/1110.jpg
Normal file
After Width: | Height: | Size: 7.5 KiB |
BIN
raw/test/civilian/1114.jpg
Normal file
After Width: | Height: | Size: 9.5 KiB |
BIN
raw/test/civilian/1115.jpg
Normal file
After Width: | Height: | Size: 9.0 KiB |
BIN
raw/test/civilian/1117.jpg
Normal file
After Width: | Height: | Size: 9.9 KiB |
BIN
raw/test/civilian/112.jpg
Normal file
After Width: | Height: | Size: 11 KiB |
BIN
raw/test/civilian/113.jpg
Normal file
After Width: | Height: | Size: 11 KiB |
BIN
raw/test/civilian/114.jpg
Normal file
After Width: | Height: | Size: 9.0 KiB |
BIN
raw/test/civilian/115.jpg
Normal file
After Width: | Height: | Size: 6.3 KiB |
BIN
raw/test/civilian/116.jpg
Normal file
After Width: | Height: | Size: 7.0 KiB |
BIN
raw/test/civilian/117.jpg
Normal file
After Width: | Height: | Size: 8.6 KiB |
BIN
raw/test/civilian/118.jpg
Normal file
After Width: | Height: | Size: 12 KiB |
BIN
raw/test/civilian/119.jpg
Normal file
After Width: | Height: | Size: 11 KiB |
BIN
raw/test/civilian/12.jpg
Normal file
After Width: | Height: | Size: 13 KiB |
BIN
raw/test/civilian/120.jpg
Normal file
After Width: | Height: | Size: 7.2 KiB |
BIN
raw/test/civilian/1202.jpg
Normal file
After Width: | Height: | Size: 9.1 KiB |
BIN
raw/test/civilian/1204.jpg
Normal file
After Width: | Height: | Size: 13 KiB |
BIN
raw/test/civilian/1208.jpg
Normal file
After Width: | Height: | Size: 7.4 KiB |
BIN
raw/test/civilian/121.jpg
Normal file
After Width: | Height: | Size: 11 KiB |
BIN
raw/test/civilian/1218.jpg
Normal file
After Width: | Height: | Size: 6.8 KiB |
BIN
raw/test/civilian/122.jpg
Normal file
After Width: | Height: | Size: 8.5 KiB |
BIN
raw/test/civilian/1220.jpg
Normal file
After Width: | Height: | Size: 8.2 KiB |
BIN
raw/test/civilian/1222.jpg
Normal file
After Width: | Height: | Size: 11 KiB |
BIN
raw/test/civilian/1224.jpg
Normal file
After Width: | Height: | Size: 9.7 KiB |
BIN
raw/test/civilian/1227.jpg
Normal file
After Width: | Height: | Size: 7.2 KiB |
BIN
raw/test/civilian/1228.jpg
Normal file
After Width: | Height: | Size: 10 KiB |
BIN
raw/test/civilian/123.jpg
Normal file
After Width: | Height: | Size: 6.4 KiB |
BIN
raw/test/civilian/1232.jpg
Normal file
After Width: | Height: | Size: 10 KiB |
BIN
raw/test/civilian/1235.jpg
Normal file
After Width: | Height: | Size: 10 KiB |
BIN
raw/test/civilian/124.jpg
Normal file
After Width: | Height: | Size: 9.6 KiB |
BIN
raw/test/civilian/1241.jpg
Normal file
After Width: | Height: | Size: 8.5 KiB |
BIN
raw/test/civilian/1246.jpg
Normal file
After Width: | Height: | Size: 12 KiB |
BIN
raw/test/civilian/1248.jpg
Normal file
After Width: | Height: | Size: 11 KiB |
BIN
raw/test/civilian/1251.jpg
Normal file
After Width: | Height: | Size: 8.3 KiB |
BIN
raw/test/civilian/1255.jpg
Normal file
After Width: | Height: | Size: 7.9 KiB |
BIN
raw/test/civilian/126.jpg
Normal file
After Width: | Height: | Size: 8.2 KiB |
BIN
raw/test/civilian/1260.jpg
Normal file
After Width: | Height: | Size: 12 KiB |
BIN
raw/test/civilian/1262.jpg
Normal file
After Width: | Height: | Size: 12 KiB |
BIN
raw/test/civilian/1263.jpg
Normal file
After Width: | Height: | Size: 10 KiB |
BIN
raw/test/civilian/1265.jpg
Normal file
After Width: | Height: | Size: 10 KiB |
BIN
raw/test/civilian/1268.jpg
Normal file
After Width: | Height: | Size: 7.4 KiB |
BIN
raw/test/civilian/1270.jpg
Normal file
After Width: | Height: | Size: 7.3 KiB |
BIN
raw/test/civilian/1271.jpg
Normal file
After Width: | Height: | Size: 8.9 KiB |
BIN
raw/test/civilian/1272.jpg
Normal file
After Width: | Height: | Size: 8.2 KiB |
BIN
raw/test/civilian/1274.jpg
Normal file
After Width: | Height: | Size: 8.2 KiB |
BIN
raw/test/civilian/1275.jpg
Normal file
After Width: | Height: | Size: 8.6 KiB |
BIN
raw/test/civilian/1279.jpg
Normal file
After Width: | Height: | Size: 8.5 KiB |
BIN
raw/test/civilian/128.jpg
Normal file
After Width: | Height: | Size: 16 KiB |
BIN
raw/test/civilian/1282.jpg
Normal file
After Width: | Height: | Size: 11 KiB |
BIN
raw/test/civilian/1283.jpg
Normal file
After Width: | Height: | Size: 8.3 KiB |