-
Notifications
You must be signed in to change notification settings - Fork 0
/
CNN_test.py
146 lines (110 loc) · 4.4 KB
/
CNN_test.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import cv2
import matplotlib.pyplot as plt
import numpy as np
import os
from utils import *
from sklearn.model_selection import train_test_split
from torch.utils.data import Dataset, DataLoader
import torchvision.transforms as transforms
from torch import optim
import torch
from torch import nn
from tqdm import tqdm
video_paths = []
ROI_paths = []
label_paths = []
for root, directories, files in os.walk(r'dataset'):
for file in files:
file_path = os.path.join(root, file)
if file.endswith('.avi'):
video_paths = np.append(video_paths, file_path)
elif file.endswith('ROI.txt'):
ROI_paths = np.append(ROI_paths, file_path)
elif file.endswith('manual.txt'):
label_paths = np.append(label_paths, file_path)
video_paths_train, video_paths_test = train_test_split(video_paths, test_size=0.25, random_state=42)
ROI_paths_train, ROI_paths_test = train_test_split(ROI_paths, test_size=0.25, random_state=42)
label_paths_train, label_paths_test = train_test_split(label_paths, test_size=0.25, random_state=42)
# labels_train, videos_without_rois_train = concat_vid_rois_and_labels(video_paths_train, ROI_paths_train, label_paths_train)
labels_test, videos_without_rois_test = concat_vid_rois_and_labels(video_paths_test, ROI_paths_test, label_paths_test)
transform = transforms.Compose([
transforms.RandomHorizontalFlip(),
transforms.RandomVerticalFlip(),
transforms.RandomApply([transforms.ColorJitter(brightness=0.15, contrast=0.15)], p=0.5),
transforms.RandomApply([transforms.RandomRotation(degrees=15)], p=0.5)
])
device = 'cpu'
class Dataset(Dataset):
def __init__(self, input, labels):
self.frames = input
self.labels = labels
def __len__(self):
return self.frames.shape[0]
def __getitem__(self, idx):
frame_output = transform(
torch.tensor(np.transpose(self.frames[idx, :, :, :], (2, 0, 1)), dtype=torch.float32).to(device))
label_output = torch.tensor(self.labels[idx], dtype=torch.long).to(device)
return label_output, frame_output
class ValidDataset(Dataset):
def __init__(self, input, labels):
self.frames = input
self.labels = labels
def __len__(self):
return self.frames.shape[0]
def __getitem__(self, idx):
frame_output = torch.tensor(np.transpose(self.frames[idx, :, :, :], (2, 0, 1)), dtype=torch.float32).to(device)
label_output = torch.tensor(self.labels[idx], dtype=torch.long).to(device)
return label_output, frame_output
# dataset_train = Dataset(videos_with_rois_train, labels_train)
# dataloader_train = DataLoader(dataset_train, batch_size= 16, shuffle=True)
dataset_test = ValidDataset(videos_without_rois_test, labels_test)
dataloader_test = DataLoader(dataset_test, batch_size=16, shuffle=True)
model = nn.Sequential(
# Conv layer 1:
nn.Conv2d(3, 128, (3, 3)),
nn.ReLU(),
nn.BatchNorm2d(128),
nn.MaxPool2d((2, 2)),
# Conv layer 2:
nn.Conv2d(128, 64, (3, 3)),
nn.ReLU(),
nn.BatchNorm2d(64),
nn.MaxPool2d((2, 2)),
# Conv layer 3:
nn.Conv2d(64, 32, (3, 3)),
nn.ReLU(),
nn.BatchNorm2d(32),
nn.MaxPool2d((2, 2)),
nn.Conv2d(32, 16, (3, 3)),
nn.ReLU(),
nn.BatchNorm2d(16),
nn.MaxPool2d((2, 2)),
nn.Flatten(),
# fully connected layers:
nn.Linear(5888, 128), # The dimensions here depend on the input size and the previous layers
nn.ReLU(),
nn.Linear(128, 9),
)
model.to(device)
optimizer = optim.Adam(params=model.parameters(), lr=0.0001, weight_decay=5e-4)
loss_fn = nn.CrossEntropyLoss()
checkpoint = torch.load('bestmodelcheckpoint.pth')
model.load_state_dict(checkpoint['model_state_dict'])
model.eval()
model.to(device)
count = 0
with torch.no_grad():
for t in range(0, videos_without_rois_test.shape[0]):
frame = videos_without_rois_test[t, :, :, :]
input = torch.tensor(np.transpose(videos_without_rois_test[t, :, :, :], (2, 0, 1)), dtype=torch.float).unsqueeze(
0).to(device)
output = model(input)
count = count + torch.argmax(torch.nn.functional.softmax(output, dim=1).cpu().detach()).item()
# im_with_keypoints[posy, :, 0] = 255
plt.subplot(1, 1, 1)
plt.title(f'Computer Count: {count}, Real Count: {np.sum(labels_test[0:t], dtype=np.int32)}')
plt.imshow(frame)
plt.tight_layout()
plt.draw()
plt.pause(0.1)
plt.clf()