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

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

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

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

Q&A

解決済

1回答

3251閲覧

reshapeに関してのエラー

uriuri

総合スコア47

Python

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

0グッド

0クリップ

投稿2018/06/26 07:53

参考サイトを参考に実装しようと考えています。
https://qiita.com/uni-3/items/a62daa5a03a02f5fa46d

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 = (360, 480, 3) 12classes = 12 13epochs = 100 14batch_size = 1 15log_filepath='./logs/' 16 17data_shape = 360*480 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 27 28def main(): 29 print("loading data...") 30 ds = dataset.Dataset(classes=classes, 31 train_file="CamVid/train.txt", 32 test_file="CamVid/test.txt") 33 train_X, train_y = ds.load_data(root_path="CamVid", 34 mode='train') # need to implement, y shape is (None, 360, 480, classes) 35 36 train_X = ds.preprocess_inputs(train_X) 37 train_Y = ds.reshape_labels(train_y) 38 print("input data shape...", train_X.shape) 39 print("input label shape...", train_Y.shape) 40 41 test_X, test_y = ds.load_data(root_path="CamVid", 42 mode='test') # need to implement, y shape is (None, 360, 480, classes) 43 test_X = ds.preprocess_inputs(test_X) 44 test_Y = ds.reshape_labels(test_y) 45 46 tb_cb = keras.callbacks.TensorBoard(log_dir=log_filepath, histogram_freq=1, write_graph=True, write_images=True) 47 print("creating model...") 48 model = SegNet(input_shape=input_shape, classes=classes) 49 model.compile(loss="categorical_crossentropy", optimizer='adadelta', metrics=["accuracy"]) 50 51 model.fit(train_X, train_Y, batch_size=batch_size, epochs=epochs, 52 verbose=1, class_weight=class_weighting , validation_data=(test_X, test_Y), shuffle=True 53 , callbacks=[tb_cb]) 54 55 model.save('seg.h5') 56 57if __name__ == '__main__': 58 main()

python

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

train.pyを実行後
loading data...
...............................................................................................................................................................................................................................................................................................................................................................................Traceback (most recent call last):
File "train.py", line 58, in <module>
main()
File "train.py", line 37, in main
train_Y = ds.reshape_labels(train_y)
File "/home/ooo/×××/SegNet/dataset.py", line 78, in reshape_labels
return np.reshape(y, (len(y), self.data_shape, self.classes))
File "/home/ooo/anaconda3/lib/python3.6/site-packages/numpy/core/fromnumeric.py", line 257, in reshape
return _wrapfunc(a, '0. reshape', newshape, order=order)
File "/home/ooo/anaconda3/lib/python3.6/site-packages/numpy/core/fromnumeric.py", line 52, in _wrapfunc
return getattr(obj, method)(*args, **kwds)
ValueError: cannot reshape array of size 63417600 into shape (367,172800,12)

というエラーがでてしまいました。
ValueErrorとして
367*172800=63417600
reshapeしようとする画像データのピクセル数が不足している訳ではなく同じ値なのですがエラーを吐いてしまいます。
参考サイトとして
https://teratail.com/questions/97039
解決策を教えていただけると幸いです。

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

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

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

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

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

guest

回答1

0

ベストアンサー

修正しました。
動作確認したいのですがGPUのメモリが足りなくなりエラーでこけ、学習が最後まで終わらないため動作の保証はできません。
ちなみに動作確認した環境は
CPU: Ryzen2600X
GPU: GTX1060(6G)
メモリ: 16GB
OS: Widnows10

Python: 3.6.5
Keras: 2.2.0
tensorflow-gpu: 1.8.0
...

python

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

投稿2018/06/26 11:29

wakame

総合スコア1170

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

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

uriuri

2018/06/28 01:07

実装してみたところ動作しました。 丁寧に対応していただきありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.49%

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

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

質問する

関連した質問