import torch
import torch.nn as nn
import torch.autograd.profiler as profiler
class VGG11(nn.Module):
def __init__(self, in_channels=1, num_classes=1000):
super(VGG11, self).__init__()
self.in_channels = in_channels
self.num_classes = num_classes
# convolutional layers
self.conv_layers = nn.Sequential(
nn.Conv2d(self.in_channels, 64, kernel_size=3, padding=1),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Conv2d(64, 128, kernel_size=3, padding=1),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Conv2d(128, 256, kernel_size=3, padding=1),
nn.ReLU(),
nn.Conv2d(256, 256, kernel_size=3, padding=1),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Conv2d(256, 512, kernel_size=3, padding=1),
nn.ReLU(),
nn.Conv2d(512, 512, kernel_size=3, padding=1),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Conv2d(512, 512, kernel_size=3, padding=1),
nn.ReLU(),
nn.Conv2d(512, 512, kernel_size=3, padding=1),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2)
)
# fully connected linear layers
self.linear_layers = nn.Sequential(
nn.Linear(in_features=512*7*7, out_features=4096),
nn.ReLU(),
nn.Dropout2d(0.5),
nn.Linear(in_features=4096, out_features=4096),
nn.ReLU(),
nn.Dropout2d(0.5),
nn.Linear(in_features=4096, out_features=self.num_classes)
)
def forward(self, x):
x = self.conv_layers(x)
# flatten to prepare for the fully connected layers
x = x.view(x.size(0), -1)
x = self.linear_layers(x)
print("vgg11_output_x:",x)
return x
model = VGG11()
inputs = torch.rand(1,1,224,224)
with profiler.profile(record_shapes=True) as prof:
with profiler.record_function("model_inference"):
model(inputs)
print(prof.key_averages().table(sort_by="cpu_time_total", row_limit=10))
print("detail:",prof.key_averages(group_by_input_shape=True).table(sort_by="cpu_time_total", row_limit=10))
with profiler.profile(profile_memory=True, record_shapes=True) as prof:
model(inputs)
print("memory1:",prof.key_averages().table(sort_by="self_cpu_memory_usage", row_limit=10))
print("memory2:",prof.key_averages().table(sort_by="cpu_memory_usage", row_limit=10))
참조링크:https://pytorch.org/tutorials/recipes/recipes/profiler_recipe.html
'Development' 카테고리의 다른 글
딥러닝 시각화 도구 visdom 에러 대처법 (0) | 2021.11.04 |
---|---|
Pyinstaller 통합라이브러리 만들시 팁! (0) | 2021.09.17 |
Pyinstaller 오류 대응 1. (0) | 2021.09.09 |
딥러닝 학습 시 랜덤시드 고정하는 방법.(torch 1.6.0 버전) (0) | 2021.06.04 |
CNN_신경망 체크 ( VGG11 망을 활용한 regression시도 중 시행착오 기록) (0) | 2021.06.04 |