以下のコードを実行した所、モデルの学習の所で、
ValueError: Input 0 of layer dense is incompatible with the layer: expected axis -1 of input shape to have value 2025 but received input with shape (None, 6075)
が発生してしまいます。
ネットワークの入力を、
python
1input_shape=(45, 45,3)
とした所、当初のエラーは解消されたように見えます。
今度は、
tensorflow.python.framework.errors_impl.InvalidArgumentError: logits and labels must have the same first dimension, got logits shape [10,10] and labels shape [100]
というエラーが出ます。
CNNをはじめて実装したので,原因がよくわかっていません。
わかる方.宜しくお願い致します。
データセット作成で使用している画像(カラー)(45×45×3)
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 121
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 15#train_images ,train_labels, test_images ,test_labels = image_dataset.make('C:/Users/dataset',img_size = 28, train_num=10000 , test_num =1000) 16train_images ,train_labels, test_images ,test_labels = image_dataset.make('C:/Users/dataset',img_size = 45, train_num=10000 , test_num =1000) 17 18train_images.shape 19 20#print(train_images.shape, train_labels.shape) 21print('train_images.shape:') 22print(train_images.shape) 23 24#plt.figure() 25#plt.imshow(train_images[0], cmap='gray') 26#plt.colorbar() 27#plt.grid(False) 28#試しに表示 29#plt.show() 30 31len(train_labels) 32train_labels 33test_images.shape 34len(test_labels) 35 36#データの前処理 37#plt.figure() 38#plt.imshow(train_images[0]) 39#plt.colorbar() 40#plt.grid(False) 41#plt.show() 42 43train_images = train_images / 255.0 44test_images = test_images / 255.0 45 46#model = keras.Sequential([ 47# keras.layers.Flatten(input_shape=(32, 32, 3)), 48# keras.layers.Dense(128, activation='relu'), 49# keras.layers.Dense(10, activation='softmax') 50#]) 51 52model = tf.keras.Sequential([ 53 tf.keras.layers.Flatten(input_shape=(45, 45,3), name='flatten_layer'), 54 tf.keras.layers.Dense(128, activation='relu'), 55 tf.keras.layers.Dropout(0.2), 56 tf.keras.layers.Dense(10, activation='softmax') 57]) 58 59#model.summary() 60 61#モデルのコンパイル 62model.compile(optimizer='adam', 63 loss='sparse_categorical_crossentropy', 64 metrics=['accuracy']) 65#モデルの訓練 66model.fit(train_images, train_labels, epochs=5) 67 68# save model 69model.save("model_cnn.h5") 70 71test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2) 72#正解率の評価 73print('\nTest accuracy:', test_acc) 74 75print("end")
実行結果
2021-02-19 13:07:03.954106: 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-19 13:07:03.956781: 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 train_images.shape: (10, 6075) 2021-02-19 13:07:06.893626: I tensorflow/compiler/jit/xla_cpu_device.cc:41] Not creating XLA devices, tf_xla_enable_xla_devices not set ~中略~ 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:66) ]] [Op:__inference_train_ function_571] Function call stack: train_function
print(train_images.shape)の結果
(10, 6075)
print(train_labels.shape)の結果
(10,10)
Tensorflow2.4.1
Python3.7