public PostScoreAction GetTrackInOut(BlockedImage image, int InLabel) { if (image.Label == InLabel) { return(TrackRelevantInBlocks); } else { return(TrackRelevantOutBlocks); } }
static float GetScore(BlockedImage image, Kernel kernel, PostScoreAction action = null) { var blocks = image.Blocks; float score = float.MaxValue; for (int i = 0; i < blocks.Length; i++) { var block = blocks[i]; float error = kernel.Score(block, action); score = Math.Min(score, error); } return(score); }
static int ClassifyImage(BlockedImage image, List <List <Tuple <AffineKernel, float> > > kernel_lists) { var blocks = image.Blocks; float[] hits = new float[NumLabels]; for (int label = 0; label < NumLabels; label++) { var kernel_list = kernel_lists[label]; foreach (var kernel in kernel_list) { float score = GetScore(image, kernel.Item1); if (score < kernel.Item1.ClassificationCutoff) { //hits[i]++; hits[label] += (float)Math.Log(kernel.Item2); } } } int argmax = -1; float max = 0; for (int label = 0; label < NumLabels; label++) { if (argmax < 0 || hits[label] > max) { argmax = label; max = hits[label]; } } Console.WriteLine($"Hits {s(hits)}"); Console.WriteLine($" Classify as {argmax}, actually a {image.Label}"); return(argmax); }
public static BlockedImage[] GetBlocks(LabeledImage[] inputs, int Rows, int Width, int Height, int Channels, int BlockDim, bool AddMirrors = false) { int BlockSize = BlockDim * BlockDim * Channels; // Unpack blocks. int num_blocks = (Width - (BlockDim - 1)) * (Height - (BlockDim - 1)); if (AddMirrors) { num_blocks *= 8; } var blocked_images = new BlockedImage[Rows]; for (int row = 0; row < Rows; row++) { var image = blocked_images[row] = new BlockedImage(inputs[row]); var pixels = image.Pixels; var image_blocks = image.Blocks = new float[num_blocks][]; int block_index = 0; for (int mirror = 0; mirror < (AddMirrors ? 8 : 1); mirror++) { for (int x = 0; x < Width - (BlockDim - 1); x++) { for (int y = 0; y < Height - (BlockDim - 1); y++) { var block = image_blocks[block_index] = new float[BlockSize]; block_index++; int i = 0; for (int _x = 0; _x < BlockDim; _x++) { for (int _y = 0; _y < BlockDim; _y++) { for (int _c = 0; _c < Channels; _c++) { if (mirror == 0) { block[i++] = (float)pixels[x + _x, y + _y, _c]; } else if (mirror == 1) { block[i++] = (float)pixels[x + BlockDim - 1 - _x, y + _y, _c]; } else if (mirror == 2) { block[i++] = (float)pixels[x + _x, y + BlockDim - 1 - _y, _c]; } else if (mirror == 3) { block[i++] = (float)pixels[x + BlockDim - 1 - _x, y + BlockDim - 1 - _y, _c]; } else if (mirror == 4) { block[i++] = (float)pixels[y + _y, x + _x, _c]; } else if (mirror == 5) { block[i++] = (float)pixels[y + BlockDim - 1 - _y, x + _x, _c]; } else if (mirror == 6) { block[i++] = (float)pixels[y + _y, x + BlockDim - 1 - _x, _c]; } else if (mirror == 7) { block[i++] = (float)pixels[y + BlockDim - 1 - _y, x + BlockDim - 1 - _x, _c]; } } } } } } } Debug.Assert(block_index == blocked_images[row].Blocks.Length); } return(blocked_images); }