# 计算机视觉任务：图像梯度和图像完成

04

Examples of the TinyImageNet-100-A

1.Saliency Maps

def compute_saliency_maps(X, y, model):

```    N,C,H,W = X.shape
saliency = np.zeros((N,H,W))

# Compute the score by a single forward pass
scores, cache = model.forward(X, mode='test')    # Score size (N,100)

# The loss function we want to optimize(maximize)
# loss = (scores[np.arange(N), y] - lambda*np.sqrt(np.sum(X**2)))   # Size (N,)

# The gradient of this loss wih respect to the input image
dscores = np.zeros_like(scores)
dscores[np.arange(N), y] = 1.0
saliency += np.max(np.abs(dX), axis=1)

return saliency```

Random images

Cherry-picked images

2.Fooling Images

```def make_fooling_image(X, target_y, model):
X_fooling = X.copy()
N,C,H,W = X_fooling.shape      # N=1
i = 0
y_pred = -1
lr = 200.0
while (y_pred != target_y) & (i<200):
scores, cache = model.forward(X_fooling, mode='test') # Score size (N,100)
# The loss function we want to optimize(maximize)
# loss = scores[np.arange(N), target_y]                 # Size (N,)
# print loss
# The gradient of this loss wih respect to the input image
dscores = np.zeros_like(scores)
dscores[np.arange(N), target_y] = 1.0
X_fooling += lr*dX
y_pred = model.loss(X_fooling).argmax(axis=1)
i+=1
print 'Iteration %d: current class: %d; target class: %d ' % (i, y_pred, target_y)

return X_fooling```

Left: original image, Middle: fooling image, Right: difference

05

1.Class visualization

```def create_class_visualization(target_y, model, **kwargs):

learning_rate = kwargs.pop('learning_rate', 10000)
blur_every = kwargs.pop('blur_every', 1)
l2_reg = kwargs.pop('l2_reg', 1e-6)
max_jitter = kwargs.pop('max_jitter', 4)
num_iterations = kwargs.pop('num_iterations', 200)
show_every = kwargs.pop('show_every', 25)

X = np.random.randn(1, 3, 64, 64)
for t in xrange(num_iterations):
# As a regularizer, add random jitter to the image
ox, oy = np.random.randint(-max_jitter, max_jitter+1, 2)
X = np.roll(np.roll(X, ox, -1), oy, -2)

# Compute the score and gradient
scores, cache = model.forward(X, mode='test')
# loss = scores[0, target_y] - l2_reg*np.sum(X**2)
dscores = np.zeros_like(scores)
dscores[0, target_y] = 1.0
dX -= 2*l2_reg*X

X += learning_rate*dX

# Undo the jitter
X = np.roll(np.roll(X, -ox, -1), -oy, -2)

# As a regularizer, clip the image
X = np.clip(X, -data['mean_image'], 255.0 - data['mean_image'])

# As a regularizer, periodically blur the image
if t % blur_every == 0:
X = blur_image(X)

# Periodically show the image
if t % show_every == 0:
print 'The loss is %f' % loss
plt.imshow(deprocess_image(X, data['mean_image']))
plt.gcf().set_size_inches(3, 3)
plt.axis('off')
plt.title('Iteration: %d' % t)
plt.show()

return X```

Generated images

2.Feature Inversion

`def invert_features(target_feats, layer, model, **kwargs): ` `learning_rate = kwargs.pop('learning_rate', 10000) ` `num_iterations = kwargs.pop('num_iterations', 500) ` `l2_reg = kwargs.pop('l2_reg', 1e-7) ` `blur_every = kwargs.pop('blur_every', 1) ` `show_every = kwargs.pop('show_every', 50) ` `X = np.random.randn(1, 3, 64, 64) ` `for t in xrange(num_iterations): ` `# Forward until target layer ` `feats, cache = model.forward(X, end=layer, mode='test') ` `# Compute the loss ` `loss = np.sum((feats-target_feats)**2) + l2_reg*np.sum(X**2) ` `# Compute the gradient of the loss with respect to the activation ` `dfeats = 2*(feats-target_feats) ` `dX, grads = model.backward(dfeats, cache) ` `dX += 2*l2_reg*X ` `X -= learning_rate*dX ` `# As a regularizer, clip the image ` ` X = np.clip(X, -data['mean_image'], 255.0 - data['mean_image']) ` `# As a regularizer, periodically blur the image ` `if (blur_every > 0) and t % blur_every == 0: ` `X = blur_image(X) ` `if (show_every > 0) and (t % show_every == 0 or t + 1 == num_iterations): print loss ` `plt.imshow(deprocess_image(X, data['mean_image'])) plt.gcf().set_size_inches(3, 3) ` `plt.axis('off') ` `plt.title('Iteration: %d' % t) ` `plt.show()`

Shallow feature reconstruction

Deep feature reconstruction

3.DeepDream

```def deepdream(X, layer, model, **kwargs):
X = X.copy()
learning_rate = kwargs.pop('learning_rate', 5.0)
max_jitter = kwargs.pop('max_jitter', 16)
num_iterations = kwargs.pop('num_iterations', 200)
show_every = kwargs.pop('show_every', 50)

for t in tqdm(xrange(num_iterations)):
# As a regularizer, add random jitter to the image
ox, oy = np.random.randint(-max_jitter, max_jitter+1, 2)
X = np.roll(np.roll(X, ox, -1), oy, -2)

# Forward until dreaming layer
fea, cache = model.forward(X, end=layer ,mode='test')

# Set the gradient equal to the feature
dfea = fea
X += learning_rate*dX

# Undo the jitter
X = np.roll(np.roll(X, -ox, -1), -oy, -2)

# As a regularizer, clip the image
mean_pixel = data['mean_image'].mean(axis=(1, 2), keepdims=True)
X = np.clip(X, -mean_pixel, 255.0 - mean_pixel)

# Periodically show the image
if t == 0 or (t + 1) % show_every == 0:
img = deprocess_image(X, data['mean_image'], mean='pixel')
plt.imshow(img)
plt.title('Iteration: %d' % (t + 1))
plt.gcf().set_size_inches(8, 8)
plt.axis('off')
plt.show()

return X```

Tibidabo

Deeplayer

Leaning Tower

424 篇文章81 人订阅

0 条评论

## 相关文章

### Bagging算法(R语言)

Bagging算法（bootstrap aggregation）由Leo Breiman提出。是一种在每个自助样本集上建立基分类器，通过投票指派得到测试样本最终...

37810

### 读论文系列：Object Detection ICCV2015 Fast RCNN

Fast RCNN是对RCNN的性能优化版本，在VGG16上，Fast R-CNN训练速度是RCNN的9倍, 测试速度是RCNN213倍；训练速度是SPP-ne...

3606

4008

3467

1252

3418

3889

### BP神经网络

BP(Back Propagation)神经网络是1986年由以Rumelhart和McCelland为首的科学家小组提出的，是一种按误差逆传播算法训练的多层前...

2579

2106

762