def get_predictions(): batch_size = 100 n = 0 results = {"prediction": [], "error": []} num_images = len(images_paths) while True: if (n * batch_size) >= num_images: break next_batch_list = [] error_indices = [] for index_in_batch, i in enumerate(range(n*batch_size, min((n + 1)*batch_size, num_images))): img_path = images_paths[i] try: preprocssed_img = utils.preprocess_img(os.path.join(image_folder_path, img_path), model_input_shape, preprocessing) next_batch_list.append(preprocssed_img) except IOError as e: print("Cannot read the image '{}', skipping it. Error: {}".format(img_path, e)) error_indices.append(index_in_batch) next_batch = np.array(next_batch_list) prediction_batch = model.predict(next_batch).tolist() error_batch = [0] * len(prediction_batch) for err_index in error_indices: prediction_batch.insert(err_index, None) error_batch.insert(err_index, 1) results["prediction"].extend(prediction_batch) results["error"].extend(error_batch) n+=1 print("{} images treated, out of {}".format(min(n * batch_size, num_images), num_images)) return results
def augmentation_generator(df_imgs, image_folder_path, batch_size, n_augmentation, input_shape, labels, preprocessing, TrainImageGen): nb_imgs = df_imgs.shape[0] batch_size_adapted = int(batch_size / n_augmentation) nb_batch = int(math.ceil(nb_imgs * 1.0 / batch_size_adapted)) while True: for num_batch in range(nb_batch): df_imgs_batch = df_imgs.iloc[num_batch * batch_size_adapted:(num_batch + 1) * batch_size_adapted, :] nb_imgs_batch = df_imgs_batch.shape[0] X_batch_list = [] y_batch_list = [] for num_img in range(nb_imgs_batch): row = df_imgs_batch.iloc[num_img, :] img_filename = row[constants.FILENAME] img_path = utils.get_file_path(image_folder_path, img_filename) label = row[constants.LABEL] label_index = labels.index(label) try: x = utils.preprocess_img(img_path, input_shape, preprocessing) x = np.tile(x, (n_augmentation, 1, 1, 1)) # TrainImageGen returns infinite loop, each of which yields batch data for batch in TrainImageGen.flow(x, batch_size=n_augmentation): X_batch_list.append(batch) y_batch_list.extend([label_index] * n_augmentation) break except IOError as e: print("Cannot read the image '{}', skipping it. Error: {}". format(img_filename, e)) X_batch = np.concatenate(X_batch_list) actual_batch_size = X_batch.shape[0] y_batch = np.zeros((actual_batch_size, n_classes)) y_batch[range(actual_batch_size), y_batch_list] = 1 yield (X_batch, y_batch)
def no_augmentation_generator(df_imgs, image_folder_path, batch_size, input_shape, labels, preprocessing): nb_imgs = df_imgs.shape[0] nb_batch = int(math.ceil(nb_imgs * 1.0 / batch_size)) while True: for num_batch in range(nb_batch): df_imgs_batch = df_imgs.iloc[num_batch * batch_size:(num_batch + 1) * batch_size, :] nb_imgs_batch = df_imgs_batch.shape[0] X_batch_list = [] y_batch_list = [] for num_img in range(nb_imgs_batch): row = df_imgs_batch.iloc[num_img, :] img_filename = row[constants.FILENAME] img_path = utils.get_file_path(image_folder_path, img_filename) label = row[constants.LABEL] label_index = labels.index(label) try: x = utils.preprocess_img(img_path, input_shape, preprocessing) X_batch_list.append(x) y_batch_list.append(label_index) except IOError as e: print("Cannot read the image '{}', skipping it. Error: {}". format(img_filename, e)) X_batch = np.array(X_batch_list) actual_batch_size = X_batch.shape[0] y_batch = np.zeros((actual_batch_size, n_classes)) y_batch[range(actual_batch_size), y_batch_list] = 1 yield (X_batch, y_batch)