def start(self): self.isRunning = True self.status = 'starting' if not (os.path.isfile(self.modelFilePath)): downloadModel(self) raise "model not found" warnings.simplefilter(action='ignore', category=FutureWarning) #some params dropoutPro = 1 classNum = 1000 skip = [] x = tf.placeholder("float", [1, 224, 224, 3]) model = vgg19.VGG19(x, dropoutPro, classNum, skip, modelPath=self.modelFilePath) score = model.fc8 softmax = tf.nn.softmax(score) with tf.Session() as session: session.run(tf.global_variables_initializer()) model.loadModel(session) self.status = 'running' while (self.isRunning): self.work(session, softmax, x)
def main(): # parse arguments args = parse_args() # initiate VGG19 model model_file_path = args.model_path + '/' + vgg19.MODEL_FILE_NAME vgg_net = vgg19.VGG19(model_file_path) # open session sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) # build the graph st = bsd_net.BSD_net(session=sess, training_images_path=args.train_input, training_gt_path=args.train_gt, training_sobel_path=args.train_sobel, output_folder=args.output_folder, net=vgg_net, content_loss_norm_type=args.content_loss_norm_type) # launch the graph in a session st.runme() # close session sess.close() print('current session is finished ok')
def main(argv): model_file_path = os.path.join(FLAGS.vgg_model, vgg19.MODEL_FILE_NAME) vgg_net = vgg19.VGG19(model_file_path) content_images = utils.get_files(FLAGS.train) style_image = utils.load_image(FLAGS.style) # create a map for content layers info CONTENT_LAYERS = {} for layer, weight in zip(CONTENT_LAYERS_NAME, CONTENT_LAYER_WEIGHTS): CONTENT_LAYERS[layer] = weight # create a map for style layers info STYLE_LAYERS = {} for layer, weight in zip(STYLE_LAYERS_NAME, STYLE_LAYER_WEIGHTS): STYLE_LAYERS[layer] = weight with tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) as sess: trainer = style_transfer_trainer.StyleTransferTrainer( session=sess, content_layer_ids=CONTENT_LAYERS, style_layer_ids=STYLE_LAYERS, content_images=content_images, style_image=add_one_dim(style_image), net=vgg_net, num_epochs=FLAGS.num_epochs, batch_size=FLAGS.batch_size, content_weight=FLAGS.content_weight, style_weight=FLAGS.style_weight, tv_weight=FLAGS.tv_weight, learn_rate=FLAGS.learn_rate, save_path=FLAGS.output, check_period=FLAGS.checkpoint_every, max_size=FLAGS.max_size or None) trainer.train()
def get_image_style(self, model, model_type, style_layers): ''' uses VGG19 model to get the features representing style of an image it is dependent on the model represented to extract these features''' if model_type is 'matconvnet': #Assumption matconvnet model is only provided, in future may add support for using #other saved models graph = tf.Graph() with graph.as_default(), tf.Session() as sess: vgg_model = vgg19.VGG19() model_weights, image_mean_value = vgg_model.read_model_from_matconvnet( model) input_image = tf.constant(np.reshape(self.image_matrix - image_mean_value, (1,) + self.image_matrix.shape)\ , name='input_image') vgg_model.get_model_from_matconvnet(input_image, model_weights, pool_type="max") tensor_list_style_layers = [ vgg_model.layer_dict[layer]['tensor_value'] for layer in style_layers ] style_layer_values = sess.run(tensor_list_style_layers) image_style = [] for layer in style_layer_values: reshaped_layer = np.reshape(layer, [-1, layer.shape[3]]) gram_matrix = np.matmul(reshaped_layer.T, reshaped_layer) image_style.append(gram_matrix) return image_style
def main(): # parse arguments args = parse_args() if args is None: exit() # initiate VGG19 model model_file_path = args.vgg_model + '/' + vgg19.MODEL_FILE_NAME vgg_net = vgg19.VGG19(model_file_path) # get file list for training content_images = utils.get_files(args.trainDB_path) # load style image style_image = utils.load_image(args.style) # create a map for content layers info CONTENT_LAYERS = {} for layer, weight in zip(args.content_layers, args.content_layer_weights): CONTENT_LAYERS[layer] = weight # create a map for style layers info STYLE_LAYERS = {} for layer, weight in zip(args.style_layers, args.style_layer_weights): STYLE_LAYERS[layer] = weight # open session config = tf.ConfigProto(allow_soft_placement=True) config.gpu_options.allow_growth = True config.gpu_options.per_process_gpu_memory_fraction = 0.5 sess = tf.Session(config=config) # build the graph for train trainer = style_transfer_trainer.StyleTransferTrainer( session=sess, content_layer_ids=CONTENT_LAYERS, style_layer_ids=STYLE_LAYERS, content_images=content_images, style_image=add_one_dim(style_image), net=vgg_net, num_epochs=args.num_epochs, batch_size=args.batch_size, content_weight=args.content_weight, style_weight=args.style_weight, tv_weight=args.tv_weight, learn_rate=args.learn_rate, save_path=args.output, check_period=args.checkpoint_every, test_image=args.test, max_size=args.max_size, ) # launch the graph in a session trainer.train() # close session sess.close()
def main(): args = parse_args() if args is None: exit() #Get VGG19 model_file_path = args.model_path + '/' + vgg19.MODEL_FILE_NAME vgg_net = vgg19.VGG19(model_file_path) # load content image and style image content_image = utils.load_image(args.content, max_size=args.max_size) style_image = utils.load_image(args.style, shape=(content_image.shape[1],content_image.shape[0])) CONTENT_LAYERS = {} for layer, weight in zip(args.content_layers,args.content_layer_weights): CONTENT_LAYERS[layer] = weight STYLE_LAYERS = {} for layer, weight in zip(args.style_layers, args.style_layer_weights): STYLE_LAYERS[layer] = weight # initial guess for output if args.initial_type == 'content': init_image = content_image elif args.initial_type == 'style': init_image = style_image elif args.initial_type == 'random': init_image = np.random.normal(size=content_image.shape, scale=np.std(content_image)) sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) # build the picture st = style.StyleTransfer(session = sess, content_layer_ids = CONTENT_LAYERS, style_layer_ids = STYLE_LAYERS, init_image = add_one_dim(init_image), content_image = add_one_dim(content_image), style_image = add_one_dim(style_image), net = vgg_net, num_iter = args.num_iter, loss_ratio = args.loss_ratio, style_ratio = args.style_ratio, content_loss_norm_type = args.content_loss_norm_type, ) result_image = st.update() sess.close() shape = result_image.shape result_image = np.reshape(result_image,shape[1:]) # save result utils.save_image(result_image,args.output)
def main(): args = parse_args() model_file_path = args.model_path + '/' + vgg19.MODEL_FILE_NAME vgg_net = vgg19.VGG19(model_file_path) content_image = utils.load_image(args.content, max_size=args.max_size) style_image = [] for style_image_path in args.style: style_image.append(utils.load_image(style_image_path, shape=(content_image.shape[1], content_image.shape[0]))) style_image = np.array(style_image) content_mask = None if args.content_mask is not None: content_mask = utils.load_image(args.content_mask, shape=(content_image.shape[1], content_image.shape[0])) content_mask = content_mask/255. # initial guess for output if args.initial_type == 'content': init_image = content_image elif args.initial_type == 'style': init_image = style_image elif args.initial_type == 'random': init_image = np.random.normal(size=content_image.shape, scale=np.std(content_image)) CONTENT_LAYERS = {} for layer, weight in zip(args.content_layers, args.content_layer_weights): CONTENT_LAYERS[layer] = weight STYLE_LAYERS = {} for layer, weight in zip(args.style_layers, args.style_layer_weights): STYLE_LAYERS[layer] = weight # open session config = tf.ConfigProto(allow_soft_placement=True) config.gpu_options.allow_growth = True sess = tf.Session(config=config) # build the graph st = StyleTransfer(session=sess, content_layer_ids=CONTENT_LAYERS, style_layer_ids=STYLE_LAYERS, init_image=add_one_dim(init_image), content_image=add_one_dim(content_image), style_image=style_image, net=vgg_net, num_iter=args.num_iter, loss_ratios=[args.loss_ratio_c, args.loss_ratio_tv], content_loss_norm_type=args.content_loss_norm_type, content_mask=content_mask) result_image = st.update() sess.close() shape = result_image.shape result_image = np.reshape(result_image, shape[1:]) utils.save_image(result_image, args.output)
def build_vgg19(self, x, reuse=None): with tf.variable_scope("vgg19", reuse=reuse): # image re-scaling x = tf.cast((x + 1) / 2, dtype=tf.float32) # [-1, 1] to [0, 1] x = tf.cast(x * 255., dtype=tf.float32) # [0, 1] to [0, 255] r, g, b = tf.split(x, 3, 3) bgr = tf.concat([b - self.vgg_mean[0], g - self.vgg_mean[1], r - self.vgg_mean[2]], axis=3) self.vgg19 = vgg19.VGG19(bgr) net = self.vgg19.vgg19_net['conv5_4'] return net # last layer
def build_fcn(self): vgg19_net = vgg19.VGG19(image=self.x) net = vgg19_net.vgg19_net['pool5'] net = t.conv2d(net, 4096, k=7, s=1, name='conv6_1') net = tf.nn.relu(net, name='relu6_1') net = tf.nn.dropout(net, self.do_rate, name='dropout-6_1') net = t.conv2d(net, 4096, k=1, s=1, name='conv7_1') net = tf.nn.relu(net, name='relu7_1') net = tf.nn.dropout(net, self.do_rate, name='dropout-7_1') feature = t.conv2d(net, self.n_classes, k=1, s=1, name='conv8_1') net = t.deconv2d(feature, vgg19_net.vgg19_net['pool4'].get_shape()[3], name='deconv_1') net = tf.add(net, vgg19_net.vgg19_net['pool4'], name='fuse_1')
def stylize(content_img, style_img, init_img, frame=None): with tf.device('/gpu:0'), tf.Session() as sess: # setup network vgg = vgg19.VGG19(content_img, vgg_path=vgg_path) net = vgg.get_model() # style loss L_style = sum_style_losses(sess, net, style_img) # content loss L_content = sum_content_losses(sess, net, content_img) # denoising loss L_tv = tf.image.total_variation(net['input']) # loss weights alpha = content_weight beta = style_weight theta = total_variational_loss_weight # total loss L_total = alpha * L_content L_total += beta * L_style L_total += theta * L_tv # video temporal loss if args.video and frame > 1: gamma = temporal_weight L_temporal = sum_shortterm_temporal_losses(sess, net, frame, init_img) L_total += gamma * L_temporal # optimization algorithm optimizer = get_optimizer(L_total) minimize_with_lbfgs(sess, net, optimizer, init_img) output_img = sess.run(net['input']) if args.video: write_video_output(frame, output_img) else: write_image_output(output_img, content_img, style_img, init_img)
def load_full_model(model_name, random_weights=False, no_cats=2, weight_decay=0, activation='softmax'): """ Loads a model with a randomly initialized last layer model_name: ResNet50, VGG19 random_weights: Random weights or ImageNet pre-training no_cats: Number of outputs weight decay: L2 weight decay for all layers activation: Activation of the final layer (None, softmax, sigmoid) """ input_tensor = keras.layers.Input(shape=(224, 224, 3)) if random_weights: weights = None else: weights = 'imagenet' if model_name == 'ResNet50': full_model = resnet50.ResNet50(weights=weights, input_tensor=input_tensor, weight_decay=weight_decay, no_cats=no_cats, activation=activation) elif model_name == 'VGG19': full_model = vgg19.VGG19(weights=weights, input_tensor=input_tensor, weight_decay=weight_decay, no_cats=no_cats, activation=activation) else: raise ValueError('Invalid model_name') return full_model
TV_WEIGHT = 2e2 CONTENT_LAYERS = ["relu4_2"] STYLE_LAYERS = ["relu1_1", "relu2_1", "relu3_1", "relu4_1", "relu5_1"] CONTENT_LAYER_WEIGHTS = [1.0] STYLE_LAYER_WEIGHTS = [0.2, 0.2, 0.2, 0.2, 0.2] LEARN_RATE = 1e-3 NUM_EPOCHS = 2 BATCH_SIZE = 4 tf.compat.v1.disable_eager_execution() if __name__ == "__main__": model_file_path = VGG_MODEL vgg_net = vgg19.VGG19(model_file_path) content_images = utils.get_files(TRAINDB_PATH) style_image = utils.load_image(STYLE) CONTENT_LAYERS_DICT = {} for layer, weight in zip(CONTENT_LAYERS, CONTENT_LAYER_WEIGHTS): CONTENT_LAYERS_DICT[layer] = weight STYLE_LAYERS_DICT = {} for layer, weight in zip(STYLE_LAYERS, STYLE_LAYER_WEIGHTS): STYLE_LAYERS_DICT[layer] = weight sess = tf.compat.v1.Session(config=tf.compat.v1.ConfigProto( allow_soft_placement=True))
def transfer_runner(content_image, style_image): model_path = 'tfb/pre_trained_model' #The directory where the pre-trained model was saved output = 'results/' + strftime( "%Y-%m-%d-%H:%M") + '.jpg' #File path of output image loss_ratio = 1e-3 #Weight of content-loss relative to style-loss content_layers = ['conv4_2'] #VGG19 layers used for content loss style_layers = ['relu1_1', 'relu2_1', 'relu3_1', 'relu4_1', 'relu5_1'] #VGG19 layers used for style loss content_layer_weights = [ 1.0 ] #Content loss for each content is multiplied by corresponding weight style_layer_weights = [ .2, .2, .2, .2, .2 ] #Style loss for each content is multiplied by corresponding weight initial_type = 'content' #choices = ['random','content','style'], The initial image for optimization (notation in the paper : x) max_size = 101 #512 #The maximum width or height of input images content_loss_norm_type = 3 #choices=[1,2,3], Different types of normalization for content loss num_iter = 400 #The number of iterations to run try: assert len(content_layers) == len(content_layer_weights) except: raise ('content layer info and weight info must be matched') try: assert len(style_layers) == len(style_layer_weights) except: raise ('style layer info and weight info must be matched') try: assert max_size > 100 except: raise ('Too small size') model_file_path = model_path + '/' + vgg19.MODEL_FILE_NAME assert os.path.exists(model_file_path) try: assert os.path.exists(model_file_path) except: raise Exception('There is no %s' % model_file_path) try: size_in_KB = os.path.getsize(model_file_path) assert abs(size_in_KB - 534904783) < 10 except: print('check file size of \'imagenet-vgg-verydeep-19.mat\'') print('there are some files with the same name') print('pre_trained_model used here can be downloaded from bellow') print( 'http://www.vlfeat.org/matconvnet/models/imagenet-vgg-verydeep-19.mat' ) raise () # initiate VGG19 model model_file_path = model_path + '/' + vgg19.MODEL_FILE_NAME vgg_net = vgg19.VGG19(model_file_path) # initial guess for output if initial_type == 'content': init_image = content_image elif initial_type == 'style': init_image = style_image elif initial_type == 'random': init_image = np.random.normal(size=content_image.shape, scale=np.std(content_image)) # check input images for style-transfer # utils.plot_images(content_image,style_image, init_image) # create a map for content layers info CONTENT_LAYERS = {} for layer, weight in zip(content_layers, content_layer_weights): CONTENT_LAYERS[layer] = weight # create a map for style layers info STYLE_LAYERS = {} for layer, weight in zip(style_layers, style_layer_weights): STYLE_LAYERS[layer] = weight with tf.Graph().as_default(): # open session sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) # build the graph st = StyleTransfer( session=sess, content_layer_ids=CONTENT_LAYERS, style_layer_ids=STYLE_LAYERS, init_image=add_one_dim(init_image), content_image=add_one_dim(content_image), style_image=add_one_dim(style_image), net=vgg_net, num_iter=num_iter, loss_ratio=loss_ratio, content_loss_norm_type=content_loss_norm_type, ) # launch the graph in a session result_image = st.update() # close session sess.close() # remove batch dimension shape = result_image.shape result_image = np.reshape(result_image, shape[1:]) # save result #utils.save_image(result_image,get_output_filepath(content, style)) return result_image
def main(): # parse arguments args = parse_args() if args is None: exit() # initiate VGG19 model model_file_path = args.model_path + '/' + vgg19.MODEL_FILE_NAME vgg_net = vgg19.VGG19(model_file_path) # load content image and style image content_image = utils.load_image(args.content, max_size=args.max_size) style_image = utils.load_image(args.style, shape=(content_image.shape[1], content_image.shape[0])) style_image2 = np.array([]) # 5.20 the second style image if args.multi_style == True: style_image2 = utils.load_image(args.style2, shape=(content_image.shape[1], content_image.shape[0])) # initial guess for output if args.initial_type == 'content': init_image = content_image elif args.initial_type == 'style': init_image = style_image elif args.initial_type == 'random': init_image = np.random.normal(size=content_image.shape, scale=np.std(content_image)) # check input images for style-transfer # utils.plot_images(content_image,style_image, init_image) # build a map from the name of content layers (like 'conv4_2') to its corresponding weight CONTENT_LAYERS = {} for layer, weight in zip(args.content_layers, args.content_layer_weights): CONTENT_LAYERS[layer] = weight # create a map for style layers info STYLE_LAYERS = {} for layer, weight in zip(args.style_layers, args.style_layer_weights): STYLE_LAYERS[layer] = weight # open session sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) # build the graph st = style_transfer.StyleTransfer( session=sess, content_layer_ids= CONTENT_LAYERS, # mapping from the name of content layer to its corresponding weight style_layer_ids= STYLE_LAYERS, # mapping from the name of style layer to its corresponding weight init_image=add_one_dim(init_image), content_image=add_one_dim(content_image), style_image=add_one_dim(style_image), net=vgg_net, num_iter=args.num_iter, loss_ratio=args.loss_ratio, content_loss_norm_type=args.content_loss_norm_type, style_image2=add_one_dim(style_image2), style_ratio=args.style_ratio, multi_style=args.multi_style, laplace=args.laplace, color_preserve=args.color_preserving, color_convert_type=args.color_convert_type, color_preserve_algo=args.color_preserve_algo, lap_lambda=args.lap_lambda, tv=args.tv, pooling_size=args.pooling_size) # launch the graph in a session result_image = st.update() # close session sess.close() # remove batch dimension shape = result_image.shape result_image = np.reshape(result_image, shape[1:]) # save result utils.save_image(result_image, args.output)
def main(unused_argv): puzzleset = PuzzleSet.read_data_sets(FLAGS.data_dir) if FLAGS.job_name is None or FLAGS.job_name == "": raise ValueError("Must specify an explicit `job_name`") if FLAGS.task_index is None or FLAGS.task_index == "": raise ValueError("Must specify an explicit `task_index`") print("job name = {0}".format(FLAGS.job_name)) print("job index = {0}".format(FLAGS.task_index)) # Construct the cluster and start the server ps_spec = FLAGS.ps_hosts.split(",") worker_spec = FLAGS.worker_hosts.split(",") # Get the number of workers. num_workers = len(worker_spec) cluster = tf.train.ClusterSpec({"ps": ps_spec, "worker": worker_spec}) # parameter servers stop here. if not FLAGS.existing_servers: # Not using existing servers. Create an in-process server. server = tf.train.Server(cluster, job_name=FLAGS.job_name, task_index=FLAGS.task_index) if FLAGS.job_name == "ps": server.join() is_chief = FLAGS.task_index == 0 # GPU = 0 cpu = 0 worker_device = "/job:worker/task:%d/cpu:%d" % (FLAGS.task_index, cpu) def _load_fn(unused_op): return 1 greedy = tf.contrib.training.GreedyLoadBalancingStrategy(1, _load_fn) with tf.device( tf.train.replica_device_setter(worker_device=worker_device, cluster=cluster, ps_strategy=greedy)): # use Parameter Server to persist the global step. global_step = tf.Variable(0, name="global_step", trainable=False) # the model train_mode = tf.placeholder(tf.bool) vgg = vgg19.VGG19() vgg.build(train_mode) # optimizer cross_entropy = -tf.reduce_sum( vgg.labels * tf.log(tf.clip_by_value(vgg.prob, 1e-10, 1.0))) opt = tf.train.AdamOptimizer(FLAGS.learning_rate) if FLAGS.sync_replicas: if FLAGS.replicas_to_aggregate is None: replicas_to_aggregate = num_workers else: replicas_to_aggregate = FLAGS.replicas_to_aggregate print("replicas_to_aggregate: " + str(replicas_to_aggregate)) opt = tf.train.SyncReplicasOptimizer( opt, replicas_to_aggregate=replicas_to_aggregate, total_num_replicas=num_workers, name="mnist_sync_replicas") # train step train_step = opt.minimize(cross_entropy, global_step=global_step) if FLAGS.sync_replicas: local_init_op = opt.local_step_init_op if is_chief: local_init_op = opt.chief_init_op ready_for_local_init_op = opt.ready_for_local_init_op # Initial token and chief queue runners required by the sync_replicas mode chief_queue_runner = opt.get_chief_queue_runner() sync_init_op = opt.get_init_tokens_op() init_op = tf.global_variables_initializer() train_dir = tempfile.mkdtemp() if FLAGS.sync_replicas: sv = tf.train.Supervisor( is_chief=is_chief, logdir=train_dir, init_op=init_op, local_init_op=local_init_op, ready_for_local_init_op=ready_for_local_init_op, recovery_wait_secs=1, global_step=global_step) else: sv = tf.train.Supervisor(is_chief=is_chief, logdir=train_dir, init_op=init_op, recovery_wait_secs=1, global_step=global_step) sess_config = tf.ConfigProto(allow_soft_placement=True, log_device_placement=False, device_filters=[ "/job:ps", "/job:worker/task:%d" % FLAGS.task_index ]) # The chief worker (task_index==0) session will prepare the session, # while the remaining workers will wait for the preparation to complete. if is_chief: print("Worker %d: Initializing session..." % FLAGS.task_index) else: print("Worker %d: Waitingfor session to be initialized..." % FLAGS.task_index) if FLAGS.existing_servers: server_grpc_url = "grpc://" + worker_spec[FLAGS.task_index] print("Using existing server at: %s" % server_grpc_url) sess = sv.prepare_or_wait_for_session(server_grpc_url, config=sess_config) else: sess = sv.prepare_or_wait_for_session(server.target, config=sess_config) print("Worker %d: Session initialization complete." % FLAGS.task_index) if FLAGS.sync_replicas and is_chief: # Chief worker will start the chief queue runner and call the init op. sess.run(sync_init_op) sv.start_queue_runners(sess, [chief_queue_runner]) # Perform training time_begin = time.time() print("Training begins @ %f" % time_begin) local_step = 0 while True: # Training feed batch_xs, batch_ys = puzzleset.train_next_batch(FLAGS.batch_size) train_feed = { vgg.images: batch_xs, vgg.labels: batch_ys, vgg.keep_prob: 0.5, train_mode: True } _, step = sess.run([train_step, global_step], feed_dict=train_feed) local_step += 1 now = time.time() print("%f: Worker %d: training step %d done (global step: %d)" % (now, FLAGS.task_index, local_step, step)) if step >= FLAGS.train_steps: break time_end = time.time() print("Training ends @ %f" % time_end) training_time = time_end - time_begin print("Training elapsed time: %f s" % training_time) # Validation feed val_images, val_labels = puzzleset.validation_batch() val_feed = { vgg.images: val_images, vgg.labels: val_labels, train_mode: False } val_xent = sess.run(cross_entropy, feed_dict=val_feed) print("After %d training step(s), validation cross entropy = %g" % (FLAGS.train_steps, val_xent))
import tensorflow as tf import numpy as np import vgg19 import utils import sys path = sys.argv[1] ut = utils.utils() img = ut.load_image(path) batch = img.reshape((1, 224, 224, 3)) with tf.Session() as sess: x = tf.placeholder(tf.float32, [None, 224, 224, 3]) vgg = vgg19.VGG19() vgg.build(x) prob = sess.run(vgg.prob, feed_dict={x: batch}) print('\nResults of %s: ' % path) ut.print_prob(prob[0])
STYLE_LOSS_WEIGHT = args["style_loss_weight"] TV_LOSS_WEIGHT = args["tv_loss_weight"] LEARNING_RATE = args["learning_rate"] ITERATIONS = args["iterations"] OPTIMIZER = args["optimizer"] INIT_TYPE = args["init_type"] PRESERVE_COLORS = args["preserve_colors"] CVT_TYPE = args["cvt_type"] CONTENT_FACTOR_TYPE = args["content_factor_type"] SAVE_IT = args["save_it"] SAVE_IT_DIR = args["save_it_dir"] net = vgg19.VGG19(MODEL_PATH) sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) st = StyleTransfer(sess, net, ITERATIONS, CONTENT_LAYERS, STYLE_LAYERS, content_image, style_image, CONTENT_LAYER_WEIGHTS, STYLE_LAYER_WEIGHTS, CONTENT_LOSS_WEIGHT, STYLE_LOSS_WEIGHT, TV_LOSS_WEIGHT, OPTIMIZER,
def build_vgg19(self): self.vgg19 = vgg19.VGG19(self.input_image) # load VGG19 model self.content_img -= self.vgg19.mean_pixels # normalize self.style_img -= self.vgg19.mean_pixels # normalize
detail_height, detail_width = detail.shape[:2] # detail = scipy.ndimage.zoom(detail, (1.0/octave_scale, 1.0/octave_scale, 1), order=1) print('resizing detail from %s to %s' % (detail.shape, octave_base.shape)) detail = imresize(detail, octave_base.shape[:2]) x = preprocess_image(octave_base + detail) dream = Input(shape=(3, img_shape[0], img_shape[1])) if args.model == 'resnet50': model = resnet50.ResNet50(include_top=False, input_tensor=dream) elif args.model == 'vgg16': model = vgg16.VGG16(include_top=False, input_tensor=dream) elif args.model == 'vgg19': model = vgg19.VGG19(include_top=False, input_tensor=dream) elif args.model == 'inception_v3': model = inception_v3.InceptionV3(include_top=False, input_tensor=dream) else: raise 'unknown model ' + args.model print('Model loaded.') loss_and_grads = create_loss_function(dream, settings, model, img_shape) evaluator = Evaluator(loss_and_grads) # run scipy-based optimization (L-BFGS) over the pixels of the generated image # so as to minimize the loss for i in range(10): print('Start of iteration', i) start_time = time.time()
def main(): # parse arguments args = parse_args() if args is None: exit() # initiate VGG19 model model_file_path = args.model_path + '/' + vgg19.MODEL_FILE_NAME vgg_net = vgg19.VGG19(model_file_path) # load content image and style image # content_image = utils.load_image(args.content, max_size=args.max_size) # style_image = utils.load_image(args.style, shape=(content_image.shape[1],content_image.shape[0])) content, c_tf, style, style_tf = utils.load_audio(args.style, args.content) # initial guess for output if args.initial_type == 'content': init_image = content elif args.initial_type == 'style': init_image = style elif args.initial_type == 'random': init_image = np.random.normal(size=content_image.shape, scale=np.std(content_image)) # check input images for style-transfer # utils.plot_images(content_image,style_image, init_image) # create a map for content layers info CONTENT_LAYERS = {} for layer, weight in zip(args.content_layers, args.content_layer_weights): CONTENT_LAYERS[layer] = weight # create a map for style layers info STYLE_LAYERS = {} for layer, weight in zip(args.style_layers, args.style_layer_weights): STYLE_LAYERS[layer] = weight # open session sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) # build the graph st = style_transfer.StyleTransfer( session=sess, content_layer_ids=CONTENT_LAYERS, style_layer_ids=STYLE_LAYERS, init_image=add_one_dim(init_image), content_image=add_one_dim(content), style_image=add_one_dim(style), net=vgg_net, num_iter=args.num_iter, loss_ratio=args.loss_ratio, content_loss_norm_type=args.content_loss_norm_type, ) # launch the graph in a session result_image = st.update() # close session sess.close() # remove batch dimension shape = result_image.shape result_image = np.reshape(result_image, shape[1:]) a = np.zeros_like(a_content) a[:N_CHANNELS, :] = np.exp(result_image[0, 0].T) - 1 # This code is supposed to do phase reconstruction p = 2 * np.pi * np.random.random_sample(a.shape) - np.pi for i in range(500): S = a * np.exp(1j * p) x = librosa.istft(S) p = np.angle(librosa.stft(x, N_FFT)) OUTPUT_FILENAME = 'outputs/out.wav' librosa.output.write_wav(OUTPUT_FILENAME, x, fs)
def main(): # parse arguments args = parse_args() if args is None: exit() # initiate VGG19 model model_file_path = args.model_path + '/' + vgg19.MODEL_FILE_NAME vgg_net = vgg19.VGG19(model_file_path) # load content image and style image content_image = utils.load_image(args.content, max_size=args.max_size) style_image = utils.load_image(args.style, shape=(content_image.shape[1],content_image.shape[0])) style_image = style_image[:,:,:3] # initial guess for output if args.initial_type == 'content': init_image = content_image elif args.initial_type == 'style': init_image = style_image elif args.initial_type == 'random': init_image = np.random.normal(size=content_image.shape, scale=np.std(content_image)) # check input images for style-transfer # utils.plot_images(content_image,style_image, init_image) # create a map for content layers info CONTENT_LAYERS = {} for layer, weight in zip(args.content_layers,args.content_layer_weights): CONTENT_LAYERS[layer] = weight # create a map for style layers info STYLE_LAYERS = {} for layer, weight in zip(args.style_layers, args.style_layer_weights): STYLE_LAYERS[layer] = weight # open session sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) # build the graph st = style_transfer.StyleTransfer(session = sess, content_layer_ids = CONTENT_LAYERS, style_layer_ids = STYLE_LAYERS, init_image = add_one_dim(init_image), content_image = add_one_dim(content_image), style_image = add_one_dim(style_image), net = vgg_net, num_iter = args.num_iter, loss_ratio = args.loss_ratio, content_loss_norm_type = args.content_loss_norm_type, ) # launch the graph in a session result_image = st.update() # close session sess.close() # remove batch dimension shape = result_image.shape result_image = np.reshape(result_image,shape[1:]) # save result utils.save_image(result_image,args.output)
model = alex.Alex() elif args.arch == 'googlenet': import googlenet model = googlenet.GoogLeNet() elif args.arch == 'vgga': import vgga model = vgga.vgga() elif args.arch == 'overfeat': import overfeat model = overfeat.overfeat() elif args.arch == 'vgg16': import vgg16 model = vgg16.VGG16() elif args.arch == 'vgg19': import vgg19 model = vgg19.VGG19() elif args.arch == 'unet': import unet model = unet.UNET() elif args.arch == 'resnet50': import resnet model = resnet.ResNet([3, 4, 6, 3]) elif args.arch == 'resnet101': import resnet model = resnet.ResNet([3, 4, 23, 3]) elif args.arch == 'resnet152': import resnet model = resnet.ResNet([3, 8, 36, 3]) else: raise ValueError('Invalid architecture name')
import style_transfer_trainer import utils import vgg19 from run_train import add_one_dim, \ CONTENT_LAYERS_NAME, STYLE_LAYERS_NAME, \ CONTENT_LAYER_WEIGHTS, STYLE_LAYER_WEIGHTS VIDEO_FILE = "/Users/dwang/Downloads/IMG_2693.mp4" VIDEO_OUT_FILE = "/Users/dwang/Downloads/IMG_2693.avi" MODEL_FILE = "/Users/dwang/transfer" STYLE_FILE = "style/wave.jpg" VGG_FILE = "/Users/dwang/transfer/imagenet-vgg-verydeep-19.mat" if __name__ == '__main__': style_image = utils.load_image(STYLE_FILE) vgg_net = vgg19.VGG19(VGG_FILE) CONTENT_LAYERS = {} for layer, weight in zip(CONTENT_LAYERS_NAME, CONTENT_LAYER_WEIGHTS): CONTENT_LAYERS[layer] = weight STYLE_LAYERS = {} for layer, weight in zip(STYLE_LAYERS_NAME, STYLE_LAYER_WEIGHTS): STYLE_LAYERS[layer] = weight cap = cv2.VideoCapture(VIDEO_FILE) fourcc = cv2.VideoWriter_fourcc('M', 'J', 'P', 'G') video_out = None with tf.Session() as sess: trainer = style_transfer_trainer.StyleTransferTrainer( session=sess, content_layer_ids=CONTENT_LAYERS,
def generate_image(self, model, model_type, content_image, content_layers, style_image, style_layers, content_weight, style_weight, num_iters, feed_back_inters, starting_image=None): '''it generates the image wose content features matches with the one provides in content_layers and style features matches with the one present in the style_layers content_weight = alpha style_weight = beta''' if model_type is 'matconvnet': #Assumption matconvnet model is only provided, in future may add support for using #other saved models content_layer_values = content_image.get_image_content( model, model_type, content_layers) style_layer_values = style_image.get_image_style( model, model_type, style_layers) print len(style_layer_values) for i in style_layer_values: print i.shape graph = tf.Graph() with graph.as_default(), tf.Session() as sess: vgg_model = vgg19.VGG19() model_weights, image_mean_value = vgg_model.read_model_from_matconvnet( model) image_shape = (1, ) + content_image.image_matrix.shape if starting_image is None: input_image = tf.Variable( tf.random_normal(shape=image_shape) * 0.256) else: input_image = tf.Variable( np.reshape( starting_image.image_matrix - image_mean_value, image_shape)) vgg_model.get_model_from_matconvnet(input_image, model_weights, pool_type='max') content_loss = 0 for content_layer_index, content_layer in enumerate( content_layers): content_layer_size = content_layer_values[ content_layer_index].size content_loss += tf.nn.l2_loss(vgg_model.layer_dict[content_layer]['tensor_value'] - \ content_layer_values[content_layer_index]) style_loss = 0 style_layer_weight = 1.0 / len(style_layers) for style_layer_index, style_layer in enumerate(style_layers): style_layer_value_train = vgg_model.layer_dict[ style_layer]['tensor_value'] print style_layer_value_train.get_shape()[3].value reshaped_style_layer_train = tf.reshape( style_layer_value_train, (-1, style_layer_value_train.get_shape()[3].value)) gram_matrix = tf.matmul( tf.transpose(reshaped_style_layer_train), reshaped_style_layer_train) style_layer_size = style_layer_values[ style_layer_index].size style_loss += style_layer_weight * tf.nn.l2_loss( (gram_matrix - style_layer_values[style_layer_index]) / style_layer_size ) \ / 2 total_loss = content_weight * content_loss + style_weight * style_loss learning_rate = 1e1 beta1 = 0.9 beta2 = 0.999 epsilon = 1e-08 train_step = tf.train.AdamOptimizer( learning_rate, beta1, beta2, epsilon).minimize(total_loss) sess.run(tf.global_variables_initializer()) minimum_total_loss = total_loss.eval() image_with_minimum_total_loss = input_image.eval() for iter in range(num_iters): #train_step.run() content_loss_value, style_loss_value, total_loss_value, _ = sess.run( [content_loss, style_loss, total_loss, train_step]) if iter % 10 == 0: save_image = Image( image_with_minimum_total_loss.reshape( content_image.image_matrix.shape) + image_mean_value) save_image.save_image(image_name=str(iter)) if iter % feed_back_inters == 0: print "Iteration - ", iter print "content_loss - ", content_loss_value print "style_loss - ", style_loss_value print "total_loss - ", total_loss_value if total_loss_value < minimum_total_loss: minimum_total_loss = total_loss_value image_with_minimum_total_loss = input_image.eval() return image_with_minimum_total_loss.reshape( content_image.image_matrix.shape) + image_mean_value
resized = cv2.resize(img.astype(np.float32), (224, 224)) - imgMean # 将一张图片转化为 tf 需要的格式的矩阵 x_input = resized.reshape((1,224,224,3)) return x_input x_input = pre_input() x = tf.placeholder("float", [1, 224, 224, 3]) ######## ----- dropoutPro = 1 classNum = 1000 skip = [] model = vgg19.VGG19(x, dropoutPro, classNum, skip) score = model.fc8 softmax = tf.nn.softmax(score) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) print (sess.run(x,feed_dict = {x: x_input})) print (sess.run(x,feed_dict = {x: x_input}).shape ) #model.loadModel(sess) #maxx = np.argmax(sess.run(softmax, feed_dict = {x: x_input})) #res = caffe_classes.class_names[maxx] #print (res)
batch = np.concatenate((test_image1, test_image2), 0) # label img1_true_result = np.array([1 if i == 292 else 0 for i in xrange(1000)]) img2_true_result = np.array([1 if i == 611 else 0 for i in xrange(1000)]) img1_true_result = img1_true_result.reshape((1, 1000)) img2_true_result = img2_true_result.reshape((1, 1000)) label = np.concatenate((img1_true_result, img2_true_result), 0) with tf.Session() as session: # construct vgg 19 network print 'Constructing the VGG19 network' start_time = time.time() vgg = vgg19.VGG19([224, 224, 3], MODEL_PARAMETERS_PATH) print 'time used %d' % (time.time() - start_time) # initialize paramerter print '\nInitializing all variables' start_time = time.time() session.run(tf.global_variables_initializer()) print 'time used %d' % (time.time() - start_time) # detection sample print '\nTesting detection of VGG19 network using two images (tiger and puzzle)' start_time = time.time() prob = session.run(vgg.get_predict_op(), feed_dict={ vgg.get_input_tensor(): batch, vgg.get_trainable_tensor(): False