fork download
  1. import numpy as np
  2. import tensorflow as tf
  3. from tensorflow.keras.applications import InceptionV3
  4. from tensorflow.keras.models import Model, load_model
  5. from tensorflow.keras.preprocessing import image
  6. from tensorflow.keras.applications.inception_v3 import preprocess_input
  7. from tensorflow.keras.preprocessing.sequence import pad_sequences
  8. from tensorflow.keras.layers import Embedding, LSTM, Dense
  9.  
  10. # Load pre-trained InceptionV3 model for feature extraction
  11. def load_cnn_model():
  12. base_model = InceptionV3(weights='imagenet')
  13. cnn_model = Model(base_model.input, base_model.layers[-2].output)
  14. return cnn_model
  15.  
  16. # Extract image features using CNN
  17. def extract_image_features(cnn_model, img_path):
  18. img = image.load_img(img_path, target_size=(299, 299))
  19. img_array = image.img_to_array(img)
  20. img_array = np.expand_dims(img_array, axis=0)
  21. img_array = preprocess_input(img_array)
  22. features = cnn_model.predict(img_array)
  23. return features
  24.  
  25. # Load tokenizer and pre-trained captioning model
  26. def load_captioning_model(tokenizer_path, captioning_model_path):
  27. tokenizer = tf.keras.models.load_model(tokenizer_path)
  28. captioning_model = load_model(captioning_model_path)
  29. return tokenizer, captioning_model
  30.  
  31. # Generate caption from image features
  32. def generate_caption(features, tokenizer, captioning_model, max_length):
  33. caption = 'startseq'
  34. for _ in range(max_length):
  35. sequence = tokenizer.texts_to_sequences([caption])[0]
  36. sequence = pad_sequences([sequence], maxlen=max_length)
  37. pred_word_index = np.argmax(captioning_model.predict([features, sequence]), axis=-1)
  38. word = tokenizer.index_word.get(pred_word_index[0], '')
  39. if word == 'endseq' or not word:
  40. break
  41. caption += ' ' + word
  42. return caption.replace('startseq', '').strip()
  43.  
  44. # Main function
  45. def main():
  46. cnn_model = load_cnn_model()
  47. img_path = 'sample_image.jpg' # Provide the path to your sample image
  48. tokenizer_path = 'tokenizer.pkl' # Path to the saved tokenizer
  49. captioning_model_path = 'captioning_model.h5' # Path to the saved captioning model
  50. max_length = 20 # Max caption length
  51.  
  52. # Extract image features
  53. features = extract_image_features(cnn_model, img_path)
  54.  
  55. # Load tokenizer and captioning model
  56. tokenizer, captioning_model = load_captioning_model(tokenizer_path, captioning_model_path)
  57.  
  58. # Generate caption
  59. caption = generate_caption(features, tokenizer, captioning_model, max_length)
  60. print("Generated Caption:", caption)
  61.  
  62. if __name__ == '__main__':
  63. main()
  64.  
Time limit exceeded #stdin #stdout #stderr 5s 269668KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
WARNING:tensorflow:From /usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/resource_variable_ops.py:435: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.