def test_json_to_prototxt(self): tests = open( os.path.join(settings.BASE_DIR, 'tests', 'unit', 'ide', 'caffe_export_test.json'), 'r') response = json.load(tests) tests.close() net = yaml.safe_load(json.dumps(response['net'])) net = {'l0': net['Input']} prototxt, input_dim = jsonToPrototxt(net, response['net_name']) self.assertEqual(net['l0']['info']['type'], 'Input')
def exportToCaffe(request): if request.method == 'POST': net = yaml.safe_load(request.POST.get('net')) net_name = request.POST.get('net_name') if net_name == '': net_name = 'Net' prototxt,input_dim = jsonToPrototxt(net,net_name) randomId=datetime.now().strftime('%Y%m%d%H%M%S')+randomword(5) with open(BASE_DIR+'/media/'+randomId+'.prototxt', 'w') as f: f.write(prototxt) return JsonResponse({'result': 'success','id': randomId, 'name': randomId+'.prototxt', 'url': '/media/'+randomId+'.prototxt'})
def test_json_to_prototxt(self): tests = open( os.path.join(settings.BASE_DIR, 'tests', 'unit', 'ide', 'caffe_export_test.json'), 'r') response = json.load(tests) tests.close() net = yaml.safe_load(json.dumps(response['net'])) net = {'l0': net['Input'], 'l1': net['Accuracy']} net['l0']['connection']['output'].append('l1') prototxt, input_dim = jsonToPrototxt(net, response['net_name']) self.assertGreater(len(prototxt), 9) self.assertEqual(net['l1']['info']['type'], 'Accuracy')
def exportToCaffe(request): if request.method == 'POST': net = yaml.safe_load(request.POST.get('net')) net_name = request.POST.get('net_name') if net_name == '': net_name = 'Net' prototxt, input_dim = jsonToPrototxt(net, net_name) randomId = datetime.now().strftime('%Y%m%d%H%M%S')+randomword(5) with open(BASE_DIR+'/media/'+randomId+'.prototxt', 'w') as f: f.write(prototxt) return JsonResponse({'result': 'success', 'id': randomId, 'name': randomId+'.prototxt', 'url': '/media/'+randomId+'.prototxt'})
def exportToTensorflow(request): if request.method == 'POST': net = yaml.safe_load(request.POST.get('net')) net_name = request.POST.get('net_name') if net_name == '': net_name = 'Net' prototxt, input_dim = jsonToPrototxt(net, net_name) randomId = datetime.now().strftime('%Y%m%d%H%M%S') + randomword(5) with open(BASE_DIR + '/media/' + randomId + '.prototxt', 'w') as f: f.write(prototxt) os.system('python ' + BASE_DIR + '/tensorflow_app/caffe-tensorflow/convert.py ' + BASE_DIR + '/media/' + randomId + '.prototxt --code-output-path=' + BASE_DIR + '/media/' + randomId + '.py') # NCHW to NHWC data format input_caffe = map(int, input_dim.split(',')) input_tensorflow = [] for i in [0, 2, 3, 1]: input_tensorflow.append(input_caffe[i]) # converting generated caffe-tensorflow code to graphdef try: net = __import__(str(randomId)) images = tf.placeholder(tf.float32, input_tensorflow) net = getattr(net, net_name)({'blob0': images}) graph_def = tf.get_default_graph().as_graph_def(add_shapes=True) with open(BASE_DIR + '/media/' + randomId + '.pbtxt', 'w') as f: f.write(str(graph_def)) except AssertionError: return JsonResponse({ 'result': 'error', 'error': 'Cannot convert to GraphDef' }) except AttributeError: return JsonResponse({ 'result': 'error', 'error': 'GraphDef not supported' }) return JsonResponse({ 'result': 'success', 'id': randomId, 'name': randomId + '.pbtxt', 'url': '/media/' + randomId + '.pbtxt' })
def exportToTensorflow(request): if request.method == 'POST': net = yaml.safe_load(request.POST.get('net')) net_name = request.POST.get('net_name') if net_name == '': net_name = 'Net' # rename input layers to 'data' inputs = get_inputs(net) for i in inputs: net[i]['props']['name'] = 'data' prototxt,input_dim = jsonToPrototxt(net,net_name) randomId=datetime.now().strftime('%Y%m%d%H%M%S')+randomword(5) with open(BASE_DIR+'/media/'+randomId+'.prototxt', 'w') as f: f.write(prototxt) convert(BASE_DIR+'/media/'+randomId+'.prototxt', None, None, BASE_DIR+'/media/'+randomId+'.py', 'test') # NCHW to NHWC data format input_caffe = input_dim input_tensorflow = [] for i in [0,2,3,1]: input_tensorflow.append(input_caffe[i]) # converting generated caffe-tensorflow code to graphdef try: net = __import__ (str(randomId)) images = tf.placeholder(tf.float32, input_tensorflow) # the name of the first layer should be 'data' ! net = getattr(net, net_name)({'data': images}) graph_def = tf.get_default_graph().as_graph_def(add_shapes=True) with open(BASE_DIR+'/media/'+randomId+'.pbtxt', 'w') as f: f.write(str(graph_def)) except AssertionError: return JsonResponse({'result': 'error', 'error': 'Cannot convert to GraphDef'}) except AttributeError: return JsonResponse({'result': 'error', 'error': 'GraphDef not supported'}) return JsonResponse({'result': 'success','id': randomId, 'name': randomId+'.pbtxt', 'url': '/media/'+randomId+'.pbtxt'})
def exportToTensorflow(request): if request.method == 'POST': net = yaml.safe_load(request.POST.get('net')) net_name = request.POST.get('net_name') if net_name == '': net_name = 'Net' prototxt, input_dim = jsonToPrototxt(net, net_name) randomId = datetime.now().strftime('%Y%m%d%H%M%S') + randomword(5) with open(BASE_DIR+'/media/'+randomId+'.prototxt', 'w') as f: f.write(prototxt) os.system('python ' + BASE_DIR + '/tensorflow_app/caffe-tensorflow/convert.py ' + BASE_DIR + '/media/' + randomId + '.prototxt --code-output-path=' + BASE_DIR + '/media/' + randomId + '.py') # NCHW to NHWC data format input_caffe = map(int, input_dim.split(',')) input_tensorflow = [] for i in [0, 2, 3, 1]: input_tensorflow.append(input_caffe[i]) # converting generated caffe-tensorflow code to graphdef try: net = __import__(str(randomId)) images = tf.placeholder(tf.float32, input_tensorflow) net = getattr(net, net_name)({'blob0': images}) graph_def = tf.get_default_graph().as_graph_def(add_shapes=True) with open(BASE_DIR+'/media/'+randomId+'.pbtxt', 'w') as f: f.write(str(graph_def)) except AssertionError: return JsonResponse({'result': 'error', 'error': 'Cannot convert to GraphDef'}) except AttributeError: return JsonResponse({'result': 'error', 'error': 'GraphDef not supported'}) return JsonResponse({'result': 'success', 'id': randomId, 'name': randomId+'.pbtxt', 'url': '/media/'+randomId+'.pbtxt'})