前提・実現したいこと
Google Colabに搭載されているTPUを使用してEfficientNetモデルを学習したいです。
しかしながら、下記エラーが発生することにより上手くいきません。
どなたか解決方法をご教授頂ければ幸いです。
発生している問題・エラーメッセージ
--------------------------------------------------------------------------- InvalidArgumentError Traceback (most recent call last) <ipython-input-4-a67e6b797143> in <module>() 20 print('### START TRAINING ###') 21 ---> 22 history = model.fit(data_train, steps_per_epoch=TRAIN_STEPS_PER_EPOCH, epochs=EPOCS, validation_data=data_val, validation_steps=VAL_STEPS_PER_EPOCH) 23 24 print('### FINISH TRAINING ###') 5 frames /usr/local/lib/python3.6/dist-packages/six.py in raise_from(value, from_value) InvalidArgumentError: Unable to parse tensor proto
該当のソースコード
python
1import tensorflow as tf 2from keras.utils import to_categorical 3 4AUTOTUNE = tf.data.experimental.AUTOTUNE 5IMAGE_SIZE = 240 6TRAIN_IMAGE_COUNT = len(df_train) 7VAL_IMAGE_COUNT = len(df_val) 8BATCH_SIZE = 32 9 10def preprocess_image(path, label=None): 11 image = tf.io.read_file(path) 12 image = tf.image.decode_jpeg(image, channels=3) 13 image = tf.image.resize(image, [IMAGE_SIZE, IMAGE_SIZE]) 14 image = tf.cast(image, tf.float32)/255.0 15 16 return image, label 17 18data_train = tf.data.Dataset.from_tensor_slices((df_train['image_path'], df_train['label'])) 19data_train = data_train.map(preprocess_image) 20data_train = data_train.shuffle(buffer_size=TRAIN_IMAGE_COUNT) 21data_train = data_train.repeat() 22data_train = data_train.batch(BATCH_SIZE) 23data_train = data_train.prefetch(buffer_size=AUTOTUNE) 24data_train = data_train.cache() 25 26data_val = tf.data.Dataset.from_tensor_slices((df_val['image_path'], df_val['label'])) 27data_val = data_val.map(preprocess_image) 28data_val = data_val.shuffle(buffer_size=VAL_IMAGE_COUNT) 29data_val = data_val.repeat() 30data_val = data_val.batch(BATCH_SIZE) 31data_val = data_val.prefetch(buffer_size=AUTOTUNE) 32data_val = data_val.cache()
python
1%tensorflow_version 2.x 2import tensorflow as tf 3print("Tensorflow version " + tf.__version__) 4 5try: 6 tpu = tf.distribute.cluster_resolver.TPUClusterResolver() # TPU detection 7 print('Running on TPU ', tpu.cluster_spec().as_dict()['worker']) 8except ValueError: 9 raise BaseException('ERROR: Not connected to a TPU runtime; please see the previous cell in this notebook for instructions!') 10 11tf.config.experimental_connect_to_cluster(tpu) 12tf.tpu.experimental.initialize_tpu_system(tpu) 13strategy = tf.distribute.TPUStrategy(tpu)
python
1from tensorflow.keras.applications import EfficientNetB1 2from tensorflow.keras.layers import GlobalAveragePooling2D, Dense, Dropout 3from tensorflow.keras import optimizers 4 5TRAIN_STEPS_PER_EPOCH = tf.math.ceil(len(df_train)/BATCH_SIZE).numpy() 6VAL_STEPS_PER_EPOCH = tf.math.ceil(len(df_val)/BATCH_SIZE).numpy() 7EPOCS = 1 8 9with strategy.scope(): 10 model = tf.keras.Sequential() 11 12 conv_base = EfficientNetB1(weights='imagenet', input_shape=(IMAGE_SIZE, IMAGE_SIZE, 3), include_top=False) 13 14 model.add(conv_base) 15 model.add(GlobalAveragePooling2D()) 16 model.add(Dense(5, activation='softmax')) 17 18 model.compile(loss='sparse_categorical_crossentropy', optimizer=optimizers.Adagrad(), metrics=['accuracy']) 19 20print('### START TRAINING ###') 21 22history = model.fit(data_train, steps_per_epoch=TRAIN_STEPS_PER_EPOCH, epochs=EPOCS, validation_data=data_val, validation_steps=VAL_STEPS_PER_EPOCH) 23 24print('### FINISH TRAINING ###')
試したこと
正直なところ、初めのdatasetを構築するところの意味があまり理解出来ていなが、下記を試した。
効果は得ることが出来なかった。
image = tf.cast(image, tf.float16)/255.0
補足情報(FW/ツールのバージョンなど)
Google Colab
Tensorflow version 2.4.0
あなたの回答
tips
プレビュー