MCPcopy Create free account
hub / github.com/TPCD/DCCL / Attribute_Classifier3

Class Attribute_Classifier3

model/vision_transformer.py:470–544  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

468
469
470class Attribute_Classifier3(nn.Module):
471 def __init__(self, dict_attribute, in_dim, projected_dim, norm_type='bn', use_independent_projection=True):
472 super().__init__()
473 print(dict_attribute)
474 self.num_attribute_class = len(dict_attribute.keys())
475 self.num_attribute_all = sum([len(v) for v in dict_attribute.values()])
476 self.classifier_list = nn.ModuleList()
477 self._log_softmax = nn.LogSoftmax(dim=1)
478 if norm_type is None or norm_type == 'none':
479 use_norm = False
480 elif norm_type == 'bn':
481 norm_class = nn.BatchNorm1d
482 use_norm = True
483 elif norm_type == 'ln':
484 norm_class = nn.LayerNorm
485 use_norm = True
486 else:
487 raise NotImplementedError
488
489 if use_independent_projection:
490 self.shared_projected_layer = None
491 else:
492 if use_norm:
493 self.shared_projected_layer = nn.Sequential(nn.Linear(in_dim, projected_dim * 2),
494 norm_class(projected_dim * 2),
495 nn.GELU(),
496 nn.Linear(projected_dim * 2, projected_dim),
497 norm_class(projected_dim),
498 )
499 else:
500 self.shared_projected_layer = nn.Sequential(nn.Linear(in_dim, projected_dim * 2),
501 nn.GELU(),
502 nn.Linear(projected_dim * 2, projected_dim)
503 )
504
505 for key in dict_attribute.keys():
506 if use_independent_projection:
507 layers = [nn.Linear(in_dim, projected_dim)]
508 if use_norm:
509 layers.append(norm_class(projected_dim))
510 layers.append(nn.GELU())
511 layers.append(nn.Linear(projected_dim, len(dict_attribute[key]) + 1)) # 1 for no present
512 self.classifier_list.append(nn.Sequential(*layers))
513 else:
514 _classifier = nn.Linear(projected_dim, len(dict_attribute[key]) + 1) # 1 for no present
515 self.classifier_list.append(_classifier)
516 self.apply(self._init_weights)
517
518 def _init_weights(self, m):
519 if isinstance(m, nn.Linear):
520 trunc_normal_(m.weight, std=.02)
521 if isinstance(m, nn.Linear) and m.bias is not None:
522 nn.init.constant_(m.bias, 0)
523
524 def forward(self, x):
525 probability = []
526 if self.shared_projected_layer is None:
527 attribute_embedding = []

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected