void SceneImporter<real>::ReadScene( std::istream& stream, Scene<real>& oScene ) { // Read the 'Scene' token ReadNextExactToken( stream, "Scene" ); ReadNextExactToken( stream, "{" ); // Read scene components std::string token = ReadNextToken( stream ); while( token != "}" ) { if ( token == "SceneInfo" ) { ReadNextExactToken( stream, "(" ); ReadSceneInfo( stream, oScene ); ReadNextExactToken( stream, ")" ); ReadNextExactToken( stream, ";" ); } else if ( token == "TurntableCamera" ) { std::string name = ReadNextToken( stream ); ReadNextExactToken( stream, "(" ); Camera<real>* camera = ReadTurntableCamera( stream, name ); ReadNextExactToken( stream, ")" ); ReadNextExactToken( stream, ";" ); oScene.AddObject( camera, true ); } else if ( token == "DirectionalLight" ) { std::string name = ReadNextToken( stream ); ReadNextExactToken( stream, "(" ); Light<real>* light = ReadDirectionalLight( stream, name ); ReadNextExactToken( stream, ")" ); ReadNextExactToken( stream, ";" ); oScene.AddObject( light, true ); } else if ( token == "PointLight" ) { std::string name = ReadNextToken( stream ); ReadNextExactToken( stream, "(" ); Light<real>* light = ReadPointLight( stream, name ); ReadNextExactToken( stream, ")" ); ReadNextExactToken( stream, ";" ); oScene.AddObject( light, true ); } else if ( token == "SpotLight" ) { std::string name = ReadNextToken( stream ); ReadNextExactToken( stream, "(" ); Light<real>* light = ReadSpotLight( stream, name ); ReadNextExactToken( stream, ")" ); ReadNextExactToken( stream, ";" ); oScene.AddObject( light, true ); } else if ( token == "BlinnPhongMaterial" ) { std::string name = ReadNextToken( stream ); ReadNextExactToken( stream, "(" ); Material* material = ReadBlinnPhongMaterial( stream, name ); ReadNextExactToken( stream, ")" ); ReadNextExactToken( stream, ";" ); oScene.AddObject( material, true ); } else if ( token == "EnvironmentMaterial" ) { std::string name = ReadNextToken( stream ); ReadNextExactToken( stream, "(" ); Material* material = ReadEnvironmentMaterial( stream, name ); ReadNextExactToken( stream, ")" ); ReadNextExactToken( stream, ";" ); oScene.AddObject( material, true ); } else if ( token == "CubeMap" ) { std::string name = ReadNextToken( stream ); ReadNextExactToken( stream, "(" ); Texture<real>* cubeMap = ReadCubeMap( stream, name ); ReadNextExactToken( stream, ")" ); ReadNextExactToken( stream, ";" ); oScene.AddObject( cubeMap, true ); } else if ( token == "Texture2D" ) { std::string name = ReadNextToken( stream ); ReadNextExactToken( stream, "(" ); Texture<real>* texture = ReadTexture2D( stream, name ); ReadNextExactToken( stream, ")" ); ReadNextExactToken( stream, ";" ); oScene.AddObject( texture, true ); } else if ( token == "Box" ) { std::string name = ReadNextToken( stream ); ReadNextExactToken( stream, "(" ); Node<real>* box = ReadBox( stream, name ); ReadNextExactToken( stream, ")" ); ReadNextExactToken( stream, ";" ); oScene.AddObject( box, true ); } else if ( token == "Cone" ) { std::string name = ReadNextToken( stream ); ReadNextExactToken( stream, "(" ); Node<real>* cone = ReadCone( stream, name ); ReadNextExactToken( stream, ")" ); ReadNextExactToken( stream, ";" ); oScene.AddObject( cone, true ); } else if ( token == "Cylinder" ) { std::string name = ReadNextToken( stream ); ReadNextExactToken( stream, "(" ); Node<real>* cylinder = ReadCylinder( stream, name ); ReadNextExactToken( stream, ")" ); ReadNextExactToken( stream, ";" ); oScene.AddObject( cylinder, true ); } else if ( token == "Plane" ) { std::string name = ReadNextToken( stream ); ReadNextExactToken( stream, "(" ); Node<real>* plane = ReadPlane( stream, name ); ReadNextExactToken( stream, ")" ); ReadNextExactToken( stream, ";" ); oScene.AddObject( plane, true ); } else if ( token == "Sphere" ) { std::string name = ReadNextToken( stream ); ReadNextExactToken( stream, "(" ); Node<real>* sphere = ReadSphere( stream, name ); ReadNextExactToken( stream, ")" ); ReadNextExactToken( stream, ";" ); oScene.AddObject( sphere, true ); } else if ( token == "TriMesh" ) { std::string name = ReadNextToken( stream ); ReadNextExactToken( stream, "(" ); Node<real>* triMesh = ReadTriMesh( stream, name ); ReadNextExactToken( stream, ")" ); ReadNextExactToken( stream, ";" ); oScene.AddObject( triMesh, true ); } else if ( token == "Node" ) { std::string name = ReadNextToken( stream ); ReadNextExactToken( stream, "(" ); Node<real>* node = ReadNode( stream, name ); ReadNextExactToken( stream, ")" ); ReadNextExactToken( stream, ";" ); oScene.AddObject( node, true ); } else { throw ( std::string( "Unknown token: " ) + token ).c_str(); } token = ReadNextToken( stream ); } // Read scene ending token ReadNextExactToken( stream, ";" ); }
__kernel void CalculateStatisticalMapSearchlight_(__global float* Classifier_Performance, __global const float* Volumes, __global const float* Mask, __constant float* c_d, __constant float* c_Correct_Classes, __private int DATA_W, __private int DATA_H, __private int DATA_D, __private int NUMBER_OF_VOLUMES, __private float n, __private int EPOCS) { int x = get_global_id(0); int y = get_global_id(1); int z = get_global_id(2); int3 tIdx = {get_local_id(0), get_local_id(1), get_local_id(2)}; if (x >= DATA_W || y >= DATA_H || z >= DATA_D) return; if ( Mask[Calculate3DIndex(x,y,z,DATA_W,DATA_H)] != 1.0f ) { Classifier_Performance[Calculate3DIndex(x,y,z,DATA_W,DATA_H)] = 0.0f; return; } __local float l_Volume[16][16][16]; // z, y, x int classification_performance = 0; // Leave one out cross validation for (int validation = 0; validation < NUMBER_OF_VOLUMES; validation++) { float weights[20]; weights[0] = 0.0f; weights[1] = 0.0f; weights[2] = 0.0f; weights[3] = 0.0f; weights[4] = 0.0f; weights[5] = 0.0f; weights[6] = 0.0f; weights[7] = 0.0f; weights[8] = 0.0f; weights[9] = 0.0f; weights[10] = 0.0f; weights[11] = 0.0f; weights[12] = 0.0f; weights[13] = 0.0f; weights[14] = 0.0f; weights[15] = 0.0f; weights[16] = 0.0f; weights[17] = 0.0f; weights[18] = 0.0f; weights[19] = 0.0f; // Do training for a number of iterations for (int epoc = 0; epoc < EPOCS; epoc++) { float gradient[20]; gradient[0] = 0.0f; gradient[1] = 0.0f; gradient[2] = 0.0f; gradient[3] = 0.0f; gradient[4] = 0.0f; gradient[5] = 0.0f; gradient[6] = 0.0f; gradient[7] = 0.0f; gradient[8] = 0.0f; gradient[9] = 0.0f; gradient[10] = 0.0f; gradient[11] = 0.0f; gradient[12] = 0.0f; gradient[13] = 0.0f; gradient[14] = 0.0f; gradient[15] = 0.0f; gradient[16] = 0.0f; gradient[17] = 0.0f; gradient[18] = 0.0f; gradient[19] = 0.0f; for (int t = 0; t < NUMBER_OF_VOLUMES; t++) { // Skip training with validation time point if (t == validation) { continue; } float s; // Classification for current timepoint ReadSphere((__local float*)l_Volume, Volumes, x, y, z, t, tIdx, DATA_W, DATA_H, DATA_D); // Make sure all threads have written to local memory barrier(CLK_LOCAL_MEM_FENCE); // Make classification s = weights[0] * 1.0f; // z - 1 s += weights[1] * l_Volume[tIdx.z + 4 - 1][tIdx.y + 4][tIdx.x + 4 - 1]; // s += weights[2] * l_Volume[tIdx.z + 4 - 1][tIdx.y + 4 - 1][tIdx.x + 4]; // s += weights[3] * l_Volume[tIdx.z + 4 - 1][tIdx.y + 4][tIdx.x + 4]; // center pixel s += weights[4] * l_Volume[tIdx.z + 4 - 1][tIdx.y + 4 + 1][tIdx.x + 4]; // s += weights[5] * l_Volume[tIdx.z + 4 - 1][tIdx.y + 4][tIdx.x + 4 + 1]; // // z s += weights[6] * l_Volume[tIdx.z + 4][tIdx.y + 4 - 1][tIdx.x + 4 - 1]; // s += weights[7] * l_Volume[tIdx.z + 4][tIdx.y + 4 - 1][tIdx.x + 4]; // s += weights[8] * l_Volume[tIdx.z + 4][tIdx.y + 4 - 1][tIdx.x + 4 + 1]; // s += weights[9] * l_Volume[tIdx.z + 4][tIdx.y + 4][tIdx.x + 4 - 1]; // s += weights[10] * l_Volume[tIdx.z + 4][tIdx.y + 4][tIdx.x + 4]; // center pixel s += weights[11] * l_Volume[tIdx.z + 4][tIdx.y + 4][tIdx.x + 4 + 1]; // s += weights[12] * l_Volume[tIdx.z + 4][tIdx.y + 4 + 1][tIdx.x + 4 - 1]; // s += weights[13] * l_Volume[tIdx.z + 4][tIdx.y + 4 + 1][tIdx.x + 4]; // s += weights[14] * l_Volume[tIdx.z + 4][tIdx.y + 4 + 1][tIdx.x + 4 + 1]; // // z + 1 s += weights[15] * l_Volume[tIdx.z + 4 + 1][tIdx.y + 4][tIdx.x + 4 - 1]; // s += weights[16] * l_Volume[tIdx.z + 4 + 1][tIdx.y + 4 - 1][tIdx.x + 4]; // s += weights[17] * l_Volume[tIdx.z + 4 + 1][tIdx.y + 4][tIdx.x + 4]; // center pixel s += weights[18] * l_Volume[tIdx.z + 4 + 1][tIdx.y + 4 + 1][tIdx.x + 4]; // s += weights[19] * l_Volume[tIdx.z + 4 + 1][tIdx.y + 4][tIdx.x + 4 + 1]; // // Calculate contribution to gradient gradient[0] += (s - c_d[t]) * 1.0f; // z - 1 gradient[1] += (s - c_d[t]) * l_Volume[tIdx.z + 4 - 1][tIdx.y + 4][tIdx.x + 4 - 1]; // gradient[2] += (s - c_d[t]) * l_Volume[tIdx.z + 4 - 1][tIdx.y + 4 - 1][tIdx.x + 4]; // gradient[3] += (s - c_d[t]) * l_Volume[tIdx.z + 4 - 1][tIdx.y + 4][tIdx.x + 4]; // center pixel gradient[4] += (s - c_d[t]) * l_Volume[tIdx.z + 4 - 1][tIdx.y + 4 + 1][tIdx.x + 4]; // gradient[5] += (s - c_d[t]) * l_Volume[tIdx.z + 4 - 1][tIdx.y + 4][tIdx.x + 4 + 1]; // // z gradient[6] += (s - c_d[t]) * l_Volume[tIdx.z + 4][tIdx.y + 4 - 1][tIdx.x + 4 - 1]; // gradient[7] += (s - c_d[t]) * l_Volume[tIdx.z + 4][tIdx.y + 4 - 1][tIdx.x + 4]; // gradient[8] += (s - c_d[t]) * l_Volume[tIdx.z + 4][tIdx.y + 4 - 1][tIdx.x + 4 + 1]; // gradient[9] += (s - c_d[t]) * l_Volume[tIdx.z + 4][tIdx.y + 4][tIdx.x + 4 - 1]; // gradient[10] += (s - c_d[t]) * l_Volume[tIdx.z + 4][tIdx.y + 4][tIdx.x + 4]; // center pixel gradient[11] += (s - c_d[t]) * l_Volume[tIdx.z + 4][tIdx.y + 4][tIdx.x + 4 + 1]; // gradient[12] += (s - c_d[t]) * l_Volume[tIdx.z + 4][tIdx.y + 4 + 1][tIdx.x + 4 - 1]; // gradient[13] += (s - c_d[t]) * l_Volume[tIdx.z + 4][tIdx.y + 4 + 1][tIdx.x + 4]; // gradient[14] += (s - c_d[t]) * l_Volume[tIdx.z + 4][tIdx.y + 4 + 1][tIdx.x + 4 + 1]; // // z + 1 gradient[15] += (s - c_d[t]) * l_Volume[tIdx.z + 4 + 1][tIdx.y + 4][tIdx.x + 4 - 1]; // gradient[16] += (s - c_d[t]) * l_Volume[tIdx.z + 4 + 1][tIdx.y + 4 - 1][tIdx.x + 4]; // gradient[17] += (s - c_d[t]) * l_Volume[tIdx.z + 4 + 1][tIdx.y + 4][tIdx.x + 4]; // center pixel gradient[18] += (s - c_d[t]) * l_Volume[tIdx.z + 4 + 1][tIdx.y + 4 + 1][tIdx.x + 4]; // gradient[19] += (s - c_d[t]) * l_Volume[tIdx.z + 4 + 1][tIdx.y + 4][tIdx.x + 4 + 1]; // // end for t } // Update weights weights[0] -= n/(float)NUMBER_OF_VOLUMES * gradient[0]; weights[1] -= n/(float)NUMBER_OF_VOLUMES * gradient[1]; weights[2] -= n/(float)NUMBER_OF_VOLUMES * gradient[2]; weights[3] -= n/(float)NUMBER_OF_VOLUMES * gradient[3]; weights[4] -= n/(float)NUMBER_OF_VOLUMES * gradient[4]; weights[5] -= n/(float)NUMBER_OF_VOLUMES * gradient[5]; weights[6] -= n/(float)NUMBER_OF_VOLUMES * gradient[6]; weights[7] -= n/(float)NUMBER_OF_VOLUMES * gradient[7]; weights[8] -= n/(float)NUMBER_OF_VOLUMES * gradient[8]; weights[9] -= n/(float)NUMBER_OF_VOLUMES * gradient[9]; weights[10] -= n/(float)NUMBER_OF_VOLUMES * gradient[10]; weights[11] -= n/(float)NUMBER_OF_VOLUMES * gradient[11]; weights[12] -= n/(float)NUMBER_OF_VOLUMES * gradient[12]; weights[13] -= n/(float)NUMBER_OF_VOLUMES * gradient[13]; weights[14] -= n/(float)NUMBER_OF_VOLUMES * gradient[14]; weights[15] -= n/(float)NUMBER_OF_VOLUMES * gradient[15]; weights[16] -= n/(float)NUMBER_OF_VOLUMES * gradient[16]; weights[17] -= n/(float)NUMBER_OF_VOLUMES * gradient[17]; weights[18] -= n/(float)NUMBER_OF_VOLUMES * gradient[18]; weights[19] -= n/(float)NUMBER_OF_VOLUMES * gradient[19]; // end for epocs } // Make classification on validation timepoint ReadSphere((__local float*)l_Volume, Volumes, x, y, z, validation, tIdx, DATA_W, DATA_H, DATA_D); // Make sure all threads have written to local memory barrier(CLK_LOCAL_MEM_FENCE); // Make classification float s; s = weights[0] * 1.0f; // z - 1 s += weights[1] * l_Volume[tIdx.z + 4 - 1][tIdx.y + 4][tIdx.x + 4 - 1]; // s += weights[2] * l_Volume[tIdx.z + 4 - 1][tIdx.y + 4 - 1][tIdx.x + 4]; // s += weights[3] * l_Volume[tIdx.z + 4 - 1][tIdx.y + 4][tIdx.x + 4]; // center pixel s += weights[4] * l_Volume[tIdx.z + 4 - 1][tIdx.y + 4 + 1][tIdx.x + 4]; // s += weights[5] * l_Volume[tIdx.z + 4 - 1][tIdx.y + 4][tIdx.x + 4 + 1]; // // z s += weights[6] * l_Volume[tIdx.z + 4][tIdx.y + 4 - 1][tIdx.x + 4 - 1]; // s += weights[7] * l_Volume[tIdx.z + 4][tIdx.y + 4 - 1][tIdx.x + 4]; // s += weights[8] * l_Volume[tIdx.z + 4][tIdx.y + 4 - 1][tIdx.x + 4 + 1]; // s += weights[9] * l_Volume[tIdx.z + 4][tIdx.y + 4][tIdx.x + 4 - 1]; // s += weights[10] * l_Volume[tIdx.z + 4][tIdx.y + 4][tIdx.x + 4]; // center pixel s += weights[11] * l_Volume[tIdx.z + 4][tIdx.y + 4][tIdx.x + 4 + 1]; // s += weights[12] * l_Volume[tIdx.z + 4][tIdx.y + 4 + 1][tIdx.x + 4 - 1]; // s += weights[13] * l_Volume[tIdx.z + 4][tIdx.y + 4 + 1][tIdx.x + 4]; // s += weights[14] * l_Volume[tIdx.z + 4][tIdx.y + 4 + 1][tIdx.x + 4 + 1]; // // z + 1 s += weights[15] * l_Volume[tIdx.z + 4 + 1][tIdx.y + 4][tIdx.x + 4 - 1]; // s += weights[16] * l_Volume[tIdx.z + 4 + 1][tIdx.y + 4 - 1][tIdx.x + 4]; // s += weights[17] * l_Volume[tIdx.z + 4 + 1][tIdx.y + 4][tIdx.x + 4]; // center pixel s += weights[18] * l_Volume[tIdx.z + 4 + 1][tIdx.y + 4 + 1][tIdx.x + 4]; // s += weights[19] * l_Volume[tIdx.z + 4 + 1][tIdx.y + 4][tIdx.x + 4 + 1]; // float classification; if (s > 0.0f) { classification = 0.0f; } else { classification = 1.0f; } if (classification == c_Correct_Classes[validation]) { classification_performance++; } // end for validation } Classifier_Performance[Calculate3DIndex(x,y,z,DATA_W,DATA_H)] = (float)classification_performance / (float)NUMBER_OF_VOLUMES; }