Sha256: 820c256da3b43a0a5d730e536d1877902a0fd9ba532223269265da38d9b34f23
Contents?: true
Size: 1.51 KB
Versions: 1
Compression:
Stored size: 1.51 KB
Contents
# This example converts a CIFAR10 grayscale image to a color image. require "dnn" require "dnn/datasets/cifar10" require "numo/linalg/autoloader" require_relative "dcgan" include DNN::Optimizers include DNN::Losses def load_dataset x, y = DNN::CIFAR10.load_train x_out = Numo::SFloat.cast(x) x_in = x_out.mean(axis: 3, keepdims: true) x_in = (x_in / 127.5) - 1 x_out = (x_out / 127.5) - 1 [x_in, x_out] end epochs = 20 batch_size = 128 gen = Generator.new([32, 32, 1]) dis = Discriminator.new([32, 32, 1], [32, 32, 3]) dcgan = DCGAN.new(gen, dis) dis.setup(Adam.new(alpha: 0.00001, beta1: 0.1), SigmoidCrossEntropy.new) dcgan.setup(Adam.new(alpha: 0.0002, beta1: 0.5), SigmoidCrossEntropy.new) x_in, x_out = load_dataset iter1 = DNN::Iterator.new(x_in, x_out) iter2 = DNN::Iterator.new(x_in, x_out) num_batchs = x_in.shape[0] / batch_size (1..epochs).each do |epoch| num_batchs.times do |index| x_in, x_out = iter1.next_batch(batch_size) images = gen.predict(x_in) y_real = Numo::SFloat.ones(batch_size, 1) y_fake = Numo::SFloat.zeros(batch_size, 1) dis_loss = dis.train_on_batch([x_in, x_out], y_real) dis_loss += dis.train_on_batch([x_in, images], y_fake) x_in, x_out = iter2.next_batch(batch_size) dcgan_loss = dcgan.train_on_batch(x_in, y_real) puts "epoch: #{epoch}, index: #{index}, dis_loss: #{dis_loss}, dcgan_loss: #{dcgan_loss}" end iter1.reset iter2.reset dcgan.save("trained/dcgan_model_epoch#{epoch}.marshal") end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
ruby-dnn-0.16.0 | examples/pix2pix/train.rb |