Информация о системе
- Платформа и распространение ОС: CentOS Linux выпуск 7.7.1908
- Версия TensorFlow: 2.1.0
Команда, используемая для запуска конвертера или кода, если вы используете Python API
converter = tf.lite.TFLiteConverter.from_concrete_functions([train_step.get_concrete_function(tf.TensorSpec(shape=(64, 64, 2048),dtype=tf.dtypes.float32),tf.TensorSpec(shape=(64, 50),dtype=tf.dtypes.int32))])
tflite_model = converter.convert()
Скопируйте и вставьте сюда точную команду
tflite_model = converter.convert()
Результат вызова конвертера
2020-08-07 09:40:27.848678: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:816] Optimization results for grappler item: graph_to_optimize
2020-08-07 09:40:27.848903: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: Graph size after: 16782 nodes (0), 22769 edges (0), time = 401.073ms.
2020-08-07 09:40:27.848929: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: Graph size after: 16782 nodes (0), 22769 edges (0), time = 457.052ms.
2020-08-07 09:40:27.848987: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:816] Optimization results for grappler item: rnn__decoder_gru_while_7_body_33225_grad_59089
2020-08-07 09:40:27.849000: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0.003ms.
2020-08-07 09:40:27.849053: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0.001ms.
2020-08-07 09:40:27.849064: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:816] Optimization results for grappler item: rnn__decoder_gru_while_31_body_39513_rewritten
2020-08-07 09:40:27.849075: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0.002ms.
2020-08-07 09:40:27.849084: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0ms.
2020-08-07 09:40:27.849092: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:816] Optimization results for grappler item: rnn__decoder_gru_while_47_body_43705_grad_45308
2020-08-07 09:40:27.849108: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0.002ms.
2020-08-07 09:40:27.849121: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0.001ms.
2020-08-07 09:40:27.849131: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:816] Optimization results for grappler item: rnn__decoder_gru_while_21_cond_36892_grad_54630
2020-08-07 09:40:27.849143: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0.001ms.
2020-08-07 09:40:27.849157: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0ms.
2020-08-07 09:40:27.849165: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:816] Optimization results for grappler item: rnn__decoder_gru_while_15_body_35321_rewritten
2020-08-07 09:40:27.849174: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0.002ms.
2020-08-07 09:40:27.849183: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0ms.
2020-08-07 09:40:27.849192: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:816] Optimization results for grappler item: rnn__decoder_gru_while_11_body_34273_rewritten
2020-08-07 09:40:27.849201: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0.001ms.
2020-08-07 09:40:27.849212: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0ms.
2020-08-07 09:40:27.849220: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:816] Optimization results for grappler item: rnn__decoder_gru_while_15_body_35321_grad_56441
2020-08-07 09:40:27.849230: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0.002ms.
2020-08-07 09:40:27.849239: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0ms.
2020-08-07 09:40:27.849250: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:816] Optimization results for grappler item: rnn__decoder_gru_while_38_body_41347_grad_48826
2020-08-07 09:40:27.849280: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0.001ms.
2020-08-07 09:40:27.849298: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0ms.
2020-08-07 09:40:27.849309: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:816] Optimization results for grappler item: rnn__decoder_gru_while_21_body_36893_rewritten
2020-08-07 09:40:27.849323: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0.001ms.
2020-08-07 09:40:27.849340: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0ms.
2020-08-07 09:40:27.849358: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:816] Optimization results for grappler item: rnn__decoder_gru_while_19_body_36369_grad_55116
2020-08-07 09:40:27.849422: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0.002ms.
2020-08-07 09:40:27.849432: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0ms.
2020-08-07 09:40:27.849442: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:816] Optimization results for grappler item: rnn__decoder_gru_while_12_body_34535_rewritten
2020-08-07 09:40:27.849453: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0.002ms.
2020-08-07 09:40:27.849462: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0ms.
2020-08-07 09:40:27.849471: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:816] Optimization results for grappler item: rnn__decoder_gru_while_13_cond_34796_rewritten
2020-08-07 09:40:27.849481: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0.002ms.
2020-08-07 09:40:27.849491: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0ms.
2020-08-07 09:40:27.849500: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:816] Optimization results for grappler item: rnn__decoder_gru_while_20_body_36631_grad_54785
2020-08-07 09:40:27.849509: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0.002ms.
2020-08-07 09:40:27.849517: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0.001ms.
2020-08-07 09:40:27.849526: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:816] Optimization results for grappler item: rnn__decoder_gru_while_23_body_37417_rewritten
2020-08-07 09:40:27.849534: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0.002ms.
2020-08-07 09:40:27.849543: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0ms.
2020-08-07 09:40:27.849551: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:816] Optimization results for grappler item: rnn__decoder_gru_while_37_body_41085_rewritten
2020-08-07 09:40:27.849560: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0.002ms.
2020-08-07 09:40:27.849570: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0.001ms.
2020-08-07 09:40:27.849580: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:816] Optimization results for grappler item: rnn__decoder_gru_while_18_body_36107_rewritten
2020-08-07 09:40:27.849590: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0.002ms.
2020-08-07 09:40:27.849599: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0ms.
2020-08-07 09:40:27.849607: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:816] Optimization results for grappler item: rnn__decoder_gru_while_28_body_38727_rewritten
2020-08-07 09:40:27.849616: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0.002ms.
2020-08-07 09:40:27.849624: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0ms.
2020-08-07 09:40:27.849636: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:816] Optimization results for grappler item: rnn__decoder_gru_while_2_body_31915_grad_60746
2020-08-07 09:40:27.849646: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0.001ms.
2020-08-07 09:40:27.849656: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0.001ms.
2020-08-07 09:40:27.849664: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:816] Optimization results for grappler item: rnn__decoder_gru_while_4_cond_32438_grad_60260
2020-08-07 09:40:27.849675: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0.002ms.
2020-08-07 09:40:27.849684: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0.001ms.
2020-08-07 09:40:27.849693: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:816] Optimization results for grappler item: rnn__decoder_gru_while_32_body_39775_rewritten
2020-08-07 09:40:27.849702: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0.002ms.
2020-08-07 09:40:27.849711: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0ms.
2020-08-07 09:40:27.849721: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:816] Optimization results for grappler item: rnn__decoder_gru_while_24_body_37679_grad_53461
2020-08-07 09:40:27.849732: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0.002ms.
2020-08-07 09:40:27.849742: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0ms.
2020-08-07 09:40:27.849751: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:816] Optimization results for grappler item: rnn__decoder_gru_while_8_body_33487_rewritten
2020-08-07 09:40:27.849761: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0.001ms.
2020-08-07 09:40:27.849771: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0ms.
2020-08-07 09:40:27.849782: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:816] Optimization results for grappler item: rnn__decoder_gru_while_6_body_32963_rewritten
2020-08-07 09:40:27.849790: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0.001ms.
2020-08-07 09:40:27.849799: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0.001ms.
2020-08-07 09:40:27.849808: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:816] Optimization results for grappler item: rnn__decoder_gru_while_21_cond_36892_rewritten
2020-08-07 09:40:27.849816: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0.001ms.
2020-08-07 09:40:27.849827: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0ms.
2020-08-07 09:40:27.849838: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:816] Optimization results for grappler item: rnn__decoder_gru_while_11_cond_34272_rewritten
2020-08-07 09:40:27.849846: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0.001ms.
2020-08-07 09:40:27.849855: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0ms.
2020-08-07 09:40:27.849863: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:816] Optimization results for grappler item: rnn__decoder_gru_while_40_body_41871_grad_48164
2020-08-07 09:40:27.849872: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0.002ms.
2020-08-07 09:40:27.849881: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0.001ms.
2020-08-07 09:40:27.849890: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:816] Optimization results for grappler item: rnn__decoder_gru_while_17_cond_35844_grad_55954
2020-08-07 09:40:27.849901: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0.002ms.
2020-08-07 09:40:27.849911: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0ms.
2020-08-07 09:40:27.849920: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:816] Optimization results for grappler item: rnn__decoder_gru_while_1_cond_31652_grad_61253
2020-08-07 09:40:27.849929: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0.001ms.
2020-08-07 09:40:27.849939: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0.001ms.
2020-08-07 09:40:27.849947: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:816] Optimization results for grappler item: rnn__decoder_gru_while_25_cond_37940_rewritten
2020-08-07 09:40:27.849957: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0.001ms.
2020-08-07 09:40:27.849967: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0ms.
2020-08-07 09:40:27.849978: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:816] Optimization results for grappler item: rnn__decoder_gru_while_8_cond_33486_rewritten
2020-08-07 09:40:27.849988: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0.002ms.
2020-08-07 09:40:27.850000: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0ms.
2020-08-07 09:40:27.850010: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:816] Optimization results for grappler item: rnn__decoder_gru_while_3_body_32177_grad_60415
2020-08-07 09:40:27.850020: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0.002ms.
2020-08-07 09:40:27.850030: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0ms.
2020-08-07 09:40:27.850041: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:816] Optimization results for grappler item: rnn__decoder_gru_while_11_cond_34272_grad_57941
2020-08-07 09:40:27.850049: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0.002ms.
2020-08-07 09:40:27.850058: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0ms.
2020-08-07 09:40:27.850068: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:816] Optimization results for grappler item: rnn__decoder_gru_while_10_body_34011_grad_58096
2020-08-07 09:40:27.850077: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0.002ms.
2020-08-07 09:40:27.850085: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0ms.
2020-08-07 09:40:27.850094: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:816] Optimization results for grappler item: rnn__decoder_gru_while_14_cond_35058_grad_56948
2020-08-07 09:40:27.850109: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0.002ms.
2020-08-07 09:40:27.850120: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0ms.
2020-08-07 09:40:27.850130: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:816] Optimization results for grappler item: rnn__decoder_gru_while_35_cond_40560_rewritten
2020-08-07 09:40:27.850139: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0.002ms.
2020-08-07 09:40:27.850148: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0ms.
2020-08-07 09:40:27.850160: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:816] Optimization results for grappler item: rnn__decoder_gru_while_31_cond_39512_rewritten
2020-08-07 09:40:27.850170: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0.001ms.
2020-08-07 09:40:27.850181: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0.001ms. function_optimizer: function_optimizer did nothing. time = 0.001ms.
2020-08-07 09:40:27.855389: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:818] function_optimizer: function_optimizer did nothing. time = 0ms.
Traceback (most recent call last):
File "convert2savedmodel.py", line 280, in <module>
tflite_model = converter.convert()
File "/share/nishome/19930072_0/miniconda3/envs/tf2.1/lib/python3.7/site-packages/tensorflow/lite/python/lite.py", line 1076, in convert
return super(TFLiteConverterV2, self).convert()
File "/share/nishome/19930072_0/miniconda3/envs/tf2.1/lib/python3.7/site-packages/tensorflow/lite/python/lite.py", line 878, in convert
self._funcs[0], lower_control_flow=False))
File "/share/nishome/19930072_0/miniconda3/envs/tf2.1/lib/python3.7/site-packages/tensorflow/python/framework/convert_to_constants.py", line 1103, in convert_variables_to_constants_v2_as_graph
aggressive_inlining=aggressive_inlining)
File "/share/nishome/19930072_0/miniconda3/envs/tf2.1/lib/python3.7/site-packages/tensorflow/python/framework/convert_to_constants.py", line 804, in __init__
self._build_tensor_data()
File "/share/nishome/19930072_0/miniconda3/envs/tf2.1/lib/python3.7/site-packages/tensorflow/python/framework/convert_to_constants.py", line 823, in _build_tensor_data
data = val_tensor.numpy()
File "/share/nishome/19930072_0/miniconda3/envs/tf2.1/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 1063, in numpy
maybe_arr = self._numpy() # pylint: disable=protected-access
File "/share/nishome/19930072_0/miniconda3/envs/tf2.1/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 1031, in _numpy
six.raise_from(core._status_to_exception(e.code, e.message), None) # pylint: disable=protected-access
File "<string>", line 3, in raise_from
tensorflow.python.framework.errors_impl.InvalidArgumentError: Cannot convert a Tensor of dtype resource to a NumPy array.
Также, пожалуйста, укажите ссылку на сохраненную модель или GraphDef
@tf.function
def train_step(img_tensor, target):
loss = 0
#初始化每个批次的隐藏状态因为图像与图像的字幕之间没有关系
hidden = decoder.reset_states(batch_size=target.shape[0])
dec_input = tf.expand_dims([tokenizer.word_index['<start>']] * target.shape[0], 1)
with tf.GradientTape() as tape:
features = encoder(img_tensor)
for i in range(1, target.shape[1]):
#通过解码器传递特征
predictions, hidden, _ = decoder(dec_input, features, hidden)
loss += loss_function(target[:, i], predictions)
# 使用 teacher forcing
dec_input = tf.expand_dims(target[:, i], 1)
total_loss = (loss / int(target.shape[1]))
trainable_variables = encoder.trainable_variables + decoder.trainable_variables
gradients = tape.gradient(loss, trainable_variables)
optimizer.apply_gradients(zip(gradients, trainable_variables))
return loss, total_loss
полный код
class BahdanauAttention(tf.keras.Model):
def __init__(self, utils):
super(BahdanauAttention, self).__init__()
self.W1 = tf.keras.layers.Dense(utils)
self.W2 = tf.keras.layers.Dense(utils)
self.V = tf.keras.layers.Dense(1)
def call(self, features, hidden):
# features(CNN_encoder output) shape == (batch_size, 64, embedding_dim)
# hidden shape == (batch_size, hidden_size)
# hidden_with_time_axis shape == (batch_size, 1, hidden_size)
hidden_with_time_axis_shape = tf.expand_dims(hidden, 1)
# score shape == (batch_size, 64, hidden_size)
score = tf.nn.tanh(self.W1(features) + self.W2(hidden_with_time_axis_shape))
# attention_weights shape == (batch_size, 64, 1)
# you get 1 at the last axis because you are applying score to self.V
attention_weights = tf.nn.softmax(self.V(score), axis=1)
# context_vector shape after sum == (batch_size, hidden_size)
context_vector = attention_weights * features
context_vector = tf.reduce_sum(context_vector, axis=1)
return context_vector, attention_weights
class CNN_Encoder(tf.keras.Model):
#由于您已经提取了特征并使用pickle进行了转储
#该编码器通过完全连接的层传递这些特征
def __init__(self, embedding):
super(CNN_Encoder, self).__init__()
# shape after fc == (batch_size, 64, embedding_dim)
self.fc = tf.keras.layers.Dense(embedding_dim)
def call(self, x):
x = self.fc(x)
x = tf.nn.relu(x)
return x
class RNN_Decoder(tf.keras.Model):
def __init__(self, embedding_dim, units, vocab_size):
super(RNN_Decoder, self).__init__()
self.units = units
self.embedding = tf.keras.layers.Embedding(vocab_size, embedding_dim)
self.gru = tf.keras.layers.GRU(self.units,
return_sequences=True,
return_state=True,
recurrent_initializer='glorot_uniform')
self.fc1 = tf.keras.layers.Dense(self.units)
self.fc2 = tf.keras.layers.Dense(vocab_size)
self.attention = BahdanauAttention(self.units)
def call(self, x , features, hidden):
#将注意力定义为一个单独的模型
context_vector, attention_weights = self.attention(features, hidden)
#x shape after passing through embedding == (batch_size, 1, embedding_dim)
x = self.embedding(x)
#x shape after concatenation == (batch_size, 1, embedding_dim + hidden_size)
x = tf.concat([tf.expand_dims(context_vector, 1), x], axis=-1)
#将concated后的的向量传递给GRU
output, state = self.gru(x)
#shape == (batch_size, max_length, hidden_size)
x = self.fc1(output)
#x shape == (batch_size, max_length, hidden_size)
x = tf.reshape(x, (-1, x.shape[2]))
# output shape == (batch_size * max_length, vocab)
x = self.fc2(x)
return x, state, attention_weights
def reset_states(self, batch_size):
return tf.zeros((batch_size, self.units))
encoder = CNN_Encoder(embedding_dim)
decoder = RNN_Decoder(embedding_dim, units, vocab_size)
optimizer = tf.keras.optimizers.Adam()
loss_object = tf.keras.losses.SparseCategoricalCrossentropy(
from_logits=True, reduction='none'
)
def loss_function(real, pred):
mask = tf.math.logical_not(tf.math.equal(real, 0))
loss_ = loss_object(real, pred)
mask = tf.cast(mask, dtype=loss_.dtype)
loss_ *= mask
return tf.reduce_mean(loss_)
#CheckPoint
checkpoint_path = './checkpoints/train'
ckpt = tf.train.Checkpoint(encoder=encoder,
decoder=decoder,
optimizer = optimizer)
ckpt_manage = tf.train.CheckpointManager(ckpt, checkpoint_path, max_to_keep=5)
start_epoch = 0
if ckpt_manage.latest_checkpoint:
start_epoch = int(ckpt_manage.latest_checkpoint.split('-')[-1])
print(start_epoch)
#恢复checkpoint_path中的最新检查点
ckpt.restore(ckpt_manage.latest_checkpoint)
BATCH_SIZE = 128
BUFFER_SIZE = 1000
embedding_dim = 256
units = 512
vocab_size = top_k + 1
num_steps = len(img_name_train) // BATCH_SIZE
# 从InceptionV3提取的向量的形状为(64,2048)
# 这两个变量表示矢量形状
features_shape = 2048
attention_features_shape = 64