前提・実現したいこと
convLSTMを用いた画像予測を行っています。
%matplotlib inline
i=0
fig, axes = plt.subplots(1, 2, figsize=(12,6))
axes[0].imshow((y_test[i]+1)/2)
axes[1].imshow((model.predict(x_test[[i]]).reshape(100,180,3)+1)/2)
i=0のところを0以外の数値にするとエラーが出ます。
発生している問題・エラーメッセージ
IndexError: index 20 is out of bounds for axis 0 with size 1
該当のソースコード
import numpy as np import matplotlib.pyplot as plt import pandas as pd import seaborn as sns from sklearn.model_selection import train_test_split import glob from PIL import Image from tqdm import tqdm import zipfile import io # 縮小後の画像サイズ height = 100 width = 180 # 読み込んだ画像を入れる配列 imgs=np.empty((0, height, width, 3)) # zipファイルを読み込んでnumpy配列にする zip_f = zipfile.ZipFile('drive/My Drive/Colab Notebooks/convLSTM/wide.zip') for name in tqdm(zip_f.namelist()): with zip_f.open(name) as file: path = io.BytesIO(file.read()) #解凍 img = Image.open(path) img = img.resize((width, height)) img_np = np.array(img).reshape(1, height, width, 3) imgs = np.append(imgs, img_np, axis=0) # 時系列で学習できる形式に整える n_seq = 5 n_sample = imgs.shape[0] - n_seq x = np.zeros((n_sample, n_seq, height, width, 3)) y = np.zeros((n_sample, height, width, 3)) for i in range(n_sample): x[i] = imgs[i:i+n_seq] y[i] = imgs[i+n_seq] x, y = (x-128)/128, (y-128)/128 x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.1, shuffle = False) from keras import layers from keras.layers.core import Activation from tensorflow.keras.models import Model inputs = layers.Input(shape=(5, height, width, 3)) x0 = layers.ConvLSTM2D(filters=16, kernel_size=(3,3), padding="same", return_sequences=True, data_format="channels_last")(inputs) x0 = layers.BatchNormalization(momentum=0.6)(x0) x0 = layers.ConvLSTM2D(filters=16, kernel_size=(3,3), padding="same", return_sequences=True, data_format="channels_last")(x0) x0 = layers.BatchNormalization(momentum=0.8)(x0) x0 = layers.ConvLSTM2D(filters=3, kernel_size=(3,3), padding="same", return_sequences=False, data_format="channels_last")(x0) out = Activation('tanh')(x0) model = Model(inputs=inputs, outputs=out) model.summary() model.compile(optimizer='rmsprop', loss='mae', metrics=['mse']) call_backs=[EarlyStopping(monitor="val_loss",patience=5)] model.fit(x_train, y_train, batch_size=16, epochs=100, verbose=2, validation_split=0.2, shuffle=True, callbacks=call_backs) # 描画 %matplotlib inline i=10 fig, axes = plt.subplots(1, 2, figsize=(12,6)) axes[0].imshow((y_test[i]+1)/2) axes[1].imshow((model.predict(x_test[[i]]).reshape(100,180,3)+1)/2)
補足情報(FW/ツールのバージョンなど)
何を変えればいいのかわかりません よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー