https://teratail.com/questions/131885#reply-200116
以前質問させていただいた部分は解決したのですが別の部分でエラーがでてしまいました。
エラーを調べたところ画像を読み込めていないということです。
https://teratail.com/questions/97976
コードの参考はこちらです。
https://qiita.com/uni-3/items/a62daa5a03a02f5fa46d
画像の位置は
dataset.pyの一部分
DataPath = '/home/ooo/××/SegNet/CamVid/'
変更しました。
プログラムは/home/ooo/××/segnet/
にdataset.pyとmodel.pyとtrain.pyが入っています。
いろいろディレクトリ指定などを変えたのですがダメでした。
ご教授いただけると幸いです。
気になる質問をクリップする
クリップした質問は、後からいつでもMYページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
回答2件
0
ベストアンサー
もう自分で直したほうが早いなと思って作成しました。
# segnet以下にtrain.py, model.py, dataset.pyが格納されています。 ─segnet ├─.idea ├─CamVid │ ├─test │ ├─testannot │ ├─train │ ├─trainannot │ ├─val │ └─valannot ├─docker │ ├─cpu │ └─gpu ├─Example_Models ├─Models ├─Scripts └─__pycache__
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))
投稿2018/06/25 11:07
編集2018/06/25 11:14総合スコア1170
0
os.getcwd()
はcurrent working directoryを返します。
プログラム自体はsegnet内で実行しているので、
/home/ooo/××/segnet/CamVid/train/0001TP_006690.png
を開こうとしていることでしょう。
正しく指定するなら、
python
1>>> DataPath = "/home/ooo/××/SegNet/CamVid/" 2>>> s = "/SegNet/CamVid/train/0001TP_006690.png" 3>>> DataPath + s[15:] 4'/home/ooo/××/SegNet/CamVid/train/0001TP_006690.png'
こんな感じだと思うので、cv2.imreadの中身を
python
1DataPath + txt[i][0][15:]
とかに変えて行けませんか。
投稿2018/06/20 18:36
総合スコア30933
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/06/21 06:40
2018/06/24 01:49
2018/06/24 02:12 編集
2018/06/25 04:37
2018/06/25 04:40
2018/06/25 08:58
2018/06/25 10:21
2018/06/25 10:24
2018/06/25 10:25
2018/06/25 10:29 編集
2018/06/25 10:30
2018/06/25 10:32
2018/06/25 10:34
2018/06/25 10:36
2018/06/25 10:39
2018/06/25 10:43
2018/06/25 10:44
2018/06/25 10:45
2018/06/25 10:50
2018/06/25 11:04
2018/06/25 11:05
2018/06/25 11:08
2018/06/25 11:09
2018/06/25 11:12
2018/06/25 11:32
あなたの回答
tips
太字
斜体
打ち消し線
見出し
引用テキストの挿入
コードの挿入
リンクの挿入
リストの挿入
番号リストの挿入
表の挿入
水平線の挿入
プレビュー
質問の解決につながる回答をしましょう。 サンプルコードなど、より具体的な説明があると質問者の理解の助けになります。 また、読む側のことを考えた、分かりやすい文章を心がけましょう。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/06/25 11:09
2018/06/25 11:31
2018/06/25 11:34 編集
2018/06/25 11:44
2018/06/25 11:47
2018/06/25 12:00
2018/06/25 12:14
2018/06/25 12:18
2018/06/26 01:04
2018/06/26 04:13
2018/06/26 10:19