keras_to_tensorflow その3

#概要
kerasのモデルをtensorflowに、変換して、convnet.jsで、使ってみた。
手順を記載する。

#環境
windows 7 sp1 64bit
anaconda3
tensorflow 1.2

#kerasのモデルをtensorflowに、変換したpbファイルから、convnetjsのモデルjsonをつくるコード。

```
import os
import os.path
import tensorflow as tf
from tensorflow.python.platform import gfile
import string
import json
import struct

with gfile.FastGFile("keras16.pb", 'rb') as f:
	graph_def = tf.GraphDef()
	graph_def.ParseFromString(f.read())
	_ = tf.import_graph_def(graph_def, name = '')
	layers = []
	flt = []
	nx = 0
	for n0 in graph_def.node:
		if n0.op == 'Placeholder':
			shape = n0.attr['shape'].shape
			layers += [{
				'layer_type': 'input',
				'out_depth': shape.dim[1].size,
				'out_sx': 1,
				'out_sy': 1
			}]
		elif n0.op == 'Const':
			tensor = n0.attr['value'].tensor
			if n0.name.endswith("/kernel"):
				nx = tensor.tensor_shape.dim[0].size
				n = tensor.tensor_shape.dim[1].size
				k = 0;
				for i in range(n):
					k = i * 4
					json_arr = {
						'sx': 1,
						'sy': 1,
						'depth': nx,
						'w': {}
					}
					for j in range(nx):
						json_arr['w'][str(j)] = struct.unpack('<f', tensor.tensor_content[k : k + 4])[0]
						k += 4 * n;
					flt += [json_arr]
			elif n0.name.endswith("/bias"):
				n = tensor.tensor_shape.dim[0].size
				json_arr = {
					'sx': 1,
					'sy': 1,
					'depth': n,
					'w': {}
				}
				if n == 1:
					json_arr['w'][str(0)] = tensor.float_val[0]
				else:
					for i in range(n):
						j = i * 4
						json_arr['w'][str(i)] = struct.unpack('<f', tensor.tensor_content[j : j + 4])[0]
				fc_array = {
					'layer_type': 'fc',
					'num_inputs': nx,
					'out_depth': n,
					'l1_decay_mul': 0,
					'l2_decay_mul': 1,
					'out_sx': 1,
					'out_sy': 1,
					'filters': flt,
					'biases': json_arr
				}
				layers += [fc_array]
				nx = tensor.tensor_shape.dim[0].size
				flt = []
		elif n0.op == 'Sigmoid':
			layers += [{
				'layer_type': 'sigmoid',
				'out_depth': nx,
				'out_sx': 1,
				'out_sy': 1
			}]
		elif n0.op == 'StridedSlice':
			layers += [{
				'layer_type': 'regression',
				'out_depth': 1,
				'out_sx': 1,
				'out_sy': 1,
				'num_inputs': 1
			}]
	print ('Done.')
	net = {
		'layers': layers
	}
	#print (net)
	f = open("./keras16net2.json", 'w')
	f.write(json.dumps(net))


```

#写真
![image.png](https://qiita-image-store.s3.amazonaws.com/0/18104/c620595e-9173-2c5b-ef69-cc6a1471cf37.png)


#サンプルコード

```
var net = new convnetjs.Net();
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
function draw(data, n) {
	var hc = n * 100 + 100;
    ctx.strokeStyle = "#f00";
    ctx.lineWidth = 1;
	ctx.moveTo(0, hc);
    for (var i = 1; i < canvas.width; i++) 
    {
        ctx.lineTo(i, hc - data[i] * 30);
    }
	ctx.stroke();
}
function test() {
    var s = 200;
    var ss = new Float32Array(s);
    for (var t = 0; t < s; t++)
    {
        var i = t / 30.0;
        var netx = new convnetjs.Vol(1, 1, 1);
        netx.w[0] = i;
        var ans = net.forward(netx);
        ss[t] = ans.w[0];
    }    
    draw(ss, 0);
}
$.getJSON("http://jsrun.it/assets/4/W/g/z/4Wgzx", function(json) {
    net.fromJSON(json);
    test();
});


```
#jsonデータ

```
{"layers":[{"out_depth":1,"out_sx":1,"out_sy":1,"layer_type":"input"},{"out_depth":30,"out_sx":1,"out_sy":1,"layer_type":"fc","num_inputs":1,"l1_decay_mul":0,"l2_decay_mul":1,"filters":[{"sx":1,"sy":1,"depth":1,"w":{"0":-0.252198725938797}},{"sx":1,"sy":1,"depth":1,"w":{"0":0.24867171049118042}},{"sx":1,"sy":1,"depth":1,"w":{"0":-0.2970523238182068}},{"sx":1,"sy":1,"depth":1,"w":{"0":0.2834603190422058}},{"sx":1,"sy":1,"depth":1,"w":{"0":-0.23915059864521027}},{"sx":1,"sy":1,"depth":1,"w":{"0":0.23399460315704346}},{"sx":1,"sy":1,"depth":1,"w":{"0":-0.2548165023326874}},{"sx":1,"sy":1,"depth":1,"w":{"0":0.21055857837200165}},{"sx":1,"sy":1,"depth":1,"w":{"0":-0.258003830909729}},{"sx":1,"sy":1,"depth":1,"w":{"0":0.26047882437705994}},{"sx":1,"sy":1,"depth":1,"w":{"0":-0.21398022770881653}},{"sx":1,"sy":1,"depth":1,"w":{"0":-0.24368037283420563}},{"sx":1,"sy":1,"depth":1,"w":{"0":0.24630802869796753}},{"sx":1,"sy":1,"depth":1,"w":{"0":-1.1452668905258179}},{"sx":1,"sy":1,"depth":1,"w":{"0":0.1726691722869873}},{"sx":1,"sy":1,"depth":1,"w":{"0":-0.22082951664924622}},{"sx":1,"sy":1,"depth":1,"w":{"0":-0.32592421770095825}},{"sx":1,"sy":1,"depth":1,"w":{"0":0.2452477663755417}},{"sx":1,"sy":1,"depth":1,"w":{"0":0.24593061208724976}},{"sx":1,"sy":1,"depth":1,"w":{"0":0.27058935165405273}},{"sx":1,"sy":1,"depth":1,"w":{"0":1.917065978050232}},{"sx":1,"sy":1,"depth":1,"w":{"0":0.20580442249774933}},{"sx":1,"sy":1,"depth":1,"w":{"0":-0.17408236861228943}},{"sx":1,"sy":1,"depth":1,"w":{"0":-0.22818773984909058}},{"sx":1,"sy":1,"depth":1,"w":{"0":-0.2116508185863495}},{"sx":1,"sy":1,"depth":1,"w":{"0":-0.2744907736778259}},{"sx":1,"sy":1,"depth":1,"w":{"0":-0.27791154384613037}},{"sx":1,"sy":1,"depth":1,"w":{"0":0.24978920817375183}},{"sx":1,"sy":1,"depth":1,"w":{"0":-0.2303035408258438}},{"sx":1,"sy":1,"depth":1,"w":{"0":0.2241906076669693}}],"biases":{"sx":1,"sy":1,"depth":30,"w":{"0":0.5794162750244141,"1":-0.38121646642684937,"2":1.3126007318496704,"3":-0.1879628300666809,"4":0.8406424522399902,"5":-0.746708333492279,"6":0.6560930609703064,"7":-0.12273017317056656,"8":0.6356635093688965,"9":-0.8339259028434753,"10":0.2332572042942047,"11":0.7938699722290039,"12":-0.8718310594558716,"13":0.3273533880710602,"14":-0.3774126470088959,"15":0.1903451830148697,"16":1.4930496215820312,"17":-0.4182469844818115,"18":-0.47124648094177246,"19":-0.4390168786048889,"20":-0.3601115047931671,"21":-0.09782565385103226,"22":0.09459342807531357,"23":0.38465195894241333,"24":0.39213815331459045,"25":1.0765173435211182,"26":0.21930475533008575,"27":-0.2090425044298172,"28":0.36953285336494446,"29":-0.317187637090683}}},{"out_depth":30,"out_sx":1,"out_sy":1,"layer_type":"sigmoid"},{"out_depth":10,"out_sx":1,"out_sy":1,"layer_type":"fc","num_inputs":30,"l1_decay_mul":0,"l2_decay_mul":1,"filters":[{"sx":1,"sy":1,"depth":30,"w":{"0":-0.11694007366895676,"1":0.37920019030570984,"2":-0.31143859028816223,"3":0.21690942347049713,"4":0.21245482563972473,"5":0.040130775421857834,"6":-0.0712108165025711,"7":-0.24941010773181915,"8":-0.3375298082828522,"9":0.288911372423172,"10":0.036419909447431564,"11":0.0995960459113121,"12":0.26822954416275024,"13":0.031109165400266647,"14":0.48464444279670715,"15":0.011000392958521843,"16":-0.2879745662212372,"17":0.43085888028144836,"18":0.22948773205280304,"19":0.3408307135105133,"20":0.9299984574317932,"21":0.49288585782051086,"22":0.15011066198349,"23":-0.33704644441604614,"24":-0.4416848421096802,"25":-0.122994564473629,"26":-0.27103403210639954,"27":-0.035270288586616516,"28":-0.1102561354637146,"29":0.42089706659317017}},{"sx":1,"sy":1,"depth":30,"w":{"0":0.6975963115692139,"1":-0.08670920133590698,"2":0.8197983503341675,"3":0.27173277735710144,"4":0.6513783931732178,"5":-0.08944439142942429,"6":0.3588143587112427,"7":-0.39308491349220276,"8":0.41725537180900574,"9":-0.24301253259181976,"10":0.22235877811908722,"11":0.5672311186790466,"12":-0.5162369608879089,"13":0.030823810026049614,"14":-0.6165170669555664,"15":0.17722119390964508,"16":0.6780178546905518,"17":-0.4753911793231964,"18":-0.32568663358688354,"19":-0.5428140163421631,"20":0.35451674461364746,"21":-0.11865320056676865,"22":0.5179699659347534,"23":0.6901814937591553,"24":0.5304526090621948,"25":0.3977537751197815,"26":0.18804974853992462,"27":0.13274644315242767,"28":0.7279859185218811,"29":-0.04368359595537186}},{"sx":1,"sy":1,"depth":30,"w":{"0":0.24401862919330597,"1":-0.4399615526199341,"2":0.2723301351070404,"3":-0.5774849057197571,"4":0.14514033496379852,"5":-0.15964382886886597,"6":0.07850401103496552,"7":-0.6695399284362793,"8":0.1253729909658432,"9":-0.3858738839626312,"10":0.1731533408164978,"11":0.43012315034866333,"12":-0.5791959762573242,"13":0.5741389989852905,"14":-0.018604161217808723,"15":0.026524832472205162,"16":0.3858923316001892,"17":0.008528056554496288,"18":-0.4759930670261383,"19":-0.6186662912368774,"20":-0.9774520397186279,"21":-0.23367896676063538,"22":0.0670010969042778,"23":-0.322417289018631,"24":-0.21907909214496613,"25":0.455215722322464,"26":0.3877612352371216,"27":-0.01731923781335354,"28":0.16943176090717316,"29":-0.19350562989711761}},{"sx":1,"sy":1,"depth":30,"w":{"0":1.045014500617981,"1":-0.5544202923774719,"2":1.2028403282165527,"3":-0.6095587611198425,"4":0.8886433839797974,"5":-0.7497661709785461,"6":1.2231894731521606,"7":-0.020188847556710243,"8":1.1995888948440552,"9":-0.831339418888092,"10":0.675859808921814,"11":0.9077718257904053,"12":-0.6191965341567993,"13":0.228066086769104,"14":-0.1478724479675293,"15":0.7343593239784241,"16":1.5188746452331543,"17":-0.622872531414032,"18":-0.5052170157432556,"19":-0.6710270643234253,"20":0.5434594750404358,"21":-0.15902256965637207,"22":0.30793869495391846,"23":0.7453311681747437,"24":0.5952115058898926,"25":1.1950864791870117,"26":0.8974630832672119,"27":-0.5931983590126038,"28":0.6964063048362732,"29":-0.32197272777557373}},{"sx":1,"sy":1,"depth":30,"w":{"0":-0.08458106964826584,"1":-0.6121855974197388,"2":0.008588702417910099,"3":0.10650108754634857,"4":0.2113385796546936,"5":-0.12944373488426208,"6":0.12537609040737152,"7":-0.027201911434531212,"8":-0.23258504271507263,"9":0.07711637765169144,"10":-0.06464628130197525,"11":0.4662233889102936,"12":-0.626414954662323,"13":0.19351589679718018,"14":-0.42037317156791687,"15":-0.10193461924791336,"16":0.2037089765071869,"17":-0.22543735802173615,"18":-0.5926460027694702,"19":-0.35004013776779175,"20":-0.6794860363006592,"21":0.04400346428155899,"22":-0.07319304347038269,"23":-0.07890559732913971,"24":0.1538345217704773,"25":0.3838157653808594,"26":0.276594340801239,"27":-0.1982879638671875,"28":0.47648122906684875,"29":-0.3792995512485504}},{"sx":1,"sy":1,"depth":30,"w":{"0":0.40804705023765564,"1":0.13600705564022064,"2":0.3408474922180176,"3":-0.3736220598220825,"4":0.5286884903907776,"5":-0.5431213974952698,"6":0.2552008330821991,"7":0.10872433334589005,"8":0.23488864302635193,"9":0.11362893879413605,"10":-0.10633924603462219,"11":0.2187977284193039,"12":-0.40163248777389526,"13":-0.25833582878112793,"14":-0.5030902028083801,"15":0.2532399594783783,"16":-0.1067628338932991,"17":-0.3862687945365906,"18":-0.17531414330005646,"19":0.04496680200099945,"20":-0.15281426906585693,"21":-0.5769955515861511,"22":0.3933098614215851,"23":0.2660668194293976,"24":-0.10615917295217514,"25":0.2705269157886505,"26":0.3929785490036011,"27":-0.5160378813743591,"28":0.14573056995868683,"29":-0.07163412868976593}},{"sx":1,"sy":1,"depth":30,"w":{"0":0.6440809965133667,"1":-0.7195137143135071,"2":1.3462445735931396,"3":-0.12427382916212082,"4":1.164023995399475,"5":-1.060242772102356,"6":0.8485880494117737,"7":-0.570131778717041,"8":0.7458465695381165,"9":-1.0797802209854126,"10":0.611534595489502,"11":1.0725560188293457,"12":-1.1708933115005493,"13":1.2837179899215698,"14":-0.834616482257843,"15":0.3912068009376526,"16":1.37554931640625,"17":-0.5546064972877502,"18":-0.8199889063835144,"19":-0.6678659319877625,"20":-0.9324909448623657,"21":-0.5313703417778015,"22":0.38626739382743835,"23":0.4940357506275177,"24":0.7740050554275513,"25":1.1380268335342407,"26":0.12386205047369003,"27":-0.25104841589927673,"28":0.6307318210601807,"29":-0.6651782989501953}},{"sx":1,"sy":1,"depth":30,"w":{"0":-0.4079558253288269,"1":0.13716299831867218,"2":-0.5320633053779602,"3":-0.04014495760202408,"4":-0.21125896275043488,"5":0.056030090898275375,"6":0.022946443408727646,"7":0.2594187259674072,"8":-0.22428186237812042,"9":0.3023318648338318,"10":-0.11719858646392822,"11":-0.42561349272727966,"12":0.08867035806179047,"13":-0.46935975551605225,"14":0.0753459557890892,"15":-0.1717980057001114,"16":-0.6749632954597473,"17":0.13564050197601318,"18":-0.04438478499650955,"19":-0.17961207032203674,"20":0.23590250313282013,"21":-0.23139066994190216,"22":-0.1393207609653473,"23":-0.3467352092266083,"24":-0.47828084230422974,"25":-0.6098076105117798,"26":-0.19711385667324066,"27":0.15303152799606323,"28":-0.03918793052434921,"29":0.4330078661441803}},{"sx":1,"sy":1,"depth":30,"w":{"0":-0.017609653994441032,"1":-0.3737178146839142,"2":0.2601378858089447,"3":-0.20810578763484955,"4":-0.09165600687265396,"5":-0.5210478901863098,"6":0.05342121794819832,"7":-0.2674887776374817,"8":0.2518901228904724,"9":-0.39843514561653137,"10":0.06317880749702454,"11":0.1757383495569229,"12":-0.198709636926651,"13":0.13361914455890656,"14":0.2115311324596405,"15":0.10622917115688324,"16":0.1413428634405136,"17":-0.11404552310705185,"18":-0.2713416516780853,"19":-0.2517029345035553,"20":-0.6604055166244507,"21":-0.36579105257987976,"22":0.16290536522865295,"23":0.4180433750152588,"24":0.045796267688274384,"25":-0.18290498852729797,"26":0.3496202826499939,"27":-0.3903462886810303,"28":0.14078402519226074,"29":-0.39574819803237915}},{"sx":1,"sy":1,"depth":30,"w":{"0":0.21898625791072845,"1":-0.1732313632965088,"2":0.27970290184020996,"3":-0.10918881744146347,"4":0.2601388394832611,"5":-0.44276052713394165,"6":-0.21355794370174408,"7":-0.4846630096435547,"8":0.24646784365177155,"9":-0.17702022194862366,"10":-0.1801585853099823,"11":-0.22130714356899261,"12":-0.6319289803504944,"13":0.09637167304754257,"14":-0.06480512022972107,"15":0.4467417299747467,"16":0.2032078355550766,"17":-0.0009916162816807628,"18":-0.28140565752983093,"19":-0.12410516291856766,"20":-0.5804194211959839,"21":-0.33522945642471313,"22":-0.18935219943523407,"23":0.25023943185806274,"24":0.49269330501556396,"25":-0.2625834345817566,"26":0.3987228572368622,"27":-0.47360360622406006,"28":0.4646017849445343,"29":-0.43184220790863037}}],"biases":{"sx":1,"sy":1,"depth":10,"w":{"0":-0.011634528636932373,"1":0.19037598371505737,"2":-0.15005487203598022,"3":0.4665026366710663,"4":-0.10053843259811401,"5":0.00026858109049499035,"6":0.28939589858055115,"7":-0.2509874999523163,"8":-0.026834703981876373,"9":-0.19866259396076202}}},{"out_depth":10,"out_sx":1,"out_sy":1,"layer_type":"sigmoid"},{"out_depth":1,"out_sx":1,"out_sy":1,"layer_type":"fc","num_inputs":10,"l1_decay_mul":0,"l2_decay_mul":1,"filters":[{"sx":1,"sy":1,"depth":10,"w":{"0":1.3644064664840698,"1":-1.5397101640701294,"2":-1.4177484512329102,"3":-2.9571518898010254,"4":-0.9073122143745422,"5":-0.7947189211845398,"6":4.6401872634887695,"7":1.1956216096878052,"8":-0.8998972773551941,"9":-0.9273321628570557}}],"biases":{"sx":1,"sy":1,"depth":1,"w":{"0":1.1908890008926392}}},{"out_depth":1,"out_sx":1,"out_sy":1,"layer_type":"regression","num_inputs":1}]}
```

#成果物
http://jsdo.it/ohisama1/iVcWV

以上。