def create_generator(path, add_contours, is_train=True, n_augments=1): """ tood """ generator_args = dict(rotation_range=5, width_shift_range=0.05, height_shift_range=0.05, horizontal_flip=False, data_format="channels_last") if is_train else {} npy_suffix = "train" if is_train else "test" img_train = np.load(os.path.join(path, "images_train.npy")) images = np.load(os.path.join(path, "images_{0}.npy".format(npy_suffix))) generator_img = ImageDataGenerator(featurewise_center=True, featurewise_std_normalization=True, **generator_args) generator_img.fit(img_train, augment=is_train, seed=RANDOM_SEED) img_flow = generator_img.flow(images, batch_size=n_augments, seed=RANDOM_SEED) masks = np.load(os.path.join(path, "masks_{0}.npy".format(npy_suffix))) generator_mask = ImageDataGenerator(**generator_args) generator_mask.fit(masks, augment=is_train, seed=RANDOM_SEED) mask_flow = generator_mask.flow(masks, batch_size=n_augments, seed=RANDOM_SEED) if add_contours: contours = np.load( os.path.join(path, "contours_{0}.npy".format(npy_suffix))) generator_contours = ImageDataGenerator(**generator_args) generator_contours.fit(contours, augment=is_train, seed=RANDOM_SEED) contours_flow = generator_contours.flow(contours, batch_size=n_augments, seed=RANDOM_SEED) return zip(img_flow, zip(mask_flow, contours_flow)), lambda x: generator_img.standardize(x) return zip(img_flow, mask_flow), lambda x: generator_img.standardize(x)
class OntheflyAugmentedImages(BaseDataset): """Use a tensorflow.keras ImageDataGenerator to augment images on the fly in a determenistic way.""" 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 _transform(self, idx, x): # if it is not cached add it if idx not in self.cache: # Remove the first in and add the new idx (i is the offset in # cache_data) _, i = self.cache.popitem(last=False) self.cache[idx] = i # Do the transformation and add it to the data np.random.seed(idx + self.random_state) x = self.generator.random_transform(x) x = self.generator.standardize(x) self.cache_data[i] = x # and if it is update it as the most recently used else: self.cache[idx] = self.cache.pop(idx) return self.cache_data[self.cache[idx]] def _train_data(self, idxs=slice(None)): # Make sure we accept everything that numpy accepts as indices idxs = np.arange(self.N)[idxs] # Get the original images and then transform them x, y = self.dataset.train_data[self.idxs[idxs]] x_hat = np.copy(x) random_state = np.random.get_state() for i, idx in enumerate(idxs): x_hat[i] = self._transform(idx, x_hat[i]) np.random.set_state(random_state) return x_hat, y def _test_data(self, idxs=slice(None)): return self._x_test[idxs], self._y_test[idxs] def _train_size(self): return self.N @property def shape(self): return self.dataset.shape @property def output_size(self): return self.dataset.output_size
# Preparing cv2 for webcam feed cap = cv2.VideoCapture(0) while (True): # Capture frame-by-frame. ret, frame = cap.read() # Target area where the hand gestures should be. cv2.rectangle(frame, (0, 0), (CROP_SIZE, CROP_SIZE), (0, 255, 0), 3) # Preprocessing the frame before input to the model. cropped_image = frame[0:CROP_SIZE, 0:CROP_SIZE] resized_frame = cv2.resize(cropped_image, (IMAGE_SIZE, IMAGE_SIZE)) reshaped_frame = (np.array(resized_frame)).reshape( (1, IMAGE_SIZE, IMAGE_SIZE, 3)) frame_for_model = data_generator.standardize(np.float64(reshaped_frame)) # Predicting the frame. prediction = np.array(model.predict(frame_for_model)) predicted_class = classes[ prediction.argmax()] # Selecting the max confidence index. # Preparing output based on the model's confidence. prediction_probability = prediction[0, prediction.argmax()] if prediction_probability > 0.5: # High confidence. cv2.putText( frame, '{} - {:.2f}%'.format(predicted_class, prediction_probability * 100), (10, 450), 1, 2, (255, 255, 0), 2, cv2.LINE_AA) elif prediction_probability > 0.2 and prediction_probability <= 0.5:
X_train /= 255 X_test /= 255 Y_train = to_categorical(Y_train, 10) Y_test = to_categorical(Y_test, 10) # data augmentation data_generator = ImageDataGenerator(rotation_range=90, width_shift_range=0.1, height_shift_range=0.1, featurewise_center=True, featurewise_std_normalization=True, horizontal_flip=True) data_generator.fit(X_train) # standardize test set for i in range(len(X_test)): X_test[i] = data_generator.standardize(X_test[i]) # net arch mdl = Sequential() mdl.add(Conv2D(32, (3, 3), padding='same', input_shape=X_train.shape[1:])) mdl.add(BatchNormalization()) mdl.add(Activation('elu')) mdl.add(Conv2D(32, (3, 3), padding='same')) mdl.add(BatchNormalization()) mdl.add(Activation('elu')) mdl.add(MaxPooling2D(pool_size=(2, 2))) mdl.add(Dropout(0.2)) mdl.add(Conv2D(64, (3, 3), padding='same')) mdl.add(BatchNormalization()) mdl.add(Activation('elu'))
class UnetDataGenerator(Sequence): """ Inspired on this thread: https://github.com/keras-team/keras/issues/12120 """ def __init__(self, features, targets, image_size, label_size, classes, batch_size, repeats=1, shuffle=True, validation=False): self._features = features self._targets = targets self._image_size = image_size self._label_size = label_size self._classes = classes self._batch_size = batch_size self._shuffle = shuffle self._repeats = repeats self._verification = validation self._ids = list(self._list_files()) self._indexes = np.arange(len(self._ids)) if self._shuffle: np.random.shuffle(self._indexes) self._indexes = np.repeat(self._indexes, self._repeats) self._augmenter = ImageDataGenerator(rotation_range=30, zoom_range=0.2, width_shift_range=0.01, height_shift_range=0.01, horizontal_flip=True, vertical_flip=True, samplewise_std_normalization=True) def _list_files(self): feature_files = os.listdir(self._features) target_files = os.listdir(self._targets) return zip(feature_files, target_files) def _encode_one_hot(self, label): x = np.zeros( (self._label_size[0] * self._label_size[1], self._classes)) for c in range(1, self._classes + 1): a = label == c x[(label == c).squeeze(), c - 1] = 1 return x def __len__(self): return int(np.floor(len(self._ids) / self._batch_size)) * self._repeats def __getitem__(self, item): indexes = self._indexes[item * self._batch_size:(item + 1) * self._batch_size] ids_temp = [self._ids[idx] for idx in indexes] X = [ cv2.resize( cv2.imread(os.path.join(self._features, ids_temp[k][0]), cv2.IMREAD_COLOR), self._image_size, cv2.INTER_NEAREST).astype(np.float64) / 255.0 for k in range(len(ids_temp)) ] y = [ cv2.resize( cv2.imread(os.path.join(self._targets, ids_temp[k][1]), cv2.IMREAD_COLOR), self._image_size, cv2.INTER_NEAREST) for k in range(len(ids_temp)) ] params = self._augmenter.get_random_transform(self._image_size) if not self._verification: X = np.array([ self._augmenter.apply_transform(self._augmenter.standardize(x), params) for x in X ]) y = np.array([ self._encode_one_hot( cv2.resize( self._augmenter.apply_transform(_y, params)[:, :, 0], self._label_size, cv2.INTER_NEAREST).reshape(-1, 1)) for _y in y ]) else: X = np.array([self._augmenter.standardize(x) for x in X]) y = np.array([ self._encode_one_hot( cv2.resize(_y[:, :, 0], self._label_size, cv2.INTER_NEAREST).reshape(-1, 1)) for _y in y ]) return np.array(X), np.array(y) def on_epoch_end(self): self._indexes = np.arange(len(self._ids)) if self._shuffle: np.random.shuffle(self._indexes) self._indexes = np.repeat(self._indexes, self._repeats)
white_color = (255, 255, 255) while (True): # Capture frame-by-frame ret, frame = cap.read() # Setting size of frame frame = cv2.resize(frame, (0, 0), fx=1, fy=1) # The image within this rectangle is what will be classified cv2.rectangle(frame, (0, 0), (csize, csize), red_color, 3) img = frame[0:csize, 0:csize] img = cv2.resize(img, (imsize, imsize)) img = (np.array(img)).reshape((1, imsize, imsize, 3)) img = data_generator.standardize(np.float64(img)) # Input image into model to make prediction prediction = np.array(model.predict(img)) pred = letters[prediction.argmax()] # Get confidence score in percentage confidence_score = prediction[0, prediction.argmax()] * 100 if confidence_score > 65: cv2.putText( frame, 'High Confidence: {} - {:.3f}%'.format(pred, confidence_score), (10, 450), 1, 2, green_color, 2, cv2.LINE_AA) elif 20 < confidence_score <= 65: cv2.putText( frame,
(0, 255, 0), 3) if idTo == 7: cv2.rectangle(frame, (points[idTo][0] - 25, points[idTo][1] - 50), (points[idTo][0] + 50, points[idTo][1] + 10), (0, 255, 0), 3) # Set up frame for hand for testing, to be replaced by Posenet palm detection #cv2.rectangle(frame, (0, 0), (rectSize, rectSize), (0, 255, 0), 3) handFrame = frame[0:rectSize, 0:rectSize] handFrameResized = cv2.resize(handFrame, (imgSize, imgSize)) handFrameReshaped = (np.array(handFrameResized)).reshape( (1, imgSize, imgSize, 3)) # Data Preprocessing preprocessedHandFrame = dataGen.standardize(np.float64(handFrameReshaped)) # Class Prediction predictClass = np.array(model.predict(preprocessedHandFrame)) predicted = classes[predictClass.argmax()] probability = predictClass[0, predictClass.argmax()] # Display predicted class and probability in frame cv2.putText(frame, '{} - {:.2f}%'.format(predicted, probability * 100), (10, 450), 1, 2, (255, 255, 0), 2, cv2.LINE_AA) cv2.imshow('frame', frame) # Exit if cv2.waitKey(1) & 0xFF == ord('q'): break
# get the ROI img = frame[top:bottom, right:left] # blur it img = cv2.GaussianBlur(img, (7, 7), 0) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = cv2.resize(img, (IMG_SIZE, IMG_SIZE)) #img=np.array(img)/255. #plt.imshow(img) #test_data = img orig = img data = img.reshape(1, IMG_SIZE, IMG_SIZE, 3) frame_for_model = data_generator.standardize(np.float64(data)) if i == 4: model_out = np.array(model.predict([frame_for_model])) prediction_probability = model_out[0, model_out.argmax()] res = out_label[np.argmax(model_out)] i = 0 if prediction_probability > 0.2: if mem == res: consecutive += 1 else: consecutive = 0 if consecutive == 3 and res not in ['nothing']: if res == 'space': sequence += ' ' elif res == 'del':
class HPatches(): def __init__(self, train=True, transform=None, download=False, train_fnames=[], test_fnames=[], denoise_model=None, use_clean=False): self.train = train self.transform = transform self.train_fnames = train_fnames self.test_fnames = test_fnames self.denoise_model = denoise_model self.use_clean = use_clean self.imgaug = ImageDataGenerator( # featurewise_center=True, # featurewise_std_normalization=True, rotation_range=20, width_shift_range=0.2, height_shift_range=0.2, horizontal_flip=True) def set_denoise_model(self, denoise_model): self.denoise_model = denoise_model def denoise_patches(self, patches): batch_size = 100 for i in tqdm(range(int(len(patches) / batch_size)), file=sys.stdout): batch = patches[i * batch_size:(i + 1) * batch_size] batch = np.expand_dims(batch, -1) batch = np.clip( self.denoise_model.predict(batch).astype(int), 0, 255).astype(np.uint8)[:, :, :, 0] patches[i * batch_size:(i + 1) * batch_size] = batch batch = patches[i * batch_size:] batch = np.expand_dims(batch, -1) batch = np.clip(self.denoise_model.predict(batch).astype(int), 0, 255).astype(np.uint8)[:, :, :, 0] patches[i * batch_size:] = batch return patches def read_image_file(self, data_dir, train=1): """Return a Tensor containing the patches """ if self.denoise_model and not self.use_clean: print('Using denoised patches') elif not self.denoise_model and not self.use_clean: print('Using noisy patches') elif self.use_clean: print('Using clean patches') sys.stdout.flush() patches = [] labels = [] counter = 0 hpatches_sequences = [x[1] for x in os.walk(data_dir)][0] if train: list_dirs = self.train_fnames else: list_dirs = self.test_fnames for directory in tqdm(hpatches_sequences, file=sys.stdout): if (directory in list_dirs): for tp in tps: if self.use_clean: sequence_path = os.path.join(data_dir, directory, tp) + '.png' else: sequence_path = os.path.join(data_dir, directory, tp) + '_noise.png' image = cv2.imread(sequence_path, 0) h, w = image.shape n_patches = int(h / w) n_aug = 3 for i in range(n_patches): patch = image[i * (w):(i + 1) * (w), 0:w] patch = cv2.resize(patch, (32, 32)) patch = np.array(patch, dtype=np.uint8) patches.append(patch) labels.append(i + n_aug * i + counter) for j in range(0, n_aug): params = self.imgaug.get_random_transform( patch.shape) patch_aug = self.imgaug.apply_transform( self.imgaug.standardize(patch), params) patches.append(patch_aug) labels.append(i + j + counter) counter += n_patches patches = np.array(patches, dtype=np.uint8) if self.denoise_model and not self.use_clean: print('Denoising patches...') patches = self.denoise_patches(patches) return patches, labels
import shap import numpy as np X_test, _ = test_image_gen.next() # background = X_test[np.random.choice(20, 10, replace=False)] background = X_test[0:5] # explain predictions of the model on three images # e = shap.DeepExplainer(tf.keras.models.load_model('flowers'), background) model = tf.keras.models.load_model('flowers-vgg1') # tulip_image_path = test_path + '/tulip/' + os.listdir(test_path + '/tulip/')[5] tulip_image_path = test_path + '/tulip/' + os.listdir(test_path + '/tulip/')[5] # plt.imshow(imread(dog_image)) img = image.load_img(tulip_image_path, target_size=(250, 250, 3)) # plt.imshow(imread(tulip_image_path)) img_orig = image.img_to_array(img) standardized_image = test_gen.standardize(img_orig) # plt.imshow((standardized_image)) # In[ ]: os.listdir(test_path + '/sunflower/') # In[ ]: # In[ ]: # In[ ]: # segment the image so we don't have to explain every pixel segments_slic = slic(img, n_segments=49, compactness=30, sigma=3)
model = u_net(INPUT_HEIGHT, INPUT_WIDTH, OUTPUT_HEIGHT, OUTPUT_WIDTH, N_CLASSES) model.load_weights(os.path.join(MODELS_DIRECTORY, UNET_CHECKPOINT_PATH_SIMPLE, '41.weights')) augmenter = ImageDataGenerator(rotation_range=30, zoom_range=0.2, width_shift_range=0.01, height_shift_range=0.01, horizontal_flip=True, vertical_flip=True, samplewise_std_normalization=True) image = cv2.imread(image_path, cv2.IMREAD_COLOR) label = cv2.imread(label_path, cv2.IMREAD_GRAYSCALE) image = cv2.resize(image, (INPUT_WIDTH, INPUT_HEIGHT), cv2.INTER_NEAREST).astype(np.float64) / 255.0 image = augmenter.standardize(image) input_batch = np.array([image]) output = model.predict(input_batch, batch_size=1) segmentation = decode_prediction(output[0]) figure = plt.figure(figsize=(10, 8)) plt.subplot(211) plt.imshow(segmentation) plt.subplot(212) plt.imshow(label) plt.show()
class TripletGenerator(Sequence): def __init__(self, file_path, image_path, image_test_path, nb_classes_batch, nb_images_per_class_batch, target_size=TARGET_SIZE, subset='training', validation_split=0.0, seed=42, is_reptile=False, shuffle=True, excluded_classes=['new_whale'], class_weight_type=None, **kwargs): self.file_path = file_path self.shuffle = shuffle self.target_size = target_size self.nb_classes_batch = nb_classes_batch self.nb_images_per_class_batch = nb_images_per_class_batch self.batch_size = nb_classes_batch * nb_images_per_class_batch self.image_path = image_path self.image_test_path = image_test_path self.df = pd.read_csv(file_path) self.index_array = None self.lock = threading.Lock() self.is_reptile = is_reptile logger.info('exclude_class: {}'.format(excluded_classes)) logger.info('class_weight_type: {}'.format(class_weight_type)) blacklisted_class_ix = self.df['Id'].isin(excluded_classes) logger.info("{} instances excluded".format( np.sum(blacklisted_class_ix))) df = self.df[~blacklisted_class_ix] classes = list(df['Id'].unique()) self.class_indices = dict(zip(classes, range(len(classes)))) self.class_inv_indices = dict(zip(range(len(classes)), classes)) train_classes, test_classes = train_test_split( classes, test_size=validation_split, random_state=seed) if subset == 'training': self.df = df[df['Id'].isin(train_classes)] else: self.df = df[df['Id'].isin(test_classes)] logger.info("data has shape: {}".format(self.df.shape)) classes = self.df['Id'].values self.classes = np.array([self.class_indices[cls] for cls in classes]) self.filenames = np.array(self.df['Image']) self.class_indices_subset = { k: v for k, v in self.class_inv_indices.items() if k in self.classes } kwargs.update({'preprocessing_function': _preprocess_input}) logger.info(kwargs) self.image_generator = ImageDataGenerator(**kwargs) # test time generator self.image_generator_inference = ImageDataGenerator( preprocessing_function=_preprocess_input) def get_test_images_and_names(self): img_names = os.listdir(self.image_test_path) filepaths = [ os.path.join(self.image_test_path, img_name) for img_name in img_names ] _x = np.zeros((len(filepaths), ) + self.target_size, dtype='float32') for i, _path in enumerate(filepaths): _img = load_img(_path, target_size=self.target_size) _x[i] = img_to_array(_img) if hasattr(_img, 'close'): _img.close() return _x, img_names def __len__(self): """ number of steps per epoch number of classes in subet / nb_classes_batch :return: """ return len(self.class_indices_subset) // self.nb_classes_batch def get_nb_classes(self): """ number of all classes :return: """ return len(self.class_indices) def _set_index_array(self): self.index_array = list(self.class_indices_subset.keys()) if self.shuffle: self.index_array = np.random.permutation(self.index_array) def on_epoch_end(self): """ shuffle at epoch end """ self._set_index_array() def get_train_image_from_class_ix(self, ixs): return self._get_batches_of_transformed_samples(ixs) def __getitem__(self, idx): if self.index_array is None: self._set_index_array() # unique classes to pull index_array = self.index_array[idx * self.nb_classes_batch:self. nb_classes_batch * (idx + 1)] return self._get_batches_of_transformed_samples(index_array) def _get_batches_of_transformed_samples(self, index_array): """ validation and training behaves the same way w.r.t. augmentation :param index_array: :return: """ batch_x = [] for i, class_ix in enumerate(index_array): _samples = np.zeros(tuple([self.nb_images_per_class_batch] + list(self.target_size)), dtype='float32') filenames = self.filenames[self.classes == class_ix] if len(filenames) > self.nb_images_per_class_batch: np.random.shuffle(filenames) filenames = filenames[:self.nb_images_per_class_batch] logger.debug("{} files for class {}".format( len(filenames), class_ix)) for j, filename in enumerate(filenames): img = load_img(os.path.join(self.image_path, filename), target_size=self.target_size) x = img_to_array(img, data_format='channels_last') # Pillow images should be closed after `load_img`, # but not PIL images. if hasattr(img, 'close'): img.close() _samples[j] = self.image_generator.standardize(x) # require at least `nb_images_per_class_batch` per class nb_missing = _samples.shape[0] - j - 1 # select images to transform. No need to standardize again. img_ix_transformed = np.random.choice(j + 1, nb_missing) for img_ix, k in zip(img_ix_transformed, range(nb_missing)): x = _samples[img_ix] params = self.image_generator.get_random_transform( self.target_size) x = self.image_generator.apply_transform(x, params) _samples[j + k + 1] = x logger.debug("{} transformations for class {}".format( nb_missing, class_ix)) batch_x.append(_samples) batch_x = np.vstack(batch_x) # build batch of labels _labels = range(10) if self.is_reptile else index_array batch_y = np.repeat(_labels, self.nb_images_per_class_batch) if self.is_reptile: batch_y = to_categorical(batch_y, num_classes=10) assert len(batch_y), len(batch_x) return batch_x, batch_y def get_test_generator(self, x): """ :param x: test data :return: NumpyArrayIterator """ return self.image_generator_inference.flow(x=x, shuffle=False, batch_size=self.batch_size)
class ImageGeneratorParallel(keras.utils.Sequence): """Generates data for Keras""" def __init__(self, features, targets, n_classes=2, batch_size=32, shuffle=True, repeats=1, parallel=True): self.n_classes = n_classes self.n_vals = len(targets) # since we are using data agumentation we repeat the number of times we show each image. # we show the same original image but it can be rotated or flipper each time, so it is not the "same" image self.list_IDs = np.repeat( np.arange(self.n_vals), repeats ) # OJO con esto, deberian ser las imagenes validas si queremos hacer bien las cosas self.batch_size = batch_size self.features = features self.shuffle = shuffle self.targets = targets self.targets_mc = keras.utils.to_categorical( targets, num_classes=self.n_classes) self.indexes = np.arange(len(self.list_IDs)) self.pool = None self.parallel = parallel self.agumentator = ImageDataGenerator( # featurewise_center=True, # featurewise_std_normalization=True, rescale=1 / 255, rotation_range=40, zoom_range=0.2, width_shift_range=0.2, height_shift_range=0.2, horizontal_flip=True, fill_mode='nearest') def __len__(self): 'Denotes the number of batches per epoch' return int(np.floor(len(self.list_IDs) / self.batch_size)) def __getitem__(self, index): 'Generate one batch of data' # Generate indexes of the batch if self.parallel: if self.pool is None: self.pool = ThreadPool(4) indexes = self.indexes[index * self.batch_size:(index + 1) * self.batch_size] # Find list of IDs list_IDs_temp = [self.list_IDs[k] for k in indexes] # Generate data if self.parallel: X, y = self.__data_generation_threads(list_IDs_temp) else: X, y = self.__data_generation(list_IDs_temp) return X, y def on_epoch_end(self): 'Updates indexes after each epoch' self.indexes = np.arange(len(self.list_IDs)) if self.shuffle: np.random.shuffle(self.indexes) def __data_generation(self, list_IDs_temp): 'Generates data containing batch_size samples' # X : (n_samples, *dim, n_channels) # Initialization X = self.features[list_IDs_temp] y = self.targets_mc[list_IDs_temp] X = np.array( self.agumentator.flow(X, batch_size=self.batch_size, shuffle=self.shuffle).next()) return X, y def __data_generation_threads(self, list_IDs_temp): 'Generates data containing batch_size samples' # X : (n_samples, *dim, n_channels) # Initialization X = self.features[list_IDs_temp] y = self.targets_mc[list_IDs_temp] X = np.array( self.pool.map( lambda xi: self.agumentator.random_transform( self.agumentator.standardize(xi)), X)) return X, y