Skip to content

Instantly share code, notes, and snippets.

@thistleknot
Created December 2, 2023 16:37
Show Gist options
  • Save thistleknot/841e627fe88c52594d03604a01dd1b47 to your computer and use it in GitHub Desktop.
Save thistleknot/841e627fe88c52594d03604a01dd1b47 to your computer and use it in GitHub Desktop.
dataset distillation extracted from kaggle
#https://www.kaggle.com/code/samuelcortinhas/mnist-dataset-distillation
# Core
import numpy as np
np.random.seed(0)
import pandas as pd
import seaborn as sns
sns.set(style='darkgrid', font_scale=1.4)
import matplotlib.pyplot as plt
%matplotlib inline
import time
# Sklearn
from sklearn.model_selection import train_test_split, StratifiedKFold
from sklearn.metrics import accuracy_score
# Tensorflow
import tensorflow as tf
tf.random.set_seed(0)
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras import callbacks
from keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.layers.experimental import preprocessing
from keras.utils.vis_utils import plot_model
# Load data
mnist_data=pd.read_csv('../input/digit-recognizer/train.csv')
# Print shape + head
print('Training dataframe dimensions:',mnist_data.shape)
print(mnist_data.head())
# Labels
y=mnist_data.label
# Scale features to be in [0,1]
X=mnist_data.drop('label', axis=1)/255
# Reshape (-1 means unspecified)
X = X.values.reshape(-1, 28, 28, 1)
# One-hot encode target
y=pd.get_dummies(y)
# Create a validation set
X_train, X_valid, y_train, y_valid = train_test_split(X, y, train_size=0.8, test_size=0.2, stratify=y, random_state=0)
# Parameters
BATCH_SIZE = 250
EPOCHS = 50
# Define model
def build_model():
model = keras.Sequential([
layers.Conv2D(filters=64, kernel_size=5, strides=1, padding='same', input_shape=[28,28,1], activation='relu'),
layers.MaxPool2D(pool_size=2, padding='same'),
layers.Dropout(rate=0.4),
layers.Conv2D(filters=128, kernel_size=3, strides=1, padding='same', activation='relu'),
layers.MaxPool2D(pool_size=4, padding='same'),
layers.Dropout(rate=0.4),
layers.Flatten(),
layers.Dense(units=256, activation='relu'),
layers.Dropout(rate=0.4),
layers.Dense(units=10, activation='softmax')
])
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['categorical_accuracy'])
return model
# Define model
model = build_model()
# Measure training time
start = time.time()
# Train model
history = model.fit(X_train, y_train, validation_data=(X_valid, y_valid), batch_size=BATCH_SIZE, epochs=EPOCHS, verbose=True)
stop = time.time()
# Convert to dataframe
history_df = pd.DataFrame(history.history)
# Predictions
preds = np.argmax(model.predict(X_valid), axis=1)
# Confidence
conf = np.max(model.predict(X_valid), axis=1)
# Final accuracy and time
score1 = accuracy_score(np.argmax(y_valid.values, axis=1), preds)
time1 = np.round(stop-start,1)
print(f'Final accuracy on validation set:{np.round(100*score1,1)}%')
print(f'Training time: {time1} secs')
# Summary of results
ssize = [33600, 10, 10, 500, 500]
distillation = ['No', 'No', 'Yes', 'No', 'Yes']
scores = np.round(100*np.array([score1, score2, score3, score4, score5]),1)
times = [time1, time2, time3, time4, time5]
# Dataframe
results = pd.DataFrame({'Support size': ssize, 'Distillation': distillation, 'Training time (s)': times, 'Accuracy (%)': scores})
print(results)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment