Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- COCO_model.h5 +3 -0
- app.py +63 -0
- model.h5 +3 -0
COCO_model.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:35200360d19ea02ce5c8f007c8bf6d8297e3c16ae3b3fb4b6eeb24ec1c07f8e6
|
| 3 |
+
size 636283447
|
app.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import clip
|
| 3 |
+
import PIL.Image
|
| 4 |
+
import skimage.io as io
|
| 5 |
+
import streamlit as st
|
| 6 |
+
from transformers import GPT2Tokenizer, GPT2LMHeadModel, AdamW, get_linear_schedule_with_warmup
|
| 7 |
+
from model import preprocess,clip_model,generate2,ClipCaptionModel
|
| 8 |
+
|
| 9 |
+
#model loading code
|
| 10 |
+
|
| 11 |
+
device = "cpu"
|
| 12 |
+
clip_model, preprocess = clip.load("ViT-B/32", device=device, jit=False)
|
| 13 |
+
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
|
| 14 |
+
|
| 15 |
+
prefix_length = 10
|
| 16 |
+
|
| 17 |
+
model = ClipCaptionModel(prefix_length)
|
| 18 |
+
|
| 19 |
+
model.load_state_dict(torch.load('C:\Deep learning lab\DLops Project\Cl+gpt2\model.h5',map_location=torch.device('cpu')))
|
| 20 |
+
|
| 21 |
+
model = model.eval()
|
| 22 |
+
|
| 23 |
+
coco_model = ClipCaptionModel(prefix_length)
|
| 24 |
+
coco_model.load_state_dict(torch.load('C:\Deep learning lab\DLops Project\Cl+gpt2\COCO_model.h5',map_location=torch.device('cpu')))
|
| 25 |
+
model = model.eval()
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def ui():
|
| 31 |
+
st.markdown("# Image Captioning")
|
| 32 |
+
uploaded_file = st.file_uploader("Upload an Image", type=['png', 'jpeg', 'jpg'])
|
| 33 |
+
|
| 34 |
+
if uploaded_file is not None:
|
| 35 |
+
image = io.imread(uploaded_file)
|
| 36 |
+
pil_image = PIL.Image.fromarray(image)
|
| 37 |
+
image = preprocess(pil_image).unsqueeze(0).to(device)
|
| 38 |
+
|
| 39 |
+
option = st.selectbox('Please select the Model',('Model', 'COCO Model'))
|
| 40 |
+
|
| 41 |
+
if option=='Model':
|
| 42 |
+
|
| 43 |
+
with torch.no_grad():
|
| 44 |
+
prefix = clip_model.encode_image(image).to(device, dtype=torch.float32)
|
| 45 |
+
prefix_embed = model.clip_project(prefix).reshape(1, prefix_length, -1)
|
| 46 |
+
generated_text_prefix = generate2(model, tokenizer, embed=prefix_embed)
|
| 47 |
+
|
| 48 |
+
st.image(uploaded_file, width = 500, channels = 'RGB')
|
| 49 |
+
st.markdown("**PREDICTION:** " + generated_text_prefix)
|
| 50 |
+
|
| 51 |
+
elif option=='COCO Model':
|
| 52 |
+
with torch.no_grad():
|
| 53 |
+
prefix = clip_model.encode_image(image).to(device, dtype=torch.float32)
|
| 54 |
+
prefix_embed = model.clip_project(prefix).reshape(1, prefix_length, -1)
|
| 55 |
+
generated_text_prefix = generate2(coco_model, tokenizer, embed=prefix_embed)
|
| 56 |
+
|
| 57 |
+
st.image(uploaded_file, width = 500, channels = 'RGB')
|
| 58 |
+
st.markdown("**PREDICTION:** " + generated_text_prefix)
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
if __name__ == '__main__':
|
| 62 |
+
ui()
|
| 63 |
+
|
model.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:2a36a09076b9779de2807d3aa533d455a398d70c1250aeb24a5cc9110e3d59a4
|
| 3 |
+
size 636272061
|