###前提・実現したいこと
chainerで出力層の結果(10次元のベクトル)を得たいです.
そのために、cifar-10やmnistの時と同じようにデータを(3HW){チャンネル,高さ,幅}にしたのですが,
いざ学習したネットワークに流しこむ時にエラーが発生してしまいます.
###発生している問題・エラーメッセージ
Invalid operation is performed in Convolution2DFunction (Forward) Expect: in_types[0].ndim == 4 Actual: 3 != 4
###該当のソースコード
python
1import chainer 2import chainer.functions as F 3import chainer.links as L 4from chainer import training 5from chainer.training import extensions 6from chainer.datasets import tuple_dataset 7from chainer import Variable 8from chainer import serializers 9import numpy as np 10import _pickle as cpickle 11import os 12import random 13 14xp = chainer.cuda.cupy 15 16def load_pickle(): 17 base_path = os.path.dirname(os.path.abspath(__file__)) 18 hair_path = os.path.normpath(os.path.join(base_path, '../result/result_pickle')) 19 imageData = [] 20 labelData = [] 21 test_tuple = [] 22 af_imageData = [] 23 af_labelData = [] 24 for directory in os.listdir(hair_path): 25 if directory.find('.DS_Store') > -1: 26 continue 27 file_path = hair_path + '/' + directory 28 for img in os.listdir(file_path): 29 if img.find('_DS_Store') > -1 or img.find('.DS_Store') > -1: 30 continue 31 pickle_dt = np.load(file_path + '/' + img).reshape(3, 5625) 32 r, g, b = pickle_dt[0], pickle_dt[1], pickle_dt[2] 33 rImg = np.asarray(np.float32(r) / 255.0).reshape(75, 75) 34 gImg = np.asarray(np.float32(g) / 255.0).reshape(75, 75) 35 bImg = np.asarray(np.float32(b) / 255.0).reshape(75, 75) 36 all_ary = np.asarray([rImg, gImg, bImg]) 37 test_tuple.append((all_ary, np.int32(int(directory)))) 38 imageData.append(all_ary) 39 labelData.append(np.int32(int(directory))) 40 41 random.shuffle(test_tuple) 42 43 for tuple in test_tuple: 44 af_imageData.append(tuple[0]) 45 af_labelData.append(tuple[1]) 46 47 return af_imageData, af_labelData 48 49def load_result_pickle(): 50 base_path = os.path.dirname(os.path.abspath(__file__)) 51 result_path = os.path.normpath(os.path.join(base_path, '../result/result_pickle')) 52 imageData = [] 53 # labelData = [] 54 55 for image in os.listdir(result_path): 56 if image.find('.DS_Store') > -1: 57 continue 58 np_pickle = np.load(result_path + '/' + image).reshape(3, 5625) 59 r, g, b = np_pickle[0], np_pickle[1], np_pickle[2] 60 rImg = np.asarray(np.float32(r) / 255.0).reshape(75, 75) 61 gImg = np.asarray(np.float32(g) / 255.0).reshape(75, 75) 62 bImg = np.asarray(np.float32(b) / 255.0).reshape(75, 75) 63 all_ary = np.asarray([rImg, gImg, bImg]) 64 imageData.append(all_ary) 65 # labelData.append(np.int32(0)) 66 67 return imageData 68 69class AlexNet(chainer.Chain): 70 71 def __init__(self): 72 super(AlexNet, self).__init__( 73 conv1 = L.Convolution2D(None, 96, 6, stride=3), 74 conv2 = L.Convolution2D(None, 128, 3, pad=2), 75 # conv3 = L.Convolution2D(None, 256, 3, pad=1), 76 conv4 = L.Convolution2D(None, 128, 3, pad=1), 77 fc6 = L.Linear(None, 6400), 78 fc7 = L.Linear(None, 6400), 79 fc8 = L.Linear(None, 1000), 80 #fc9 = L.Linear(None, 20) 81 82 ) 83 84 # self.vector = 0 85 86 def __call__(self, x): 87 h = F.max_pooling_2d(F.local_response_normalization(F.relu(self.conv1(x))), 3, stride=1) 88 h = F.max_pooling_2d(F.local_response_normalization(F.relu(self.conv2(h))), 3, stride=1) 89 # h = F.relu(self.conv3(h)) 90 h = F.max_pooling_2d(F.relu(self.conv4(h)), 3, stride=2) 91 h = F.dropout(F.relu(self.fc6(h))) 92 h = F.dropout(F.relu(self.fc7(h))) 93 h = self.fc8(h) 94 # self.vector = h 95 return h 96 97# ベクトル化するために全データを配列へ入れる 98imgData = load_result_pickle() 99# モデルのロード 100model = AlexNet() 101chainer.serializers.load_npz('smaller_alex_not_Classifier', model) 102optimizer = chainer.optimizers.Adam() 103optimizer.setup(model) 104 105for img in imgData: 106 y = model(img)
###試したこと
学習時はデータをL.Classifier()に入れて学習させました。
http://qiita.com/Worldforward/items/f72d907cbe2760b72c2a
ここを参考に,AlexNet単体と,L.Classifier(AlexNet())のパラメータは別々に保存してあります。
4つ目の次元についてが分かりません。バッチサイズなのか,正解ラベルなのか..?と思っています。
学習時にはtrainerを利用して,データをtuple_datasetにすることによって学習させました。
GPUを利用していて,GTX1070を使っています。
よろしくお願いします。
###補足情報(言語/FW/ツール等のバージョンなど)
より詳細な情報
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。