Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
CS626-Project
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Meet Narendra
CS626-Project
Commits
1af71c2e
Commit
1af71c2e
authored
Nov 06, 2022
by
Meet Narendra
💬
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
saswat call
parent
fd16c581
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
85 additions
and
4 deletions
+85
-4
.gitignore
.gitignore
+1
-0
Utils/__init__.py
Utils/__init__.py
+0
-0
embeddings.py
embeddings.py
+33
-0
feature_maps.py
feature_maps.py
+2
-0
preprocess.py
preprocess.py
+45
-2
word_vectors.py
word_vectors.py
+4
-2
No files found.
.gitignore
View file @
1af71c2e
...
...
@@ -2,3 +2,4 @@
*Logs*
*word_vectors_cache*
*dataset*
*testfiles*
__init__.py
→
Utils/
__init__.py
View file @
1af71c2e
File moved
embeddings.py
0 → 100644
View file @
1af71c2e
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
feature_maps.py
View file @
1af71c2e
...
...
@@ -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
...
...
preprocess.py
View file @
1af71c2e
...
...
@@ -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__":
...
...
word_vectors.py
View file @
1af71c2e
...
...
@@ -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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment