def __init__(self, seq_len, set='train', dataset='mnist', rng=None, infinite=True, digits=None): if dataset == 'fashion_mnist': (x_train, y_train), (x_test, y_test) = utils.load_fashion_mnist() elif dataset == 'mnist': (x_train, y_train), (x_test, y_test) = utils.load_mnist() else: raise ValueError('wrong dataset name') self.x_train = x_train self.y_train = y_train self.y_train2idxs = {} for i in range(10): self.y_train2idxs[i] = np.where(self.y_train == i)[0] if set == 'train': self.x = x_train self.y = y_train else: self.x = x_test self.y = y_test self.input_dim = self.x.shape[-1] self.img_shape = (int(np.sqrt(self.input_dim)), int(np.sqrt(self.input_dim)), 1) self.x = np.reshape(self.x, (self.x.shape[0], ) + self.img_shape) self.x = np.float32(self.x) self.x_train = np.reshape(self.x_train, (self.x_train.shape[0], ) + self.img_shape) self.x_train = np.float32(self.x_train) self.y2idxs = {} for i in range(10): self.y2idxs[i] = np.where(self.y == i)[0] self.seq_len = seq_len self.rng = np.random.RandomState(42) if not rng else rng self.nsamples = self.x.shape[0] self.infinite = infinite self.digits = digits if digits is not None else range(10) self.n_classes = len(self.digits) self.batch_size = self.n_classes self.set = set print(set, 'dataset size:', self.x.shape) print(set, 'N classes', self.n_classes) print(set, 'min, max', np.min(self.x), np.max(self.x)) print(set, 'nsamples', self.nsamples) print('--------------')
def main(): ''' Predict the model defined above. ''' dataset_name = 'cifar_10' model_name = 'MobileNet' image_size = 128 channels = 3 batch_size = 1 if (dataset_name == 'mnist'): (x_train, y_train), (x_val, y_val), (x_test, y_test) = load_mnist(image_size, channels) elif (dataset_name == 'cifar_10'): (x_train, y_train), (x_val, y_val), (x_test, y_test) = load_cifar10( image_size, channels) elif (dataset_name == 'cifar_100'): (x_train, y_train), (x_val, y_val), (x_test, y_test) = load_cifar100( image_size, channels) elif (dataset_name == 'fashion_mnist'): (x_train, y_train), (x_val, y_val), (x_test, y_test) = load_fashion_mnist( image_size, channels) model = load_model(f'{dataset_name}_{model_name}') optimizer = optimizers.SGD(lr=0.001, momentum=0.9, decay=0.0001, nesterov=False) model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy']) start = time.time() predict = model.predict(x_val[:32], verbose=1, batch_size=batch_size) end = time.time() print(str(end - start))
self.output_layer.b -= self.output_layer.db * learning_rate # =============== EDIT HERE =============== hidden_1 = 15 #None hidden_2 = 10 #None num_epochs = 500 #None learning_rate = 36e-5 #None print_every = 20 #None # ========================================= batch_size = 100 train_acc = [] test_acc = [] x_train, y_train, x_test, y_test = load_fashion_mnist('./data') num_feature = x_train.shape[1] model = MLP(input_size=num_feature, hidden_size_1=hidden_1, hidden_size_2=hidden_2, output_size=10) num_data = len(x_train) num_batch = int(np.ceil(num_data / batch_size)) for i in range(1, num_epochs + 1): epoch_loss = 0.0 for b in range(0, len(x_train), batch_size): x_batch = x_train[b: b + batch_size] y_batch = y_train[b: b + batch_size] loss = model.loss(x_batch, y_batch) epoch_loss += loss
def main(): ''' Train the model defined above. ''' dataset_model = 'mnist' image_size = 128 channels = 1 n_classes = 10 epochs = 3 batch_size = 32 layers_conv, layers_hidden = 16, 2 if (dataset_model == 'mnist'): (x_train, y_train), (x_val, y_val), (x_test, y_test) = load_mnist(image_size, channels) elif (dataset_model == 'cifar_10'): (x_train, y_train), (x_val, y_val), (x_test, y_test) = load_cifar10( image_size, channels) elif (dataset_model == 'cifar_100'): (x_train, y_train), (x_val, y_val), (x_test, y_test) = load_cifar100( image_size, channels) elif (dataset_model == 'fashion_mnist'): (x_train, y_train), (x_val, y_val), (x_test, y_test) = load_fashion_mnist( image_size, channels) print('x_train shape:', x_train.shape) print('x_val shape:', x_val.shape) print('x_test shape:', x_test.shape) print('y_train shape:', y_train.shape) print('y_val shape:', y_val.shape) print('y_test shape:', y_test.shape) print('Loaded Images ') #x_train, y_train = generate_aug(x_train, y_train) #train_generator = get_train_generator() # mobile_model = MobileNet('MobileNet', image_size, channels, n_classes) mobile_model = VGG16('VGG16', image_size, channels, n_classes) mobile_model.summary() mobile_model.compile() mobile_model.fit((x_train, y_train), (x_val, y_val), epochs, batch_size) ''' loss_and_metrics = mobile_model.evaluate((x_train, y_train), batch_size = batch_size) print('\n\n[Evaluation on the train dataset]\n', loss_and_metrics, '\n\n') mobile_model.predict(x_train, y_train, batch_size = batch_size) loss_and_metrics = mobile_model.evaluate((x_val, y_val), batch_size = batch_size) print('[Evaluation on the vel dataset]\n', loss_and_metrics, '\n\n') mobile_model.predict(x_val, y_val, batch_size = batch_size) loss_and_metrics = mobile_model.evaluate((x_test, y_test), batch_size = batch_size) print('[Evaluation on the test dataset]\n', loss_and_metrics, '\n\n') mobile_model.predict(x_test, y_test, batch_size = batch_size) ''' mobile_model.save_model(dataset_model + "_teste") import numpy as np predictions_valid = mobile_model.get_model().predict(x_test, batch_size=batch_size, verbose=1) np.set_printoptions(precision=4) # np.set_printoptions(suppress=True) with open('files/Preds.txt', 'w+') as f: for i in predictions_valid: for j in i: f.write('%.4f ' % j) f.write("\n") with open('files/ConvLayersW.txt', 'a+') as file: file.write('%d\n' % (layers_conv)) with open('files/HiddenLayersW.txt', 'a+') as file: file.write('%d\n' % (layers_hidden)) with open('files/conf.txt', 'a+') as conf: id_layer = 8 list_mark = [0] * len(mobile_model.get_model().layers) layers = mobile_model.get_model().layers[id_layer] busca_em_profun(mobile_model.get_model(), layers, len(mobile_model.get_model().layers), conf, list_mark, id_layer) conf.write('flatten 1 %s\n' % layers.name) layers = mobile_model.get_model().layers[10] with open('files/HiddenLayersW.txt', 'a+') as f: print_hidden_soft_layer_W(f, layers, conf) with open('files/HiddenLayersB.txt', 'a+') as f: print_hidden_soft_layer_b(f, layers) layers = mobile_model.get_model().layers[11] with open('files/SmrLayerW.txt', 'a+') as f: print_hidden_soft_layer_W(f, layers, conf) with open('files/SmrLayerB.txt', 'a+') as f: print_hidden_soft_layer_b(f, layers) conf.write('endconf 1 %s\n' % layers.name)
import utils import config import numpy as np import cupy as cp print "Loading CIFAR10" if config.fargs[ 'dataset'] == "cifar10" else "Loading Fashion MNIST" X_train, X_test = utils.load_cifar10( "data" ) if config.fargs['dataset'] == "cifar10" else utils.load_fashion_mnist("data") X_train = (X_train / 255.0).astype(np.float32) X_test = (X_test / 255.0).astype(np.float32) mean = X_train.mean(axis=(0, 2, 3)) std = X_train.std(axis=(0, 2, 3)) X_train = (X_train - mean[:, None, None]) / std[:, None, None] X_test = (X_test - mean[:, None, None]) / std[:, None, None] X_train_flip = np.flip(X_train, 3) X_test_flip = np.flip(X_test, 3) X_train = cp.asarray(X_train).reshape(X_train.shape[0], X_train.shape[1], config.pixel * config.pixel) X_test = cp.asarray(X_test).reshape(X_test.shape[0], X_test.shape[1], config.pixel * config.pixel) X_train_flip = cp.asarray(X_train_flip).reshape(X_train_flip.shape[0], X_train_flip.shape[1], config.pixel * config.pixel) X_test_flip = cp.asarray(X_test_flip).reshape(X_test_flip.shape[0], X_test_flip.shape[1], config.pixel * config.pixel) def featurize(st, ed, train, flip=False):
from mxnet import init, gluon from mxnet.gluon import loss as gloss, nn import utils batch_size = 256 num_inputs = 784 num_outputs = 10 num_hiddens = 256 num_epochs = 100 dropout_prob1 = 0.2 dropout_prob2 = 0.5 learning_rate = 0.1 train_iter, test_iter = utils.load_fashion_mnist(batch_size) net = nn.Sequential() net.add(nn.Dense(num_hiddens, activation='relu')) net.add(nn.Dropout(dropout_prob1)) net.add(nn.Dense(num_hiddens, activation='relu')) net.add(nn.Dropout(dropout_prob2)) net.add(nn.Dense(num_outputs)) net.initialize(init.Normal(sigma=0.01)) loss = gloss.SoftmaxCrossEntropyLoss() trainer = gluon.Trainer(net.collect_params(), 'sgd', {'learning_rate': learning_rate}) utils.train_mnist(net=net, train_iter=train_iter, test_iter=test_iter, loss=loss, num_epochs=num_epochs, batch_size=batch_size, trainer=trainer)
layers_conv, layers_hidden = 3, 1 if (dataset_model == 'mnist'): (x_train, y_train), (x_val, y_val), (x_test, y_test) = load_mnist(image_size, channels) elif (dataset_model == 'cifar_10'): (x_train, y_train), (x_val, y_val), (x_test, y_test) = load_cifar10(image_size, channels) elif (dataset_model == 'cifar_100'): (x_train, y_train), (x_val, y_val), (x_test, y_test) = load_cifar100(image_size, channels) elif (dataset_model == 'fashion_mnist'): (x_train, y_train), (x_val, y_val), (x_test, y_test) = load_fashion_mnist( image_size, channels) #print('x_train shape:', x_train.shape) #print('x_val shape:', x_val.shape) #print('x_test shape:', x_test.shape) #print('y_train shape:', y_train.shape) # print('y_val shape:', y_val.shape) # print('y_test shape:', y_test.shape) # print('Loaded Images ', type(x_train[0][0][0][0])) #x_train, y_train = generate_aug(x_train, y_train) #train_generator = get_train_generator() json_file = open('files/cifar_10_MobileNet.json', 'r') loaded_model_json = json_file.read()
def main(): ''' Train the model defined above. ''' dataset_name = 'cifar_10' model_name = 'MobileNet' image_size = 224 channels = 3 n_classes = 10 epochs = 40 batch_size = 32 layers_conv, layers_hidden = 14, 1 if (dataset_name == 'mnist'): (x_train, y_train), (x_val, y_val), (x_test, y_test) = load_mnist(image_size, channels) elif (dataset_name == 'cifar_10'): (x_train, y_train), (x_val, y_val), (x_test, y_test) = load_cifar10( image_size, channels) elif (dataset_name == 'cifar_100'): (x_train, y_train), (x_val, y_val), (x_test, y_test) = load_cifar100( image_size, channels) elif (dataset_name == 'fashion_mnist'): (x_train, y_train), (x_val, y_val), (x_test, y_test) = load_fashion_mnist( image_size, channels) print('x_train shape:', x_train.shape) print('x_val shape:', x_val.shape) print('x_test shape:', x_test.shape) print('y_train shape:', y_train.shape) print('y_val shape:', y_val.shape) print('y_test shape:', y_test.shape) print('Loaded Images ') #x_train, y_train = generate_aug(x_train, y_train) #train_generator = get_train_generator() #mobile_model = AlexNet(f"{dataset_name}_{model_name}", image_size, channels, n_classes) mobile_model = MobileNet(f"{dataset_name}_{model_name}", image_size, channels, n_classes) #mobile_model = VGG16(f"{dataset_name}_{model_name}", image_size, channels, n_classes) #mobile_model = Inception(f"{dataset_name}_{model_name}", image_size, channels, n_classes) mobile_model.summary() mobile_model.compile() mobile_model.fit((x_train, y_train), (x_val, y_val), epochs, batch_size) ''' loss_and_metrics = mobile_model.evaluate((x_train, y_train), batch_size = batch_size) print('\n\n[Evaluation on the train dataset]\n', loss_and_metrics, '\n\n') mobile_model.predict(x_train, y_train, batch_size = batch_size) loss_and_metrics = mobile_model.evaluate((x_val, y_val), batch_size = batch_size) print('[Evaluation on the vel dataset]\n', loss_and_metrics, '\n\n') mobile_model.predict(x_val, y_val, batch_size = batch_size) loss_and_metrics = mobile_model.evaluate((x_test, y_test), batch_size = batch_size) print('[Evaluation on the test dataset]\n', loss_and_metrics, '\n\n') mobile_model.predict(x_test, y_test, batch_size = batch_size) ''' mobile_model.save_model(f"{dataset_name}_{model_name}") mobile_model.get_model().load_weights( f"files/{dataset_name}_{model_name}.h5") mobile_model.compile() import numpy as np predictions_valid = mobile_model.get_model().predict(x_test, batch_size=batch_size, verbose=1) np.set_printoptions(precision=4) # np.set_printoptions(suppress=True) with open('files/Preds.txt', 'w+') as f: for i in predictions_valid: for j in i: f.write('%.4f ' % j) f.write("\n") with open('files/ConvLayersW.txt', 'a+') as file: file.write('%d\n' % (layers_conv)) with open('files/HiddenLayersW.txt', 'a+') as file: file.write('%d\n' % (layers_hidden)) c, h, s = 95, 97, 98 #AlexNet 8, 10, 11 #Mobilenet 95, 97, 98 #Inception 87, 93, 96 #VGG16 31, 33, 34 with open('files/conf.txt', 'a+') as conf: id_layer = c list_mark = [0] * len(mobile_model.get_model().layers) layers = mobile_model.get_model().layers[id_layer] busca_em_profun(mobile_model.get_model(), layers, len(mobile_model.get_model().layers), conf, list_mark, id_layer) conf.write('flatten 1 %s\n' % layers.name) layers = mobile_model.get_model().layers[h] with open('files/HiddenLayersW.txt', 'a+') as f: print_hidden_soft_layer_W(f, layers, conf) #layers = mobile_model.get_model().layers[35] #print_hidden_soft_layer_W(f, layers, conf) layers = mobile_model.get_model().layers[h] with open('files/HiddenLayersB.txt', 'a+') as f: print_hidden_soft_layer_b(f, layers) #layers = mobile_model.get_model().layers[35] #print_hidden_soft_layer_b(f, layers) layers = mobile_model.get_model().layers[s] with open('files/SmrLayerW.txt', 'a+') as f: print_hidden_soft_layer_W(f, layers, conf) with open('files/SmrLayerB.txt', 'a+') as f: print_hidden_soft_layer_b(f, layers) conf.write('endconf 1 %s\n' % layers.name)
def __init__(self, seq_len, batch_size, dataset='mnist', set='train', rng=None, infinite=True, digits=None): if dataset == 'fashion_mnist': (x_train, y_train), (x_test, y_test) = utils.load_fashion_mnist() if set == 'train': self.x = x_train self.y = y_train else: self.x = x_test self.y = y_test elif dataset == 'mnist': (x_train, y_train), (x_test, y_test) = utils.load_mnist() if set == 'train': self.x = x_train self.y = y_train elif set == 'test': self.x = x_test self.y = y_test elif dataset == 'cifar10': self.x, self.y = utils.load_cifar('data/cifar', subset=set) self.x = np.transpose(self.x, (0, 2, 3, 1)) # (N,3,32,32) -> (N,32,32,3) self.x = np.float32(self.x) self.img_shape = self.x.shape[1:] self.input_dim = np.prod(self.img_shape) else: raise ValueError('wrong dataset name') if dataset == 'mnist' or dataset == 'fashion_mnist': self.input_dim = self.x.shape[-1] self.img_shape = (int(np.sqrt(self.input_dim)), int(np.sqrt(self.input_dim)), 1) self.x = np.reshape(self.x, (self.x.shape[0], ) + self.img_shape) self.x = np.float32(self.x) self.classes = np.unique(self.y) self.n_classes = len(self.classes) self.y2idxs = {} self.nsamples = 0 for i in list(self.classes): self.y2idxs[i] = np.where(self.y == i)[0] self.nsamples += len(self.y2idxs[i]) self.batch_size = batch_size self.seq_len = seq_len self.rng = np.random.RandomState(42) if not rng else rng self.infinite = infinite self.digits = digits if digits is not None else np.arange( self.n_classes) print(set, 'dataset size:', self.x.shape) print(set, 'N classes', self.n_classes) print(set, 'min, max', np.min(self.x), np.max(self.x)) print(set, 'nsamples', self.nsamples) print(set, 'digits', self.digits) print('--------------')
import torch from torch import nn import torch.nn.functional as F import numpy as np from utils import load_fashion_mnist from model.torch_model import torch_MLP import time np.random.seed(19941017) x_train, y_train, x_val, y_val = load_fashion_mnist('./data') num_feature = x_train.shape[1] total_num = len(x_train) output_size = y_train.shape[1] print( f"feature number : {num_feature} | data_number : {total_num} | class : {output_size}" ) y_train = np.argmax(y_train, axis=1) y_val = np.argmax(y_val, axis=1) x_train = torch.Tensor(x_train) y_train = torch.Tensor(y_train) x_val = torch.Tensor(x_val) y_val = torch.Tensor(y_val).to(torch.long) epochs = 1500 batch_size = 50 learning_rate = 0.001 hidden = [50, 30, 20, 10]