Last active
August 24, 2017 15:53
-
-
Save katsugeneration/ff55c42be60f1fd7688fbdef123a5a89 to your computer and use it in GitHub Desktop.
Convert image by CNN weights
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from scipy.ndimage import convolve | |
import tensorflow as tf | |
from tensorflow import pywrap_tensorflow | |
from PIL import Image | |
from matplotlib import pyplot as plt | |
plt.axis('off') | |
img = Image.open('009_0001.jpg') | |
plt.imshow(img) | |
plt.show() | |
checkpoint = pywrap_tensorflow.NewCheckpointReader('inception-v3/model.ckpt-157585') | |
conv0 = checkpoint.get_tensor('conv0/weights') | |
conv0 = conv0.transpose((3, 2, 0, 1)) # transpose kernel [H, W, In, Out] to [Out, In, H, W] | |
images = [] | |
for i in range(32): | |
_kernel = conv0[i] | |
print(i) | |
img1 = np.asarray(img) | |
img1 = img1.transpose((2, 0, 1)) # transpose image [H, W, C] to [C, H, W] | |
imgR = convolve(img1[0], _kernel[0], mode='constant') | |
imgG = convolve(img1[1], _kernel[1], mode='constant') | |
imgB = convolve(img1[2], _kernel[2], mode='constant') | |
img1 = np.asarray(imgR + imgG + imgB) | |
images.append(img1) | |
plt.axis('off') | |
plt.imshow(img1, cmap='gray') | |
plt.show() | |
for i in range(11): | |
for j in range(11): | |
if i != j: | |
img1 = (images[i] + images[j]) / 2.0 | |
print("%d + %d" % (i, j)) | |
plt.axis('off') | |
plt.imshow(img1, cmap='gray') | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment