Kaveh-Workstation commited on
Commit
d19fddf
1 Parent(s): 250bb13
This view is limited to 50 files because it contains too many changes.   See raw diff
.gitattributes CHANGED
The diff for this file is too large to render. See raw diff
 
app.py ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ import pickle
4
+ import numpy as np
5
+ import pandas as pd
6
+ from transformers import CLIPProcessor, CLIPModel
7
+ from transformers import VisionTextDualEncoderModel, VisionTextDualEncoderProcessor
8
+ from sklearn.metrics.pairwise import cosine_similarity
9
+ import csv
10
+ from PIL import Image
11
+
12
+ model_path = "kaveh/rclip"
13
+ embeddings_file = './image_embeddings_8_clip14_cxrbert.pkl'
14
+ csv_path = "./captions.csv"
15
+
16
+ def load_image_ids(csv_file):
17
+ ids = []
18
+ captions = []
19
+ with open(csv_file, 'r') as f:
20
+ reader = csv.reader(f, delimiter='\t')
21
+ for row in reader:
22
+ ids.append(row[0])
23
+ captions.append(row[1])
24
+ return ids, captions
25
+
26
+ def load_embeddings(embeddings_file):
27
+ with open(embeddings_file, 'rb') as f:
28
+ image_embeddings = pickle.load(f)
29
+ return image_embeddings
30
+
31
+
32
+ def find_similar_images(query_embedding, image_embeddings, k=2):
33
+ similarities = cosine_similarity(query_embedding.reshape(1, -1), image_embeddings)
34
+ closest_indices = np.argsort(similarities[0])[::-1][:k]
35
+ scores = sorted(similarities[0])[::-1][:k]
36
+ return closest_indices, scores
37
+
38
+
39
+ def main(query, k=2):
40
+ # Load RCLIP model
41
+ model = VisionTextDualEncoderModel.from_pretrained(model_path)
42
+ processor = VisionTextDualEncoderProcessor.from_pretrained(model_path)
43
+
44
+ # Load image embeddings
45
+ image_embeddings = load_embeddings(embeddings_file)
46
+
47
+ # Embed the query
48
+ inputs = processor(text=query, images=None, return_tensors="pt", padding=True)
49
+ with torch.no_grad():
50
+ query_embedding = model.get_text_features(**inputs)[0].numpy()
51
+
52
+ # Get image names
53
+ ids, captions = load_image_ids(csv_path)
54
+
55
+ # Find similar images
56
+ similar_image_indices, scores = find_similar_images(query_embedding, image_embeddings, k=int(k))
57
+
58
+ # Return the results
59
+ similar_image_names = [f"./images/{ids[index]}.jpg" for index in similar_image_indices]
60
+ similar_image_captions = [captions[index] for index in similar_image_indices]
61
+ similar_images = [Image.open(i) for i in similar_image_names]
62
+
63
+ return similar_images, pd.DataFrame([[t+1 for t in range(k)], similar_image_names, similar_image_captions, scores], index=["#", "path", "caption", "score"]).T
64
+
65
+
66
+ # Define the Gradio interface
67
+ examples = [
68
+ ["Chest X-ray photos",5],
69
+ ["Orthopantogram (OPG)",5],
70
+ ["Brain Scan",5],
71
+ ["tomography",5]
72
+ ]
73
+
74
+ title="RCLIP Image Retrieval"
75
+ description = "CLIP model fine-tuned on the ROCO dataset"
76
+
77
+ with gr.Blocks(title=title) as demo:
78
+ with gr.Row():
79
+ with gr.Column(scale=5):
80
+ gr.Markdown("# "+title)
81
+ gr.Markdown(description)
82
+ gr.HTML(value="<img src=\"https://newresults.co.uk/wp-content/uploads/2022/02/teesside-university-logo.png\" alt=\"teesside logo\" width=\"120\" height=\"70\">", show_label=False,scale=1)
83
+ #Image.open("./data/teesside university logo.png"), height=70, show_label=False, container=False)
84
+ with gr.Column(variant="compact"):
85
+ with gr.Row(variant="compact"):
86
+ query = gr.Textbox(label="Enter your query", show_label=False, placeholder= "Enter your query" , scale=5)
87
+ btn = gr.Button("Search query", variant="primary", scale=1)
88
+
89
+ n_s = gr.Slider(2, 10, label='Number of Top Results', value=5, step=1.0, show_label=True)
90
+
91
+
92
+ with gr.Column(variant="compact"):
93
+ gr.Markdown("## Results")
94
+ gallery = gr.Gallery(label="found images", show_label=True, elem_id="gallery", columns=[2], rows=[4], object_fit="contain", height="auto", preview=True)
95
+ gr.Markdown("Information of the found images")
96
+ df = gr.DataFrame()
97
+ btn.click(main, [query, n_s], [gallery, df])
98
+
99
+ with gr.Column(variant="compact"):
100
+ gr.Markdown("## Examples")
101
+ gr.Examples(examples, [query, n_s])
102
+
103
+
104
+ demo.launch(debug='True')
105
+
captions.txt ADDED
The diff for this file is too large to render. See raw diff
 
image_embeddings_8_clip14_cxrbert.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:fc76673a76678efa04336a1badc9dd8e29bc45ac0b0f97284ca18d3ddb73c298
3
+ size 16742563
images/ROCO_00001.jpg ADDED

Git LFS Details

  • SHA256: 5d3441e60b492c9790a73ba4f7e9eeb3414a375f7fec3cb7e4d42d144abbef50
  • Pointer size: 130 Bytes
  • Size of remote file: 87.8 kB
images/ROCO_00006.jpg ADDED

Git LFS Details

  • SHA256: 36021398f994b4a35986b71da3242cbee46e27973786fe9ed64e4c1a0511250b
  • Pointer size: 130 Bytes
  • Size of remote file: 97.7 kB
images/ROCO_00016.jpg ADDED

Git LFS Details

  • SHA256: 7e4579f9d203252776a22d61617db2c75a0c711c16422eeaad0deede942a562f
  • Pointer size: 131 Bytes
  • Size of remote file: 147 kB
images/ROCO_00025.jpg ADDED

Git LFS Details

  • SHA256: 82b8d5b98e41698b3ab139e50c6b96b6100777a91698397f6309ac4a58f270da
  • Pointer size: 130 Bytes
  • Size of remote file: 48.8 kB
images/ROCO_00031.jpg ADDED

Git LFS Details

  • SHA256: 1064ee22d120846fdc232455129b9eb3618c02e6cffe954d1dd99f85012443b1
  • Pointer size: 131 Bytes
  • Size of remote file: 119 kB
images/ROCO_00036.jpg ADDED

Git LFS Details

  • SHA256: e985bcd25fef14fd7455d6a6b5d54dd26433ed8d35a68e42a925d312f8806eeb
  • Pointer size: 131 Bytes
  • Size of remote file: 145 kB
images/ROCO_00061.jpg ADDED

Git LFS Details

  • SHA256: b03ac20ee849d5e330541d3d2d095f69de21d3c0f534921a9ef84947dcf07d9d
  • Pointer size: 130 Bytes
  • Size of remote file: 67.7 kB
images/ROCO_00084.jpg ADDED

Git LFS Details

  • SHA256: 0f17f6d9a548ec824df4014eea8d57080a23f43f750d72db4c5e0f48ba39ca78
  • Pointer size: 131 Bytes
  • Size of remote file: 166 kB
images/ROCO_00138.jpg ADDED

Git LFS Details

  • SHA256: b9a7ebbe477b9048fcfea9640f1e3ef1571d3e4713e3952fd39cc8396c92ae7d
  • Pointer size: 131 Bytes
  • Size of remote file: 101 kB
images/ROCO_00153.jpg ADDED

Git LFS Details

  • SHA256: 750de09a944ceb3cd327e167c83d9ab8daf6b33c969e9a8fbf1e7c8d4a20e353
  • Pointer size: 131 Bytes
  • Size of remote file: 133 kB
images/ROCO_00176.jpg ADDED

Git LFS Details

  • SHA256: 09be06eda5dbd4055eb18ef780ebb4e2f0661fe0e43604fb269c2b7faee3f561
  • Pointer size: 130 Bytes
  • Size of remote file: 33.4 kB
images/ROCO_00185.jpg ADDED

Git LFS Details

  • SHA256: 16966317f109a9b410793f7514e46818ad9435c560499bc03748e818b4652614
  • Pointer size: 131 Bytes
  • Size of remote file: 187 kB
images/ROCO_00190.jpg ADDED

Git LFS Details

  • SHA256: 50b33b94a587338d23e66e1988e5e99e37afae37465b13c6d47b889723bfb40c
  • Pointer size: 131 Bytes
  • Size of remote file: 408 kB
images/ROCO_00206.jpg ADDED

Git LFS Details

  • SHA256: a14d0b231a3c3cba0fd382269d16179873610ef0d4eb82caa46159d8af2bc3f1
  • Pointer size: 130 Bytes
  • Size of remote file: 94.3 kB
images/ROCO_00218.jpg ADDED

Git LFS Details

  • SHA256: 3e6af244a6f0c1bd7363ef01e03ab35b19328700c847b7e5a0ffc393dc61e344
  • Pointer size: 131 Bytes
  • Size of remote file: 117 kB
images/ROCO_00251.jpg ADDED

Git LFS Details

  • SHA256: 29ee03b10bbc8b14848a8cfa48e2ef14dbe321aa1fd51ad921ebd473cb14ed5d
  • Pointer size: 130 Bytes
  • Size of remote file: 69.6 kB
images/ROCO_00258.jpg ADDED

Git LFS Details

  • SHA256: 028bef9592d62fa8f2f68fee87df37633b021586d0776fffc9e0682c16a53253
  • Pointer size: 131 Bytes
  • Size of remote file: 108 kB
images/ROCO_00261.jpg ADDED

Git LFS Details

  • SHA256: 792f9a2f35b4b933d307a9a420023589d1fd581acf24ee921de3a00345281e25
  • Pointer size: 131 Bytes
  • Size of remote file: 135 kB
images/ROCO_00264.jpg ADDED

Git LFS Details

  • SHA256: 5339c8e6935b2d782a1477af3642bf28eeffe6c542370e7cab4343d6cc0dbbb0
  • Pointer size: 130 Bytes
  • Size of remote file: 42.7 kB
images/ROCO_00271.jpg ADDED

Git LFS Details

  • SHA256: ba54d8c97ce3460be3b09c60beee577035a860157fc83186995fbdf5a8c53109
  • Pointer size: 130 Bytes
  • Size of remote file: 55.9 kB
images/ROCO_00300.jpg ADDED

Git LFS Details

  • SHA256: f2b3d4b4489a12e82c2b984eb0a2e38021323e2628bbf67751272f718088ce3b
  • Pointer size: 130 Bytes
  • Size of remote file: 84.1 kB
images/ROCO_00302.jpg ADDED

Git LFS Details

  • SHA256: 1360ca529d28dc3ea66648428d4bca4af8d2bfea6efdbbf7e3ae8f37b7d81c3e
  • Pointer size: 130 Bytes
  • Size of remote file: 74 kB
images/ROCO_00303.jpg ADDED

Git LFS Details

  • SHA256: aaa1107073e07990363c46fc882d58f060784ae922da6953ab20825e2d60aa76
  • Pointer size: 131 Bytes
  • Size of remote file: 102 kB
images/ROCO_00307.jpg ADDED

Git LFS Details

  • SHA256: a22c8b090b9ff9b29bd887cdb33932a5b260dee573c34dd2c3595ce7c1af2450
  • Pointer size: 130 Bytes
  • Size of remote file: 69.9 kB
images/ROCO_00316.jpg ADDED

Git LFS Details

  • SHA256: 5ce0d6772fc9091471b04b8bf10c23d4c28f0049eb474e15cb93d33d92212087
  • Pointer size: 130 Bytes
  • Size of remote file: 19.4 kB
images/ROCO_00319.jpg ADDED

Git LFS Details

  • SHA256: 92647211d805848bb511750eb89d3aa8c8a772056fdb757db66d6931e3973506
  • Pointer size: 130 Bytes
  • Size of remote file: 84.1 kB
images/ROCO_00328.jpg ADDED

Git LFS Details

  • SHA256: b77346b9803d37e6946d0d4bd7f84ebb6d19cbce0ff365aebf760e8db01734bb
  • Pointer size: 130 Bytes
  • Size of remote file: 26.2 kB
images/ROCO_00332.jpg ADDED

Git LFS Details

  • SHA256: 66eb5dc91d9308efc3a54dda1b8c03054d1286aa5ec512ca5c7d6824e69f5fbe
  • Pointer size: 130 Bytes
  • Size of remote file: 48.5 kB
images/ROCO_00333.jpg ADDED

Git LFS Details

  • SHA256: 9e604c5d9b573be3803ce3ef7a7c24122fdb672557046cfb7846a15bd373f7ae
  • Pointer size: 130 Bytes
  • Size of remote file: 56.9 kB
images/ROCO_00341.jpg ADDED

Git LFS Details

  • SHA256: 2482f8fc17e76475f72ad9cf0d46219f5919bed6969cd743ee580f1a155a788d
  • Pointer size: 130 Bytes
  • Size of remote file: 58.9 kB
images/ROCO_00350.jpg ADDED

Git LFS Details

  • SHA256: 38efe2ea7a119147b703f39e704b8272abed99a4ef0481b9d5e3dc440e41ed01
  • Pointer size: 130 Bytes
  • Size of remote file: 66.1 kB
images/ROCO_00382.jpg ADDED

Git LFS Details

  • SHA256: 61a7b8536b36ebbf9380c3832d647cabb54d30fac60ab252e68f4d1b85d9a380
  • Pointer size: 130 Bytes
  • Size of remote file: 73.1 kB
images/ROCO_00391.jpg ADDED

Git LFS Details

  • SHA256: 7e6fc215cd09be5ef3bbc20ea1a28a3f5aadaf4c0f4ad275a662f85a28122109
  • Pointer size: 130 Bytes
  • Size of remote file: 26.2 kB
images/ROCO_00402.jpg ADDED

Git LFS Details

  • SHA256: 693d12cad4fd017cb7e341c72ebeab0b16d1a2f2af66fab0172fbe4f02bb3b60
  • Pointer size: 131 Bytes
  • Size of remote file: 103 kB
images/ROCO_00425.jpg ADDED

Git LFS Details

  • SHA256: 32063a56ad3b9e6d78e4183cbcf64d04677a9dee712bc535584eed3b59cba719
  • Pointer size: 130 Bytes
  • Size of remote file: 90.5 kB
images/ROCO_00428.jpg ADDED

Git LFS Details

  • SHA256: adf49e991823d31b30d734efea14665dbd97f29a9379f50882c1c1fb784d8801
  • Pointer size: 131 Bytes
  • Size of remote file: 127 kB
images/ROCO_00446.jpg ADDED

Git LFS Details

  • SHA256: 0269f254a6b8c49e94f3abec06d2c2be22e4c47a5dfbe878326439f421d4cd15
  • Pointer size: 130 Bytes
  • Size of remote file: 48.2 kB
images/ROCO_00447.jpg ADDED

Git LFS Details

  • SHA256: b9d780ec540f514034eb5bc30dff1587646b3e42e787c51bdd1dd5273e84c356
  • Pointer size: 131 Bytes
  • Size of remote file: 146 kB
images/ROCO_00450.jpg ADDED

Git LFS Details

  • SHA256: d20148649d92318f79814b1b29c9cea5d202a24ae0d80dc79f8ba0747cb539b0
  • Pointer size: 130 Bytes
  • Size of remote file: 81.3 kB
images/ROCO_00454.jpg ADDED

Git LFS Details

  • SHA256: d6d5e9f80b818d1910442733f271a07e5ba842ea859a702ef13803ec9fc29f5b
  • Pointer size: 130 Bytes
  • Size of remote file: 64.1 kB
images/ROCO_00468.jpg ADDED

Git LFS Details

  • SHA256: b3fdddd820352e0bdeacf24304768ae88be9b4dfb9af717fcf1087f3ac14797c
  • Pointer size: 130 Bytes
  • Size of remote file: 79.7 kB
images/ROCO_00477.jpg ADDED

Git LFS Details

  • SHA256: 9ff0cfd48f957ba2024467ebb41ad615ee17506b40ec179f6acd42bd49f209f6
  • Pointer size: 130 Bytes
  • Size of remote file: 21.9 kB
images/ROCO_00481.jpg ADDED

Git LFS Details

  • SHA256: 63e5d93b71dd0756fc227f0137fada469848b5d7402ad92aae754a1757438558
  • Pointer size: 130 Bytes
  • Size of remote file: 29 kB
images/ROCO_00494.jpg ADDED

Git LFS Details

  • SHA256: e9f7b581c3424f4e19e3d2de02eba083359dc0365dc00005c6384a567308ffbd
  • Pointer size: 130 Bytes
  • Size of remote file: 91.7 kB
images/ROCO_00496.jpg ADDED

Git LFS Details

  • SHA256: 05041280dc07260bbfd3ced5a4aad2982cded0c1620079b32c23dc951219a8f9
  • Pointer size: 130 Bytes
  • Size of remote file: 69.2 kB
images/ROCO_00506.jpg ADDED

Git LFS Details

  • SHA256: e082aa654ac8d18455ebf97b8ff207ea247bb66232465492564c57d8524fd2d2
  • Pointer size: 130 Bytes
  • Size of remote file: 42.3 kB