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

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

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

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

Q&A

解決済

1回答

291閲覧

kerasでSegNetで画像サイズの変更、画像読み込みについて

hss_

総合スコア39

Keras

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

0グッド

0クリップ

投稿2018/12/14 03:35

編集2018/12/14 05:12

「keras(tensorflow)でSegNet」
(https://qiita.com/uni-3/items/a62daa5a03a02f5fa46d)
を参考に記載されているコードをそのまま自分でも動かしてみようと
試みておりますが、実行するとメモリーのエラーがでてしまうため
下記を試みました。

試したこと:読み込み画像サイスを(360×480)から(176×240)に変更し
コード中の対応する数値を変更しました(フォルダ内の全ての画像サイズを変更)。

Python

1 2## dataset.py 3import cv2 4import numpy as np 5 6from keras.applications import imagenet_utils 7 8import os 9 10DataPath = './CamVid/' 11data_shape = 176*240 12 13class Dataset: 14 def __init__(self, classes=12, train_file='train.txt', test_file='test.txt'): 15 self.train_file = train_file 16 self.test_file = test_file 17 self.data_shape = 176*240 18 self.classes = classes 19 20 def normalized(self, rgb): 21 #return rgb/255.0 22 norm=np.zeros((rgb.shape[0], rgb.shape[1], 3),np.float32) 23 24 b=rgb[:,:,0] 25 g=rgb[:,:,1] 26 r=rgb[:,:,2] 27 28 norm[:,:,0]=cv2.equalizeHist(b) 29 norm[:,:,1]=cv2.equalizeHist(g) 30 norm[:,:,2]=cv2.equalizeHist(r) 31 32 return norm 33 34 def one_hot_it(self, labels): 35 x = np.zeros([176,240,12]) 36 for i in range(176): 37 for j in range(240): 38 x[i,j,labels[i][j]] = 1 39 return x 40 41 def load_data(self, mode='train'): 42 data = [] 43 label = [] 44 if (mode == 'train'): 45 filename = self.train_file 46 else: 47 filename = self.test_file 48 49 with open(DataPath + filename) as f: 50 txt = f.readlines() 51 txt = [line.split(' ') for line in txt] 52 53 for i in range(len(txt)): 54 data.append(self.normalized(cv2.imread(os.getcwd() + txt[i][0][7:]))) 55 label.append(self.one_hot_it(cv2.imread(os.getcwd() + txt[i][1][7:][:-1])[:,:,0])) 56 print('.',end='') 57 #print("train data file", os.getcwd() + txt[i][0][7:]) 58 #print("label data raw", cv2.imread(os.getcwd() + '/CamVid/trainannot/0001TP_006690.png')) 59 return np.array(data), np.array(label) 60 61 62 def preprocess_inputs(self, X): 63 ### @ https://github.com/fchollet/keras/blob/master/keras/applications/imagenet_utils.py 64 """Preprocesses a tensor encoding a batch of images. 65 # Arguments 66 x: input Numpy tensor, 4D. 67 data_format: data format of the image tensor. 68 mode: One of "caffe", "tf". 69 - caffe: will convert the images from RGB to BGR, 70 then will zero-center each color channel with 71 respect to the ImageNet dataset, 72 without scaling. 73 - tf: will scale pixels between -1 and 1, 74 sample-wise. 75 # Returns 76 Preprocessed tensor. 77 """ 78 return imagenet_utils.preprocess_input(X) 79 80 def reshape_labels(self, y): 81 return np.reshape(y, (len(y), self.data_shape, self.classes))

ここで

Python

1## train.py 2import os 3import glob 4import numpy as np 5import keras 6 7from model import SegNet 8 9import dataset 10 11input_shape = (176, 240, 3) 12classes = 12 13epochs = 10 14batch_size = 1 15log_filepath='./logs/' 16 17data_shape = 176*240 18 19class_weighting = [0.2595, 0.1826, 4.5640, 0.1417, 0.5051, 0.3826, 9.6446, 1.8418, 6.6823, 6.2478, 3.0, 7.3614] 20 21## set gpu usage 22import tensorflow as tf 23config = tf.ConfigProto(gpu_options=tf.GPUOptions(allow_growth=True, per_process_gpu_memory_fraction = 0.8)) 24session = tf.Session(config=config) 25keras.backend.tensorflow_backend.set_session(session) 26 27def main(): 28 print("loading data...") 29 ds = dataset.Dataset(classes=classes) 30 train_X, train_y = ds.load_data('train') # need to implement, y shape is (None, 176, 240, classes) 31 32 train_X = ds.preprocess_inputs(train_X) 33 train_Y = ds.reshape_labels(train_y) 34 print("input data shape...", train_X.shape) 35 print("input label shape...", train_Y.shape) 36 37 test_X, test_y = ds.load_data('test') # need to implement, y shape is (None, 176, 240, classes) 38 test_X = ds.preprocess_inputs(test_X) 39 test_Y = ds.reshape_labels(test_y) 40 41 tb_cb = keras.callbacks.TensorBoard(log_dir=log_filepath, histogram_freq=1, write_graph=True, write_images=True) 42 print("creating model...") 43 model = SegNet(input_shape=input_shape, classes=classes) 44 model.compile(loss="categorical_crossentropy", optimizer='adadelta', metrics=["accuracy"]) 45 46 model.fit(train_X, train_Y, batch_size=batch_size, epochs=epochs, 47 verbose=1, class_weight=class_weighting , validation_data=(test_X, test_Y), shuffle=True 48 , callbacks=[tb_cb]) 49 50 model.save('seg.h5') 51 52if __name__ == '__main__': 53 main()

を実行すると下記エラーが表示されます。
runfile('C:/Users/user/code/train.py', wdir='C:/Users/user/code')
loading data...
Traceback (most recent call last):

File "<ipython-input-18-ea305e956635>", line 1, in <module>
runfile('C:/Users/user/code/train.py', wdir='C:/Users/user/code')

File "C:\Users\user\anaconda3\envs\DL\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 704, in runfile
execfile(filename, namespace)

File "C:\Users\user\anaconda3\envs\DL\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/user/code/train.py", line 52, in <module>
main()

File "C:/Users/user/code/train.py", line 29, in main
train_X, train_y = ds.load_data('train') # need to implement, y shape is (None, 176, 240, classes)

File "C:\Users\user\code\dataset.py", line 53, in load_data
label.append(self.one_hot_it(cv2.imread(os.getcwd() + txt[i][1][7:][:-1])[:,:,0]))

File "C:\Users\user\code\dataset.py", line 36, in one_hot_it
x[i,j,labels[i][j]] = 1

IndexError: index 35 is out of bounds for axis 2 with size 12

このエラーの意味と対処方法が分からずに困っております。
お手数をお掛けいたしますが、ご教示いただけると大変ありがたいです。

補足:CamVidフォルダ中のtestannot,trainannot,valannotを元画像サイズ(360×480)で
実行すると正常に学習が開始されます。

windows10
python3.6.7
tensorflow1.12.0
keras2.2.4

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

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

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

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

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

guest

回答1

0

自己解決

画像サイズ変更したtestannot、trainannot、valannot の画像に問題が
あって読み込みに失敗していたようです。
「FastStone Photo Resizer」で色深度8bit(256)まで指定して(176×240)に
画像サイズを変更したらうまく動作しました。

投稿2018/12/20 04:22

hss_

総合スコア39

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問