def init_network(params): # parse params with default values architecture = params.get('architecture', 'resnet101') local_whitening = params.get('local_whitening', False) pooling = params.get('pooling', 'gem') regional = params.get('regional', False) whitening = params.get('whitening', False) mean = params.get('mean', [0.485, 0.456, 0.406]) std = params.get('std', [0.229, 0.224, 0.225]) pretrained = params.get('pretrained', True) # get output dimensionality size dim = OUTPUT_DIM[architecture] # loading network from torchvision if pretrained: if architecture not in FEATURES: # initialize with network pretrained on imagenet in pytorch net_in = getattr(torchvision.models, architecture)(pretrained=True) else: # initialize with random weights, later on we will fill features with custom pretrained network net_in = getattr(torchvision.models, architecture)(pretrained=False) else: # initialize with random weights net_in = getattr(torchvision.models, architecture)(pretrained=False) # initialize features # take only convolutions for features, # always ends with ReLU to make last activations non-negative if architecture.startswith('alexnet'): features = list(net_in.features.children())[:-1] elif architecture.startswith('vgg'): features = list(net_in.features.children())[:-1] elif architecture.startswith('resnet'): features = list(net_in.children())[:-2] elif architecture.startswith('densenet'): features = list(net_in.features.children()) features.append(nn.ReLU(inplace=True)) elif architecture.startswith('squeezenet'): features = list(net_in.features.children()) else: raise ValueError( 'Unsupported or unknown architecture: {}!'.format(architecture)) # initialize local whitening if local_whitening: lwhiten = nn.Linear(dim, dim, bias=True) # TODO: lwhiten with possible dimensionality reduce if pretrained: lw = architecture if lw in L_WHITENING: print( ">> {}: for '{}' custom computed local whitening '{}' is used" .format(os.path.basename(__file__), lw, os.path.basename(L_WHITENING[lw]))) whiten_dir = os.path.join(get_data_root(), 'whiten') lwhiten.load_state_dict( model_zoo.load_url(L_WHITENING[lw], model_dir=whiten_dir)) else: print( ">> {}: for '{}' there is no local whitening computed, random weights are used" .format(os.path.basename(__file__), lw)) else: lwhiten = None # initialize pooling if pooling == 'gemmp': pool = POOLING[pooling](mp=dim) else: pool = POOLING[pooling]() # initialize regional pooling if regional: rpool = pool rwhiten = nn.Linear(dim, dim, bias=True) # TODO: rwhiten with possible dimensionality reduce if pretrained: rw = '{}-{}-r'.format(architecture, pooling) if rw in R_WHITENING: print( ">> {}: for '{}' custom computed regional whitening '{}' is used" .format(os.path.basename(__file__), rw, os.path.basename(R_WHITENING[rw]))) whiten_dir = os.path.join(get_data_root(), 'whiten') rwhiten.load_state_dict( model_zoo.load_url(R_WHITENING[rw], model_dir=whiten_dir)) else: print( ">> {}: for '{}' there is no regional whitening computed, random weights are used" .format(os.path.basename(__file__), rw)) pool = Rpool(rpool, rwhiten) # initialize whitening if whitening: whiten = nn.Linear(dim, dim, bias=True) # TODO: whiten with possible dimensionality reduce if pretrained: w = architecture if local_whitening: w += '-lw' w += '-' + pooling if regional: w += '-r' if w in WHITENING: print(">> {}: for '{}' custom computed whitening '{}' is used". format(os.path.basename(__file__), w, os.path.basename(WHITENING[w]))) whiten_dir = os.path.join(get_data_root(), 'whiten') whiten.load_state_dict( model_zoo.load_url(WHITENING[w], model_dir=whiten_dir)) else: print( ">> {}: for '{}' there is no whitening computed, random weights are used" .format(os.path.basename(__file__), w)) else: whiten = None # create meta information to be stored in the network meta = { 'architecture': architecture, 'local_whitening': local_whitening, 'pooling': pooling, 'regional': regional, 'whitening': whitening, 'mean': mean, 'std': std, 'outputdim': dim, } # create a generic image retrieval network net = ImageRetrievalNet(features, lwhiten, pool, whiten, meta) # initialize features with custom pretrained network if needed if pretrained and architecture in FEATURES: print( ">> {}: for '{}' custom pretrained features '{}' are used".format( os.path.basename(__file__), architecture, os.path.basename(FEATURES[architecture]))) model_dir = os.path.join(get_data_root(), 'networks') net.features.load_state_dict( model_zoo.load_url(FEATURES[architecture], model_dir=model_dir)) return net