public static void Run(int epochs = 5) { // requires Internet connection (dynamic train, dynamic test) = tf.keras.datasets.fashion_mnist.load_data(); // will be able to do (trainImages, trainLabels) = train; ndarray trainImages = np.expand_dims(train.Item1 / 255.0f, axis: 3); ndarray trainLabels = train.Item2; ndarray testImages = np.expand_dims(test.Item1 / 255.0f, axis: 3); ndarray testLabels = test.Item2; bool loaded = 60000 == trainImages.Length; Debug.Assert(loaded); var model = new Sequential(new Layer[] { // will be able to do: new Flatten(kwargs: new { input_shape = (28, 28) }), new ResNetBlock(kernelSize: 3, filters: new [] { 1, 2, 3 }), new ResNetBlock(kernelSize: 3, filters: new [] { 1, 2, 3 }), new Flatten(), new Dense(units: 10, activation: tf.nn.softmax_fn), }); model.compile( optimizer: new AdamOptimizer(), loss: "sparse_categorical_crossentropy", metrics: new dynamic[] { "accuracy" }); model.fit(trainImages, trainLabels, epochs: epochs); var testEvalResult = model.evaluate(testImages, testLabels); double testAcc = testEvalResult[1]; Console.WriteLine($"Test accuracy: {testAcc}"); model.summary(); }
public void Embedding() { var model = new Sequential(); model.add(new Embedding(1000, 64, input_length: 10)); // the model will take as input an integer matrix of size (batch, // input_length). // the largest integer (i.e. word index) in the input should be no larger // than 999 (vocabulary size). // now model.output_shape == (None, 10, 64), where None is the batch // dimension. var input_array = np.random.randint(1000, size: (32, 10)); model.compile("rmsprop", "mse"); }
public void Test1() { var epochs = 200; var batchSize = 128; var classes = 10; var hiddenCount = 128; var validationSplit = 0.2; var flatImageSize = 28 * 28; var mnist = MnistDataset.Read(@"C:\Projects\heightmap_upscale\data\mnist"); var x_train = PrepareImage(mnist.TrainingImages); var x_test = PrepareImage(mnist.TestImages); var y_train = PrepareLabels(mnist.TrainingLabels); var model = new Sequential(); model.add(new Dense(classes, activation: new softmax())); //model.Compile("SGD", loss: "categorical_crossentropy", metrics: new[] { "accuracy" }); model.compile("SGD", "categorical_crossentropy"); }
public Model CanLearn() { // requires Internet connection (dynamic train, dynamic test) = tf.keras.datasets.fashion_mnist.load_data(); ndarray trainImage = NormalizeChannelValue(train.Item1)[0]; trainImage = (ndarray)np.expand_dims(trainImage, axis: 2).reshape(new [] { 28 * 28, 1 }); var coords = Coord(28, 28).ToNumPyArray().reshape(new [] { 28 * 28, 2 }); var model = new Sequential(new object[] { new Siren(2, Enumerable.Repeat(128, 3).ToArray()), new Dense(units: 1, activation: tf.keras.activations.relu_fn), }); model.compile( optimizer: new Adam(), loss: "mse"); model.fit(coords, targetValues: trainImage, epochs: 2, batchSize: 28 * 28, stepsPerEpoch: 1024); double testLoss = model.evaluate(coords, trainImage); return(model); }
static void Main(string[] args) { GradientEngine.UseEnvironmentFromVariable(); TensorFlowSetup.Instance.EnsureInitialized(); // this allows SIREN to oversaturate channels without adding to the loss var clampToValidChannelRange = PythonFunctionContainer.Of <Tensor, Tensor>(ClampToValidChannelValueRange); var siren = new Sequential(new object[] { new GaussianNoise(stddev: 1f / (128 * 1024)), new Siren(2, Enumerable.Repeat(256, 5).ToArray()), new Dense(units: 4, activation: clampToValidChannelRange), new GaussianNoise(stddev: 1f / 128), }); siren.compile( // too slow to converge //optimizer: new SGD(momentum: 0.5), // lowered learning rate to avoid destabilization optimizer: new Adam(learning_rate: 0.00032), loss: "mse"); if (args.Length == 0) { siren.load_weights("sample.weights"); Render(siren, 1034 * 3, 1536 * 3, "sample6X.png"); return; } foreach (string imagePath in args) { using var original = new Bitmap(imagePath); byte[,,] image = ToBytesHWC(original); int height = image.GetLength(0); int width = image.GetLength(1); int channels = image.GetLength(2); Debug.Assert(channels == 4); var imageSamples = PrepareImage(image); var coords = ImageTools.Coord(height, width).ToNumPyArray() .reshape(new[] { width *height, 2 }); var upscaleCoords = ImageTools.Coord(height * 2, width * 2).ToNumPyArray(); var improved = ImprovedCallback.Create((sender, eventArgs) => { if (eventArgs.Epoch < 10) { return; } ndarray <float> upscaled = siren.predict( upscaleCoords.reshape(new[] { height *width * 4, 2 }), batch_size: 1024); upscaled = (ndarray <float>)upscaled.reshape(new[] { height * 2, width * 2, channels }); using var bitmap = ToImage(RestoreImage(upscaled)); bitmap.Save("sample4X.png", ImageFormat.Png); siren.save_weights("sample.weights"); Console.WriteLine(); Console.WriteLine("saved!"); }); siren.fit(coords, imageSamples, epochs: 100, batchSize: 16 * 1024, shuffleMode: TrainingShuffleMode.Epoch, callbacks: new ICallback[] { improved }); } }
public Task TrainAsync(MLPointCollection points) { return(Task.Factory.StartNew(() => { _modelDispatcher.Invoke(() => { Log.Info("Training"); var xydata = _dataGenerator.GetPointsXYData(points); var x = np.array(xydata.x.ToArray()); var y = np.array(xydata.y.ToArray()); var count = (int)(xydata.x.Count / (decimal)xydata.dataItemsPerX); x = x.reshape(count, xydata.dataItemsPerX); y = y.reshape(count, 3); //Tensorflow.InvalidArgumentError: 'In[0] mismatch In[1] shape: 28 vs. 1120: [5,28] [1120,60] 0 0' /*_model = keras.Sequential( * new List<ILayer> * { * new Flatten(new FlattenArgs * { * InputShape = new TensorShape(xydata.dataItemsPerX) * }), * //keras.layers.Flatten(), * keras.layers.Dense(xydata.dataItemsPerX, activation: "relu"),//, input_shape: new TensorShape(-1, xydata.dataItemsPerX)), * keras.layers.Dense(60, activation: "relu"), * keras.layers.Dense(40, activation: "relu"), * keras.layers.Dense(3, activation: "softmax"), * }); * * _model.compile(keras.optimizers.SGD(0.01F), keras.losses.CategoricalCrossentropy(from_logits: true), */ var numberOfClasses = 3; _model = keras.Sequential( new List <ILayer> { new Flatten(new FlattenArgs { InputShape = new TensorShape(xydata.dataItemsPerX) }), //keras.layers.Flatten(), keras.layers.Dense(xydata.dataItemsPerX, activation: "relu"), //, input_shape: new TensorShape(-1, xydata.dataItemsPerX)), keras.layers.Dropout(0.2F), keras.layers.Dense(12, activation: "relu"), keras.layers.Dropout(0.2F), keras.layers.Dense(6, activation: "relu"), keras.layers.Dense(numberOfClasses, activation: "softmax"), }); //var loss = new SGD(0.05F); //var optimiser = new SparseCategoricalCrossentropy(); //model.compile(loss, optimiser, new[] { "accuracy" }); //model.compile(new SGD(0.1F), new SparseCategoricalCrossentropy(), new[] { "accuracy" }); // logits and labels must have the same first dimension, got logits shape [5,3] and labels shape [15]' _model.compile( keras.optimizers.Adam(0.01F), keras.losses.CategoricalCrossentropy(), new[] { "acc" }); //here // SparseCategoricalCrossentropy? Validation set? More generated data? _model.fit(x, y, 5, 100, 1, validation_split: 0.1F); Log.Info("Training complete"); }); }));//, TaskCreationOptions.LongRunning); }