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

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

新規登録して質問してみよう
ただいま回答率
85.48%
Keras

Kerasは、TheanoやTensorFlow/CNTK対応のラッパーライブラリです。DeepLearningの数学的部分を短いコードでネットワークとして表現することが可能。DeepLearningの最新手法を迅速に試すことができます。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Python

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

Q&A

0回答

833閲覧

Python3 4次元のデータセットが3次元として認識されてしまう

SuzuAya

総合スコア71

Keras

Kerasは、TheanoやTensorFlow/CNTK対応のラッパーライブラリです。DeepLearningの数学的部分を短いコードでネットワークとして表現することが可能。DeepLearningの最新手法を迅速に試すことができます。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Python

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

0グッド

0クリップ

投稿2019/06/06 08:57

編集2019/06/06 11:21

前提・実現したいこと

kerasのImageDataGeneratorを使って各サンプルごとの平均を0にする処理を行いたいのですが、
以下の通り、データセットの次元が4次元であるべきところ、3次元であるというエラーが発生してしまいます。
しかし、ndimを使ってデータセットの次元を確認すると、ちゃんと4次元になっています。
1次元(サンプルの数)の部分をうまく認識してくれていないようなのですが、このような場合、どうしたら4次元として読み込んでもらえるか、アドバイスをいただけないでしょうか。

発生している問題・エラーメッセージ

ValueError Traceback (most recent call last) <ipython-input-19-2f1bdd86e710> in <module>() 186 #各サンプルの平均を0にする 187 train_datagen = ImageDataGenerator(samplewise_center=True) --> 188 train_generator = train_datagen.flow(x_train, None, batch_size=32) 189 190 validation_datagen = ImageDataGenerator(samplewise_center=True) ValueError: ('Input data in `NumpyArrayIterator` should have rank 4. You passed an array with shape', (512, 496, 1))

該当のソースコード

Python

1from __future__ import absolute_import 2from __future__ import division 3from __future__ import print_function 4 5from keras.layers import Lambda, Input, Dense 6from keras.models import Model 7from keras.models import Sequential, model_from_json 8from keras.losses import mse, binary_crossentropy 9from keras.layers import Conv2D, Flatten 10from keras.layers import Reshape, Conv2DTranspose 11from keras.utils import plot_model, np_utils 12from keras.utils import plot_model 13from keras.callbacks import Callback, EarlyStopping, TensorBoard, ModelCheckpoint, LearningRateScheduler, CSVLogger 14from keras import optimizers 15from keras import backend as K 16from keras.preprocessing.image import array_to_img, img_to_array,load_img 17from keras.preprocessing.image import ImageDataGenerator 18from keras.layers import Activation, BatchNormalization 19 20import numpy as np 21import matplotlib.pyplot as plt 22import argparse 23import os 24import re 25import glob 26import random as rn 27import tensorflow as tf 28import cv2 29from PIL import Image 30 31import warnings 32warnings.filterwarnings('ignore') 33 34%matplotlib inline 35get_ipython().run_line_magic('matplotlib', 'inline') 36 37 38#reparameterization trick 39# instead of sampling from Q(z|X), sample eps = N(0,I) 40# z = z_mean + sqrt(var)*eps 41def sampling(args): 42 """Reparameterization trick by sampling fr an isotropic unit Gaussian. 43 44 # Arguments 45 args (tensor): mean and log of variance of Q(z|X) 46 47 # Returns 48 z (tensor): sampled latent vector 49 """ 50 z_mean, z_log_var = args 51 batch = K.shape(z_mean)[0] 52 dim = K.int_shape(z_mean)[1] 53 # by default, random_normal has mean=0 and std=1.0 54 epsilon = K.random_normal(shape=(batch, dim)) 55 return z_mean + K.exp(0.5 * z_log_var) * epsilon 56 57 58def plot_results(models, 59 data, 60 batch_size=25, 61 model_name="vae_OCT"): 62 """Plots labels and MNIST digits as function of 2-dim latent vector 63 64 # Arguments 65 models (tuple): encoder and decoder models 66 data (tuple): test data and label 67 batch_size (int): prediction batch size 68 model_name (string): which model is using this function 69 """ 70 71 encoder, decoder = models 72 x_test = data 73 os.makedirs(model_name, exist_ok=True) 74 75 filename = os.path.join(model_name, "vae_mean.png") 76 # display a 2D plot of the digit classes in the latent space 77 z_mean, _, _ = encoder.predict(x_test, 78 batch_size=batch_size) 79 80#original dataset 81#train 82filenames = glob.glob("./NORMAL_resize_pixel_100_1_0506/*.jpeg") 83x_train= [] 84 85for filename in filenames: 86 img = img_to_array(load_img( 87 filename, color_mode = "grayscale" 88 , target_size=(512,496))) 89 x_train.append(img) 90 91x_train = np.asarray(x_train) 92print(x_train.shape)# (100, 512, 496, 1) 93 94#test 95filenames = glob.glob("./NORMAL_resize_pixel_0506/*.jpeg") 96x_test= [] 97 98for filename in filenames: 99 img = img_to_array(load_img( 100 filename, color_mode = "grayscale" 101 , target_size=(512,496))) 102 x_test.append(img) 103 104x_test = np.asarray(x_test) 105 106 107image_size_width = x_train.shape[1] 108image_size_height = x_train.shape[2] 109original_dim = 512 * 496 #3削除 110x_train = np.reshape(x_train, [-1, image_size_width,image_size_height,1]) 111x_test = np.reshape(x_test,[ -1, image_size_width,image_size_height,1]) 112x_train = x_train.astype('float32') / 255 113x_test = x_test.astype('float32') / 255 114 115#numpy配列をタプルに変換 116x_train = tuple(x_train) 117x_test = tuple(x_test) 118 119#各サンプルの平均を0にする 120train_datagen = ImageDataGenerator(samplewise_center=True) 121train_generator = train_datagen.flow(x_train, batch_size=32) 122 123validation_datagen = ImageDataGenerator(samplewise_center=True) 124validation_generator = validation_datagen.flow(x_test, batch_size=32) 125 126print(x_train.shape)# 表示されず(タプルに変換したため?) 127print(x_test.shape) # 表示されず(タプルに変換したため?) 128 129# network parameters 130input_shape = (image_size_width,image_size_height,1) 131batch_size = 25#50 132kernel_size = 3 133filters = 16 134latent_dim = 2 135epochs = 100 136 137# VAE model = encoder + decoder 138# build encoder model 139inputs = Input(shape=input_shape, name='encoder_input') 140x = inputs 141for i in range(4): 142 filters *= 2 143 x = Conv2D(filters=filters, 144 kernel_size=kernel_size, 145 activation = 'relu', 146 strides=2,padding='same')(x) 147 #x = BatchNormalization()(x) 148 #x = Activation('relu')(x) 149 150# shape info needed to build decoder model 151shape = K.int_shape(x) 152 153# generate latent vector Q(z|X) 154x = Flatten()(x) 155x = Dense(64, activation='relu')(x) 156z_mean = Dense(latent_dim, name='z_mean')(x) 157z_log_var = Dense(latent_dim, name='z_log_var')(x) 158 159# use reparameterization trick to push the sampling out as input 160# note that "output_shape" isn't necessary with the TensorFlow backend 161z = Lambda(sampling, output_shape=(latent_dim,), name='z')([z_mean, z_log_var]) 162 163# instantiate encoder model 164encoder = Model(inputs, [z_mean, z_log_var, z], name='encoder') 165encoder.summary() 166plot_model(encoder, to_file='vae_cnn_encoder.png', show_shapes=True) 167 168# build decoder model 169latent_inputs = Input(shape=(latent_dim,), name='z_sampling') 170x = Dense(shape[1] * shape[2] * shape[3], activation='relu')(latent_inputs) 171x = Reshape((shape[1], shape[2], shape[3]))(x) 172 173for i in range(4): 174 x = Conv2DTranspose(filters=filters, 175 kernel_size=kernel_size, 176 activation = 'relu', 177 strides=2,padding='same')(x) 178 #x = BatchNormalization()(x) 179 #x = Activation('relu')(x) 180 filters //= 2 181 182outputs = Conv2DTranspose(filters=1, 183 kernel_size=kernel_size, 184 activation='sigmoid', 185 padding='same', 186 name='decoder_output')(x) 187#outputs = BatchNormalization()(outputs)#(x) 188#outputs = Activation('sigmoid')(outputs)#(x) 189 190# instantiate decoder model 191decoder = Model(latent_inputs, outputs, name='decoder') 192decoder.summary() 193plot_model(decoder, to_file='vae_cnn_decoder.png', show_shapes=True) 194 195# instantiate VAE model 196outputs = decoder(encoder(inputs)[2]) 197vae = Model(inputs, outputs, name='vae') 198 199 200 201def plot_history(history): 202 203 # ?????????? 204 plt.plot(history.history['loss']) 205 plt.plot(history.history['val_loss']) 206 plt.title('model loss') 207 plt.xlabel('epoch') 208 plt.ylabel('loss') 209 plt.legend(['loss', 'val_loss'], loc='lower right') 210 plt.savefig('loss.png') # -----(2) 211 plt.show() 212 213 214 215if __name__ == '__main__': 216 parser = argparse.ArgumentParser() 217 help_ = "Load h5 model trained weights" 218 parser.add_argument("-w", "--weights", help=help_) 219 help_ = "Use mse loss instead of binary cross entropy (default)" 220 parser.add_argument("-m", "--mse", help=help_, action='store_true') 221 args = parser.parse_args([]) 222 models = (encoder, decoder) 223 data = (x_test) 224 225 226 227 228 # VAE loss = mse_loss or xent_loss + kl_loss 229 if args.mse: 230 reconstruction_loss = mse(K.flatten(inputs), K.flatten(outputs)) 231 else: 232 reconstruction_loss = binary_crossentropy(K.flatten(inputs), 233 K.flatten(outputs)) 234 235 reconstruction_loss *= image_size_width * image_size_height 236 kl_loss = 1 + z_log_var - K.square(z_mean) - K.exp(z_log_var) 237 kl_loss = K.sum(kl_loss, axis=-1) 238 kl_loss *= -0.5 239 vae_loss = K.mean(reconstruction_loss + kl_loss) 240 vae.add_loss(vae_loss) 241 Adam = optimizers.Adam(lr=0.0005) 242 vae.compile(optimizer=Adam) 243 vae.summary() 244 plot_model(vae, to_file='vae_cnn.png', show_shapes=True) 245 246 callbacks = [] 247 callbacks.append(ModelCheckpoint(filepath="model.ep{epoch:02d}.h5", save_best_only = True, period=5)) 248 callbacks.append(EarlyStopping(monitor='val_loss', patience=7, verbose=1)) 249 callbacks.append(CSVLogger("history.csv")) 250 251 if args.weights: 252 vae.load_weights(args.weights) 253 else: 254 # train the autoencoder 255 history = vae.fit_generator(train_generator, 256 steps_per_epoch=x_train.shape[0] // batch_size, 257 epochs=epochs,#20, 258 validation_data=(validation_generator,None), 259 validation_steps=x_test.shape[0] // batch_size, 260 verbose=1, 261 callbacks=callbacks) 262 263 plot_results(models, data, batch_size=batch_size, model_name="vae_OCT") 264 plot_history(history)

試したこと

以下を参考に試しに次元を増やしてみると、今度は「tuple is out of index」のエラーが出てしまいます。

https://deepage.net/features/numpy-newaxis.html

http://www.kamishima.net/mlmpyja/nbayes2/shape.html

補足情報(FW/ツールのバージョンなど)

google colabを使っています。

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

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

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

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

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

mather

2019/06/06 09:34

print(x_train.shape) などの出力値を書いてください。 また、あちこちで x_train 自体が更新されているようですが、エラーになった箇所の直前ではどのような状態になっているのですか? 加えて、公式ドキュメントは参照しましたか? https://keras.io/preprocessing/image/#flow
SuzuAya

2019/06/06 11:22

出力値を追記しました。 エラーになった箇所の直前では、numpy配列からタプルへ変換されている状態です。 公式ドキュメントは参照しました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

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

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

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問