Skip to content
Snippets Groups Projects
Commit dd87be88 authored by Finn Bear's avatar Finn Bear
Browse files

Remove old files.

parent 38565545
No related branches found
No related tags found
No related merge requests found
ai/
gan/
\ No newline at end of file
#!/usr/bin/env bash
mkdir -p /tmp/rust-autograd/data/mnist
curl http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz --output "/tmp/rust-autograd/data/mnist/train-images-idx3-ubyte.gz"
curl http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz --output "/tmp/rust-autograd/data/mnist/train-labels-idx1-ubyte.gz"
curl http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz --output "/tmp/rust-autograd/data/mnist/t10k-images-idx3-ubyte.gz"
curl http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz --output "/tmp/rust-autograd/data/mnist/t10k-labels-idx1-ubyte.gz"
gzip -d /tmp/rust-autograd/data/mnist/*.gz
\ No newline at end of file
use autograd::ndarray as ndarray;
use std::fs::File;
use std::io;
use std::io::Read;
use std::path::Path;
type NdArray = ndarray::Array<f32, ndarray::IxDyn>;
/// load mnist dataset as "ndarray" objects.
///
/// Returns ((x_train, y_train), (x_test, y_test)).
///
/// Shape of x_train and x_test: (num_samples, 28)
/// Shape of y_train and y_test: (num_samples, 1)
pub fn load() -> ((NdArray, NdArray), (NdArray, NdArray)) {
// load dataset as `Vec`s
let (train_x, num_image_train): (Vec<f32>, usize) =
load_images("/tmp/rust-autograd/data/mnist/train-images-idx3-ubyte");
let (train_y, num_label_train): (Vec<f32>, usize) =
load_labels("/tmp/rust-autograd/data/mnist/train-labels-idx1-ubyte");
let (test_x, num_image_test): (Vec<f32>, usize) =
load_images("/tmp/rust-autograd/data/mnist/t10k-images-idx3-ubyte");
let (test_y, num_label_test): (Vec<f32>, usize) =
load_labels("/tmp/rust-autograd/data/mnist/t10k-labels-idx1-ubyte");
// Vec to ndarray
let as_arr = NdArray::from_shape_vec;
let x_train = as_arr(ndarray::IxDyn(&[num_image_train, 28 * 28]), train_x).unwrap();
let y_train = as_arr(ndarray::IxDyn(&[num_label_train, 1]), train_y).unwrap();
let x_test = as_arr(ndarray::IxDyn(&[num_image_test, 28 * 28]), test_x).unwrap();
let y_test = as_arr(ndarray::IxDyn(&[num_label_test, 1]), test_y).unwrap();
((x_train, y_train), (x_test, y_test))
}
fn load_images<P: AsRef<Path>>(path: P) -> (Vec<f32>, usize) {
let ref mut buf_reader =
io::BufReader::new(File::open(path).expect("Please run ./download_mnist.sh beforehand"));
let magic = read_be_u32(buf_reader);
if magic != 2051 {
panic!("Invalid magic number. expected 2051, got {}", magic)
}
let num_image = read_be_u32(buf_reader) as usize;
let rows = read_be_u32(buf_reader) as usize;
let cols = read_be_u32(buf_reader) as usize;
assert!(rows == 28 && cols == 28);
// read images
let mut buf: Vec<u8> = vec![0u8; num_image * rows * cols];
let _ = buf_reader.read_exact(buf.as_mut());
let ret = buf.into_iter().map(|x| (x as f32) / 255.).collect();
(ret, num_image)
}
fn load_labels<P: AsRef<Path>>(path: P) -> (Vec<f32>, usize) {
let ref mut buf_reader = io::BufReader::new(File::open(path).unwrap());
let magic = read_be_u32(buf_reader);
if magic != 2049 {
panic!("Invalid magic number. Got expect 2049, got {}", magic);
}
let num_label = read_be_u32(buf_reader) as usize;
// read labels
let mut buf: Vec<u8> = vec![0u8; num_label];
let _ = buf_reader.read_exact(buf.as_mut());
let ret: Vec<f32> = buf.into_iter().map(|x| x as f32).collect();
(ret, num_label)
}
// mnist is stored as big endian
fn read_be_u32<T: Read>(reader: &mut T) -> u32 {
let mut buf: [u8; 4] = [0, 0, 0, 0];
let _ = reader.read_exact(&mut buf);
u32::from_be_bytes(buf)
}
#[allow(dead_code)]
fn main() {}
\ No newline at end of file
from __future__ import print_function, division
from keras.datasets import mnist
from keras.layers import Input, Dense, Reshape, Flatten, Dropout
from keras.layers import BatchNormalization, Activation, ZeroPadding2D
from keras.layers.advanced_activations import LeakyReLU
from keras.layers.convolutional import UpSampling2D, Conv2D
from keras.models import Sequential, Model
from tensorflow.keras.optimizers import Adam
import matplotlib.pyplot as plt
import sys
import numpy as np
class GAN():
def __init__(self):
self.img_rows = 28
self.img_cols = 28
self.channels = 1
self.img_shape = (self.img_rows, self.img_cols, self.channels)
self.latent_dim = 100
optimizer = Adam(0.0002, 0.5)
# Build and compile the discriminator
self.discriminator = self.build_discriminator()
self.discriminator.compile(loss='binary_crossentropy',
optimizer=optimizer,
metrics=['accuracy'])
# Build the generator
self.generator = self.build_generator()
# The generator takes noise as input and generates imgs
z = Input(shape=(self.latent_dim,))
img = self.generator(z)
# For the combined model we will only train the generator
self.discriminator.trainable = False
# The discriminator takes generated images as input and determines validity
validity = self.discriminator(img)
# The combined model (stacked generator and discriminator)
# Trains the generator to fool the discriminator
self.combined = Model(z, validity)
self.combined.compile(loss='binary_crossentropy', optimizer=optimizer)
def build_generator(self):
model = Sequential()
model.add(Conv2D(2, 3, strides=1))
model.add(Dense(256, input_dim=self.latent_dim))
model.add(LeakyReLU(alpha=0.2))
model.add(BatchNormalization(momentum=0.8))
model.add(Dense(512))
model.add(LeakyReLU(alpha=0.2))
model.add(BatchNormalization(momentum=0.8))
model.add(Dense(1024))
model.add(LeakyReLU(alpha=0.2))
model.add(BatchNormalization(momentum=0.8))
model.add(Dense(np.prod(self.img_shape), activation='tanh'))
model.add(Reshape(self.img_shape))
model.summary()
noise = Input(shape=(self.latent_dim,))
img = model(noise)
return Model(noise, img)
def build_discriminator(self):
model = Sequential()
model.add(Flatten(input_shape=self.img_shape))
model.add(Dense(512))
model.add(LeakyReLU(alpha=0.2))
model.add(Dense(256))
model.add(LeakyReLU(alpha=0.2))
model.add(Dense(1, activation='sigmoid'))
model.summary()
img = Input(shape=self.img_shape)
validity = model(img)
return Model(img, validity)
def train(self, epochs, batch_size=128, sample_interval=50):
# Load the dataset
(X_train, _), (_, _) = mnist.load_data()
# Rescale -1 to 1
X_train = X_train / 127.5 - 1.
X_train = np.expand_dims(X_train, axis=3)
# Adversarial ground truths
valid = np.ones((batch_size, 1))
fake = np.zeros((batch_size, 1))
for epoch in range(epochs):
# ---------------------
# Train Discriminator
# ---------------------
# Select a random batch of images
idx = np.random.randint(0, X_train.shape[0], batch_size)
imgs = X_train[idx]
noise = np.random.normal(0, 1, (batch_size, self.latent_dim))
# Generate a batch of new images
gen_imgs = self.generator.predict(noise)
# Train the discriminator
d_loss_real = self.discriminator.train_on_batch(imgs, valid)
d_loss_fake = self.discriminator.train_on_batch(gen_imgs, fake)
d_loss = 0.5 * np.add(d_loss_real, d_loss_fake)
# ---------------------
# Train Generator
# ---------------------
noise = np.random.normal(0, 1, (batch_size, self.latent_dim))
# Train the generator (to have the discriminator label samples as valid)
g_loss = self.combined.train_on_batch(noise, valid)
# Plot the progress
print ("%d [D loss: %f, acc.: %.2f%%] [G loss: %f]" % (epoch, d_loss[0], 100*d_loss[1], g_loss))
# If at save interval => save generated image samples
if epoch % sample_interval == 0:
self.sample_images(epoch)
def sample_images(self, epoch):
r, c = 5, 5
noise = np.random.normal(0, 1, (r * c, self.latent_dim))
gen_imgs = self.generator.predict(noise)
# Rescale images 0 - 1
gen_imgs = 0.5 * gen_imgs + 0.5
fig, axs = plt.subplots(r, c)
cnt = 0
for i in range(r):
for j in range(c):
axs[i,j].imshow(gen_imgs[cnt, :,:,0], cmap='gray')
axs[i,j].axis('off')
cnt += 1
fig.savefig("images/%d.png" % epoch)
plt.close()
if __name__ == '__main__':
gan = GAN()
gan.train(epochs=30000, batch_size=32, sample_interval=200)
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment