import torch.nn as nn import torch.utils.model_zoo as model_zoo import torch import os from torch.nn import functional as F __all__ = ['vgg19'] model_urls = { 'vgg19': 'https://download.pytorch.org/models/vgg19-dcbb9e9d.pth', } class VGG(nn.Module): def __init__(self, features): super(VGG, self).__init__() self.features = features self.reg_layer = nn.Sequential( nn.Conv2d(512, 256, kernel_size=3, padding=1), nn.ReLU(inplace=True), nn.Conv2d(256, 128, kernel_size=3, padding=1), nn.ReLU(inplace=True), nn.Conv2d(128, 1, 1) ) # left_header # self.left_header = nn.Sequential( # nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, padding=1), # nn.MaxPool2d(kernel_size=2, stride=2), # nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, padding=1), # nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, padding=1), # nn.AvgPool2d(kernel_size=2, stride=2), # nn.Flatten(), # nn.Linear(18432, 1000), # nn.Linear(1000, 5) # ) # # right_header # self.right_header = nn.Sequential( # nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, padding=1), # nn.MaxPool2d(kernel_size=2, stride=2), # nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, padding=1), # nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, padding=1), # nn.AvgPool2d(kernel_size=2, stride=2), # nn.Flatten(), # nn.Linear(18432, 1000), #512 - 32768 # nn.Linear(1000, 13) # ) def forward(self, x): outputs = [] # VGG19 outputs 0 x = self.features(x) outputs.append(x) #with torch.no_grad(): # left_header 1 posture # x_left = self.left_header(x) # outputs.append(x_left) # # right_header 2 # x_right = self.right_header(x) # outputs.append(x_right) # density outputs 3 x = F.upsample_bilinear(x, scale_factor=2) x = self.reg_layer(x) outputs.append(torch.abs(x)) return torch.abs(x) def make_layers(cfg, batch_norm=False): layers = [] in_channels = 3 for v in cfg: if v == 'M': layers += [nn.MaxPool2d(kernel_size=2, stride=2)] else: conv2d = nn.Conv2d(in_channels, v, kernel_size=3, padding=1) if batch_norm: layers += [conv2d, nn.BatchNorm2d(v), nn.ReLU(inplace=True)] else: layers += [conv2d, nn.ReLU(inplace=True)] in_channels = v return nn.Sequential(*layers) cfg = { 'E': [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 256, 'M', 512, 512, 512, 512, 'M', 512, 512, 512, 512] } def vgg19(): """VGG 19-layer model (configuration "E") model pre-trained on ImageNet """ #make_layers(cfg['E']) model = VGG(make_layers(cfg['E'])) model.load_state_dict(model_zoo.load_url(model_urls['vgg19']), strict=False) # checkpoint = torch.load('./Net_model_bird/0121-110701/115_ckpt.tar') # model.load_state_dict(checkpoint['model_state_dict']) # model.load_state_dict(torch.load(os.path.join('./Net_model_bird', '84_best_model.pth'))) return model