CS231n割り当て3第3四半期のエクスペリエンスノート



Cs231n Assignment3 Q3 Experience Notes



ネットワークの視覚化


オンラインクラスの1学期が終わりました... CS231nの宿題は3か月間停止し、人間は怠惰で不活性な動物であることが判明しました。第1四半期にAssignment1を書き始めてから、ほぼ1年が経ちましたが、今日はほぼ1年になります。私は本当に高度な先延ばしの患者です...まあ、時間内にそれを補います。ネットワークの視覚化についてこのブログを書いた後、私は夏に乾いた気分になります!

3番目の部分は、ニューラルネットワークによって特定のカテゴリとして判断できるランダムノイズ画像から画像を生成することについてです。コード部分は以前と同じなので、これ以上紹介しません。

def create_class_visualization(target_y, model, dtype, **kwargs): ''' Generate an image to maximize the score of target_y under a pretrained model. Inputs: - target_y: Integer in the range [0, 1000) giving the index of the class - model: A pretrained CNN that will be used to generate the image - dtype: Torch datatype to use for computations Keyword arguments: - l2_reg: Strength of L2 regularization on the image - learning_rate: How big of a step to take - num_iterations: How many iterations to use - blur_every: How often to blur the image as an implicit regularizer - max_jitter: How much to gjitter the image as an implicit regularizer - show_every: How often to show the intermediate result ''' model.type(dtype) l2_reg = kwargs.pop('l2_reg', 1e-3) learning_rate = kwargs.pop('learning_rate', 25) num_iterations = kwargs.pop('num_iterations', 100) blur_every = kwargs.pop('blur_every', 10) max_jitter = kwargs.pop('max_jitter', 16) show_every = kwargs.pop('show_every', 25) # Randomly initialize the image as a PyTorch Tensor, and make it requires gradient. img = torch.randn(1, 3, 224, 224).mul_(1.0).type(dtype).requires_grad_() for t in range(num_iterations): # Randomly jitter the image a bit this gives slightly nicer results ox, oy = random.randint(0, max_jitter), random.randint(0, max_jitter) img.data.copy_(jitter(img.data, ox, oy)) ######################################################################## # TODO: Use the model to compute the gradient of the score for the # # class target_y with respect to the pixels of the image, and make a # # gradient step on the image using the learning rate. Don't forget the # # L2 regularization term! # # Be very careful about the signs of elements in your code. # ######################################################################## # *****START OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)***** scores = model(img) loss = scores[0][target_y] - l2_reg * torch.sum((img**2)).item() loss.backward() img.data += learning_rate * img.grad.data / img.grad.data.norm() img.grad.data.zero_() # *****END OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)***** ######################################################################## # END OF YOUR CODE # ######################################################################## # Undo the random jitter img.data.copy_(jitter(img.data, -ox, -oy)) # As regularizer, clamp and periodically blur the image for c in range(3): lo = float(-SQUEEZENET_MEAN[c] / SQUEEZENET_STD[c]) hi = float((1.0 - SQUEEZENET_MEAN[c]) / SQUEEZENET_STD[c]) img.data[:, c].clamp_(min=lo, max=hi) if t % blur_every == 0: blur_image(img.data, sigma=0.5) # Periodically show the image if t == 0 or (t + 1) % show_every == 0 or t == num_iterations - 1: plt.imshow(deprocess(img.data.clone().cpu())) class_name = class_names[target_y] plt.title('%s Iteration %d / %d' % (class_name, t + 1, num_iterations)) plt.gcf().set_size_inches(4, 4) plt.axis('off') plt.show() return deprocess(img.data.cpu())

ここで言及されているL2の通常の用語を考慮する必要があることに注意してください。



効果を見てみましょう。

画像
蜘蛛の一種で、見た目も毛皮のような匂いがします。不満を持った友達が、産卵したアシカを見に来ました。これは実像です。 (しかし、あなたがそれが無毛の鳥であると主張するならば、私はあなたを否定することはできません...



画像

さて、Q3のコードサイズは比較的小さいです。主にニューラルネットワークの視覚化の素晴らしい結果を見てみましょう。Q4でお会いしましょう。さようなら!