pritish commited on
Commit
5cf2822
1 Parent(s): 05202af

fixed FileNotFoundError

Browse files
Files changed (6) hide show
  1. .gitignore +1 -0
  2. __pycache__/model.cpython-38.pyc +0 -0
  3. app.py +7 -0
  4. model.py +9 -4
  5. test.ipynb +0 -0
  6. tmp.jpg +0 -0
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ __pycache__/*
__pycache__/model.cpython-38.pyc DELETED
Binary file (8.92 kB)
 
app.py CHANGED
@@ -15,8 +15,15 @@ img_url = st.text_input(label='Enter Image URL')
15
 
16
  if (img_url != "") and (img_url != None):
17
  img = Image.open(requests.get(img_url, stream=True).raw)
 
18
  st.image(img)
19
 
20
  img.save('tmp.jpg')
21
  pred_caption = generate_caption('tmp.jpg', caption_model)
 
 
22
  st.write(pred_caption)
 
 
 
 
 
15
 
16
  if (img_url != "") and (img_url != None):
17
  img = Image.open(requests.get(img_url, stream=True).raw)
18
+ img = img.convert('RGB')
19
  st.image(img)
20
 
21
  img.save('tmp.jpg')
22
  pred_caption = generate_caption('tmp.jpg', caption_model)
23
+
24
+ st.write('Predicted Captions:')
25
  st.write(pred_caption)
26
+
27
+ for _ in range(4):
28
+ pred_caption = generate_caption('tmp.jpg', caption_model, add_noise=True)
29
+ st.write(pred_caption)
model.py CHANGED
@@ -267,12 +267,14 @@ def load_image_from_path(img_path):
267
  return img
268
 
269
 
270
- def generate_caption(img, caption_model):
271
  if isinstance(img, str):
272
  img = load_image_from_path(img)
273
 
274
- if isinstance(img, np.ndarray):
275
- img = tf.convert_to_tensor(img)
 
 
276
 
277
  img = tf.expand_dims(img, axis=0)
278
  img_embed = caption_model.cnn_model(img)
@@ -318,6 +320,9 @@ def get_caption_model():
318
  sample_enc_out = caption_model.encoder(sample_img_embed, training=False)
319
  caption_model.decoder(sample_y, sample_enc_out, training=False)
320
 
321
- caption_model.load_weights(r'saved_models\image_captioning_transformer_weights_1.h5')
 
 
 
322
 
323
  return caption_model
 
267
  return img
268
 
269
 
270
+ def generate_caption(img, caption_model, add_noise=False):
271
  if isinstance(img, str):
272
  img = load_image_from_path(img)
273
 
274
+ if add_noise == True:
275
+ noise = tf.random.normal(img.shape)*0.1
276
+ img = (img + noise)
277
+ img = (img - tf.reduce_min(img))/(tf.reduce_max(img) - tf.reduce_min(img))
278
 
279
  img = tf.expand_dims(img, axis=0)
280
  img_embed = caption_model.cnn_model(img)
 
320
  sample_enc_out = caption_model.encoder(sample_img_embed, training=False)
321
  caption_model.decoder(sample_y, sample_enc_out, training=False)
322
 
323
+ try:
324
+ caption_model.load_weights('saved_models/image_captioning_transformer_weights_1.h5')
325
+ except FileNotFoundError:
326
+ caption_model.load_weights('Image-Captioning/saved_models/image_captioning_transformer_weights_1.h5')
327
 
328
  return caption_model
test.ipynb ADDED
The diff for this file is too large to render. See raw diff
 
tmp.jpg CHANGED