Skip to content

Instantly share code, notes, and snippets.

@jzstark
Last active August 23, 2017 11:36
Show Gist options
  • Select an option

  • Save jzstark/980a43a65e78613b4912c69008b3e909 to your computer and use it in GitHub Desktop.

Select an option

Save jzstark/980a43a65e78613b4912c69008b3e909 to your computer and use it in GitHub Desktop.
A general visualisation script for MNIST and CIFAR dataset

VisualModule

Visualize image dataset. Currently support MNIST and CIFAR10. visualize_dataset takes 2 arguments:

  1. imgs of type Dense.Ndarray.D.arr,
  2. len, of type int, for showing lenxlen images sample from the imgs
#!/usr/bin/env owl
open Owl
let visualize_dataset x len =
assert( len > 1 && len < 100 );
let num = len * len in
let mats = Array.make num (Dense.Matrix.D.zeros 1 1) in
for i = 0 to num - 1 do
let fig = Dense.Ndarray.D.get_slice_simple [[i];[];[]] x
|> Dense.Ndarray.D.squeeze
|> Dense.Matrix.D.of_ndarray
in
mats.(i) <- fig;
done;
let mats2 = Array.make len (Dense.Matrix.D.zeros 1 1) in
for i = 0 to (len-1) do
let m = Array.sub mats (i*len) len
|> Dense.Matrix.D.concatenate ~axis:0
in
mats2.(i) <- m
done;
let img = Dense.Matrix.D.concatenate ~axis:1 mats2 in
Plot.image img
(*
let x, _, _ = Dataset.load_cifar_test_data () in
let x = Dense.Ndarray.Generic.cast_s2d x
|> Dense.Ndarray.D.slice [[];[];[];[0]]
|> Dense.Ndarray.D.squeeze
in
visualize_dataset x 10
*)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment