<jupyter_start><jupyter_text>Download caffe model from YuvalNirkin's GitHub<jupyter_code>!curl -L https://github.com/YuvalNirkin/face_segmentation/releases/download/1.0/face_seg_fcn8s.zip -o face_seg_fcn8s.zip
!unzip face_seg_fcn8s.zip<jupyter_output>Archive:  face_seg_fcn8s.zip
  inflating: face_seg_fcn8s_deploy.prototxt  
  inflating: face_seg_fcn8s.caffemodel<jupyter_text>Import caffe & load model<jupyter_code>import caffe
layers_output = './layers'
caffe_model = './face_seg_fcn8s.caffemodel'
caffe_proto = './face_seg_fcn8s_deploy.prototxt'
caffe.set_mode_cpu()
net = caffe.Net(caffe_proto, caffe_model, caffe.TEST)
net.blobs.items()
net.params.items()<jupyter_output><empty_output><jupyter_text>Save layers' weights to ./layers/*.npyCode borrows from https://github.com/michalfaber/keras_Realtime_Multi-Person_Pose_Estimation.<jupyter_code>import numpy as np
import os
!mkdir layers
#for layer_name, blob in net.blobs.items():
#    print(layer_name, blob.data.shape)

# write out weight matrices and bias vectors
for k, v in net.params.items():
    print(k)
    print(v[0].data.shape)
    try:
        print(v[1].data.shape)
    except:
        None
    try:
        np.save(os.path.join(layers_output, "W_{:s}.npy".format(k)), v[0].data)
    except:
        None
    try:
        np.save(os.path.join(layers_output, "b_{:s}.npy".format(k)), v[1].data)
    except:
        None<jupyter_output>conv1_1
(64, 3, 3, 3)
(64,)
conv1_2
(64, 64, 3, 3)
(64,)
conv2_1
(128, 64, 3, 3)
(128,)
conv2_2
(128, 128, 3, 3)
(128,)
conv3_1
(256, 128, 3, 3)
(256,)
conv3_2
(256, 256, 3, 3)
(256,)
conv3_3
(256, 256, 3, 3)
(256,)
conv4_1
(512, 256, 3, 3)
(512,)
conv4_2
(512, 512, 3, 3)
(512,)
conv4_3
(512, 512, 3, 3)
(512,)
conv5_1
(512, 512, 3, 3)
(512,)
conv5_2
(512, 512, 3, 3)
(512,)
conv5_3
(512, 512, 3, 3)
(512,)
fc6
(4096, 512, 7, 7)
(4096,)
fc7
(4096, 4096, 1, 1)
(4096,)
score_fr
(21, 4096, 1, 1)
(21,)
upscore2
(21, 21, 4, 4)
scale_pool4
(512,)
score_pool4
(21, 512, 1, 1)
(21,)
upscore_pool4
(21, 21, 4, 4)
scale_pool3
(256,)
score_pool3
(21, 256, 1, 1)
(21,)
upscore8
(21, 21, 16, 16)<jupyter_text>(Optional) Pack .npy files into a layers.zip<jupyter_code>import zipfile
def zipdir(path, ziph):
    # ziph is zipfile handle
    for root, dirs, files in os.walk(path):
        for file in files:
            ziph.write(os.path.join(root, file))
zipf = zipfile.ZipFile('layers.zip', 'w', zipfile.ZIP_DEFLATED)
zipdir('layers/', zipf)
zipf.close()<jupyter_output><empty_output><jupyter_text>Import keras<jupyter_code>from keras.models import Sequential, Model
from keras.layers import *
from keras.layers.advanced_activations import LeakyReLU
from keras.activations import relu
from keras.initializers import RandomNormal
from keras.applications import *
import keras.backend as K
import os
import time<jupyter_output><empty_output><jupyter_text>Create keras FCN8s modelCode borrows from [jgraving/keras-fcn-1](https://github.com/jgraving/keras-fcn-1/blob/master/fcn/fcn.py), which is forked from [JihongJu/keras-fcn](https://github.com/JihongJu/keras-fcn).<jupyter_code>from FCN8s_keras import FCN
model = FCN()
model.summary()<jupyter_output><empty_output><jupyter_text>Load weights for all FCN8s layerCode borrows from https://github.com/michalfaber/keras_Realtime_Multi-Person_Pose_Estimation.<jupyter_code>layers_output = "./layers"
start_style_time = time.time()

for layer in model.layers:
    layer_name = layer.name
    if (os.path.exists(os.path.join(layers_output, "W_%s.npy" % layer_name))) and (layer_name[:10] != "scale_pool"):
        print('Loading {}'.format(layer_name))
        w = np.array(np.load(os.path.join(layers_output, "W_%s.npy" % layer_name)).tolist())
        try:
            b = np.array(np.load(os.path.join(layers_output, "b_%s.npy" % layer_name)).tolist())
        except:
          None

        try:
            w = np.transpose(w, (2, 3, 1, 0))
        except:
            None
        
        try:
            layer_weights = [w, b]
            layer.set_weights(layer_weights)
        except:
            layer_weights = [w]
            layer.set_weights(layer_weights)

end_style_time = time.time()
print('Elapsed time in weights setting: %f' % (end_style_time - start_style_time))<jupyter_output><empty_output><jupyter_text>Save FCN8s model weights to a .h5 file<jupyter_code>model.save_weights("Keras_FCN8s_face_seg_YuvalNirkin.h5")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14250056727932833, 'fraction_numerical': 0.06353528477422282, 'mean_word_length': 4.2727272727272725, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 5, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '187313', 'license': 'MIT', 'n_tokens_mistral': 2008, 'n_tokens_neox': 1787, 'n_words': 364, 'path': 'FCN8s_caffe2keras_weights.ipynb', 'repo_name': 'shaoanlu/face-segmentation-keras', 'size': '4407'}