def __init__(self, backbone, head='mlp', features_dim=128, L2Norm=True): super(HypContrastiveModel, self).__init__() self.backbone = backbone['backbone'] self.backbone_dim = backbone['dim'] self.head = head self.L2Norm = L2Norm if head == 'linear': self.contrastive_head = nn.Linear(self.backbone_dim, features_dim) elif head == 'mlp': self.contrastive_head = nn.Sequential( nn.Linear(self.backbone_dim, self.backbone_dim), nn.ReLU(), nn.Linear(self.backbone_dim, features_dim)) else: raise ValueError('Invalid head {}'.format(head)) import hyptorch.nn as hypnn class atdict(dict): __getattr__ = dict.__getitem__ __setattr__ = dict.__setitem__ __delattr__ = dict.__delitem__ hypargs = atdict() hypargs.c = 1.0 hypargs.train_x = False hypargs.train_c = False self.tp = hypnn.ToPoincare(c=hypargs.c, train_x=hypargs.train_x, train_c=hypargs.train_c)
def __init__(self, embed_len, inst_num, c=0.05): # for downloading pretrained model super(MINet, self).__init__() self.inst_num = inst_num resnet18_pretn = models.resnet18(pretrained=True) pretn_state_dict = resnet18_pretn.state_dict() self.resnet18 = models.resnet18(num_classes=embed_len) model_state_dict = self.resnet18.state_dict() update_state = { k: v for k, v in pretn_state_dict.items() if k not in ["fc.weight", "fc.bias"] and k in model_state_dict } model_state_dict.update(update_state) self.resnet18.load_state_dict(model_state_dict) self.c = c self.tp = hypnn.ToPoincare(c=self.c, train_x=True, train_c=True, ball_dim=embed_len) self.att_weight = hypnn.HypLinear(embed_len, 1, c=self.c) self.label_pred = hypnn.HyperbolicMLR(ball_dim=embed_len, n_classes=2, c=self.c)
def __init__(self, block, num_blocks, in_channel=3, zero_init_residual=False): super(HypResNet, self).__init__() self.in_planes = 64 self.conv1 = nn.Conv2d(in_channel, 64, kernel_size=3, stride=1, padding=1, bias=False) self.bn1 = nn.BatchNorm2d(64) self.maxpool = nn.MaxPool2d(kernel_size=2, stride=2, padding=1) self.layer1 = self._make_layer(block, 64, num_blocks[0], stride=1) self.layer2 = self._make_layer(block, 128, num_blocks[1], stride=2) self.layer3 = self._make_layer(block, 256, num_blocks[2], stride=2) self.layer4 = self._make_layer(block, 512, num_blocks[3], stride=2) self.avgpool = nn.AvgPool2d(7, stride=1) ''' HypTorch ''' import hyptorch.nn as hypnn class atdict(dict): __getattr__ = dict.__getitem__ __setattr__ = dict.__setitem__ __delattr__ = dict.__delitem__ hypargs = atdict() hypargs.c = 1.0 hypargs.train_x = False hypargs.train_c = False self.tp = hypnn.ToPoincare(c=hypargs.c, train_x=hypargs.train_x, train_c=hypargs.train_c) for m in self.modules(): if isinstance(m, nn.Conv2d): nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu') elif isinstance(m, (nn.BatchNorm2d, nn.GroupNorm)): nn.init.constant_(m.weight, 1) nn.init.constant_(m.bias, 0) # Zero-initialize the last BN in each residual branch, # so that the residual branch starts with zeros, and each residual block behaves # like an identity. This improves the model by 0.2~0.3% according to: # https://arxiv.org/abs/1706.02677 if zero_init_residual: for m in self.modules(): if isinstance(m, Bottleneck): nn.init.constant_(m.bn3.weight, 0) elif isinstance(m, BasicBlock): nn.init.constant_(m.bn2.weight, 0)
def __init__(self, args): super(Netold, self).__init__() self.conv1 = nn.Conv2d(3, 20, 5, 1) self.conv2 = nn.Conv2d(20, 50, 5, 1) self.fc1 = nn.Linear(4 * 4 * 50, 500) self.fc2 = nn.Linear(500, args.dim) self.tp = hypnn.ToPoincare( c=args.c, train_x=args.train_x, train_c=args.train_c, ball_dim=args.dim ) self.mlr = hypnn.HyperbolicMLR(ball_dim=args.dim, n_classes=120, c=args.c)
def __init__(self, args): super(NetHypConv2, self).__init__() self.tp = hypnn.ToPoincare(c=args.c, train_x=args.train_x, train_c=args.train_c, ball_dim=args.dim) self.conv1 = hypnn.HypConv2(1, 20, 5, c=args.c) self.conv2 = hypnn.HypConv2(20, 50, 5, c=args.c) self.fc1 = hypnn.HypLinear2(4 * 4 * 50, 500, c=args.c) self.fc2 = hypnn.HypLinear2(500, args.dim, c=args.c) self.mlr = hypnn.HyperbolicMLR(ball_dim=args.dim, n_classes=10, c=args.c)
def __init__(self, embed_len, inst_num, c=0.001): # for downloading pretrained model super(MINet, self).__init__() self.inst_num = inst_num self.lenet = LeNet(embed_len) self.c = c self.tp = hypnn.ToPoincare(c=self.c, train_x=True, train_c=False, ball_dim=embed_len) self.att_weight = hypnn.HypLinear(embed_len, 1, c=self.c) self.msi_pred = hypnn.HyperbolicMLR(ball_dim=embed_len, n_classes=2, c=self.c)
def __init__(self, args): super(NetHypConv, self).__init__() self.tp = hypnn.ToPoincare(c=args.c, train_x=args.train_x, train_c=args.train_c, ball_dim=args.dim) self.conv1 = hypnn.HypConv( 1, 20, 5, c=args.c) # input: 28 x 28, output: 20 x 24 x 24 (pool 2x2 after) self.conv2 = hypnn.HypConv( 20, 50, 5, c=args.c ) # input: 20 x 12 x 12, output: 50 x 8 x 8 (pool 2x2 after) self.fc1 = hypnn.HypLinear(4 * 4 * 50, 500, c=args.c) # input: 50 x 4 x 4, output: 500 self.fc2 = hypnn.HypLinear(500, args.dim, c=args.c) self.mlr = hypnn.HyperbolicMLR(ball_dim=args.dim, n_classes=10, c=args.c)
def __init__(self, c, x_dim=3, hid_dim=64, z_dim=64, args=None): super().__init__() self.c = c print(f'encoder with some hyperbolic') self.c1 = nn.Conv2d(x_dim, hid_dim, 3, padding=1) # self.b1 = nn.BatchNorm2d(hid_dim) # relu and maxpool(2) on forward pass self.c2 = nn.Conv2d(hid_dim, hid_dim, 3, padding=1) # self.b2 = nn.BatchNorm2d(hid_dim) # relu and maxpool(2) on forward pass self.c3 = nn.Conv2d(hid_dim, 10, 3, padding=1) # hid_dim # self.b3 = nn.BatchNorm2d(hid_dim) # relu and maxpool(2) on forward pass self.c4 = hypnn.HypConv3(10, 10, 3, c, padding=1) # hid_dim, z_dim # self.b4 = nn.BatchNorm2d(z_dim) self.e2p = hypnn.ToPoincare(c=args.c, train_c=args.train_c, train_x=args.train_x)
def __init__(self, sample_size, num_seq=8, seq_len=5, pred_step=3, network='resnet50', distance='dot', poincare_c=1.0, poincare_ball_dim=256): super(DPC_RNN, self).__init__() # to reproduce the experiments torch.cuda.manual_seed(233) print('Using DPC-RNN model') # number of dimensions in the image self.sample_size = sample_size self.num_seq = num_seq self.seq_len = seq_len self.distance = distance # how many futures to predict self.pred_step = pred_step # 2 if seq_len is 5 if network == 'resnet8' or network == 'resnet10': self.last_duration = int(math.ceil(seq_len / 2)) else: self.last_duration = int(math.ceil(seq_len / 4)) # 4 if size of the image is 128 # change for toy experiment #self.last_size = 1 self.last_size = int(math.ceil(sample_size / 32)) print('final feature map has size %dx%d' % (self.last_size, self.last_size)) # f - choose an appropriate feature extractor. In this case, a resent self.backbone, self.param = select_resnet( network, track_running_stats=False, distance=self.distance) #print (self.param) self.param['num_layers'] = 1 # param for GRU self.param['hidden_size'] = self.param['feature_size'] # param for GRU self.agg = ConvGRU(input_size=self.param['feature_size'], hidden_size=self.param['hidden_size'], kernel_size=1, num_layers=self.param['num_layers']) # two layered network \phi self.network_pred = nn.Sequential( nn.Conv2d(self.param['feature_size'], self.param['feature_size'], kernel_size=1, padding=0), nn.ReLU(inplace=True), nn.Conv2d(self.param['feature_size'], self.param['feature_size'], kernel_size=1, padding=0) ) # what does mask do ? self.mask = None self.relu = nn.ReLU(inplace=False) self._initialize_weights(self.agg) self._initialize_weights(self.network_pred) # exponential map self.tp = hypnn.ToPoincare(c=1.0, train_x=True, train_c=True, ball_dim=self.param['feature_size'])