def data():
    nb_classes = 10
    # the data, shuffled and split between train and test sets
    (X_train, y_train), (X_test, y_test) = cifar10.load_data()
    print('X_train shape:', X_train.shape)
    print(X_train.shape[0], 'train samples')
    print(X_test.shape[0], 'test samples')

    # convert class vectors to binary class matrices
    Y_train = np_utils.to_categorical(y_train, nb_classes)
    Y_test = np_utils.to_categorical(y_test, nb_classes)

    X_train = X_train.astype('float32')
    X_test = X_test.astype('float32')
    X_train /= 255
    X_test /= 255

    # this will do preprocessing and realtime data augmentation
    datagen = ImageDataGenerator(
        featurewise_center=False,  # set input mean to 0 over the dataset
        samplewise_center=False,  # set each sample mean to 0
        featurewise_std_normalization=False,  # divide inputs by std of the dataset
        samplewise_std_normalization=False,  # divide each input by its std
        zca_whitening=False,  # apply ZCA whitening
        rotation_range=0,  # randomly rotate images in the range (degrees, 0 to 180)
        width_shift_range=0.1,  # randomly shift images horizontally (fraction of total width)
        height_shift_range=0.1,  # randomly shift images vertically (fraction of total height)
        horizontal_flip=True,  # randomly flip images
        vertical_flip=False)  # randomly flip images

    # compute quantities required for featurewise normalization
    # (std, mean, and principal components if ZCA whitening is applied)
    datagen.fit(X_train)

    return datagen, X_train, Y_train, X_test, Y_test
def data_cifar10():
    """
    Preprocess CIFAR10 dataset
    :return:
    """

    # These values are specific to CIFAR10
    img_rows = 32
    img_cols = 32
    nb_classes = 10

    # the data, shuffled and split between train and test sets
    (X_train, y_train), (X_test, y_test) = cifar10.load_data()

    if keras.backend.image_dim_ordering() == 'th':
        X_train = X_train.reshape(X_train.shape[0], 3, img_rows, img_cols)
        X_test = X_test.reshape(X_test.shape[0], 3, img_rows, img_cols)
    else:
        X_train = X_train.reshape(X_train.shape[0], img_rows, img_cols, 3)
        X_test = X_test.reshape(X_test.shape[0], img_rows, img_cols, 3)
    X_train = X_train.astype('float32')
    X_test = X_test.astype('float32')
    X_train /= 255
    X_test /= 255
    print('X_train shape:', X_train.shape)
    print(X_train.shape[0], 'train samples')
    print(X_test.shape[0], 'test samples')

    # convert class vectors to binary class matrices
    Y_train = np_utils.to_categorical(y_train, nb_classes)
    Y_test = np_utils.to_categorical(y_test, nb_classes)
    return X_train, Y_train, X_test, Y_test
Exemple #3
0
def load_cifar_dataset():
    from keras.datasets import cifar10
    from keras.preprocessing.image import ImageDataGenerator
    num_classes = 10
    data_augmentation = True
    num_predictions = 20

    # The data, shuffled and split between train and test sets:
    (x_train, y_train), (x_test, y_test) = cifar10.load_data()

    # normalizing
    x_train = x_train.astype('float32')
    x_test = x_test.astype('float32')
    x_train /= 255
    x_test /= 255

    print('x_train shape:', x_train.shape)
    print(x_train.shape[0], 'train samples')
    print(x_test.shape[0], 'test samples')

    # Convert class vectors to binary class matrices.
    y_train = keras.utils.to_categorical(y_train, num_classes)
    y_test = keras.utils.to_categorical(y_test, num_classes)

    return x_train, y_train, x_test, y_test
def main():

    # load data
    #mndata = MNIST('../data')
    #train_data=mndata.load_training()
    #test_data=mndata.load_testing()

    (X_train, y_train), (X_test, y_test) = cifar10.load_data()
    
    # parse aguments
    parser = argparse.ArgumentParser(description='train simple resnet')
    parser.add_argument('-e', metavar='epochs', dest="epochs",type=int,
                        help='number of training epochs')
    parser.add_argument('-b', metavar='batch_size', dest="batch_size",type=int,
                        help='batch_size to train with')
    parser.add_argument('-s', metavar='samples', dest="samples",type=int,
                        help='number of training samples to use')
    parser.add_argument('-d', metavar='depth', dest="depth",type=int,
                        help='number of reblocks to use')
    parser.add_argument('-p', metavar="survival_p", dest="survival_p", type=float, 
                        help='set this to use stochastic resnet')
    parser.add_argument('-f', metavar="filter_increase", dest="filt_inc", nargs='+', 
                        help='specify at which resblocks the filters should double')

    parser.set_defaults(epochs=100,samples=0,batch_size=20,depth=17,survival_p=.5,filt_inc=[3,7,13])
    args = parser.parse_args()
    
    epochs=args.epochs
    samples = len(train_data[1]) if args.samples == 0 else args.samples
    batch_size=args.batch_size
    depth=args.depth
    samples_test = len(test_data[1]) if args.samples == 0 else args.samples
    pl=args.survival_p
    filt_inc = args.filt_inc

    X_train = np.array(X_train[:samples])
    y_train = np.array(y_train[:samples])
    X_test = np.array(X_test[:samples_test])
    y_test = np.array(y_test[:samples_test])

    z = np.array([binomial(1,1-(d/depth)*(1-pl)) for d in range(1,depth)]) #.reshape(1,16)
    
    resnet = get_resnet((3,32,32), depth, filt_inc,z)
    sgd =SGD(lr=0.001, decay=1e-4,momentum=0.9)
    resnet.compile(optimizer=sgd,
              loss='categorical_crossentropy',
              metrics=['accuracy'])

    labels = to_categorical(y_train, 10)
    #sample_z = SurvivalProb(depth,pl)
    
    #Z = np.array([z for e in range(batch_size)])

    resnet.fit([X_train],labels,nb_epoch=epochs,batch_size=batch_size)
    
    labels_test = to_categorical(y_test, 10)
    #resnet.z = np.ones(depth)
    score = resnet.evaluate(X_test, labels_test, batch_size=batch_size)

    print(score)
def main(_):

    if FLAGS.dataset == 'cifar10':
        (X_train, y_train), (_, _) = cifar10.load_data()
        X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.2, random_state=0)
    else:
        with open('data/train.p', mode='rb') as f:
            train = pickle.load(f)
        X_train, X_val, y_train, y_val = train_test_split(train['features'], train['labels'], test_size=0.33, random_state=0)

    train_output_file = "{}_{}_{}.p".format(FLAGS.network, FLAGS.dataset, 'bottleneck_features_train')
    validation_output_file = "{}_{}_{}.p".format(FLAGS.network, FLAGS.dataset, 'bottleneck_features_validation')

    print("Resizing to", (w, h, ch))
    print("Saving to ...")
    print(train_output_file)
    print(validation_output_file)

    with tf.Session() as sess:
        K.set_session(sess)
        K.set_learning_phase(1)

        model = create_model()

        print('Bottleneck training')
        train_gen = gen(sess, X_train, y_train, batch_size)
        bottleneck_features_train = model.predict_generator(train_gen(), X_train.shape[0])
        data = {'features': bottleneck_features_train, 'labels': y_train}
        pickle.dump(data, open(train_output_file, 'wb'))

        print('Bottleneck validation')
        val_gen = gen(sess, X_val, y_val, batch_size)
        bottleneck_features_validation = model.predict_generator(val_gen(), X_val.shape[0])
        data = {'features': bottleneck_features_validation, 'labels': y_val}
        pickle.dump(data, open(validation_output_file, 'wb'))
Exemple #6
0
    def build_data_cifar10(self):
        print("Loading data...")

        (self.Xtrain, _), (_, _) = cifar10.load_data()
        self.Xtrain = self.Xtrain[:501]

        self.extra = np.shape(self.Xtrain)[0] % self.batch_size

        if self.extra > 0:
            self.Xtrain = self.Xtrain[:-self.extra]

        Ytrain = self.Xtrain.copy()
        self.Xtrain[:,:,:,-self.margin:] = 0

        RGB_Shapr = (np.shape(self.Xtrain)[0], np.shape(self.Xtrain)[2], np.shape(self.Xtrain)[3], 256)
        RGB_Shapr_train = (np.shape(self.Xtrain)[0], np.shape(self.Xtrain)[2]*np.shape(self.Xtrain)[3], 256)
        self.Rtrain = np.zeros(RGB_Shapr, dtype=self.Xtrain.dtype)
        self.Gtrain = np.zeros(RGB_Shapr, dtype=self.Xtrain.dtype)
        self.Btrain = np.zeros(RGB_Shapr, dtype=self.Xtrain.dtype)

        for s in range(np.shape(self.Xtrain)[0]):
            for j in range(3):
                for i in range(self.img_rows):
                    for k in range(self.img_cols):
                        if j == 0:
                            self.Rtrain[s, i, k, Ytrain[s,j,i,k]] = 1
                        elif j == 1:
                            self.Gtrain[s, i, k, Ytrain[s,j,i,k]] = 1
                        else:
                            self.Btrain[s, i, k, Ytrain[s,j,i,k]] = 1

        self.Rtrain = np.reshape(self.Rtrain,RGB_Shapr_train)
        self.Gtrain = np.reshape(self.Gtrain,RGB_Shapr_train)
        self.Btrain = np.reshape(self.Btrain,RGB_Shapr_train)
def load_cifar10(outputlayer='svm', theano_shared=True):
    ''' Loads the cifar dataset

    :type ds_rate: float
    :param ds_rate: downsample rate; should be larger than 1, if provided.

    :type theano_shared: boolean
    :param theano_shared: If true, the function returns the dataset as Theano
    shared variables. Otherwise, the function returns raw data.
    '''

    (X_train, y_train), (X_test, y_test) = cifar10.load_data()

    train_set = X_train[0:45000,:,:].reshape((45000, 32*32*3)), y_train[0:45000,:].flatten()
    valid_set = X_train[45000:,:,:].reshape((5000, 32*32*3)), y_train[45000:,:].flatten()
    test_set =  X_test.reshape((len(X_test), 32*32*3)), y_test.flatten()

    print('Loading cifar10 data with %s output layer' % outputlayer)

    if outputlayer=='svm':
        test_set_x, test_set_y = shared_svm(test_set)
        valid_set_x, valid_set_y = shared_svm(valid_set)
        train_set_x, train_set_y = shared_svm(train_set)

    if outputlayer=='Logistic':
        test_set_x, test_set_y = shared_dataset(test_set)
        valid_set_x, valid_set_y = shared_dataset(valid_set)
        train_set_x, train_set_y = shared_dataset(train_set)

    rval = [(train_set_x, train_set_y), (valid_set_x, valid_set_y), (test_set_x, test_set_y)]
    return rval
def train_on_cifar10(model):
    from keras.datasets import cifar10
    from keras.utils import np_utils
    # settings
    batch_size = 32
    nb_classes = 10
    nb_epoch = 200


    # the data, shuffled and split between train and test sets
    (X_train, y_train), (X_test, y_test) = cifar10.load_data()
    X_train = X_train.astype('float32')
    X_test = X_test.astype('float32')
    X_train /= 255.
    X_test /= 255.
    X_train -= np.mean(X_train, axis=0)
    X_test -= np.mean(X_test, axis=0)


    print('X_train shape:', X_train.shape)
    print(X_train.shape[0], 'train samples')
    print(X_test.shape[0], 'test samples')

    # convert class vectors to binary class matrices
    Y_train = np_utils.to_categorical(y_train, nb_classes)
    Y_test = np_utils.to_categorical(y_test, nb_classes)

    model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"])
    model.fit(X_train, Y_train, batch_size=batch_size, epochs=nb_epoch, validation_data=(X_test,Y_test))
    score = model.evaluate(X_test, Y_test)
    print(score)
    return None
Exemple #9
0
 def __init__(self, **kwargs):
     super(CIFAR10, self).__init__(**kwargs)
     self.cls_names = cls_names
     (X_train, y_train), (X_test, y_test) = cifar10.load_data()
     y_train = y_train.reshape((y_train.shape[0]))
     y_test = y_test.reshape((y_test.shape[0]))
     if self.data_set == 'train':
         X = X_train
         y = y_train
     elif self.data_set == 'train-small':
         X = X_train[:1000]
         y = y_train[:1000]
     elif self.data_set == 'test':
         X = X_test
         y = y_test
     elif self.data_set == 'test-small':
         X = X_test[:1000]
         y = y_test[:1000]
     elif self.data_set == 'all':
         X = np.vstack((X_train, X_test))
         y = np.vstack((y_train, y_test))
     else:
         raise ValueError('MNIST Unsupported data_set: ', self.data_set)
     if X.shape[-1] == 3:
         X = X.transpose((0, 3, 1, 2))
     # normalization
     if self.norm:
         X = X.astype(np.float32) / 255
     X = self.init_layout_X(X)
     y = self.init_layout_y(y)
     self.X = X
     self.y = y
Exemple #10
0
 def data_mix(self):
     
     # randomly choose dataset
     dataset = random.choice(['mnist', 'cifar10', 'cifar100'])#
     
     n_labels = 10
     
     if dataset == "mnist":
         data = mnist.load_data()
     
     if dataset == "cifar10":
         data = cifar10.load_data()
     
     if dataset == "cifar100":
         data = cifar100.load_data()
         n_labels = 100
     
     # Choose dataset size. This affects regularization needed
     r = np.random.rand()
     
     # not using full dataset to make regularization more important and 
     # speed up testing a little bit
     data_size = int( 2000 * (1-r) + 40000 * r )
     
     # I do not use test data for validation, but last 10000 instances in dataset 
     # so that trained models can be compared to results in literature
     (CX, CY), (CXt, CYt) = data
     
     if dataset == "mnist":
         CX = np.expand_dims(CX, axis=1)
     
     data = CX[:data_size], CY[:data_size], CX[-10000:], CY[-10000:];
      
     return data, n_labels
Exemple #11
0
 def get_cifar10():
     (X_train, y_train), (X_test, y_test) = cifar10.load_data()
     Y_train = np_utils.to_categorical(y_train, 10).astype("float32")
     Y_test = np_utils.to_categorical(y_test, 10).astype("float32")
     X_train = X_train.astype("float32") / 255
     X_test = X_test.astype("float32") / 255
     return (X_train, Y_train), (X_test, Y_test)
Exemple #12
0
def construct_split_cifar10(task_labels,  split='train'):
    """Split CIFAR10 dataset by labels.

        Args:
            task_labels: list of list of labels, one for each dataset
            split: whether to use train or testing data

        Returns:
            List of (X, y) tuples representing each dataset
    """
    # Load CIFAR10 data and normalize
    nb_classes = 10
    (X_train, y_train), (X_test, y_test) = cifar10.load_data()
    # X_train = X_train.reshape(-1, 3, 32, 32)
    # X_test = X_test.reshape(-1, 32**2)
    X_train = X_train.astype('float32')
    X_test = X_test.astype('float32')
    no = X_train.max()
    X_train /= no
    X_test /= no

    if split == 'train':
        X, y = X_train, y_train
    else:
        X, y = X_test, y_test

    return split_dataset_by_labels(X, y, task_labels, nb_classes)
Exemple #13
0
def test():
    from keras.datasets import mnist, cifar10
    from keras.models import Sequential
    from keras.layers import Flatten, Dense, Activation, MaxPooling2D, Dropout, Convolution2D
    from keras.utils import np_utils
    (x_train, y_train), (x_test, y_test) = cifar10.load_data()

    x_train = x_train.astype('float32').reshape((len(x_train), 32, 32, 3)) / 255.
    x_test = x_test.astype('float32').reshape((len(x_test), 32, 32, 3)) / 255.
    y_train = np_utils.to_categorical(y_train)
    y_test = np_utils.to_categorical(y_test)
    rows, cols = x_train.shape[1:3]

    model = Sequential()
    model.add(SemiSupervizedConvolution2D(16, 3, 3, batch_input_shape=(16, rows, cols, 3)))
    model.add(Convolution2D(32, 3, 3, border_mode='same', dim_ordering='tf'))
    model.add(MaxPooling2D(pool_size=(2, 2), dim_ordering='tf'))
    model.add(Dropout(0.25))

    model.add(SemiSupervizedConvolution2D(64, 3, 3))
    model.add(Convolution2D(64, 3, 3, border_mode='same', dim_ordering='tf'))
    model.add(MaxPooling2D(pool_size=(2, 2), dim_ordering='tf'))
    model.add(Dropout(0.25))

    model.add(Flatten())
    model.add(Dense(512))
    model.add(Activation('relu'))
    model.add(Dropout(0.5))
    model.add(Dense(10))
    model.add(Activation('softmax'))

    model.compile(loss='categorical_crossentropy', optimizer='rmsprop')

    model.fit(x_train, y_train, batch_size=16, show_accuracy=True,
              validation_data=(x_test, y_test), nb_epoch=20)
    def train(self):
        # load data
        (x_train, y_train), (x_test, y_test) = cifar10.load_data()
        y_train = keras.utils.to_categorical(y_train, self.num_classes)
        y_test = keras.utils.to_categorical(y_test, self.num_classes)
        
        x_train, x_test = self.color_preprocessing(x_train, x_test)

        # build network
        model = self.build_model()
        model.summary()

        # Save the best model during each training checkpoint
        checkpoint = ModelCheckpoint(self.model_filename,
                                    monitor='val_loss', 
                                    verbose=0,
                                    save_best_only= True,
                                    mode='auto')
        plot_callback = PlotLearning()
        tb_cb = TensorBoard(log_dir=self.log_filepath, histogram_freq=0)

        cbks = [checkpoint, plot_callback, tb_cb]

        # set data augmentation
        print('Using real-time data augmentation.')
        datagen = ImageDataGenerator(horizontal_flip=True,width_shift_range=0.125,height_shift_range=0.125,fill_mode='constant',cval=0.)
        datagen.fit(x_train)

        # start training
        model.fit_generator(datagen.flow(x_train, y_train,batch_size=self.batch_size),steps_per_epoch=self.iterations,epochs=self.epochs,callbacks=cbks,validation_data=(x_test, y_test))
        
        model.save(self.model_filename)

        self._model = model
Exemple #15
0
def load_cifar10_data(img_rows, img_cols):

    # Load cifar10 training and validation sets
    (X_train, Y_train), (X_valid, Y_valid) = cifar10.load_data()

    # Resize training images
    X_train = X_train[:5000]
    Y_train = Y_train[:5000]
    X_valid = X_train[:5000]
    Y_valid = Y_valid[:5000]
    X_train = np.array([cv2.resize(img, (img_rows, img_cols))
                        for img in X_train])
    X_valid = np.array([cv2.resize(img, (img_rows, img_cols))
                        for img in X_valid])

    # Transform targets to keras compatible format
    Y_train = np_utils.to_categorical(Y_train, num_classes)
    Y_valid = np_utils.to_categorical(Y_valid, num_classes)

    X_train = X_train.astype('float32')
    X_valid = X_valid.astype('float32')

    # preprocess data
    X_train = X_train / 255.0
    X_valid = X_valid / 255.0

    return X_train, Y_train, X_valid, Y_valid
Exemple #16
0
def cifar10(*args, **kwargs):
    dataset = cx.Dataset()
    from keras.datasets import cifar10
    (x_train, y_train), (x_test, y_test) = cifar10.load_data()
    inputs = np.concatenate((x_train, x_test))
    x_train, x_test = None, None
    inputs = inputs.astype('float32')
    inputs /= 255
    labels = np.concatenate((y_train, y_test))
    y_train, y_test = None, None
    targets = to_categorical(labels, 10)
    labels = np.array([str(label[0]) for label in labels], dtype=str)
    dataset.name = "CIFAR-10"
    dataset.description = """
Original source: https://www.cs.toronto.edu/~kriz/cifar.html

The CIFAR-10 dataset consists of 60000 32x32 colour images in 10
classes, with 6000 images per class.

The classes are completely mutually exclusive. There is no overlap
between automobiles and trucks. "Automobile" includes sedans, SUVs,
things of that sort. "Truck" includes only big trucks. Neither
includes pickup trucks.
"""
    dataset.load_direct([inputs], [targets], [labels])
    return dataset
Exemple #17
0
    def get_data(self):
        
        from keras.datasets import cifar10
        
        (X_train, y_train), (X_test, y_test) = cifar10.load_data()
        
        if self.verbose:
            print('X_train shape:', X_train.shape)
            print(X_train.shape[0], 'train samples')
            print(X_test.shape[0], 'test samples')
        
        # convert class vectors to binary class matrices
        from keras.utils import np_utils
        Y_train = np_utils.to_categorical(y_train, nb_classes)
        Y_test = np_utils.to_categorical(y_test, nb_classes)
        
        X_train = X_train.astype('float32')
        X_test = X_test.astype('float32')
        X_train /= 255
        X_test /= 255
    
        
        img_mean = X_train.mean(axis=0)[np.newaxis,:,:,:]

        N = X_train.shape[0]
        perms = np.random.permutation(N)
        self.X_train   = X_train[perms,:]
        self.Y_train = Y_train[perms,:]
        self.X_test = X_test
        self.Y_test = Y_test
        
        self.rawdata=[self.X_train, self.Y_train, self.X_test, self.Y_test, img_mean]
Exemple #18
0
def get_cifar10():
    """Get cifar10 data."""
    (X_train, y_train), (X_test, y_test) = cifar10.load_data()
    Y_train = np_utils.to_categorical(y_train, 10)
    Y_test = np_utils.to_categorical(y_test, 10)
    X_train = X_train.astype('float32') / 255
    X_test = X_test.astype('float32') / 255
    return X_train, X_test, Y_train, Y_test
Exemple #19
0
def test_cifar():
    # only run data download tests 20% of the time
    # to speed up frequent testing
    random.seed(time.time())
    if random.random() > 0.8:
        (X_train, y_train), (X_test, y_test) = cifar10.load_data()
        (X_train, y_train), (X_test, y_test) = cifar100.load_data('fine')
        (X_train, y_train), (X_test, y_test) = cifar100.load_data('coarse')
Exemple #20
0
def get_data(dataset='mnist'):
    """
    images in [-0.5, 0.5] (instead of [0, 1]) which suits C&W attack and generally gives better performance
    
    :param dataset:
    :return: 
    """
    assert dataset in ['mnist', 'cifar', 'svhn'], \
        "dataset parameter must be either 'mnist' 'cifar' or 'svhn'"
    if dataset == 'mnist':
        # the data, shuffled and split between train and test sets
        (X_train, y_train), (X_test, y_test) = mnist.load_data()
        # reshape to (n_samples, 28, 28, 1)
        X_train = X_train.reshape(-1, 28, 28, 1)
        X_test = X_test.reshape(-1, 28, 28, 1)
    elif dataset == 'cifar':
        # the data, shuffled and split between train and test sets
        (X_train, y_train), (X_test, y_test) = cifar10.load_data()
    else:
        if not os.path.isfile(os.path.join(PATH_DATA, "svhn_train.mat")):
            print('Downloading SVHN train set...')
            call(
                "curl -o ../data/svhn_train.mat "
                "http://ufldl.stanford.edu/housenumbers/train_32x32.mat",
                shell=True
            )
        if not os.path.isfile(os.path.join(PATH_DATA, "svhn_test.mat")):
            print('Downloading SVHN test set...')
            call(
                "curl -o ../data/svhn_test.mat "
                "http://ufldl.stanford.edu/housenumbers/test_32x32.mat",
                shell=True
            )
        train = sio.loadmat(os.path.join(PATH_DATA,'svhn_train.mat'))
        test = sio.loadmat(os.path.join(PATH_DATA, 'svhn_test.mat'))
        X_train = np.transpose(train['X'], axes=[3, 0, 1, 2])
        X_test = np.transpose(test['X'], axes=[3, 0, 1, 2])
        # reshape (n_samples, 1) to (n_samples,) and change 1-index
        # to 0-index
        y_train = np.reshape(train['y'], (-1,)) - 1
        y_test = np.reshape(test['y'], (-1,)) - 1

    # cast pixels to floats, normalize to [0, 1] range
    X_train = X_train.astype('float32')
    X_test = X_test.astype('float32')
    X_train = (X_train/255.0) - (1.0 - CLIP_MAX)
    X_test = (X_test/255.0) - (1.0 - CLIP_MAX)

    # one-hot-encode the labels
    Y_train = np_utils.to_categorical(y_train, 10)
    Y_test = np_utils.to_categorical(y_test, 10)

    print("X_train:", X_train.shape)
    print("Y_train:", Y_train.shape)
    print("X_test:", X_test.shape)
    print("Y_test", Y_test.shape)

    return X_train, Y_train, X_test, Y_test
    def accuracy(self):
        (x_train, y_train), (x_test, y_test) = cifar10.load_data()
        y_train = keras.utils.to_categorical(y_train, self.num_classes)
        y_test = keras.utils.to_categorical(y_test, self.num_classes)

        # color preprocessing
        x_train, x_test = self.color_preprocessing(x_train, x_test)

        return self._model.evaluate(x_test, y_test, verbose=0)[1]
Exemple #22
0
def load_data():
	''' load and normalize data from dataset files '''
	(X, y), (X_test, y_test) = cifar10.load_data()
	n = X.shape[0]
	n1 = int(n * training_set_ratio)
	X_train=X[0:n1,:]
	y_train=y[0:n1]
	X_val=X[n1:n,:]
	y_val=y[n1:n]
	return X_train, y_train, X_val, y_val, X_test, y_test
Exemple #23
0
 def load_data(self):
     data_name = self.data_name
     if data_name == "MNIST":
         return mnist.load_data()
     elif data_name == "CIFAR10-GRAY":
         (X_train, y_train), (X_test, y_test) = cifar10.load_data()
         X_train_gray = rgb2gray(X_train)
         X_test_gray = rgb2gray(X_test)
         return (X_train_gray, y_train), (X_test_gray, y_test)
     else:
         raise ValueError("data_name of {} is not supported!".format(data_name))
Exemple #24
0
def construct_transfer_cifar10_cifar100(nb_tasks=4, split='train'):
    """
    Returns a two task dataset in which the first task is the full CIFAR10 dataset and the second task are 10 from CIFAR100
    classes from the CIFAR100 dataset.

    params:
        nb_tasks The total number of tasks 
        split Whether to return training or validation data

    returns:
        A list with two tuples containing the two data sets
    """
    (X_train, y_train), (X_test, y_test) = cifar10.load_data()
    # X_train = X_train.reshape(-1, 3, 32, 32)
    # X_test = X_test.reshape(-1, 32**2)
    X_train = X_train.astype('float32')
    X_test = X_test.astype('float32')
    no = X_train.max()
    X_train /= no
    X_test /= no

    if split == 'train':
        X, y = X_train, y_train
    else:
        X, y = X_test, y_test

    nb_classes = nb_tasks*10
    datasets = [(X,np_utils.to_categorical(y, nb_classes))]

    # Load CIFAR100 data and normalize
    (X_train, y_train), (X_test, y_test) = cifar100.load_data()
    X_train = X_train.astype('float32')
    X_test = X_test.astype('float32')
    m = np.max( (np.max(X_train), np.max(X_test) ) )
    X_train /= m
    X_test /= m

    if split == 'train':
        X, y = X_train, y_train
    else:
        X, y = X_test, y_test

    # split dataset by labels
    task_labels = [ range(10*i,10*(i+1)) for i in range(1,nb_tasks) ]
    for labels in task_labels:
        idx = np.in1d(y+10, labels)
        data = X[idx], np_utils.to_categorical(y[idx]+10, nb_classes)
        datasets.append(data)


    all_task_labels = [range(10)]
    all_task_labels.extend(task_labels)
    return all_task_labels, datasets
def load_cifar_10():
    from keras.datasets import cifar10
    num_classes = 10
    (x_train, y_train), (x_test, y_test) = cifar10.load_data()
    x_train = x_train.astype('float32')
    x_test = x_test.astype('float32')
    x_train /= 255.0
    x_test /= 255.0    
    y_train = to_categorical(y_train, num_classes)
    y_test = to_categorical(y_test, num_classes)

    return (x_train,y_train),(x_test,y_test)
def load_data():
    """
    Load data using pre-configured dataset loader within keras.

    MNIST (http://yann.lecun.com/exdb/mnist/)
        - mnist.load_data()
    CIFAR10 (https://www.cs.toronto.edu/~kriz/cifar.html)
        - cifar10.load_data()
    CIFAR100 (https://www.cs.toronto.edu/~kriz/cifar.html)
        - cifar100.load_data()
    """
    # The data, shuffled and split between train and test sets
    (X_data, train_labels), (y_data, test_labels) = cifar10.load_data()

    X_data = X_data.astype('float32')/255
    y_data = y_data.astype('float32')/255

    if GREYSCALE:
        train_data = rgb_to_greyscale(X_data)
        test_data = rgb_to_greyscale(y_data)
    else:
        train_data = X_data
        test_data = y_data

    # Number of classes in the used dataset
    nb_classes = 10
    # input image dimensions
    img_rows, img_cols = 32, 32
    # RGB images have 3 channels and greyscale images have 1
    img_channels = test_data.shape[1]

    # Static creation of training and validating datasets
    validate_data_id = np.random.randint(train_data.shape[0], size=10000)
    validate_data = train_data[validate_data_id, :]
    validate_labels = train_labels[validate_data_id, :]
    train_data_id = np.random.randint(train_data.shape[0], size=40000)
    train_data = train_data[train_data_id, :]
    train_labels = train_labels[train_data_id, :]

    # create flat array of test labels for evaluation of the model
    validated_classes = list(chain.from_iterable(test_labels))

    # convert class vectors to binary class matrices
    train_labels = np_utils.to_categorical(train_labels, nb_classes)
    validate_labels = np_utils.to_categorical(validate_labels, nb_classes)
    test_labels = np_utils.to_categorical(test_labels, nb_classes)

    dataset = Dataset(nb_classes, img_rows, img_cols, img_channels,
                      validated_classes,
                      train_data, train_labels,
                      validate_data, validate_labels,
                      test_data, test_labels)
    return dataset
    def train(self, epochs, batch_size=128, sample_interval=50):

        # Load the dataset
        (X_train, y_train), (_, _) = cifar10.load_data()

        # Extract dogs and cats
        X_cats = X_train[(y_train == 3).flatten()]
        X_dogs = X_train[(y_train == 5).flatten()]
        X_train = np.vstack((X_cats, X_dogs))

        # Rescale -1 to 1
        X_train = X_train / 175.5 - 1.
        y_train = y_train.reshape(-1, 1)

        # Adversarial ground truths
        valid = np.ones((batch_size, 1))
        fake = np.zeros((batch_size, 1))

        for epoch in range(epochs):

            # ---------------------
            #  Train Discriminator
            # ---------------------

            # Select a random batch of images
            idx = np.random.randint(0, X_train.shape[0], batch_size)
            imgs = X_train[idx]

            masked_imgs, missing_parts, _ = self.mask_randomly(imgs)

            # Generate a batch of new images
            gen_missing = self.generator.predict(masked_imgs)

            # Train the discriminator
            d_loss_real = self.discriminator.train_on_batch(missing_parts, valid)
            d_loss_fake = self.discriminator.train_on_batch(gen_missing, fake)
            d_loss = 0.5 * np.add(d_loss_real, d_loss_fake)

            # ---------------------
            #  Train Generator
            # ---------------------

            g_loss = self.combined.train_on_batch(masked_imgs, [missing_parts, valid])

            # Plot the progress
            print ("%d [D loss: %f, acc: %.2f%%] [G loss: %f, mse: %f]" % (epoch, d_loss[0], 100*d_loss[1], g_loss[0], g_loss[1]))

            # If at save interval => save generated image samples
            if epoch % sample_interval == 0:
                idx = np.random.randint(0, X_train.shape[0], 6)
                imgs = X_train[idx]
                self.sample_images(epoch, imgs)
Exemple #28
0
def load_cifar10():
    """
        Loads the cifar10 dataset (60000 32x32 colur images) in 10 balanced classes.
    """
    num_classes = 10
    img_rows, img_cols = (32, 32)
    
    (x_train, y_train), (x_test, y_test) = cifar10.load_data()

    x_train, y_train = prepare_image_dataset(x_train, y_train, (img_rows, img_cols), num_classes, reshape=False)
    x_test, y_test = prepare_image_dataset(x_train, y_train, (img_rows, img_cols), num_classes, reshape=False)
    input_shape = (img_rows, img_cols, 3)
    return (x_train, y_train), (x_test, y_test), input_shape
Exemple #29
0
def test_cifar():
    # only run data download tests 20% of the time
    # to speed up frequent testing
    random.seed(time.time())
    if random.random() > 0.8:
        (x_train, y_train), (x_test, y_test) = cifar10.load_data()
        assert len(x_train) == len(y_train) == 50000
        assert len(x_test) == len(y_test) == 10000
        (x_train, y_train), (x_test, y_test) = cifar100.load_data('fine')
        assert len(x_train) == len(y_train) == 50000
        assert len(x_test) == len(y_test) == 10000
        (x_train, y_train), (x_test, y_test) = cifar100.load_data('coarse')
        assert len(x_train) == len(y_train) == 50000
        assert len(x_test) == len(y_test) == 10000
    def __init__(self):
        # prepare trainigs data
        (X_train, y_train), (X_test, y_test) = cifar10.load_data()
        print('X_train shape:', X_train.shape)
        print(X_train.shape[0], 'train samples')
        print(X_test.shape[0], 'test samples')

        self.Y_train = np_utils.to_categorical(y_train, 10)
        self.Y_test = np_utils.to_categorical(y_test, 10)

        self.X_train = X_train.astype('float32')
        self.X_test = X_test.astype('float32')
        self.X_train /= 255
        self.X_test /= 255
Exemple #31
0
    nbr_digits = 3

    # paths
    save_path = os.path.join('../../../experiments', EXPERIMENT_ID)
    model_path = os.path.join('../../../experiments/', MODEL_EXP_ID)
    CNN_weights = os.path.join(model_path, 'best_weights.hdf5')
    CNN_arch = os.path.join(model_path, 'model.json')

    # create folder of experiments code, parameters and results
    createExpFolderandCodeList(save_path)

    # load model
    model = load_model(CNN_arch, CNN_weights)

    # load test set
    _, (data, y_true) = cifar10.load_data()
    y_true = np.squeeze(y_true)

    # select only zeros and ones
    data = data[(y_true == 0) + (y_true == 1)]
    y_true = y_true[(y_true == 0) + (y_true == 1)]

    # get meta data
    size_x = data[0].shape[-2]
    size_y = data[0].shape[-1]
    max_x = np.max(data)

    # add noise
    for i in range(len(data)):
        data[i] = add_noise(data[i])
Exemple #32
0
import numpy as np
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout

#Data                      1. 数据
'''
Your data needs to be stored as NumPy arrays or as a list of NumPy arrays. 
Ideally, you split the data in training and test sets, for which you can also 
resort to the train_test_split module of sklearn.cross_validation.
'''
##Keras Data Sets         1.1 Keras数据集合
from keras.datasets import boston_housing, mnist, cifar10, imdb
(x_train, y_train), (x_test, y_test) = mnist.load_data()
(x_train2, y_train2), (x_test2, y_test2) = boston_housing.load_data()
(x_train3, y_train3), (x_test3, y_test3) = cifar10.load_data()
(x_train4, y_train4), (x_test4, y_test4) = imdb.load_data(num_words=20000)
num_classes = 10

#Preprocessing            2. 预处理
##Sequence Padding        2.1 序列填充
from keras.preprocessing import sequence

x_train4 = sequence.pad_sequences(x_train4, maxlen=80)
x_test4 = sequence.pad_sequences(x_test4, maxlen=80)
##One-Hot Encoding        2.2 独热编码
from keras.utils import to_categorical

Y_train = to_categorical(y_train, num_classes)
Y_test = to_categorical(y_test, num_classes)
Y_train3 = to_categorical(y_train3, num_classes)
Exemple #33
0
import matplotlib.pyplot as plt
from keras.datasets import cifar10
from PIL import Image

# 데이터 변수 선언
(x_train, y_train), (x_test,
                     y_test) = cifar10.load_data()  # Keras Dataset에서 다운받기

labels = [
    'airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse',
    'ship', 'truck'
]

# 이미지 출력 -------------------------------------
for i in range(40):
    img = Image.fromarray(x_train[i])
    plt.subplot(5, 8, i + 1)
    plt.title(labels[y_train[i][0]])
    plt.tick_params(bottom='off')
    plt.tick_params(left='off')
    plt.imshow(img)
plt.show()
def main_color():
    # load the CIFAR10 data
    (x_train, _), (x_test, y_test) = cifar10.load_data()
    #(x_train, _), (x_test, _) = mnist.load_data()
    """
    for preprocessing,
    RGB to LAB
    
    for img in all of img
        DO CLAHE
    
    LAB to RGB 
    """
    x_train_prime = []
    for _img in x_train:
        #_img = cv2.cvtColor(_img, cv2.COLOR_GRAY2RGB)
        #_img = cv2.resize(_img, (80, 80))
        #_img = hist.preprocessing_hist(_img)
        x_train_prime.append(_img)
    x_train = np.array(x_train_prime)
    print(x_train.shape)

    x_test_prime = []
    for _img in x_test:
        #_img = cv2.cvtColor(_img, cv2.COLOR_GRAY2RGB)
        #_img = cv2.resize(_img, (80, 80))
        #_img = hist.preprocessing_hist(_img)
        x_test_prime.append(_img)
    x_test = np.array(x_test_prime)
    print(x_test.shape)
    """
    written by wooramkang 2018.08.21

    depending on CLAHE parameters,
    
    depending on dataset, you could use resizing and colorizing as well 
    
    08.22
    filter grid size    
        2 * 2
        4 * 4
        8 * 8
        16 * 16
    """

    img_rows = x_train.shape[1]
    img_cols = x_train.shape[2]
    channels = x_train.shape[3]

    imgs_dir = 'saved_images'
    save_dir = os.path.join(os.getcwd(), imgs_dir)
    if not os.path.isdir(save_dir):
            os.makedirs(save_dir)

    imgs = x_test[:100]
    print(y_test[:100])
    i = 0
    for _img in imgs:
        i = i+1
        Image.fromarray(_img).save('saved_images/{0}_img_raw.png'.format(i))
    #print raw img each image by image

    imgs = imgs.reshape((10, 10, img_rows, img_cols, channels))
    imgs = np.vstack([np.hstack(i) for i in imgs])
    Image.fromarray(imgs).save('saved_images/sumof_img_raw.png')

    x_train = x_train.astype('float32') / 255
    x_test = x_test.astype('float32') / 255

    x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, channels)
    x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, channels)

    input_shape = (img_rows, img_cols, 3)
    print(input_shape[1])
    batch_size = 32
    kernel_size = 3
    latent_dim = 256
    layer_filters = [64, 32, 16]

    inputs = Input(shape=input_shape, name='encoder_input')
    x = inputs


    for filters in layer_filters:
        x = Conv2D(filters=filters,
               kernel_size=kernel_size,
               strides=1,
               activation='relu',
               padding='same')(x)
        x = BatchNormalization()(x)
        x = Activation('elu')(x)

    for filters in layer_filters[::-1]:
        x = Conv2D(filters=filters,
               kernel_size=kernel_size,
               strides=1,
               activation='relu',
               padding='same')(x)
        x = BatchNormalization()(x)
        x = Activation('elu')(x)

    x = Dropout(rate=0.2)(x)
    x = Conv2D(filters=3,
                              kernel_size=kernel_size,
                              strides=1,
                              activation='sigmoid',
                              padding='same',
                              name='finaloutput_AE'
                              )(x)

    outputs = x#Conv2D(3, (3, 3), activation='relu', padding='same', name='finaloutput')(outputs)

    #decoder = Model(latent_inputs, outputs, name='decoder')
    #decoder.summary()

    #autoencoder = Model(inputs, decoder(encoder(inputs)), name='autoencoder')
    autoencoder = Model(inputs, x, name='autoencoder')
    autoencoder.summary()
    save_dir = os.path.join(os.getcwd(), 'saved_models')
    model_name = 'AE_model.{epoch:03d}.h5'
    if not os.path.isdir(save_dir):
            os.makedirs(save_dir)
    filepath = os.path.join(save_dir, model_name)

    lr_reducer = ReduceLROnPlateau(factor=np.sqrt(0.1),
                                   cooldown=0,
                                   patience=5,
                                   verbose=1,
                                   min_lr=0.5e-6)

    checkpoint = ModelCheckpoint(filepath=filepath,
                                 monitor='val_loss',
                                 verbose=1,
                                 save_best_only=True)

    autoencoder.compile(loss='mse', optimizer='adam')

    callbacks = [lr_reducer, checkpoint]

    # .fit(data for train, data for groundtruth, validtation set, epochs, batchsize, ...)
    autoencoder.fit(x_train,
                    x_train,
                    validation_data=(x_test, x_test),
                    epochs=10,
                    batch_size=batch_size,
                    callbacks=callbacks)
    autoencoder.summary()

    x_decoded = autoencoder.predict(x_test)

    imgs = x_decoded[:100]
    print(imgs.shape)
    imgs = (imgs * 255).astype(np.uint8)

    i = 0
    for _img in imgs:
        i = i + 1
        Image.fromarray(_img).save('saved_images/{0}_img_gen.png'.format(i))
    #print generated img each image by image

    imgs = imgs.reshape((10, 10, img_rows, img_cols, channels))
    imgs = np.vstack([np.hstack(i) for i in imgs])
    Image.fromarray(imgs).save('saved_images/sumof_img_gen.png')
server = tf.train.Server(cluster,
    job_name=FLAGS.job_name,
    task_index=FLAGS.task_index,
    config= config)

def prepare_input_data(X_train, X_test):
    X_train = X_train.astype('float32') / 255.0
    X_test = X_test.astype('float32') / 255.0
    return X_train, X_test

def prepare_output_data(y_train, y_test):
    y_train = np_utils.to_categorical(y_train)
    y_test = np_utils.to_categorical(y_test)
    return y_train, y_test

(X_train, Y_train), (X_test, Y_test) = cifar10.load_data()
x_train, x_test = prepare_input_data(X_train[:10000], X_test[:2000])
y_train, y_test = prepare_output_data(Y_train[:10000], Y_test[:2000])

# Network parameters
n_classes = 10 
image_size = 32
channel_size = 3
n_samples =  x_train.shape[0]
batch_size = 128
training_epochs = 5
num_iterations = n_samples//batch_size
test_step = 100
sigma = 1e-3
lr= 1e-2
input_shape = (image_size,image_size, channel_size)
# Random Erasing of Image Data Augumentation

import numpy as np
import matplotlib.pyplot as plt

from sklearn.utils import shuffle
from sklearn.metrics import f1_score
from sklearn.model_selection import train_test_split
from keras.datasets import cifar10

(train_X, train_y), (test_X, test_y) = cifar10.load_data()


# show images
def show_images(dataset):
    for i in range(4):
        fig = dataset[i]
        plt.imshow(fig)
        plt.show()


# Random Erasing
def random_erasing(img):
    p = 0.5
    s_l, s_h = 0.02, 0.4
    r1, r2 = 0.3, 1.0 / 0.3

    p1 = np.random.uniform(0, 1)

    if p1 < p:
        return img
Exemple #37
0
def main(model_name,
         dataset,
         neural_function,
         batch_size=32,
         epochs=100,
         color=1,
         data_augmentation=True,
         is_neg=1):
    output_folder = "Output"
    save_models = "saved_models/"

    save_dir = os.path.join(output_folder, save_models)

    if dataset == "cifar10":
        (x_train_c, y_train), (x_test_c, y_test) = cifar10.load_data()
        num_classes = 10
    elif dataset == "cifar100":
        (x_train_c, y_train), (x_test_c, y_test) = cifar100.load_data()
        num_classes = 100

    print("Loaded {} dataset".format(dataset))

    original_shape_train = x_train_c.shape
    original_shape_test = x_test_c.shape

    if color:
        x_train = x_train_c
        x_test = x_test_c
    else:

        x_train = np.zeros((original_shape_train[0], original_shape_train[1],
                            original_shape_train[2], 1))
        for k in range(original_shape_train[0]):
            x_train[k] = np.expand_dims(cv2.cvtColor(x_train_c[k],
                                                     cv2.COLOR_BGR2GRAY),
                                        axis=3)

        x_test = np.zeros((original_shape_test[0], original_shape_test[1],
                           original_shape_test[2], 1))
        for k in range(original_shape_test[0]):
            x_test[k] = np.expand_dims(cv2.cvtColor(x_test_c[k],
                                                    cv2.COLOR_BGR2GRAY),
                                       axis=3)

    print("Using {} images to train data".format(
        "color" if color else "greyscale"))

    print('x_train shape:', x_train.shape)
    print(x_train.shape[0], 'train samples')
    print(x_test.shape[0], 'test samples')

    # Convert class vectors to binary class matrices.
    y_train = keras.utils.to_categorical(y_train, num_classes)
    y_test = keras.utils.to_categorical(y_test, num_classes)

    input_shape = x_train.shape[1:]

    if neural_function == "linear":
        print("Using {} function for neurons".format(neural_function))
        if model_name == "alexnet":
            original_lr = 0.0001
            opt = RMSprop(lr=original_lr, decay=1e-6)
            dense_neu = 512
            model = alexnet(input_shape, num_classes, dense_neurons=dense_neu)
        elif model_name == "resnet":
            depth = 20
            model = resnet_v1(input_shape=input_shape,
                              depth=depth,
                              num_classes=num_classes)
            # opt = Adam(lr=lr_schedule(0))
            original_lr = 0.0001
            opt = RMSprop(lr=original_lr, decay=1e-6)

    elif neural_function == "quadratic":
        print("Using {} function for neurons".format(neural_function))
        if model_name == "alexnet":
            original_lr = 0.0001
            opt = RMSprop(lr=original_lr, decay=1e-6)
            model = qalexnet(input_shape,
                             num_classes,
                             activation='elu',
                             is_neg=is_neg)
        elif model_name == "resnet":
            depth = 20
            model = qresnet_v1(input_shape=input_shape,
                               depth=depth,
                               num_classes=num_classes,
                               q=True,
                               is_neg=is_neg)
            original_lr = 0.0001
            opt = RMSprop(lr=original_lr, decay=1e-6)

    else:
        print("Undefined neural function \"{}\"".format(neural_function))

    print("Compiling {} model with {} neural function to train on {}".format(
        model_name, neural_function, dataset))
    model.compile(loss='categorical_crossentropy',
                  optimizer=opt,
                  metrics=['accuracy'])

    x_train = x_train.astype('float32')
    x_test = x_test.astype('float32')
    x_train /= 255
    x_test /= 255

    this_model_name = "{}_{}_{}_{}_{}".format(
        dataset, model_name, "negative" if is_neg else "positive",
        neural_function, "color" if color else "greyscale")
    print("Model name {}".format(this_model_name))

    tensor_log_dir = 'logs/{}'.format(this_model_name)
    output_tensor_logs = os.path.join(output_folder, tensor_log_dir)
    print("Tensorflow log directory - {}".format(output_tensor_logs))
    tensorboard = TensorBoard(log_dir=output_tensor_logs)

    if not data_augmentation:
        print('Not using data augmentation.')
        model.fit(x_train,
                  y_train,
                  batch_size=batch_size,
                  epochs=epochs,
                  validation_data=(x_test, y_test),
                  shuffle=True)
    else:
        print('Using real-time data augmentation.')
        # This will do preprocessing and realtime data augmentation:
        datagen = ImageDataGenerator(
            featurewise_center=False,  # set input mean to 0 over the dataset
            samplewise_center=False,  # set each sample mean to 0
            featurewise_std_normalization=
            False,  # divide inputs by std of the dataset
            samplewise_std_normalization=False,  # divide each input by its std
            zca_whitening=False,  # apply ZCA whitening
            zca_epsilon=1e-06,  # epsilon for ZCA whitening
            rotation_range=
            0,  # randomly rotate images in the range (degrees, 0 to 180)
            # randomly shift images horizontally (fraction of total width)
            width_shift_range=0.1,
            # randomly shift images vertically (fraction of total height)
            height_shift_range=0.1,
            shear_range=0.,  # set range for random shear
            zoom_range=0.,  # set range for random zoom
            channel_shift_range=0.,  # set range for random channel shifts
            # set mode for filling points outside the input boundaries
            fill_mode='nearest',
            cval=0.,  # value used for fill_mode = "constant"
            horizontal_flip=True,  # randomly flip images
            vertical_flip=False,  # randomly flip images
            # set rescaling factor (applied before any other transformation)
            rescale=None,
            # set function that will be applied on each input
            preprocessing_function=None,
            # image data format, either "channels_first" or "channels_last"
            data_format=None,
            # fraction of images reserved for validation (strictly between 0 and 1)
            validation_split=0.0)

        datagen.fit(x_train)

        model.fit_generator(datagen.flow(x_train,
                                         y_train,
                                         batch_size=batch_size),
                            epochs=epochs,
                            validation_data=datagen.flow(x_test, y_test),
                            workers=4,
                            callbacks=[tensorboard])

        if not os.path.isdir(save_dir):
            os.makedirs(save_dir)
        model_path = os.path.join(save_dir, this_model_name)
        model.save(model_path)
        print('Saved trained model at %s ' % model_path)

        scores = model.evaluate(x_test, y_test, verbose=1)

        print('Test loss:', scores[0])
        print('Test accuracy:', scores[1])

        results = [
            datetime.datetime.now(), this_model_name,
            model.count_params(), scores[0], scores[1]
        ]

        testing_output_file = os.path.join(output_folder, "results.csv")
        with open(testing_output_file, 'a') as f:
            writer = csv.writer(f)
            writer.writerow(results)
Exemple #38
0
def run_cifar10(batch_size,
                nb_epoch,
                depth,
                nb_dense_block,
                nb_filter,
                growth_rate,
                dropout_rate,
                learning_rate,
                weight_decay,
                logfile,
                plot_architecture):
    """ Run CIFAR10 experiments

    :param batch_size: int -- batch size
    :param nb_epoch: int -- number of training epochs
    :param depth: int -- network depth
    :param nb_dense_block: int -- number of dense blocks
    :param nb_filter: int -- initial number of conv filter
    :param growth_rate: int -- number of new filters added by conv layers
    :param dropout_rate: float -- dropout rate
    :param learning_rate: float -- learning rate
    :param weight_decay: float -- weight decay
    :param plot_architecture: bool -- whether to plot network architecture

    """

    ###################
    # Data processing #
    ###################

    # the data, shuffled and split between train and test sets
    (X_train, y_train), (X_test, y_test) = cifar10.load_data()

    nb_classes = len(np.unique(y_train))
    img_dim = X_train.shape[1:]

    if K.image_data_format() == "channels_first":
        n_channels = X_train.shape[1]
    else:
        n_channels = X_train.shape[-1]

    # convert class vectors to binary class matrices
    Y_train = np_utils.to_categorical(y_train, nb_classes)
    Y_test = np_utils.to_categorical(y_test, nb_classes)

    X_train = X_train.astype('float32')
    X_test = X_test.astype('float32')

    # Normalisation
    X = np.vstack((X_train, X_test))
    # 2 cases depending on the image ordering
    if K.image_data_format() == "channels_first":
        for i in range(n_channels):
            mean = np.mean(X[:, i, :, :])
            std = np.std(X[:, i, :, :])
            X_train[:, i, :, :] = (X_train[:, i, :, :] - mean) / std
            X_test[:, i, :, :] = (X_test[:, i, :, :] - mean) / std

    elif K.image_data_format() == "channels_last":
        for i in range(n_channels):
            mean = np.mean(X[:, :, :, i])
            std = np.std(X[:, :, :, i])
            X_train[:, :, :, i] = (X_train[:, :, :, i] - mean) / std
            X_test[:, :, :, i] = (X_test[:, :, :, i] - mean) / std

    ###################
    # Construct model #
    ###################

    model = densenet.DenseNet(nb_classes,
                              img_dim,
                              depth,
                              nb_dense_block,
                              growth_rate,
                              nb_filter,
                              dropout_rate=dropout_rate,
                              weight_decay=weight_decay)
    # Model output
    model.summary()

    # Build optimizer
    # opt = Adam(lr=learning_rate, beta_1=0.9, beta_2=0.999, epsilon=1e-08)
    opt = SGD(lr=learning_rate, momentum=0.9, nesterov=True)

    model.compile(loss='categorical_crossentropy',
                  optimizer=opt,
                  metrics=["accuracy"])

    if plot_architecture:
        from keras.utils.visualize_util import plot
        plot(model, to_file='./figures/densenet_archi.png', show_shapes=True)


    ####################
    # Network training #
    ####################

    print("Training")

    list_train_loss = []
    list_test_loss = []
    list_learning_rate = []

    datagen = ImageDataGenerator()

    for e in range(nb_epoch):

        if e == int(0.5 * nb_epoch):
            K.set_value(model.optimizer.lr, np.float32(learning_rate / 10.))

        if e == int(0.75 * nb_epoch):
            K.set_value(model.optimizer.lr, np.float32(learning_rate / 100.))

        l_train_loss = []
        start = time.time()

        model.fit_generator(datagen.flow(X_train, Y_train, batch_size), epochs=1)

        test_logloss, test_acc = model.evaluate(X_test,
                                                Y_test,
                                                verbose=1,
                                                batch_size=64)
        list_test_loss.append([test_logloss, test_acc])
        list_learning_rate.append(float(K.get_value(model.optimizer.lr)))
        # to convert numpy array to json serializable
        print('Epoch %s/%s, Time: %s' % (e + 1, nb_epoch, time.time() - start))

        d_log = {}
        d_log["batch_size"] = batch_size
        d_log["nb_epoch"] = nb_epoch
        d_log["optimizer"] = opt.get_config()
        # d_log["train_loss"] = list_train_loss
        d_log["test_loss"] = list_test_loss
        d_log["learning_rate"] = list_learning_rate

        json_file = os.path.join('./log', logfile)
        with open(json_file, 'w') as fp:
            json.dump(d_log, fp, indent=4, sort_keys=True)
Exemple #39
0
import keras
from keras.datasets import cifar10
from keras.layers import Dense, Activation, Flatten, Conv2D, MaxPooling2D
from keras.models import Sequential
from keras.utils import to_categorical, np_utils
from PIL import Image
from keras.optimizers import SGD
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
import os
(train_X, train_Y), (test_X, test_Y) = cifar10.load_data()

train_X = train_X.reshape(-1, 32, 32, 3)
test_X = test_X.reshape(-1, 32, 32, 3)

train_X = train_X.astype('float32')
test_X = test_X.astype('float32')
train_X = train_X / 255
test_X = test_X / 255

train_Y_one_hot = to_categorical(train_Y, 10)
test_Y_one_hot = to_categorical(test_Y, 10)

model = Sequential()

model.add(Conv2D(64, (3, 3), activation='relu', input_shape=(32, 32, 3)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Flatten())
Exemple #40
0
    #    epochs = int(save_file.split('/')[4].split('_')[1].split('.')[0])
    starting_epoch = int(save_file.split('/')[4].split('.')[0].split('_')[-1])

data_augmentation = True
num_classes = 10

# Subtracting pixel mean improves accuracy
subtract_pixel_mean = True

n = 3

# Model name, depth and version
model_type = args.tc  #'P100_resnet50_he_256_1'

# Load the CIFAR10 data.
(x_train, y_train), (x_test, y_test) = cifar10.load_data()

# Normalize data.
x_train = x_train.astype('float32') / 255
x_test = x_test.astype('float32') / 255

# If subtract pixel mean is enabled
if subtract_pixel_mean:
    x_train_mean = np.mean(x_train, axis=0)
    x_train -= x_train_mean
    x_test -= x_train_mean

print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
print('y_train shape:', y_train.shape)
Exemple #41
0
    #Return the learning rate
    return float(alpha)


# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-o",
                "--output",
                required=True,
                help="path to the output loss/accuracy plot")
args = vars(ap.parse_args())

# load the training and testing data, then scale it into the
# range [0, 1]
print("[INFO] loading CIFAR-10 data...")
((trainX, trainY), (testX, testY)) = cifar10.load_data()
trainX = trainX.astype("float") / 255.0
testX = testX.astype("float") / 255.0

# convert the labels from integers to vectors
lb = LabelBinarizer()
trainY = lb.fit_transform(trainY)
testY = lb.transform(testY)

# initialize the label names for the CIFAR-10 dataset
labelNames = [
    "airplane", "automobile", "bird", "cat", "deer", "dog", "frog", "horse",
    "ship", "truck"
]

#Define the set of callbacks to be passed to
Exemple #42
0
def load_cifar10():
    (X_train, y_train), (X_test, y_test) = cifar10.load_data()
    X_train = normalize_minus1_1(cast_to_floatx(X_train))
    X_test = normalize_minus1_1(cast_to_floatx(X_test))
    return (X_train, y_train), (X_test, y_test)
Exemple #43
0
def CNN_conf(cfg, hist_save, epochs=1, test=False, gpu_no=0):
    verbose = 1  #CHRIS TODO set this to 0
    batch_size = 100
    num_classes = 10
    epochs = 2000  #CHRIS increased from 1 to 5 to make results less random and noisy
    data_augmentation = False
    num_predictions = 20
    logfile = 'mnist-cnn.log'
    savemodel = False

    # The data, shuffled and split between train and test sets:
    (x_train, y_train), (x_test,
                         y_test) = cifar10.load_data()  #mnist.load_data()

    #CHRIS reshape only needed for mnist
    #x_train = x_train.reshape(x_train.shape[0],x_train.shape[1],x_train.shape[2],1)
    #x_test = x_test.reshape(x_test.shape[0],x_test.shape[1],x_test.shape[2],1)

    cfg_df = pd.DataFrame(cfg, index=[0])

    # Convert class vectors to binary class matrices.
    y_train = keras.utils.to_categorical(y_train.flatten(), num_classes)
    y_test = keras.utils.to_categorical(y_test.flatten(), num_classes)

    #print('skip steps:')
    #print([cfg['skint_0'],cfg['skint_1'],cfg['skint_2']],[cfg['skst_0'],cfg['skst_1'],cfg['skst_2']])
    #(skip_ints,skip_ints_count) passed to Skip_manager constructor TODO get from cfg vector
    skip_manager = Skip_manager(
        [cfg['skint_0'], cfg['skint_1'], cfg['skint_2']],
        [cfg['skst_0'], cfg['skst_1'], cfg['skst_2']])

    input1 = keras.layers.Input(shape=(x_train.shape[1], x_train.shape[2],
                                       x_train.shape[3]))

    layer = Dropout(cfg['dropout_0'], input_shape=x_train.shape[1:])(input1)
    layer = skip_manager.connect_skip(layer)
    #CHRIS removed following:
    #layer = Conv2D(cfg['filters_0'], (cfg['k_0'], cfg['k_0']), padding='same',kernel_regularizer=l2(cfg['l2']), bias_regularizer=l2(cfg['l2']))(layer)
    #layer = Activation(cfg['activation'])(layer)#kernel_initializer='random_uniform',
    #layer = skip_manager.connect_skip(layer)

    #stack 0
    for i in range(cfg['stack_0']):
        layer = Conv2D(cfg['filters_0'], (cfg['k_0'], cfg['k_0']),
                       padding='same',
                       kernel_regularizer=l2(cfg['l2']),
                       bias_regularizer=l2(cfg['l2']))(layer)
        layer = Activation(cfg['activation'])(layer)
        layer = skip_manager.connect_skip(layer)
    if (cfg['stack_0'] > 0):
        #maxpooling as cnn
        if (cfg['no_pooling']):
            layer = Conv2D(cfg['filters_1'], (cfg['k_1'], cfg['k_1']),
                           strides=(cfg['s_0'], cfg['s_0']),
                           padding='same',
                           kernel_regularizer=l2(cfg['l2']),
                           bias_regularizer=l2(cfg['l2']))(layer)
        else:
            layer = MaxPooling2D(pool_size=(cfg['k_1'], cfg['k_1']),
                                 strides=(cfg['s_0'], cfg['s_0']),
                                 padding='same')(layer)
        layer = Activation(cfg['activation'])(layer)
        layer = Dropout(cfg['dropout_1'])(layer)
        layer = skip_manager.connect_skip(layer)

    #stack 1
    for i in range(cfg['stack_1']):
        layer = Conv2D(cfg['filters_2'], (cfg['k_2'], cfg['k_2']),
                       padding='same',
                       kernel_regularizer=l2(cfg['l2']),
                       bias_regularizer=l2(cfg['l2']))(layer)
        layer = Activation(cfg['activation'])(layer)
        layer = skip_manager.connect_skip(layer)
    if (cfg['stack_1'] > 0):
        if (cfg['no_pooling']):
            layer = Conv2D(cfg['filters_3'], (cfg['k_3'], cfg['k_3']),
                           strides=(cfg['s_1'], cfg['s_1']),
                           padding='same',
                           kernel_regularizer=l2(cfg['l2']),
                           bias_regularizer=l2(cfg['l2']))(layer)
        else:
            layer = MaxPooling2D(pool_size=(cfg['k_3'], cfg['k_3']),
                                 strides=(cfg['s_1'], cfg['s_1']),
                                 padding='same')(layer)
        layer = Activation(cfg['activation'])(layer)
        layer = Dropout(cfg['dropout_2'])(layer)
        layer = skip_manager.connect_skip(layer)

    #stack 2
    for i in range(cfg['stack_2']):
        layer = Conv2D(cfg['filters_4'], (cfg['k_4'], cfg['k_4']),
                       padding='same',
                       kernel_regularizer=l2(cfg['l2']),
                       bias_regularizer=l2(cfg['l2']))(layer)
        layer = Activation(cfg['activation'])(layer)
        layer = skip_manager.connect_skip(layer)
    if (cfg['stack_2'] > 0):
        if (cfg['no_pooling']):
            layer = Conv2D(cfg['filters_5'], (cfg['k_5'], cfg['k_5']),
                           strides=(cfg['s_2'], cfg['s_2']),
                           padding='same',
                           kernel_regularizer=l2(cfg['l2']),
                           bias_regularizer=l2(cfg['l2']))(layer)
        else:
            layer = MaxPooling2D(pool_size=(cfg['k_5'], cfg['k_5']),
                                 strides=(cfg['s_2'], cfg['s_2']),
                                 padding='same')(layer)
        layer = Activation(cfg['activation'])(layer)
        layer = Dropout(cfg['dropout_3'])(layer)
        layer = skip_manager.connect_skip(layer)

    #stack 3
    for i in range(cfg['stack_3']):
        layer = Conv2D(cfg['filters_6'], (cfg['k_6'], cfg['k_6']),
                       padding='same',
                       kernel_regularizer=l2(cfg['l2']),
                       bias_regularizer=l2(cfg['l2']))(layer)
        layer = Activation(cfg['activation'])(layer)
        layer = skip_manager.connect_skip(layer)
    if (cfg['stack_3'] > 0):
        if (cfg['no_pooling']):
            layer = Conv2D(cfg['filters_7'], (cfg['k_7'], cfg['k_7']),
                           strides=(cfg['s_3'], cfg['s_3']),
                           padding='same',
                           kernel_regularizer=l2(cfg['l2']),
                           bias_regularizer=l2(cfg['l2']))(layer)
        else:
            layer = MaxPooling2D(pool_size=(cfg['k_7'], cfg['k_7']),
                                 strides=(cfg['s_3'], cfg['s_3']),
                                 padding='same')(layer)
        layer = Activation(cfg['activation'])(layer)
        layer = Dropout(cfg['dropout_4'])(layer)
        layer = skip_manager.connect_skip(layer)

    #stack 4
    for i in range(cfg['stack_4']):
        layer = Conv2D(cfg['filters_8'], (cfg['k_8'], cfg['k_8']),
                       padding='same',
                       kernel_regularizer=l2(cfg['l2']),
                       bias_regularizer=l2(cfg['l2']))(layer)
        layer = Activation(cfg['activation'])(layer)
        layer = skip_manager.connect_skip(layer)
    if (cfg['stack_4'] > 0):
        if (cfg['no_pooling']):
            layer = Conv2D(cfg['filters_9'], (cfg['k_9'], cfg['k_9']),
                           strides=(cfg['s_4'], cfg['s_4']),
                           padding='same',
                           kernel_regularizer=l2(cfg['l2']),
                           bias_regularizer=l2(cfg['l2']))(layer)
        else:
            layer = MaxPooling2D(pool_size=(cfg['k_9'], cfg['k_9']),
                                 strides=(cfg['s_4'], cfg['s_4']),
                                 padding='same')(layer)
        layer = Activation(cfg['activation'])(layer)
        layer = Dropout(cfg['dropout_5'])(layer)
        layer = skip_manager.connect_skip(layer)

    #stack 5
    for i in range(cfg['stack_5']):
        layer = Conv2D(cfg['filters_10'], (cfg['k_10'], cfg['k_10']),
                       padding='same',
                       kernel_regularizer=l2(cfg['l2']),
                       bias_regularizer=l2(cfg['l2']))(layer)
        layer = Activation(cfg['activation'])(layer)
        layer = skip_manager.connect_skip(layer)
    if (cfg['stack_5'] > 0):
        if (cfg['no_pooling']):
            layer = Conv2D(cfg['filters_11'], (cfg['k_11'], cfg['k_11']),
                           strides=(cfg['s_5'], cfg['s_5']),
                           padding='same',
                           kernel_regularizer=l2(cfg['l2']),
                           bias_regularizer=l2(cfg['l2']))(layer)
        else:
            layer = MaxPooling2D(pool_size=(cfg['k_11'], cfg['k_11']),
                                 strides=(cfg['s_5'], cfg['s_5']),
                                 padding='same')(layer)
        layer = Activation(cfg['activation'])(layer)
        layer = Dropout(cfg['dropout_6'])(layer)
        layer = skip_manager.connect_skip(layer)

    #stack 6
    for i in range(cfg['stack_6']):
        layer = Conv2D(cfg['filters_12'], (cfg['k_12'], cfg['k_12']),
                       padding='same',
                       kernel_regularizer=l2(cfg['l2']),
                       bias_regularizer=l2(cfg['l2']))(layer)
        layer = Activation(cfg['activation'])(layer)
        layer = skip_manager.connect_skip(layer)
    if (cfg['stack_6'] > 0):
        if (cfg['no_pooling']):
            layer = Conv2D(cfg['filters_13'], (cfg['k_13'], cfg['k_13']),
                           strides=(cfg['s_6'], cfg['s_6']),
                           padding='same',
                           kernel_regularizer=l2(cfg['l2']),
                           bias_regularizer=l2(cfg['l2']))(layer)
        else:
            layer = MaxPooling2D(pool_size=(cfg['k_13'], cfg['k_13']),
                                 strides=(cfg['s_6'], cfg['s_6']),
                                 padding='same')(layer)
        layer = Activation(cfg['activation'])(layer)
        layer = Dropout(cfg['dropout_7'])(layer)
        layer = skip_manager.connect_skip(layer)

    #global averaging
    if (cfg['global_pooling']):
        layer = GlobalAveragePooling2D()(layer)
    else:
        layer = Flatten()(layer)

    #head
    if cfg['dense_size_0'] > 0:
        layer = Dense(cfg['dense_size_0'],
                      kernel_regularizer=l2(cfg['l2']),
                      bias_regularizer=l2(cfg['l2']))(layer)
        layer = Activation(cfg['activ_dense'])(layer)
    if cfg['dense_size_1'] > 0:
        layer = Dense(cfg['dense_size_1'],
                      kernel_regularizer=l2(cfg['l2']),
                      bias_regularizer=l2(cfg['l2']))(layer)
        layer = Activation(cfg['activ_dense'])(layer)
    layer = Dense(num_classes,
                  kernel_regularizer=l2(cfg['l2']),
                  bias_regularizer=l2(cfg['l2']))(layer)
    layer = Activation(cfg['activ_dense'])(layer)

    cfg['decay'] = cfg['lr'] / float(epochs)

    def step_decay(epoch):
        initial_lrate = cfg['lr']
        drop = 0.1
        epochs_drop = 20.0
        lrate = initial_lrate * math.pow(drop,
                                         math.floor((1 + epoch) / epochs_drop))
        return lrate

    callbacks = []
    if (cfg['step'] == True):
        callbacks = [LearningRateScheduler(step_decay)]
        cfg['decay'] = 0.

    # initiate RMSprop optimizer
    #opt = keras.optimizers.rmsprop(lr= cfg['lr'], decay=cfg['decay'])
    opt = keras.optimizers.SGD(lr=cfg['lr'],
                               momentum=0.9,
                               decay=cfg['decay'],
                               nesterov=False)

    model = keras.models.Model(inputs=input1, outputs=layer)

    # Let's train the model using RMSprop
    model.compile(loss='categorical_crossentropy',
                  optimizer=opt,
                  metrics=['accuracy'])

    if test:
        return model  #TODO remove this, just for testing

    #print("amount of parameters:")
    #print(model.count_params())
    #CHRIS test if gpu has enough memory
    nvmlInit()
    handle = nvmlDeviceGetHandleByIndex(int(gpu_no))
    meminfo = nvmlDeviceGetMemoryInfo(handle)
    #max_size = meminfo.total #6689341440
    if meminfo.free / 1024.**2 < 1.0:
        print('gpu is allready in use')
    nvmlShutdown()
    #if model.count_params()*4*2 >= max_size:#CHRIS *4*2: 4 byte per parameter times 2 for backpropagation
    #print('network too large for memory')
    #return 1000000000.0*(model.count_params()*4*2/max_size), 5.0*(model.count_params()*4*2/max_size)

    #max_size = 32828802 * 2 #CHRIS twice as large as RESnet-34-like implementation
    #max_size = 129200130 #CHRIS twice as wide as RESnet-34-like implementation with batchsize=10, one network of this size was able to be ran on tritanium gpu
    max_size = 130374394  #CHRIS twice as wide as RESnet-34-like implementation with batchsize=100, one network of this size was able to be ran on tritanium gpu
    #if model.count_params() > max_size:
    #print('network too large for implementation')
    #return 1000000000.0*(model.count_params()/max_size), 5.0*(model.count_params()/max_size)
    x_train = x_train.astype('float32')
    x_test = x_test.astype('float32')
    x_train /= 255.
    x_test /= 255.

    hist_func = TimedAccHistory()

    if not data_augmentation:
        print('Not using data augmentation.')
        start = time.time()
        hist = model.fit(x_train,
                         y_train,
                         batch_size=batch_size,
                         epochs=epochs,
                         validation_data=(x_test, y_test),
                         callbacks=[hist_func],
                         verbose=verbose,
                         shuffle=True)
        stop = time.time()
    else:
        print('Using real-time data augmentation.')
        # This will do preprocessing and realtime data augmentation:
        datagen = ImageDataGenerator(
            featurewise_center=False,  # set input mean to 0 over the dataset
            samplewise_center=False,  # set each sample mean to 0
            featurewise_std_normalization=
            False,  # divide inputs by std of the dataset
            samplewise_std_normalization=False,  # divide each input by its std
            zca_whitening=False,  # apply ZCA whitening
            rotation_range=
            0,  # randomly rotate images in the range (degrees, 0 to 180)
            width_shift_range=
            0.1,  # randomly shift images horizontally (fraction of total width)
            height_shift_range=
            0.1,  # randomly shift images vertically (fraction of total height)
            horizontal_flip=True,  # randomly flip images
            vertical_flip=False)  # randomly flip images
        datagen.fit(x_train)

        # Fit the model on the batches generated by datagen.flow().
        start = time.time()
        hist = model.fit_generator(datagen.flow(x_train,
                                                y_train,
                                                batch_size=batch_size),
                                   verbose=verbose,
                                   callbacks=callbacks,
                                   epochs=epochs,
                                   steps_per_epoch=len(x_train) / batch_size,
                                   validation_data=(x_test, y_test))
        stop = time.time()

    timer = stop - start
    #print('run-time:')
    #print(timer)
    hist_save.append([hist.history['val_acc'], hist_func.timed])

    if savemodel:
        model.save('best_model_mnist.h5')
    maxval = max(hist.history['val_acc'])
    #loss = -1 * math.log( 1.0 - max(hist.history['val_acc']) ) #np.amin(hist.history['val_loss'])
    loss = -1 * math.log(max(hist.history['val_acc'])
                         )  #CHRIS minimizing this will maximize accuracy
    #print('max val_acc:')
    #print(max(hist.history['val_acc']))
    #print('loss:')
    #print(loss)
    #perf5 = max(hist.history['val_top_5_categorical_accuracy'])

    if logfile is not None:
        log_file = logfile  #os.path.join(data_des, logfile)
        cfg_df['perf'] = maxval

        # save the configurations to log file
        if os.path.isfile(log_file):
            cfg_df.to_csv(log_file, mode='a', header=False, index=False)
        else:
            cfg_df.to_csv(log_file, mode='w', header=True, index=False)
    return timer, loss
from keras.datasets import cifar10
import matplotlib.pyplot as plt
import keras.utils as utils
import numpy as np


def reshape_image(input_image_arrays):
    output_array = []
    for image_array in input_image_arrays:
        output_array.append(image_array.reshape(-1))
    return np.asarray(output_array)


(train_images, train_labels), (test_images, test_labels) = cifar10.load_data()

first_image = train_images[4]

# print(first_image[0])
# plt.imshow(first_image)
# plt.show()

labels_array = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']

train_labels = utils.to_categorical(train_labels)
test_labels = utils.to_categorical(test_labels)
first_label_index = train_labels[0][0]

max_index = np.argmax(train_labels[0])
max_index = np.argmax(train_labels[0])
print(labels_array[max_index])
from keras.layers import Dense, Activation, Dropout
from keras.callbacks import TensorBoard
from time import strftime
from sklearn.metrics import confusion_matrix

# CONSTANTS
LABEL_Names = ['Plane', 'Car', 'Bird', 'Cat', 'Deer', 'Dog', 'Frog', 'Horse', 'Ship', 'Truck']
IMG_Width = 32
IMG_Height = 32
IMG_Pix = IMG_Height*IMG_Width
COLOR_Channels = 3 #RGB
TOTAL_Inputs =IMG_Pix * COLOR_Channels
VALIDATION_Size = 10000
LOG_Dir = 'tensorboard_cifar_logs/' #there are folder with logs from learning

(x_train_all, y_train_all),(x_test, y_test) = cifar10.load_data()
pic = array_to_img(x_train_all[7])

#pic.show()
#print(y_train_all.shape)
#print(y_train_all[7][0])
#print(LABEL_NAMES[y_train_all[7][0]])
#plt.imshow(x_train_all[4])
#plt.xlabel(LABEL_NAMES[y_train_all[4][0]])
#plt.show()

#showing images
#for i in range (10):
    #print(LABEL_NAMES[y_train_all[i][0]])
    #plt.imshow(x_train_all[i])
    #plt.xlabel(LABEL_NAMES[y_train_all[i][0]])
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Conv2D, MaxPooling2D

import os

batch_size = 32
num_classes = 10
epochs = 100
num_predictions = 20
save_dir = os.path.join(os.getcwd(), 'saved_models')
model_name = 'keras_cifar10_trained_model.h5'

# The data, split between train and test sets:
(x_tr, y_tr), (x_test, y_test) = cifar10.load_data()
print('x_train shape:', x_tr.shape)
print(x_tr.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')

# Convert class vectors to binary class matrices.
y_tr = keras.utils.to_categorical(y_tr, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)

convNetModel = Sequential()
convNetModel.add(Conv2D(32, (3, 3), padding='same',
                        input_shape=x_tr.shape[1:]))
convNetModel.add(Activation('relu'))
convNetModel.add(Conv2D(32, (3, 3)))
convNetModel.add(Activation('relu'))
convNetModel.add(MaxPooling2D(pool_size=(2, 2)))
Exemple #47
0
        percent = self.i * 100.0 / self.max_steps  # 计算完成进度,格式为xx.xx%
        process_bar = '\r[' + '>' * num_arrow + '-' * num_line + ']' \
                      + '%.2f' % percent + '%  ' + info  # 带输出的字符串,'\r'表示不换行回到最左边
        print(process_bar, end='')  # 这两句打印字符到终端
        if self.i > self.max_steps:
            self.close()

    def close(self):
        print("\n")
        self.i = 0


batch_size = 128
import numpy as np

(x_train, y_train), (x_test, y_test) = load_data()  # 50000, 32,32,3
y_train = np.array(keras.utils.to_categorical(y_train, 10).tolist())
y_test = np.array(keras.utils.to_categorical(y_test, 10).tolist())
x_train, x_test = color_preprocessing(x_train, x_test)
x = tf.placeholder(tf.float32, [None, 32, 32, 3])
y = tf.placeholder(tf.float32, [None, 10])

with slim.arg_scope(resnet_arg_scope(is_training=True)):
    net, end_points = resnet_v2_32(x, 10)  # 分辨率会缩小32倍

loss = tf.reduce_mean(
    tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=net))
update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
global_step = tf.Variable(0, trainable=False)
learning_rate = tf.train.exponential_decay(0.1, global_step, 4000000, 0.1)
with tf.control_dependencies(update_ops):
Exemple #48
0
def run(epochs, batch_size, lr, gpus, data_augmentation, cpu_relocation,
        cpu_merge):
    time_reporter = StepTimeReporter()

    # img_arr is of shape (n, h, w, c)
    def resize_image_arr(img_arr):
        x_resized_list = []
        for i in range(img_arr.shape[0]):
            img = img_arr[i]
            resized_img = resize(img, (224, 224))
            x_resized_list.append(resized_img)
        return np.stack(x_resized_list)

    (x_train, y_train), (x_test, y_test) = cifar10.load_data()

    x_train = x_train[:2048]
    y_train = y_train[:2048]
    x_test = x_test[:2048]
    y_test = y_test[:2048]

    print("Resizing images")

    # Resize image arrays
    x_train = resize_image_arr(x_train)
    x_test = resize_image_arr(x_test)

    NUM_CLASSES = 10

    # Convert class vectors to binary class matrices.
    y_train = keras.utils.to_categorical(y_train, NUM_CLASSES)
    y_test = keras.utils.to_categorical(y_test, NUM_CLASSES)

    # Normalize the data
    x_train = x_train.astype('float32')
    x_test = x_test.astype('float32')
    x_train /= 255
    x_test /= 255

    model = ResNet50(include_top=True, weights=None, classes=10)

    STEPS = 2

    if random.choice([True, False]):
        opt = keras.optimizers.SGD(lr=lr)
        opt = runai.ga.keras.optimizers.Optimizer(opt, STEPS)
    else:
        opt = runai.ga.keras.optimizers.SGD(STEPS, lr=lr)

    opt = hvd.DistributedOptimizer(opt)

    # Not needed to change the device scope for model definition:

    model.compile(loss='categorical_crossentropy',
                  optimizer=opt,
                  metrics=['accuracy'])

    if not data_augmentation:
        model.fit(x_train,
                  y_train,
                  batch_size=batch_size,
                  epochs=epochs,
                  validation_data=(x_test, y_test),
                  shuffle=False)
    else:
        datagen = ImageDataGenerator(
            featurewise_center=False,  # set input mean to 0 over the dataset
            samplewise_center=False,  # set each sample mean to 0
            featurewise_std_normalization=
            False,  # divide inputs by std of the dataset
            samplewise_std_normalization=False,  # divide each input by its std
            zca_whitening=False,  # apply ZCA whitening
            rotation_range=
            0,  # randomly rotate images in the range (degrees, 0 to 180)
            width_shift_range=
            0,  # randomly shift images horizontally (fraction of total width)
            height_shift_range=
            0,  # randomly shift images vertically (fraction of total height)
            horizontal_flip=True,  # randomly flip images
            vertical_flip=False)  # randomly flip images

        # Compute quantities required for feature-wise normalization
        # (std, mean, and principal components if ZCA whitening is applied).
        datagen.fit(x_train)

        # Fit the model on the batches generated by datagen.flow().
        model.fit_generator(
            datagen.flow(x_train, y_train, batch_size=batch_size),
            steps_per_epoch=int(10 * x_train.shape[0] / batch_size),
            epochs=epochs,
            validation_data=(x_test, y_test),
            workers=1,
            callbacks=[
                time_reporter,
                hvd.callbacks.BroadcastGlobalVariablesCallback(0)
            ])

    print("BYE BYE")
def train(argv=None):
    ''' train conditional Gated PixelCNN model 
    Usage:
    	python train_keras_datasets.py -c sample_train.cfg        : training example using configfile
    	python train_keras_datasets.py --option1 hoge ...         : train with command-line options
        python train_keras_datasets.py -c test.cfg --opt1 hoge... : overwrite config options with command-line options
    '''

    ### parsing arguments from command-line or config-file ###
    if argv is None:
        argv = sys.argv

    conf_parser = argparse.ArgumentParser(
        description=__doc__,  # printed with -h/--help
        formatter_class=argparse.RawDescriptionHelpFormatter,
        add_help=False)
    conf_parser.add_argument("-c",
                             "--conf_file",
                             help="Specify config file",
                             metavar="FILE_PATH")
    args, remaining_argv = conf_parser.parse_known_args()

    defaults = {}

    if args.conf_file:
        config = configparser.SafeConfigParser()
        config.read([args.conf_file])
        defaults.update(dict(config.items("General")))

    parser = argparse.ArgumentParser(parents=[conf_parser])
    parser.set_defaults(**defaults)
    parser.add_argument("--nb_epoch",
                        help="Number of epochs [Required]",
                        type=int,
                        metavar="INT")
    parser.add_argument("--batch_size",
                        help="Minibatch size",
                        type=int,
                        metavar="INT")
    parser.add_argument(
        "--conditional",
        help="model the conditional distribution p(x|h) (default:False)",
        type=str,
        metavar="BOOL")
    parser.add_argument("--dataset_name",
                        help="{'mnist', 'cifar10', 'cifar100'}",
                        type=str,
                        metavar="DATASET_NAME")
    parser.add_argument(
        "--nb_pixelcnn_layers",
        help="Number of PixelCNN Layers (exept last two ReLu layers)",
        metavar="INT")
    parser.add_argument("--nb_filters",
                        help="Number of filters for each layer",
                        metavar="INT")
    parser.add_argument(
        "--filter_size_1st",
        help="Filter size for the first layer. (default: (7,7))",
        metavar="INT,INT")
    parser.add_argument(
        "--filter_size",
        help="Filter size for the subsequent layers. (default: (3,3))",
        metavar="INT,INT")
    parser.add_argument("--optimizer",
                        help="SGD optimizer (default: adadelta)",
                        type=str,
                        metavar="OPT_NAME")
    parser.add_argument("--es_patience",
                        help="Patience parameter for EarlyStopping",
                        type=int,
                        metavar="INT")
    parser.add_argument(
        "--save_root",
        help=
        "Root directory which trained files are saved (default: /tmp/pixelcnn)",
        type=str,
        metavar="DIR_PATH")
    parser.add_argument(
        "--timezone",
        help=
        "Trained files are saved in save_root/YYYYMMDDHHMMSS/ (default: Asia/Tokyo)",
        type=str,
        metavar="REGION_NAME")
    parser.add_argument(
        "--save_best_only",
        help="The latest best model will not be overwritten (default: False)",
        type=str,
        metavar="BOOL")
    parser.add_argument("--plot_model",
                        help="If True, plot a Keras model (using graphviz)",
                        type=str,
                        metavar="BOOL")

    args = parser.parse_args(remaining_argv)

    conditional = strtobool(args.conditional) if args.conditional else False
    try:
        dataset_name = args.dataset_name
    except:
        sys.exit("Error: --dataset_name must be specified.")

    ### load keras dataset ###
    if dataset_name == 'mnist':
        from keras.datasets import mnist
        (X_train, h_train), (X_validation, h_validation) = mnist.load_data()
        input_size = (28, 28)
        nb_classes = 10
        nb_channels = 1
    elif dataset_name == 'cifar10':
        from keras.datasets import cifar10
        (X_train, h_train), (X_validation, h_validation) = cifar10.load_data()
        input_size = (32, 32)
        nb_classes = 10
        nb_channels = 3
    elif dataset_NAME == 'cifar100':
        from keras.datasets import cifar100
        (X_train, h_train), (X_validation, h_validation) = cifar100.load_data()
        input_size = (32, 32)
        nb_classes = 100
        nb_channels = 3

    utils = Utils()

    ### build PixelCNN model ###
    model_params = {}
    model_params['input_size'] = input_size
    model_params['nb_channels'] = nb_channels
    model_params['conditional'] = conditional
    if conditional:
        model_params['latent_dim'] = nb_classes
    if args.nb_pixelcnn_layers:
        model_params['nb_pixelcnn_layers'] = int(args.nb_pixelcnn_layers)
    if args.nb_filters:
        model_params['nb_filters'] = int(args.nb_filters)
    if args.filter_size_1st:
        model_params['filter_size_1st'] = tuple(
            map(int, args.filter_size_1st.split(',')))
    if args.filter_size:
        model_params['filter_size'] = tuple(
            map(int, args.filter_size.split(',')))
    if args.optimizer:
        model_params['optimizer'] = args.optimizer
    if args.es_patience:
        model_params['es_patience'] = int(args.patience)
    if args.save_best_only:
        model_params['save_best_only'] = strtobool(args.save_best_only)

    save_root = args.save_root if args.save_root else '/tmp/pixelcnn_' + dataset_name
    timezone = args.timezone if args.timezone else 'Asia/Tokyo'
    current_datetime = datetime.now(
        pytz.timezone(timezone)).strftime('%Y%m%d_%H%M%S')
    save_root = os.path.join(save_root, current_datetime)
    model_params['save_root'] = save_root

    if not os.path.exists(save_root):
        os.makedirs(save_root)

    try:
        nb_epoch = int(args.nb_epoch)
        batch_size = int(args.batch_size)
    except:
        sys.exit("Error: {--nb_epoch, --batch_size} must be specified.")

    pixelcnn = PixelCNN(**model_params)
    pixelcnn.build_model()
    pixelcnn.model.summary()

    pixelcnn.print_train_parameters(save_root)
    pixelcnn.export_train_parameters(save_root)
    with open(os.path.join(save_root, 'parameters.txt'), 'a') as txt_file:
        txt_file.write('########## other options ##########\n')
        txt_file.write('nb_epoch\t: %s\n' % nb_epoch)
        txt_file.write('batch_size\t: %s\n' % batch_size)
        txt_file.write('\n')
    plot_model = strtobool(args.plot_model) if args.plot_model else True
    if plot_model:
        plot(pixelcnn.model,
             to_file=os.path.join(save_root, 'pixelcnn_model.png'))

    if conditional:
        train_generator = utils.build_data_generator_from_keras_datasets(
            dataset_name, X_train, h_train, batch_size)
        validation_generator = utils.build_data_generator_from_keras_datasets(
            dataset_name, X_validation, h_validation, batch_size)
    else:
        train_generator = utils.build_data_generator_from_keras_datasets(
            dataset_name, X_train, None, batch_size)
        validation_generator = utils.build_data_generator_from_keras_datasets(
            dataset_name, X_validation, None, batch_size)

    nb_train_samples = len(X_train)
    nb_validation_samples = len(X_validation)

    pixelcnn.fit_generator(train_generator=train_generator,
                           samples_per_epoch=nb_train_samples,
                           nb_epoch=nb_epoch,
                           validation_data=validation_generator,
                           nb_val_samples=nb_validation_samples)

    return (0)
Aprendizagem não supervisionada
Redução de dimensionalidade - Tarefa
Base de dados CIFAR10
Construção de um autoencoder para redimensionamento, codificação e decodificação de imagens

"""

import numpy as np
import matplotlib.pyplot as plt
from keras.datasets import cifar10
from keras.models import Model, Sequential
from keras.layers import Input, Dense
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

(previsores_treinamento, _), (previsores_teste, _) = cifar10.load_data()

previsores_treinamento = previsores_treinamento.astype('float32') / 255
previsores_teste = previsores_teste.astype('float32') / 255

previsores_treinamento = previsores_treinamento.reshape(
    (len(previsores_treinamento), np.prod(previsores_treinamento.shape[1:])))
previsores_teste = previsores_teste.reshape(
    (len(previsores_teste), np.prod(previsores_teste.shape[1:])))
""" Algoritmo para o treinamento """
# 3072 - 1536 - 768 - 1536 - 3072
autoencoder = Sequential()

# Encode
autoencoder.add(Dense(units=1536, activation='relu', input_dim=3072))
autoencoder.add(Dense(units=768, activation='relu'))
Exemple #51
0
def main():
    args = parse_args()

    if args.name is None:
        args.name = 'WideResNet%s-%s' % (args.depth, args.width)
        if args.scheduler == 'CosineAnnealing':
            args.name += '_wCosineAnnealing'

    if not os.path.exists('models/%s' % args.name):
        os.makedirs('models/%s' % args.name)

    print('Config -----')
    for arg in vars(args):
        print('%s: %s' % (arg, getattr(args, arg)))
    print('------------')

    with open('models/%s/args.txt' % args.name, 'w') as f:
        for arg in vars(args):
            print('%s: %s' % (arg, getattr(args, arg)), file=f)

    joblib.dump(args, 'models/%s/args.pkl' % args.name)

    # create model
    model = WideResNet(args.depth, args.width, num_classes=10)
    model.compile(loss='categorical_crossentropy',
                  optimizer=SGD(lr=0.1, momentum=0.9),
                  metrics=['accuracy'])

    (x_train, y_train), (x_test, y_test) = cifar10.load_data()
    x_train = x_train.astype('float32') / 255
    x_test = x_test.astype('float32') / 255

    x_train = standardize(x_train)
    x_test = standardize(x_test)

    datagen = ImageDataGenerator(width_shift_range=0.1,
                                 height_shift_range=0.1,
                                 horizontal_flip=True)

    y_train = keras.utils.to_categorical(y_train, 10)
    y_test = keras.utils.to_categorical(y_test, 10)

    callbacks = [
        ModelCheckpoint('models/%s/model.hdf5' % args.name,
                        verbose=1,
                        save_best_only=True),
        CSVLogger('models/%s/log.csv' % args.name)
    ]

    if args.scheduler == 'CosineAnnealing':
        callbacks.append(
            CosineAnnealingScheduler(T_max=args.epochs,
                                     eta_max=0.05,
                                     eta_min=4e-4))
    else:
        callbacks.append(LearningRateScheduler(adjust_learning_rate))

    model.fit_generator(datagen.flow(x_train,
                                     y_train,
                                     batch_size=args.batch_size),
                        steps_per_epoch=len(x_train) // args.batch_size,
                        validation_data=(x_test, y_test),
                        epochs=args.epochs,
                        verbose=1,
                        workers=4,
                        callbacks=callbacks)

    scores = model.evaluate(x_test, y_test, verbose=1)
    print('Test loss:', scores[0])
    print('Test accuracy:', scores[1])
Exemple #52
0
    def train(self, epochs, batch_size=128, save_interval=50):

        # Charger les données
        (X_train, y_train), (_, _) = cifar10.load_data()
        X_train = np.array(
            X_train[np.argwhere(y_train.squeeze() == 5)].squeeze())

        # Redimensionnement de -1 à 1
        X_train = X_train / 127.5 - 1.

        if self.channels == 1:
            X_train = np.expand_dims(
                X_train, axis=3
            )  # S'il y a un seul channel, il faut expand les dimensions de l'ensemble d'entraînement

        # Adversarial ground truths
        valid = np.ones((batch_size, 1))
        fake = np.zeros((batch_size, 1))

        f = open(self.savePath + "data.csv", "a+")
        f.write(
            "Time, Epoch, DiscriminatorLoss, DiscriminatorAcc, GeneratorLoss\n"
        )
        f.close()

        debut = time.time()

        for epoch in range(epochs):

            # ----------------------------
            #  Entraînement Discriminateur
            # ----------------------------

            # Choisir une batch aléatoire d'images
            idx = np.random.randint(0, X_train.shape[0], batch_size)
            imgs = X_train[idx]

            # Échantillonner le bruit et générer une batch de nouvelles images
            noise = np.random.normal(0, 1, (batch_size, self.latent_dim))
            gen_imgs = self.generator.predict(noise)

            # Entraîner le discriminateur (vrai classifié comme 1 et généré comme 0)
            d_loss_real = self.discriminator.train_on_batch(imgs, valid)
            d_loss_fake = self.discriminator.train_on_batch(gen_imgs, fake)
            d_loss = 0.5 * np.add(d_loss_real, d_loss_fake)

            # ------------------------
            #  Entraînement Générateur
            # ------------------------

            # Entraîner le générateur (pour que le discriminateur n'arrive pas à discerner les fausses images)
            g_loss = self.combined.train_on_batch(noise, valid)

            # Progression
            if epoch % 10 == 0:
                delta = time.time() - debut
                f = open(self.savePath + "data.csv", "a+")
                print("%d [D loss: %f, acc.: %.2f%%] [G loss: %f]" %
                      (epoch, d_loss[0], 100 * d_loss[1], g_loss))
                f.write("%.3f, %d, %f, %.2f, %f\n" %
                        (delta, epoch, d_loss[0], 100 * d_loss[1], g_loss))
                f.close()

            # Selon l'intervalle de sauvegarde, on sauvegarde les images générées
            if epoch % save_interval == 0:
                self.save_imgs(epoch)
Exemple #53
0
def process_model(model,
                  dataset,
                  normalise,
                  weight_prune,
                  relu_prune,
                  do_eval,
                  add_svm_dists,
                  output_path,
                  epsilon,
                  weight_prune_threshold=1e-3,
                  relu_prune_threshold=0.05,
                  validation_size=5000):
    if dataset == "CIFAR10":
        (x_train, y_train), _ = cifar10.load_data()
        y_train = y_train.reshape(-1)
        num_classes = 10
    elif dataset == "MNIST":
        (x_train, y_train), _ = mnist.load_data()
        x_train = np.expand_dims(x_train, axis=-1)
        num_classes = 10
    elif dataset == "FASHION_MNIST":
        (x_train, y_train), _ = fashion_mnist.load_data()
        x_train = np.expand_dims(x_train, axis=-1)
        num_classes = 10
    else:
        raise ValueError("Unrecognised dataset")

    # Leave aside a validation set
    x_valid = x_train[-validation_size:]
    y_valid = y_train[-validation_size:]
    x_train = x_train[:-validation_size]
    y_train = y_train[:-validation_size]

    # Normalize data
    x_train = x_train.astype("float32") / 255
    x_valid = x_valid.astype("float32") / 255

    if normalise:
        mean = x_train.mean(axis=(0, 1, 2))
        std = x_train.std(axis=(0, 1, 2)) + 1e-8
        print("Normalising channels with values", mean, std)
        input_shape = x_train.shape[1:]
        normalise_input = Input(shape=input_shape, name="input_for_normalise")
        normalise_layer = Lambda(lambda x:
                                 (x - K.constant(mean)) / K.constant(std))
        normalise_layer.mean = mean
        normalise_layer.std = std
        output = normalise_layer(normalise_input)
        for l in model._layers[1:]:
            output = l(output)
        model = Model(inputs=normalise_input, outputs=output)
        model.compile(loss="sparse_categorical_crossentropy",
                      optimizer=Adam(lr=1e-4),
                      metrics=["accuracy"])

    if do_eval:
        evaluate(model, x_valid, y_valid)
    if weight_prune:
        prune_small_weights(model, weight_prune_threshold)
        if do_eval:
            evaluate(model, x_valid, y_valid)
    if relu_prune:
        relu_pruned_model, layer_ops = prune_relus(model, relu_prune_threshold,
                                                   x_train, y_train, epsilon)
        if do_eval:
            evaluate(relu_pruned_model, x_valid, y_valid)
    if add_svm_dists:
        model = add_svm_distances(model,
                                  x_train,
                                  y_train,
                                  num_classes=num_classes)

    # Save processed model

    layer_config = []
    weights_to_save = {}

    relu_layers = [
        l for l in model._layers if isinstance(l, ReLU) or
        (isinstance(l, Activation) and l.activation == activations.get("relu"))
    ]

    for i, layer in enumerate(model._layers):
        input_layers = []
        for n in layer._inbound_nodes:
            input_layers.extend(n.inbound_layers)
        # input_indices = [model._layers.index(l) for l in input_layers]
        input_indices = [model._layers.index(input_layers[-1])
                         ] if len(input_layers) > 0 else []

        if isinstance(layer, InputLayer):
            layer_config.append({"type": "Input"})

        elif isinstance(layer, Add):
            assert (len(input_indices) == 2)
            layer_config.append({"type": "Add"})

        elif isinstance(layer, AveragePooling2D):
            assert (len(input_indices) == 1)
            layer_config.append({
                "type": "AveragePool",
                "pool_size": layer.pool_size
            })

        elif isinstance(layer, BatchNormalization):
            assert (len(input_indices) == 1)
            layer_id = "Normalization_{}".format(i)
            [gamma, beta, mm, mv] = layer.get_weights()
            eps = layer.epsilon
            weights_to_save[layer_id + "/mean"] = mm
            weights_to_save[layer_id + "/std"] = np.sqrt(mv + eps)
            weights_to_save[layer_id + "/gamma"] = gamma
            weights_to_save[layer_id + "/beta"] = beta
            layer_config.append({"type": "Normalization"})

        elif isinstance(layer, Conv2D):
            assert (len(input_indices) == 1)
            layer_id = "Conv2D_{}".format(i)
            [weights, bias] = layer.get_weights()
            weights_to_save[layer_id + "/weight"] = weights
            weights_to_save[layer_id + "/bias"] = bias
            layer_config.append({
                "type": "Conv2D",
                "weight_shape": weights.shape,
                "stride": layer.strides,
                "padding": layer.padding
            })

        elif isinstance(layer, Dense):
            assert (len(input_indices) == 1)
            layer_id = "FullyConnected_{}".format(i)
            [weights, bias] = layer.get_weights()
            weights_to_save[layer_id + "/weight"] = weights
            weights_to_save[layer_id + "/bias"] = bias
            layer_config.append({
                "type": "FullyConnected",
                "weight_shape": weights.shape
            })

        elif isinstance(layer, Flatten):
            assert (len(input_indices) == 1)
            layer_config.append({"type": "Flatten"})

        elif isinstance(layer, MaxPooling2D):
            assert (len(input_indices) == 1)
            layer_config.append({
                "type": "MaxPool",
                "pool_size": layer.pool_size
            })

        elif isinstance(
                layer,
                Lambda) and layer.mean is not None and layer.std is not None:
            assert (len(input_indices) == 1)
            layer_id = "Normalization_{}".format(i)
            mean, std = layer.mean, layer.std
            weights_to_save[layer_id + "/mean"] = mean
            weights_to_save[layer_id + "/std"] = std
            weights_to_save[layer_id + "/gamma"] = np.ones_like(mean)
            weights_to_save[layer_id + "/beta"] = np.zeros_like(mean)
            layer_config.append({"type": "Normalization"})

        elif layer in relu_layers:
            assert (len(input_indices) == 1)
            if relu_prune:
                layer_type = "MaskedRelu"
                layer_id = "MaskedRelu_{}".format(i)
                index = relu_layers.index(layer)
                ops = layer_ops[index]
                weights_to_save[layer_id + "/mask"] = layer_ops[index]
            else:
                layer_type = "Relu"
            layer_config.append({"type": layer_type})

        elif isinstance(layer, Softmax) and i == len(model._layers) - 1:
            pass

        else:
            raise ValueError("Unsupported layer")

        layer_config[-1]["input_indices"] = input_indices

    output_config_path = str(output_path) + "__config.json"
    output_weights_path = str(output_path) + "__weights.mat"

    with open(str(output_config_path), "w") as outfile:
        json.dump(layer_config, outfile)
    sio.savemat(str(output_weights_path), weights_to_save)

    print("Saved processed model in:")
    print("    " + str(output_config_path))
    print("    " + str(output_weights_path))
Exemple #54
0
import matplotlib.pyplot as plt
import time

# Some hyperparameters
num_epcohs = 500
lr_discrim = 0.0002
beta_1_discrim = 0.5
lr_gen = 0.0002
beta_1_gen = 0.5
batch_size = 100
z_dim = 100
CONTINUE_TRAINING = True
start_point = 0

# load the parameter
(train_X, _), (_, _) = cifar10.load_data()

# the data configuration
img_size_cifar = 32
num_channels_cifar = 3
img_size_flat_cifar = img_size_cifar * img_size_cifar * num_channels_cifar
img_shape_cifar = (img_size_cifar, img_size_cifar, num_channels_cifar)
num_classes_cifar = 10

# process the data
train_X = train_X / 255
train_X = train_X * 2 - 1

kernel_init = tf.truncated_normal_initializer(mean=0.0, stddev=0.02)

Exemple #55
0
import numpy
from keras.datasets import cifar10
import numpy as np
np.random.seed(10)
import time
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

t=time.clock()

(x_img_train,y_label_train), \
(x_img_test, y_label_test)=cifar10.load_data()

label_dict={0:"airplane",1:"automobile",2:"bird",3:"cat",4:"deer",
            5:"dog",6:"frog",7:"horse",8:"ship",9:"truck"}

import matplotlib.pyplot as plt


def plot_images_labels_prediction(images, labels, prediction,
                                  idx, num=10):
    fig = plt.gcf()
    fig.set_size_inches(12, 14)
    if num > 16: num = 16
    for i in range(0, num):
        ax = plt.subplot(4, 4, 1 + i)
        ax.imshow(images[idx], cmap='binary')

        title = str(i) + ',' + label_dict[labels[i][0]]
        if len(prediction) > 0:
            title += '=>' + label_dict[prediction[i]]
Exemple #56
0
        self.ytr = y

    def predict(self, X):
        """X = N x D where each row is an example we wish to predict label for"""
        num_test = X.shape[0]
        Ypred = np.zeros(
            num_test, dtype=ytr.dtype
        )  # lets make sure the output type matches the input type

        # loop over all test rows
        for i in range(num_test):
            # find the nearest training image to the i'th test image
            # using the L1 distance (sum of absolute value differences)
            distances = np.sum(np.abs(self.Xtr - X[i, :]), axis=1)
            min_index = np.argmin(
                distances)  # get the index with smallest distance
            Ypred[i] = self.ytr[
                min_index]  # predict the label of the nearest example
        return Ypred


(Xtr, ytr), (Xte, Yte) = cifar10.load_data()
Xtr_rows = Xtr.reshape(Xtr.shape[0], 32 * 32 * 3)
Xte_rows = Xte.reshape(Xte.shape[0], 32 * 32 * 3)

nn = NearestNeighbor()  # create a Nearest Neighbor classifier class
nn.train(Xtr_rows,
         ytr)  # train the classifier on the training images and labels
Yte_predict = nn.predict(Xte_rows)  # predict labels on the test images
print('Accuracy: {}'.format(np.mean(Yte_predict=Yte)))
Exemple #57
0
def app():

    # The data, shuffled and split between train and test sets:
    (x_train, y_train), (x_test, y_test) = cifar10.load_data()
    print('x_train shape:', x_train.shape)
    print(x_train.shape[0], 'train samples')
    print(x_test.shape[0], 'test samples')

    print(x_train.shape[1:])

    # Convert class vectors to binary class matrices.
    y_train = keras.utils.to_categorical(y_train, num_classes)
    y_test = keras.utils.to_categorical(y_test, num_classes)

    model = get_model()
    # initiate RMSprop optimizer
    opt = keras.optimizers.rmsprop(lr=0.0001, decay=1e-6)

    # Let's train the model using RMSprop
    model.compile(loss='categorical_crossentropy',
                  optimizer=opt,
                  metrics=['accuracy'])

    x_train = x_train.astype('float32')
    x_test = x_test.astype('float32')
    x_train /= 255
    x_test /= 255
    return x_train

    if not data_augmentation:
        print('Not using data augmentation.')
        model.fit(x_train,
                  y_train,
                  batch_size=batch_size,
                  epochs=epochs,
                  validation_data=(x_test, y_test),
                  shuffle=True)
    else:
        print('Using real-time data augmentation.')
        # This will do preprocessing and realtime data augmentation:
        datagen = ImageDataGenerator(
            featurewise_center=False,  # set input mean to 0 over the dataset
            samplewise_center=False,  # set each sample mean to 0
            featurewise_std_normalization=
            False,  # divide inputs by std of the dataset
            samplewise_std_normalization=False,  # divide each input by its std
            zca_whitening=False,  # apply ZCA whitening
            rotation_range=
            0,  # randomly rotate images in the range (degrees, 0 to 180)
            width_shift_range=
            0.1,  # randomly shift images horizontally (fraction of total width)
            height_shift_range=
            0.1,  # randomly shift images vertically (fraction of total height)
            horizontal_flip=True,  # randomly flip images
            vertical_flip=False)  # randomly flip images

        # Compute quantities required for feature-wise normalization
        # (std, mean, and principal components if ZCA whitening is applied).
        datagen.fit(x_train)

        # Fit the model on the batches generated by datagen.flow().
        model.fit_generator(datagen.flow(x_train,
                                         y_train,
                                         batch_size=batch_size),
                            steps_per_epoch=x_train.shape[0] // batch_size,
                            epochs=epochs,
                            validation_data=(x_test, y_test))

    # Save model and weights
    if not os.path.isdir(save_dir):
        os.makedirs(save_dir)
    model_path = os.path.join(save_dir, model_name)
    model.save(model_path)
    print('Saved trained model at %s ' % model_path)

    # Load label names to use in prediction results
    label_list_path = 'datasets/cifar-10-batches-py/batches.meta'

    keras_dir = os.path.expanduser(os.path.join('~', '.keras'))
    datadir_base = os.path.expanduser(keras_dir)
    if not os.access(datadir_base, os.W_OK):
        datadir_base = os.path.join('/tmp', '.keras')
    label_list_path = os.path.join(datadir_base, label_list_path)

    with open(label_list_path, mode='rb') as f:
        labels = pickle.load(f)

    # Evaluate model with test data set and share sample prediction results
    evaluation = model.evaluate_generator(datagen.flow(x_test,
                                                       y_test,
                                                       batch_size=batch_size),
                                          steps=x_test.shape[0] // batch_size)

    print('Model Accuracy = %.2f' % (evaluation[1]))

    predict_gen = model.predict_generator(datagen.flow(x_test,
                                                       y_test,
                                                       batch_size=batch_size),
                                          steps=x_test.shape[0] // batch_size)

    for predict_index, predicted_y in enumerate(predict_gen):
        actual_label = labels['label_names'][np.argmax(y_test[predict_index])]
        predicted_label = labels['label_names'][np.argmax(predicted_y)]
        print('Actual Label = %s vs. Predicted Label = %s' %
              (actual_label, predicted_label))
        if predict_index == num_predictions:
            break
Exemple #58
0
    hang_all_str.summary()
    return hang_all_str


hang_all_str = hang_net()

#set parameter class number,weight decay(wd),momentum(rush)
hang_kinds = 10
hang_iter = 1
hang_block = 64
hang_wd = 0
hang_rush = 0.93
hang_rate = 0.006

# Load the CIFAR10 data.
(hangtr, hangtr_label), (hangte, hangte_label) = cifar10.load_data()

#shrink train data
#hangtr_remain, hangtr, hangtr_label_remain, hangtr_label = \
#train_test_split(hangtr, hangtr_label, test_size=12500, stratify=hangtr_label)
print("traning sample number:", hangtr.shape)

# data pre process.cahnge class vector to binary class matrix.
hangtr_label = keras.utils.to_categorical(hangtr_label, hang_kinds)
hangte_label = keras.utils.to_categorical(hangte_label, hang_kinds)
sgd = optimizers.SGD(lr=hang_rate,
                     decay=hang_wd,
                     momentum=hang_rush,
                     nesterov=True)
hang_all_str.compile(loss='categorical_crossentropy',
                     optimizer=sgd,
Exemple #59
0
def load_dataset(dset, normalize_data, options):
    if dset == 'mnist':
        # input image dimensions
        img_rows, img_cols = 28, 28
        # the data, split between train and test sets
        (x_train, y_train), (x_test, y_test) = mnist.load_data()
        print(x_train.shape)
        n_channels = 1

    elif dset == 'cifar10':
        img_rows, img_cols = 32, 32
        n_channels = 3

        (x_train, y_train), (x_test, y_test) = cifar10.load_data()

    elif dset == 'fashion':
        img_rows, img_cols = 28, 28
        n_channels = 1

        (x_train, y_train), (x_test, y_test) = fashion_mnist.load_data()

    elif dset == 'mnist-clut':

        img_rows, img_cols = 60, 60
        # the data, split between train and test sets

        #folder='/media/home/rdata/image/'
        folder = '/home/btek/datasets/image/'
        data = np.load(folder + "mnist_cluttered_60x60_6distortions.npz",
                       allow_pickle=True)
        y_trn = data['y_train']
        y_val = data['y_valid']
        y_tst = data['y_test']
        x_train, y_train = data['x_train'], np.argmax(y_trn, axis=-1)
        x_valid, y_valid = data['x_valid'], np.argmax(y_val, axis=-1)
        x_test, y_test = data['x_test'], np.argmax(y_tst, axis=-1)
        x_train = np.vstack((x_train, x_valid))
        y_train = np.concatenate((y_train, y_valid))
        n_channels = 1
        normalize_data = False  # this dataset is already somehow normalized

        #decay_epochs =[e_i*30,e_i*100]

    elif dset == 'lfw_faces':
        from sklearn.datasets import fetch_lfw_people
        lfw_people = fetch_lfw_people(min_faces_per_person=20, resize=0.4)

        # introspect the images arrays to find the shapes (for plotting)
        n_samples, img_rows, img_cols = lfw_people.images.shape
        n_channels = 1

        X = lfw_people.data
        n_features = X.shape[1]

        # the label to predict is the id of the person
        y = lfw_people.target
        target_names = lfw_people.target_names
        num_classes = target_names.shape[0]

        from sklearn.model_selection import train_test_split

        #X -= X.mean()
        #X /= X.std()
        #split into a training and testing set
        x_train, x_test, y_train, y_test = train_test_split(X,
                                                            y,
                                                            test_size=0.25)

    if K.image_data_format() == 'channels_first':
        x_train = x_train.reshape(x_train.shape[0], n_channels, img_rows,
                                  img_cols)
        x_test = x_test.reshape(x_test.shape[0], n_channels, img_rows,
                                img_cols)
        input_shape = (n_channels, img_rows, img_cols)
    else:
        x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols,
                                  n_channels)
        x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols,
                                n_channels)
        input_shape = (img_rows, img_cols, n_channels)
        ''' why I have written this?? BTEK
        if(n_channels==1):
            x_train = np.repeat(x_train,3, axis=3)
            x_test = np.repeat(x_test,3, axis=3)
            n_channels=3
            input_shape = (img_rows, img_cols, n_channels)
        '''
    num_classes = np.shape(np.unique(y_train))[0]
    x_train = x_train.astype('float32')
    x_test = x_test.astype('float32')

    if normalize_data:
        #Simple norm 0.1
        #x_train /= 255
        #x_test /= 255

        #Standard norm mean 0 , std 1, per input
        #this normalization is very bad. BTEK for IMAGES
        #trn_mn = np.mean(x_train, axis=0) this normalization is very bad. BTEK for IMAGES
        #trn_std = np.std(x_train, axis=0) this normalization is very bad. BTEK for IMAGES

        # Standard for mean 127 and std per image.
        # This does not have 0 mean  but some negative value
        # Std is 1.0 some paper results wer taken by this I guess
        # trn_mn = np.mean(x_train)
        # trn_std = np.std(x_train)
        # x_train -= 127.0   # I use this because other normalizations do not create symmetric distribution.
        # x_test -= 127.0
        # x_train/=(trn_std+1e-7)
        # x_test/=(trn_std+1e-7)
        # print("Data normed Mean(train):", np.mean(x_train), " Std(train):", np.std(x_train))
        # print("Data normed Mean(test):", np.mean(x_test), " Std(test):", np.std(x_test))

        # Standard for mean 127 and std per image.
        # This does not have 0 mean  and std is not 1.0
        # Std is
        #        x_train /= (255/4)
        #        x_test /= (255/4)
        #        x_train -= 2.0
        #        x_test  -=  2.0
        #        print("Data normed Mean(train):", np.mean(x_train), " Std(train):", np.std(x_train))
        #        print("Data normed Mean(test):", np.mean(x_test), " Std(test):", np.std(x_test))
        # CHANGİNG THİS aug2020 FOR FACES TEST
        x_train = x_train.astype('float32')
        x_test = x_test.astype('float32')
        x_train /= 255
        x_test /= 255
        trn_mn = np.mean(x_train, axis=0)
        x_train -= trn_mn
        x_test -= trn_mn
        print('x_train shape:', x_train.shape)
        print("Data normed Mean(train):", np.mean(x_train), " Std(train):",
              np.std(x_train))
        print("Data normed Mean(test):", np.mean(x_test), " Std(test):",
              np.std(x_test))

        # non-zero normalization.


#        trn_mn = np.mean(x_train[np.nonzero(x_train)])
#        trn_std = np.std(x_train[np.nonzero(x_train)])
#        x_train[np.nonzero(x_train)] -= trn_mn
#        x_test[np.nonzero(x_test)] -= trn_mn
#        print("Data normed Mean(train):", np.mean(x_train), " Std(train):", np.std(x_train))
#        print("Data normed Mean(test):", np.mean(x_test), " Std(test):", np.std(x_test))
#        x_train/=(trn_std+1e-7)
#        x_test/=(trn_std+1e-7)
#        print("Data normed Mean(train):", np.mean(x_train), " Std(train):", np.std(x_train))
#        print("Data normed Mean(test):", np.mean(x_test), " Std(test):", np.std(x_test))

    print('x_train shape:', x_train.shape)
    print(x_train.shape[0], 'train samples')
    print(x_test.shape[0], 'test samples')

    # convert class vectors to binary class matrices
    y_train = keras.utils.to_categorical(y_train, num_classes)
    y_test = keras.utils.to_categorical(y_test, num_classes)

    return x_train, y_train, x_test, y_test, input_shape, num_classes
Exemple #60
0
import numpy as np
import cv2

from keras.datasets import cifar10

ROOT = '/home/ninad/Desktop/DLproj/'
catgs = [
    'airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse',
    'ship', 'truck'
]

(x_test, y_test) = cifar10.load_data()[1]
print('Data is loaded\n')

num = 1000  #to test dropout on
T_mc = 100
shuff_idxs = np.load(ROOT + 'code/keras_cifar10/rand_idxs.npy')
rand_idxs = shuff_idxs[:num]  #take the 1st num examples
xdata = x_test[rand_idxs, :, :, :]


#images should be in 'uint8' format
def augment_gamma(images, gamma=1.0):
    # build a lookup table mapping the pixel values [0, 255] to
    # their adjusted gamma values
    invGamma = 1.0 / gamma
    table = np.array([((i / 255.0)**invGamma) * 255
                      for i in np.arange(0, 256)]).astype("uint8")

    # apply gamma correction using the lookup table
    return np.asarray([cv2.LUT(img, table) for img in images])