def fromConfig(cls, config): encoder = get_encoder(config) if config['fpn_out'] == 2: fpn = FPN_2(encoder, get_fpn_skip(config), config['skip_out_channel']) for param in fpn.parameters(): param.requires_grad = True elif config['fpn_out'] == 3: fpn = FPN_3(encoder, get_fpn_skip(config), config['skip_out_channel']) for param in fpn.parameters(): param.requires_grad = True return fpn
def __init__( self, hparams: LinearClassifierMethodParams = None, **kwargs, ): super().__init__() if hparams is None: hparams = self.params(**kwargs) elif isinstance(hparams, dict): hparams = self.params(**hparams, **kwargs) self.hparams = AttributeDict(attr.asdict(hparams)) # actually do a load that is a little more flexible self.model = utils.get_encoder(hparams.encoder_arch) self.dataset = utils.get_class_dataset(hparams.dataset_name) self.classifier = torch.nn.Linear(hparams.embedding_dim, self.dataset.num_classes)
def run(args): encoder = get_encoder(args) img_descriptor = ImageDescriptor(args, encoder=encoder) print(img_descriptor) if args.mode == 'train': # train the network if args.plot: fig, axes = plt.subplots(ncols=1, figsize=(7, 3)) img_descriptor.train( plot_loss=lambda model: plot_loss(model, fig=fig, axes=axes)) else: img_descriptor.train() elif args.mode == 'test': # get image caption for one image img_descriptor.test(args.image_path, plot=args.plot) elif args.mode == 'val': # only run validation set for a specific epoch and get the average loss and perplexity img_descriptor.evaluate(print_info=True) else: raise ValueError('Invalid mode.')
def __init__( self, hparams: Union[ModelParams, dict, None] = None, **kwargs, ): super().__init__() if hparams is None: hparams = self.params(**kwargs) elif isinstance(hparams, dict): hparams = self.params(**hparams, **kwargs) if isinstance(self.hparams, AttributeDict): self.hparams.update(AttributeDict(attr.asdict(hparams))) else: self.hparams = AttributeDict(attr.asdict(hparams)) # Check for configuration issues if (hparams.gather_keys_for_queue and not hparams.shuffle_batch_norm and not hparams.encoder_arch.startswith("ws_")): warnings.warn( "Configuration suspicious: gather_keys_for_queue without shuffle_batch_norm or weight standardization" ) some_negative_examples = hparams.use_negative_examples_from_batch or hparams.use_negative_examples_from_queue if hparams.loss_type == "ce" and not some_negative_examples: warnings.warn( "Configuration suspicious: cross entropy loss without negative examples" ) # Create encoder model self.model = utils.get_encoder(hparams.encoder_arch, hparams.dataset_name) # Create dataset self.dataset = utils.get_moco_dataset(hparams) if hparams.use_lagging_model: # "key" function (no grad) self.lagging_model = copy.deepcopy(self.model) for param in self.lagging_model.parameters(): param.requires_grad = False else: self.lagging_model = None self.projection_model = utils.MLP( hparams.embedding_dim, hparams.dim, hparams.mlp_hidden_dim, num_layers=hparams.projection_mlp_layers, normalization=get_mlp_normalization(hparams), weight_standardization=hparams.use_mlp_weight_standardization, ) self.prediction_model = utils.MLP( hparams.dim, hparams.dim, hparams.mlp_hidden_dim, num_layers=hparams.prediction_mlp_layers, normalization=get_mlp_normalization(hparams, prediction=True), weight_standardization=hparams.use_mlp_weight_standardization, ) if hparams.use_lagging_model: # "key" function (no grad) self.lagging_projection_model = copy.deepcopy( self.projection_model) for param in self.lagging_projection_model.parameters(): param.requires_grad = False else: self.lagging_projection_model = None # this classifier is used to compute representation quality each epoch self.sklearn_classifier = LogisticRegression(max_iter=100, solver="liblinear") if hparams.use_negative_examples_from_queue: # create the queue self.register_buffer("queue", torch.randn(hparams.dim, hparams.K)) self.queue = torch.nn.functional.normalize(self.queue, dim=0) self.register_buffer("queue_ptr", torch.zeros(1, dtype=torch.long)) else: self.queue = None
from utils import LSHUtils, get_encoder from pprint import pprint import numpy as np from argparse import ArgumentParser parameters = ArgumentParser() parameters.add_argument("--encoder", type=str, default="bert") parameters.add_argument("--id", type=str, required=True) parameters.add_argument("--debug", type=str, required=False) args = vars(parameters.parse_args()) encoder_model = get_encoder(args["encoder"]) lsh_id = args["id"] debug = args["debug"] lsh_utils = LSHUtils(lsh_id) encoder = encoder_model() query = input("QUERY>>>") output = lsh_utils.get_support_set(query, encoder) # pprint(output) print(len(output)) if debug: test_element = input("TEST_ELEMENT>>>") pprint([(idx, i) for idx, i in enumerate(output) if test_element in i]) output_2 = np.matmul(encoder.embed([query]), np.array(encoder.embed(output)).T)
args = ArgumentParser() args.add_argument("--host", default="0.0.0.0", type=str) args.add_argument("--port", default=8000, type=int) args.add_argument("--lsh_id", required=True, type=str) args.add_argument("--encoder", default="use", type=str) parsed = vars(args.parse_args()) hostname = parsed["host"] port = parsed["port"] lsh_id = parsed["lsh_id"] encoder = get_encoder(parsed["encoder"])() lsh_utils = LSHUtils(lsh_id) @app.route("/get_support", methods=["POST"]) def get_support_set(): data = request.get_json() query = data["query"] return lsh_utils.get_support_set(query, encoder) @app.route("/get_support_multiple", methods=["POST"]) def get_support_set(): data = request.get_json() query = data["query"]
def __init__(self, config): super(Net, self).__init__() self.encoder_type = config["encoder_type"] self.decoder = Decoder(layer_channels[self.encoder_type]) self.encoder = get_encoder(config) self.get_skip_layer()