本文整理汇总了Python中torchvision.models.resnet.resnet18方法的典型用法代码示例。如果您正苦于以下问题:Python resnet.resnet18方法的具体用法?Python resnet.resnet18怎么用?Python resnet.resnet18使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类torchvision.models.resnet
的用法示例。
在下文中一共展示了resnet.resnet18方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: forward_pytorch
# 需要导入模块: from torchvision.models import resnet [as 别名]
# 或者: from torchvision.models.resnet import resnet18 [as 别名]
def forward_pytorch(weightfile, image):
net=resnet.resnet18()
checkpoint = torch.load(weightfile)
net.load_state_dict(checkpoint)
if args.cuda:
net.cuda()
print(net)
net.eval()
image = torch.from_numpy(image)
if args.cuda:
image = Variable(image.cuda())
else:
image = Variable(image)
t0 = time.time()
blobs = net.forward(image)
#print(blobs.data.numpy().flatten())
t1 = time.time()
return t1-t0, blobs, net.parameters()
# Reference from:
示例2: __init__
# 需要导入模块: from torchvision.models import resnet [as 别名]
# 或者: from torchvision.models.resnet import resnet18 [as 别名]
def __init__(self, pretrained=True, input_channels=3):
model = resnet.resnet18(pretrained=pretrained)
super().__init__(
model=model,
input_channels=input_channels)
示例3: __init__
# 需要导入模块: from torchvision.models import resnet [as 别名]
# 或者: from torchvision.models.resnet import resnet18 [as 别名]
def __init__(self):
super().__init__()
rn18 = resnet18(pretrained=True)
# discard last Resnet block, avrpooling and classification FC
# layer1 = up to and including conv3 block
self.layer1 = nn.Sequential(*list(rn18.children())[:6])
# layer2 = conv4 block only
self.layer2 = nn.Sequential(*list(rn18.children())[6:7])
# modify conv4 if necessary
# Always deal with stride in first block
modulelist = list(self.layer2.children())
_ModifyBlock(modulelist[0], stride=(1,1))
示例4: test_speedup_integration
# 需要导入模块: from torchvision.models import resnet [as 别名]
# 或者: from torchvision.models.resnet import resnet18 [as 别名]
def test_speedup_integration(self):
for model_name in ['resnet18', 'squeezenet1_1', 'mobilenet_v2']:
Model = getattr(models, model_name)
net = Model(pretrained=True, progress=False).to(device)
net.eval() # this line is necessary
# random generate the prune config for the pruner
cfgs = generate_random_sparsity(net)
pruner = L1FilterPruner(net, cfgs)
pruner.compress()
pruner.export_model(MODEL_FILE, MASK_FILE)
pruner._unwrap_model()
speedup_model = Model().to(device)
speedup_model.eval()
state_dict = torch.load(MODEL_FILE)
speedup_model.load_state_dict(state_dict)
zero_bn_bias(net)
zero_bn_bias(speedup_model)
data = torch.ones(BATCH_SIZE, 3, 224, 224).to(device)
ms = ModelSpeedup(speedup_model, data, MASK_FILE)
ms.speedup_model()
ori_out = net(data)
speeded_out = speedup_model(data)
ori_sum = torch.sum(ori_out).item()
speeded_sum = torch.sum(speeded_out).item()
print('Sum of the output of %s (before speedup):'%model_name, ori_sum)
print('Sum of the output of %s (after speedup):'%model_name, speeded_sum)
assert (abs(ori_sum - speeded_sum) / abs(ori_sum) < RELATIVE_THRESHOLD) or \
(abs(ori_sum - speeded_sum) < ABSOLUTE_THRESHOLD)
示例5: __init__
# 需要导入模块: from torchvision.models import resnet [as 别名]
# 或者: from torchvision.models.resnet import resnet18 [as 别名]
def __init__(self, classes=19):
"""
Model initialization
:param x_n: number of input neurons
:type x_n: int
"""
super().__init__()
base = resnet.resnet18(pretrained=True)
self.in_block = nn.Sequential(
base.conv1,
base.bn1,
base.relu,
base.maxpool
)
self.encoder1 = base.layer1
self.encoder2 = base.layer2
self.encoder3 = base.layer3
self.encoder4 = base.layer4
self.decoder1 = Decoder(64, 64, 3, 1, 1, 0)
self.decoder2 = Decoder(128, 64, 3, 2, 1, 1)
self.decoder3 = Decoder(256, 128, 3, 2, 1, 1)
self.decoder4 = Decoder(512, 256, 3, 2, 1, 1)
# Classifier
self.tp_conv1 = nn.Sequential(nn.ConvTranspose2d(64, 32, 3, 2, 1, 1),
nn.BatchNorm2d(32),
nn.ReLU(inplace=True),)
self.conv2 = nn.Sequential(nn.Conv2d(32, 32, 3, 1, 1),
nn.BatchNorm2d(32),
nn.ReLU(inplace=True),)
self.tp_conv2 = nn.ConvTranspose2d(32, classes, 2, 2, 0)
示例6: get
# 需要导入模块: from torchvision.models import resnet [as 别名]
# 或者: from torchvision.models.resnet import resnet18 [as 别名]
def get(cls, args):
model = ResNet3DAutoencoder([2, 2, 2, 2])
if args.pretrained:
from torchvision.models.resnet import resnet18
model2d = resnet18(pretrained=True)
model.encoder.load_2d(model2d)
return model
示例7: __init__
# 需要导入模块: from torchvision.models import resnet [as 别名]
# 或者: from torchvision.models.resnet import resnet18 [as 别名]
def __init__(self, arch, scene_classes=1055):
super(RGBBranch, self).__init__()
# --------------------------------#
# Base Network #
# ------------------------------- #
if arch == 'ResNet-18':
# ResNet-18 Network
base = resnet.resnet18(pretrained=True)
# Size parameters for ResNet-18
size_fc_RGB = 512
elif arch == 'ResNet-50':
# ResNet-50 Network
base = resnet.resnet50(pretrained=True)
# Size parameters for ResNet-50
size_fc_RGB = 2048
# --------------------------------#
# RGB Branch #
# ------------------------------- #
# First initial block
self.in_block = nn.Sequential(
nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3, bias=False),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3, stride=2, padding=1, return_indices=True)
)
# Encoder
self.encoder1 = base.layer1
self.encoder2 = base.layer2
self.encoder3 = base.layer3
self.encoder4 = base.layer4
# -------------------------------------#
# RGB Classifier #
# ------------------------------------ #
self.dropout = nn.Dropout(0.3)
self.avgpool = nn.AvgPool2d(7, stride=1)
self.fc = nn.Linear(size_fc_RGB, scene_classes)
# Loss
self.criterion = nn.CrossEntropyLoss()
示例8: create_model
# 需要导入模块: from torchvision.models import resnet [as 别名]
# 或者: from torchvision.models.resnet import resnet18 [as 别名]
def create_model(model_name, num_classes=1000, pretrained=False, **kwargs):
if 'test_time_pool' in kwargs:
test_time_pool = kwargs.pop('test_time_pool')
else:
test_time_pool = True
if model_name == 'dpn68':
model = dpn68(
pretrained=pretrained, test_time_pool=test_time_pool, num_classes=num_classes)
elif model_name == 'dpn68b':
model = dpn68b(
pretrained=pretrained, test_time_pool=test_time_pool, num_classes=num_classes)
elif model_name == 'dpn92':
model = dpn92(
pretrained=pretrained, test_time_pool=test_time_pool, num_classes=num_classes)
elif model_name == 'dpn98':
model = dpn98(
pretrained=pretrained, test_time_pool=test_time_pool, num_classes=num_classes)
elif model_name == 'dpn131':
model = dpn131(
pretrained=pretrained, test_time_pool=test_time_pool, num_classes=num_classes)
elif model_name == 'dpn107':
model = dpn107(
pretrained=pretrained, test_time_pool=test_time_pool, num_classes=num_classes)
elif model_name == 'resnet18':
model = resnet18(pretrained=pretrained, num_classes=num_classes, **kwargs)
elif model_name == 'resnet34':
model = resnet34(pretrained=pretrained, num_classes=num_classes, **kwargs)
elif model_name == 'resnet50':
model = resnet50(pretrained=pretrained, num_classes=num_classes, **kwargs)
elif model_name == 'resnet101':
model = resnet101(pretrained=pretrained, num_classes=num_classes, **kwargs)
elif model_name == 'resnet152':
model = resnet152(pretrained=pretrained, num_classes=num_classes, **kwargs)
elif model_name == 'densenet121':
model = densenet121(pretrained=pretrained, num_classes=num_classes, **kwargs)
elif model_name == 'densenet161':
model = densenet161(pretrained=pretrained, num_classes=num_classes, **kwargs)
elif model_name == 'densenet169':
model = densenet169(pretrained=pretrained, num_classes=num_classes, **kwargs)
elif model_name == 'densenet201':
model = densenet201(pretrained=pretrained, num_classes=num_classes, **kwargs)
elif model_name == 'inception_v3':
model = inception_v3(
pretrained=pretrained, num_classes=num_classes, transform_input=False, **kwargs)
else:
assert False, "Unknown model architecture (%s)" % model_name
return model