Example #1
0
File: ai.c Project: ceppa/socket
void addNode(int id_sistema,int id_digital,int id_digital_out,
		int id_ai_sottosistema)
{
	register int i,j;
	struct ai_system_node *system_temp;
	struct ai_input_node *input_temp;
	struct ai_output_node *output_temp;

	i=0;
	while((i<NUM_AI_SISTEMI)&&(ai_sistemi[i].id_sistema!=id_sistema))
		i++;

	if(i==NUM_AI_SISTEMI)
		return;

	system_temp=&ai_sistemi[i];


	input_temp=system_temp->input_nodes;
	while(input_temp && (input_temp->id_digital!=id_digital))
		input_temp=input_temp->next;
	if(!input_temp)
	{
		input_temp=addInputNode(system_temp->input_nodes,id_digital);
		system_temp->input_nodes=input_temp;
	}

	output_temp=input_temp->output_nodes;
	while(output_temp && (output_temp->id_digital_out!=id_digital_out))
		output_temp=output_temp->next;
	if(!output_temp)
	{
		output_temp=addOutputNode(input_temp->output_nodes,id_digital_out);
		input_temp->output_nodes=output_temp;
	}


	if(id_ai_sottosistema)
	{
		for(i=0;i<system_temp->n_ss;i++)
			if((system_temp->ss_nodes[i].id_ai_sottosistema==-1)||
					(system_temp->ss_nodes[i].id_ai_sottosistema==id_ai_sottosistema))
				break;

		if((i<system_temp->n_ss)&&
			(system_temp->ss_nodes[i].id_ai_sottosistema==id_ai_sottosistema))
		{
			for(j=0;j<system_temp->ss_nodes[i].n_in;j++)
				if((system_temp->ss_nodes[i].ai_input_nodes[j]==0)||
					(system_temp->ss_nodes[i].ai_input_nodes[j]==input_temp))
					break;
			if(j<system_temp->ss_nodes[i].n_in)
				system_temp->ss_nodes[i].ai_input_nodes[j]=input_temp;
		}			
	}
}
Example #2
0
void TensorFlowEngine::load() {
	const auto networkFilename = getFilename();
	tensorflow::SessionOptions options;
	tensorflow::ConfigProto &config = options.config;
	tensorflow::GPUOptions* gpuOptions = config.mutable_gpu_options();
	gpuOptions->set_allow_growth(true); // Set this so that tensorflow will not use up all GPU memory
	//gpuOptions->set_per_process_gpu_memory_fraction(0.5);
	mSession.reset(tensorflow::NewSession(options));
	tensorflow::GraphDef tensorflow_graph;

	{
		reportInfo() << "Loading network file: " << networkFilename << reportEnd();
		tensorflow::Status s = ReadBinaryProto(tensorflow::Env::Default(), networkFilename, &tensorflow_graph);
		if (!s.ok()) {
			throw Exception("Could not read TensorFlow graph file " + networkFilename);
		}
	}

	bool nodesSpecified = true;
    int inputCounter = 0;
	if(mInputNodes.size() == 0) {
		nodesSpecified = false;
	}
    for(int i = 0; i < tensorflow_graph.node_size(); ++i) {
		tensorflow::NodeDef node = tensorflow_graph.node(i);
		if(mInputNodes.count(node.name()) > 0) {
		}
		if(node.op() == "Placeholder") {
			if(node.name().find("keras_learning_phase") != std::string::npos) {
				//mLearningPhaseTensors.insert(node.name());
				mLearningPhaseTensors.push_back(node.name());
			} else {
				// Input node found:
				// Get its shape
				// Input nodes use the Op Placeholder
				reportInfo() << "Found input node: " << i << " with name " << node.name() << reportEnd();
				auto shape = getShape(node);
				reportInfo() << "Node has shape " << shape.toString() << reportEnd();
				if(mInputNodes.count(node.name()) == 0) {
					if(nodesSpecified) {
						throw Exception("Encountered unknown node " + node.name());
					}
					reportInfo() << "Node was not specified by user" << reportEnd();
					// If node has not been specified by user, we need to add it
					// and thus know its type (fast image or tensor)
					// It is assumed to be an image if input shape has at least 4 dimensions
					NodeType type = NodeType::TENSOR;
					if(shape.getKnownDimensions() >= 2) {
						reportInfo() << "Assuming node is an image" << reportEnd();
						type = NodeType::IMAGE;
					} else {
						reportInfo() << "Assuming node is a tensor" << reportEnd();
					}
					addInputNode(inputCounter, tensorflow_graph.node(0).name(), type, shape);
					++inputCounter;
				}

				// Set its shape
				mInputNodes[node.name()].shape = shape;
			}
		}
	}

	reportInfo() << "Creating session." << reportEnd();
	tensorflow::Status s = mSession->Create(tensorflow_graph);
	if (!s.ok()) {
		throw Exception("Could not create TensorFlow Graph");
	}

	//tensorflow::graph::SetDefaultDevice("/gpu:0", &tensorflow_graph);

	// Clear the proto to save memory space.
	tensorflow_graph.Clear();
	reportInfo() << "TensorFlow graph loaded from: " << networkFilename << reportEnd();

	setIsLoaded(true);
}