Пример #1
0
def test_style_transfer():

    content_fname = os.path.join(PROJECT_ROOT, 'input/content/modern.jpg')
    style_fname = os.path.join(PROJECT_ROOT, 'input/style/goeritz.jpg')

    c_img = cv2.imread(content_fname)
    s_img = cv2.imread(style_fname)

    true_transfer_img = np.load(
        os.path.join(PROJECT_ROOT, 'tests/stylized_imgs_truth.npy'))

    model = adain_style_transfer(alpha=1.0)
    model.load_weights(os.path.join(PROJECT_ROOT, "pretrained/adain.h5"))

    content_imgs = preprocess(c_img, (512, 512))
    style_imgs = preprocess(s_img, (512, 512))
    stylized_imgs = model.predict([content_imgs, style_imgs])
    stylized_img = postprocess(stylized_imgs)

    assert np.allclose(stylized_img, true_transfer_img)
Пример #2
0
    image[image < 0] = 0
    image *= 255
    image = image.astype(np.uint8)
    return image

if __name__ == '__main__':
    
    encoder_input = 416
    decoder_input = int(encoder_input/8)
    
    # 1. contents / style images
    c_img = cv2.imread(content_fname)[:,:,::-1]
    s_img = cv2.imread(style_fname)[:,:,::-1]

    # 2. load input imgs
    c_img_prep = preprocess(c_img, (encoder_input,encoder_input))
    s_img_prep = preprocess(s_img, (encoder_input,encoder_input))
    
    # 3. encoding
    encoder = mobile_encoder(input_size=encoder_input)
    mobile_decoder = build_mobile_combine_decoder(decoder_input)
    # mobile_decoder.load_weights("adain/models/h5/mobile_decoder.h5", by_name=True)
    mobile_decoder.load_weights("mobile_decoder.h5", by_name=True)

    c_features = encoder.predict(c_img_prep)
    s_features = encoder.predict(s_img_prep)

    stylized_imgs = mobile_decoder.predict([c_features, s_features])
    print(stylized_imgs.max(), stylized_imgs.min())
    img = postprocess(stylized_imgs[0])
Пример #3
0
DEFAULT_ENCODER_H5 = os.path.join(MODEL_ROOT, "h5", "mobile_encoder.h5")
DEFAULT_DECODER_H5 = os.path.join(MODEL_ROOT, "h5", "vgg_decoder.h5")

content_fname="../input/content/chicago.jpg"
style_fname="../input/style/asheville.jpg"
alpha = 1.0


if __name__ == '__main__':
    
    # 1. contents / style images
    c_img = cv2.imread(content_fname)[:,:,::-1]
    s_img = cv2.imread(style_fname)[:,:,::-1]

    # 2. load input imgs
    c_img_prep = preprocess(c_img, (256,256))
    s_img_prep = preprocess(s_img, (256,256))
    
    # 3. encoding
    from adain.encoder import mobile_encoder
    from adain.decoder import combine_and_decode_model
    encoder = mobile_encoder()
    decoder = combine_and_decode_model()
    encoder.load_weights(DEFAULT_ENCODER_H5)
    decoder.load_weights(DEFAULT_DECODER_H5)

    c_features = encoder.predict(c_img_prep)
    s_features = encoder.predict(s_img_prep)

    stylized_imgs = decoder.predict([c_features, s_features])
    stylized_img = stylized_imgs[0].astype(np.uint8)
    from linear.encoder import vgg_encoder
    from linear.decoder import vgg_decoder
    from linear.mat import build_model as mat_model
    
    encoder = vgg_encoder(img_size)
    decoder = vgg_decoder(int(img_size)/4)
    mat = mat_model(input_shape=[int(img_size/4),
                                 int(img_size/4),
                                 256])
     
    # 1. contents / style images
    c_img = cv2.imread(content_fname)[:,:,::-1]
    s_img = cv2.imread(style_fname)[:,:,::-1]
 
    # 2. load input imgs
    c_img_prep = preprocess(c_img, (img_size,img_size))
    s_img_prep = preprocess(s_img, (img_size,img_size))
 
    # 3. encoding
    c_features = encoder.predict(c_img_prep)
    s_features = encoder.predict(s_img_prep)
 
    # 4. mix
    cs_features = mat.predict([c_features, s_features])
 
    # 5. decode
    stylized_imgs = decoder.predict(cs_features)
 
    # 6. post process
    stylized_img = stylized_imgs[0].astype(np.uint8)