Commit 1af71c2e authored by Meet Narendra's avatar Meet Narendra 💬

saswat call

parent fd16c581
......@@ -2,3 +2,4 @@
*Logs*
*word_vectors_cache*
*dataset*
*testfiles*
import torch
from logger import Logger
LOGGER = Logger().logger()
LOGGER.info("Running the model cuda_available = "+str(torch.cuda.is_available()))
from feature_maps import FeatureMaps
from word_vectors import WordVectors
from preprocess import Preprocessor
from Utils import device
#Author: @meetdoshi
class Embeddings():
def __init__(self) -> None:
self.word_vectors = WordVectors()
self.feature_maps = FeatureMaps()
return
def get_embeddings(self,word,img):
'''
Function to get embeddings for a word
@params
word: str
img: numpy image f64
'''
word_vector = self.word_vectors.get_vector(word)
feature_maps = self.feature_maps.get_fmaps_content(img)
embedding = torch.cat((word_vector,feature_maps),dim=0)
return embedding
if __name__ == "__main__":
img = Preprocessor().process('./testfiles/style.jpg')
emb = Embeddings()
print(emb.get_embeddings('hello',img).shape)
\ No newline at end of file
......@@ -2,6 +2,7 @@ import torch
from logger import Logger
LOGGER = Logger().logger()
LOGGER.info("Running the model cuda_available = "+str(torch.cuda.is_available()))
from Utils import device
#Author: @meetdoshi
class FeatureMaps():
def __init__(self,arch="vgg19"):
......@@ -13,6 +14,7 @@ class FeatureMaps():
super()
try:
self.model = torch.hub.load('pytorch/vision:v0.10.0',arch,pretrained=True)
self.model = self.model.to(device)
except Exception as e:
LOGGER.error("Could not load model" + str(e))
return
......
......@@ -6,10 +6,53 @@ from PIL import Image
import numpy as np
LOGGER = Logger().logger()
#Author: @meetdoshi
from Utils import device
class Preprocessor:
def __init__(self) -> None:
pass
@staticmethod
def load_image(path):
'''
Function to load image
@params
path: os.path
'''
img = Image.open(path)
return img
@staticmethod
def subtract_mean(img):
'''
Function to subtract mean values of RGB channels computed over whole ImageNet dataset
@params
img: 3d numpy array
'''
mean = np.reshape([103.939, 116.779, 123.68],(1,1,3))#b,g,r
return img-mean
@staticmethod
def reshape_img(img):
'''
Function to reshpae image in 224x224xnum_of_channels shape
@params
img: 3d numpy array
'''
#loader = transforms.Compose([transforms.ToTensor(),transforms.Resize([224,224]),transforms.Normalize(mean=[0.485, 0.456, 0.406],std=[0.229, 0.224, 0.225],),])
loader = transforms.Compose([transforms.ToTensor(),transforms.Resize([224,224])])
img = loader(img).unsqueeze(0)
#assert img.shape == (1,3,224,224)
return img.to(device,torch.float)
@staticmethod
def process(path):
'''
Function to preprocess the image
@params
path: os.path
'''
img = Preprocessor.load_image(path)
img = Preprocessor.reshape_img(img)
#img = Preprocessor.subtract_mean(img)
return img
'''
if __name__=="__main__":
......
......@@ -2,6 +2,7 @@ import torch
from logger import Logger
LOGGER = Logger().logger()
from torchnlp.word_to_vector import GloVe, FastText, BPEmb, CharNGram
from Utils import device
#Author: @meetdoshi
class WordVectors():
def __init__(self,model='fasttext'):
......@@ -41,7 +42,7 @@ class WordVectors():
LOGGER.error("Model not found")
return self.vectors[word]
'''
if __name__ == "__main__":
wv = WordVectors()
print(wv.get_vector('ababasba').shape)
......@@ -51,3 +52,4 @@ if __name__ == "__main__":
print(wv.get_vector('hbasbasbsello').shape)
wv = WordVectors(model='chargram')
print(wv.get_vector('hebasallo').shape)
'''
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment