Code Monkey home page Code Monkey logo

Comments (1)

Lee-Gihun avatar Lee-Gihun commented on September 26, 2024

Since we didn't use a KNN classifier, I believe you're referring to our K-means approach. Unfortunately, we don't have official code for this. However, it can be easily implemented in just a few lines. Let me provide the following code, which is nearly identical to our original approach. The only difference is that we ran it multiple times with different encoder checkpoints and seeds to achieve the best results during the competition period.

The result is as follows:

d50780de-63a2-41da-ba85-2d6e2cf470d0

import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
from train_tools.models import MEDIARFormer
from train_tools.data_utils.datasetter import get_dataloaders_labeled

# Load data and model
dataloaders = get_dataloaders_labeled("/home/gihun/MEDIAR", "./train_tools/data_utils/mapping_labeled.json", "./train_tools/data_utils/mapping_tuning.json")
model = MEDIARFormer().to("cuda:7").eval()
model.load_state_dict(torch.load("./weights/pretrained/phase1.pth", map_location="cpu"))

# Extract and process embeddings
embeddings_all = []
for batch in dataloaders["train"]:
    with torch.no_grad():
        embeddings = model.encoder(batch["img"].to("cuda:7"))[-1].mean(dim=[2, 3]).cpu()
        embeddings_all.append(embeddings)
embeddings_all = torch.cat(embeddings_all, dim=0)

# Cluster embeddings
kmeans = KMeans(n_clusters=40, random_state=0).fit(embeddings_all.numpy())
labels, counts = np.unique(kmeans.labels_, return_counts=True)
sorted_indices = np.argsort(counts)[::-1]

# Plot histogram
plt.figure(figsize=(10, 6))
plt.bar(range(len(counts)), counts[sorted_indices], tick_label=labels[sorted_indices])
plt.xlabel('Cluster Label')
plt.ylabel('Count')
plt.title('KMeans Labels Histogram (Desc. Count)')
plt.show()```

from mediar.

Related Issues (19)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.