def test_compute_error(self, classes): mnist = MNIST() xin, _ = mnist.get_batch(30, dataset='testing', classes=classes) xout, _ = mnist.get_batch( 6, dataset='testing', classes=[x for x in range(10) if x not in classes]) x = np.concatenate((xin, xout)) np.random.shuffle(x) errors = [] for img in x: errors.append(self.compute_error(img, expand_dims=True)) top_k_error = np.sort(errors)[-6] errors = np.reshape(errors, (6, 6)) input_images = self.weights_to_grid(x.transpose((1, 2, 3, 0)), 6, 6) fig, (ax0, ax1, ax2) = plt.subplots(ncols=3, figsize=(10, 5)) fig.suptitle("Trained with classes") ax0.imshow(input_images, cmap=plt.cm.gray, interpolation='nearest') ax0.set_title('input images') shown = ax1.imshow(errors, cmap=plt.cm.inferno, interpolation='nearest') plt.colorbar(shown, ax=ax1) ax2.imshow(errors >= top_k_error, plt.cm.inferno, interpolation="nearest") ax1.set_title('Errors') plt.show()
def train(self, batch_size, passes, new_training=True, classes=list(range(10))): """ :param batch_size: :param passes: :param new_training: :return: """ mnist = MNIST() with tf.Session() as sess: # prepare session if new_training: saver, global_step = Model.start_new_session(sess) else: saver, global_step = Model.continue_previous_session( sess, ckpt_file='saver/checkpoint') # start training for step in range(1 + global_step, 1 + passes + global_step): x, y = mnist.get_batch(batch_size, classes=classes) self.training.run(feed_dict={self.x: x}) if step % 10 == 0: loss = self.loss.eval(feed_dict={self.x: x}) print("pass {}, training loss {}".format(step, loss)) if step % 100 == 0: # save weights saver.save(sess, 'saver/cnn', global_step=step) print('checkpoint saved')
def lime(model): #process image mnist = MNIST() image, label = mnist.get_batch(1, dataset='testing') image = image[0] image = np.concatenate((image, image, image), axis=2) print(image.shape) print('Print image:') plt.imshow(np.squeeze(image) / 2 + 0.5) plt.show() print(model.compute_error(image, expand_dims=True)) #Explain explainer = lime_image.LimeImageExplainer() explanation = explainer.explain_instance(image, model.compute_error, top_labels=2) print(explanation) #Show superpixels temp, mask = explanation.get_image_and_mask(explanation.top_labels[0], positive_only=True, hide_rest=True) #pdb.set_trace() print('Print superpixels:') marked = seg.mark_boundaries(temp / 2 + 0.5, mask).astype(np.uint8) plt.imshow(marked) plt.show()
def reconstruct(self): """ """ def weights_to_grid(weights, rows, cols): """convert the weights tensor into a grid for visualization""" height, width, in_channel, out_channel = weights.shape padded = np.pad(weights, [(1, 1), (1, 1), (0, 0), (0, rows * cols - out_channel)], mode='constant', constant_values=0) transposed = padded.transpose((3, 1, 0, 2)) reshaped = transposed.reshape((rows, -1)) grid_rows = [ row.reshape((-1, height + 2, in_channel)).transpose((1, 0, 2)) for row in reshaped ] grid = np.concatenate(grid_rows, axis=0) return grid.squeeze() mnist = MNIST() with tf.Session() as sess: saver, global_step = Model.continue_previous_session( sess, ckpt_file='saver/checkpoint') # visualize weights first_layer_weights = tf.get_default_graph().get_tensor_by_name( "conv_1/kernel:0").eval() grid_image = weights_to_grid(first_layer_weights, 4, 8) fig, ax0 = plt.subplots(ncols=1, figsize=(8, 4)) ax0.imshow(grid_image, cmap=plt.cm.gray, interpolation='nearest') ax0.set_title('first conv layers weights') plt.show() # visualize results batch_size = 32 x, y = mnist.get_batch(batch_size, dataset='testing') org, recon = sess.run((self.x, self.reconstruction), feed_dict={self.x: x}) input_images = weights_to_grid(org.transpose((1, 2, 3, 0)), 6, 6) recon_images = weights_to_grid(recon.transpose((1, 2, 3, 0)), 6, 6) fig, (ax0, ax1) = plt.subplots(ncols=2, figsize=(10, 5)) ax0.imshow(input_images, cmap=plt.cm.gray, interpolation='nearest') ax0.set_title('input images') ax1.imshow(recon_images, cmap=plt.cm.gray, interpolation='nearest') ax1.set_title('reconstructed images') plt.show()
def train(self, batch_size, passes, new_training=True): """ :param batch_size: :param passes: :param new_training: :return: """ mnist = MNIST() with tf.Session() as sess: # prepare session if new_training: saver, global_step = Model.start_new_session(sess) else: saver, global_step = Model.continue_previous_session( sess, ckpt_file='./saver/checkpoint') print("mnist mid point") # start training for step in range(1 + global_step, 1 + passes + global_step): print(str(step) + " entered") x, y = mnist.get_batch(batch_size) print(x.shape) #print(y.shape) print("got x and y") self.training.run(feed_dict={self.x: x}) print("imported from mnist.py") if step % 10 == 0: loss = self.loss.eval(feed_dict={self.x: x}) print("pass {}, training loss {}".format(step, loss)) if step % 5000 == 0: # save weights saver.save(sess, './saver/cnn', global_step=step) print('checkpoint saved') print(str(step) + " exited") del x
def reconstruct(self, vis_weights=True, classes="all"): """ """ def weights_to_grid(weights, rows, cols): """convert the weights tensor into a grid for visualization""" height, width, in_channel, out_channel = weights.shape padded = np.pad(weights, [(1, 1), (1, 1), (0, 0), (0, rows * cols - out_channel)], mode='constant', constant_values=0) transposed = padded.transpose((3, 1, 0, 2)) reshaped = transposed.reshape((rows, -1)) grid_rows = [ row.reshape((-1, height + 2, in_channel)).transpose((1, 0, 2)) for row in reshaped ] grid = np.concatenate(grid_rows, axis=0) return grid.squeeze() mnist = MNIST() with tf.Session() as sess: saver, global_step = Model.continue_previous_session( sess, ckpt_file='saver/checkpoint') # visualize weights if vis_weights: first_layer_weights = tf.get_default_graph( ).get_tensor_by_name("conv_1/kernel:0").eval() grid_image = weights_to_grid(first_layer_weights, 4, 8) fig, ax0 = plt.subplots(ncols=1, figsize=(8, 4)) ax0.imshow(grid_image, cmap=plt.cm.gray, interpolation='nearest') ax0.set_title('first conv layers weights') plt.show() # visualize results batch_size = 36 x, y = mnist.get_batch(batch_size, dataset='testing') org, recon = sess.run((self.x, self.reconstruction), feed_dict={self.x: x}) diff = (org - recon).squeeze() magnitude = np.linalg.norm(org.squeeze(), axis=(1, 2)) error = np.linalg.norm(diff, axis=(1, 2)) print(np.mean(error)) error = error / magnitude input_images = weights_to_grid(org.transpose((1, 2, 3, 0)), 6, 6) recon_images = weights_to_grid(recon.transpose((1, 2, 3, 0)), 6, 6) errors = np.reshape(error, (6, 6)) fig, (ax0, ax1, ax2) = plt.subplots(ncols=3, figsize=(10, 5)) fig.suptitle("Trained with classes: {}".format(classes)) ax0.imshow(input_images, cmap=plt.cm.gray, interpolation='nearest') ax0.set_title('input images') ax1.imshow(recon_images, cmap=plt.cm.gray, interpolation='nearest') ax1.set_title('reconstructed images') shown = ax2.imshow(errors, cmap=plt.cm.inferno, interpolation='nearest') plt.colorbar(shown, ax=ax2) ax2.set_title('Errors') plt.show()