diff --git a/dataset.py b/dataset.py new file mode 100644 index 0000000..61af42b --- /dev/null +++ b/dataset.py @@ -0,0 +1,58 @@ +import gzip + +import cPickle as pickle +import numpy as np + +def load_mnist32x32(): + # dataset='I:\Data\PhD Life\Tutorial\Python\data\mnist.pkl.gz' # linux + # dataset='/u/students/gif/Desktop/PhD/Tutorial/dataset/MNIST/mnist32x32.pkl.gz' # linux + dataset = '/local/scratch/gif/dataset/MNIST/mnist32x32.pkl.gz' #the-villa + # dataset = 'I:\Data\PhD Life\Tutorial\dataset\MNIST\mnist32x32.pkl.gz' # laptop + f = gzip.open(dataset,'rb') + train_set, valid_set, test_set = pickle.load(f) + f.close() + + test_set_x, test_set_y = test_set + valid_set_x, valid_set_y = valid_set + train_set_x, train_set_y = train_set + + + train_set_x = train_set_x.reshape(train_set_x.shape[0], 1, 32, 32) + valid_set_x = valid_set_x.reshape(valid_set_x.shape[0], 1, 32, 32) + test_set_x = test_set_x.reshape(test_set_x.shape[0], 1, 32, 32) + + return (train_set_x.astype('float32'), train_set_y.astype('uint8')), (valid_set_x.astype('float32'), valid_set_y.astype('uint8')), (test_set_x.astype('float32'), test_set_y.astype('uint8')) + +def load_usps(): + # dataset = 'I:\Data\PhD Life\Tutorial\dataset\USPS\usps.pkl.gz' #windows + dataset='/u/students/gif/Desktop/PhD/Tutorial/dataset/USPS/usps.pkl.gz' # linux + f = gzip.open(dataset,'rb') + train_set, test_set = pickle.load(f) + f.close() + + train_set_x, train_set_y = train_set + test_set_x, test_set_y = test_set + + + + train_set_x = train_set_x.reshape(train_set_x.shape[0], 1, 28, 28).astype('float32') + test_set_x = test_set_x.reshape(test_set_x.shape[0], 1, 28, 28).astype('float32') + + rval = (train_set_x, train_set_y), (test_set_x, test_set_y) + return rval + +def load_svhn(): + # dataset = '/u/students/gif/Desktop/PhD/Tutorial/dataset/SVHN/svhn_gray.pkl.gz' #linux + dataset = '/local/scratch/gif/dataset/SVHN/svhn_gray.pkl.gz' #the-villa + # dataset = 'I:\Data\PhD Life\Tutorial\dataset\SVHN\svhn_gray.pkl.gz' # laptop + f = gzip.open(dataset,'rb') + (X_train, y_train), (X_test, y_test) = pickle.load(f) + f.close() + + idx10 = np.where(y_train == 10) + y_train[idx10] = 0 + + idx10 = np.where(y_test == 10) + y_test[idx10] = 0 + + return (X_train.astype('float32'), y_train.astype('uint8')), (X_test.astype('float32'), y_test.astype('uint8')) \ No newline at end of file diff --git a/drcn.py b/drcn.py new file mode 100644 index 0000000..e2b4413 --- /dev/null +++ b/drcn.py @@ -0,0 +1,410 @@ +from keras.models import Model +from keras.layers import Input, Flatten +from keras.layers.convolutional import Convolution2D, MaxPooling2D, AveragePooling2D, Deconvolution2D, UpSampling2D +from keras.layers.core import Activation, Dropout, Dense, Reshape +from keras.layers.normalization import BatchNormalization +from keras.optimizers import Adam, RMSprop + +import os +import numpy as np +import time + +from myutils import * + +def iterate_minibatches(inputs, targets, batchsize, shuffle=False): + assert len(inputs) == len(targets) + + if shuffle: + indices = np.arange(len(inputs)) + np.random.shuffle(indices) + + for start_idx in range(0, len(inputs), batchsize): + end_idx = start_idx + batchsize + if end_idx > len(inputs): + end_idx = start_idx + (len(inputs) % batchsize) + + if shuffle: + excerpt = indices[start_idx:end_idx] + + else: + excerpt = slice(start_idx, end_idx) + + yield inputs[excerpt], targets[excerpt] + +def accuracy(Y1, Y2): + n = Y1.shape[0] + ntrue = np.count_nonzero(np.argmax(Y1, axis=1) == np.argmax(Y2, axis=1)) + return ntrue * 1.0 / n + +def save_weights(model, PARAMDIR, CONF): + # model: keras model + print(' == save weights == ') + + # save weights + PARAMPATH = os.path.join(PARAMDIR, '%s_weights.h5') % CONF + model.save(PARAMPATH) + + # save architecture + CONFPATH = os.path.join(PARAMDIR, '%s_conf.json') % CONF + archjson = model.to_json() + + open(CONFPATH, 'wb').write(archjson) + +class DRCN(object): + def __init__(self, name='svhn-mnist'): + self.name = name + + def create_model(self, input_shape=(1, 32, 32), dy=10, nb_filters=[64, 128], kernel_size=(3, 3), pool_size=(2, 2), + dropout=0.5, output_activation='softmax'): + + _input = Input(shape=input_shape) + + _h = _input + for nf in nb_filters: + _h = Convolution2D(nf, kernel_size[0], kernel_size[1], border_mode='same', subsample=(2, 2))(_h) + # _h = Convolution2D(nf, kernel_size[0], kernel_size[1], border_mode='same')(_h) + _h = BatchNormalization()(_h) + _h = Activation('relu')(_h) + _h = Dropout(dropout)(_h) + + # _h = AveragePooling2D(pool_size=pool_size)(_h) + # _h = MaxPooling2D(pool_size=pool_size)(_h) + + + _h = Flatten()(_h) + + + _h = Dense(1000)(_h) + _h = BatchNormalization()(_h) + _feat = Activation('relu')(_h) + + _h = Dropout(dropout)(_feat) + + + _y = Dense(dy, activation=output_activation)(_feat) + + # convnet + self.convnet_model = Model(input=_input, output=_y) + self.convnet_model.compile(loss='categorical_crossentropy', optimizer=Adam(lr=3e-4)) + + # shared features + self.feat_model = Model(input=_input, output=_feat) + + print(self.convnet_model.summary()) + + # conv autoencoder + dim = 4 + _xdec = Dense(3200, activation='relu')(_feat) + _xdec = Reshape((nb_filters[-1], dim, dim))(_xdec) + for nf in reversed(nb_filters): + dim *= 2 + _xdec = Deconvolution2D(nf, kernel_size[0], kernel_size[1], (None, nf, dim, dim), subsample=(2, 2), border_mode='same')(_xdec) + # _xdec = UpSampling2D()(_xdec) + + # _xdec = Convolution2D(nf, kernel_size[0], kernel_size[1], border_mode='same')(_xdec) + + _xdec = BatchNormalization()(_xdec) + _xdec = Activation('relu')(_xdec) + # _xdec = Dropout(dropout)(_xdec) + + + + # _xdec = Deconvolution2D(1, kernel_size[0], kernel_size[1], (None, 1, 32, 32), subsample=(2, 2), border_mode='same', activation='tanh')(_xdec) + _xdec = Convolution2D(1, kernel_size[0], kernel_size[1], border_mode='same', activation='sigmoid')(_xdec) + + self.convae_model = Model(input=_input, output=_xdec) + self.convae_model.compile(loss='mse', optimizer=Adam(lr=3e-4)) + + print(self.convae_model.summary()) + + + def fit_convnet(self, X, Y, nb_epoch=50, batch_size=128, shuffle=True, + validation_data=None, test_data=None, PARAMDIR=None, CONF=None): + + + + history = {} + history['losses'] = [] + history['accs'] = [] + history['val_losses'] = [] + history['val_accs'] = [] + history['test_losses'] = [] + history['test_accs'] = [] + history['elapsed_times'] = [] + + best_ep = 1 + for e in range(nb_epoch): + loss = 0. + n_batch = 0 + start_t = time.time() + for X_batch, Y_batch in iterate_minibatches(X, Y, batch_size, shuffle=shuffle): + + l = self.convnet_model.train_on_batch(X_batch, Y_batch) + loss += l + n_batch += 1 + + elapsed_t = time.time() - start_t + history['elapsed_times'].append(elapsed_t) + + loss /= n_batch + history['losses'].append(loss) + + # calculate accuracy + acc = accuracy(self.convnet_model.predict(X), Y) + history['accs'].append(acc) + + + val_loss = -1 + val_acc = -1 + best_val_acc = -1 + if validation_data is not None: + (X_val, Y_val) = validation_data + val_loss = 0. + n_batch = 0 + for Xv, Yv in iterate_minibatches(X_val, Y_val ,batch_size, shuffle=False): + l = self.convnet_model.test_on_batch(Xv, Yv) + val_loss += l + n_batch += 1 + val_loss /= n_batch + history['val_losses'].append(val_loss) + + val_acc = accuracy(self.convnet_model.predict(X_val), Y_val) + history['val_accs'].append(val_acc) + + test_loss = -1 + test_acc = -1 + if test_data is not None: + (X_test, Y_test) = test_data + test_loss = 0. + n_batch = 0 + for Xt, Yt in iterate_minibatches(X_test, Y_test, batch_size, shuffle=False): + l = self.convnet_model.test_on_batch(Xt, Yt) + test_loss += l + n_batch += 1 + + test_loss /= n_batch + history['test_losses'].append(test_loss) + + test_acc = accuracy(self.convnet_model.predict(X_test), Y_test) + history['test_accs'].append(test_acc) + + + + print('Epoch-%d: (loss: %.3f, acc: %.3f), (val_loss: %.3f, val_acc: %.3f), (test_Loss: %.3f, test_acc: %.3f) -- %.2f sec' % \ + ((e+1), loss, acc, val_loss, val_acc, test_loss, test_acc, elapsed_t)) + + if PARAMDIR is not None: + if (acc + val_acc) > best_val_acc: + best_val_acc = (acc + val_acc) + best_ep = e + 1 + save_weights(self.convnet_model, PARAMDIR, CONF) + else: + print('do not save, best val_acc: %.3f at %d' % (best_val_acc, best_ep)) + + + # store history + HISTPATH = '%s_hist.npy' % CONF + np.save(HISTPATH, history) + + + + def fit_convae(self, X, nb_epoch=50, batch_size=128, shuffle=True, + validation_data=None, test_data=None, PARAMDIR=None, CONF=None): + + + history = {} + history['losses'] = [] + history['val_losses'] = [] + history['test_losses'] = [] + history['elapsed_times'] = [] + + best_ep = 1 + for e in range(nb_epoch): + loss = 0. + n_batch = 0 + start_t = time.time() + for X_batch, Y_batch in iterate_minibatches(X, np.copy(X), batch_size, shuffle=shuffle): + + l = self.convae_model.train_on_batch(X_batch, Y_batch) + loss += l + n_batch += 1 + + elapsed_t = time.time() - start_t + history['elapsed_times'].append(elapsed_t) + + loss /= n_batch + history['losses'].append(loss) + + + + val_loss = -1 + best_val_loss = 100000 + + test_loss = -1 + + + print('Epoch-%d: (loss: %.3f), (val_loss: %.3f), (test_Loss: %.3f) -- %.2f sec' % \ + ((e+1), loss, val_loss,test_loss,elapsed_t)) + + if PARAMDIR is not None: + if loss < best_val_loss: + best_val_loss = loss + best_ep = e + 1 + save_weights(self.convae_model, PARAMDIR, CONF) + else: + print('do not save, best val loss: %.3f at %d' % (best_val_loss, best_ep)) + + + # store history + HISTPATH = '%s_hist.npy' % CONF + np.save(HISTPATH, history) + + # visualization + if validation_data is not None: + Xt = validation_data + Xt = postprocess_images(Xt, omin=-1, omax=1) + imgfile = '%s_tgt.png' % CONF + show_images(Xt, filename=imgfile) + + Xt_pred = self.convae_model.predict(Xt) + Xt_pred = postprocess_images(Xt_pred, omin=-1, omax=1) + imgfile = '%s_tgt_pred.png' % CONF + show_images(Xt_pred, filename=imgfile) + + if test_data is not None: + Xs = test_data + Xs = postprocess_images(Xs, omin=-1, omax=1) + imgfile = '%s_src.png' % CONF + show_images(Xs, filename=imgfile) + + Xs_pred = self.convae_model.predict(Xs) + Xs_pred = postprocess_images(Xs_pred, omin=-1, omax=1) + imgfile = '%s_src_pred.png' % CONF + show_images(Xs_pred, filename=imgfile) + + def fit_drcn(self, X, Y, Xu, nb_epoch=50, batch_size=128, shuffle=True, + validation_data=None, test_data=None, PARAMDIR=None, CONF=None): + history = {} + history['losses'] = [] + history['accs'] = [] + history['gen_losses'] = [] + history['val_losses'] = [] + history['val_accs'] = [] + history['test_losses'] = [] + history['test_accs'] = [] + history['elapsed_times'] = [] + + best_ep = 1 + for e in range(nb_epoch): + start_t = time.time() + # convae training + gen_loss = 0. + n_batch = 0 + for Xu_batch, Yu_batch in iterate_minibatches(Xu, np.copy(Xu), batch_size, shuffle=shuffle): + l = self.convae_model.train_on_batch(Xu_batch, Yu_batch) + gen_loss += l + n_batch += 1 + + gen_loss /= n_batch + history['gen_losses'].append(gen_loss) + + # convnet training + loss = 0. + n_batch = 0 + + for X_batch, Y_batch in iterate_minibatches(X, Y, batch_size, shuffle=shuffle): + + l = self.convnet_model.train_on_batch(X_batch, Y_batch) + loss += l + n_batch += 1 + + loss /= n_batch + history['losses'].append(loss) + + # calculate accuracy + acc = accuracy(self.convnet_model.predict(X), Y) + history['accs'].append(acc) + + + elapsed_t = time.time() - start_t + history['elapsed_times'].append(elapsed_t) + + + val_loss = -1 + val_acc = -1 + best_val_acc = -1 + if validation_data is not None: + (X_val, Y_val) = validation_data + val_loss = 0. + n_batch = 0 + for Xv, Yv in iterate_minibatches(X_val, Y_val ,batch_size, shuffle=False): + l = self.convnet_model.test_on_batch(Xv, Yv) + val_loss += l + n_batch += 1 + val_loss /= n_batch + history['val_losses'].append(val_loss) + + val_acc = accuracy(self.convnet_model.predict(X_val), Y_val) + history['val_accs'].append(val_acc) + + test_loss = -1 + test_acc = -1 + if test_data is not None: + (X_test, Y_test) = test_data + test_loss = 0. + n_batch = 0 + for Xt, Yt in iterate_minibatches(X_test, Y_test, batch_size, shuffle=False): + l = self.convnet_model.test_on_batch(Xt, Yt) + test_loss += l + n_batch += 1 + + test_loss /= n_batch + history['test_losses'].append(test_loss) + + test_acc = accuracy(self.convnet_model.predict(X_test), Y_test) + history['test_accs'].append(test_acc) + + + + print('Epoch-%d: (loss: %.3f, acc: %.3f, gen_loss: %.3f), (val_loss: %.3f, val_acc: %.3f), (test_Loss: %.3f, test_acc: %.3f) -- %.2f sec' % \ + ((e+1), loss, acc, gen_loss, val_loss, val_acc, test_loss, test_acc, elapsed_t)) + + if PARAMDIR is not None: + if (acc + val_acc) > best_val_acc: + best_val_acc = (acc + val_acc) + best_ep = e + 1 + save_weights(self.convnet_model, PARAMDIR, CONF) + else: + print('do not save, best val_acc: %.3f at %d' % (best_val_acc, best_ep)) + + + # store history + HISTPATH = '%s_hist.npy' % CONF + np.save(HISTPATH, history) + + # visualization + if validation_data is not None: + (X_val, Y_val) = validation_data + Xs = X_val[:100] + + Xs = postprocess_images(Xs, omin=0, omax=1) + imgfile = '%s_src.png' % CONF + show_images(Xs, filename=imgfile) + + Xs_pred = self.convae_model.predict(Xs) + Xs_pred = postprocess_images(Xs_pred, omin=0, omax=1) + imgfile = '%s_src_pred.png' % CONF + show_images(Xs_pred, filename=imgfile) + + if test_data is not None: + (X_test, Y_test) = test_data + Xt = X_test[:100] + Xt = postprocess_images(Xt, omin=0, omax=1) + imgfile = '%s_tgt.png' % CONF + show_images(Xt, filename=imgfile) + + Xt_pred = self.convae_model.predict(Xt) + Xt_pred = postprocess_images(Xt_pred, omin=0, omax=1) + imgfile = '%s_tgt_pred.png' % CONF + show_images(Xt_pred, filename=imgfile) + diff --git a/main_sm.py b/main_sm.py new file mode 100644 index 0000000..678a69c --- /dev/null +++ b/main_sm.py @@ -0,0 +1,81 @@ +import numpy as np +import gzip +import cPickle as pickle + +from keras.utils import np_utils + +from drcn import * +from myutils import * + + +def load_svhn(dataset = '/local/scratch/gif/dataset/SVHN/svhn_gray.pkl.gz'): + f = gzip.open(dataset,'rb') + (X_train, y_train), (X_test, y_test) = pickle.load(f) + f.close() + + idx10 = np.where(y_train == 10) + y_train[idx10] = 0 + + idx10 = np.where(y_test == 10) + y_test[idx10] = 0 + + return (X_train.astype('float32'), y_train.astype('uint8')), (X_test.astype('float32'), y_test.astype('uint8')) + +def load_mnist32x32(dataset = '/local/scratch/gif/dataset/MNIST/mnist32x32.pkl.gz'): + f = gzip.open(dataset,'rb') + train_set, valid_set, test_set = pickle.load(f) + f.close() + + test_set_x, test_set_y = test_set + valid_set_x, valid_set_y = valid_set + train_set_x, train_set_y = train_set + + + train_set_x = train_set_x.reshape(train_set_x.shape[0], 1, 32, 32) + valid_set_x = valid_set_x.reshape(valid_set_x.shape[0], 1, 32, 32) + test_set_x = test_set_x.reshape(test_set_x.shape[0], 1, 32, 32) + + return (train_set_x.astype('float32'), train_set_y.astype('uint8')), (valid_set_x.astype('float32'), valid_set_y.astype('uint8')), (test_set_x.astype('float32'), test_set_y.astype('uint8')) + + +# Load datasets +print('Load datasets') +(Xr_train, y_train), (Xr_test, y_test) = load_svhn() # source +(_, _), (_, _), (Xr_tgt_test, y_tgt_test) = load_mnist32x32() # target + +# Convert class vectors to binary class matrices +nb_classes = 10 +Y_train = np_utils.to_categorical(y_train, nb_classes) +Y_test = np_utils.to_categorical(y_test, nb_classes) +Y_tgt_test = np_utils.to_categorical(y_tgt_test, nb_classes) + +# Preprocess input images +X_train = preprocess_images(Xr_train, tmin=0, tmax=1) +X_test = preprocess_images(Xr_test, tmin=0, tmax=1) +X_tgt_test = preprocess_images(Xr_tgt_test, tmin=0, tmax=1) + +drcn = DRCN() +input_shape = (X_train.shape[1], X_train.shape[2], X_train.shape[3]) + +print('Create Model') +drcn.create_model(input_shape=input_shape, dy=nb_classes, nb_filters=[100, 150, 200], kernel_size=(5, 5), pool_size=(2, 2), + dropout=0.5, output_activation='softmax') + +# print('Train...') +# PARAMDIR = '' +# CONF = 'svhn-mnist_convnet' +# drcn.fit_convnet(X_train, Y_train, +# validation_data=(X_test, Y_test), +# test_data=(X_tgt_test, Y_tgt_test), +# PARAMDIR=PARAMDIR, CONF=CONF +# ) + +print('Train convae...') +PARAMDIR = '' +CONF = 'svhn-mnist_drcn' +# drcn.fit_convae(X_tgt_test, validation_data=X_tgt_test[:100], test_data=X_train[:100], PARAMDIR=PARAMDIR, CONF=CONF) +drcn.fit_drcn(X_train, Y_train, X_tgt_test, validation_data=(X_test, Y_test), + test_data=(X_tgt_test, Y_tgt_test), + PARAMDIR=PARAMDIR, CONF=CONF +) + diff --git a/myutils.py b/myutils.py index 807ad76..d7b63b5 100644 --- a/myutils.py +++ b/myutils.py @@ -1,209 +1,48 @@ +from PIL import Image, ImageDraw import numpy as np -import random -from sklearn import preprocessing -from keras.utils import np_utils +def preprocess_images(X, tmin=-1, tmax=1): + V = X * (tmax - tmin) / 255. + V += tmin + return V + +def postprocess_images(V, omin=-1, omax=1): + X = V - omin + X = X * 255. / (omax - omin) + return X + +def show_images(Xo, padsize=1, padval=0, filename=None, title=None): + X = np.copy(Xo) + [n, c, d1, d2] = X.shape + if c== 1: + X = np.reshape(X, (n, d1, d2)) + + n = int(np.ceil(np.sqrt(X.shape[0]))) + + padding = ((0, n ** 2 - X.shape[0]), (0, padsize), (0, padsize)) + ((0, 0), ) * (X.ndim - 3) + canvas = np.pad(X, padding, mode='constant', constant_values=(padval, padval)) + + canvas = canvas.reshape((n, n) + canvas.shape[1:]).transpose((0, 2, 1, 3) + tuple(range(4, canvas.ndim + 1))) + canvas = canvas.reshape((n * canvas.shape[1], n * canvas.shape[3]) + canvas.shape[4:]) + + if title is not None: + title_canv = np.zeros((50, canvas.shape[1])) + title_canv = title_canv.astype('uint8') + canvas = np.vstack((title_canv, canvas)).astype('uint8') + + I = Image.fromarray(canvas) + d = ImageDraw.Draw(I) + fill = 255 + d.text((10, 10), title, fill=fill, font=fnt) + else: + canvas = canvas.astype('uint8') + I = Image.fromarray(canvas) + + if filename is None: + I.show() + else: + I.save(filename) + + return I -def get_subsample(X, y, nc, C=10): - # nc : number of samples per classes - G_list = [] - L_list = [] - for c in range(0,C): - inds_c = np.where(y == c) - - inds_c = inds_c[0] - - # inds_c = np.random.permutation(inds_c) - G = X[inds_c] - L = y[inds_c] - - G = G[0:nc] - L = L[0:nc] - - G_list.append(G) - L_list.append(L) - - - - X_sub = G_list[0] - y_sub = L_list[0] - for c in range(1,C): - X_sub = np.concatenate((X_sub, G_list[c]), axis=0) - y_sub = np.concatenate((y_sub, L_list[c]), axis=0) - - return X_sub, y_sub - -# # model1 => model2 -def copy_weights(model1, model2, l=1): - ignored_layers = ['Activation','Dropout','MaxPooling2D','Unpooling2D'] - inds1 = [] - for l in range(0,len(model1.layers)): - lname = model1.layers[l].get_config()["name"] - if lname not in ignored_layers: - # print(l,':',model1.layers[l].get_config()["name"]) - inds1.append(l) - # print('+'*10) - inds2 = [] - for l in range(0,len(model2.layers)): - lname = model2.layers[l].get_config()["name"] - if lname not in ignored_layers: - # print(l,':',model2.layers[l].get_config()["name"]) - inds2.append(l) - - n1 = len(inds1) - n2 = len(inds2) - - nmin = np.minimum(n1,n2) - - for i in range(0,nmin-1): - l1 = inds1[i] - l2 = inds2[i] - # print(l1,l2) - - if l < 1: - W2 = model2.layers[l2].get_weights() - model2.layers[l2].set_weights(W2 + l*model1.layers[l1].get_weights()) - else: - model2.layers[l2].set_weights(model1.layers[l1].get_weights()) - -def copy_weights_convae(model1, model2): - ignored_layers = ['Activation','Dropout','MaxPooling2D','Unpooling2D'] - inds1 = [] - for l in range(0,len(model1.layers)): - lname = model1.layers[l].get_config()["name"] - if lname not in ignored_layers: - # print(l,':',model1.layers[l].get_config()["name"]) - inds1.append(l) - # print('+'*10) - inds2 = [] - for l in range(0,len(model2.layers)): - lname = model2.layers[l].get_config()["name"] - if lname not in ignored_layers: - # print(l,':',model2.layers[l].get_config()["name"]) - inds2.append(l) - - n1 = len(inds1) - n2 = len(inds2) - - # nmin = np.minimum(n1,n2) - nmin = int(n1/2) - # print(nmin) - - for i in range(0,nmin): - l1 = inds1[i] - l2 = inds2[i] - # print(l1,l2) - model2.layers[l2].set_weights(model1.layers[l1].get_weights()) - -def get_corrupted_output(X, corruption_level=0.3): - return np.random.binomial(1, 1-corruption_level, X.shape) * X - -def get_gaussian_noise(X, sd=0.5): - # Injecting small gaussian noise - X += np.random.normal(0, sd, X.shape) - return X - -# Create semi-supervised sets of labeled and unlabeled data -# where there are equal number of labels from each class -# 'x': MNIST images -# 'y': MNIST labels (binarized / 1-of-K coded) -def create_semisupervised(X, y, n_labeled, num_classes): - - - [n, c, dim1, dim2] = X.shape - Xr = X.reshape((n, c*dim1*dim2)) - - # split by class - def split_by_class(x, y, num_classes): - result_x = [0]*num_classes - result_y = [0]*num_classes - - Y = np_utils.to_categorical(y, num_classes) - for i in range(num_classes): - idx_i = np.where(y == i)[0] - result_x[i] = x[idx_i,:] - result_y[i] = Y[idx_i,:] - return result_x, result_y - - - x_list, y_list = split_by_class(Xr, y, num_classes) - - n_classes = y_list[0].shape[1] - - - if n_labeled%n_classes != 0: - raise("n_labeled (wished number of labeled samples) not divisible by n_classes (number of classes)") - - n_labels_per_class = n_labeled/n_classes - x_labeled = [0]*n_classes - x_unlabeled = [0]*n_classes - y_labeled = [0]*n_classes - y_unlabeled = [0]*n_classes - - for i in range(n_classes): - idx = range(x_list[i].shape[0]) - random.shuffle(idx) - x_labeled[i] = x_list[i][idx[:n_labels_per_class],:] - y_labeled[i] = y_list[i][idx[:n_labels_per_class],:] - x_unlabeled[i] = x_list[i][idx[n_labels_per_class:],:] - y_unlabeled[i] = y_list[i][idx[n_labels_per_class:],:] - - X_l = np.vstack(x_labeled) - X_l = X_l.reshape((X_l.shape[0], c, dim1, dim2)) - Y_l = np.vstack(y_labeled) - - X_u = np.vstack(x_unlabeled) - X_u = X_u.reshape((X_u.shape[0], c, dim1, dim2)) - Y_u = np.vstack(y_unlabeled) - - return X_l, Y_l, X_u, Y_u - -def remove_mean(X, scaler=None): - [n,c,dim1,dim2] = X.shape - Z = X.reshape(n,c*dim1*dim2) - if scaler is None: - scaler = preprocessing.StandardScaler(with_mean=True, with_std=False).fit(Z) - - Z = scaler.transform(Z) - Z = Z.reshape(n,c,dim1,dim2) - - return Z, scaler - -def remove_mean_and_std(X, scaler=None): - [n,c,dim1,dim2] = X.shape - Z = X.reshape(n,c*dim1*dim2) - if scaler is None: - scaler = preprocessing.StandardScaler(with_mean=True, with_std=True).fit(Z) - - Z = scaler.transform(Z) - Z = Z.reshape(n,c,dim1,dim2) - - return Z, scaler - -def min_max(X, feature_range=(0,1)): - scaler = preprocessing.MinMaxScaler(feature_range=feature_range) - [n,c,dim1,dim2] = X.shape - - Z = X.reshape(n,c*dim1*dim2) - Z = scaler.fit_transform(Z) - Z = Z.reshape(n,c,dim1,dim2) - return Z - - -def iterate_minibatches(inputs, targets, batchsize, shuffle=False): - assert len(inputs) == len(targets) - if shuffle: - indices = np.arange(len(inputs)) - np.random.shuffle(indices) - for start_idx in range(0, len(inputs) - batchsize + 1, batchsize): - if shuffle: - excerpt = indices[start_idx:start_idx + batchsize] - else: - excerpt = slice(start_idx, start_idx + batchsize) - yield inputs[excerpt], targets[excerpt] - - - - -