return tf.clip_by_value(image, clip_value_min=0, clip_value_max=1)


l2_reg = 1e-3
learning_rate = 25
epochs = 5000
epoch0 = 10
epoch1 = 1000
target = 76
MEAN = np.float32([0.485, 0.456, 0.406])
STD = np.float32([0.229, 0.224, 0.225])

SAVE_PATH = "cs231n/datasets/squeezenet.ckpt"
if not os.path.exists(SAVE_PATH + ".index"):
    raise ValueError("You need to download SqueezeNet!")
model = SqueezeNet()
model.load_weights(SAVE_PATH)
model.trainable = False

X = 255 * np.random.rand(224, 224, 3)
X = process(X)
X = X[None]
X = tf.Variable(X)
for _ in range(epochs):
    with tf.GradientTape() as tape:
        tape.watch(X)
        loss = model(X)[0, target] - l2_reg * tf.nn.l2_loss(X)
    dX = tape.gradient(loss, X)
    X.assign_add(dX[0] * learning_rate)
    X.assign(clip(X))
    assert vnum >= 16, "You must install SciPy >= 0.16.0 to complete this notebook."


check_scipy()

from cs231n.classifiers.squeezenet import SqueezeNet
import tensorflow as tf

tf.reset_default_graph()  # remove all existing variables in the graph
sess = get_session()  # start a new Session

# Load pretrained SqueezeNet model
SAVE_PATH = 'cs231n/datasets/squeezenet.ckpt'
# if not os.path.exists(SAVE_PATH):
#     raise ValueError("You need to download SqueezeNet!")
model = SqueezeNet(save_path=SAVE_PATH, sess=sess)

# Load data for testing
content_img_test = preprocess_image(load_image('styles/tubingen.jpg',
                                               size=192))[None]
style_img_test = preprocess_image(
    load_image('styles/starry_night.jpg', size=192))[None]
answers = np.load('style-transfer-checks-tf.npz')


def content_loss(content_weight, content_current, content_original):
    shapes = tf.shape(content_current)
    F_l = tf.reshape(content_current, [shapes[1], shapes[2] * shapes[3]])
    P_l = tf.reshape(content_original, [shapes[1], shapes[2] * shapes[3]])
    loss = content_weight * (tf.reduce_sum((F_l - P_l)**2))
    return loss
Exemple #3
0
def get_session():
    """Create a session that dynamically allocates memory."""
    # See: https://www.tensorflow.org/tutorials/using_gpu#allowing_gpu_memory_growth
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    session = tf.Session(config=config)
    return session

sess = get_session() # start a new Session

MODEL_PATH = 'style_transfer_utils/squeezenet.ckpt'

if not os.path.exists(MODEL_PATH + ".index"):
    raise ValueError("Invalid path to SqueezeNet model!!")
model = SqueezeNet(save_path=MODEL_PATH, sess=sess)




def content_loss(content_weight, content_current, content_original):
    """
    Compute the content loss for style transfer.

    Inputs:
    - content_weight: scalar constant we multiply the content_loss by.
    - content_current: features of the current image, Tensor with shape [1, height, width, channels]
    - content_target: features of the content image, Tensor with shape [1, height, width, channels]

    Returns:
    - scalar content loss