Update README.md
Browse files3 unmodified lines456789101112131415161718192021222324252627282930313233343536373 unmodified linesThis is the structure of the BLIPNet model. You can load the model with it, or you can create a bigger model for your task.class BLIPNet(torch.nn.Module):def __init__(self, ):super().__init__()#Generation Modelself.model = BlipForConditionalGeneration.from_pretrained(MODEL_NAME, cache_dir="model")#Same with https://huggingface.co/uf-aice-lab/BLIP-Mathself.ebd_dim = ebd_dim= 443136#Classification Modelfc_dim = 64 # You can choose a higher number for better performance, for example, 1024.self.head = nn.Sequential(nn.Linear(self.ebd_dim, fc_dim),nn.ReLU(),)self.score = nn.Linear(fc_dim, 5) #5 classesdef forward(self, pixel_values, input_ids):outputs = self.model(input_ids=input_ids, pixel_values=pixel_values, labels=input_ids)image_text_embeds = self.model.vision_model(pixel_values, return_dict=True).last_hidden_stateimage_text_embeds = self.head(image_embeds.view(-1, self.ebd_dim))#A classification model is based on embeddings from a generative model to leverage BLIP's powerful image-text encoding capabilities.logits = self.score(image_embeds)#generated text, probabilities of classificationreturn outputs, logitsYou need to input the sample in the same way as:https://huggingface.co/uf-aice-lab/BLIP-MathThen you can get the text and score at the same time.3 unmodified lines456789101112131415161718192021222324252627282930313233343536373839403 unmodified linesThis is the structure of the BLIPNet model. You can load the model with it, or you can create a bigger model for your task.class BLIPNet(torch.nn.Module):def __init__(self, ):super().__init__()#Generation Modelself.model = BlipForConditionalGeneration.from_pretrained(MODEL_NAME, cache_dir="model")#Same with https://huggingface.co/uf-aice-lab/BLIP-Mathself.ebd_dim = ebd_dim= 443136#Classification Modelfc_dim = 64 # You can choose a higher number for better performance, for example, 1024.self.head = nn.Sequential(nn.Linear(self.ebd_dim, fc_dim),nn.ReLU(),)self.score = nn.Linear(fc_dim, 5) #5 classesdef forward(self, pixel_values, input_ids):outputs = self.model(input_ids=input_ids, pixel_values=pixel_values, labels=input_ids)image_text_embeds = self.model.vision_model(pixel_values, return_dict=True).last_hidden_stateimage_text_embeds = self.head(image_embeds.view(-1, self.ebd_dim))#A classification model is based on embeddings from a generative model to leverage BLIP's powerful image-text encoding capabilities.logits = self.score(image_embeds)#generated text, probabilities of classificationreturn outputs, logitsmodel = BLIPNet()model.load_state_dict(torch.load(best_model_wts_path) ,strict=False)You need to input the sample in the same way as:https://huggingface.co/uf-aice-lab/BLIP-MathThen you can get the text and score at the same time.
README.md+30-27