dabasajay / image-caption-generator Goto Github PK
View Code? Open in Web Editor NEWA neural network to generate captions for an image using CNN and RNN with BEAM Search.
License: MIT License
A neural network to generate captions for an image using CNN and RNN with BEAM Search.
License: MIT License
i'm using a google colab.
when i start !python train_val.py
Traceback (most recent call last):
File "train_val.py", line 86, in
verbose=1)
File "/usr/local/lib/python3.6/dist-packages/keras/legacy/interfaces.py", line 91, in wrapper
return func(*args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/keras/engine/training.py", line 1658, in fit_generator
initial_epoch=initial_epoch)
File "/usr/local/lib/python3.6/dist-packages/keras/engine/training_generator.py", line 103, in fit_generator
callbacks._call_begin_hook('train')
File "/usr/local/lib/python3.6/dist-packages/keras/callbacks.py", line 103, in _call_begin_hook
self.on_train_begin()
File "/usr/local/lib/python3.6/dist-packages/keras/callbacks.py", line 219, in on_train_begin
callback.on_train_begin(logs)
File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/callbacks.py", line 905, in on_train_begin
if self.model._in_multi_worker_mode():
AttributeError: 'Model' object has no attribute '_in_multi_worker_mode'
This is the output from training- the model is not getting saved due to a callback issue.
4:6:34: Using Inceptionv3 model
{}: Generating image features using inceptionv3 model...
2022-01-14 04:06:34.692740: E tensorflow/stream_executor/cuda/cuda_driver.cc:271] failed call to cuInit: CUDA_ERROR_NO_DEVICE: no CUDA-capable device is detected
Downloading data from https://storage.googleapis.com/tensorflow/keras-applications/inception_v3/inception_v3_weights_tf_dim_ordering_tf_kernels.h5
96116736/96112376 [==============================] - 1s 0us/step
96124928/96112376 [==============================] - 1s 0us/step
100% 8091/8091 [28:33<00:00, 4.72it/s]
4:35:12: Completed & Saved features for 8091 images successfully
4:35:12: Parsing captions file...
4:35:12: Parsed captions: 40460
4:35:12: Parsed & Saved successfully
4:35:12: Available images for training: 6000
4:35:12: Available captions for training: 30000
4:35:13: Available images for validation: 1000
4:35:13: Available captions for validation: 5000
RNN Model (Decoder) Summary :
Model: "model_1"
input_3 (InputLayer) [(None, 40)] 0 []
input_2 (InputLayer) [(None, 2048)] 0 []
embedding (Embedding) (None, 40, 300) 2213400 ['input_3[0][0]']
dense (Dense) (None, 300) 614700 ['input_2[0][0]']
lstm (LSTM) (None, 40, 256) 570368 ['embedding[0][0]']
repeat_vector (RepeatVector) (None, 40, 300) 0 ['dense[0][0]']
time_distributed (TimeDistribu (None, 40, 300) 77100 ['lstm[0][0]']
ted)
concatenate_2 (Concatenate) (None, 40, 600) 0 ['repeat_vector[0][0]',
'time_distributed[0][0]']
bidirectional (Bidirectional) (None, 512) 1755136 ['concatenate_2[0][0]']
dense_2 (Dense) (None, 7378) 3784914 ['bidirectional[0][0]']
==================================================================================================
Total params: 9,015,618
Trainable params: 9,015,618
Non-trainable params: 0
None
steps_train: 94, steps_val: 16
Batch Size: 64
Total Number of Epochs = 20
train_val.py:86: UserWarning: Model.fit_generator
is deprecated and will be removed in a future version. Please use Model.fit
, which supports generators.
verbose=1)
Epoch 1/20
Traceback (most recent call last):
File "train_val.py", line 86, in
verbose=1)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 2030, in fit_generator
initial_epoch=initial_epoch)
File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/execute.py", line 59, in quick_execute
inputs, attrs, num_outputs)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Matrix size-incompatible: In[0]: [3732,1000], In[1]: [2048,300]
[[node model_1/dense/Relu
(defined at /usr/local/lib/python3.7/dist-packages/keras/backend.py:4867)
]] [Op:__inference_train_function_569695]
Errors may have originated from an input operation.
Input Source operations connected to node model_1/dense/Relu:
In[0] model_1/dense/BiasAdd (defined at /usr/local/lib/python3.7/dist-packages/keras/layers/core/dense.py:210)
Operation defined at: (most recent call last)
File "train_val.py", line 86, in
verbose=1)File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 2030, in fit_generator
initial_epoch=initial_epoch)File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 64, in error_handler
return fn(*args, **kwargs)File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1216, in fit
tmp_logs = self.train_function(iterator)File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 878, in train_function
return step_function(self, iterator)File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 867, in step_function
outputs = model.distribute_strategy.run(run_step, args=(data,))File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 860, in run_step
outputs = model.train_step(data)File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 808, in train_step
y_pred = self(x, training=True)File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 64, in error_handler
return fn(*args, **kwargs)File "/usr/local/lib/python3.7/dist-packages/keras/engine/base_layer.py", line 1083, in call
outputs = call_fn(inputs, *args, **kwargs)File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 92, in error_handler
return fn(*args, **kwargs)File "/usr/local/lib/python3.7/dist-packages/keras/engine/functional.py", line 452, in call
inputs, training=training, mask=mask)File "/usr/local/lib/python3.7/dist-packages/keras/engine/functional.py", line 589, in _run_internal_graph
outputs = node.layer(*args, **kwargs)File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 64, in error_handler
return fn(*args, **kwargs)File "/usr/local/lib/python3.7/dist-packages/keras/engine/base_layer.py", line 1083, in call
outputs = call_fn(inputs, *args, **kwargs)File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 92, in error_handler
return fn(*args, **kwargs)File "/usr/local/lib/python3.7/dist-packages/keras/layers/core/dense.py", line 213, in call
outputs = self.activation(outputs)File "/usr/local/lib/python3.7/dist-packages/keras/activations.py", line 311, in relu
return backend.relu(x, alpha=alpha, max_value=max_value, threshold=threshold)File "/usr/local/lib/python3.7/dist-packages/keras/backend.py", line 4867, in relu
x = tf.nn.relu(x)
2022-01-14 04:35:25.329300: W tensorflow/core/kernels/data/generator_dataset_op.cc:107] Error occurred when finalizing GeneratorDataset iterator: FAILED_PRECONDITION: Python interpreter state is not initialized. The process may be terminated.
[[{{node PyFunc}}]]
train_val.py:80: UserWarning: Model.fit_generator
is deprecated and will be removed in a future version. Please use Model.fit
, which supports generators.
model.fit_generator(generator_train,
Epoch 1/11
2023-06-07 21:32:02.265457: W tensorflow/tsl/framework/cpu_allocator_impl.cc:83] Allocation of 179664000 exceeds 10% of free system memory.
Traceback (most recent call last):
File "train_val.py", line 80, in
model.fit_generator(generator_train,
File "C:\Users\ajayt\anaconda3\lib\site-packages\keras\engine\training.py", line 2636, in fit_generator
return self.fit(
File "C:\Users\ajayt\anaconda3\lib\site-packages\keras\utils\traceback_utils.py", line 70, in error_handler
raise e.with_traceback(filtered_tb) from None
File "C:\Users\ajayt\anaconda3\lib\site-packages\tensorflow\python\eager\execute.py", line 52, in quick_execute
tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
tensorflow.python.framework.errors_impl.InvalidArgumentError: Graph execution error:
Detected at node 'model/dense/Relu' defined at (most recent call last):
File "train_val.py", line 80, in
model.fit_generator(generator_train,
File "C:\Users\ajayt\anaconda3\lib\site-packages\keras\engine\training.py", line 2636, in fit_generator
return self.fit(
File "C:\Users\ajayt\anaconda3\lib\site-packages\keras\utils\traceback_utils.py", line 65, in error_handler
return fn(*args, **kwargs)
File "C:\Users\ajayt\anaconda3\lib\site-packages\keras\engine\training.py", line 1685, in fit
tmp_logs = self.train_function(iterator)
File "C:\Users\ajayt\anaconda3\lib\site-packages\keras\engine\training.py", line 1284, in train_function
return step_function(self, iterator)
File "C:\Users\ajayt\anaconda3\lib\site-packages\keras\engine\training.py", line 1268, in step_function
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "C:\Users\ajayt\anaconda3\lib\site-packages\keras\engine\training.py", line 1249, in run_step
outputs = model.train_step(data)
File "C:\Users\ajayt\anaconda3\lib\site-packages\keras\engine\training.py", line 1050, in train_step
y_pred = self(x, training=True)
File "C:\Users\ajayt\anaconda3\lib\site-packages\keras\utils\traceback_utils.py", line 65, in error_handler
return fn(*args, **kwargs)
File "C:\Users\ajayt\anaconda3\lib\site-packages\keras\engine\training.py", line 558, in call
return super().call(*args, **kwargs)
File "C:\Users\ajayt\anaconda3\lib\site-packages\keras\utils\traceback_utils.py", line 65, in error_handler
return fn(*args, **kwargs)
File "C:\Users\ajayt\anaconda3\lib\site-packages\keras\engine\base_layer.py", line 1145, in call
outputs = call_fn(inputs, *args, **kwargs)
File "C:\Users\ajayt\anaconda3\lib\site-packages\keras\utils\traceback_utils.py", line 96, in error_handler
return fn(*args, **kwargs)
File "C:\Users\ajayt\anaconda3\lib\site-packages\keras\engine\functional.py", line 512, in call
return self._run_internal_graph(inputs, training=training, mask=mask)
File "C:\Users\ajayt\anaconda3\lib\site-packages\keras\engine\functional.py", line 669, in _run_internal_graph
outputs = node.layer(*args, **kwargs)
File "C:\Users\ajayt\anaconda3\lib\site-packages\keras\utils\traceback_utils.py", line 65, in error_handler
return fn(*args, **kwargs)
File "C:\Users\ajayt\anaconda3\lib\site-packages\keras\engine\base_layer.py", line 1145, in call
outputs = call_fn(inputs, *args, **kwargs)
File "C:\Users\ajayt\anaconda3\lib\site-packages\keras\utils\traceback_utils.py", line 96, in error_handler
return fn(*args, **kwargs)
File "C:\Users\ajayt\anaconda3\lib\site-packages\keras\layers\core\dense.py", line 255, in call
outputs = self.activation(outputs)
File "C:\Users\ajayt\anaconda3\lib\site-packages\keras\activations.py", line 317, in relu
return backend.relu(
File "C:\Users\ajayt\anaconda3\lib\site-packages\keras\backend.py", line 5396, in relu
x = tf.nn.relu(x)
Node: 'model/dense/Relu'
Matrix size-incompatible: In[0]: [3743,1000], In[1]: [2048,300]
[[{{node model/dense/Relu}}]] [Op:__inference_train_function_21643]
2023-06-07 21:32:04.855625: W tensorflow/core/kernels/data/generator_dataset_op.cc:108] Error occurred when finalizing GeneratorDataset iterator: FAILED_PRECONDITION: Python interpreter state is not initialized. The process may be terminated.
[[{{node PyFunc}}]]
It would be better if you add requirements.txt file containing all the required libraries for Python along with their version numbers used while making & testing of this project instead of mentioning it in the readme.md file as requirements.txt has become a de facto now a days.
We are training the model, error on train_val.py file.
ValueError: Error when checking input: expected input_8 to have shape (2048,) but got array with shape (1000,)
I tried your new code but got keyerror like this
Traceback (most recent call last):
File "train_val.py", line 15, in
X1train, X2train, max_length = loadTrainData(config)
File "E:\github\Image-Caption-Generator\utils\load_data.py", line 186, in loadTrainData
train_image_features = load_image_features(config['model_data_path']+'features_'+str(config['model_type'])+'.pkl', train_image_ids)
File "E:\github\Image-Caption-Generator\utils\load_data.py", line 78, in load_image_features
features = {_id: all_features[_id] for _id in ids}
File "E:\github\Image-Caption-Generator\utils\load_data.py", line 78, in
features = {_id: all_features[_id] for _id in ids}
KeyError: '172092461_a9a9762e13'
Can you help me again?
Hello there!
Would it be possible that you release the pretrained model, so we could generate captions for our unlabeled datasets? That would be awesome!
"In config.py, we can modify the parameter max_length.
However, for my trained model, max_length can only be 26. ****
If I change it to another number, it will throw an error:
ValueError: Error when checking input: expected input_3 to have shape (26,) but got array with shape (52,)."
When I run train_val.py i get the following error I am using google colab
ValueError: in user code:
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:806 train_function *
return step_function(self, iterator)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:796 step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:1211 run
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2585 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2945 _call_for_each_replica
return fn(*args, **kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:789 run_step **
outputs = model.train_step(data)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:757 train_step
self.trainable_variables)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:2737 _minimize
trainable_variables))
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/optimizer_v2/optimizer_v2.py:562 _aggregate_gradients
filtered_grads_and_vars = _filter_grads(grads_and_vars)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/optimizer_v2/optimizer_v2.py:1271 _filter_grads
([v.name for _, v in grads_and_vars],))
ValueError: No gradients provided for any variable: ['embedding/embeddings:0', 'dense/kernel:0', 'dense/bias:0', 'lstm/lstm_cell/kernel:0', 'lstm/lstm_cell/recurrent_kernel:0', 'lstm/lstm_cell/bias:0', 'time_distributed/kernel:0', 'time_distributed/bias:0', 'bidirectional/forward_lstm_1/lstm_cell_2/kernel:0', 'bidirectional/forward_lstm_1/lstm_cell_2/recurrent_kernel:0', 'bidirectional/forward_lstm_1/lstm_cell_2/bias:0', 'bidirectional/backward_lstm_1/lstm_cell_3/kernel:0', 'bidirectional/backward_lstm_1/lstm_cell_3/recurrent_kernel:0', 'bidirectional/backward_lstm_1/lstm_cell_3/bias:0', 'dense_2/kernel:0', 'dense_2/bias:0'].
tensorflow.python.framework.errors_impl.InternalError: Blas GEMM launch failed : a.shape=(938, 256), b.shape=(256, 256), m=938, n=256, k=256
[[{{node lstm_1/while/MatMul_4}}]]
[[{{node bidirectional_1/strided_slice}}]]
face such error, what an i do? help!
Hi! I'm would like to say thank you, This proyect helps my a lot.
Mi computer in not good at all, so I decided to use google colab and it works. I install the requeriments with a txt file and my training was perfect. But when I dowload the .hdf5 files and use it in my Pycharm proyect the files doesn't work. This is my error:
File "C:\Users\Usuario\anaconda3\envs\ic_gpu\lib\site-packages\keras\engine\saving.py", line 419, in load_model
model = _deserialize_model(f, custom_objects, compile)
File "C:\Users\Usuario\anaconda3\envs\ic_gpu\lib\site-packages\keras\engine\saving.py", line 251, in _deserialize_model
if weight_names:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
I don't know how fix it.
Hi I'm student in South Korea
I add some code to use pre-trained word vector(Glove)
but actually I'm not familiar with github :(
so if you have some time, would you please come to my repo and review about that?
thx:)
How to run this project
The training was successful.
After that, I put all the test images inside the test_data-> crop_test, changed the path as
'test_data_path': 'test_data/crop_test/'
Changed this also after the completion of training
'model_load_path': 'model_data/model_inceptionv3_epoch-07_train_loss-2.7715_val_loss-3.5321.hdf5'
Generating caption for 2377.jpg
Traceback (most recent call last):
File "test.py", line 59, in
generated_caption = generate_caption_beam_search(caption_model, tokenizer, image, max_length, beam_index=config['beam_search_k'])
File "/home/Image-Caption-Generator/utils/model.py", line 140, in generate_caption_beam_search
preds = model.predict([image,padded_seq], verbose=0)
File "/home/img-cap/lib/python3.6/site-packages/keras/engine/training.py", line 1149, in predict
x, _, _ = self._standardize_user_data(x)
File "/home/img-cap/lib/python3.6/site-packages/keras/engine/training.py", line 751, in _standardize_user_data
exception_prefix='input')
File "/home/img-cap/lib/python3.6/site-packages/keras/engine/training_utils.py", line 138, in standardize_input_data
str(data_shape))
ValueError: Error when checking input: expected input_2 to have shape (33,) but got array with shape (40,)
Thank you.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.