def __getitem__(self, idx): batch_x = self.x[idx * self.batch_size:(idx + 1) * self.batch_size] if self.y_col != None: batch_y = self.y[idx * self.batch_size:(idx + 1) * self.batch_size] images_list = [] for i, file_name in enumerate(batch_x): image = skimage.io.imread(file_name) image = skimage.color.gray2rgb(image) # Perform random data augmentation np.random.seed(self.random_state + idx + i) rotation_angle = np.random.uniform(-self.rotation_range, self.rotation_range) image = skimage.transform.rotate(image, rotation_angle) # Randomize the occurence of horizontal flip if self.horizontal_flip and np.random.random_integers(0, 1): image = image[:, ::-1] image = skimage.transform.resize(image, self.target_size, anti_aliasing=True, mode='constant') images_list.append(image) images_array = np.array(images_list) if self.preprocess_for == 'resnet50': images_array = resnet50_preprocess_input(images_array) if self.y_col != None: y_onehot = np_utils.to_categorical(batch_y, num_classes=self.num_classes) return (images_array, y_onehot) else: return images_array
def preprocess_input(image, model_type): if model_type == "vgg19": return vgg19_preprocess_input(image) if model_type == "resnet50": return resnet50_preprocess_input(image) if model_type == "xception": return xception_preprocess_input(image) print(f"Using fallback for model type {model_type}") return image
def _dog_prediction(self, img_path): """For given image path returns flag whether it contains dog or not. Parameters: ----------- img_path: str Filepath to image file. Returns: ----------- result: int Value 1 if image contains human, 0 otherwise. """ img = self._path_to_tensor(img_path) img = resnet50_preprocess_input(img) result = np.argmax(self.dog_predictor.predict(img)) result = int((result <= 268) and (result >= 151)) return result
def find_faces(data, model, show_boxes=False): global num_faces # Arrays to hold all cropped face images and their corresponding labels faces = [] labels = [] # Check that the model provided is one of the expected models assert model in ("MobileNetV2", "VGG16", "ResNet50") # Iterate through each image in the provided data for image in data: # Read the image using OpenCV bgr_image = cv2.imread(filename=data_directory_prefix + "images/" + image["name"]) # For each face in the image (most images contain multiple faces) load the bounding box and label for bndbox, label in zip(image["bndboxes"], image["classes"]): # Obtain bounding box values xmin = bndbox[0] ymin = bndbox[1] xmax = bndbox[2] ymax = bndbox[3] # Many images have tiny labelled faces, exclude them here to reduce training time later limit = 30 if (xmax - xmin) > limit and (ymax - ymin) > limit: # Crop the selected face out of the original image face_image = bgr_image[ymin:ymax, xmin:xmax] # Resize the face image with OpenCV to the selected image size face_image = cv2.resize(src=face_image, dsize=(IMG_SIZE, IMG_SIZE)) if show_boxes: cv2.imshow("Cropped", face_image) cv2.waitKey(0) # Convert the cropped image to an array face_image = img_to_array(img=face_image) # Preprocess the image according to the selected model to be used if model == "ResNet50": face_image = resnet50_preprocess_input(face_image) elif model == "VGG16": face_image = vgg16_preprocess_input(face_image) elif model == "MobileNetV2": face_image = mobilenet_preprocess_input(face_image) # Add the cropped face and its label to the corresponding arrays faces.append(face_image) labels.append(label) # Optionally show the bounding boxes around the selected faces on the orignial image if show_boxes: # Choose a color based on the class if label == 0: color = (0, 255, 0) elif label == 1: color = (0, 0, 255) else: color = (0, 255, 255) # Draw a rectangle around the face bgr_image = cv2.rectangle(img=bgr_image, pt1=(xmin, ymin), pt2=(xmax, ymax), color=color, thickness=2) # Display the image if show_boxes: cv2.imshow("image", bgr_image) cv2.waitKey(0) # Convert the faces and labels arrays to np arrays faces = np.array(faces, dtype=np.float32) labels = np.array(labels) print(num_faces) # Return the new data arrays return faces, labels
def resnet50_preprocess_image(resized_image): preprocessed_image = resnet50_preprocess_input(resized_image) return preprocessed_image