diff --git a/VGGVisualizer.py b/VGGVisualizer.py index 148a20e..3737679 100644 --- a/VGGVisualizer.py +++ b/VGGVisualizer.py @@ -127,66 +127,61 @@ def __call__(self, x, t): print (h.data[0, 0, :6, :6]) """ outsize1 = h.data.shape[2:] - print 'outsize1: ', outsize1 h, indexes1 = F.max_pooling_2d(h, 2, stride=2) h = F.relu(self.conv2_1(h)) h = F.relu(self.conv2_2(h)) outsize2 = h.data.shape[2:] - print 'outsize2: ', outsize2 h, indexes2 = F.max_pooling_2d(h, 2, stride=2) + h = F.relu(self.conv3_1(h)) h = F.relu(self.conv3_2(h)) h = F.relu(self.conv3_3(h)) outsize3 = h.data.shape[2:] - print 'outsize3: ', outsize3 h, indexes3 = F.max_pooling_2d(h, 2, stride=2) h = F.relu(self.conv4_1(h)) h = F.relu(self.conv4_2(h)) h = F.relu(self.conv4_3(h)) outsize4 = h.data.shape[2:] - print 'outsize4: ', outsize4 h, indexes4 = F.max_pooling_2d(h, 2, stride=2) h = F.relu(self.conv5_1(h)) h = F.relu(self.conv5_2(h)) h = F.relu(self.conv5_3(h)) outsize5 = h.data.shape[2:] - print 'outsize5: ', outsize5 h, indexes5 = F.max_pooling_2d(h, 2, stride=2) + h_tmp = h.data.copy() + print 'h.shape' + print h_tmp.shape + + # Reconstruction + i = 10 + import numpy as np + h = np.zeros(h_tmp.shape) + h[0][i] = h_tmp[0][i] + h = Variable(h) + h = F.unpooling_2d(h, indexes5, 2, stride=2, outsize=outsize5) h = self.deconv5_3(F.relu(h)) h = self.deconv5_2(F.relu(h)) h = self.deconv5_1(F.relu(h)) - """ - h = F.relu(self.deconv5_3(h)) - h = F.relu(self.deconv5_2(h)) - h = F.relu(self.deconv5_1(h)) - """ + # return h h = F.unpooling_2d(h, indexes4, 2, stride=2, outsize=outsize4) h = self.deconv4_3(F.relu(h)) h = self.deconv4_2(F.relu(h)) h = self.deconv4_1(F.relu(h)) - """ - h = F.relu(self.deconv4_3(h)) - h = F.relu(self.deconv4_2(h)) - h = F.relu(self.deconv4_1(h)) - """ + h = F.unpooling_2d(h, indexes3, 2, stride=2, outsize=outsize3) h = self.deconv3_3(F.relu(h)) h = self.deconv3_2(F.relu(h)) h = self.deconv3_1(F.relu(h)) - """ - h = F.relu(self.deconv3_3(h)) - h = F.relu(self.deconv3_2(h)) - h = F.relu(self.deconv3_1(h)) - """ + h = F.unpooling_2d(h, indexes2, 2, stride=2, outsize=outsize2) h = self.deconv2_2(F.relu(h)) @@ -196,13 +191,16 @@ def __call__(self, x, t): h = self.deconv1_2(F.relu(h)) h = self.deconv1_1(F.relu(h)) + print(h.data) + print(h.data.shape) + + # Return first layer visualizations return h print('--- After pooling (subset) ---') print h_prim.data[0, 0, :6, :6] - """ h = F.dropout(F.relu(self.fc6(h)), train=self.train, ratio=0.5) h = F.dropout(F.relu(self.fc7(h)), train=self.train, ratio=0.5) diff --git a/test.py b/test.py index 61ebe68..8b525c8 100644 --- a/test.py +++ b/test.py @@ -19,6 +19,25 @@ vgg = VGG() serializers.load_hdf5('VGG.model', vgg) + + + # Visualize fst filter + """ + imgs = () + for i in range(64): + fil = vgg.conv1_1.W.data[i] + fil = np.rollaxis(fil, 0, 3) + min_val = fil.min() + fil -= min_val + max_val = fil.max() + fil *= ( 255.0 / max_val) + imgs += (fil,) + imgs += (np.zeros((3, 3, 3)),) + + vis = np.concatenate(imgs, axis=0) + cv.imwrite('filters_conv1_1_new.jpg', vis) + """ + vgg = VGGVisualizer.from_VGG(vgg) reconstruction = vgg(Variable(img), None) @@ -27,9 +46,19 @@ # Assume a single image in batch and get it img = reconstruction.data[0] + print('Max: {}'.format(img.max())) + print('Min: {}'.format(img.min())) + img -= img.min() + if img.max() > 0: + img *= 255.0 / img.max() + else: + img *= 255.0 + + print('img.shape: {}'.format(img.shape)) img = np.rollaxis(img, 0, 3) - img += mean + # img += mean - cv.imwrite('cat_reconstructed.jpg', img) + # cv.imwrite('cat_reconstructed.jpg', img) + cv.imwrite('new_dog.jpg', img) print('Done')