以下のコードを実行した所、モデルの学習の所で、
tensorflow.python.framework.errors_impl.InvalidArgumentError
が発生してしまいます。
原因と対処方法をご教示頂けると幸いです。
Python
1import sys , os 2import random 3 4import numpy as np 5import cv2 6 7 8# 画像をランダムの位置で切り抜くプログラム 9def random_clip(img , clip_size , num = 10): 10 clip_images = [] 11 height, width = img.shape[:2] 12 13 # 画像をclip_sizeサイズごとにnum回切り抜く 14 for y in range( num ): 15 rand_y = random.randint(0,height - clip_size) 16 rand_x = random.randint(0,width - clip_size) 17 clip_img = img[ rand_y : rand_y + clip_size, rand_x : rand_x + clip_size] 18 clip_img = clip_img.flatten().astype(np.float32)/255.0 19 clip_images.append(clip_img) 20 21 return clip_images 22 23 24# データセットから画像とラベルをランダムに取得 25def random_sampling( images , labels , train_num , test_num = 0 ): 26 image_train_batch = [] 27 label_train_batch = [] 28 29 #乱数を発生させ,リストを並び替える. 30 random_seq = list(range(len(images))) 31 random.shuffle(random_seq) 32 33 # バッチサイズ分画像を選択 34 image_train_batch = images[ :train_num ] 35 label_train_batch = labels[ :train_num ] 36 37 if test_num == 0: # 検証用データの指定がないとき 38 return image_train_batch , label_train_batch 39 else: 40 image_test_batch = images[ train_num : train_num + test_num ] 41 label_test_batch = labels[ train_num : train_num + test_num ] 42 return image_train_batch , label_train_batch , image_test_batch , label_test_batch 43 44 45# フォルダーの画像をランダム位置でクリップした後にリサイズして読み込む 46def make( folder_name , img_size = 0 , clip_num = 0 , clip_size = 0 ,train_num = 0 , test_num = 0 ): 47 train_image = [] 48 test_image = [] 49 train_label = [] 50 test_label= [] 51 52 # フォルダ内のディレクトリの読み込み 53 classes = os.listdir( folder_name ) 54 55 for i, d in enumerate(classes): 56 files = os.listdir( folder_name + '/' + d ) 57 58 tmp_image = [] 59 tmp_label = [] 60 for f in files: 61 # 1枚の画像に対する処理 62 if not 'jpg' in f:# jpg以外のファイルは無視 63 continue 64 65 # 画像読み込み 66 img = cv2.imread( folder_name+ '/' + d + '/' + f) 67 # one_hot_vectorを作りラベルとして追加 68 label = np.zeros(len(classes)) 69 if d == "0": 70 label[i] = 0 71 elif d == "1": 72 label[i] = 1 73 else: 74 label[i] = 0 75 76 # リサイズをする処理 77 if img_size != 0: 78 img = cv2.resize( img , (img_size , img_size )) 79 img = img.flatten().astype(np.float32)/255.0 80 tmp_image.append(img) 81 tmp_label.append(label) 82 elif clip_size != 0 and clip_num != 0: 83 img = random_clip( img , clip_size , clip_num) 84 tmp_image.extend( img ) 85 for j in range(clip_num): 86 tmp_label.append(label) 87 else: 88 img = img.flatten().astype(np.float32)/255.0 89 tmp_image.append(img) 90 tmp_label.append(label) 91 92 93 # データセットのサイズが指定されているときはその枚数分ランダムに抽出する 94 if train_num == 0 : 95 train_image.extend( tmp_image ) 96 train_label.extend( tmp_label ) 97 # テスト画像の指定がないとき 98 elif test_num == 0 : 99 sampled_image , sampled_label = random_sampling( tmp_image , tmp_label , train_num ) 100 train_image.extend( sampled_image ) 101 train_label.extend( sampled_label ) 102 else : 103 sampled_train_image , sampled_train_label , sampled_test_image , sampled_test_label = random_sampling( tmp_image , tmp_label , train_num , test_num ) 104 train_image.extend( sampled_train_image ) 105 train_label.extend( sampled_train_label ) 106 test_image.extend( sampled_test_image ) 107 test_label.extend( sampled_test_label ) 108 109 print(d + 'read complete ,' + str(len(train_label)) + ' pictures exit') 110 111 # numpy配列に変換 112 train_image = np.asarray( train_image ) 113 train_label = np.asarray( train_label ) 114 115 if test_num != 0: #testデータセットがあるときは返り値が変わる 116 test_image = np.asarray( test_image ) 117 test_label = np.asarray( test_label ) 118 return train_image , train_label , test_image , test_label 119 120 return train_image , train_label
Python
1# TensorFlow と tf.keras のインポート 2import tensorflow as tf 3from tensorflow import keras 4 5# ヘルパーライブラリのインポート 6import numpy as np 7import matplotlib.pyplot as plt 8 9print(tf.__version__) 10 11#mnist = keras.datasets.mnist 12 13#(train_images, train_labels), (test_images, test_labels) = mnist.load_data() 14import image_dataset 15train_images ,train_labels, test_images ,test_labels = image_dataset.make('C:/Users/dataset',img_size = 32, train_num=10000 , test_num =1000) 16 17train_images.shape 18#print(train_images.shape, train_labels.shape)の結果 19print(train_images.shape, train_labels.shape) 20 21#plt.figure() 22#plt.imshow(train_images[0], cmap='gray') 23#plt.colorbar() 24#plt.grid(False) 25#試しに表示 26#plt.show() 27 28len(train_labels) 29train_labels 30test_images.shape 31len(test_labels) 32 33#データの前処理 34#plt.figure() 35#plt.imshow(train_images[0]) 36#plt.colorbar() 37#plt.grid(False) 38#plt.show() 39 40train_images = train_images / 255.0 41test_images = test_images / 255.0 42 43model = keras.Sequential([ 44 keras.layers.Flatten(input_shape=(32, 32, 3)), 45 keras.layers.Dense(128, activation='relu'), 46 keras.layers.Dense(10, activation='softmax') 47]) 48#モデルのコンパイル 49model.compile(optimizer='adam', 50 loss='sparse_categorical_crossentropy', 51 metrics=['accuracy']) 52#モデルの訓練 53model.fit(train_images, train_labels, epochs=5) 54 55# save model 56model.save("model_cnn.h5") 57 58test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2) 59#正解率の評価 60print('\nTest accuracy:', test_acc) 61 62print("end")
実行結果
2021-02-17 11:33:13.182244: W tensorflow/stream_executor/platform/default/dso_loader.cc:60] Could not load dynamic library 'cudart64_110.dll'; dlerror: cudart64_110.dll not f ound 2021-02-17 11:33:13.192100: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine. 2.4.1 0read complete ,1 pictures exit 1read complete ,2 pictures exit (10, 3072) (10, 10) 2021-02-17 11:33:18.164654: I tensorflow/compiler/jit/xla_cpu_device.cc:41] Not creating XLA devices, tf_xla_enable_xla_devices not set ~中略~ 2021-02-17 11:33:18.459982: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:116] None of the MLIR optimization passes are enabled (registered 2) Epoch 1/5 WARNING:tensorflow:Model was constructed with shape (None, 32, 32, 3) for input KerasTensor(type_spec=TensorSpec(shape=(None, 32, 32, 3), dtype=tf.float32, name='flatten_inpu t'), name='flatten_input', description="created by layer 'flatten_input'"), but it was called on an input with incompatible shape (None, 3072). WARNING:tensorflow:Model was constructed with shape (None, 32, 32, 3) for input KerasTensor(type_spec=TensorSpec(shape=(None, 32, 32, 3), dtype=tf.float32, name='flatten_inpu t'), name='flatten_input', description="created by layer 'flatten_input'"), but it was called on an input with incompatible shape (None, 3072). Traceback (most recent call last): File "cnn_image.py", line 53, in <module> model.fit(train_images, train_labels, epochs=5) File "C:\Users\anaconda3\envs\tensorflow-cpu\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1100, in fit tmp_logs = self.train_function(iterator) File "C:\Users\anaconda3\envs\tensorflow-cpu\lib\site-packages\tensorflow\python\eager\def_function.py", line 828, in __call__ result = self._call(*args, **kwds) File "C:\Users\anaconda3\envs\tensorflow-cpu\lib\site-packages\tensorflow\python\eager\def_function.py", line 888, in _call return self._stateless_fn(*args, **kwds) File "C:\Users\anaconda3\envs\tensorflow-cpu\lib\site-packages\tensorflow\python\eager\function.py", line 2942, in __call__ return graph_function._call_flat( File "C:\Users\anaconda3\envs\tensorflow-cpu\lib\site-packages\tensorflow\python\eager\function.py", line 1918, in _call_flat return self._build_call_outputs(self._inference_function.call( File "C:\Users\anaconda3\envs\tensorflow-cpu\lib\site-packages\tensorflow\python\eager\function.py", line 555, in call outputs = execute.execute( File "C:\Users\anaconda3\envs\tensorflow-cpu\lib\site-packages\tensorflow\python\eager\execute.py", line 59, in quick_execute tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name, tensorflow.python.framework.errors_impl.InvalidArgumentError: logits and labels must have the same first dimension, got logits shape [10,10] and labels shape [100] [[node sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits (defined at cnn_image.py:53) ]] [Op:__inference_train_ function_536] Function call stack: train_function
Tensorflow2.4.1
Python3.7
データセットに使用している画像はRGBカラー画像です。
model.summary()の結果
Model: "sequential" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= flatten_layer (Flatten) (None, 2352) 0 _________________________________________________________________ dense (Dense) (None, 128) 301184 _________________________________________________________________ dropout (Dropout) (None, 128) 0 _________________________________________________________________ dense_1 (Dense) (None, 10) 1290 ================================================================= Total params: 302,474 Trainable params: 302,474 Non-trainable params: 0
あなたの回答
tips
プレビュー