Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,50 +7,100 @@ from tensorflow.keras.preprocessing.image import img_to_array
|
|
| 7 |
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
| 8 |
import pickle
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
# Load your pre-trained model and tokenizer
|
| 11 |
-
model =
|
|
|
|
| 12 |
with open("tokenizer.pkl", "rb") as handle:
|
| 13 |
tokenizer = pickle.load(handle)
|
| 14 |
|
| 15 |
-
# Load your precomputed features if required (else comment out)
|
| 16 |
-
# with open("features.pkl", "rb") as f:
|
| 17 |
-
# features = pickle.load(f)
|
| 18 |
-
|
| 19 |
# Image feature extractor model
|
| 20 |
feature_extractor = VGG16()
|
| 21 |
feature_extractor = tf.keras.Model(feature_extractor.input, feature_extractor.layers[-2].output)
|
| 22 |
|
| 23 |
# Description generation function
|
| 24 |
def generate_caption(image):
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
break
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
| 54 |
|
| 55 |
# Gradio Interface
|
| 56 |
title = "📸 Image Caption Generator"
|
|
@@ -68,4 +118,4 @@ iface = gr.Interface(
|
|
| 68 |
)
|
| 69 |
|
| 70 |
if __name__ == "__main__":
|
| 71 |
-
iface.launch()
|
|
|
|
| 7 |
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
| 8 |
import pickle
|
| 9 |
|
| 10 |
+
# Custom Lambda layer with explicit output shape
|
| 11 |
+
class CustomLambda(tf.keras.layers.Lambda):
|
| 12 |
+
def __init__(self, function, output_shape=None, **kwargs):
|
| 13 |
+
super().__init__(function, output_shape=output_shape, **kwargs)
|
| 14 |
+
|
| 15 |
+
def compute_output_shape(self, input_shape):
|
| 16 |
+
if self.output_shape is None:
|
| 17 |
+
# Default behavior for attention-like operations
|
| 18 |
+
if isinstance(input_shape, list) and len(input_shape) == 2:
|
| 19 |
+
return input_shape[0] # Return shape of first input
|
| 20 |
+
return input_shape
|
| 21 |
+
return super().compute_output_shape(input_shape)
|
| 22 |
+
|
| 23 |
+
# Define custom objects for model loading
|
| 24 |
+
custom_objects = {
|
| 25 |
+
'Lambda': CustomLambda,
|
| 26 |
+
'lambda': CustomLambda
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
# Multiple loading strategies
|
| 30 |
+
def load_model_safely():
|
| 31 |
+
strategies = [
|
| 32 |
+
# Strategy 1: Load with custom objects
|
| 33 |
+
lambda: tf.keras.models.load_model("caption_model.h5", custom_objects=custom_objects),
|
| 34 |
+
# Strategy 2: Load without compilation
|
| 35 |
+
lambda: tf.keras.models.load_model("caption_model.h5", compile=False),
|
| 36 |
+
# Strategy 3: Load with different custom objects
|
| 37 |
+
lambda: tf.keras.models.load_model("caption_model.h5",
|
| 38 |
+
custom_objects={'Lambda': tf.keras.layers.Lambda}),
|
| 39 |
+
]
|
| 40 |
+
|
| 41 |
+
for i, strategy in enumerate(strategies, 1):
|
| 42 |
+
try:
|
| 43 |
+
model = strategy()
|
| 44 |
+
print(f"Model loaded successfully using strategy {i}!")
|
| 45 |
+
return model
|
| 46 |
+
except Exception as e:
|
| 47 |
+
print(f"Strategy {i} failed: {e}")
|
| 48 |
+
continue
|
| 49 |
+
|
| 50 |
+
raise Exception("All loading strategies failed")
|
| 51 |
+
|
| 52 |
# Load your pre-trained model and tokenizer
|
| 53 |
+
model = load_model_safely()
|
| 54 |
+
|
| 55 |
with open("tokenizer.pkl", "rb") as handle:
|
| 56 |
tokenizer = pickle.load(handle)
|
| 57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
# Image feature extractor model
|
| 59 |
feature_extractor = VGG16()
|
| 60 |
feature_extractor = tf.keras.Model(feature_extractor.input, feature_extractor.layers[-2].output)
|
| 61 |
|
| 62 |
# Description generation function
|
| 63 |
def generate_caption(image):
|
| 64 |
+
try:
|
| 65 |
+
# Preprocess the image
|
| 66 |
+
image = image.resize((224, 224))
|
| 67 |
+
image = img_to_array(image)
|
| 68 |
+
image = np.expand_dims(image, axis=0)
|
| 69 |
+
image = preprocess_input(image)
|
| 70 |
+
|
| 71 |
+
# Extract features
|
| 72 |
+
feature = feature_extractor.predict(image, verbose=0)
|
| 73 |
+
|
| 74 |
+
# Generate caption
|
| 75 |
+
input_text = 'startseq'
|
| 76 |
+
max_length = 34 # set this to your model's max_length
|
| 77 |
+
|
| 78 |
+
for _ in range(max_length):
|
| 79 |
+
sequence = tokenizer.texts_to_sequences([input_text])[0]
|
| 80 |
+
sequence = pad_sequences([sequence], maxlen=max_length)
|
| 81 |
+
|
| 82 |
+
try:
|
| 83 |
+
yhat = model.predict([feature, sequence], verbose=0)
|
| 84 |
+
yhat = np.argmax(yhat)
|
| 85 |
+
except Exception as e:
|
| 86 |
+
print(f"Prediction error: {e}")
|
| 87 |
+
return "Error generating caption"
|
| 88 |
+
|
| 89 |
+
word = ''
|
| 90 |
+
for w, i in tokenizer.word_index.items():
|
| 91 |
+
if i == yhat:
|
| 92 |
+
word = w
|
| 93 |
+
break
|
| 94 |
+
|
| 95 |
+
if word == 'endseq' or word == '':
|
| 96 |
break
|
| 97 |
+
input_text += ' ' + word
|
| 98 |
+
|
| 99 |
+
caption = input_text.replace('startseq', '').strip()
|
| 100 |
+
return caption
|
| 101 |
+
|
| 102 |
+
except Exception as e:
|
| 103 |
+
return f"Error processing image: {str(e)}"
|
| 104 |
|
| 105 |
# Gradio Interface
|
| 106 |
title = "📸 Image Caption Generator"
|
|
|
|
| 118 |
)
|
| 119 |
|
| 120 |
if __name__ == "__main__":
|
| 121 |
+
iface.launch()
|