Skip to content
TheChatArchiveThe Chat Archive
Coherecommand-a-03-2025· CohereHugging Face LMArena Human Preference 140K

i want just load images, not links

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Qwenqwen3-235b-a22b’s answer.
  1. User

    i want just load images, not links

  2. Cohere Assistant

    If you want to directly load images into memory instead of creating soft links, you can modify the script to load the images using a library like PIL (Pillow) and then proceed with creating embeddings. Below is the updated script that loads images directly from a directory, processes them, and then creates embeddings using lightly_train.embed.

    import lightly_train
    import pandas as pd
    from tqdm import tqdm
    from pathlib import Path
    from PIL import Image
    import torch
    from torch.utils.data import Dataset, DataLoader
    import torchvision.transforms as transforms
    
    class ImageDataset(Dataset):
        def __init__(self, image_paths, transform=None):
            self.image_paths = image_paths
            self.transform = transform
    
        def __len__(self):
            return len(self.image_paths)
    
        def __getitem__(self, idx):
            img_path = self.image_paths[idx]
            img = Image.open(img_path).convert("RGB")
            if self.transform:
                img = self.transform(img)
            return img, img_path
    
    def load_images_from_dir(directory):
        """Loads image paths from a given directory and returns a DataFrame."""
        directory = Path(directory)
        image_paths = list(directory.glob('*.*'))  # Adjust the glob pattern as needed
        df = pd.DataFrame({'path': [str(path) for path in image_paths]})
        return df
    
    if __name__ == "__main__":
        # Specify the directory containing your images
        image_dir = '/path/to/your/images'
        
        # Load image paths from the directory
        df = load_images_from_dir(image_dir)
        
        # Optionally, sample a subset of images
        df = df.sample(min(10000, len(df)))  # Sample up to 10,000 images
        
        # Define transformations for the images (if needed)
        transform = transforms.Compose([
            transforms.Resize((256, 256)),  # Example resize
            transforms.ToTensor(),
        ])
        
        # Create a dataset and dataloader
        dataset = ImageDataset(df['path'].tolist(), transform=transform)
        dataloader = DataLoader(dataset, batch_size=32, shuffle=False, num_workers=4)
        
        # Prepare lists to store embeddings and paths
        embeddings = []
        paths = []
        
        # Extract embeddings using lightly_train.embed
        device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
        model = lightly_train.get_model(checkpoint="last.ckpt", format="torch").to(device)
        model.eval()
        
        with torch.no_grad():
            for batch in tqdm(dataloader, desc="Extracting embeddings"):
                images, batch_paths = batch
                images = images.to(device)
                batch_embeddings = model.embed(images)
                embeddings.append(batch_embeddings.cpu())
                paths.extend(batch_paths)
        
        # Concatenate embeddings and save them along with paths
        embeddings = torch.cat(embeddings, dim=0)
        embeddings_df = pd.DataFrame({'path': paths, 'embedding': embeddings.numpy().tolist()})
        
        # Save embeddings to a file (e.g., PyTorch .pt file or CSV)
        torch.save({'embeddings': embeddings, 'paths': paths}, "exported_images_embeddings.pt")
        
        # Alternatively, save as CSV (embeddings will be saved as lists)
        embeddings_df.to_csv("exported_images_embeddings.csv", index=False)
    

    Key Changes:

    1. Image Loading: The script now loads images directly into memory using PIL.Image and applies optional transformations (e.g., resizing, normalization).
    2. Dataset and DataLoader: A custom ImageDataset class is used to handle image loading and preprocessing, and a DataLoader is used for batch processing.
    3. Embedding Extraction: The script extracts embeddings using the lightly_train model and stores them along with their corresponding paths.
    4. Saving Embeddings: Embeddings are saved in a .pt file (PyTorch format) or as a CSV file, depending on your preference.

    Replace '/path/to/your/images' with the actual path to your image directory. Adjust the transformations and other parameters as needed for your specific use case.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.