<jupyter_start><jupyter_text>Image Predictions   Create Custom Dataset<jupyter_code>class ShopeeDataset(Dataset):
    
    def __init__(self,image_path_lst,isTraining=False, transform=None):
        self.image_paths = image_path_lst
        self.transform = transform
    
    def __len__(self):
        return self.image_paths.shape[0]
    
    def __getitem__(self, idx):
        image_path = self.image_paths[idx]
        # read image convert to RGB and apply augmentation
        image = cv2.imread(image_path)
#         print(image_path)
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        if self.transform:
            aug = self.transform(image=image)
            image = aug['image']
        
        return image, torch.tensor(1)
            
def getTestAugmentation(IMG_SIZE, isTraining=False):
    
    
    return albumentations.Compose([
        albumentations.Resize(IMG_SIZE, IMG_SIZE, always_apply=True),
        albumentations.Normalize(),
        ToTensorV2(p=1.0)
    ])<jupyter_output><empty_output><jupyter_text>Build Model<jupyter_code>class ShopeeLabelGroupClassfier(nn.Module):
    
    def __init__(self,
                     model_name='tf_efficientnet_b0',
                     loss_fn='softmax',
                     classes = CFG.classes,
                     fc_dim = CFG.fc_dim,
                     pretrained=True,
                     use_fc=True,
                     isTraining=False
                ):
        
        
        super(ShopeeLabelGroupClassfier,self).__init__()
        
        # create bottlenack backbone network from pretrained model 
        self.backbone = timm.create_model(model_name, pretrained=pretrained)
        in_features = self.backbone.classifier.in_features
        # we will put FC layers over backbone to classfy images based on label groups
        self.backbone.classifier = nn.Identity()
        self.backbone.global_pool = nn.Identity()
        self.pooling = nn.AdaptiveAvgPool2d(1)
        self.use_fc = use_fc
        self.loss_fn =loss_fn
        
        # build top fc layers
        if self.use_fc:
            self.dropout = nn.Dropout(0.2)
            self.fc = nn.Linear(in_features,fc_dim )
            self.bn = nn.BatchNorm1d(fc_dim)
            in_features = fc_dim
        self.loss_fn = loss_fn
        
        if self.loss_fn=='softmax':
            self.final = nn.Linear(in_features, CFG.classes)
    
    def forward(self, image, label):
        features = self.get_features(image)
        
        if self.loss_fn=='softmax' and CFG.isTraining:
            logits = self.final(features)
            return logits
        else:
            return features
    
    def get_features(self,inp):
        batch_dim = inp.shape[0]
        inp = self.backbone(inp)
        inp = self.pooling(inp).view(batch_dim, -1)
        if self.use_fc:
            inp = self.dropout(inp)
            inp = self.fc(inp)
            inp = self.bn(inp)
        
        return inp<jupyter_output><empty_output><jupyter_text>Generate Image Embeddings<jupyter_code>def generateImageEmbeddings(images):
    model = ShopeeLabelGroupClassfier(pretrained=False).to(CFG.device)
    model.load_state_dict(torch.load(CFG.model_path))
    model.eval()
    
    # create dataset
    test_aug = getTestAugmentation(CFG.img_size, CFG.isTraining)
    test_dataset = ShopeeDataset(image_path_lst=images, isTraining=CFG.isTraining, transform = test_aug)
    
    test_data_loader = torch.utils.data.DataLoader(
                                                test_dataset,
        batch_size=CFG.batch_size,
        num_workers =CFG.num_workers
    )
    
    embeddings = []
    
    with torch.no_grad():
        
        for image, label  in (test_data_loader):
            image = image.cuda()
            label = label.cuda()
            features = model(image, label)
            image_embeddings = features.detach().cpu().numpy()
            embeddings.append(image_embeddings)
            
    del model
    image_embeddings = np.concatenate(embeddings)
    print("Final Embedding shape {}".format(image_embeddings.shape))
    
    del embeddings
    gc.collect()
    
            
    return image_embeddings<jupyter_output><empty_output><jupyter_text>get top K neighbors<jupyter_code>def get_topk_neighbors(data, embeddings, KNN=50):

    model = NearestNeighbors(n_neighbors = KNN)
    model.fit(embeddings)
    distances, indices = model.kneighbors(embeddings)
    
    threshold = 4.5
    predictions = []
    for k in tqdm(range(embeddings.shape[0])):
#         print(k)
        idx = np.where(distances[k,] < threshold)[0]
        ids = indices[k,idx]
#         print(ids)
        posting_ids = data['posting_id'].iloc[ids].values
        predictions.append(posting_ids)
        
    del model, distances, indices
    gc.collect()
    return data, predictions
image_paths, cu_df, test_df = read_test_dataset()
test_df.head()
image_embeddings = generateImageEmbeddings(image_paths.values)

data, image_predictions = get_topk_neighbors(test_df, image_embeddings, KNN=50 if len(test_df)>3 else 3)
data.head()
data['matches'] = image_predictions
data[['posting_id', 'matches']].to_csv('submission.csv', index=False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09487375669472073, 'fraction_numerical': 0.005164498852333588, 'mean_word_length': 2.345489443378119, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '416601', 'license': 'MIT', 'n_tokens_mistral': 1678, 'n_tokens_neox': 1642, 'n_words': 277, 'path': 'notebooks/ProductClassificationSoftmax[Inference].ipynb', 'repo_name': 'cr21/Shopee-Product-Matching', 'size': '5228'}