CoolFace
Modelpublic

LeroyDyer/SpydazWeb_VisonEncoderDecoder_Project

sourceHugging Faceapache-2.0updated 2y agoView on Hugging Face
1likes37downloads
README.md207 linesDownload Raw Back to root
1---2library_name: transformers3license: apache-2.04language:5- en6tags:7- image-text-to-text8- text-to-text9- image-text-to-image-text10pipeline_tag: image-text-to-text11BaseModel:12- Mixtral_AI_Cyber_Matrix_2.0(7b) 13Decoder: 14- Locutusque/TinyMistral-248M-v215ImageProcessor:16- ikim-uk-essen/BiomedCLIP_ViT_patch16_22417- Lin-Chen/ShareGPT4V-7B_Pretrained_vit-large336-l1218Encoder:19- google/vit-base-patch16-224-in21k20---21 22# LeroyDyer/Mixtral_AI_Cyber_Q_Vision23<img src="https://cdn-avatars.huggingface.co/v1/production/uploads/65d883893a52cd9bcd8ab7cf/tRsCJlHNZo1D02kBTmfy9.jpeg" width="300"/>24https://github.com/spydaz25 26VisionEncoderDecoderModel is a generic model class that will be instantiated as a transformer architecture27with one of the base vision model classes of the library as encoder and another one as decoder28when created with the :29 30```python31#  class method for the encoder and :32transformers.AutoModel.from_pretrained33# class method for the decoder.34transformers.AutoModelForCausalLM.from_pretrained 35 36 37```38 39### Model Description40This is an experiment in vision - the model has been created as a mistral/VisionEncoder/Decoder 41 42Customized from: 43 44```yaml45 46BaseModel:47- Mixtral_AI_Cyber_Matrix_2.0(7b) 48Decoder: 49- Locutusque/TinyMistral-248M-v250ImageProcessor:51- ikim-uk-essen/BiomedCLIP_ViT_patch16_22452- Lin-Chen/ShareGPT4V-7B_Pretrained_vit-large336-l1253Encoder:54- google/vit-base-patch16-224-in21k55 56 57 58```59 60- **Developed by:** [LeroyDyer]61- **Model type:** [image-text-to-image-text]62- **Language(s) (NLP):** [English]63 64 65## Summary66 67This is the model card of a 🤗 transformers model that has been pushed on the Hub. 68Previous vision models have been 50/50 as the multimodel model actully requires a lot of memory and gpu and harddrive space to create;69the past versions have been attempts to Merge the capabilitys into the main mistral model whilst still retaining its mistral tag!70After reading many hugging face articles: 71 72The BackBone Issue is the main cause of creating multi modals !:73 74with the advent of tiny models we are able to leverage the decoder abilitys as a single expert-ish... within the model :75by reducing the size to a fully trainined tiny model!76this will only produce decodings and not conversations so it needs to be smart and respond with defined answers: but in general it will produce captions: but as domain based it may be specialized in medical or art etc:77 78The main llm still needs to retain these models within hence the back bone method of instigating a VisionEncoderDecoder model: istead of a llava model which still need wrangling to work correctly without spoiling the original transformers installation:79Previous experiments proved that the mistral large model could be used as a decoder but the total model jumped to 13b so the when applying the tiny model it was only effected by the weight of the model 248M80 81 82 83 84## How to Get Started with the Model85 86 87### VisionEncoderDecoderModel88#### As a vision encoder model : 89 90the tensors are combined into the original mistral model so it can be accessed by intaciating the correct model which is the VisionEncoderDecoderModel91 92 93```python94from transformers import AutoProcessor, VisionEncoderDecoderModel95import requests96from PIL import Image97import torch98 99processor = AutoProcessor.from_pretrained("LeroyDyer/Mixtral_AI_Cyber_Q_Vision")100model = VisionEncoderDecoderModel.from_pretrained("LeroyDyer/Mixtral_AI_Cyber_Q_Vision")101 102# load image from the IAM dataset103url = "https://fki.tic.heia-fr.ch/static/img/a01-122-02.jpg"104image = Image.open(requests.get(url, stream=True).raw).convert("RGB")105 106# training107model.config.decoder_start_token_id = processor.tokenizer.eos_token_id108model.config.pad_token_id = processor.tokenizer.pad_token_id109model.config.vocab_size = model.config.decoder.vocab_size110 111pixel_values = processor(image, return_tensors="pt").pixel_values112text = "hello world"113labels = processor.tokenizer(text, return_tensors="pt").input_ids114outputs = model(pixel_values=pixel_values, labels=labels)115loss = outputs.loss116 117# inference (generation)118generated_ids = model.generate(pixel_values)119generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]120```121### As a standard LLM:122 123it can still also be used as a normal AutoModelForCausalLM or MistralModelForCausalLM !124 125[More Information Needed]126 127## Training Details128 129Currently inputs are raw and untrained ; 130 131ie: they NEED to be trained as the tensors are randomize maybe? 132despite using pretrained starting blocks. the encoder decoder modules are ready to be placed in train mode: 133The main model ie the LLM will need lora/Qlora/Peft etc: 134 135This model will stay in this state as a base training point ! so later versions will be trained; 136This model is fully usable and still expected to score well ;137 138The small tiny mistral is also a great performer and a great block to begin a smaller experts model (later) or any multimodal project ie: its like a mini pretrined bert/llama(Mistral is a clone of llamaAlpaca!139 140```python141 142 143from transformers import ViTImageProcessor, AutoTokenizer, VisionEncoderDecoderModel144from datasets import load_dataset145 146image_processor = ViTImageProcessor.from_pretrained("LeroyDyer/Mixtral_AI_Cyber_Q_Vision")147tokenizer = AutoTokenizer.from_pretrained("LeroyDyer/Mixtral_AI_Cyber_Q_Vision")148model = VisionEncoderDecoderModel.from_encoder_decoder_pretrained(149    "LeroyDyer/Mixtral_AI_Cyber_Q_Vision", "LeroyDyer/Mixtral_AI_Cyber_Q_Vision"150)151 152model.config.decoder_start_token_id = tokenizer.cls_token_id153model.config.pad_token_id = tokenizer.pad_token_id154 155dataset = load_dataset("huggingface/cats-image")156image = dataset["test"]["image"][0]157pixel_values = image_processor(image, return_tensors="pt").pixel_values158 159labels = tokenizer(160    "an image of two cats chilling on a couch",161    return_tensors="pt",162).input_ids163 164# the forward function automatically creates the correct decoder_input_ids165loss = model(pixel_values=pixel_values, labels=labels).loss166 167 168 169```170 171 172### Model Architecture 173 174 175Aha !!! Here is how you create such a model :: 176 177 178 179``` python180 181from transformers import MistralConfig, ViTConfig, VisionEncoderDecoderConfig, VisionEncoderDecoderModel182 183# Initializing a ViT & Mistral style configuration184config_encoder = ViTConfig()185config_decoder = MistralConfig()186 187config = VisionEncoderDecoderConfig.from_encoder_decoder_configs(config_encoder, config_decoder)188 189# Initializing a ViTMistral model (with random weights) from a ViT & Mistral style configurations190model = VisionEncoderDecoderModel(config=config)191 192# Accessing the model configuration193config_encoder = model.config.encoder194config_decoder = model.config.decoder195# set decoder config to causal lm196config_decoder.is_decoder = True197config_decoder.add_cross_attention = True198 199# Saving the model, including its configuration200model.save_pretrained("my-model")201 202# loading model and config from pretrained folder203encoder_decoder_config = VisionEncoderDecoderConfig.from_pretrained("my-model")204model = VisionEncoderDecoderModel.from_pretrained("my-model", config=encoder_decoder_config)205 206 207```