CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
y33-j3T

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: y33-j3T/Coursera-Deep-Learning
Path: blob/master/Convolutional Neural Networks/week4/Neural Style Transfer/__pycache__/nst_utils.cpython-36.pyc
Views: 13378
3

���Y<�@s�ddlZddlZddlZddlZddljZddlmZddl	m
Z
ddlTddlZ
ddlZGdd�d�Zdd�Zejfd	d
�Zdd�Zd
d�ZdS)�N)�imshow)�Image)�*c@sBeZdZdZdZdZdZejdddg�j	d�Z
d	Zd
ZdZ
dZd
S)�CONFIGi�i,�g333333�?g�Q���^@g`��"�1]@gj�t��Y@�z-pretrained-model/imagenet-vgg-verydeep-19.matzimages/stone_style.jpgzimages/content300.jpgzoutput/N)rrrr)�__name__�
__module__�__qualname__�IMAGE_WIDTH�IMAGE_HEIGHT�COLOR_CHANNELS�NOISE_RATIO�np�array�reshape�MEANSZ	VGG_MODELZSTYLE_IMAGEZ
CONTENT_IMAGEZ
OUTPUT_DIR�rr�:/home/jovyan/work/week4/Neural Style Transfer/nst_utils.pyrsrcstjj|�}|d��fdd��dd���fdd����fdd	�}d
d�}i}tjtjdtjtj	tj
f�d
d�|d<||ddd�|d<||ddd�|d<||d�|d<||ddd�|d<||ddd�|d<||d�|d<||ddd�|d<||ddd�|d<||ddd�|d<||dd d!�|d!<||d!�|d"<||d"d#d$�|d$<||d$d%d&�|d&<||d&d'd(�|d(<||d(d)d*�|d*<||d*�|d+<||d+d,d-�|d-<||d-d.d/�|d/<||d/d0d1�|d1<||d1d2d3�|d3<||d3�|d4<|S)5a]
    Returns a model for the purpose of 'painting' the picture.
    Takes only the convolution layer weights and wrap using the TensorFlow
    Conv2d, Relu and AveragePooling layer. VGG actually uses maxpool but
    the paper indicates that using AveragePooling yields better results.
    The last few fully connected layers are not used.
    Here is the detailed configuration of the VGG model:
        0 is conv1_1 (3, 3, 3, 64)
        1 is relu
        2 is conv1_2 (3, 3, 64, 64)
        3 is relu    
        4 is maxpool
        5 is conv2_1 (3, 3, 64, 128)
        6 is relu
        7 is conv2_2 (3, 3, 128, 128)
        8 is relu
        9 is maxpool
        10 is conv3_1 (3, 3, 128, 256)
        11 is relu
        12 is conv3_2 (3, 3, 256, 256)
        13 is relu
        14 is conv3_3 (3, 3, 256, 256)
        15 is relu
        16 is conv3_4 (3, 3, 256, 256)
        17 is relu
        18 is maxpool
        19 is conv4_1 (3, 3, 256, 512)
        20 is relu
        21 is conv4_2 (3, 3, 512, 512)
        22 is relu
        23 is conv4_3 (3, 3, 512, 512)
        24 is relu
        25 is conv4_4 (3, 3, 512, 512)
        26 is relu
        27 is maxpool
        28 is conv5_1 (3, 3, 512, 512)
        29 is relu
        30 is conv5_2 (3, 3, 512, 512)
        31 is relu
        32 is conv5_3 (3, 3, 512, 512)
        33 is relu
        34 is conv5_4 (3, 3, 512, 512)
        35 is relu
        36 is maxpool
        37 is fullyconnected (7, 7, 512, 4096)
        38 is relu
        39 is fullyconnected (1, 1, 4096, 4096)
        40 is relu
        41 is fullyconnected (1, 1, 4096, 1000)
        42 is softmax
    �layerscs`�d|ddd}|dd}|dd}�d|dddd}||ksXt�||fS)zS
        Return the weights and bias from the VGG model for a given layer.
        r�r)�AssertionError)�layerZexpected_layer_name�wb�W�b�
layer_name)�
vgg_layersrr�_weightsSsz load_vgg_model.<locals>._weightscSstjj|�S)zq
        Return the RELU function wrapped over a TensorFlow layer. Expects a
        Conv2d layer input.
        )�tf�nnZrelu)Zconv2d_layerrrr�_relu`szload_vgg_model.<locals>._relucsL�||�\}}tj|�}tjtj||j��}tjj||ddddgdd�|S)zj
        Return the Conv2D layer using the weights, biases from the VGG
        model at 'layer'.
        r�SAME)�filter�strides�padding)r�constantrr�sizer Zconv2d)�
prev_layerrrrr)rrr�_conv2dgs
zload_vgg_model.<locals>._conv2dcs��|||��S)zq
        Return the Conv2D + RELU layer using the weights, biases from the VGG
        model at 'layer'.
        r)r(rr)r)r!rr�_conv2d_reluqsz$load_vgg_model.<locals>._conv2d_relucSs$tjj|ddddgddddgdd�S)z2
        Return the AveragePooling layer.
        rrr")Zksizer$r%)rr Zavg_pool)r(rrr�_avgpoolxsz load_vgg_model.<locals>._avgpoolr�float32)�dtype�inputrZconv1_1rZconv1_2Zavgpool1�Zconv2_1�Zconv2_2Zavgpool2�
Zconv3_1�Zconv3_2�Zconv3_3�Zconv3_4Zavgpool3�Zconv4_1�Zconv4_2�Zconv4_3�Zconv4_4Zavgpool4�Zconv5_1�Zconv5_2� Zconv5_3�"Zconv5_4Zavgpool5)�scipy�io�loadmatr�Variabler�zerosrrrr
)�pathZvggr*r+�graphr)r)r!rrr�load_vgg_models>5

&rDcCs<tjjdddtjtjtjf�jd�}|||d|}|S)zM
    Generates a noisy image by adding random noise to the content_image
    �rr,i����)r�random�uniformrrrr
�astype)Z
content_imageZnoise_ratioZnoise_imageZinput_imagerrr�generate_noise_image�s$rIcCs tj|d|j�}|tj}|S)zB
    Reshape and normalize the input image (content or style)
    r)r)rr�shaperr)�imagerrr�reshape_and_normalize_image�s
rLcCs4|tj}tj|ddd�jd�}tjj||�dS)Nr���uint8)rrr�cliprHr=�misc�imsave)rBrKrrr�
save_image�s
rR)�os�sys�scipy.ior=�
scipy.misc�matplotlib.pyplot�pyplot�pltr�PILr�	nst_utils�numpyr�
tensorflowrrrDrrIrLrRrrrr�<module>s