def render_icons( directions, model, layer, size=80, n_steps=128, verbose=False, S=None, num_attempts=3, cossim=True, alpha=False, ): model.load_graphdef() image_attempts = [] loss_attempts = [] depth = 4 if alpha else 3 batch = len(directions) input_shape = (batch, size, size, depth) # Render two attempts, and pull the one with the lowest loss score. for attempt in range(num_attempts): # Render an image for each activation vector param_f = lambda: param.image(size, batch=len(directions), fft=True, decorrelate=True, alpha=alpha) if cossim is True: obj_list = [ direction_neuron_cossim_S(layer, v, batch=n, S=S) for n, v in enumerate(directions) ] else: obj_list = [ direction_neuron_S(layer, v, batch=n, S=S) for n, v in enumerate(directions) ] obj_list += [objectives.penalize_boundary_complexity(input_shape, w=5)] obj = objectives.Objective.sum(obj_list) # holy mother of transforms transforms = [ transform.pad(16, mode='constant'), transform.jitter(4), transform.jitter(4), transform.jitter(8), transform.jitter(8), transform.jitter(8), transform.random_scale(0.998**n for n in range(20, 40)), transform.random_rotate( chain(range(-20, 20), range(-10, 10), range(-5, 5), 5 * [0])), transform.jitter(2), transform.crop_or_pad_to(size, size) ] if alpha: transforms.append(transform.collapse_alpha_random()) # This is the tensorflow optimization process # print("attempt: ", attempt) with tf.Graph().as_default(), tf.Session() as sess: learning_rate = 0.05 losses = [] trainer = tf.train.AdamOptimizer(learning_rate) T = render.make_vis_T(model, obj, param_f, trainer, transforms) vis_op, t_image = T("vis_op"), T("input") losses_ = [obj_part(T) for obj_part in obj_list] tf.global_variables_initializer().run() for i in range(n_steps): loss, _ = sess.run([losses_, vis_op]) losses.append(loss) # if i % 100 == 0: # print(i) img = t_image.eval() img_rgb = img[:, :, :, :3] if alpha: # print("alpha true") k = 0.8 bg_color = 0.0 img_a = img[:, :, :, 3:] img_merged = img_rgb * ( (1 - k) + k * img_a) + bg_color * k * (1 - img_a) image_attempts.append(img_merged) else: # print("alpha false") image_attempts.append(img_rgb) loss_attempts.append(losses[-1]) # Use only the icons with the lowest loss loss_attempts = np.asarray(loss_attempts) loss_final = [] image_final = [] # print("merging best scores from attempts...") for i, d in enumerate(directions): # note, this should be max, it is not a traditional loss mi = np.argmax(loss_attempts[:, i]) image_final.append(image_attempts[mi][i]) return (image_final, loss_final)
def test_penalize_boundary_complexity(inceptionv1): # TODO: is input shape really unknown at evaluation time? # TODO: is the sign correctly defined on this objective? It seems I need to invert it. objective = objectives.penalize_boundary_complexity([1, 32, 32, 3]) assert_gradient_ascent(-1 * objective, inceptionv1)