Reputation: 1
This is my first ever question on stackoverflow. So I apologise if I do it incorrectly.
I am new to developing and I am trying to create an iOS app that uses Turicreate and StyleTransfer. I am using google colabs and it’s free GPU.
The problem I am having is that accessing the advanced parameters - the style loss multiplier. This all worked fine in Turicreate v5.8. But now the (v6.0 onwards) the parameters can not be accessed - as confirmed by Turicreate GitHub issues page here - https://github.com/apple/turicreate/issues/3341
The above page also details all the code/changes I have tried.
The parameters are are accessed and modified so that you can alter the strength of the style vs content images.
This is what i have done - sorry it is quite lengthy and everything was done in Google Colabs using the GPU
Turicreate v5.8 - this works well and the style transfers more to the image being altered.
!pip install turicreate==5.8
!pip uninstall -y mxnet
!pip install mxnet-cu100==1.4.0.post0
import turicreate as tc
tc.config.set_num_gpus(1)
from google.colab import drive
drive.mount('./drive')
import os
os.chdir('drive/My Drive/trainingdata')
style = tc.load_images('style')
content = tc.load_images('content')
params = {
'print_loss_breakdown': True,
'style_loss_mult': [ 1e-2, 1e-2, 1e-2, 1e-2],
'finetune_all_params': True,
}
model = tc.style_transfer.create(style, content, max_iterations
= 10000, _advanced_parameters=params)
Turicreate v6.0 or higher - altering the style_loss_mult makes zero difference to the output when using the style transfer model. The model is created and works but looks like it is using the default setting of 'style_loss_mult': [ 1e-4, 1e-4, 1e-4, 1e-4]
!pip install turicreate==6.4.1
!pip uninstall -y tensorflow
!pip install tensorflow-gpu==2.0.4
import turicreate as tc
tc.config.set_num_gpus(1)
from google.colab import drive
drive.mount('./drive')
import os
os.chdir('drive/My Drive/trainingdata')
style = tc.load_images('style')
content = tc.load_images('content')
params = {
'print_loss_breakdown': True,
'style_loss_mult': [ 1e-2, 1e-2, 1e-2, 1e-2],
'finetune_all_params': True,
}
model = tc.style_transfer.create(style, content, max_iterations =
10000, _advanced_parameters=params)
Apple developers on the turicreate github advised to alter the python code base to hard code the parameters so they are used. So i have tried the following - i think the code is correct, but again no changes in the style transfer strength is seen. I modified the /usr/local/lib/python3.7/dist-packages/turicreate/toolkits/style_transfer/style_transfer.py file (Bold is the code i have added/changed)
params = {
"batch_size": batch_size,
"vgg16_content_loss_layer": 2, # conv3_3 layer
"lr": 0.001,
"content_loss_mult": 1.0,
**"style_loss_mult": [1e-1, 1e-1, 1e-1, 1e-1],** # conv 1-4 layers
"finetune_all_params": True,
"pretrained_weights": False,
"print_loss_breakdown": False,
"input_shape": (256, 256),
"training_content_loader_type": "stretch",
"use_augmentation": False,
"sequential_image_processing": False,
# Only used if use_augmentaion is True
"aug_resize": 0,
"aug_min_object_covered": 0,
"aug_rand_crop": 0.9,
"aug_rand_pad": 0.9,
"aug_rand_gray": 0.0,
"aug_aspect_ratio": 1.25,
"aug_hue": 0.05,
"aug_brightness": 0.05,
"aug_saturation": 0.05,
"aug_contrast": 0.05,
"aug_horizontal_flip": True,
"aug_area_range": (0.05, 1.5),
"aug_pca_noise": 0.0,
"aug_max_attempts": 20,
"aug_inter_method": 2,
"checkpoint": False,
"checkpoint_prefix": "style_transfer",
"checkpoint_increment": 1000,
}
if "_advanced_parameters" in kwargs:
# Make sure no additional parameters are provided
new_keys = set(kwargs["_advanced_parameters"].keys())
set_keys = set(params.keys())
unsupported = new_keys - set_keys
if unsupported:
raise _ToolkitError("Unknown advanced parameters: {}".format(unsupported))
params.update(kwargs["_advanced_parameters"])
name = "style_transfer"
import turicreate as _turicreate
# Imports tensorflow
_minimal_package_import_check("turicreate.toolkits.libtctensorflow")
model = _turicreate.extensions.style_transfer()
pretrained_resnet_model = _pre_trained_models.STYLE_TRANSFER_BASE_MODELS[
"resnet-16"
]()
pretrained_vgg16_model = _pre_trained_models.STYLE_TRANSFER_BASE_MODELS["Vgg16"]()
options = {}
options["image_height"] = params["input_shape"][0]
options["image_width"] = params["input_shape"][1]
options["content_feature"] = content_feature
options["style_feature"] = style_feature
if verbose is not None:
options["verbose"] = verbose
else:
options["verbose"] = False
if batch_size is not None:
options["batch_size"] = batch_size
if max_iterations is not None:
options["max_iterations"] = max_iterations
options["num_styles"] = len(style_dataset)
options["resnet_mlmodel_path"] = pretrained_resnet_model.get_model_path("coreml")
options["vgg_mlmodel_path"] = pretrained_vgg16_model.get_model_path("coreml")
options["pretrained_weights"] = params["pretrained_weights"]
options["pretrained_weights"] = params["pretrained_weights"]
**options["style_loss_mult"] = params["style_loss_mult"]**
model.train(style_dataset[style_feature], content_dataset[content_feature], options)
return StyleTransfer(model_proxy=model, name=name)
Could anyone please help me figure out how to change the codebase to change the advanced parameters?
It doesn’t look like there will be an update for Turicreate to fix the issues with the style transfer tool, so any help would be appreciated!
Thanks in advance for your help
Upvotes: 0
Views: 61