{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":85540,"databundleVersionId":9725394,"sourceType":"competition"}],"dockerImageVersionId":30787,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## My small secret: \n**You can actually generate prompts from images and use this to make features!**","metadata":{}},{"cell_type":"code","source":"import os\nimport torch\nfrom PIL import Image\nfrom transformers import Blip2ForConditionalGeneration, AutoProcessor\nimport pandas as pd\n\n# Define the image directory\nimage_dir = '/kaggle/input/applications-of-deep-learning-wustl-fall-2024/SimPictures/SimPictures/'\noutput_file = 'image_prompts.csv'\n\n# Initialize the BLIP-2 model and processor\nmodel_name = 'Salesforce/blip2-opt-6.7b'  # Pre-trained BLIP-2 model\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")  # Use GPU if available\nmodel = Blip2ForConditionalGeneration.from_pretrained(model_name, torch_dtype=torch.float16).to(device)\nprocessor = AutoProcessor.from_pretrained(model_name)\n\n# Function to generate prompt from an image\ndef generate_prompt(image_path):\n    image = Image.open(image_path)\n    inputs = processor(images=image, return_tensors=\"pt\").to(device)  # Move inputs to correct device\n    with torch.no_grad():\n        prompt = model.generate(**inputs)\n    # Decode and return the prompt\n    decoded_prompt = processor.decode(prompt[0], skip_special_tokens=True)\n    return decoded_prompt\n\n# Set batch size and process images in batches\nbatch_size = 3  # Adjust batch size as needed\nimage_files = [f for f in os.listdir(image_dir) if f.endswith('.png')]\nprompts = []\n\n# Process images in batches\nfor i in range(0, len(image_files), batch_size):\n    batch_files = image_files[i:i + batch_size]\n    batch_prompts = []\n    for image_file in batch_files:\n        home_id = image_file.split('_')[0]  # Extract home ID from the file name\n        image_path = os.path.join(image_dir, image_file)\n        prompt = generate_prompt(image_path)\n        batch_prompts.append({'homeid': home_id, 'prompt': prompt})\n    \n    # Append batch results to the main list\n    prompts.extend(batch_prompts)\n    \n    # Clear memory after each batch\n    torch.cuda.empty_cache()\n\n# Save the prompts to a CSV file\nprompt_df = pd.DataFrame(prompts)\nprompt_df.to_csv(output_file, index=False)\n\nprint(f'Prompts generated and saved to {output_file}')\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]}]}