snaramirez872 commited on
Commit
dc282ed
·
1 Parent(s): 727eb93

added files

Browse files
.gitattributes CHANGED
@@ -32,3 +32,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
32
  *.zip filter=lfs diff=lfs merge=lfs -text
33
  *.zst filter=lfs diff=lfs merge=lfs -text
34
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
32
  *.zip filter=lfs diff=lfs merge=lfs -text
33
  *.zst filter=lfs diff=lfs merge=lfs -text
34
  *tfevents* filter=lfs diff=lfs merge=lfs -text
35
+ datasets/lfw.tar filter=lfs diff=lfs merge=lfs -text
app.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import io
2
+ import tarfile
3
+ import numpy as np
4
+ import random as RAND
5
+ import torch
6
+ import torchvision.transforms as TRNSFM
7
+ import torchvision.models as MDLS
8
+ from PIL import Image as IMG
9
+ from scipy.spatial.distance import cosine
10
+ import streamlit as st
11
+
12
+ def similar(image): # Function for Streamlit App
13
+ pict = form(IMG.open(image).convert('RGB'))
14
+ pictFeats = mod(pict.unsqueeze(0)).detach().numpy().squeeze()
15
+
16
+ for na, feat in feats.items():
17
+ s = 1 - cosine(pictFeats, feat)
18
+ simScores.append((na, s))
19
+
20
+ simScores.sort(key=lambda x: x[1], reverse=True)
21
+
22
+ st.write("### Selected Image")
23
+ test = IMG.open(image)
24
+ test.show()
25
+ print('\n')
26
+
27
+ # 10 Most Similar Images from Dictionary
28
+ st.write("### 10 Most Similar Images")
29
+ for na in simScores[:10]:
30
+ for x in range(10):
31
+ st.write(f"### {x}")
32
+
33
+ new_na = na[:3] + "images/" + na[3:]
34
+ new_path = "http://vis-www.cs.umass.edu/" + new_na
35
+ simImages = IMG.open(new_path)
36
+ simImages.show()
37
+
38
+ mod = MDLS.resnet50(pretrained=True)
39
+ mod.eval()
40
+ mod = torch.nn.Sequential(*list(mod.children())[:-1])
41
+ inFile = tarfile.open('/datasets/lfw.tar', 'r')
42
+
43
+ feats = {}
44
+ simScores = [] # Similarity Scores for Later
45
+
46
+ form = TRNSFM.Compose([
47
+ TRNSFM.Resize(256),
48
+ TRNSFM.CenterCrop(224),
49
+ TRNSFM.ToTensor(),
50
+ TRNSFM.Normalize(mean=[0.485,0.456,0.406], std=[0.229,0.224,0.225])
51
+ ]) # Image Pre-processing
52
+
53
+ stuffs = inFile.getmembers()
54
+
55
+ for stuff in stuffs: # Going through the TAR file
56
+ f = inFile.extractfile(stuff)
57
+
58
+ if stuff.isdir():
59
+ continue
60
+
61
+ if stuff.name.lower().endswith(('.jpg', '.jpeg', '.png')):
62
+ n = stuff.name
63
+ pic = form(IMG.open(io.BytesIO(f.read())).convert('RGB')) # Pre-processes the image before feeding it into the model
64
+
65
+ feats[n] = mod(pic.unsqueeze(0)).detach().numpy().squeeze()
66
+
67
+ # Stuff for App
68
+ st.title("Similar Image Finder")
69
+
70
+ upload = st.file_uploader("Upload an Image...", type=['.jpg', '.jpeg', '.png'])
71
+
72
+ if upload is not None:
73
+ similar(upload)
74
+
75
+ st.write("## OR")
76
+
77
+ # Random Image Selector from 5 Pictures
78
+ randImages = [
79
+ '/datasets/random-images/img1.jpg',
80
+ '/datasets/random-images/img2.jpg',
81
+ '/datasets/random-images/img3.jpg',
82
+ '/datasets/random-images/img4.jpg',
83
+ '/datasets/random-images/img5.jpg'
84
+ ]
85
+
86
+ if st.button("Surprise Me!"): # Button
87
+ imageOptOne = RAND.choice(randImages)
88
+ similar(imageOptOne)
89
+
90
+ inFile.close()
datasets/lfw.tar ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:9e7dd83504865735bc0c48fef4f803a337136fcf5ce14cbf6b32e6287ed95531
3
+ size 201461760
datasets/random-images/img1.jpg ADDED
datasets/random-images/img2.jpg ADDED
datasets/random-images/img3.jpg ADDED
datasets/random-images/img4.jpg ADDED
datasets/random-images/img5.jpg ADDED
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ io
2
+ tarfile
3
+ numpy
4
+ random
5
+ torch
6
+ torchvision
7
+ PIL
8
+ scipy