chainerの畳み込み処理画像の可視化
chainerで機械学習を行っているのですが、畳み込み処理後の画像を可視化しようと考えています。
学習モデルは以下のようになっています。
def make_image(a,z):
c,n,w,h,=a.shape
for i in range(n):
print(n,i)
image_array = np.zeros((c,n,w,h))
print(type(image_array))
print(image_array.shape)
print(type(a))
print(a.shape)
print(len(a[0,i,:,:]))
image_array[0,i,:,:] = a[0,i,:,:]
print(type(image_array))
image_array = image_array.astype(np.unit8)
fp = "./h0"+str(z)+"_"+str(i)+".jpg"
Image.fromarray(image_array).save(fp)
class CNN(chainer.Chain):
def __init__(self, train=True): super(CNN, self).__init__( conv1=L.Convolution2D(1, 32, 3), conv2=L.Convolution2D(32, 64, 3), l1=L.Linear(9216, 10), ) self.train = train def __call__(self, x): h = F.max_pooling_2d(F.relu(self.conv1(x)), 1) print("h1",type(h),h.shape) for i in range(32): make_image(h,i) print("array1",type(array1),array1.shape) print("h1",h.shape) h = F.max_pooling_2d(F.relu(self.conv2(h)), 2) return self.l1(h)
__call__内でmake_imageを使ってnumpy配列に格納後、画像化しようとしましたが、うまくいきませんでした。
以下が実行結果になります。
h1 <class 'chainer.variable.Variable'> (1, 32, 26, 26)
32 0
<class 'numpy.ndarray'>
(1, 32, 26, 26)
<class 'chainer.variable.Variable'>
(1, 32, 26, 26)
26
Traceback (most recent call last):
File "C:\Users\c6q001\Documents\python_test\notMnist_SAMPLE\4_predictCNN.py", line 100, in <module>
main()
File "C:\Users\c6q001\Documents\python_test\notMnist_SAMPLE\4_predictCNN.py", line 86, in main
y= model.predictor(img.reshape(-1, 1, 28, 28))
File "C:\Users\c6q001\Documents\python_test\notMnist_SAMPLE\4_predictCNN.py", line 52, in call
make_image(h,i)
File "C:\Users\c6q001\Documents\python_test\notMnist_SAMPLE\4_predictCNN.py", line 24, in make_image
image_array[0,i,:,:] = a[0,i,:,:]
ValueError: setting an array element with a sequence.
配列の形はあっていると思うのですが、そこが間違っているようです。
畳み込み処理後の画像を可視化できればいいので、改善点、ほかの手法などあれば教えていただければと思います。
よろしくお願いいたします。
あなたの回答
tips
プレビュー