質問するログイン新規登録

回答編集履歴

2

誤字の修正

2021/11/18 01:58

投稿

HRCo4
HRCo4

スコア140

answer CHANGED
@@ -1,5 +1,5 @@
1
1
  (回答修正しました)
2
- コードと見ると、np.expand_dims を2回行っており、ネットワークの入力が1chということから、おそらくコードの参考元では1chのグレースケール画像を入力しており、今回試したのはカラーあるいは3chのグレースケール画像を入力しようとしたのではないでしょうか?
2
+ コードと見ると、np.expand_dims を2回行っていますね。ネットワークの入力が1chということから、おそらくコードの参考元では1chのグレースケール画像を入力しており、今回試したのはカラーあるいは3chのグレースケール画像を入力しようとしたのではないでしょうか?
3
3
 
4
4
  おそらくPreprocessedDatasetクラスで画像をロードするときに np.expand_dims を行っており、推論実行するときにまた np.expand_dims を行っているため、入力する画像の shape が (1, 1, h, w, ch) になっています。
5
5
 

1

補足追加

2021/11/18 01:58

投稿

HRCo4
HRCo4

スコア140

answer CHANGED
@@ -1,16 +1,103 @@
1
+ (回答修正しました)
1
- PreprocessedDatasetクラスで画像をロードるときに np.expand_dims を行っており、推論実行するきにまた np.expand_dims を行ってるため、入力る画像の shape が (1, 1, h, w, ch) にってます。
2
+ ードと見るとnp.expand_dims を2回行っており、ネットワークの入力が1chということからおそらくコードの参考元では1chのグレースケール画像を入力しており、今回試したのはカラーあいは3chのグレースケール画像を入力しようとしたではないでしょうか?
2
3
 
3
- pred = model.predictor(np.expand_dims(img, axis=0)) を
4
+ おそらくPreprocessedDatasetクラスで画像をロードするときに np.expand_dims を行っており、推論実行するときにまた np.expand_dims を行っているため、入力する画像の shape が (1, 1, h, w, ch) になっています。
4
- pred = model.predictor(img) に
5
- 書き換えればよろしいかと。
6
5
 
7
- あと、chainer は確か入力が (batch, ch, h, w) 形式ったかと思います。
6
+ ネットワーク構成的に入力を1chにしなければならないで画像を読み込ん際にグレースケール化すれば問題ないかと思います。
7
+
8
- 、skimage の imread は (h, w, ch) の形式で読み込んだはずでので、
8
+ 修正しコードを記入しておきま
9
9
  ```
10
+ import os
11
+ import numpy as np
12
+ import skimage. io as io
13
+ from skimage.color import rgb2gray # 追加(グレースケール用)
14
+ import chainer
15
+ import chainer.links as L
16
+ import chainer.functions as F
17
+
18
+ class PreprocessedDataset(chainer.dataset.DatasetMixin):
19
+ def __init__(
20
+ self,
21
+ root_path,
22
+ split_list
23
+ ):
24
+ self.root_path = root_path
25
+ with open(split_list) as f:
26
+ self.split_list = [line.rstrip() for line in f]
27
+ self.dtype = np.float32
28
+
29
+ def __len__(self):
30
+ return len(self.split_list)
31
+
10
- def _get_image(self, i):
32
+ def _get_image(self, i):
11
33
  image = io.imread(os.path.join(self.root_path, self.split_list[i]))
12
34
  image = self._min_max_normalize_one_image(image)
35
+ if len(image.shape) == 3: # カラーあるいは3chグレスケ画像の場合は1chグレスケ化
13
- image = image.transpose(2,0,1)
36
+ image = rgb2gray(image) # (h, w, ch) -> (h, w)
14
- return np.expand_dims(image.astype(self.dtype), axis=0)
37
+ return np.expand_dims(image.astype(self.dtype), axis=0) # (h, w) -> (1, h, w)
38
+
39
+ def _min_max_normalize_one_image(self, image):
40
+ max_int = image.max()
41
+ min_int = image.min()
42
+ out = (image.astype(np.float32) - min_int) / (max_int - min_int)
43
+ return out
44
+
45
+ def _get_label(self, i):
46
+ label = 0 if 'false' in self.split_list[i] else 1
47
+ return label
48
+
49
+ def get_example(self, i):
50
+ x, y = self._get_image(i), self._get_label(i)
51
+ return x, y
52
+
53
+ class ClassificationModel(chainer.Chain):
54
+
55
+ def __init__(self, n_class=2):
56
+ super(ClassificationModel, self).__init__()
57
+ with self.init_scope():
58
+
59
+ self.conv1 = L.Convolution2D(1, 32, 5, 1, 2)
60
+ self.bn1 = L.BatchNormalization(32)
61
+ self.conv2 = L.Convolution2D(32, 64, 5, 1, 2)
62
+ self.bn2 = L.BatchNormalization(64)
63
+ self.conv3 = L.Convolution2D(64, 128, 3, 1, 1)
64
+ self.bn3 = L.BatchNormalization(128)
65
+ self.conv4 = L.Convolution2D(128, 256, 3, 1, 1)
66
+ self.bn4 = L.BatchNormalization(256)
67
+ self.fc5 = L.Linear(16384, 1024)
68
+ self.fc6 = L.Linear(1024, n_class)
69
+
70
+ def __call__(self, x):
71
+ h = F.relu(self.conv1(x)) ←#エラー箇所
72
+ h = F.max_pooling_2d(self.bn1(h), 2, 2)
73
+ h = F.relu(self.conv2(x))
74
+ h = F.max_pooling_2d(self.bn2(h), 2, 2)
75
+ h = F.relu(self.conv3(x))
76
+ h = F.max_pooling_2d(self.bn3(h), 2, 2)
77
+ h = F.relu(self.conv4(x))
78
+ h = F.max_pooling_2d(self.bn4(h), 2, 2)
79
+ h = F.dropout(F.relu(self.fc5(h)))
80
+ return self.fc6(h)
81
+
82
+ root_path = './dataset_cls'
83
+ split_list = './dataset_cls/split_list/test.txt'
84
+
85
+ test_dataset = PreprocessedDataset(root_path, split_list)
86
+
87
+ model = L.Classifier(ClassificationModel(n_class=2))
88
+
89
+ print('================')
90
+ for i in range(10):
91
+ with chainer.using_config('train', False):
92
+
93
+ img, label = test_dataset.get_example(i)
94
+
95
+ pred = model.predictor(np.expand_dims(img, axis=0)) # (1, h, w) -> (1, 1, h, w)
96
+
97
+ pred = F.softmax(pred)
98
+
99
+ print('test {}'.format(i + 1))
100
+ print(' pred: {}'.format(np.argmax(pred.data)))
101
+ print(' label: {}'.format(label))
102
+ print('================')
15
- ```
103
+ ```
16
- のようにして、shape を (1, ch, h, w) にする必要があります。