コード例 #1
0
def open_sesemi():
    args = parse_args()
    network = args.network
    dataset = args.dataset
    nb_labels = args.nb_labels
    os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu_id

    arg2var = {
        'convnet': convnet,
        'wrn': wrn,
        'nin': nin,
        'svhn': svhn,
        'cifar10': cifar10,
        'cifar100': cifar100,
    }

    # Experiment- and dataset-dependent parameters.
    zca = True
    hflip = True
    epochs = 50
    if dataset in {'svhn', 'cifar10'}:
        if dataset == 'svhn':
            zca = False
            hflip = False
            epochs = 30
        nb_classes = 10
    elif dataset == 'cifar100':
        nb_classes = 100
    else:
        raise ValueError('`dataset` must be "svhn", "cifar10", "cifar100".')
    super_dropout = 0.2
    in_network_dropout = 0.0
    if network == 'convnet' and dataset == 'svhn':
        super_dropout = 0.5
        in_network_dropout = 0.5
    elif network == 'wrn' and dataset == 'svhn':
        super_dropout = 0.5

    # Prepare the dataset.
    (x_train, y_train), (x_test, y_test) = arg2var[dataset].load_data()

    x_test = global_contrast_normalize(x_test)
    x_train = global_contrast_normalize(x_train)

    if zca:
        zca_whiten = zca_whitener(x_train)
        x_train = zca_whiten(x_train)
        x_test = zca_whiten(x_test)

    x_test = x_test.reshape((len(x_test), 32, 32, 3))
    x_train = x_train.reshape((len(x_train), 32, 32, 3))

    if nb_labels in {50000, 73257}:
        x_labeled = x_train
        y_labeled = y_train
    else:
        labels_per_class = nb_labels // nb_classes
        sample_inds = stratified_sample(y_train, labels_per_class)
        x_labeled = x_train[sample_inds]
        y_labeled = y_train[sample_inds]

    y_labeled = to_categorical(y_labeled)

    # Shared training parameters.
    base_lr = 0.05
    batch_size = 16
    lr_decay_power = 0.5
    input_shape = (32, 32, 3)
    max_iter = (len(x_train) // batch_size) * epochs

    # Compile the SESEMI model.
    sesemi_model, inference_model = compile_sesemi(arg2var[network],
                                                   input_shape, nb_classes,
                                                   base_lr, in_network_dropout,
                                                   super_dropout)
    print(sesemi_model.summary())

    lr_poly_decay = LRScheduler(base_lr, max_iter, lr_decay_power)
    evaluate = DenseEvaluator(inference_model, (x_test, y_test),
                              hflip,
                              oversample=True)

    super_datagen = ImageDataGenerator(
        width_shift_range=[-2, -1, 0, 1, 2],
        height_shift_range=[-2, -1, 0, 1, 2],
        horizontal_flip=hflip,
        preprocessing_function=gaussian_noise,
        fill_mode='reflect',
    )
    self_datagen = ImageDataGenerator(
        width_shift_range=[-2, -1, 0, 1, 2],
        height_shift_range=[-2, -1, 0, 1, 2],
        horizontal_flip=False,
        preprocessing_function=gaussian_noise,
        fill_mode='reflect',
    )

    super_data = super_datagen.flow(x_labeled,
                                    y_labeled,
                                    shuffle=True,
                                    batch_size=1,
                                    seed=None)
    self_data = self_datagen.flow(x_train,
                                  shuffle=True,
                                  batch_size=1,
                                  seed=None)
    train_data_loader = datagen(super_data, self_data, batch_size)

    # Fit the SESEMI model on mini-batches with data augmentation.
    print('Run configuration:')
    print('network=%s,' % network, 'dataset=%s,' % dataset, \
          'horizontal_flip=%s,' % hflip, 'ZCA=%s,' % zca, \
          'nb_epochs=%d,' % epochs, 'batch_size=%d,' % batch_size, \
          'nb_labels=%d,' % len(y_labeled), 'gpu_id=%s' % args.gpu_id)
    sesemi_model.fit_generator(
        train_data_loader,
        epochs=epochs,
        verbose=1,
        steps_per_epoch=len(x_train) // batch_size,
        callbacks=[lr_poly_decay, evaluate],
    )
コード例 #2
0
def open_sesemi():
    args = parse_args()
    network = args.network
    nb_extra = args.nb_extra
    os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu_id

    arg2var = {
        'convnet': convnet,
        'wrn': wrn,
    }

    # Load Tiny Images.
    # Code adapted from https://github.com/smlaine2/tempens
    with open('./datasets/tiny-images/tiny_index.pkl', 'rb') as f:
        tinyimg_index = pickle.load(f, encoding='latin1')

    if nb_extra == 237203:
        print("Using all classes common with CIFAR-100.")
        with open('./datasets/cifar-100/meta', 'rb') as f:
            cifar_labels = pickle.load(f,
                                       encoding='latin1')['fine_label_names']
        cifar_to_tinyimg = {'maple_tree': 'maple', 'aquarium_fish': 'fish'}
        cifar_labels = [
            l if l not in cifar_to_tinyimg else cifar_to_tinyimg[l]
            for l in cifar_labels
        ]
        load_indices = sum(
            [list(range(*tinyimg_index[label])) for label in cifar_labels], [])
    elif nb_extra == 500000:
        print("Using %d random images." % nb_extra)
        nb_tinyimages = max(e for s, e in tinyimg_index.values())
        load_indices = np.arange(nb_tinyimages)
        rng.shuffle(load_indices)
        load_indices = load_indices[:nb_extra]
        load_indices.sort()  # sorted for faster seeks.
    else:
        raise ValueError('`--extra` must be integer 237203 or 500000.')

    print("Loading %d auxiliary unlabeled Tiny Images." % len(load_indices))
    z_train = load_tinyimages(load_indices)

    # Load CIFAR-100.
    (x_train, y_train), (x_test, y_test) = cifar100.load_data()

    x_test = global_contrast_normalize(x_test)
    x_train = global_contrast_normalize(x_train)
    z_train = global_contrast_normalize(z_train)

    zca_whiten = zca_whitener(np.concatenate([x_train, z_train], axis=0))
    x_test = zca_whiten(x_test)
    x_train = zca_whiten(x_train)
    z_train = zca_whiten(z_train)

    x_test = x_test.reshape((len(x_test), 32, 32, 3))
    x_train = x_train.reshape((len(x_train), 32, 32, 3))
    z_train = z_train.reshape((len(z_train), 32, 32, 3))

    y_train = to_categorical(y_train)

    # Shared training parameters.
    zca = True
    hflip = True
    epochs = 50
    base_lr = 0.05
    batch_size = 8
    nb_classes = 100
    lr_decay_power = 0.5
    super_dropout = 0.2
    in_network_dropout = 0.0
    input_shape = (32, 32, 3)
    max_iter = (len(x_train) // batch_size) * epochs

    # Compile the SESEMI model.
    sesemi_model, inference_model = compile_sesemi(arg2var[network],
                                                   input_shape, nb_classes,
                                                   base_lr, in_network_dropout,
                                                   super_dropout)
    print(sesemi_model.summary())

    lr_poly_decay = LRScheduler(base_lr, max_iter, lr_decay_power)
    evaluate = DenseEvaluator(inference_model, (x_test, y_test),
                              hflip,
                              oversample=True)

    super_datagen = ImageDataGenerator(
        width_shift_range=[-2, -1, 0, 1, 2],
        height_shift_range=[-2, -1, 0, 1, 2],
        horizontal_flip=hflip,
        preprocessing_function=gaussian_noise,
        fill_mode='reflect',
    )
    self_datagen = ImageDataGenerator(
        width_shift_range=[-2, -1, 0, 1, 2],
        height_shift_range=[-2, -1, 0, 1, 2],
        horizontal_flip=False,
        preprocessing_function=gaussian_noise,
        fill_mode='reflect',
    )

    super_data = super_datagen.flow(x_train,
                                    y_train,
                                    shuffle=True,
                                    batch_size=1,
                                    seed=None)
    self_data = self_datagen.flow(x_train,
                                  shuffle=True,
                                  batch_size=1,
                                  seed=None)
    extra_data = self_datagen.flow(z_train,
                                   shuffle=True,
                                   batch_size=1,
                                   seed=None)
    train_data_loader = datagen_tinyimages(super_data, self_data, extra_data,
                                           batch_size)

    # Fit the SESEMI model on mini-batches with data augmentation.
    print('Run configuration:')
    print('network=%s,' % network, 'ZCA=%s,' % zca, 'nb_epochs=%d,' % epochs, \
          'horizontal_flip=%s,' % hflip, 'nb_extra=%d,' % len(z_train), \
          'batch_size=%d,' % batch_size, 'gpu_id=%s' % args.gpu_id)
    sesemi_model.fit_generator(
        train_data_loader,
        epochs=epochs,
        verbose=1,
        steps_per_epoch=len(x_train) // batch_size,
        callbacks=[lr_poly_decay, evaluate],
    )
コード例 #3
0
def main():
    args = parse_args()
    network = args.network
    dataset = args.dataset
    nb_labels = args.nb_labels
    os.environ['CUDA_VISIBLE_DEVICES'] = str(args.gpu_id)
    
    arg2var = {'convnet': convnet,
               'wrn': wrn,
			   'resnet50v2':resnet50v2,
               'svhn': svhn,
               'cifar10': cifar10,
               'cifar100': cifar100,}
    
    # Dataset-specific parameters
    hflip = True
    zca = True
    epochs = 10
    if dataset in ['svhn', 'cifar10']:
        if dataset == 'svhn':
            hflip = False
            zca = False
            epochs = 30
        nb_classes = 10
    elif dataset == 'cifar100':
        nb_classes = 100
    else:
        raise ValueError('`dataset` must be "svhn", "cifar10", "cifar100".')

    (x_train, y_train), (x_test, y_test) = arg2var[dataset].load_data()

    x_train = global_contrast_normalize(x_train)
    x_test = global_contrast_normalize(x_test)
    
    if zca:
        zca_whiten = zca_whitener(x_train)
        x_train = zca_whiten(x_train)
        x_test = zca_whiten(x_test)

    x_train = x_train.reshape((len(x_train), 32, 32, 3))
    x_test = x_test.reshape((len(x_test), 32, 32, 3))
    
    labels_per_class = nb_labels // nb_classes
    if nb_labels == 73257:
        labels_per_class = 1000000
    sample_inds = stratified_sample(y_train, labels_per_class)
    x_labeled = x_train[sample_inds]
    y_labeled = y_train[sample_inds]
    y_labeled = to_categorical(y_labeled)
    
    # Training parameters
    input_shape = (32, 32, 3)
    batch_size = 32
    base_lr = 0.05
    lr_decay_power = 0.5
    dropout_rate = 0.2
    max_iter = (len(x_train) // batch_size) * epochs

    sesemi_model, inference_model = open_sesemi(
        arg2var[network], input_shape, nb_classes, base_lr, dropout_rate)
    print(sesemi_model.summary())

    super_datagen = ImageDataGenerator(
            width_shift_range=3,
            height_shift_range=3,
            horizontal_flip=hflip,
            preprocessing_function=gaussian_noise,
            fill_mode='reflect',
        )
    self_datagen = ImageDataGenerator(
            width_shift_range=3,
            height_shift_range=3,
            horizontal_flip=False,
            preprocessing_function=gaussian_noise,
            fill_mode='reflect',
        )

    super_data = super_datagen.flow(
            x_labeled, y_labeled, shuffle=True, batch_size=1, seed=None)
    self_data = self_datagen.flow(
            x_train, shuffle=True, batch_size=1, seed=None)
    train_data_loader = datagen(super_data, self_data, batch_size)

    lr_poly_decay = LRScheduler(base_lr, max_iter, lr_decay_power)
    evaluate = DenseEvaluator(inference_model, (x_test, y_test), hflip)
    
    # Fit the SESEMI model on mini-batches with data augmentation
    print('Run configuration:')
    print('network=%s,' % network, 'dataset=%s,' % dataset, \
          'horizontal_flip=%s,' % hflip, 'ZCA=%s,' % zca, \
          'nb_epochs=%d,' % epochs, 'batch_size=%d,' % batch_size, \
          'nb_labels=%d,' % len(x_labeled), 'gpu_id=%d' % args.gpu_id)
    sesemi_model.fit_generator(train_data_loader,
                               epochs=epochs, verbose=1,
                               steps_per_epoch=len(x_train) // batch_size,
                               callbacks=[lr_poly_decay, evaluate],)
    return