def calculate_parameter(request): if request.method == 'POST': net = yaml.safe_load(request.POST.get('net')) try: # While calling get_shapes we need to remove the flag # added in frontend to show the parameter on pane netObj = copy.deepcopy(net) for layerId in netObj: for param in netObj[layerId]['params']: netObj[layerId]['params'][param] = netObj[layerId][ 'params'][param][0] # use get_shapes method to obtain shapes of each layer netObj = get_shapes(netObj) for layerId in net: net[layerId]['shape'] = {} net[layerId]['shape']['input'] = netObj[layerId]['shape'][ 'input'] net[layerId]['shape']['output'] = netObj[layerId]['shape'][ 'output'] except BaseException: return JsonResponse({ 'result': 'error', 'error': str(sys.exc_info()[1]) }) return JsonResponse({'result': 'success', 'net': net})
def test_tf_export(self): model_file = open(os.path.join(settings.BASE_DIR, 'example/keras', 'AlexNet.json'), 'r') response = self.client.post(reverse('keras-import'), {'file': model_file}) response = json.loads(response.content) net = get_shapes(response['net']) response = self.client.post(reverse('tf-export'), {'net': json.dumps(net), 'net_name': ''}) response = json.loads(response.content) self.assertEqual(response['result'], 'success')
def test_tf_export(self): model_file = open( os.path.join(settings.BASE_DIR, 'example/keras', 'AlexNet.json'), 'r') response = self.client.post(reverse('keras-import'), {'file': model_file}) response = json.loads(response.content) net = get_shapes(response['net']) response = self.client.post(reverse('tf-export'), { 'net': json.dumps(net), 'net_name': '' }) response = json.loads(response.content) self.assertEqual(response['result'], 'success')
def test_shapes(self): with open( os.path.join(settings.BASE_DIR, 'example/keras', 'vgg16.json'), 'r') as f: response = self.client.post(reverse('keras-import'), {'file': f}) response = json.loads(response.content) net = get_shapes(response['net']) with open( os.path.join(settings.BASE_DIR, 'example/keras', 'vgg16.json'), 'r') as f: model = model_from_json(json.dumps(json.load(f))) for layer in model.layers: self.assertEqual(list(layer.output_shape[::-1][:-1]), net[layer.name]['shape']['output'])
def export_json(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' try: net = get_shapes(net) except BaseException: return JsonResponse({ 'result': 'error', 'error': str(sys.exc_info()[1]) }) layer_map = { 'ImageData': data, 'Data': data, 'Input': data, 'WindowData': data, 'MemoryData': data, 'DummyData': data, 'InnerProduct': dense, 'Softmax': activation, 'SELU': activation, 'Softplus': activation, 'Softsign': activation, 'ReLU': activation, 'TanH': activation, 'Sigmoid': activation, 'HardSigmoid': activation, 'Dropout': dropout, 'Flatten': flatten, 'Reshape': reshape, 'Permute': permute, 'RepeatVector': repeat_vector, 'Regularization': regularization, 'Masking': masking, 'Convolution': convolution, 'Deconvolution': deconvolution, 'Upsample': upsample, 'Pooling': pooling, 'LocallyConnected': locally_connected, 'RNN': recurrent, 'GRU': recurrent, 'LSTM': recurrent, 'Embed': embed, 'Concat': concat, 'Eltwise': eltwise, 'PReLU': activation, 'ELU': activation, 'ThresholdedReLU': activation, 'BatchNorm': batch_norm, 'GaussianNoise': gaussian_noise, 'GaussianDropout': gaussian_dropout, 'AlphaDropout': alpha_dropout, 'Scale': '' } # Check if conversion is possible for layerId in net: layerType = net[layerId]['info']['type'] if ('Loss' in layerType or layerType == 'Accuracy' or layerType in layer_map): pass else: return JsonResponse({ 'result': 'error', 'error': 'Cannot convert ' + layerType + ' to Keras' }) stack = [] net_out = {} dataLayers = [ 'ImageData', 'Data', 'HDF5Data', 'Input', 'WindowData', 'MemoryData', 'DummyData' ] processedLayer = {} inputLayerId = [] outputLayerId = [] def isProcessPossible(layerId): inputs = net[layerId]['connection']['input'] for layerId in inputs: if processedLayer[layerId] is False: return False return True # Finding the data layer for layerId in net: processedLayer[layerId] = False if (net[layerId]['info']['type'] == 'Python'): return JsonResponse({ 'result': 'error', 'error': 'Cannot convert Python to Keras' }) if (net[layerId]['info']['type'] in dataLayers): stack.append(layerId) if (not net[layerId]['connection']['input']): inputLayerId.append(layerId) if (not net[layerId]['connection']['output']): outputLayerId.append(layerId) while (len(stack)): if ('Loss' in net[layerId]['info']['type'] or net[layerId]['info']['type'] == 'Accuracy'): pass elif (net[layerId]['info']['type'] in layer_map): i = len(stack) - 1 while isProcessPossible(stack[i]) is False: i = i - 1 layerId = stack[i] stack.remove(layerId) if (net[layerId]['info']['type'] != 'Scale'): layer_in = [ net_out[inputId] for inputId in net[layerId]['connection']['input'] ] # Need to check if next layer is Scale if (net[layerId]['info']['type'] == 'BatchNorm'): idNext = net[layerId]['connection']['output'][0] nextLayer = net[idNext] # If the BN layer is followed by Scale, then we need to pass both layers # as in Keras parameters from both go into one single layer net_out.update(layer_map[net[layerId]['info']['type']]( net[layerId], layer_in, layerId, idNext, nextLayer)) elif (net[layerId]['info']['type'] == 'Scale'): type = net[net[layerId]['connection']['input'] [0]]['info']['type'] if (type != 'BatchNorm'): return JsonResponse({ 'result': 'error', 'error': 'Cannot convert ' + net[layerId]['info']['type'] + ' to Keras' }) else: net_out.update(layer_map[net[layerId]['info']['type']]( net[layerId], layer_in, layerId)) for outputId in net[layerId]['connection']['output']: if outputId not in stack: stack.append(outputId) processedLayer[layerId] = True else: return JsonResponse({ 'result': 'error', 'error': 'Cannot convert ' + net[layerId]['info']['type'] + ' to Keras' }) final_input = [] final_output = [] for i in inputLayerId: final_input.append(net_out[i]) for j in outputLayerId: final_output.append(net_out[j]) model = Model(inputs=final_input, outputs=final_output, name=net_name) json_string = Model.to_json(model) randomId = datetime.now().strftime('%Y%m%d%H%M%S') + randomword(5) with open(BASE_DIR + '/media/' + randomId + '.json', 'w') as f: json.dump(json.loads(json_string), f, indent=4) return JsonResponse({ 'result': 'success', 'id': randomId, 'name': randomId + '.json', 'url': '/media/' + randomId + '.json' })
def exportJson(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' try: net = get_shapes(net) except: return JsonResponse({ 'result': 'error', 'error': str(sys.exc_info()[1]) }) layer_map = { 'ImageData': data, 'Data': data, 'Input': data, 'WindowData': data, 'MemoryData': data, 'DummyData': data, 'Convolution': convolution, 'Pooling': pooling, 'Deconvolution': deconvolution, 'RNN': recurrent, 'LSTM': recurrent, 'InnerProduct': dense, 'Dropout': dropout, 'Embed': embed, 'Concat': concat, 'Eltwise': eltwise, 'BatchNorm': batchNorm, 'ReLU': activation, 'PReLU': activation, 'ELU': activation, 'Sigmoid': activation, 'TanH': activation, 'Flatten': flatten, 'Reshape': reshape, 'Softmax': activation, 'Scale': '' } stack = [] net_out = {} dataLayers = [ 'ImageData', 'Data', 'HDF5Data', 'Input', 'WindowData', 'MemoryData', 'DummyData' ] processedLayer = {} inputLayerId = None outputLayerId = None def isProcessPossible(layerId): inputs = net[layerId]['connection']['input'] for layerId in inputs: if processedLayer[layerId] is False: return False return True # Finding the data layer for layerId in net: processedLayer[layerId] = False if (net[layerId]['info']['type'] == 'Python'): return JsonResponse({ 'result': 'error', 'error': 'Cannot convert Python to Keras' }) if (net[layerId]['info']['type'] in dataLayers): stack.append(layerId) if (not net[layerId]['connection']['input']): inputLayerId = layerId if (not net[layerId]['connection']['output']): outputLayerId = layerId while (len(stack)): if ('Loss' in net[layerId]['info']['type'] or net[layerId]['info']['type'] == 'Accuracy'): pass elif (net[layerId]['info']['type'] in layer_map): i = len(stack) - 1 while isProcessPossible(stack[i]) is False: i = i - 1 layerId = stack[i] stack.remove(layerId) if (net[layerId]['info']['type'] != 'Scale'): layer_in = [ net_out[inputId] for inputId in net[layerId]['connection']['input'] ] # Need to check if next layer is Scale if (net[layerId]['info']['type'] == 'BatchNorm'): type = net[net[layerId]['connection']['output'] [0]]['info']['type'] idNext = net[layerId]['connection']['output'][0] net_out.update(layer_map[net[layerId]['info']['type']]( net[layerId], layer_in, layerId, type, idNext)) elif (net[layerId]['info']['type'] == 'Scale'): type = net[net[layerId]['connection']['input'] [0]]['info']['type'] if (type != 'BatchNorm'): return JsonResponse({ 'result': 'error', 'error': 'Cannot convert ' + net[layerId]['info']['type'] + ' to Keras' }) else: net_out.update(layer_map[net[layerId]['info']['type']]( net[layerId], layer_in, layerId)) for outputId in net[layerId]['connection']['output']: if outputId not in stack: stack.append(outputId) processedLayer[layerId] = True else: return JsonResponse({ 'result': 'error', 'error': 'Cannot convert ' + net[layerId]['info']['type'] + ' to Keras' }) model = Model(net_out[inputLayerId], net_out[outputLayerId], name=net_name) json_string = Model.to_json(model) randomId = datetime.now().strftime('%Y%m%d%H%M%S') + randomword(5) with open(BASE_DIR + '/media/' + randomId + '.json', 'w') as f: json.dump(json.loads(json_string), f, indent=4) return JsonResponse({ 'result': 'success', 'id': randomId, 'name': randomId + '.json', 'url': '/media/' + randomId + '.json' })