Skip to content

Commit

Permalink
Refactored code
Browse files Browse the repository at this point in the history
  • Loading branch information
titu1994 committed Dec 2, 2016
1 parent 09c0a74 commit 842b282
Show file tree
Hide file tree
Showing 4 changed files with 222 additions and 126 deletions.
86 changes: 55 additions & 31 deletions INetwork.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,45 +34,73 @@
parser = argparse.ArgumentParser(description='Neural style transfer with Keras.')
parser.add_argument('base_image_path', metavar='base', type=str,
help='Path to the image to transform.')

parser.add_argument('syle_image_paths', metavar='ref', nargs='+', type=str,
help='Path to the style reference image.')

parser.add_argument('result_prefix', metavar='res_prefix', type=str,
help='Prefix for the saved results.')
parser.add_argument("--style_masks", type=str, default=None, nargs='+', help='Masks for style images')
parser.add_argument("--color_mask", type=str, default=None, help='Mask for color preservation')

parser.add_argument("--image_size", dest="img_size", default=400, type=int, help='Output Image size')
parser.add_argument("--style_masks", type=str, default=None, nargs='+',
help='Masks for style images')

parser.add_argument("--color_mask", type=str, default=None,
help='Mask for color preservation')

parser.add_argument("--image_size", dest="img_size", default=400, type=int,
help='Minimum image size')

parser.add_argument("--content_weight", dest="content_weight", default=0.025, type=float,
help="Weight of content") # 0.025
parser.add_argument("--style_weight", dest="style_weight", nargs='+', default=[1], type=float, help="Weight of content") # 1.0
help="Weight of content")

parser.add_argument("--style_weight", dest="style_weight", nargs='+', default=[1], type=float,
help="Weight of style, can be multiple for multiple styles")

parser.add_argument("--style_scale", dest="style_scale", default=1.0, type=float,
help="Scale the weightage of the style") # 1, 0.5, 2
help="Scale the weighing of the style")

parser.add_argument("--total_variation_weight", dest="tv_weight", default=8.5e-5, type=float,
help="Total Variation in the Weights") # 1.0
help="Total Variation weight")

parser.add_argument("--num_iter", dest="num_iter", default=10, type=int,
help="Number of iterations")

parser.add_argument("--num_iter", dest="num_iter", default=10, type=int, help="Number of iterations")
parser.add_argument("--model", default="vgg16", type=str, help="Choices are 'vgg16' and 'vgg19'")
parser.add_argument("--content_loss_type", default=0, type=int, help='Can be one of 0, 1 or 2. Readme contains '
'the required information of each mode.')
parser.add_argument("--model", default="vgg16", type=str,
help="Choices are 'vgg16' and 'vgg19'")

parser.add_argument("--content_loss_type", default=0, type=int,
help='Can be one of 0, 1 or 2. Readme contains the required information of each mode.')

parser.add_argument("--rescale_image", dest="rescale_image", default="False", type=str,
help="Rescale image after execution to original dimentions")

parser.add_argument("--rescale_method", dest="rescale_method", default="bilinear", type=str,
help="Rescale image algorithm")

parser.add_argument("--maintain_aspect_ratio", dest="maintain_aspect_ratio", default="True", type=str,
help="Maintain aspect ratio of image")
help="Maintain aspect ratio of loaded images")

parser.add_argument("--content_layer", dest="content_layer", default="conv5_2", type=str,
help="Content layer used for content loss.")

parser.add_argument("--content_layer", dest="content_layer", default="conv5_2", type=str, help="Optional 'conv4_2'")
parser.add_argument("--init_image", dest="init_image", default="content", type=str,
help="Initial image used to generate the final image. Options are 'content', 'noise', or 'gray'")

parser.add_argument("--pool_type", dest="pool", default="max", type=str,
help='Pooling type. Can be "ave" for average pooling'
' or "max" for max pooling ')
help='Pooling type. Can be "ave" for average pooling or "max" for max pooling')

parser.add_argument('--preserve_color', dest='color', default="False", type=str,
help='Preserve original color in image')

parser.add_argument('--min_improvement', default=0.0, type=float,
help='Defines minimum improvement required to continue script')


def str_to_bool(v):
return v.lower() in ("true", "yes", "t", "1")

''' Arguments '''

args = parser.parse_args()
base_image_path = args.base_image_path
style_reference_image_paths = args.syle_image_paths
Expand All @@ -91,17 +119,12 @@

if style_masks_present:
assert len(style_image_paths) == len(mask_paths), "Wrong number of style masks provided.\n" \
"Number of style images = %d, \n" \
"Number of style mask paths = %d." % \
(len(style_image_paths), len(style_masks_present))
"Number of style images = %d, \n" \
"Number of style mask paths = %d." % \
(len(style_image_paths), len(style_masks_present))

color_mask_present = args.color_mask is not None


def str_to_bool(v):
return v.lower() in ("true", "yes", "t", "1")


rescale_image = str_to_bool(args.rescale_image)
maintain_aspect_ratio = str_to_bool(args.maintain_aspect_ratio)
preserve_color = str_to_bool(args.color)
Expand All @@ -126,6 +149,14 @@ def str_to_bool(v):
for style_weight in args.style_weight:
style_weights.append(style_weight * args.style_scale)

# Decide pooling function
pooltype = str(args.pool).lower()
assert pooltype in ["ave", "max"], 'Pooling argument is wrong. Needs to be either "ave" or "max".'

pooltype = 1 if pooltype == "ave" else 0

read_mode = "gray" if args.init_image == "gray" else "color"

# dimensions of the generated picture.
img_width = img_height = 0

Expand All @@ -135,6 +166,7 @@ def str_to_bool(v):
assert args.init_image in ["content", "noise", "gray"], "init_image must be one of ['content', 'noise', 'gray']"
assert args.content_loss_type in [0, 1, 2], "Content Loss Type must be one of 0, 1 or 2"


# util function to open, resize and format pictures into appropriate tensors
def preprocess_image(image_path, load_dims=False, read_mode="color"):
global img_width, img_height, img_WIDTH, img_HEIGHT, aspect_ratio
Expand Down Expand Up @@ -246,21 +278,13 @@ def load_mask(mask_path, shape, return_mask_img=False):
return mask_tensor


# Decide pooling function
pooltype = str(args.pool).lower()
assert pooltype in ["ave", "max"], 'Pooling argument is wrong. Needs to be either "ave" or "max".'

pooltype = 1 if pooltype == "ave" else 0


def pooling_func(x):
if pooltype == 1:
return AveragePooling2D((2, 2), strides=(2, 2))(x)
else:
return MaxPooling2D((2, 2), strides=(2, 2))(x)


read_mode = "gray" if args.init_image == "gray" else "color"
# get tensor representations of our images
base_image = K.variable(preprocess_image(base_image_path, True, read_mode=read_mode))

Expand Down
86 changes: 55 additions & 31 deletions Network.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,45 +32,73 @@
parser = argparse.ArgumentParser(description='Neural style transfer with Keras.')
parser.add_argument('base_image_path', metavar='base', type=str,
help='Path to the image to transform.')

parser.add_argument('syle_image_paths', metavar='ref', nargs='+', type=str,
help='Path to the style reference image.')

parser.add_argument('result_prefix', metavar='res_prefix', type=str,
help='Prefix for the saved results.')
parser.add_argument("--style_masks", type=str, default=None, nargs='+', help='Masks for style images')
parser.add_argument("--color_mask", type=str, default=None, help='Mask for color preservation')

parser.add_argument("--image_size", dest="img_size", default=400, type=int, help='Output Image size')
parser.add_argument("--style_masks", type=str, default=None, nargs='+',
help='Masks for style images')

parser.add_argument("--color_mask", type=str, default=None,
help='Mask for color preservation')

parser.add_argument("--image_size", dest="img_size", default=400, type=int,
help='Minimum image size')

parser.add_argument("--content_weight", dest="content_weight", default=0.025, type=float,
help="Weight of content") # 0.025
parser.add_argument("--style_weight", dest="style_weight", nargs='+', default=[1], type=float, help="Weight of content") # 1.0
help="Weight of content")

parser.add_argument("--style_weight", dest="style_weight", nargs='+', default=[1], type=float,
help="Weight of style, can be multiple for multiple styles")

parser.add_argument("--style_scale", dest="style_scale", default=1.0, type=float,
help="Scale the weightage of the style") # 1, 0.5, 2
help="Scale the weighing of the style")

parser.add_argument("--total_variation_weight", dest="tv_weight", default=8.5e-5, type=float,
help="Total Variation in the Weights") # 1.0
help="Total Variation weight")

parser.add_argument("--num_iter", dest="num_iter", default=10, type=int,
help="Number of iterations")

parser.add_argument("--num_iter", dest="num_iter", default=10, type=int, help="Number of iterations")
parser.add_argument("--model", default="vgg16", type=str, help="Choices are 'vgg16' and 'vgg19'")
parser.add_argument("--content_loss_type", default=0, type=int, help='Can be one of 0, 1 or 2. Readme contains '
'the required information of each mode.')
parser.add_argument("--model", default="vgg16", type=str,
help="Choices are 'vgg16' and 'vgg19'")

parser.add_argument("--content_loss_type", default=0, type=int,
help='Can be one of 0, 1 or 2. Readme contains the required information of each mode.')

parser.add_argument("--rescale_image", dest="rescale_image", default="False", type=str,
help="Rescale image after execution to original dimentions")

parser.add_argument("--rescale_method", dest="rescale_method", default="bilinear", type=str,
help="Rescale image algorithm")

parser.add_argument("--maintain_aspect_ratio", dest="maintain_aspect_ratio", default="True", type=str,
help="Maintain aspect ratio of image")
help="Maintain aspect ratio of loaded images")

parser.add_argument("--content_layer", dest="content_layer", default="conv5_2", type=str,
help="Content layer used for content loss.")

parser.add_argument("--content_layer", dest="content_layer", default="conv5_2", type=str, help="Optional 'conv4_2'")
parser.add_argument("--init_image", dest="init_image", default="content", type=str,
help="Initial image used to generate the final image. Options are 'content', 'noise', or 'gray'")

parser.add_argument("--pool_type", dest="pool", default="max", type=str,
help='Pooling type. Can be "ave" for average pooling'
' or "max" for max pooling ')
help='Pooling type. Can be "ave" for average pooling or "max" for max pooling')

parser.add_argument('--preserve_color', dest='color', default="False", type=str,
help='Preserve original color in image')

parser.add_argument('--min_improvement', default=0.0, type=float,
help='Defines minimum improvement required to continue script')


def str_to_bool(v):
return v.lower() in ("true", "yes", "t", "1")

''' Arguments '''

args = parser.parse_args()
base_image_path = args.base_image_path
style_reference_image_paths = args.syle_image_paths
Expand All @@ -89,17 +117,12 @@

if style_masks_present:
assert len(style_image_paths) == len(mask_paths), "Wrong number of style masks provided.\n" \
"Number of style images = %d, \n" \
"Number of style mask paths = %d." % \
(len(style_image_paths), len(style_masks_present))
"Number of style images = %d, \n" \
"Number of style mask paths = %d." % \
(len(style_image_paths), len(style_masks_present))

color_mask_present = args.color_mask is not None


def str_to_bool(v):
return v.lower() in ("true", "yes", "t", "1")


rescale_image = str_to_bool(args.rescale_image)
maintain_aspect_ratio = str_to_bool(args.maintain_aspect_ratio)
preserve_color = str_to_bool(args.color)
Expand All @@ -124,6 +147,14 @@ def str_to_bool(v):
for style_weight in args.style_weight:
style_weights.append(style_weight * args.style_scale)

# Decide pooling function
pooltype = str(args.pool).lower()
assert pooltype in ["ave", "max"], 'Pooling argument is wrong. Needs to be either "ave" or "max".'

pooltype = 1 if pooltype == "ave" else 0

read_mode = "gray" if args.init_image == "gray" else "color"

# dimensions of the generated picture.
img_width = img_height = 0

Expand All @@ -133,6 +164,7 @@ def str_to_bool(v):
assert args.init_image in ["content", "noise", "gray"], "init_image must be one of ['content', 'noise', 'gray']"
assert args.content_loss_type in [0, 1, 2], "Content Loss Type must be one of 0, 1 or 2"


# util function to open, resize and format pictures into appropriate tensors
def preprocess_image(image_path, load_dims=False, read_mode="color"):
global img_width, img_height, img_WIDTH, img_HEIGHT, aspect_ratio
Expand Down Expand Up @@ -244,21 +276,13 @@ def load_mask(mask_path, shape, return_mask_img=False):
return mask_tensor


# Decide pooling function
pooltype = str(args.pool).lower()
assert pooltype in ["ave", "max"], 'Pooling argument is wrong. Needs to be either "ave" or "max".'

pooltype = 1 if pooltype == "ave" else 0


def pooling_func(x):
if pooltype == 1:
return AveragePooling2D((2, 2), strides=(2, 2))(x)
else:
return MaxPooling2D((2, 2), strides=(2, 2))(x)


read_mode = "gray" if args.init_image == "gray" else "color"
# get tensor representations of our images
base_image = K.variable(preprocess_image(base_image_path, True, read_mode=read_mode))

Expand Down
Loading

0 comments on commit 842b282

Please sign in to comment.