{"id":741,"date":"2018-02-22T09:11:10","date_gmt":"2018-02-22T15:11:10","guid":{"rendered":"http:\/\/www.gergltd.com\/home\/?p=741"},"modified":"2018-02-22T09:11:10","modified_gmt":"2018-02-22T15:11:10","slug":"reading-in-weights-from-matconvnet-to-keras-dealing-with-kerasflatten","status":"publish","type":"post","link":"https:\/\/www.gergltd.com\/home\/reading-in-weights-from-matconvnet-to-keras-dealing-with-kerasflatten\/","title":{"rendered":"Reading in Weights from MatConvNet to Keras &#8212; Dealing with Keras::Flatten()"},"content":{"rendered":"<p>Reading in the weights&#8230;<\/p>\n<pre> # bastardized from https:\/\/aboveintelligent.com\/face-recognition-with-keras-and-opencv-2baf2a83b799\r\n from scipy.io import loadmat\r\n data = loadmat(weightsFilename,\r\n matlab_compatible=False,\r\n struct_as_record=False) \r\n \r\n net = data['net'][0,0]\r\n net = net.net_params[0,0]\r\n \r\n # first conv layer\r\n tmp = net.layers[1,0][0,0]\r\n weights = np.zeros((8,8,1,8))\r\n bias = np.zeros(8)\r\n for k in range(8):\r\n weights[:,:,0,k] = np.rot90(tmp.k[0,0][0,k], -2)\r\n bias[k] = tmp.b[0,k]\r\n model.layers[1].set_weights([weights, bias])<\/pre>\n<p>Keras Flatten() layer gave me some troubles.\u00a0 In MATLAB, the last level was flattened via&#8230;<\/p>\n<pre>% concatenate all end layer feature maps into vector\r\nnet.fv = [];\r\nfor j = 1 : numel(net.layers{n}.a)\r\n sa = size(net.layers{n}.a{j});\r\n net.fv = [net.fv; reshape(net.layers{n}.a{j}, sa(1) * sa(2), sa(3))];\r\nend<\/pre>\n<p>There is a final Dense layer output 2 classes.\u00a0 I read the weights in from MATLAB and then mangle them to produce a MATLAB faithful computation.<\/p>\n<pre> # Dense layer\r\n weights = np.transpose(data['net'][0,0].net_params[0,0].ffW)\r\n idx = np.arange(300).reshape((5,5,12)).flatten('F')\r\n idx = np.argsort(idx)\r\n weights = weights[idx,:]\r\n bias = data['net'][0,0].net_params[0,0].ffb.flatten()\r\n \r\n model.layers[8].set_weights([weights, bias])<\/pre>\n<p>Essentially, I mimic the forward mangling process.\u00a0 MATLAB is column major.\u00a0 So, I create a vector of 300 elements from 0 to 299 (my last conv layer output is 5x5x12). I flatten this vector using FORTRAN style flattening.\u00a0 Now, I must get the reverse mapping which is accomplished via and argsort.\u00a0 Finally, I reorder the MATLAB weight vector so that when keras reorders via flatten(), its in the same ordering as the MATLAB MatConvNet computation.<\/p>\n<pre>net.o = sigm(net.ffW * net.fv + repmat(net.ffb, 1, size(net.fv, 2)));<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Reading in the weights&#8230; # bastardized from https:\/\/aboveintelligent.com\/face-recognition-with-keras-and-opencv-2baf2a83b799 from scipy.io import loadmat data = loadmat(weightsFilename, matlab_compatible=False, struct_as_record=False) net = data[&#8216;net&#8217;][0,0] net = net.net_params[0,0] # first conv layer tmp = net.layers[1,0][0,0] weights = np.zeros((8,8,1,8)) bias = np.zeros(8) for k in range(8): weights[:,:,0,k] = np.rot90(tmp.k[0,0][0,k], -2) bias[k] = tmp.b[0,k] model.layers[1].set_weights([weights, bias]) Keras Flatten() layer gave me some [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[1],"tags":[64,62,61,63],"class_list":["post-741","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-argsort","tag-keras","tag-machine-learning","tag-matconvnet"],"_links":{"self":[{"href":"https:\/\/www.gergltd.com\/home\/wp-json\/wp\/v2\/posts\/741","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gergltd.com\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gergltd.com\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gergltd.com\/home\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gergltd.com\/home\/wp-json\/wp\/v2\/comments?post=741"}],"version-history":[{"count":1,"href":"https:\/\/www.gergltd.com\/home\/wp-json\/wp\/v2\/posts\/741\/revisions"}],"predecessor-version":[{"id":742,"href":"https:\/\/www.gergltd.com\/home\/wp-json\/wp\/v2\/posts\/741\/revisions\/742"}],"wp:attachment":[{"href":"https:\/\/www.gergltd.com\/home\/wp-json\/wp\/v2\/media?parent=741"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gergltd.com\/home\/wp-json\/wp\/v2\/categories?post=741"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gergltd.com\/home\/wp-json\/wp\/v2\/tags?post=741"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}