Skip to content

Commit

Permalink
fix: save perceptron as image
Browse files Browse the repository at this point in the history
  • Loading branch information
visrut7 committed Jan 1, 2024
1 parent af5f158 commit 14c6a05
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
6 changes: 5 additions & 1 deletion src/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module main

import os
import rand
import ppm
import input_layer { InputLayer }
import perceptron { Perceptron }

Expand Down Expand Up @@ -55,5 +56,8 @@ fn main() {

train(mut nn, mut layer)
accuracy := test(nn, mut layer)
println(accuracy)
println('Accuracy: ${accuracy}')

image := ppm.create_ppm_from_matrix(nn.get_weights())
image.save_image('images/perceptron.ppm')!
}
9 changes: 4 additions & 5 deletions src/ppm/ppm.v
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module ppm

import os
import math
import matrix { Matrix }

pub struct ImageDimensions {
Expand All @@ -26,11 +25,11 @@ pub fn create_ppm_from_matrix(mat Matrix) PPM {
for row in mat {
for value in row {
// Convert the f64 value to an integer for grayscale (0 or 255)
color_value := int(math.ceil(value * 255))
color_value := (value + 255) / (2.0 * 255)
// Append RGB components (all the same for grayscale)
buffer << color_value
buffer << color_value
buffer << color_value
buffer << int(255 * (1.0 - color_value))
buffer << int(255 * (1.0 - color_value))
buffer << int(255 * color_value)
}
}

Expand Down

0 comments on commit 14c6a05

Please sign in to comment.