def __init__( self, in_channels, out_channels, max_input_h, max_input_w, intermediate_channels_lambda=None, reparam_factor=1.5625, ): super().__init__() if intermediate_channels_lambda is None: intermediate_channels = max(in_channels // 4, 1) else: intermediate_channels = intermediate_channels_lambda(in_channels) num_box_filters = out_channels // intermediate_channels self.sample_features = nn.Sequential( nn.Conv2d(in_channels, intermediate_channels, kernel_size=3, padding=1), nn.BatchNorm2d(intermediate_channels), nn.ReLU(inplace=True), BoxConv2d( intermediate_channels, num_box_filters, max_input_h, max_input_w, reparametrization_factor=reparam_factor, ), nn.BatchNorm2d(out_channels), nn.ReLU(inplace=True), )
def __init__( self, in_channels, out_channels, max_input_h, max_input_w, n_boxes=4, reparam_factor=1.5625, ): super().__init__() bt_channels = in_channels // n_boxes self.conv = nn.Sequential( nn.Conv2d(in_channels, bt_channels, (1, 1), bias=False), nn.BatchNorm2d(bt_channels), nn.ReLU(True), BoxConv2d( bt_channels, n_boxes, max_input_h, max_input_w, reparametrization_factor=reparam_factor, ), nn.BatchNorm2d(in_channels), nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1), )
def add_extras(cfg, i, batch_norm=False): # Extra layers added to VGG for feature scaling layers = [] in_channels = i flag = False for k, v in enumerate(cfg): if in_channels != 'S': if v == 'S': layers += [ nn.Conv2d(in_channels, in_channels, kernel_size=(1, 3)[flag], stride=2, padding=1) ] if flag: assert cfg[k + 1] % in_channels == 0 layers += [ BoxConv2d(in_channels, cfg[k + 1] // in_channels, max_input_h=10, max_input_w=10) ] else: layers += [nn.Conv2d(in_channels, v, kernel_size=(1, 3)[flag])] flag = not flag in_channels = v return layers
def __init__(self, inplanes, planes, num_boxes, max_input_h, max_input_w, downsample=None, groups=1, base_width=64, dilation=1, norm_layer=None): super(BoxBottleneck, self).__init__() if norm_layer is None: norm_layer = nn.BatchNorm2d width = int(planes * (base_width / 64.)) * groups self.conv1 = conv1x1(inplanes, width // 4) self.bn1 = norm_layer(width // 4) self.conv2 = BoxConv2d(width // 4, num_boxes, max_input_h, max_input_w, reparametrization_factor=1.5625) self.bn2 = nn.BatchNorm2d(width // 4 * num_boxes) self.conv3 = conv1x1(width // 4 * num_boxes, planes * self.expansion) self.bn3 = nn.BatchNorm2d(planes * self.expansion) self.relu = nn.ReLU(inplace=True) self.downsample = downsample
def __init__(self, in_channels, num_boxes, max_input_h, max_input_w, dropout_prob=0.0, reparam_factor=1.5625): super().__init__() assert in_channels % num_boxes == 0 bt_channels = in_channels // num_boxes # bottleneck channels self.main_branch = nn.Sequential( nn.Conv2d(in_channels, bt_channels, (1, 1), bias=False), nn.BatchNorm2d(bt_channels), nn.ReLU(True), # BEHOLD: BoxConv2d(bt_channels, num_boxes, max_input_h, max_input_w, reparametrization_factor=reparam_factor), nn.BatchNorm2d(in_channels), nn.Dropout2d(dropout_prob))
def main(): train_set = torchvision.datasets.ImageFolder( root = './data/ImageNet/tiny-imagenet-200/train', transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize(mean =[.4802486, .44807222, .39754647], std = [.2769859, .26906505, .2820814])]) ) val_set = torchvision.datasets.ImageFolder( root = './data/ImageNet/tiny-imagenet-200/val', transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize(mean =[.4802486, .44807222, .39754647], std = [.2769859, .26906505, .2820814])]) ) test_set = torchvision.datasets.ImageFolder( root = './data/ImageNet/tiny-imagenet-200/test', transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize(mean =[.4802486, .44807222, .39754647], std = [.2769859, .26906505, .2820814])]) ) train_loader = torch.utils.data.DataLoader(train_set, batch_size = 256) val_loader = torch.utils.data.DataLoader(val_set, batch_size = 256) test_loader = torch.utils.data.DataLoader(test_set, batch_size = 256) device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') res_net = nn.Sequential( BoxConv2d(3, 32, 64, 64) ) sample_x, sample_y = next(iter(train_loader)) ex = res_net(sample_x)
def __init__(self, input_dim, conv1_numf, conv2_numb, conv3_numf, conv2_stride, max_w, max_h, downsample=None): super(BoxBottleneck2, self).__init__() self.conv1 = nn.Conv2d(input_dim, conv1_numf, kernel_size=1, stride=1, padding=0, bias=False) self.bn1 = nn.BatchNorm2d(conv1_numf) self.conv2 = BoxConv2d(conv1_numf, conv2_numb, max_w, max_h, 1.5625) self.bn2 = nn.BatchNorm2d(conv2_numb * conv1_numf) self.conv3 = nn.Conv2d(conv2_numb * conv1_numf, conv3_numf, kernel_size=1, stride=1, padding=0, bias=False) self.bn3 = nn.BatchNorm2d(conv3_numf) self.relu = nn.ReLU(inplace=True) self.downsample = downsample
def __init__( self, in_channels, num_boxes, max_input_h, max_input_w, dropout_prob=0.0, reparam_factor=1.5625, ): super().__init__() assert ( in_channels % num_boxes == 0 ), "Input channels must must be divisible by the number of boxes" bt_channels = in_channels // num_boxes # bottleneck channels self.main_branch = nn.Sequential( nn.Conv2d(in_channels, bt_channels, (1, 1), bias=False), nn.BatchNorm2d(bt_channels), nn.ReLU(True), BoxConv2d( bt_channels, num_boxes, max_input_h, max_input_w, reparametrization_factor=reparam_factor, ), nn.BatchNorm2d(in_channels), nn.Dropout2d(dropout_prob), )
def vgg(cfg, i, batch_norm=False): layers = [] in_channels = i for t, v in enumerate(cfg): box_max = 300 if v == 'M': layers += [nn.MaxPool2d(kernel_size=2, stride=2)] box_max //= 2 elif v == 'C': layers += [nn.MaxPool2d(kernel_size=2, stride=2, ceil_mode=True)] box_max //= 2 else: if t != 0: assert v%in_channels == 0 boxConv2d = BoxConv2d(in_channels, v//in_channels, \ max_input_h=box_max, max_input_w=box_max, reparametrization_factor=1.5625) if batch_norm: layers += [boxConv2d, nn.BatchNorm2d(v), nn.ReLU(inplace=True)] else: layers += [boxConv2d, nn.ReLU(inplace=True)] 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 pool5 = nn.MaxPool2d(kernel_size=3, stride=1, padding=1) conv6 = nn.Conv2d(512, 1024, kernel_size=3, padding=6, dilation=6) conv7 = nn.Conv2d(1024, 1024, kernel_size=1) layers += [pool5, conv6, nn.ReLU(inplace=True), conv7, nn.ReLU(inplace=True)] return layers
def __init__(self, in_channels, num_boxes, max_input_h, max_input_w, dropout_prob=0.0): super().__init__() assert in_channels % num_boxes == 0 bt_channels = in_channels // num_boxes # bottleneck channels self.main_branch = nn.Sequential( nn.Conv2d(in_channels, bt_channels, (1,1), bias=False), nn.BatchNorm2d(bt_channels), # BEHOLD: BoxConv2d(bt_channels, num_boxes, max_input_h, max_input_w), nn.BatchNorm2d(in_channels), nn.Dropout2d(dropout_prob))
def __init__(self): super(Net, self).__init__() self.conv1 = BoxConv2d(1, 40, 28, 28) self.conv1_1x1 = nn.Conv2d(40, 40, 1, 1) self.fc1 = nn.Linear(7*7*40, 10)