def main(): # Define arguments parser = argparse.ArgumentParser() parser.add_argument("-i", "--image", help="Path to style image") parser.add_argument("-c", "--content", help="Path to content image") parser.add_argument("-o", "--output", help="Output directory") parser.add_argument("-n", "--noise", help="Path to noise image") parser.add_argument( "-z", "--noiseType", help="Noise type (0 histogram, 1 crappy, 2 less crappy, 3 white)", type=int, default=0) parser.add_argument("-t", "--iterations", help="Max number of iterations to run", type=int) parser.add_argument("-w", "--width", help="Width of output image", type=int) parser.add_argument("-l", "--height", help="Height of output", type=int) parser.add_argument("-p", "--pyramid", help="Use multiscale image pyramid", type=int, default=0) parser.add_argument( "-a", "--wLayers", help= "Array for the weights of each layer for style, should look like 1,0,0.5,2,0 ", default='1,1') parser.add_argument( "-d", "--wLayersContent", help= "Array for the weights of each layer for content, should look like 1,0,0.5,2,0 ", default='0,0,1') parser.add_argument( "-b", "--wPyramidStyle", help= "Array for the weights of each pyramid level, should look like 1,0,0.5,2,0", default='1,1') parser.add_argument("-q", "--betaStyle", help="Weight for the content loss", type=float, default='1') parser.add_argument( "-y", "--wPyramidContent", help= "Array for the weights of each pyramid level of the content, should look like 1,0,0.5,2,0", default='1,1') args = parser.parse_args() styleImage = None if args.output: output_directory = args.output else: output_directory = "result" if not os.path.exists(output_directory): os.makedirs(output_directory) if args.image: styleImage = utils.load_image_big(args.image, crop=False) # [0, 1) if args.content: contentImage = utils.load_image_big(args.content, crop=False) # [0, 1) else: print("Image not defined") return # Iterations if args.iterations: iter = args.iterations else: iter = 20 if args.betaStyle: beta = args.betaStyle else: beta = 1 # Dimensions for the output image if args.width: width = args.width else: width = np.shape(contentImage)[0] if args.height: height = args.height else: height = np.shape(contentImage)[1] #Clear the output folder if not os.path.exists(output_directory): os.makedirs(output_directory) try: filesToRemove = [f for f in os.listdir(output_directory)] for f in filesToRemove: if os.path.isfile(output_directory): os.remove(os.path.join(output_directory, f)) else: shutil.rmtree(os.path.join(output_directory, f)) #if os.path.isfile(output_directory): # os.unlink(output_directory) #elif os.path.isdir(output_directory): shutil.rmtree(output_directory) except Exception as e: print(e) # Define noise noiseImage = None if args.noise: noiseImage = utils.load_image_big(args.noise, crop=False) else: if args.noiseType == 0: noise = utils.his_noise(contentImage, sizeW=np.shape(contentImage)[0], sizeH=np.shape(contentImage)[1]) elif args.noiseType == 1: noise = utils.crappy_noise(width, height) elif args.noiseType == 2: noise = utils.less_crappy_noise( contentImage, width, height, ) else: noise = utils.white_noise(height, width) skimage.io.imsave(os.path.join(output_directory, "his_noise.png"), noise) noiseImage = noise print("Noise Image Shape: ", noiseImage.shape) #If 4-D, the shape is [batch_size, height, width, channels] noiseImage = noiseImage.reshape( (1, np.shape(noiseImage)[0], np.shape(noiseImage)[1], 3)).astype("float32") # Define image skimage.io.imsave(os.path.join(output_directory, "originStyle.png"), styleImage) styleImage = styleImage.reshape( (1, np.shape(styleImage)[0], np.shape(styleImage)[1], 3)) skimage.io.imsave(os.path.join(output_directory, "originContent.png"), contentImage) contentImage = contentImage.reshape( (1, np.shape(contentImage)[0], np.shape(contentImage)[1], 3)) with open(os.path.join(output_directory, "Parameters.txt"), "w") as text_file: a = datetime.datetime.now() a = a.strftime("%Y-%m-%d %H:%M%:%S") print(a) print("Texture Synthesis Experiment", file=text_file) print("Time = {}".format(a), file=text_file) for arg in vars(args): print(arg, "\t\t\t", getattr(args, arg)) print(arg, "\t\t\t", getattr(args, arg), file=text_file) #Weights for the loss function weightsLayers = args.wLayers.split(',') weightsLayers = list(map(float, weightsLayers)) weightsPyramid = args.wPyramidStyle.split(',') weightsPyramid = list(map(float, weightsPyramid)) weightsPyramidContent = args.wPyramidContent.split(',') weightsPyramidContent = list(map(float, weightsPyramidContent)) weightsLayersContent = args.wLayersContent.split(',') weightsLayersContent = list(map(float, weightsLayersContent)) print(weightsLayers) print('\n') print(weightsPyramid) # Run synthesis #styleImage = np array of style image #noiseImage = np array of noise start = time.time() run_tensorflow(styleImage, noiseImage, contentImage, output_directory, args.pyramid, weightsLayers, weightsLayersContent, weightsPyramid, weightsPyramidContent, iter, beta) elapsed = time.time() - start with open(os.path.join(output_directory, "Parameters.txt"), "a") as text_file: print("\n") tmp = time.strftime("%H:%M%:%S", time.gmtime(elapsed)) print("Elapsed time = %s" % tmp) print("Elapsed time = %s" % tmp, file=text_file)
def main(): # Define arguments parser = argparse.ArgumentParser() parser.add_argument("-i", "--image", help="Path to image") parser.add_argument("-o", "--output", help="Output directory") parser.add_argument("-n", "--noise", help="Path to noise image") parser.add_argument( "-z", "--noiseType", help="Noise type (0 histogram, 1 crappy, 2 less crappy, 3 white)", type=int, default=0) parser.add_argument("-t", "--iterations", help="Max number of iterations to run", type=int) parser.add_argument("-w", "--width", help="Width of output image", type=int) parser.add_argument("-l", "--height", help="Height of output", type=int) parser.add_argument("-p", "--pyramid", help="Use multiscale image pyramid", type=int, default=0) parser.add_argument( "-a", "--wLayers", help= "Array for the weights of each layer, should look like 1,0,0.5,2,0 ", default='1,1') parser.add_argument( "-b", "--wPyramid", help= "Array for the weights of each pyramid level, should look like 1,0,0.5,2,0", default='1,1') args = parser.parse_args() proc_img = None if args.output: output_directory = args.output else: output_directory = "result" if not os.path.exists(output_directory): os.makedirs(output_directory) if args.image: proc_img = utils.load_image_big(args.image, crop=False) # [0, 1) else: print("Image not defined") return # Iterations if args.iterations: iter = args.iterations else: iter = 20 # Dimensions for the output image if args.width: width = args.width else: width = np.shape(proc_img)[0] if args.height: height = args.height else: height = np.shape(proc_img)[1] #Clear the output folder if not os.path.exists(output_directory): os.makedirs(output_directory) try: filesToRemove = [f for f in os.listdir(output_directory)] for f in filesToRemove: if os.path.isfile(output_directory): os.remove(os.path.join(output_directory, f)) else: shutil.rmtree(os.path.join(output_directory, f)) #if os.path.isfile(output_directory): # os.unlink(output_directory) #elif os.path.isdir(output_directory): shutil.rmtree(output_directory) except Exception as e: print(e) # Define noise init = None if args.noise: init = utils.load_image_big(args.noise) else: if args.noiseType == 0: noise = utils.his_noise(proc_img, sizeW=np.shape(proc_img)[1], sizeH=np.shape(proc_img)[0]) elif args.noiseType == 1: noise = utils.crappy_noise(width, height) elif args.noiseType == 2: noise = utils.less_crappy_noise( proc_img, width, height, ) else: noise = utils.white_noise(height, width) skimage.io.imsave(os.path.join(output_directory, "his_noise.png"), noise) init = noise #If 4-D, the shape is [batch_size, height, width, channels] batch2 = init.reshape( (1, np.shape(init)[0], np.shape(init)[1], 3)).astype("float32") ######tex = tf.Variable(batch2) # Define image skimage.io.imsave(os.path.join(output_directory, "origin.png"), proc_img) batch1 = proc_img.reshape( (1, np.shape(proc_img)[0], np.shape(proc_img)[1], 3)) ######images = tf.cast(tf.Variable(batch1, trainable=False, dtype='float64'), tf.float32) #original_im_size = 256 #####images_ = [i.reshape((1, np.shape(i)[0], np.shape(i)[1], 3)) for i in create_pyramids(proc_img, args.pyramid)] #images_ = [tf.cast(tf.Variable(i, trainable=False, dtype="float64", name="inputImage"), tf.float32) for i in images_] #noises = [tf.Variable(i, name = "noise") for i in [j.reshape((1, np.shape(j)[0], np.shape(j)[1], 3)).astype("float32") for j in create_pyramids(init, args.pyramid)]] # images_ = [tf.cast(tf.Variable(images_[i], trainable=False, dtype="float64", name="InputImage%s"%str(i)), tf.float32) for i in range(len(images_))] # tmp = [j.reshape((1, np.shape(j)[0], np.shape(j)[1], 3)).astype("float32") for j in create_pyramids(init, args.pyramid)] # noises = [tf.Variable(i, name="noise%s"%str(i)) for i in range(len(tmp))] #noises = [tf.Variable(i) for i in [init.reshape((1, np.shape(init)[0], np.shape(init)[1], 3)).astype("float32") for j in range(args.pyramid + 1)] with open(os.path.join(output_directory, "Parameters.txt"), "w") as text_file: a = datetime.datetime.now() a = a.strftime("%Y-%m-%d %H:%M%:%S") print(a) print("Texture Synthesis Experiment", file=text_file) print("Time = {}".format(a), file=text_file) for arg in vars(args): print(arg, "\t\t\t", getattr(args, arg)) print(arg, "\t\t\t", getattr(args, arg), file=text_file) #Weights for the loss function weightsLayers = args.wLayers.split(',') weightsLayers = list(map(float, weightsLayers)) weightsPyramid = args.wPyramid.split(',') weightsPyramid = list(map(float, weightsPyramid)) print(weightsLayers) print('\n') print(weightsPyramid) #utils.testGaussian() # Run synthesis ## TODO ## ## Rename all variables and parameters #batch1 = np array of target image #batch2 = np array of noise run_tensorflow(batch1, batch2, output_directory, args.pyramid, weightsLayers, weightsPyramid, iter) exit() start = time.time() if args.pyramid > 0: run_synthesis_pyramid(noises, images_, proc_img, iter, output_directory, weightsLayers, weightsPyramid) # Using VGG19 else: run_synthesis(tex, images, proc_img, iter, output_directory, weightsLayers) # Using VGG19 elapsed = time.time() - start with open(os.path.join(output_directory, "Parameters.txt"), "a") as text_file: print("\n") tmp = time.strftime("%H:%M%:%S", time.gmtime(elapsed)) print("Elapsed time = %s" % tmp) print("Elapsed time = %s" % tmp, file=text_file)