def __init__(self): super(LTCNDepthModel, self).__init__() self.transform_input = True self.Conv2d_6a_3x3 = BatchNormConv2d(256, 100, kernel_size=2, stride=1) # self.Conv2d_6b_3x3 = BatchNormConv2d(100, 20, kernel_size=3, stride=1) self.SpatialSoftmax = nn.Softmax2d() self.FullyConnected5a = Dense(6 * 6 * 100, 1000) self.FullyConnected6a = Dense(1000, 500) self.FullyConnected7a = Dense(500, 32) # Depth layers self.Conv2d_depth_1a_3x3 = BatchNormConv2d(1, 64, kernel_size=3, stride=2) self.Conv2d_depth_1b_3x3 = BatchNormConv2d(64, 32, kernel_size=3, stride=1) self.Conv2d_depth_1c_3x3 = BatchNormConv2d(32, 10, kernel_size=3, stride=1) self.SpatialSoftmax_depth = nn.Softmax2d() self.FullyConnected2a_depth = Dense(72 * 72 * 10, 10) self.alpha = 10.0
def mask_filter(pixel_mask, link_mask, neighbors=8, scale=4): """ pixel_mask: batch_size * 2 * H * W link_mask: batch_size * 16 * H * W """ batch_size = link_mask.size(0) mask_height = link_mask.size(2) mask_width = link_mask.size(3) pixel_class = nn.Softmax2d()(pixel_mask) # print(pixel_class.shape) pixel_class = pixel_class[:, 1] > 0.7 # print(pixel_class.shape) # pixel_class = pixel_mask[:, 1] > pixel_mask[:, 0] # link_neighbors = torch.ByteTensor([batch_size, neighbors, mask_height, mask_width]) link_neighbors = torch.zeros([batch_size, neighbors, mask_height, mask_width], \ dtype=torch.uint8, device=pixel_mask.device) for i in range(neighbors): # print(link_mask[:, [2 * i, 2 * i + 1]].shape) tmp = nn.Softmax2d()(link_mask[:, [2 * i, 2 * i + 1]]) # print(tmp.shape) link_neighbors[:, i] = tmp[:, 1] > 0.7 # link_neighbors[:, i] = link_mask[:, 2 * i + 1] > link_mask[:, 2 * i] link_neighbors[:, i] = link_neighbors[:, i] & pixel_class # res_mask = np.zeros([batch_size, mask_height, mask_width], dtype=np.uint8) pixel_class = pixel_class.cpu().numpy() link_neighbors = link_neighbors.cpu().numpy() return pixel_class, link_neighbors
def __init__(self, inception): super(TCNDepthModel, self).__init__() self.transform_input = True self.Conv2d_1a_3x3 = inception.Conv2d_1a_3x3 self.Conv2d_2a_3x3 = inception.Conv2d_2a_3x3 self.Conv2d_2b_3x3 = inception.Conv2d_2b_3x3 self.Conv2d_3b_1x1 = inception.Conv2d_3b_1x1 self.Conv2d_4a_3x3 = inception.Conv2d_4a_3x3 self.Mixed_5b = inception.Mixed_5b self.Mixed_5c = inception.Mixed_5c self.Mixed_5d = inception.Mixed_5d self.Conv2d_6a_3x3 = BatchNormConv2d(288, 100, kernel_size=3, stride=1) self.Conv2d_6b_3x3 = BatchNormConv2d(100, 20, kernel_size=3, stride=1) self.SpatialSoftmax = nn.Softmax2d() self.FullyConnected7a = Dense(31 * 31 * 20, 32) # Depth layers self.Conv2d_depth_1a_3x3 = BatchNormConv2d(1, 64, kernel_size=3, stride=2) self.Conv2d_depth_1b_3x3 = BatchNormConv2d(64, 32, kernel_size=3, stride=1) self.Conv2d_depth_1c_3x3 = BatchNormConv2d(32, 10, kernel_size=3, stride=1) self.SpatialSoftmax_depth = nn.Softmax2d() self.FullyConnected2a_depth = Dense(72 * 72 * 10, 10) self.alpha = 10.0
def forward(self, input, train=False): ''' :param input: RGB image :return: transformed feature map ''' output1 = self.encoder.level1(input) # 8h 8w output2_0 = self.encoder.level2_0(output1) # 4h 4w output3_0 = self.encoder.level3_0(output2_0) # 2h 2w # print(str(output1_0.size())) for i, layer in enumerate(self.encoder.level3): if i == 0: output3 = layer(output3_0) else: output3 = layer(output3) # 2h 2w output4_0 = self.encoder.level4_0(self.encoder.BR3(torch.cat([output3_0, output3], 1))) # h w # print(str(output2_0.size())) for i, layer in enumerate(self.encoder.level4): if i == 0: output4 = layer(output4_0) else: output4 = layer(output4) output4_cat = self.encoder.BR4(torch.cat([output4_0, output4], 1)) Enc_final = self.encoder.classifier(output4_cat) Dnc_stage1 = self.bn_4(self.up(Enc_final, scale_factor=2, mode="bilinear")) # 2h 2w stage1_confidence = nn.Softmax2d()(Dnc_stage1) b, c, h, w = Dnc_stage1.size() # Coarse_att = ((torch.max(Coarse_confidence,dim=1)[0]).unsqueeze(1)).expand(b,c,h,w) stage1_blocking = (torch.max(stage1_confidence, dim=1)[0]).unsqueeze(1).expand(b, c, h, w) Dnc_stage2_0 = self.level3_C(output3) # 2h 2w Dnc_stage2 = self.bn_3( self.up(Dnc_stage2_0 * (1 - stage1_blocking) + (Dnc_stage1), scale_factor=2, mode="bilinear")) # 4h 4w stage2_confidence = nn.Softmax2d()(Dnc_stage2) # 4h 4w b, c, h, w = Dnc_stage2.size() stage2_blocking = (torch.max(stage2_confidence, dim=1)[0]).unsqueeze(1).expand(b, c, h, w) Dnc_stage3 = output2_0 * (1 - stage2_blocking) + (Dnc_stage2) classifier = self.classifier(Dnc_stage3) import torch.nn.functional as F classifier = F.interpolate( classifier, scale_factor=2, mode="bilinear", align_corners=True) if train: return Enc_final, classifier else : return classifier
def __init__(self, inception, action_dim=4): super(TCNModel, self).__init__() self.action_dim = action_dim self.state_dim = 128 self.transform_input = True self.Conv2d_1a_3x3 = inception.Conv2d_1a_3x3 self.Conv2d_2a_3x3 = inception.Conv2d_2a_3x3 self.Conv2d_2b_3x3 = inception.Conv2d_2b_3x3 self.Conv2d_3b_1x1 = inception.Conv2d_3b_1x1 self.Conv2d_4a_3x3 = inception.Conv2d_4a_3x3 self.Mixed_5b = inception.Mixed_5b self.Mixed_5c = inception.Mixed_5c self.Mixed_5d = inception.Mixed_5d self.Mixed_6a = inception.Mixed_6a self.Mixed_6b = inception.Mixed_6b self.Mixed_6c = inception.Mixed_6c self.Mixed_6d = inception.Mixed_6d self.Mixed_6e = inception.Mixed_6e self.Mixed_7a = inception.Mixed_7a self.Mixed_7b = inception.Mixed_7b self.Mixed_7c = inception.Mixed_7c self.Conv2d_6a_3x3 = BatchNormConv2d(288, 100, kernel_size=3, stride=1) self.Conv2d_6b_3x3 = BatchNormConv2d(100, 20, kernel_size=3, stride=1) self.SpatialSoftmax = nn.Softmax2d() self.FullyConnected7a = Dense(31 * 31 * 20, self.state_dim) self.FullyConnectedSingle = Dense(self.state_dim, 128) # self.FullyConnectedConcat = Dense(2*self.state_dim, 128) # self.FullyConnectedPose1 = Dense(128, 256) self.FullyConnectedPose2 = Dense(256, 128) self.FullyConnectedPose3 = Dense(128, self.action_dim) self.tanh = torch.nn.Tanh() self.hardtanh = torch.nn.Hardtanh(min_val=0, max_val=math.pi) self.FullyConnectedAction1 = Dense(self.state_dim + self.action_dim, 256) self.FullyConnectedAction2 = Dense(256, 512) self.FullyConnectedAction3 = Dense(512, 256) self.FullyConnectedAction4 = Dense(256, 32) # For depth self.Depth_Conv2d_6a_3x3 = BatchNormConv2d(1, 100, kernel_size=3, stride=1) self.Depth_Conv2d_6b_3x3 = BatchNormConv2d(100, 20, kernel_size=3, stride=1) self.Depth_max_pool_1 = F.max_pool2d(x, kernel_size=2, stride=2) self.Depth_Conv2d_6b_3x3 = BatchNormConv2d(83, 20, kernel_size=3, stride=1) self.Depth_SpatialSoftmax = nn.Softmax2d() self.Depth_FullyConnected7a = Dense(31 * 31 * 20, self.state_dim) self.Depth_FullyConnectedSingle = Dense(self.state_dim, 128) self.alpha = 10.0
def __init__(self, num_classes, h1=20, h2=30): super(DenseClassifier, self).__init__() self.Conv2d_1a_3x3 = BatchNormConv2d(100, 10, kernel_size=3, stride=1) self.SpatialSoftmax = nn.Softmax2d() self.FullyConnected7a = Dense(33 * 33 * 100, VOCAB_SIZE) self.FullyConnected7b = Dense(128, VOCAB_SIZE) self._Conv2d_1a_3x3 = BatchNormConv2d(100, 10, kernel_size=3, stride=1) self._SpatialSoftmax = nn.Softmax2d() self._FullyConnected7a = Dense(33 * 33 * 100, VOCAB_SIZE) self._FullyConnected7b = Dense(128, VOCAB_SIZE)
def __init__(self): self._done = False ############################# ### Kinect runtime object ### ############################# self.kinect = PyKinectRuntime.PyKinectRuntime( PyKinectV2.FrameSourceTypes_Color | PyKinectV2.FrameSourceTypes_Depth) self.depth_width, self.depth_height = self.kinect.depth_frame_desc.Width, self.kinect.depth_frame_desc.Height # Default: 512, 424 self.color_width, self.color_height = self.kinect.color_frame_desc.Width, self.kinect.color_frame_desc.Height # Default: 1920, 1080 ######################## ### Load the network ### ######################## self.net = FCN_NEW(n=8, f=40) self.net.load_state_dict(torch.load('model_FCN_NEW.pkl')) # self.net.load_state_dict(torch.load('model_FCN_ADF.pkl')) self.net.eval() ############### ### Add GPU ### ############### self.device = torch.device( "cuda" if torch.cuda.is_available() else "cpu") self.net = self.net.to(self.device) self.softmax = nn.Softmax2d()
def __init__(self, in_size=3): super(SegmenterModel, self).__init__() self.features = nn.Sequential() self.features.add_module('3x3', conv_bn_relu(in_planes = 3, out_planes = 8, stride = 2)) self.features.add_module('downsample_1', conv_bn_relu(8, 16, stride = 2)) self.features.add_module('conv_1', conv_bn_relu(16, 16)) self.features.add_module('conv_2', conv_bn_relu(16, 16)) self.features.add_module('downsample_2', conv_bn_relu(16, 32, stride = 2)) self.features.add_module('conv_3', conv_bn_relu(32, 32)) self.features.add_module('conv_4', conv_bn_relu(32, 32)) self.features.add_module('downsample_3', conv_bn_relu(32, 64, stride = 2)) self.features.add_module('conv_5', conv_bn_relu(64, 64)) self.features.add_module('conv_6', conv_bn_relu(64, 64)) self.features.add_module('upsample_1', convtranspose_bn_relu(64, 32)) self.features.add_module('conv_7', conv_bn_relu(32, 32)) self.features.add_module('conv_8', conv_bn_relu(32, 32)) self.features.add_module('upsample_2', convtranspose_bn_relu(32, 16)) self.features.add_module('conv_9', conv_bn_relu(16, 16)) self.features.add_module('conv_10', conv_bn_relu(16, 16)) self.features.add_module('upsample_3', convtranspose_bn_relu(16, 8)) self.features.add_module('3x3_1', conv_bn_relu(8, 8)) self.features.add_module('upsample_4', convtranspose_bn_relu(8, 1)) self.fc_classifier = nn.Softmax2d()
def __init__(self, inception): super(TCNModel, self).__init__() self.transform_input = True self.Conv2d_1a_3x3 = inception.Conv2d_1a_3x3 self.Conv2d_2a_3x3 = inception.Conv2d_2a_3x3 self.Conv2d_2b_3x3 = inception.Conv2d_2b_3x3 self.Conv2d_3b_1x1 = inception.Conv2d_3b_1x1 self.Conv2d_4a_3x3 = inception.Conv2d_4a_3x3 self.Mixed_5b = inception.Mixed_5b self.Mixed_5c = inception.Mixed_5c self.Mixed_5d = inception.Mixed_5d self.Mixed_6a = inception.Mixed_6a self.Mixed_6b = inception.Mixed_6b self.Mixed_6c = inception.Mixed_6c self.Mixed_6d = inception.Mixed_6d self.Mixed_6e = inception.Mixed_6e self.Mixed_7a = inception.Mixed_7a self.Mixed_7b = inception.Mixed_7b self.Mixed_7c = inception.Mixed_7c self.Conv2d_6a_3x3 = BatchNormConv2d(288, 100, kernel_size=3, stride=1) self.Conv2d_6b_3x3 = BatchNormConv2d(100, 20, kernel_size=3, stride=1) self.SpatialSoftmax = nn.Softmax2d() self.FullyConnected7a = Dense(31 * 31 * 20, 32) self.alpha = 10.0
def __init__(self, feat_dyn_dim, hidden_dim, n_points, resolution=None, pos_enc=True): super(KeyPointsBroadcastExtractor, self).__init__() self.temp = 1 n_coords = 2 if pos_enc else 0 if resolution is not None: self.resolution = resolution else: self.resolution = [64, 64] self.nn = nn.Sequential( nn.Conv2d(n_coords + feat_dyn_dim, hidden_dim, 1), nn.ReLU(True), nn.BatchNorm2d(hidden_dim), nn.Conv2d(hidden_dim, hidden_dim, 3, 1, 1), nn.ReLU(True), nn.BatchNorm2d(hidden_dim), nn.Conv2d(hidden_dim, hidden_dim, 3, 1, 1), nn.ReLU(True), nn.BatchNorm2d(hidden_dim), nn.Conv2d(hidden_dim, n_points, 1), ) self.softmax2d = nn.Softmax2d() self.grids = Grid() self.register_buffer('grid', self.grids._build_warping_grid(self.resolution))
def __init__(self): super(MyLeNet, self).__init__() self.choice_conv = nn.ModuleDict({ 'conv1': nn.Conv2d(1, 10, 3, 1, 2), 'conv2': nn.Conv2d(10, 10, 3, 1), 'conv3': nn.Conv2d(1, 10, 3, 1), 'conv4': nn.Conv2d(1, 10, 5, 1), 'conv5': nn.Conv2d(1, 6, 5, 1), ##c1 'conv6': nn.Conv2d(6, 16, 5, 1), ##c2 'conv7': nn.Conv2d(16, 120, 5, 1) ##c3 }) self.choice_pooling = nn.ModuleDict({ 'maxpooling1': nn.MaxPool2d(2, 2), #'maxpooling2':nn.MaxPool2d(1,1), 'avgpooling1': nn.AvgPool2d(2, 2), }) self.choice_activations = nn.ModuleDict({ 'rule': nn.ReLU(), 'leakyrule': nn.LeakyReLU(), 'logsigmoid': nn.LogSigmoid(), 'prelu': nn.PReLU(), 'sigmoid': nn.Sigmoid(), 'tanh': nn.Tanh(), 'softmin': nn.Softmin(), 'softmax': nn.Softmax(), 'softmax2': nn.Softmax2d() }) self.choice_fc = nn.ModuleDict({ 'f1': nn.Linear(120, 84), 'f2': nn.Linear(84, 10) })
def __init__(self, n_classes, dropout_p=0.4): super().__init__() use_cuda = torch.cuda.is_available() # check if GPU exists self.device = torch.device( "cuda" if use_cuda else "cpu") # use CPU or GPU self.n_classes = n_classes self.dropout_p = dropout_p self.resnet = models.resnet34(pretrained=True) self.resnet_layers = list(self.resnet.children()) self.upsample = nn.Upsample(scale_factor=2, mode='nearest') self.upsample_7x7 = nn.Upsample((7, 7), mode='nearest') self.seq1 = nn.Sequential(*self.resnet_layers[0:3]) self.seq2 = nn.Sequential(*self.resnet_layers[3:5]) self.seq3 = nn.Sequential(*self.resnet_layers[5]) self.seq4 = nn.Sequential(*self.resnet_layers[6]) self.seq5 = nn.Sequential(*self.resnet_layers[7]) self.conv2d_0 = self.conv2d(512, 1024, kernel=3, stride=2, padding=1) self.conv2d_01 = self.conv2d(1024, 2048, kernel=3, stride=2, padding=1) self.adaptivePooling = nn.AdaptiveAvgPool2d((1, 1)) self.conv2d_00d = self.conv2d(2048 + 2048, 2048, kernel=3, padding=1) self.conv2d_01d = self.conv2d(2048 + 1024, 1024, kernel=3, padding=1) self.conv2d_02d = self.conv2d(1024 + 512, 512, kernel=3, padding=1) self.conv2d_1 = self.conv2d(512 + 256, 256, kernel=3, padding=1) self.conv2d_2 = self.conv2d(256 + 128, 128, kernel=3, padding=1) self.conv2d_3 = self.conv2d(128 + 64, 64, kernel=3, padding=1) self.conv2d_4 = self.conv2d(64 + 64, 64, kernel=3, padding=1) self.conv2d_f = self.conv2d_final(64, self.n_classes) self.softmax = nn.Softmax2d()
def __init__(self, mapping, shapes, z_shape, dropout): super(Generator, self).__init__() self.z_size = z_shape[0] filters = 512 self.init_shape = (filters, *shapes[0]) self.preprocess = nn.Sequential( nn.Linear(self.z_size, reduce(mul, self.init_shape), bias=False), nn.LeakyReLU(True)) self.blocks = nn.ModuleList() in_ch = filters for s in shapes[1:-1]: out_ch = in_ch // 2 block = nn.Sequential( utils.Resize(s), nn.Conv2d(in_ch, out_ch, 3, padding=1, bias=False), nn.LeakyReLU(True), nn.Conv2d(out_ch, out_ch, 3, padding=1, bias=False), nn.BatchNorm2d(out_ch), nn.LeakyReLU(True), ) in_ch = out_ch self.blocks.append(block) out_ch = len(mapping) self.output = nn.Sequential( utils.Resize(shapes[-1]), nn.Conv2d(in_ch, out_ch, 3, padding=1, bias=True), nn.Softmax2d())
def forward(self, x, is_training=True): """ Parameters ---------- x : torch.FloatTensor Batch of encoded feature tensors | Shape: (batch_size, 128, occ_map_size/2^5, occ_map_size/2^5) is_training : bool whether its training or testing phase Returns ------- x : torch.FloatTensor Batch of output Layouts | Shape: (batch_size, 2, occ_map_size, occ_map_size) """ for i in range(4, -1, -1): x = self.convs[("upconv", i, 0)](x) x = self.convs[("norm", i, 0)](x) x = self.convs[("relu", i, 0)](x) x = upsample(x) x = self.convs[("upconv", i, 1)](x) x = self.convs[("norm", i, 1)](x) if is_training: x = self.convs["topview"](x) else: softmax = nn.Softmax2d() x = softmax(self.convs["topview"](x)) return x
def __init__(self, spatial_features_size, width, height): super().__init__() # comment lines 106-117 from ./local/lib/python3.6/site-packages/torchvision/models/inception.py # to make it load faster # those line initialize weights, they slow down the code because of a problem with scipy.stats # we are using pretrained inception model, so we don't need that self.inception = models.inception_v3(pretrained=True, progress=True) self.conv1 = self.inception.Conv2d_1a_3x3 self.conv2_1 = self.inception.Conv2d_2a_3x3 self.conv2_2 = self.inception.Conv2d_2b_3x3 self.conv3 = self.inception.Conv2d_3b_1x1 self.conv4 = self.inception.Conv2d_4a_3x3 self.mix5_1 = self.inception.Mixed_5b self.mix5_2 = self.inception.Mixed_5c self.mix5_3 = self.inception.Mixed_5d # freezing inception model for param in self.parameters(): param.requires_grad = False self.conv6_1 = nn.Conv2d(288, 100, kernel_size=3, stride=1) self.batch_norm_1 = nn.BatchNorm2d(100, eps=1e-3) self.conv6_2 = nn.Conv2d(100, spatial_features_size, kernel_size=3, stride=1) self.batch_norm_2 = nn.BatchNorm2d(spatial_features_size, eps=1e-3) # softmax2d is an activation in each channel # spatial softmax s_{ij}=\frac{exp(a_{ij})}{\sum_{i',j'} exp(a_{i'j'})} self.spatial_softmax = nn.Softmax2d() self.fc7 = nn.Linear( spatial_features_size * int(width / 8 - 7) * int(height / 8 - 7), 32) self.alpha = 10.0
def add_final_layer(self, out_channels): self.softmax = nn.Softmax2d() self.final = nn.Conv2d(self.layer_data[2], out_channels, kernel_size=1, stride=1) self.forward = self.__has_final_layer
def __init__(self): super(NNActivationModule, self).__init__() self.activations = nn.ModuleList([ nn.ELU(), nn.Hardshrink(), nn.Hardsigmoid(), nn.Hardtanh(), nn.Hardswish(), nn.LeakyReLU(), nn.LogSigmoid(), # nn.MultiheadAttention(), nn.PReLU(), nn.ReLU(), nn.ReLU6(), nn.RReLU(), nn.SELU(), nn.CELU(), nn.GELU(), nn.Sigmoid(), nn.SiLU(), nn.Mish(), nn.Softplus(), nn.Softshrink(), nn.Softsign(), nn.Tanh(), nn.Tanhshrink(), # nn.Threshold(0.1, 20), nn.GLU(), nn.Softmin(), nn.Softmax(), nn.Softmax2d(), nn.LogSoftmax(), # nn.AdaptiveLogSoftmaxWithLoss(), ])
def __init__(self): super(LUnet, self).__init__() # All layers which have weights are created and initlialitzed in init. # parameterless modules are used in functional style F. in forward # (object version of parameterless modules can be created with nn.init too ) # https://pytorch.org/docs/master/nn.html#conv2d # in_channels, out_channels, kernel_size, stride, padding, dilation, groups, bias L = 32 self.conv1 = nn.Linear(512 * 512, 16 * L) self.conv3 = nn.Linear(16 * L, 8 * L) self.conv5 = nn.Linear(8 * L, 4 * L) self.conv7 = nn.Linear(4 * L, 2 * L) self.conv9 = nn.Linear(2 * L, L) self.conv11 = nn.Linear(L, 2 * L) self.conv13 = nn.Linear(2 * L, 4 * L) self.conv15 = nn.Linear(4 * L, 8 * L) self.conv17 = nn.Linear(8 * L, 16 * L) self.conv19 = nn.Linear(16 * L, 512 * 512 * 2) self.softmax = nn.Softmax2d() # weights can be initialized here: # for example: for idx, m in enumerate(self.modules()): if isinstance(m, nn.Linear): nn.init.xavier_normal_(m.weight)
def __init__(self, in_channels=5, out_channels=3, ngpu=2): super(GeneratorLowParameter, self).__init__() self.ngpu = ngpu self.down1 = UNetDown(in_channels, 8, normalize=True) self.down2 = UNetDown(8, 16) self.down3 = UNetDown(16, 32) self.down4 = UNetDown(32, 64, dropout=0.2) self.down5 = UNetDown(64, 64, dropout=0.2) # self.down6 = UNetDown(64, 64, dropout=0.5) # self.down7 = UNetDown(64, 64, dropout=0.5) self.down6 = UNetDown(64, 64, normalize=False, dropout=0.2) self.up1 = UNetUp(64, 64, dropout=0.2) # self.up2 = UNetUp(128, 128, dropout=0.5) # self.up3 = UNetUp(64, 64, dropout=0.5) self.up4 = UNetUp(128, 64, dropout=0.2) self.up5 = UNetUp(128, 32) self.up6 = UNetUp(64, 16) self.up7 = UNetUp(32, 8) self.final = nn.Sequential( nn.Upsample(size=(375, 1242)), nn.ZeroPad2d((1, 0, 1, 0)), nn.Conv2d(16, out_channels, 4, padding=1), nn.Softmax2d(), )
def __init__(self): super(Unet_regression, self).__init__() self.num_filters = 32 self.num_channels = 3 self.num_classes = 2 filters = [ self.num_filters, self.num_filters * 2, self.num_filters * 4, self.num_filters * 8, self.num_filters * 16 ] self.conv1 = conv_block(self.num_channels, filters[0]) self.pool1 = nn.MaxPool2d(kernel_size=2) self.conv2 = conv_block(filters[0], filters[1]) self.pool2 = nn.MaxPool2d(kernel_size=2) self.conv3 = conv_block(filters[1], filters[2]) self.pool3 = nn.MaxPool2d(kernel_size=2) self.conv4 = conv_block(filters[2], filters[3], if_dropout=True) self.pool4 = nn.MaxPool2d(kernel_size=2) self.center = conv_block(filters[3], filters[4], if_dropout=True) self.up4 = UpCatconv(filters[4], filters[3], if_dropout=True) self.up3 = UpCatconv(filters[3], filters[2]) self.up2 = UpCatconv(filters[2], filters[1]) self.up1 = UpCatconv(filters[1], filters[0]) self.out_seg = nn.Sequential( nn.Conv2d(filters[0], self.num_classes, kernel_size=1), nn.Softmax2d()) self.out_tanh = nn.Sequential(nn.Conv2d(filters[0], 1, kernel_size=1), nn.Tanh()) print('Initial Unet_regression.')
def __init__(self, num_channels=1, num_classes=2): super(UNet, self).__init__() num_feat = [64, 128, 256, 512, 1024] self.down1 = nn.Sequential(Conv3x3(num_channels, num_feat[0])) self.down2 = nn.Sequential(nn.MaxPool2d(kernel_size=2), Conv3x3(num_feat[0], num_feat[1])) self.down3 = nn.Sequential(nn.MaxPool2d(kernel_size=2), Conv3x3(num_feat[1], num_feat[2])) self.down4 = nn.Sequential(nn.MaxPool2d(kernel_size=2), Conv3x3(num_feat[2], num_feat[3])) self.bottom = nn.Sequential(nn.MaxPool2d(kernel_size=2), Conv3x3(num_feat[3], num_feat[4])) self.up1 = UpConcat(num_feat[4], num_feat[3]) self.upconv1 = Conv3x3(num_feat[4], num_feat[3]) self.up2 = UpConcat(num_feat[3], num_feat[2]) self.upconv2 = Conv3x3(num_feat[3], num_feat[2]) self.up3 = UpConcat(num_feat[2], num_feat[1]) self.upconv3 = Conv3x3(num_feat[2], num_feat[1]) self.up4 = UpConcat(num_feat[1], num_feat[0]) self.upconv4 = Conv3x3(num_feat[1], num_feat[0]) self.final = nn.Sequential(nn.Conv2d(num_feat[0], num_classes, kernel_size=1), nn.Softmax2d())
def diceCoeff(pred, gt, smooth=1, activation='sigmoid'): r""" computational formula£º dice = (2 * (pred ¡É gt)) / (pred ¡È gt) """ if activation is None or activation == "none": activation_fn = lambda x: x elif activation == "sigmoid": activation_fn = nn.Sigmoid() elif activation == "softmax2d": activation_fn = nn.Softmax2d() else: raise NotImplementedError("Activation implemented for sigmoid and softmax2d ¼¤»îº¯ÊýµÄ²Ù×÷") pred = activation_fn(pred) N = gt.size(0) pred_flat = pred.view(N, -1) gt_flat = gt.view(N, -1) intersection = (pred_flat * gt_flat).sum(1) unionset = pred_flat.sum(1) + gt_flat.sum(1) loss = 2 * (intersection + smooth) / (unionset + smooth) return loss.sum() / N
def __init__(self): super(CNN, self).__init__() self.layer1 = nn.Sequential( nn.Conv2d(1, 10, kernel_size=3, padding=1), nn.BatchNorm2d(10), nn.ReLU(), nn.MaxPool2d(2), ) self.layer2 = nn.Sequential( nn.Conv2d(10, 10, kernel_size=3, padding=1), nn.BatchNorm2d(20), nn.ReLU(), ) self.layer3 = nn.Sequential( nn.Conv2d(10, 20, kernel_size=3, padding=1), nn.BatchNorm2d(20), nn.ReLU(), ) self.layer4 = nn.Sequential( nn.Conv2d(20, 20, kernel_size=3, padding=1), nn.BatchNorm2d(20), nn.ReLU(), nn.MaxPool2d(2)) self.layer5 = nn.Linear(7 * 7 * 20, 10) #28-14-7 (2 maxpools) self.layer6 = nn.Softmax2d()
def __init__(self,opt): super(QHead_,self).__init__() self.model_mg = nn.Sequential(nn.Conv2d(in_channels = 1, out_channels = 512, kernel_size = (1,3), stride = 1, padding = 0), nn.BatchNorm2d(512,0.8),nn.LeakyReLU(0.2,inplace=True), nn.Conv2d(in_channels = 512, out_channels = 256, kernel_size = (1,1), stride = 1, padding = 0), nn.BatchNorm2d(256,0.8),nn.LeakyReLU(0.2,inplace=True), nn.Conv2d(in_channels = 256, out_channels = 256, kernel_size= (1,1), stride = 1, padding = 0), nn.BatchNorm2d(256,0.8),nn.LeakyReLU(0.2,inplace=True), nn.Conv2d(in_channels = 256, out_channels = 2, kernel_size = (1,1), stride =1, padding =0)) self.model_o = nn.Sequential(nn.Conv2d(in_channels = 1, out_channels = 512, kernel_size = (1,3), stride = 1, padding = 0), nn.BatchNorm2d(512,0.8),nn.LeakyReLU(0.2,inplace=True), nn.Conv2d(in_channels = 512, out_channels = 256, kernel_size = (1,1), stride = 1, padding = 0), nn.BatchNorm2d(256,0.8),nn.LeakyReLU(0.2,inplace=True), nn.Conv2d(in_channels = 256, out_channels = 256, kernel_size= (1,1), stride = 1, padding = 0), nn.BatchNorm2d(256,0.8),nn.LeakyReLU(0.2,inplace=True), nn.Conv2d(in_channels = 256, out_channels = 2, kernel_size = (1,1), stride =1, padding =0)) self.model_cell = nn.Sequential(nn.Conv2d(in_channels = 1, out_channels = 64, kernel_size = (1,3), stride= 1, padding = 0), nn.BatchNorm2d(64,0.8),nn.LeakyReLU(0.2,inplace=True), nn.Conv2d(in_channels = 64, out_channels = 64, kernel_size = (1,1), stride = 1, padding = 0), nn.BatchNorm2d(64,0.8),nn.LeakyReLU(0.2,inplace=True)) self.softmax = nn.Softmax2d() self.label_mg_layer = nn.Sequential(nn.Linear(16,300),nn.BatchNorm1d(300,0.8),nn.LeakyReLU(0.2,inplace=True), nn.Linear(300,100),nn.BatchNorm1d(100,0.8),nn.LeakyReLU(0.2,inplace=True),nn.Linear(100,8),nn.Softmax()) self.label_o_layer = nn.Sequential(nn.Linear(24,300),nn.BatchNorm1d(300,0.8),nn.LeakyReLU(0.2,inplace=True), nn.Linear(300,100),nn.BatchNorm1d(100,0.8),nn.LeakyReLU(0.2,inplace=True),nn.Linear(100,12),nn.Softmax()) self.label_c_layer = nn.Sequential(nn.Linear(128,100),nn.BatchNorm1d(100,0.8),nn.LeakyReLU(0.2,inplace=True),nn.Linear(100,50),nn.BatchNorm1d(50,0.8),nn.LeakyReLU(),nn.Linear(50,1),nn.Sigmoid())
def __init__(self, num_F=32): super(feature3d, self).__init__() self.F = num_F self.l19 = conv3d_bn(self.F*2, self.F, kernel_size=3, stride=1, flag_bias=flag_bias_t, bn=flag_bn, activefun=activefun_t) self.l20 = conv3d_bn(self.F, self.F, kernel_size=3, stride=1, flag_bias=flag_bias_t, bn=flag_bn, activefun=activefun_t) self.l21 = conv3d_bn(self.F*2, self.F*2, kernel_size=3, stride=2, flag_bias=flag_bias_t, bn=flag_bn, activefun=activefun_t) self.l22 = conv3d_bn(self.F*2, self.F*2, kernel_size=3, stride=1, flag_bias=flag_bias_t, bn=flag_bn, activefun=activefun_t) self.l23 = conv3d_bn(self.F*2, self.F*2, kernel_size=3, stride=1, flag_bias=flag_bias_t, bn=flag_bn, activefun=activefun_t) self.l24 = conv3d_bn(self.F*2, self.F*2, kernel_size=3, stride=2, flag_bias=flag_bias_t, bn=flag_bn, activefun=activefun_t) self.l25 = conv3d_bn(self.F*2, self.F*2, kernel_size=3, stride=1, flag_bias=flag_bias_t, bn=flag_bn, activefun=activefun_t) self.l26 = conv3d_bn(self.F*2, self.F*2, kernel_size=3, stride=1, flag_bias=flag_bias_t, bn=flag_bn, activefun=activefun_t) self.l27 = conv3d_bn(self.F*2, self.F*2, kernel_size=3, stride=2, flag_bias=flag_bias_t, bn=flag_bn, activefun=activefun_t) self.l28 = conv3d_bn(self.F*2, self.F*2, kernel_size=3, stride=1, flag_bias=flag_bias_t, bn=flag_bn, activefun=activefun_t) self.l29 = conv3d_bn(self.F*2, self.F*2, kernel_size=3, stride=1, flag_bias=flag_bias_t, bn=flag_bn, activefun=activefun_t) self.l30 = conv3d_bn(self.F*2, self.F*4, kernel_size=3, stride=2, flag_bias=flag_bias_t, bn=flag_bn, activefun=activefun_t) self.l31 = conv3d_bn(self.F*4, self.F*4, kernel_size=3, stride=1, flag_bias=flag_bias_t, bn=flag_bn, activefun=activefun_t) self.l32 = conv3d_bn(self.F*4, self.F*4, kernel_size=3, stride=1, flag_bias=flag_bias_t, bn=flag_bn, activefun=activefun_t) self.l33 = deconv3d_bn(self.F*4, self.F*2, kernel_size=3, stride=2, flag_bias=flag_bias_t, bn=flag_bn, activefun=activefun_t) self.l34 = deconv3d_bn(self.F*2, self.F*2, kernel_size=3, stride=2, flag_bias=flag_bias_t, bn=flag_bn, activefun=activefun_t) self.l35 = deconv3d_bn(self.F*2, self.F*2, kernel_size=3, stride=2, flag_bias=flag_bias_t, bn=flag_bn, activefun=activefun_t) self.l36 = deconv3d_bn(self.F*2, self.F, kernel_size=3, stride=2, flag_bias=flag_bias_t, bn=flag_bn, activefun=activefun_t) self.l37 = deconv3d_bn(self.F, 1, kernel_size=3, stride=2, bn=False, activefun=None) self.softmax = nn.Softmax2d()
def __init__(self): super(SEC_NN, self).__init__() self.features = nn.Sequential( # the Sequential name has to be 'vgg feature'. the params name will be like feature.0.weight , nn.Conv2d(3, 64, (3, 3), (1, 1), (1, 1)), nn.ReLU(), nn.Conv2d(64, 64, (3, 3), (1, 1), (1, 1)), nn.ReLU(), nn.MaxPool2d((3, 3), (2, 2), (1, 1), ceil_mode=True), nn.Conv2d(64, 128, (3, 3), (1, 1), (1, 1)), nn.ReLU(), nn.Conv2d(128, 128, (3, 3), (1, 1), (1, 1)), nn.ReLU(), nn.MaxPool2d((3, 3), (2, 2), (1, 1), ceil_mode=True), nn.Conv2d(128, 256, (3, 3), (1, 1), (1, 1)), nn.ReLU(), nn.Conv2d(256, 256, (3, 3), (1, 1), (1, 1)), nn.ReLU(), nn.Conv2d(256, 256, (3, 3), (1, 1), (1, 1)), nn.ReLU(), nn.MaxPool2d((3, 3), (2, 2), (1, 1), ceil_mode=True), nn.Conv2d(256, 512, (3, 3), (1, 1), (1, 1)), nn.ReLU(), nn.Conv2d(512, 512, (3, 3), (1, 1), (1, 1)), nn.ReLU(), nn.Conv2d(512, 512, (3, 3), (1, 1), (1, 1)), nn.ReLU(), nn.MaxPool2d((3, 3), (1, 1), (1, 1), ceil_mode=True), nn.Conv2d(512, 512, (3, 3), padding=2, dilation=2), nn.ReLU(), nn.Conv2d(512, 512, (3, 3), padding=2, dilation=2), nn.ReLU(), nn.Conv2d(512, 512, (3, 3), padding=2, dilation=2), nn.ReLU(), nn.MaxPool2d((3, 3), (1, 1), (1, 1), ceil_mode=True), nn.AvgPool2d((3, 3), (1, 1), (1, 1), ceil_mode=True), #AvgPool2d, nn.Conv2d(512, 1024, (3, 3), padding=12, dilation=12), nn.ReLU(), nn.Dropout(0.5), nn.Conv2d(1024, 1024, (1, 1)), nn.ReLU(), nn.Dropout(0.5), nn.Conv2d(1024, 21, (1, 1)) # 1024 / 512 # nn.Softmax2d() ) self.softmax2d = nn.Softmax2d() self.min_prob = 0.0001 # self.mask2pre = nn.AdaptiveAvgPool2d(1) # self.mask2pre = nn.AdaptiveMaxPool2d(1) self.mask2pre = nn.Sequential(nn.MaxPool2d(5, stride=2), nn.AdaptiveAvgPool2d(1)) 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.init.constant_(m.weight, 1) nn.init.constant_(m.bias, 0)
def __init__(self,norm_flag = 'l2'): super(SiameseNet, self).__init__() self.CNN = deeplab_V2() if norm_flag == 'l2': self.norm = F.normalize if norm_flag == 'exp': self.norm = nn.Softmax2d()
def __init__(self): super(CNN, self).__init__() self.layer1 = nn.Sequential( nn.Conv2d(1,4, kernel_size = (5,5), padding = (2, 2)), nn.BatchNorm2d(4), nn.ReLU(), nn.MaxPool2d(2)) kaiming_uniform(self.layer1[0].weight) self.layer2 = nn.Sequential( nn.Conv2d(4, 16, kernel_size = (5,5), padding = (2,2)), nn.BatchNorm2d(16), nn.ReLU(), nn.MaxPool2d(2)) kaiming_uniform(self.layer2[0].weight) self.layer3 = nn.Sequential( nn.Conv2d(16, 32, kernel_size = (5,5), padding = (2,2)), nn.BatchNorm2d(32), nn.ReLU(), nn.MaxPool2d(2)) kaiming_uniform(self.layer3[0].weight) self.fc1 = nn.Sequential( nn.Linear(4096, 4*n_bins), nn.Sigmoid() ) xavier_uniform(self.fc1[0].weight) self.fc2 = nn.Sequential( nn.Linear(4*n_bins, 4*n_bins), nn.Sigmoid() ) xavier_uniform(self.fc2[0].weight) self.out = nn.Softmax2d()
def __init__(self, in_channels, out_channels, size1=(128, 545), size2=(120, 529), size3=(104, 497), size4=(72, 186), l1weight=0.2): super(AttentionModule_stg0, self).__init__() self.l1weight = l1weight self.pre = ResidualBlock(in_channels, 1) ## trunk branch self.trunk = nn.Sequential(ResidualBlock(in_channels, 1), ResidualBlock(in_channels, 1)) ## softmax branch: bottom-up self.mp1 = nn.MaxPool2d(kernel_size=3, stride=(1, 1)) self.sm1 = ResidualBlock(in_channels, (4, 8)) self.skip1 = ResidualBlock(in_channels, 1) self.mp2 = nn.MaxPool2d(kernel_size=3, stride=(1, 1)) self.sm2 = ResidualBlock(in_channels, (8, 16)) self.skip2 = ResidualBlock(in_channels, 1) self.mp3 = nn.MaxPool2d(kernel_size=3, stride=(1, 2)) self.sm3 = ResidualBlock(in_channels, (16, 32)) self.skip3 = ResidualBlock(in_channels, 1) self.mp4 = nn.MaxPool2d(kernel_size=3, stride=(2, 2)) self.sm4 = nn.Sequential(ResidualBlock(in_channels, (16, 32)), ResidualBlock(in_channels, 1)) ## softmax branch: top-down self.up4 = nn.UpsamplingBilinear2d(size=size4) self.sm5 = ResidualBlock(in_channels, 1) self.up3 = nn.UpsamplingBilinear2d(size=size3) self.sm6 = ResidualBlock(in_channels, 1) self.up2 = nn.UpsamplingBilinear2d(size=size2) self.sm7 = ResidualBlock(in_channels, 1) self.up1 = nn.UpsamplingBilinear2d(size=size1) # 1*1 convolution blocks self.conv1 = nn.Sequential( nn.BatchNorm2d(in_channels), nn.ReLU(inplace=True), nn.Conv2d(in_channels, in_channels, kernel_size=1, stride=1, bias=False), nn.BatchNorm2d(in_channels), nn.ReLU(inplace=True), nn.Conv2d(in_channels, in_channels, kernel_size=1, stride=1, bias=False), #nn.Sigmoid() nn.Softmax2d()) self.post = ResidualBlock(in_channels, 1)
def __init__(self): super(D, self).__init__() self.main = nn.Sequential( nn.Conv2d(512, 11, kernel_size=(2, 2), stride=(1, 1), bias=False), #n.LeakyReLU() nn.Softmax2d())