def validation(epoch): model.eval() validation_loss = 0 correct = 0 for data, target in val_loader: if use_cuda: data, target = data.cuda(), target.cuda() output = model(data) # sum up batch loss criterion = torch.nn.CrossEntropyLoss(reduction='elementwise_mean') validation_loss += criterion(output, target).data.item() # get the index of the max log-probability pred = output.data.max(1, keepdim=True)[1] valid = pred.eq(target.data.view_as(pred)).cpu() wrong_images = np.argwhere(valid.numpy() == 0)[:, 0] correct += valid.sum() if epoch > 15: show_images(data, 5, indexes=wrong_images) plt.show() validation_loss /= len(val_loader.dataset) print( '\nValidation set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'. format(validation_loss, correct, len(val_loader.dataset), 100. * correct / len(val_loader.dataset)))
def show_move(self, direction, show_full_grid=False): if isinstance(direction, str): key_map = { "W": 1, "A": 2, "S": 3, "D": 0, "w": 1, "a": 2, "s": 3, "d": 0 } direction = key_map[direction] plt.close() (reward, terminal) = self.move(direction) # self.get_view() if show_full_grid: view = self.view.clone() grid = self.environment.grid.clone() view[self.range, self.range] = 1. grid[tuple(self.environment.agent_point)] = 1. tools.show_images([view.numpy(), grid.numpy()], titles=["Agent perspective", "Maze map"]) else: self.show_view() return reward, terminal
def unprocess(image, mean_pixel): return image + mean_pixel if __name__ == '__main__': pretrained_vgg_19_path = '/home/meizu/WORK/code/neural_style/model/imagenet-vgg-verydeep-19.mat' test_image_path = '/home/meizu/WORK/code/neural_style/images/1-content.jpg' figures_save_path = '/home/meizu/WORK/code/neural_style/figures' # read image test_image = tools.imread(test_image_path) # load pretrained vgg19 weights weights, mean_pixel = load_weights(pretrained_vgg_19_path) # preprocess the test image test_image_preprocessed = np.array([preprocess(test_image, mean_pixel) ]).astype(np.float32) # infer the input image with vgg19 imput_image_infered = net_infer(weights, test_image_preprocessed) sess = tf.InteractiveSession() aa = sess.run(imput_image_infered['relu4_2']) aa = aa.reshape(aa.shape[1], aa.shape[2], aa.shape[3]) aa_for_plot = [] for i in range(aa.shape[2]): aa_for_plot.append(aa[:, :, i]) tools.show_images(aa_for_plot[::8], cols=8) print ''
w, h = gray.shape rgb = np.empty((w, h, 3), dtype=np.float32) rgb[:, :, 2] = rgb[:, :, 1] = rgb[:, :, 0] = gray return rgb if __name__ == '__main__': # prepare the content image for style transform # and the specified style image content_image_path = '../images/1-content.jpg' style_image_path = '../images/1-style.jpg' content_image = tools.imread(content_image_path) style_image = tools.imread(style_image_path) style_images = [style_image] # for multi-styles tools.show_images([content_image, style_image], titles=['content image', 'style'], axis='on') # pretrained vgg path pre_trained_vgg_path = '../model/imagenet-vgg-verydeep-19.mat' # set content image width for model input # and style_scale for scaling the style image width = 384 if width is not None: new_shape = (int( math.floor( float(content_image.shape[0]) / content_image.shape[1] * width)), width) content_image = misc.imresize(content_image, new_shape).astype(np.float32)
generated_images - content_images) * 2 / tf.to_float( size) # remain the same as in the paper return content_loss def total_variation_loss(layer): shape = tf.shape(layer) height = shape[1] width = shape[2] y = tf.slice(layer, [0, 0, 0, 0], tf.stack([-1, height - 1, -1, -1])) - tf.slice( layer, [0, 1, 0, 0], [-1, -1, -1, -1]) x = tf.slice(layer, [0, 0, 0, 0], tf.stack([-1, -1, width - 1, -1])) - tf.slice( layer, [0, 0, 1, 0], [-1, -1, -1, -1]) loss = tf.nn.l2_loss(x) / tf.to_float( tf.size(x)) + tf.nn.l2_loss(y) / tf.to_float(tf.size(y)) return loss if __name__ == '__main__': import argparse import tools parser = argparse.ArgumentParser() parser.add_argument('--conf', default='../conf/candy.yml') parser.add_argument('--loss_model_file', default='../models/vgg_16.ckpt') args = parser.parse_args() FLAGS = utils.read_conf_file(args.conf) features = get_style_features(FLAGS) tools.show_images(features, cols=2) print("")