我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用keras.preprocessing.image.ImageDataGenerator()。
def train_model(model, X, X_test, Y, Y_test): batch_size = 100 epochs = 2 checkpoints = [] if not os.path.exists('Data/Checkpoints/'): os.makedirs('Data/Checkpoints/') checkpoints.append(ModelCheckpoint('Data/Checkpoints/best_weights.h5', monitor='val_loss', verbose=0, save_best_only=True, save_weights_only=True, mode='auto', period=1)) checkpoints.append(TensorBoard(log_dir='Data/Checkpoints/./logs', histogram_freq=0, write_graph=True, write_images=False, embeddings_freq=0, embeddings_layer_names=None, embeddings_metadata=None)) # Creates live data: # For better yield. The duration of the training is extended. # If you don't want, use this: # model.fit(X, Y, batch_size=batch_size, epochs=epochs, validation_data=(X_test, Y_test), shuffle=True, callbacks=checkpoints) generated_data = ImageDataGenerator(featurewise_center=False, samplewise_center=False, featurewise_std_normalization=False, samplewise_std_normalization=False, zca_whitening=False, rotation_range=0, width_shift_range=0.1, height_shift_range=0.1, horizontal_flip = True, vertical_flip = False) generated_data.fit(X) model.fit_generator(generated_data.flow(X, Y, batch_size=batch_size), steps_per_epoch=X.shape[0]/6, epochs=epochs, validation_data=(X_test, Y_test), callbacks=checkpoints) return model
def data_augmentation(self): if self.verbose: print 'enhancing training set with data augmentation... ' start_time = time.time() self.datagen = ImageDataGenerator( rotation_range=20, width_shift_range=0.3, height_shift_range=0.3, shear_range=0.2, fill_mode='nearest', horizontal_flip=True, vertical_flip=True ) self.datagen.fit(self.xs_train) if self.verbose: end_time = time.time() self.print_time(start_time,end_time,'data augmentation')
def data_augmentation(self): if self.verbose: print 'enhancing training set with data augmentation... ' start_time = time.time() self.datagen = ImageDataGenerator( rotation_range=30, width_shift_range=0.2, height_shift_range=0.2, shear_range=0.2, fill_mode='nearest', horizontal_flip=True, vertical_flip=True ) self.datagen.fit(self.xs_train) if self.verbose: end_time = time.time() self.print_time(start_time,end_time,'data augmentation')
def ms_valgen(): validation_datagen = ImageDataGenerator().flow_from_directory( VAL_DIR, target_size=(max([w1,w2,w3]), max([h1,h2,h3])), batch_size=mini_batch_sz, class_mode='binary' ) meanstdev = [pickle.load(open('meanSTDDEV320')), pickle.load(open('meanSTDDEV240')), pickle.load(open('meanSTDDEV400'))] while 1: X,y = validation_datagen.next() quad1, quad2 = sample(np.random.permutation(4),2) x1, y1 = getXY(quad1, w1) x2, y2 = getXY(quad2, w2, imsize=w1) X1 = submean(cropX(X, x=x1, y=y1, size=w1), meanstdev[0]) X2 = submean(cropX(resizeX(X, w1), x=x2, y=y2, size=w2), meanstdev[1]) X3 = submean(X, meanstdev[2]) yield ([X1, X2, X3], y)
def DataGen(): train_datagen = ImageDataGenerator(zoom_range=0.25, rotation_range=15., channel_shift_range=25., width_shift_range=0.02, height_shift_range=0.02, horizontal_flip=True, fill_mode='constant') validation_datagen = ImageDataGenerator(horizontal_flip=True) train_generator = train_datagen.flow_from_directory( TRAIN_DIR, target_size=(img_width, img_width), batch_size=mini_batch_sz, class_mode='binary') validation_generator = validation_datagen.flow_from_directory( VAL_DIR,target_size=(img_width, img_height), batch_size=mini_batch_sz, class_mode='binary', shuffle=False) return (standardized(train_generator, training=True, inception=False), standardized(validation_generator, inception=False))
def train(): model= get_model() x_train,y_train= load_data('kaggle_data/train_images_512x512') datagen = ImageDataGenerator( horizontal_flip=True, vertical_flip=True) model.fit_generator(datagen.flow(x_train, y_train, batch_size=batch_size), steps_per_epoch=len(x_train) / batch_size, epochs=epochs) model.save(model_name+'_model.h5')
def get_image_data_augmentor_from_dataset(dataset): from keras.preprocessing.image import ImageDataGenerator dataset_config = dataset['config'] augShearRange = float(get_option(dataset_config, 'augShearRange', 0.1)) augZoomRange = float(get_option(dataset_config, 'augZoomRange', 0.1)) augHorizontalFlip = bool(get_option(dataset_config, 'augHorizontalFlip', False)) augVerticalFlip = bool(get_option(dataset_config, 'augVerticalFlip', False)) augRotationRange = float(get_option(dataset_config, 'augRotationRange', 0.2)) return ImageDataGenerator( rotation_range=augRotationRange, shear_range=augShearRange, zoom_range=augZoomRange, horizontal_flip=augHorizontalFlip, vertical_flip=augVerticalFlip )
def test_n_images_total(sample_dataset_dir): n_images_train = 18 data_path = os.path.join(sample_dataset_dir, 'Training') # From Path n_total_images = cds.compute_n_images(data_path) assert n_images_train == n_total_images, 'N_images from data path fail' # From Generator train_datagen = image.ImageDataGenerator() generator = train_datagen.flow_from_directory( data_path, target_size=(10, 10), batch_size=10, shuffle=False, class_mode='sparse') n_total_images = cds.compute_n_images(data_path, generator) assert n_images_train == n_total_images, 'N_images from generator fail' # Test to see if mean and std have 3 components (BGR) / They are np.float64 values
def getDataGenObject(directory): datagen = ImageDataGenerator( rescale=1. / 255, # rotation_range = 40, # width_shift_range = 0.1, # height_shift_range = 0.1, # shear_range = 0.1, # zoom_range = 0.1, # horizontal_flip = True, # fill_mode = "nearest" ) datagen_generator = datagen.flow_from_directory( directory, target_size=(img_height, img_width), batch_size=batch_size, class_mode=None, shuffle=False ) return datagen_generator
def getDataGenObject ( directory, class_mode ): datagen = ImageDataGenerator( rescale = 1./255, # rotation_range = 40, # width_shift_range = 0.1, # height_shift_range = 0.1, # shear_range = 0.1, # zoom_range = 0.1, # horizontal_flip = True, # fill_mode = "nearest" ) datagen_generator = datagen.flow_from_directory( directory, target_size = ( data["img_height"], data["img_width"] ), batch_size = data["batch_size"], class_mode = class_mode, shuffle = False ) return datagen_generator
def get_generator(featurewise_center=False, featurewise_std=False, rotation=10, width_shift=0.05, height_shift=0.05, zoom=[0.95, 1.05], horizontal=False, vertical=False): ''' ???????????????????keras?? ''' datagen = ImageDataGenerator( featurewise_center=featurewise_center, featurewise_std_normalization=featurewise_std, rotation_range=rotation, width_shift_range=width_shift, height_shift_range=height_shift, zoom_range=zoom, horizontal_flip=horizontal, vertical_flip=vertical) return datagen
def train_with_data_augmentation(self, batch_size, num_epoch, lr_schedule): datagen = ImageDataGenerator( width_shift_range=0.125, # randomly shift images horizontally, fraction height_shift_range=0.125, # randomly shift images vertically, fraction horizontal_flip=True) opt = keras.optimizers.SGD(lr=lr_schedule(0), momentum=0.9, nesterov=True) callback_list = [LearningRateScheduler(lr_schedule)] self.ae.compile(optimizer=opt, loss='mse') assert False, 'seems that y is not augmented.' # history = self.ae.fit_generator( # datagen.flow( # self.dataset.train_xs, # self.dataset.train_xs, # nb_epoch=num_epoch, # batch_size=batch_size, # validation_data=(self.dataset.test_xs, self.dataset.test_xs), # shuffle=True, callbacks=callback_list) self.history = history.history
def train_generator(): """Train Generator for Keras Returns ---------- train_gen : generator Yield augmented images val_gen : generator Yield non-augmented images """ train_gen = ImageDataGenerator( rotation_range=30, shear_range=0.1, zoom_range=0.1, width_shift_range=0.2, height_shift_range=0.2, ) val_gen = ImageDataGenerator() return train_gen, val_gen
def test_images_generator(test_path): ''' Creates a generator that pulls images from a test directory that contains shade vs sunny subdirectories. ''' from keras.utils.np_utils import to_categorical from keras.preprocessing import image from keras.preprocessing.image import ImageDataGenerator from keras.applications.resnet50 import preprocess_input from sklearn.model_selection import train_test_split from image_utilities import load_images_from_directory, preprocess_input_resnet import numpy as np #load_images from from the train and val directories test_datagen = ImageDataGenerator(preprocessing_function=preprocess_input_resnet) test_generator = test_datagen.flow_from_directory(directory=test_path, target_size=[224, 224], batch_size=26, class_mode='categorical') return test_datagen, test_generator
def get_generators(): train_datagen = ImageDataGenerator( rescale=1./255, shear_range=0.2, horizontal_flip=True, rotation_range=10., width_shift_range=0.2, height_shift_range=0.2) test_datagen = ImageDataGenerator(rescale=1./255) train_generator = train_datagen.flow_from_directory( os.path.join('data', 'train'), target_size=(299, 299), batch_size=32, classes=data.classes, class_mode='categorical') validation_generator = test_datagen.flow_from_directory( os.path.join('data', 'test'), target_size=(299, 299), batch_size=32, classes=data.classes, class_mode='categorical') return train_generator, validation_generator
def ms_traingen(): train_datagen = ImageDataGenerator(rotation_range=30., horizontal_flip=True, fill_mode='reflect').flow_from_directory( TRAIN_DIR, target_size=(max([w1,w2,w3]), max([h1,h2,h3])), batch_size=mini_batch_sz, class_mode='binary' ) meanstdev = [pickle.load(open('meanSTDDEV320')), pickle.load(open('meanSTDDEV240')), pickle.load(open('meanSTDDEV400'))] while 1: X,y = train_datagen.next() for i in xrange(len(X)): if randint(0, 4)//4: X[i] = random_bright_shift(X[i]) if randint(0, 4)//4: X[i] = random_contrast_shift(X[i]) quad1, quad2 = sample(np.random.permutation(4),2) x1, y1 = getXY(quad1, w1) x2, y2 = getXY(quad2, w2, imsize=w1) X1 = submean(cropX(X, x=x1, y=y1, size=w1), meanstdev[0]) X2 = submean(cropX(resizeX(X, w1), x=x2, y=y2, size=w2), meanstdev[1]) X3 = submean(X, meanstdev[2]) yield ([X1, X2, X3], y)
def image_generator(): return 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) zoom_range=0.1, # randomly zoom in on images by (percentage as fraction) width_shift_range=0.05, # randomly shift images horizontally (fraction of total width) height_shift_range=0.05, # randomly shift images vertically (fraction of total height) horizontal_flip=True, # randomly flip images vertical_flip=False )
def __init__(self, annotations_group, num_classes=0, image=None): self.annotations_group = annotations_group self.num_classes_ = num_classes self.image = image super(SimpleGenerator, self).__init__(ImageDataGenerator(), group_method='none', shuffle_groups=False)
def _train_generator(): return ImageDataGenerator( featurewise_center=True, featurewise_std_normalization=True, rotation_range=180, zoom_range=0.2, width_shift_range=0.2, height_shift_range=0.2, shear_range=0.3, horizontal_flip=True, vertical_flip=True, fill_mode='reflect')
def _val_generator(): return ImageDataGenerator( featurewise_center=True, featurewise_std_normalization=True)
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 __init__(self, dataset, augmentation_params, N=None): # Initialize member variables self.dataset = dataset self.augmentation_params = augmentation_params self.N = len(self.dataset.train_data) * 10 if N is None else N assert len(self.dataset.shape) == 3 # Allocate space for the augmented data self._x = np.memmap( TemporaryFile(), dtype=np.float32, shape=(self.N,) + self.dataset.shape ) self._y = np.zeros((self.N, self.dataset.output_size)) # Train a generator and generate all the data generator = ImageDataGenerator(**self.augmentation_params) x, y = self.dataset.train_data[:] generator.fit(x) start = 0 for bx, by in generator.flow(x, y, batch_size=128): end = min(self.N, start+len(bx)) self._x[start:end] = bx[:end-start] self._y[start:end] = by[:end-start] start = end if start >= self.N: break
def __init__(self, dataset, augmentation_params, N=None, random_state=0, cache_size=None): # Initialize some member variables self.dataset = dataset self.generator = ImageDataGenerator(**augmentation_params) self.N = N or (len(self.dataset.train_data) * 10) self.random_state = random_state assert len(self.dataset.shape) == 3 # Figure out the base images for each of the augmented ones self.idxs = np.random.choice( len(self.dataset.train_data), self.N ) # Fit the generator self.generator.fit(self.dataset.train_data[:][0]) # Standardize the test data self._x_test = np.copy(self.dataset.test_data[:][0]) self._x_test = self.generator.standardize(self._x_test) self._y_test = self.dataset.test_data[:][1] # Create an LRU cache to speed things up a bit for the transforms cache_size = cache_size or len(self.dataset.train_data) self.cache = OrderedDict([(-i,i) for i in range(cache_size)]) self.cache_data = np.empty( shape=(cache_size,) + self.dataset.shape, dtype=np.float32 )
def get_gen(set_name, batch_size, translate, scale, shuffle=True): if set_name == 'train': (X, Y), _ = get_mnist_dataset() elif set_name == 'test': _, (X, Y) = get_mnist_dataset() image_gen = ImageDataGenerator( zoom_range=scale, width_shift_range=translate, height_shift_range=translate ) gen = image_gen.flow(X, Y, batch_size=batch_size, shuffle=shuffle) return gen
def augment(dataset='mnist'): if dataset == 'mnist' : return ImageDataGenerator(width_shift_range=0.1, height_shift_range=0.1) elif dataset == 'cifar10': return ImageDataGenerator(width_shift_range=0.1, height_shift_range=0.1, horizontal_flip=True) elif dataset == 'svhn2' : return ImageDataGenerator(width_shift_range=0.1, height_shift_range=0.1) else: raise ValueError(dataset)
def train(self, dataset, nb_epoch, batch_size, data_augmentation): # sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True) self.model.compile(loss='categorical_crossentropy', optimizer='SGD', metrics=['accuracy']) if not data_augmentation: print('<--------------------Not using data augmentation-------------------->') self.model.fit(dataset.x_train, dataset.y_train, batch_size=batch_size, epochs=nb_epoch, validation_data=(dataset.x_valid, dataset.y_valid), shuffle=True) else: print('<--------------------Using real-time data augmentation-------------------->') # do pre-processing and real-time 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=20, # randomly rotate images in the range (degrees, 0 to 180) width_shift_range=0.2, # randomly shift images horizontally (fraction of total width) height_shift_range=0.2, # 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(dataset.x_train) # fit the model on the batches generated by datagen.flow() self.model.fit_generator(datagen.flow(dataset.x_train, dataset.y_train, batch_size=batch_size), steps_per_epoch=dataset.x_train.shape[0], epochs=nb_epoch, validation_data=(dataset.x_valid, dataset.y_valid))
def save_bottleneck_features(): # build the Inception V3 network model = inception_v3.InceptionV3(include_top=False, weights='imagenet', input_tensor=None, input_shape=None, pooling='avg') # Save the bottleneck features for the training data set datagen = ImageDataGenerator(preprocessing_function= inception_v3.preprocess_input) generator = datagen.flow_from_directory(train_data_dir, target_size=(img_width, img_height), batch_size=batch_size, class_mode='sparse', shuffle=False) features = model.predict_generator(generator, nb_train_samples // batch_size) labels = np.eye(generator.num_class, dtype='uint8')[generator.classes] labels = labels[0:(nb_train_samples // batch_size) * batch_size] np.save(open(output_dir+'bottleneck_features_train.npy', 'wb'), features) np.save(open(output_dir+'bottleneck_labels_train.npy', 'wb'), labels) # Save the bottleneck features for the validation data set generator = datagen.flow_from_directory(validation_data_dir, target_size=(img_width, img_height), batch_size=batch_size, class_mode=None, shuffle=False) features = model.predict_generator(generator, nb_validation_samples // batch_size) labels = np.eye(generator.num_class, dtype='uint8')[generator.classes] labels = labels[0:(nb_validation_samples // batch_size) * batch_size] np.save(open(output_dir+'bottleneck_features_validation.npy', 'wb'), features) np.save(open(output_dir+'bottleneck_labels_validation.npy', 'wb'), labels)
def get_batches(path, gen=image.ImageDataGenerator(), shuffle=True, batch_size=8, class_mode='categorical'): return gen.flow_from_directory(path, target_size=(32,32), class_mode=class_mode, shuffle=shuffle, batch_size=batch_size)
def test_image_data_generator(self): for test_images in self.all_test_images: img_list = [] for im in test_images: img_list.append(image.img_to_array(im)[None, ...]) images = np.vstack(img_list) generator = image.ImageDataGenerator( featurewise_center=True, samplewise_center=True, featurewise_std_normalization=True, samplewise_std_normalization=True, zca_whitening=True, rotation_range=90., width_shift_range=0.1, height_shift_range=0.1, shear_range=0.5, zoom_range=0.2, channel_shift_range=0., fill_mode='nearest', cval=0.5, horizontal_flip=True, vertical_flip=True) generator.fit(images, augment=True) tmp_folder = tempfile.mkdtemp(prefix='test_images') for x, y in generator.flow(images, np.arange(images.shape[0]), shuffle=True, save_to_dir=tmp_folder): assert x.shape[1:] == images.shape[1:] break shutil.rmtree(tmp_folder)
def test_image_data_generator_invalid_data(self): generator = image.ImageDataGenerator( featurewise_center=True, samplewise_center=True, featurewise_std_normalization=True, samplewise_std_normalization=True, zca_whitening=True, dim_ordering='tf') # Test fit with invalid data with pytest.raises(ValueError): x = np.random.random((3, 10, 10)) generator.fit(x) with pytest.raises(ValueError): x = np.random.random((32, 3, 10, 10)) generator.fit(x) with pytest.raises(ValueError): x = np.random.random((32, 10, 10, 5)) generator.fit(x) # Test flow with invalid data with pytest.raises(ValueError): x = np.random.random((32, 10, 10, 5)) generator.flow(np.arange(x.shape[0])) with pytest.raises(ValueError): x = np.random.random((32, 10, 10)) generator.flow(np.arange(x.shape[0])) with pytest.raises(ValueError): x = np.random.random((32, 3, 10, 10)) generator.flow(np.arange(x.shape[0]))
def test_image_data_generator_fit(self): generator = image.ImageDataGenerator( featurewise_center=True, samplewise_center=True, featurewise_std_normalization=True, samplewise_std_normalization=True, zca_whitening=True, dim_ordering='tf') # Test grayscale x = np.random.random((32, 10, 10, 1)) generator.fit(x) # Test RBG x = np.random.random((32, 10, 10, 3)) generator.fit(x) generator = image.ImageDataGenerator( featurewise_center=True, samplewise_center=True, featurewise_std_normalization=True, samplewise_std_normalization=True, zca_whitening=True, dim_ordering='th') # Test grayscale x = np.random.random((32, 1, 10, 10)) generator.fit(x) # Test RBG x = np.random.random((32, 3, 10, 10)) generator.fit(x)
def test_directory_iterator(self): num_classes = 2 tmp_folder = tempfile.mkdtemp(prefix='test_images') # create folders and subfolders paths = [] for cl in range(num_classes): class_directory = 'class-{}'.format(cl) classpaths = [ class_directory, os.path.join(class_directory, 'subfolder-1'), os.path.join(class_directory, 'subfolder-2'), os.path.join(class_directory, 'subfolder-1', 'sub-subfolder') ] for path in classpaths: os.mkdir(os.path.join(tmp_folder, path)) paths.append(classpaths) # save the images in the paths count = 0 filenames = [] for test_images in self.all_test_images: for im in test_images: # rotate image class im_class = count % num_classes # rotate subfolders classpaths = paths[im_class] filename = os.path.join(classpaths[count % len(classpaths)], 'image-{}.jpg'.format(count)) filenames.append(filename) im.save(os.path.join(tmp_folder, filename)) count += 1 # create iterator generator = image.ImageDataGenerator() dir_iterator = generator.flow_from_directory(tmp_folder) # check number of classes and images assert(len(dir_iterator.class_indices) == num_classes) assert(len(dir_iterator.classes) == count) assert(sorted(dir_iterator.filenames) == sorted(filenames)) shutil.rmtree(tmp_folder)
def trainnetwork(): train_data_gen = ImageDataGenerator( rescale=1./255, shear_range=0.2, zoom_range=0.4, horizontal_flip=False) test_data_gen = ImageDataGenerator( rescale=1./255, shear_range=0.3, zoom_range=0.3, horizontal_flip=False) train_generator = train_data_gen.flow_from_directory( 'data/train', # classes=hanzi, target_size=(48, 48), color_mode='grayscale', # save_to_dir='data/debug', save_format='png') test_generator = test_data_gen.flow_from_directory( 'data/test', target_size=(48, 48), # classes=hanzi, color_mode='grayscale', save_format='png') model = genmodel() nb = len(hanzi) weight = ((nb - np.arange(nb)) / nb + 1.0)**3 history = model.fit_generator(train_generator, samples_per_epoch = 40872*2, nb_epoch = 15, verbose=1) score = model.evaluate_generator(test_generator, val_samples=1703) print('Test score:', score[0]) print('Test accuracy:', score[1]) model.save_weights('weights.model')
def create_keras_generator(data_path, batch_size=10, target_size=(10, 10)): # Keras image loaders train_datagen = image.ImageDataGenerator() generator = train_datagen.flow_from_directory( data_path, target_size=target_size, batch_size=batch_size, shuffle=False, class_mode='sparse') return generator
def test_compute_mean(sample_dataset_dir): data_path = os.path.join(sample_dataset_dir, 'Training') n_components = 3 mean, std = cds.compute_mean_std(data_path, target_size=(100, 100), batch_size=10) assert n_components == mean.shape[0], 'N_components mean from data path fail' assert n_components == std.shape[0], 'N_components std from data path fail' assert isinstance(mean[0], np.float64), "Wrong type!" # From Generator train_datagen = image.ImageDataGenerator() generator = train_datagen.flow_from_directory( data_path, target_size=(10, 10), batch_size=10, shuffle=False, class_mode='sparse') mean, std = cds.compute_mean_std(data_path, target_size=(100, 100), batch_size=10, generator=generator) assert n_components == mean.shape[0], 'N_components mean from generator fail' assert isinstance(mean[0], np.float64), "Wrong type!" # Test to check if it is returning the correct number of classes and that the numbers are ints
def test_compute_class_histogram(sample_dataset_dir): data_path = os.path.join(sample_dataset_dir, 'Training') n_classes = 2 class_hist = cds.create_class_histogram(data_path) assert n_classes == class_hist.shape[0], 'N_classes from data path fail' assert isinstance(class_hist[0], np.int64), "Wrong type!, should be int" # From Generator train_datagen = image.ImageDataGenerator() generator = train_datagen.flow_from_directory( data_path, target_size=(10, 10), batch_size=10, shuffle=False, class_mode='sparse') class_hist = cds.create_class_histogram(data_path, generator) assert n_classes == class_hist.shape[0], 'N_classes from generator fail' assert isinstance(class_hist[0], np.int64), "Wrong type!, should be int" # Test to see if mean and std have 3 components (BGR) / They are np.float64 values , class hist and n_total_img work
def test_compute_stats(sample_dataset_dir): data_path = os.path.join(sample_dataset_dir, 'Training') n_components = 3 n_classes = 2 n_images_train = 18 dict_stats = cds.compute_stats(data_path, target_size=(100, 100), batch_size=10) assert n_components == dict_stats['mean'].shape[0], 'N_components mean from data path fail' assert n_components == dict_stats['std'].shape[0], 'N_components std from data path fail' assert isinstance(dict_stats['mean'][0], np.float64), "Wrong type!" assert n_classes == dict_stats['class_histogram'].shape[0], 'N_classes from generator fail' assert isinstance(dict_stats['class_histogram'][0], np.int64), "Wrong type!, should be int" assert n_images_train == dict_stats['n_images'], 'N_images from data path fail' # From Generator train_datagen = image.ImageDataGenerator() generator = train_datagen.flow_from_directory( data_path, target_size=(10, 10), batch_size=10, shuffle=False, class_mode='sparse') dict_stats = cds.compute_stats(data_path, target_size=(100, 100), batch_size=10, generator=generator) assert n_components == dict_stats['mean'].shape[0], 'N_components mean from generator fail' assert n_components == dict_stats['std'].shape[0], 'N_components std from generator fail' assert isinstance(dict_stats['mean'][0], np.float64), "Wrong type!" assert n_classes == dict_stats['class_histogram'].shape[0], 'N_classes from generator fail' assert isinstance(dict_stats['class_histogram'][0], np.int64), "Wrong type!, should be int" assert n_images_train == dict_stats['n_images'], 'N_images from generator fail'
def get_gen(class_dir, BATCH_SIZE): train_datagen = ImageDataGenerator( rescale=1. / 255, horizontal_flip=False) train_gen = train_datagen.flow_from_directory( "data/%s"%class_dir, target_size=(img_size, img_size), batch_size=BATCH_SIZE, class_mode="categorical") return train_gen
def realtime(): #initialize preview cv2.namedWindow("preview") vc = cv2.VideoCapture(0) if vc.isOpened(): #get the first frame rval, frame = vc.read() else: rval = False classes=["peace","punch","stop","thumbs_up"] while rval: frame=cv2.flip(frame,1) cv2.rectangle(frame,(300,200),(500,400),(0,255,0),1) cv2.putText(frame,"Place your hand in the green box.", (50,50), cv2.FONT_HERSHEY_PLAIN , 1, 255) cv2.putText(frame,"Press esc to exit.", (50,100), cv2.FONT_HERSHEY_PLAIN , 1, 255) cv2.imshow("preview", frame) frame=frame[200:400,300:500] #frame = cv2.resize(frame, (200,200)) frame = cv2.cvtColor( frame, cv2.COLOR_RGB2GRAY) frame=frame.reshape((1,)+frame.shape) frame=frame.reshape(frame.shape+(1,)) test_datagen = ImageDataGenerator(rescale=1./255) m=test_datagen.flow(frame,batch_size=1) y_pred=model.predict_generator(m,1) histarray2={'PEACE': y_pred[0][0], 'PUNCH': y_pred[0][1], 'STOP': y_pred[0][2], 'Thumbs Up': y_pred[0][3]} update(histarray2) print(classes[list(y_pred[0]).index(y_pred[0].max())]) rval, frame = vc.read() key = cv2.waitKey(20) if key == 27: # exit on ESC break cv2.destroyWindow("preview") vc=None #loading the model
def train_func(): if not data_augmentation: print('Not using data augmentation.') history = model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epoch, 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 featurewise 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(). history = model.fit_generator(datagen.flow(X_train, Y_train, batch_size=batch_size), samples_per_epoch=X_train.shape[0], nb_epoch=nb_epoch, validation_data=(X_test, Y_test)) ret_dict["training_accuracy"] = history.history['acc'][-1] ret_dict["test_accuracy"] = history.history['val_acc'][-1]
def train_model(): if not data_augmentation: print('Not using data augmentation.') history = model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epoch, validation_data=(X_test, Y_test), shuffle=True) profile_output['TRAIN_ACCURACY'] = history.history['acc'][-1] 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 featurewise 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(). history = model.fit_generator(datagen.flow(X_train, Y_train, batch_size=batch_size), samples_per_epoch=X_train.shape[0], nb_epoch=nb_epoch, validation_data=(X_test, Y_test)) profile_output['TRAIN_ACCURACY'] = history.history['acc'][-1] # Calling training and profile memory usage