| import os |
| import torch |
| import dataset4eo as eodata |
| import jsonlines |
| import numpy as np |
| import pdb |
| from prompt_utils import generate_prompt_for_segmentation,\ |
| generate_color_coded_segmentation_map, get_significant_classes,\ |
| get_tableau_colors |
|
|
| from mistral_inference.transformer import Transformer |
| from mistral_inference.generate import generate |
|
|
| from mistral_common.tokens.tokenizers.mistral import MistralTokenizer |
| from mistral_common.protocol.instruct.messages import UserMessage, TextChunk, ImageChunk |
| from mistral_common.protocol.instruct.request import ChatCompletionRequest |
|
|
|
|
| def get_caption_from_id(model, dataset, class_names, id): |
| class_colors = get_tableau_colors() |
| sample = dataset[id] |
| label = sample["segmentation_map"] |
| |
| |
| |
| |
|
|
| current_classes, current_percents = get_significant_classes(label) |
| unknownId = -1 |
| current_class_names = {i:class_names[ind] for i,ind in enumerate(current_classes) if ind!=unknownId} |
| current_percents = {i:percent for i,percent in enumerate(current_percents.values())} |
|
|
| color_names = list(class_colors.keys()) |
| current_color_names = {i:color_names[i] for i,ind in enumerate(current_classes)} |
|
|
| prompt = generate_prompt_for_segmentation(current_color_names, current_class_names, current_percents) |
| label_remap = {i:ind for i,ind in enumerate(current_classes)} |
| image = generate_color_coded_segmentation_map(label, class_colors, label_remap=label_remap) |
|
|
| |
| |
| |
|
|
| completion_request = ChatCompletionRequest(messages=[UserMessage(content=[ImageChunk(image=image), TextChunk(text=prompt)])]) |
|
|
| encoded = tokenizer.encode_chat_completion(completion_request) |
|
|
| images = encoded.images |
| tokens = encoded.tokens |
|
|
| out_tokens, _ = generate([tokens], model, images=[images], max_tokens=256, temperature=0.35, eos_id=tokenizer.instruct_tokenizer.tokenizer.eos_id) |
| result = tokenizer.decode(out_tokens[0]) |
| return result |
|
|
|
|
|
|
| if __name__=="__main__": |
| import tqdm |
| import json |
| import argparse |
|
|
| parser = argparse.ArgumentParser(description="Process some integers.") |
| |
| |
| parser.add_argument('--part', type=int, required=True, help="Specify the part number as an integer.") |
| |
| |
| args = parser.parse_args() |
| |
| |
| part = args.part |
|
|
| |
| n_total = 15000 |
| n_parts = 10 |
| step = n_total // n_parts |
|
|
| partitions = { |
| i: {"start": (i - 1) * step, "end": i * step} for i in range(1, n_parts + 1) |
| } |
|
|
| partitions[1]['start']=1254 |
| index_dict = partitions[part] |
| |
| mistral_models_path = "Pixtral-12B" |
| tokenizer = MistralTokenizer.from_file(f"{mistral_models_path}/tekken.json") |
| model = Transformer.from_folder(mistral_models_path) |
|
|
| |
| dataset = eodata.StreamingDataset(input_dir="optimized_enmap_nlcd_dataset", num_channels=202, channels_to_select=[0,1,2], shuffle=False, drop_last=True) |
| class_names = { |
| 11: "Open Water", |
| 12: "Perennial Ice/Snow", |
| 21: "Developed, Open Space", |
| 22: "Developed, Low Intensity", |
| 23: "Developed, Medium Intensity", |
| 24: "Developed, High Intensity", |
| 31: "Barren Land (Rock/Sand/Clay)", |
| 41: "Deciduous Forest", |
| 42: "Evergreen Forest", |
| 43: "Mixed Forest", |
| 51: "Dwarf Scrub", |
| 52: "Shrub/Scrub", |
| 71: "Grassland/Herbaceous", |
| 72: "Sedge/Herbaceous", |
| 73: "Lichens", |
| 74: "Moss", |
| 81: "Pasture/Hay", |
| 82: "Cultivated Crops", |
| 90: "Woody Wetlands", |
| 95: "Emergent Herbaceous Wetlands" |
| } |
|
|
| class_names = {} |
|
|
| filename = f"hyper_id_text_nlcd_part{part}.jsonl" |
| for id in tqdm.tqdm(range(index_dict['start'], index_dict['end'])): |
| caption = get_caption_from_id(model, dataset, class_names, id) |
| record = {"id": id, "caption": caption} |
| with jsonlines.open(filename, mode='a') as writer: |
| writer.write(record) |
|
|
|
|