Example #1
0
 def _load_cifar100(self):
     (self.x_train, self.y_train), (self.x_test, self.y_test) =\
                                     load_cifar100(to_categoric = False)
     self.x_train = np.transpose(x_train.astype('f'), (0, 3, 1, 2))
     self.y_train = y_train.flatten().astype('i')
     self.x_test = np.transpose(x_test.astype('f'), (0, 3, 1, 2))
     self.y_test = y_test.flatten().astype('i')
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))
Example #3
0
 def _load_cifar100(self):
     (self.x_train, self.y_train), (self.x_test,
                                    self.y_test) = load_cifar100()
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)
epochs = 300
batch_size = 32
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()
Example #6
0
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)
Example #7
0
def main():
    # Training settings
    parser = argparse.ArgumentParser(description='Embedding extraction module')
    parser.add_argument('--net',
                        default='lenet5',
                        help='DNN name (default=lenet5)')
    parser.add_argument('--root',
                        default='data',
                        help='rootpath (default=data)')
    parser.add_argument('--dataset',
                        default='imagenet',
                        help='dataset (default=imagenet)')
    parser.add_argument('--tensor_folder',
                        default='tensor_pub',
                        help='tensor_folder (default=tensor_pub)')
    parser.add_argument('--layer-info',
                        default='layer_info',
                        help='layer-info (default=layer_info)')
    parser.add_argument('--gpu-id',
                        default='1',
                        type=str,
                        help='id(s) for CUDA_VISIBLE_DEVICES')
    parser.add_argument('-j',
                        '--workers',
                        default=8,
                        type=int,
                        metavar='N',
                        help='number of data loading workers (default: 8)')
    parser.add_argument('-b',
                        '--batch-size',
                        default=1,
                        type=int,
                        metavar='N',
                        help='should be 1')
    args = parser.parse_args()
    use_cuda = True
    # Define what device we are using
    print("CUDA Available: ", torch.cuda.is_available())

    root = args.root
    dataset = args.dataset
    net = args.net
    tensor_folder = args.tensor_folder
    layers, cols = utils.get_layer_info(root, dataset, net, args.layer_info)
    print(dataset)
    print(root, dataset, net)

    os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu_id

    if dataset.startswith('imagenet'):
        if net == 'resnet50':
            model = utils.load_resnet50_model(True)
        elif net == 'vgg16':
            model = utils.load_vgg_model(pretrained=True, net=net)
        else:
            model = utils.load_resnet_model(pretrained=True)

        sub_models = utils.load_imagenet_sub_models(
            utils.get_model_root(root, dataset, net), layers, net, cols)
        # sub_models = utils.load_resnet_sub_models(utils.get_model_root(root,
        # dataset, net), layers, net)
        test_loader = utils.load_imagenet_test(args.batch_size, args.workers)
        anatomy(model, sub_models, test_loader, root, dataset, tensor_folder,
                net, layers)

    else:  # cifar10, cifar100, mnist
        device = torch.device("cuda" if (
            use_cuda and torch.cuda.is_available()) else "cpu")
        nclass = 10
        if dataset == 'cifar100':
            nclass = 100
        model = utils.load_model(
            net, device, utils.get_pretrained_model(root, dataset, net),
            dataset)
        weight_models = utils.load_weight_models(
            net, device, utils.get_model_root(root, dataset, net), layers,
            cols, nclass)
        if dataset == 'mnist':
            train_loader, test_loader = utils.load_mnist(
                utils.get_root(root, dataset, 'data', net))
        elif dataset == 'cifar10':
            train_loader, test_loader = utils.load_cifar10(
                utils.get_root(root, dataset, 'data', net))
        elif dataset == 'cifar100':
            train_loader, test_loader = utils.load_cifar100(
                utils.get_root(root, dataset, 'data', net))
        else:  #default mnist
            train_loader, test_loader = utils.load_mnist(
                utils.get_root(root, dataset, 'data', net))
        anatomy(model, weight_models, test_loader, root, dataset,
                tensor_folder, net, layers)
Example #8
0
import numpy as np
import cupy as cp
import chainer.functions as F
import utils
import config

X_train, X_test = utils.load_cifar10(
    "data") if config.fargs['dataset'] == "cifar10" else utils.load_cifar100(
        "data")


class CoatesNgNet:
    def __init__(self):
        self.bias = config.fargs['bias']
        all_patches, idxs = utils.grab_patches(
            X_train,
            patch_size=config.fargs['patch_size'],
            max_threads=16,
            tot_patches=100000)
        all_patches = utils.normalize_patches(all_patches, zca_bias=1e-3)
        filters = all_patches[np.random.choice(all_patches.shape[0],
                                               config.fargs['num_filters'],
                                               replace=False)].astype(
                                                   np.float32)
        self.filters = cp.asarray(filters)
        if config.flip:
            self.filters = cp.concatenate(
                (self.filters, cp.flip(self.filters, 3)), axis=0)

    def forward(self, x):
        n = x.shape[0]