質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.49%
機械学習

機械学習は、データからパターンを自動的に発見し、そこから知能的な判断を下すためのコンピューターアルゴリズムを指します。人工知能における課題のひとつです。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

1回答

4423閲覧

Tensorflowによるモデル学習が上手くいきません。

tanuki_

総合スコア31

機械学習

機械学習は、データからパターンを自動的に発見し、そこから知能的な判断を下すためのコンピューターアルゴリズムを指します。人工知能における課題のひとつです。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2021/02/19 02:03

編集2021/02/24 04:01

以下のコードを実行した所、モデルの学習の所で、
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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

meg_

2021/02/19 02:43

訓練用の画像はグレースケールですか?
tanuki_

2021/02/19 03:15

はい。グレースケールです。
jbpb0

2021/02/19 03:28

print(train_images.shape) の結果は、どうなりますか?
tanuki_

2021/02/19 03:41

(10, 6075)となりました。
tanuki_

2021/02/19 04:37

45×45x3=6075ですね。 失礼しました。
jbpb0

2021/02/19 04:55

x3ということは、カラーなのですかね? データは実質グレーでも、データ形式としてはカラー(3チャンネル)になっていて、三つのチャンネルに同じ値が入ってる?? もしそうなら、ネットワークの入力は input_shape=(45, 45) なので、x3の分が考慮されてないような
tanuki_

2021/02/19 05:12

本来は2チャンネルでやりたいのですが方法が分からない状態です。 x3の考慮をして動かしてみます。
aipy2020

2021/02/19 16:46

print(train_labels.shape) はどうなりますか? labelが0か1の整数なら(10, 1)、クラス数が10個なら(10,10)になると思います。
tanuki_

2021/02/19 23:29

(10, 10)が返ってきました。
jbpb0

2021/02/20 02:38

それだと loss='sparse_categorical_crossentropy' と矛盾してると思います
tanuki_

2021/02/20 05:05

すみません。 具体的にどういう事でしょうか? 調べても良く理解出来ませんでした。。
jbpb0

2021/02/20 09:27

私の 2021/02/19 20:00 のコメントに書いた参考Webページを見てください
tanuki_

2021/02/20 09:36

分かりました。 見てみます。 ありがとうございます。
toast-uz

2021/02/20 22:43

質問とは直接関係ありませんが「CNNをはじめて実装した」→Conv層があってはじめてCNNと呼べますので、現段階ではCNNとは呼べません。
jbpb0

2021/02/21 05:47

TensorFlowのチュートリアル はじめてのニューラルネットワーク:分類問題の初歩 https://www.tensorflow.org/tutorials/keras/classification?hl=ja でも、model.compile()で、質問者さんのコードと同じ loss='sparse_categorical_crossentropy' としてますよね では、上記チュートリアルではラベルはどうなっているか確認するために、それの、 model.fit(train_images, train_labels, epochs=5) の直前に、 print(train_labels.shape) print(train_labels) を追記してgoogleコラボで実行したら、結果は下記のようになります (やってみてください) (60000,) [9 0 0 ... 3 0 5] つまり、ラベルのshapeは「(サンプル数,)」で、ラベルは分類したい整数の値(上記チュートリアルでは0〜9)です 一方、質問者さんのコードでは、ラベルのshapeは > (10, 10)が返ってきました。 でしたから、違いますよね また、ラベルの値も、make()でonehotにしてますから、0か1かのどちらかで、やはりチュートリアルとは違いますよね loss='sparse_categorical_crossentropy' とするのなら、ラベルの形式を上記チュートリアルのようにしなければいけません あるいは、ラベルの形式は変えず、 loss='sparse_categorical_crossentropy' ではなく、 loss='categorical_crossentropy' を使うか
guest

回答1

0

python

1print(train_images.shape)

の結果「(10, 6075)」と、

python

1input_shape=(45, 45)

は、矛盾してると思います

また、

python

1print(train_labels.shape)

の結果「(10,10)」と、

python

1loss='sparse_categorical_crossentropy'

は、矛盾してると思います

投稿2021/02/28 10:20

jbpb0

総合スコア7651

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

tanuki_

2021/03/01 05:23

ご指摘ありがとうございます。 現在病気療養中の為復帰出来次第必ず完成させます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.49%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問