def __init__(self, backbone, hyperpixel_ids, benchmark, device): r"""Constructor for Hyperpixel Flow framework""" # Feature extraction network initialization. if backbone == 'resnet50': self.backbone = resnet.resnet50(pretrained=True).to(device) nbottlenecks = [3, 4, 6, 3] elif backbone == 'resnet101': self.backbone = resnet.resnet101(pretrained=True).to(device) nbottlenecks = [3, 4, 23, 3] elif backbone == 'fcn101': self.backbone = gcv.models.get_fcn_resnet101_voc( pretrained=True).to(device).pretrained nbottlenecks = [3, 4, 23, 3] else: raise Exception('Unavailable backbone: %s' % backbone) self.bottleneck_ids = reduce( add, list(map(lambda x: list(range(x)), nbottlenecks))) self.layer_ids = reduce(add, [[i + 1] * x for i, x in enumerate(nbottlenecks)]) self.backbone.eval() # Hyperpixel id and pre-computed jump and receptive field size initialization # Reference: https://fomoro.com/research/article/receptive-field-calculator # (the jump and receptive field sizes for 'fcn101' are heuristic values) self.hyperpixel_ids = util.parse_hyperpixel(hyperpixel_ids) self.jsz = torch.tensor([4, 4, 4, 4, 8, 8, 8, 8, 16, 16]).to(device) self.rfsz = torch.tensor([11, 19, 27, 35, 43, 59, 75, 91, 107, 139]).to(device) # Miscellaneous self.hsfilter = geometry.gaussian2d(7).to(device) self.device = device self.benchmark = benchmark
def __init__(self, backbone='resnet50'): super().__init__() if backbone == 'resnet18': backbone = resnet18(pretrained=True) self.out_channels = [256, 512, 512, 256, 256, 128] elif backbone == 'resnet34': backbone = resnet34(pretrained=True) self.out_channels = [256, 512, 512, 256, 256, 256] elif backbone == 'resnet50': backbone = resnet50(pretrained=True) self.out_channels = [1024, 512, 512, 256, 256, 256] elif backbone == 'resnet101': backbone = resnet101(pretrained=True) self.out_channels = [1024, 512, 512, 256, 256, 256] else: # backbone == 'resnet152': backbone = resnet152(pretrained=True) self.out_channels = [1024, 512, 512, 256, 256, 256] self.feature_extractor = nn.Sequential(*list(backbone.children())[:7]) conv4_block1 = self.feature_extractor[-1][0] conv4_block1.conv1.stride = (1, 1) conv4_block1.conv2.stride = (1, 1) conv4_block1.downsample[0].stride = (1, 1)
def __init__(self, pretrained=True, output_channel=1, rate=1, fixed_feature_weights=False): super(SANet, self).__init__() self.output_channel = output_channel resnet = resnet101(pretrained=pretrained) # Freeze resnet weights if fixed_feature_weights: for p in resnet.parameters(): p.requires_grad = False c = [i // rate for i in [64, 256, 512, 1024, 2048]] self.layer0 = nn.Sequential(resnet.conv1, resnet.bn1, resnet.relu) self.layer1 = nn.Sequential(resnet.maxpool, resnet.layer1) self.layer2 = nn.Sequential(resnet.layer2) self.layer3 = nn.Sequential(resnet.layer3) self.layer4 = nn.Sequential(resnet.layer4) # previous layers self.mid1 = conv_bn(c[0], c[0], 1) self.mid2 = conv_bn(c[1], c[1], 1) self.mid3 = conv_bn(c[2], c[2], 1) self.mid4 = conv_bn(c[3], c[3], 1) # Smooth layers self.smooth1 = conv_bn(c[4], c[3], 1) self.smooth2 = conv_bn(c[3], c[2], 1) self.smooth3 = conv_bn(c[2], c[1], 1) self.smooth4 = conv_bn(c[0] * 2, c[0], 1) self.smooth5 = conv(c[0] * 4, c[0] * 2, 1) self.upconv1 = conv_bn(c[4], c[3], 1) self.upconv2 = conv_bn(c[3], c[2], 1) self.upconv3 = conv_bn(c[2], c[1], 1) self.upconv4 = conv_bn(c[1], c[0], 1) self.upconv5 = conv(c[0] * 2, c[0], 1) self.att1 = SAB(c[3], c[3]) self.att2 = SAB(c[2], c[2]) self.att3 = SAB(c[1], c[1]) self.att4 = SAB(c[0], c[0]) # upshuffle layers self.up1 = upshuffle(c[3], c[0], 8) self.up2 = upshuffle(c[2], c[0], 4) self.up3 = upshuffle(c[1], c[0], 2) # Context block self.gc2 = ContextBlock(c[1], 1 / 4) self.gc3 = ContextBlock(c[2], 1 / 4) self.gc4 = ContextBlock(c[3], 1 / 4) self.gc5 = ContextBlock(c[4], 1 / 4) # Depth prediction self.predict = predict(c[0], self.output_channel)
def __init__(self, backbone, use_original_imgsize): super(HypercorrSqueezeNetwork, self).__init__() # 1. Backbone network initialization self.backbone_type = backbone self.use_original_imgsize = use_original_imgsize if backbone == 'vgg16': self.backbone = vgg.vgg16(pretrained=True) self.feat_ids = [17, 19, 21, 24, 26, 28, 30] self.extract_feats = extract_feat_vgg nbottlenecks = [2, 2, 3, 3, 3, 1] elif backbone == 'resnet50': self.backbone = resnet.resnet50(pretrained=True) self.feat_ids = list(range(4, 17)) self.extract_feats = extract_feat_res nbottlenecks = [3, 4, 6, 3] elif backbone == 'resnet101': self.backbone = resnet.resnet101(pretrained=True) self.feat_ids = list(range(4, 34)) self.extract_feats = extract_feat_res nbottlenecks = [3, 4, 23, 3] else: raise Exception('Unavailable backbone: %s' % backbone) self.bottleneck_ids = reduce( add, list(map(lambda x: list(range(x)), nbottlenecks))) self.lids = reduce(add, [[i + 1] * x for i, x in enumerate(nbottlenecks)]) self.stack_ids = torch.tensor( self.lids).bincount().__reversed__().cumsum(dim=0)[:3] self.backbone.eval() self.hpn_learner = HPNLearner(list(reversed(nbottlenecks[-3:]))) self.cross_entropy_loss = nn.CrossEntropyLoss()
def create_dataset(split): batch_size = 50 resnet = resnet101(True).cuda() resnet.eval() resnet.forward = forward.__get__(resnet, ResNet) dataloader = DataLoader(CLEVR(sys.argv[1], split), batch_size=batch_size, num_workers=4) size = len(dataloader) print(split, 'total', size * batch_size) f = h5py.File('data/{}_features.hdf5'.format(split), 'w', libver='latest') dset = f.create_dataset('data', (size * batch_size, 1024, 14, 14), dtype='f4') with torch.no_grad(): for i, image in tqdm(enumerate(dataloader)): image = image.to(device) features = resnet(image).detach().cpu().numpy() dset[i * batch_size:(i + 1) * batch_size] = features f.close()
def __init__(self, mode_name='wide_resnet50_2'): super(Backbone, self).__init__() assert mode_name in ('resnet18', 'resnet34', 'resnet50', 'resnet101', 'resnet152', 'resnext50_32x4d', 'resnext101_32x8d', 'wide_resnet50_2', 'wide_resnet101_2') if mode_name == 'resnet18': self.res_back_bone = resnet.resnet18() elif mode_name == 'resnet34': self.res_back_bone = resnet.resnet34() elif mode_name == 'resnet50': self.res_back_bone = resnet.resnet50() elif mode_name == 'resnet101': self.res_back_bone = resnet.resnet101() elif mode_name == 'resnet152': self.res_back_bone = resnet.resnet152() elif mode_name == 'resnext50_32x4d': self.res_back_bone = resnet.resnext50_32x4d() elif mode_name == 'resnext101_32x8d': self.res_back_bone = resnet.resnext101_32x8d() elif mode_name == 'wide_resnet50_2': self.res_back_bone = resnet.wide_resnet50_2() else: self.res_back_bone = resnet.wide_resnet101_2() self.backbone = nn.Module() layer0 = nn.Sequential(*list(self.res_back_bone.children())[:4]) self.backbone.add_module('layer0', layer0) self.backbone.add_module('layer1', self.res_back_bone.layer1) self.backbone.add_module('layer2', self.res_back_bone.layer2) self.backbone.add_module('layer3', self.res_back_bone.layer3) self.backbone.add_module('layer4', self.res_back_bone.layer4)
def __init__(self, backbone="resnet50", backbone_path=None): super().__init__() if backbone == "resnet18": backbone = resnet18(pretrained=not backbone_path) self.out_channels = [256, 512, 512, 256, 256, 128] elif backbone == "resnet34": backbone = resnet34(pretrained=not backbone_path) self.out_channels = [256, 512, 512, 256, 256, 256] elif backbone == "resnet50": backbone = resnet50(pretrained=not backbone_path) self.out_channels = [1024, 512, 512, 256, 256, 256] elif backbone == "resnet101": backbone = resnet101(pretrained=not backbone_path) self.out_channels = [1024, 512, 512, 256, 256, 256] else: backbone = resnet152(pretrained=not backbone_path) self.out_channels = [1024, 512, 512, 256, 256, 256] if backbone_path: backbone.load_state_dict(torch.load(backbone_path)) self.feature_extractor = nn.Sequential(*list(backbone.children())[:7]) conv4_block1 = self.feature_extractor[-1][0] conv4_block1.conv1.stride = (1, 1) conv4_block1.conv2.stride = (1, 1) conv4_block1.downsample[0].stride = (1, 1)
def __init__(self, name="resnet50", pretrain=True): super().__init__() if name == "resnet50": base_net = resnet.resnet50(pretrained=False) elif name == "resnet101": base_net = resnet.resnet101(pretrained=False) else: print(" base model is not support !") if pretrain: print("load the {} weight from ./cache".format(name)) base_net.load_state_dict( model_zoo.load_url(model_urls["resnet50"], model_dir="../cache")) # print(base_net) self.stage1 = nn.Sequential(base_net.conv1, base_net.bn1, base_net.relu, base_net.maxpool) self.stage2 = base_net.layer1 self.stage3 = base_net.layer2 self.stage4 = base_net.layer3 self.stage5 = base_net.layer4 self.up2 = nn.ConvTranspose2d(64, 64, kernel_size=4, stride=2, padding=1)
def __init__(self, backbone, num_classes=21): super().__init__() if backbone == 'resnet-18': self.backbone = resnet18() self.in_channel = [512, 128, 64, 64] self.out_channel = [256, 64, 64, 64] elif backbone == 'resnet-34': self.backbone = resnet34() self.in_channel = [512, 128, 64, 64] self.out_channel = [256, 64, 64, 64] elif backbone == 'resnet-50': self.backbone = resnet50() self.in_channel = [2048, 512, 256, 64] self.out_channel = [1024, 256, 64, 64] elif backbone == 'resnet-101': self.backbone = resnet101() self.in_channel = [2048, 512, 256, 64] self.out_channel = [1024, 256, 64, 64] elif backbone == 'resnet-152': self.backbone = resnet152() self.in_channel = [2048, 512, 256, 64] self.out_channel = [1024, 256, 64, 64] else: raise NotImplementedError self.encoder = Encoder(self.backbone) self.decoder = Decoder(self.in_channel, self.out_channel) self.out = nn.Conv2d(64, num_classes, 1) self._init_weight()
def __init__(self, backbone='resnet50', pretrained_path=None): super().__init__() if backbone == 'resnet18': backbone = resnet18(pretrained=not pretrained_path) self.final_out_channels = 256 self.low_level_inplanes = 64 elif backbone == 'resnet34': backbone = resnet34(pretrained=not pretrained_path) self.final_out_channels = 256 self.low_level_inplanes = 64 elif backbone == 'resnet50': backbone = resnet50(pretrained=not pretrained_path) self.final_out_channels = 1024 self.low_level_inplanes = 256 elif backbone == 'resnet101': backbone = resnet101(pretrained=not pretrained_path) self.final_out_channels = 1024 self.low_level_inplanes = 256 else: # backbone == 'resnet152': backbone = resnet152(pretrained=not pretrained_path) self.final_out_channels = 1024 self.low_level_inplanes = 256 if pretrained_path: backbone.load_state_dict(torch.load(pretrained_path)) self.early_extractor = nn.Sequential(*list(backbone.children())[:5]) self.later_extractor = nn.Sequential(*list(backbone.children())[5:7]) conv4_block1 = self.later_extractor[-1][0] conv4_block1.conv1.stride = (1, 1) conv4_block1.conv2.stride = (1, 1) conv4_block1.downsample[0].stride = (1, 1)
def main(): device = torch.device( 'cuda:0' if args.gpu and torch.cuda.is_available() else 'cpu') dataset_dir = '../' + args.dataset_dir + '/Data/RGB/' dataloader, dataset_size = load_data(dataset_dir, args.batch_size, args.num_workers) model = resnet101(num_classes=51) model.to(device) model.load_state_dict(torch.load(args.model_path, map_location='cpu')) vis_results = visualize(model, dataloader, dataset_size, device, args.selected_layer) layer_output = vis_results[0][0] print(layer_output.shape) num_features = layer_output.shape[0] num_row = int(num_features**0.5) for i in range(num_features): feature = layer_output[i].data.numpy() feature = 1.0 / (1 + np.exp(-1 * feature)) feature = np.round(feature * 255) ax = plt.subplot(num_row, num_row, i + 1) ax.axis('off') plt.imshow(feature, cmap=plt.cm.gray) plt.subplots_adjust(wspace=0.2, hspace=0) plt.savefig('{0}.jpg'.format(args.selected_layer))
def __init__(self, pretrained=None): super(HeadCommon, self).__init__() self.config = ConfigParser() config_path = os.path.abspath( os.path.join(__file__, "../../", "config.ini")) assert os.path.exists(config_path), "config.ini not exists!" self.config.read(config_path) self.backbone_type = self.config['BACKBONE']['BACKBONE_TYPE'] _pretrained = True if pretrained is not None else False assert self.backbone_type in ['resnet', 'vgg16'] if self.backbone_type == 'resnet': resnet_layer = int(self.config['BACKBONE']['RESNET_LAYER']) assert resnet_layer in [18, 34, 50, 101, 152] if resnet_layer == 18: _resnet = resnet.resnet18(_pretrained) elif resnet_layer == 34: _resnet = resnet.resnet34(_pretrained) elif resnet_layer == 50: _resnet = resnet.resnet50(_pretrained) elif resnet_layer == 101: _resnet = resnet.resnet101(_pretrained) else: _resnet = resnet.resnet152(_pretrained) # using resnet_c5 the last bottle neck of resnet _resnet.layer4[0].conv2.stride = (1, 1) _resnet.layer4[0].downsample[0].stride = (1, 1) self.resnet_c5 = _resnet.layer4 self.resnet_c5_avg = _resnet.avgpool elif self.backbone_type == 'vgg16': assert not bool(int(self.config['HEAD']['MASK_HEAD_ON'])), ( "When mask head on, not support vgg16 backbone.") vgg = vgg16(pretrained=True) self.vgg_fc = nn.Sequential( *list(vgg.classifier._modules.values())[:-1])
def __init__(self, backbone='resnet50', backbone_path=None): super().__init__() if backbone == 'resnet18': backbone = resnet18(pretrained=not backbone_path) self.out_channels = [256, 512, 512, 256, 256, 128] elif backbone == 'resnet34': backbone = resnet34(pretrained=not backbone_path) self.out_channels = [256, 512, 512, 256, 256, 256] elif backbone == 'resnet50': backbone = resnet50(pretrained=not backbone_path) self.out_channels = [1024, 512, 512, 256, 256, 256] elif backbone == 'resnet101': backbone = resnet101(pretrained=not backbone_path) self.out_channels = [1024, 512, 512, 256, 256, 256] else: # backbone == 'resnet152': backbone = resnet152(pretrained=not backbone_path) self.out_channels = [1024, 512, 512, 256, 256, 256] if backbone_path: backbone.load_state_dict(torch.load(backbone_path)) for name, parameter in backbone.named_parameters(): if 'layer2' not in name and 'layer3' not in name and 'layer4' not in name: parameter.requires_grad_(False) self.feature_extractor = nn.Sequential(*list(backbone.children())[:7]) conv4_block1 = self.feature_extractor[-1][0] conv4_block1.conv1.stride = (1, 1) conv4_block1.conv2.stride = (1, 1) conv4_block1.downsample[0].stride = (1, 1)
def __init__(self, resnet_layer, pretrained=None): super(ResNet, self).__init__() assert resnet_layer in [18, 34, 50, 101, 152] pretrained = True if pretrained is not None else False if resnet_layer == 18: self.resnet = resnet.resnet18(pretrained) elif resnet_layer == 34: self.resnet = resnet.resnet34(pretrained) elif resnet_layer == 50: self.resnet = resnet.resnet50(pretrained) elif resnet_layer == 101: self.resnet = resnet.resnet101(pretrained) else: self.resnet = resnet.resnet152(pretrained) # fix layer grad for p in self.resnet.conv1.parameters(): p.requires_grad = False for p in self.resnet.layer1.parameters(): p.requires_grad = False for p in self.resnet.layer2.parameters(): p.requires_grad = True for p in self.resnet.layer3.parameters(): p.requires_grad = True # fix batch norm layer for m in self.resnet.modules(): if isinstance(m, nn.BatchNorm2d): for p in m.parameters(): p.requires_grad = False
def __init__(self, pretrained=True): super(I2D, self).__init__() resnet = resnet101(pretrained=pretrained) self.layer0 = nn.Sequential(resnet.conv1, resnet.bn1, resnet.relu, resnet.maxpool) self.layer1 = nn.Sequential(resnet.layer1) # 256 self.layer2 = nn.Sequential(resnet.layer2) # 512 self.layer3 = nn.Sequential(resnet.layer3) # 1024 self.layer4 = nn.Sequential(resnet.layer4) # 2048 # Top layer self.toplayer = nn.Conv2d(2048, 256, kernel_size=1, stride=1, padding=0) # Reduce channels # Lateral layers self.latlayer1 = nn.Conv2d(1024, 256, kernel_size=1, stride=1, padding=0) self.latlayer2 = nn.Conv2d( 512, 256, kernel_size=1, stride=1, padding=0) self.latlayer3 = nn.Conv2d( 256, 256, kernel_size=1, stride=1, padding=0) # Smooth layers self.smooth1 = nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1) self.smooth2 = nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1) self.smooth3 = nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1) # Depth prediction self.predict1 = smooth(256, 64) self.predict2 = predict(64, 1)
def __init__(self, pretrained=True, fixed_feature_weights=False): super(I2D, self).__init__() resnet = resnet101(pretrained=pretrained) # Freeze those weights if fixed_feature_weights: for p in resnet.parameters(): p.requires_grad = False self.layer0 = nn.Sequential(resnet.conv1, resnet.bn1, resnet.relu, resnet.maxpool) self.layer1 = nn.Sequential(resnet.layer1) self.layer2 = nn.Sequential(resnet.layer2) self.layer3 = nn.Sequential(resnet.layer3) self.layer4 = nn.Sequential(resnet.layer4) # Top layer self.toplayer = nn.Conv2d(2048, 256, kernel_size=1, stride=1, padding=0) # Reduce channels # Lateral layers self.latlayer1 = nn.Conv2d(1024, 256, kernel_size=1, stride=1, padding=0) self.latlayer2 = nn.Conv2d(512, 256, kernel_size=1, stride=1, padding=0) self.latlayer3 = nn.Conv2d(256, 256, kernel_size=1, stride=1, padding=0) # Smooth layers self.smooth1 = nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1) self.smooth2 = nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1) self.smooth3 = nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1) # Aggregate layers self.agg1 = agg_node(256, 128) self.agg2 = agg_node(256, 128) self.agg3 = agg_node(256, 128) self.agg4 = agg_node(256, 128) # Upshuffle layers self.up1 = upshuffle(128, 128, 8) self.up2 = upshuffle(128, 128, 4) self.up3 = upshuffle(128, 128, 2) # Depth prediction self.predict1 = smooth(512, 128) self.predict2 = predict(128, 1)
def get(cls, args): model = ResNet3D(Bottleneck3D, [3, 8, 36, 3]) # 101 if args.pretrained: from torchvision.models.resnet import resnet101 model2d = resnet101(pretrained=True) model.load_2d(model2d) return model
def get_classification_model(classifier_name='resnet18', num_classes=1000, pretrained=True): """ Get the detection model :param pretrained: :param classifier_name: :param num_classes: :return: """ if classifier_name == 'resnet18': model = resnet18(pretrained, num_classes=num_classes) elif classifier_name == 'resnet34': model = resnet34(pretrained, num_classes=num_classes) elif classifier_name == 'resnet50': model = resnet50(pretrained, num_classes=num_classes) elif classifier_name == 'resnet101': model = resnet101(pretrained, num_classes=num_classes) elif classifier_name == 'resnet152': model = resnet152(pretrained, num_classes=num_classes) else: raise ValueError('Unsupported resnet type.') return model
def __init__(self, resnet_layer, pretrained=None): super(ResNetFPN, self).__init__() self.lateral_conv1 = nn.Conv2d(2048, 256, kernel_size=1) self.lateral_conv2 = nn.Conv2d(1024, 256, kernel_size=1) self.lateral_conv3 = nn.Conv2d(512, 256, kernel_size=1) self.lateral_conv4 = nn.Conv2d(256, 256, kernel_size=1) self.anti_aliasing_conv1 = nn.Conv2d(256, 256, kernel_size=3, padding=1) self.anti_aliasing_conv2 = nn.Conv2d(256, 256, kernel_size=3, padding=1) self.anti_aliasing_conv3 = nn.Conv2d(256, 256, kernel_size=3, padding=1) self._init_parameters() assert resnet_layer in [18, 34, 50, 101, 152] pretrained = True if pretrained is not None else False if resnet_layer == 18: self.resnet = resnet.resnet18(pretrained) elif resnet_layer == 34: self.resnet = resnet.resnet34(pretrained) elif resnet_layer == 50: self.resnet = resnet.resnet50(pretrained) elif resnet_layer == 101: self.resnet = resnet.resnet101(pretrained) elif resnet_layer == 152: self.resnet = resnet.resnet152(pretrained)
def load_resnet(): model = resnet101(pretrained=True) del model.layer4 del model.avgpool del model.fc return model
def __init__(self): super(MGN_PTL, self).__init__() num_classes = opt.classn feats = 256 self.val = False self.ptl = PTL() self.ptl.init_param() if opt.backbone == 'resnet50': self.backbone = resnet50(pretrained=True) elif opt.backbone == 'resnet101': self.backbone = resnet101(pretrained=True) res_conv4 = nn.Sequential(*self.backbone.layer3[1:]) res_g_conv5 = self.backbone.layer4 res_p_conv5 = nn.Sequential( Bottleneck(1024, 512, downsample=nn.Sequential( nn.Conv2d(1024, 2048, 1, bias=False), nn.BatchNorm2d(2048))), Bottleneck(2048, 512), Bottleneck(2048, 512)) res_p_conv5.load_state_dict(self.backbone.layer4.state_dict()) self.p1 = nn.Sequential(copy.deepcopy(res_conv4), copy.deepcopy(res_g_conv5)) self.p2 = nn.Sequential(copy.deepcopy(res_conv4), copy.deepcopy(res_p_conv5)) self.p3 = nn.Sequential(copy.deepcopy(res_conv4), copy.deepcopy(res_p_conv5)) self.maxpool_zg_p1 = nn.MaxPool2d(kernel_size=(12, 4)) self.maxpool_zg_p2 = nn.MaxPool2d(kernel_size=(24, 8)) self.maxpool_zg_p3 = nn.MaxPool2d(kernel_size=(24, 8)) self.maxpool_zp2 = nn.MaxPool2d(kernel_size=(12, 8)) self.maxpool_zp3 = nn.MaxPool2d(kernel_size=(8, 8)) self.reduction = nn.Sequential( nn.Conv2d(2048 + 128, feats, 1, bias=True), nn.BatchNorm2d(feats), nn.ReLU()) self._init_reduction(self.reduction) self.fc_id_2048_0 = nn.Linear(feats, num_classes) self.fc_id_2048_1 = nn.Linear(feats, num_classes) self.fc_id_2048_2 = nn.Linear(feats, num_classes) self.fc_id_256_1_0 = nn.Linear(feats, num_classes) self.fc_id_256_1_1 = nn.Linear(feats, num_classes) self.fc_id_256_2_0 = nn.Linear(feats, num_classes) self.fc_id_256_2_1 = nn.Linear(feats, num_classes) self.fc_id_256_2_2 = nn.Linear(feats, num_classes) self._init_fc(self.fc_id_2048_0) self._init_fc(self.fc_id_2048_1) self._init_fc(self.fc_id_2048_2) self._init_fc(self.fc_id_256_1_0) self._init_fc(self.fc_id_256_1_1) self._init_fc(self.fc_id_256_2_0) self._init_fc(self.fc_id_256_2_1) self._init_fc(self.fc_id_256_2_2)
def __init__(self): super(ResNet, self).__init__() cnn = resnet101(pretrained=True) self.cnn = cnn self.features = nn.Sequential(cnn.conv1, cnn.bn1, cnn.relu, cnn.maxpool, cnn.layer1, cnn.layer2, cnn.layer3, cnn.layer4)
def resnet101(pretrained=False, **kwargs): """Constructs a ResNet-101 model. Args: pretrained (bool): If True, returns a model pre-trained on ImageNet """ model = ResNet_C5(Bottleneck, [3, 4, 23, 3], **kwargs) model_full = resnet.resnet101(pretrained=True) if pretrained: model.load_pretrained(model_full) return model
def _get_backbone(self, backbone): if backbone == 'resnet-101': resnet = resnet101(pretrained=True) extractor = nn.Sequential(resnet.conv1, resnet.bn1, resnet.relu, resnet.maxpool, resnet.layer1, resnet.layer2, resnet.layer3, resnet.layer4) return extractor else: raise NotImplementedError
def test_gflops(): from fblib.util.model_resources.flops import compute_gflops from torchvision.models import resnet x50 = resnext101_32x4d(pretrained=False) print('GFLOPS for ResNeXt: {}'.format(compute_gflops(x50))) res50 = resnet.resnet101(pretrained=False) print('GFLOPS for ResNet: {}'.format(compute_gflops(res50)))
def __init__(self, return_layers=None): model = resnet101(pretrained=True, replace_stride_with_dilation=[False, True, True]) return_layers = { 'layer1': 'layer1', 'layer2': 'layer2', 'layer3': 'layer3', 'layer4': 'layer4', } or return_layers super(ResNet101, self).__init__(model, return_layers)
def _resnet101(msda='fmix', pretrained=False, *args, **kwargs): from torchvision.models.resnet import resnet101 model = resnet101(*args, **kwargs) if pretrained: state = load_state_dict_from_url( f'http://marc.ecs.soton.ac.uk/pytorch-models/imagenet/resnet101/imagenet_resnet101_{msda}.pt', progress=True) model.load_state_dict(state) return model
def get_backbone(self, name, pretrained): if name == "resne34": return resnet34(pretrained=pretrained) elif name == "resnet50": return resnet50(pretrained=pretrained) elif name == "resnet101": return resnet101(pretrained=pretrained) elif name == "resnet152": return resnet152(pretrained=pretrained) else: return resnet18(pretrained=pretrained) #default
def get_base_model(name): if name == "resnet18": return resnet.resnet18() if name == "resnet34": return resnet.resnet34() if name == "resnet50": return resnet.resnet50() if name == "resnet101": return resnet.resnet101() if name == "resnet152": return resnet.resnet152()
def resnet101(pretrained=False, fixed_feature=True): """ "ResNet-101 model from torchvision's resnet model. :param pretrained: if true, return a model pretrained on ImageNet :param fixed_feature: if true and pretrained is true, model features are fixed while training. """ from torchvision.models.resnet import resnet101 model = resnet101(pretrained) ff = True if pretrained and fixed_feature else False return _ResNet(model, ff)
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 'extra' in kwargs: extra = kwargs.pop('extra') else: extra = True if model_name == 'dpn68': model = dpn68( num_classes=num_classes, pretrained=pretrained, test_time_pool=test_time_pool) elif model_name == 'dpn68b': model = dpn68b( num_classes=num_classes, pretrained=pretrained, test_time_pool=test_time_pool) elif model_name == 'dpn92': model = dpn92( num_classes=num_classes, pretrained=pretrained, test_time_pool=test_time_pool, extra=extra) elif model_name == 'dpn98': model = dpn98( num_classes=num_classes, pretrained=pretrained, test_time_pool=test_time_pool) elif model_name == 'dpn131': model = dpn131( num_classes=num_classes, pretrained=pretrained, test_time_pool=test_time_pool) elif model_name == 'dpn107': model = dpn107( num_classes=num_classes, pretrained=pretrained, test_time_pool=test_time_pool) elif model_name == 'resnet18': model = resnet18(num_classes=num_classes, pretrained=pretrained, **kwargs) elif model_name == 'resnet34': model = resnet34(num_classes=num_classes, pretrained=pretrained, **kwargs) elif model_name == 'resnet50': model = resnet50(num_classes=num_classes, pretrained=pretrained, **kwargs) elif model_name == 'resnet101': model = resnet101(num_classes=num_classes, pretrained=pretrained, **kwargs) elif model_name == 'resnet152': model = resnet152(num_classes=num_classes, pretrained=pretrained, **kwargs) elif model_name == 'densenet121': model = densenet121(num_classes=num_classes, pretrained=pretrained, **kwargs) elif model_name == 'densenet161': model = densenet161(num_classes=num_classes, pretrained=pretrained, **kwargs) elif model_name == 'densenet169': model = densenet169(num_classes=num_classes, pretrained=pretrained, **kwargs) elif model_name == 'densenet201': model = densenet201(num_classes=num_classes, pretrained=pretrained, **kwargs) elif model_name == 'inception_v3': model = inception_v3( num_classes=num_classes, pretrained=pretrained, transform_input=False, **kwargs) else: assert False, "Unknown model architecture (%s)" % model_name return model