前提・実現したいこと
python3.8
tensorflow_gpu=2.3
Keras=2.4.3
を使っています
エラーの状況がよく把握しておらずわかりにくい質問で申し訳ありませんがよろしくお願い致します。
発生している問題・エラーメッセージ
2021-07-07 12:55:34.765009: W tensorflow/core/framework/cpu_allocator_impl.cc:81] Allocation of 2267728000 exceeds 10% of free system memory.
Epoch 1/10
WARNING:tensorflow:Model was constructed with shape (None, 4096) for input Tensor("input_2:0", shape=(None, 4096), dtype=float32), but it was called on an input with incompatible shape (None, 1000).
Traceback (most recent call last):
File "/home/limlab/image_captioning/c2.py", line 245, in <module>
model.fit([X1train, X2train], ytrain, epochs=10, verbose=2, callbacks=[checkpoint], validation_data=([X1val, X2val], yval))
File "/home/limlab/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py", line 108, in _method_wrapper
return method(self, *args, **kwargs)
File "/home/limlab/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py", line 1098, in fit
tmp_logs = train_function(iterator)
File "/home/limlab/.local/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py", line 780, in call
result = self._call(*args, **kwds)
File "/home/limlab/.local/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py", line 823, in _call
self._initialize(args, kwds, add_initializers_to=initializers)
File "/home/limlab/.local/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py", line 696, in _initialize
self._stateful_fn._get_concrete_function_internal_garbage_collected( # pylint: disable=protected-access
File "/home/limlab/.local/lib/python3.8/site-packages/tensorflow/python/eager/function.py", line 2855, in _get_concrete_function_internal_garbage_collected
graph_function, _, _ = self._maybe_define_function(args, kwargs)
File "/home/limlab/.local/lib/python3.8/site-packages/tensorflow/python/eager/function.py", line 3213, in _maybe_define_function
graph_function = self._create_graph_function(args, kwargs)
File "/home/limlab/.local/lib/python3.8/site-packages/tensorflow/python/eager/function.py", line 3065, in _create_graph_function
func_graph_module.func_graph_from_py_func(
File "/home/limlab/.local/lib/python3.8/site-packages/tensorflow/python/framework/func_graph.py", line 986, in func_graph_from_py_func
func_outputs = python_func(*func_args, **func_kwargs)
File "/home/limlab/.local/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py", line 600, in wrapped_fn
return weak_wrapped_fn().wrapped(*args, **kwds)
File "/home/limlab/.local/lib/python3.8/site-packages/tensorflow/python/framework/func_graph.py", line 973, in wrapper
raise e.ag_error_metadata.to_exception(e)
ValueError: in user code:
/home/limlab/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:806 train_function * return step_function(self, iterator) /home/limlab/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:796 step_function ** outputs = model.distribute_strategy.run(run_step, args=(data,)) /home/limlab/.local/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py:1211 run return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs) /home/limlab/.local/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py:2585 call_for_each_replica return self._call_for_each_replica(fn, args, kwargs) /home/limlab/.local/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py:2945 _call_for_each_replica return fn(*args, **kwargs) /home/limlab/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:789 run_step ** outputs = model.train_step(data) /home/limlab/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:747 train_step y_pred = self(x, training=True) /home/limlab/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py:985 __call__ outputs = call_fn(inputs, *args, **kwargs) /home/limlab/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/functional.py:385 call return self._run_internal_graph( /home/limlab/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/functional.py:508 _run_internal_graph outputs = node.layer(*args, **kwargs) /home/limlab/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py:975 __call__ input_spec.assert_input_compatibility(self.input_spec, inputs, /home/limlab/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/input_spec.py:212 assert_input_compatibility raise ValueError( ValueError: Input 0 of layer dense is incompatible with the layer: expected axis -1 of input shape to have value 4096 but received input with shape [None, 1000]
from tensorflow.keras.preprocessing.sequence import pad_sequences from tensorflow.keras.utils import to_categorical from numpy import array #画像と出力単語を紐づける関数 def create_sequences(tokenizer, max_length, descriptions, photos): X1, X2, y = list(), list(), list()#X1が入力画像、X2が入力語、yがX1とX2に対応する出力語 #各画像名でループ for key, desc_list in descriptions.items(): #各画像のキャプションでループ for desc in desc_list: #シーケンスをエンコードする seq = tokenizer.texts_to_sequences([desc])[0] #1つのシーケンスを複数のX、Yペアに分割する for i in range(1, len(seq)): #入力と出力のペアに分割する in_seq, out_seq = seq[:i], seq[i] #行列のサイズを最大の単語数に合わせる in_seq = pad_sequences([in_seq], maxlen=max_length)[0] #出力シーケンス out_seq = to_categorical([out_seq], num_classes=vocab_size)[0] #全てをarrayに格納 X1.append(photos[key][0]) X2.append(in_seq) y.append(out_seq) return array(X1), array(X2), array(y) #トレーニングデータの入力画像、入力語、出力語を紐付ける X1train, X2train, ytrain = create_sequences(tokenizer, max_length, train_descriptions, train_features) print(X1train) #バリデーションデータの入力画像、入力語、出力語を紐付ける X1val, X2val, yval = create_sequences(tokenizer, max_length, val_descriptions, val_features) from tensorflow.keras.layers import Input,Dense,LSTM,Embedding,Dropout from keras.layers.merge import add #モデルを定義する関数 def define_model(vocab_size, max_length): #画像の特徴を入力するレイヤ inputs1 = Input(shape=(4096,)) fe1 = Dropout(0.5)(inputs1) fe2 = Dense(256, activation='relu')(fe1) #文章を入力するレイヤ inputs2 = Input(shape=(max_length,)) se1 = Embedding(vocab_size, 256, mask_zero=True)(inputs2) se2 = Dropout(0.5)(se1) se3 = LSTM(256)(se2) #上の二つの出力を統合する部分 decoder1 = add([fe2, se3]) decoder2 = Dense(256, activation='relu')(decoder1) outputs = Dense(vocab_size, activation='softmax')(decoder2) #モデルの定義.二つを入力にとって一つを出力する形になる model = Model(inputs=[inputs1, inputs2], outputs=outputs) model.compile(loss='categorical_crossentropy', optimizer='adam') return model from tensorflow.keras.callbacks import ModelCheckpoint #モデルの定義 model = define_model(vocab_size, max_length) #コールバックを定義する filepath = 'model-ep{epoch:03d}-loss{loss:.3f}-val_loss{val_loss:.3f}.h5' checkpoint = ModelCheckpoint(filepath, monitor='val_loss', verbose=1, save_best_only=True, mode='min') #学習 model.fit([X1train, X2train], ytrain, epochs=10, verbose=2, callbacks=[checkpoint], validation_data=([X1val, X2val], yval))
回答1件
あなたの回答
tips
プレビュー