here is the code of model:- class tinyvgg12(nn.Module):
def init(self,input,hidden,output):
super().init()
self.Conv_block1 = nn.Sequential(
nn.Conv2d(
in_channels = input,
out_channels = hidden,
kernel_size=3,
stride=1,
padding=1
),
nn.ReLU(),
nn.Conv2d(
in_channels = hidden,
out_channels = hidden,
kernel_size=3,
stride=1,
padding=1
),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2,stride=2)
)
self.Conv_block2 = nn.Sequential(
nn.Conv2d(
in_channels =hidden,
out_channels = hidden,
kernel_size=3,
stride=1,
padding=1
),
nn.ReLU(),
nn.Conv2d(
in_channels=hidden,
out_channels=hidden,
kernel_size=3,
stride=1,
padding=1
),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2,stride=2)
)
self.classifier = nn.Sequential(
nn.Flatten(),
nn.Linear(
in_features=hidden*16*16,
out_features=output
)
)
def forward(self,x):
x = self.Conv_block1(x)
#print(x.shape)
x = self.Conv_block2(x)
#print(x.shape)
x = self.classifier(x)
#print(x.shape)
return x
return self.Conv_block2(self.Conv_block1(x))
here is the code for detecting objects:-
import cv2
import torch
import torchvision.transforms as transforms
from torchvision.models import resnet50
model = model_x_5
device = “cuda” if torch.cuda.is_available() else “cpu”
model.eval()
transform = transforms.Compose([
transforms.ToPILImage(),
transforms.Resize(size=(224,224)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485,0.456,0.406], std=[0.229,0.224,0.225])
])
video_path = “/content/data/Baby brushes his teeth.mp4”
cap = cv2.VideoCapture(video_path)
while cap.isOpened():
ret,frame = cap.read()
if not ret:
break
frame_rgb = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
input_tensor = transform(frame_rgb)
input_tensor = input_tensor.unsqueeze(0)
with torch.no_grad():
output = model(input_tensor.to(device))
cv2.imshow("object detection",frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
error:-
RuntimeError Traceback (most recent call last)
in <cell line: 19>()
25 input_tensor = input_tensor.unsqueeze(0)
26 with torch.no_grad():
—> 27 output = model(input_tensor.to(device))
28
29 cv2.imshow(“object detection”,frame)
5 frames
/usr/local/lib/python3.10/dist-packages/torch/nn/modules/linear.py in forward(self, input)
112
113 def forward(self, input: Tensor) → Tensor:
→ 114 return F.linear(input, self.weight, self.bias)
115
116 def extra_repr(self) → str:
RuntimeError: mat1 and mat2 shapes cannot be multiplied (1x200704 and 16384x1)