runs = [run_1, run_2, run_3, run_4, run_5, run_6, run_7] num_seeds = 3 target = True naive = True if __name__ == "__main__": # Generate num_seeds number of weight initialisation runs. for j in range(len(runs)): if target: run = runs[j] elif naive: run = Run.restore('exp_13', 1, num_seeds, naive=False, save_opp=True) else: run = Run.restore('exp_13', 1, num_seeds, save_opp=True) gen_graphs(run, save_opp=True) if target or naive: for i in range(num_seeds): print("Current seed = " + str(i)) # Calls source network function if target: transfer_networks.network(i, run, run.hyperparameters) run.save(naive=False) # Calls naive network function if naive:
# This model must already use linear activation for the final layer loss = model.layers[layer_idx].output[..., class_idx] grad_tensor = K.gradients(loss, layer_input)[0] # create function that evaluate the gradient for a given input # This function accept numpy array derivative_fn = K.function([layer_input], [grad_tensor]) # evaluate the derivative_fn grad_eval_by_hand = derivative_fn([img[np.newaxis, ...]])[0] print(grad_eval_by_hand.shape) grad_eval_by_hand = np.abs(grad_eval_by_hand).max(axis=(0, 3)) # normalize to range between 0 and 1 arr_min, arr_max = np.min(grad_eval_by_hand), np.max(grad_eval_by_hand) grad_eval_by_hand = (grad_eval_by_hand - arr_min) / (arr_max - arr_min + K.epsilon()) plt.imsave(create_path(run.path, model_type, "images", "sal_image_{}_alt.png".format(seed)), grad_eval_by_hand) if __name__ == "__main__": r = Run.restore(exp_name, run_num, 3, save_opp=True) for network in ['target', 'naive', 'source']: for seed in range(0, 3): saliency_map(r, network, seed, dataset_type, attempt, category=category, positive=True) print("{} network done for seed {}".format(network, seed)) # for i in range(0, 3): # saliency_map(r, 'target', i, "train", 0, category="dog", positive=True)