panda_gpt.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import sys
  2. import os
  3. sys.path.insert(0, os.path.dirname(__file__))
  4. from embd_input import MyModel
  5. import numpy as np
  6. from torch import nn
  7. import torch
  8. # use PandaGPT path
  9. panda_gpt_path = os.path.join(os.path.dirname(__file__), "PandaGPT")
  10. imagebind_ckpt_path = "./models/panda_gpt/"
  11. sys.path.insert(0, os.path.join(panda_gpt_path,"code","model"))
  12. from ImageBind.models import imagebind_model
  13. from ImageBind import data
  14. ModalityType = imagebind_model.ModalityType
  15. max_tgt_len = 400
  16. class PandaGPT:
  17. def __init__(self, args):
  18. self.visual_encoder,_ = imagebind_model.imagebind_huge(pretrained=True, store_path=imagebind_ckpt_path)
  19. self.visual_encoder.eval()
  20. self.llama_proj = nn.Linear(1024, 5120) # self.visual_hidden_size, 5120)
  21. self.max_tgt_len = max_tgt_len
  22. self.model = MyModel(["main", *args])
  23. self.generated_text = ""
  24. self.device = "cpu"
  25. def load_projection(self, path):
  26. state = torch.load(path, map_location="cpu")
  27. self.llama_proj.load_state_dict({
  28. "weight": state["llama_proj.weight"],
  29. "bias": state["llama_proj.bias"]})
  30. def eval_inputs(self, inputs):
  31. self.model.eval_string("<Img>")
  32. embds = self.extract_multimoal_feature(inputs)
  33. for i in embds:
  34. self.model.eval_float(i.T)
  35. self.model.eval_string("</Img> ")
  36. def chat(self, question):
  37. return self.chat_with_image(None, question)
  38. def chat_with_image(self, inputs, question):
  39. if self.generated_text == "":
  40. self.model.eval_string("###")
  41. self.model.eval_string(" Human: ")
  42. if inputs:
  43. self.eval_inputs(inputs)
  44. self.model.eval_string(question)
  45. self.model.eval_string("\n### Assistant:")
  46. ret = self.model.generate_with_print(end="###")
  47. self.generated_text += ret
  48. return ret
  49. def extract_multimoal_feature(self, inputs):
  50. features = []
  51. for key in ["image", "audio", "video", "thermal"]:
  52. if key + "_paths" in inputs:
  53. embeds = self.encode_data(key, inputs[key+"_paths"])
  54. features.append(embeds)
  55. return features
  56. def encode_data(self, data_type, data_paths):
  57. type_map = {
  58. "image": ModalityType.VISION,
  59. "audio": ModalityType.AUDIO,
  60. "video": ModalityType.VISION,
  61. "thermal": ModalityType.THERMAL,
  62. }
  63. load_map = {
  64. "image": data.load_and_transform_vision_data,
  65. "audio": data.load_and_transform_audio_data,
  66. "video": data.load_and_transform_video_data,
  67. "thermal": data.load_and_transform_thermal_data
  68. }
  69. load_function = load_map[data_type]
  70. key = type_map[data_type]
  71. inputs = {key: load_function(data_paths, self.device)}
  72. with torch.no_grad():
  73. embeddings = self.visual_encoder(inputs)
  74. embeds = embeddings[key]
  75. embeds = self.llama_proj(embeds).cpu().numpy()
  76. return embeds
  77. if __name__=="__main__":
  78. a = PandaGPT(["--model", "./models/ggml-vicuna-13b-v0-q4_1.bin", "-c", "2048", "--lora", "./models/panda_gpt/ggml-adapter-model.bin","--temp", "0"])
  79. a.load_projection("./models/panda_gpt/adapter_model.bin")
  80. a.chat_with_image(
  81. {"image_paths": ["./media/llama1-logo.png"]},
  82. "what is the text in the picture? 'llama' or 'lambda'?")
  83. a.chat("what is the color of it?")