CIFAR10-CNN
A Neural network on CIFAR-10 dataset adopted from Keras' example.
| open Owl | |
| open Owl_neural | |
| open Algodiff.S | |
| open Owl_neural_neuron | |
| (* | |
| let test_cnn nn x y = | |
| for i = 0 to 9 do | |
| let u = Dense.Ndarray.S.slice [[i]] x in | |
| Dense.Ndarray.S.reshape u [|3;32;32|] | |
| |> Dense.Matrix.S.of_ndarray | |
| |> Dataset.print_mnist_image; | |
| let p = Graph.run (Arr u) nn |> Algodiff.S.unpack_mat in | |
| Owl_dense_matrix_generic.print p; | |
| Printf.printf "prediction: %i\n" (let _, _, j = Owl_dense_matrix_generic.max_i p in j) | |
| done | |
| let mnist_model () = | |
| let open Owl_neural_graph in | |
| let nn = input [|28;28;1|] | |
| |> conv2d [|3;3;1;32|] [|1;1|] ~act_typ:Activation.Relu ~padding:Owl_dense_ndarray_generic.VALID | |
| |> conv2d [|3;3;32;64|] [|1;1|] ~act_typ:Activation.Relu ~padding:Owl_dense_ndarray_generic.VALID | |
| |> max_pool2d [|2;2|] [|2;2|] | |
| |> dropout 0.25 | |
| |> fully_connected 1024 ~act_typ:Activation.Relu | |
| |> linear 10 ~act_typ:Activation.Softmax | |
| |> get_network | |
| in print nn; | |
| nn | |
| *) | |
| let model () = | |
| let open Owl_neural_graph in | |
| let nn = input [|32;32;3|] | |
| |> conv2d [|3;3;3;32|] [|1;1|] ~act_typ:Activation.Relu | |
| |> conv2d [|3;3;32;32|] [|1;1|] ~act_typ:Activation.Relu ~padding:Owl_dense_ndarray_generic.VALID | |
| |> max_pool2d [|2;2|] [|2;2|] ~padding:Owl_dense_ndarray_generic.VALID | |
| |> dropout 0.25 | |
| |> conv2d [|3;3;32;64|] [|1;1|] ~act_typ:Activation.Relu | |
| |> conv2d [|3;3;64;64|] [|1;1|] ~act_typ:Activation.Relu ~padding:Owl_dense_ndarray_generic.VALID | |
| |> max_pool2d [|2;2|] [|2;2|] ~padding:Owl_dense_ndarray_generic.VALID | |
| |> dropout 0.25 | |
| |> fully_connected 512 ~act_typ:Activation.Relu | |
| |> linear 10 ~act_typ:Activation.Softmax | |
| |> get_network | |
| in | |
| print nn; | |
| nn | |
| let train_cifar10_keras_graph () = | |
| let x, y = Dataset.load_cifar_train_data 1 in | |
| let m = Dense.Matrix.S.row_num x in (* 10000 *) | |
| let x = Dense.Matrix.S.to_ndarray x in | |
| let x = Dense.Ndarray.S.reshape x [|m;3;32;32|] in | |
| let x = Dense.Ndarray.S.transpose ~axis:[|0;2;3;1|] x in | |
| let classes = 10 in | |
| let y' = Dense.Matrix.S.zeros m classes in | |
| for i = 0 to m - 1 do | |
| Dense.Matrix.S.set y' i (int_of_float y.{i,0}) 1. | |
| done; | |
| let nn = model () in | |
| let params = Params.config | |
| ~batch:(Batch.Mini 32) ~learning_rate:(Learning_Rate.RMSprop (0.0001, 1e-6)) ~checkpoint:0.1 0.2 in | |
| Graph.train_cnn ~params nn x y' (* |> ignore; | |
| let a, b = Owl_neural_optimise.Utils.draw_samples (Arr x) (Mat y) 10 in | |
| test_cnn nn (Algodiff.S.unpack_arr a) (Algodiff.S.unpack_mat b) *) | |
| let _ = | |
| train_cifar10_keras_graph () |