def get_datagenerators_folders(image_path_224, image_path_224_crop, image_path_331, image_path_331_crop, tta=False, batch_size=100): """ This function produces a list of datagenerators from paths to all 4 preprocessed versions of images Parameters: image_path_* (string): path to corresponding image data set tta (boolean): switch to turn on test time augmentation, default is off batch_size (int): mini-batch size for making predictions Returns: datagen_list (list): a list of test data generators. """ img_util_224 = imgUtils(224) test_datagen_224_tta, test_datagen_224_notta = img_util_224.dataGen( rotation=15, h_shift=0.05, w_shift=0.05) img_util_331 = imgUtils(331) test_datagen_331_tta, test_datagen_331_notta = img_util_331.dataGen( rotation=15, h_shift=0.05, w_shift=0.05) if tta: test_datagen_224 = test_datagen_224_tta test_datagen_331 = test_datagen_331_tta else: test_datagen_224 = test_datagen_224_notta test_datagen_331 = test_datagen_331_notta test_generator_224 = img_util_224.testgenerator_from_folder( batch_size=batch_size, test=test_datagen_224, data_dir=image_path_224) test_generator_224_crop = img_util_224.testgenerator_from_folder( batch_size=batch_size, test=test_datagen_224, data_dir=image_path_224_crop) test_generator_331 = img_util_331.testgenerator_from_folder( batch_size=batch_size, test=test_datagen_331, data_dir=image_path_331) test_generator_331_crop = img_util_331.testgenerator_from_folder( batch_size=batch_size, test=test_datagen_331, data_dir=image_path_331_crop) datagenlist = [ test_generator_224, test_generator_224_crop, test_generator_331, test_generator_331_crop ] datagenlist = datagenlist * 6 return datagenlist
def get_datagenerators_file(image_path_uncrop, image_path_crop, tta=False): """ This function creates a list of image data generators corresponds to each model used to ensemble with different types of augmentation (cropped or uncopped). Parameters: image_path_crop/uncrop (string): the path to images with different augmentations. tta (boolean): a flag to check if test-time augmentation should be applied or not. Returns: datagenlist (list): a list of image data generators corresponds to each model used to ensemble. """ img_util_224 = imgUtils(224) img_util_331 = imgUtils(331) img_224 = img_util_224.proc_img(image_path_uncrop) img_331 = img_util_331.proc_img(image_path_uncrop) img_224_crop = img_util_224.proc_img(image_path_crop) img_331_crop = img_util_331.proc_img(image_path_crop) test_datagen_tta, test_datagen_no_tta = img_util_224.dataGen(rotation=15, h_shift=0.05, w_shift=0.05) if tta: test_datagen = test_datagen_tta else: test_datagen = test_datagen_no_tta test_generator_224 = test_datagen.flow(img_224, batch_size=1, shuffle=False) test_generator_224_crop = test_datagen.flow(img_224_crop, batch_size=1, shuffle=False) test_generator_331 = test_datagen.flow(img_331, batch_size=1, shuffle=False) test_generator_331_crop = test_datagen.flow(img_331_crop, batch_size=1, shuffle=False) datagenlist = [ test_generator_224, test_generator_224_crop, test_generator_331, test_generator_331_crop ] datagenlist = datagenlist * 6 return datagenlist
model_name = args.model_name img_size = args.img_size weights = os.path.normpath(args.weight_path) hyperparameters = args.hyperparameters exp_name = model_name + '_' + str(img_size) output_path = args.output if output_path is not None: output_path = os.path.normpath(output_path) else: output_path = os.getcwd() train_dir, valid_dir, weight_dir1, weight_dir2, img_dir1, img_dir2 = make_path( data_path, output_path, exp_name) img_proc = imgUtils(img_size) train_idg, val_idg = img_proc.dataGen(rotation_range, height_shift, width_shift) train_gen, val_gen = img_proc.generator(batch_size, train_idg, val_idg, train_dir, valid_dir) if hyperparameters is not None: hyperparameters = pickle.load(open(hyperparameters, "rb")) lr = hyperparameters['learning_rate'] momentum = hyperparameters['momentum'] dropout_rate = hyperparameters['dropout_rate'] else: lr = 0.001 momentum = 0.9 dropout_rate = 0.3
def get_datagenerators_folder(image_path_224, image_path_224_crop, image_path_331, image_path_331_crop, tta=False): """ This function creates a list of image data generators corresponds to each model used to ensemble using the path to directories with different types of augmentation (224x224 or 331x331 in size, cropped or uncopped). Parameters: image_path_224/331_crop/NONE (string): the path to images with different augmentations. tta (boolean): a flag to check if test-time augmentation should be applied or not. Returns: datagenlist (list): a list of image data generators corresponds to each model used to ensemble. """ img_util_224 = imgUtils(224) test_datagen_224_tta, test_datagen_224_notta = img_util_224.dataGen( rotation=15, h_shift=0.05, w_shift=0.05) img_util_331 = imgUtils(331) test_datagen_331_tta, test_datagen_331_notta = img_util_331.dataGen( rotation=15, h_shift=0.05, w_shift=0.05) if tta: test_datagen_224 = test_datagen_224_tta test_datagen_331 = test_datagen_331_tta else: test_datagen_224 = test_datagen_224_notta test_datagen_331 = test_datagen_331_notta images_224 = glob.glob(os.path.join(image_path_224, '*')) images_224 = pd.DataFrame.from_dict({'filename': images_224}) images_224_crop = glob.glob(os.path.join(image_path_224_crop, '*')) images_224_crop = pd.DataFrame.from_dict({'filename': images_224_crop}) images_331 = glob.glob(os.path.join(image_path_331, '*')) images_331 = pd.DataFrame.from_dict({'filename': images_331}) images_331_crop = glob.glob(os.path.join(image_path_331_crop, '*')) images_331_crop = pd.DataFrame.from_dict({'filename': images_331_crop}) batch_size = 100 test_generator_224 = img_util_224.testgenerator_from_dataframe( batch_size=batch_size, test=test_datagen_224, dataframe=images_224, class_mode=None) test_generator_224_crop = img_util_224.testgenerator_from_dataframe( batch_size=batch_size, test=test_datagen_224, dataframe=images_224_crop, class_mode=None) test_generator_331 = img_util_331.testgenerator_from_dataframe( batch_size=batch_size, test=test_datagen_331, dataframe=images_331, class_mode=None) test_generator_331_crop = img_util_331.testgenerator_from_dataframe( batch_size=batch_size, test=test_datagen_331, dataframe=images_331_crop, class_mode=None) datagenlist = [ test_generator_224, test_generator_224_crop, test_generator_331, test_generator_331_crop ] datagenlist = datagenlist * 6 return datagenlist
def generate_gradCAM(input_path, img_size, models, output_path=os.getcwd()): """ This function generates a Grad-CAM image for a given image. The Grad-CAM image is overlayed on the original image. The image will be demonstrated and saved in a given path. Parameters: input_path (string): the path to the image. img_size (int): the size of the input image. models (list): a list of models used to generate the Grad-CAM image. output_path (string): the path to save the output image. """ img_list = [] img_array_list = [] img_name_list = [] # current_path, input_filename = os.path.split(input_path) result_base = os.path.join(output_path, 'gradCAM_img') img_proc = imgUtils(img_size) # Create new folder to save the resulting image if not os.path.exists(result_base): os.mkdir(result_base) # Check if input path is a directory or file if os.path.isdir(input_path): for filename in os.listdir(input_path): img_path = os.path.join(input_path, filename) input_img = image.load_img(img_path, target_size=(img_size, img_size), color_mode='rgb', interpolation='lanczos') img_array = np.asarray(input_img, dtype='float64') img_array_list.append(img_array) img_preproc = img_proc.preprocess(img_array) img = np.expand_dims(img_preproc, axis=0) img_list.append(img) # Split filename for result image name base_name = os.path.basename(filename) img_name = (os.path.splitext(base_name))[0] result_name = img_name + '_gradCAM.jpg' result_path = os.path.join(result_base, result_name) img_name_list.append(result_path) else: input_img = image.load_img(input_path, target_size=(img_size, img_size), color_mode='rgb', interpolation='lanczos') img_array = np.asarray(input_img, dtype='float64') img_array_list.append(img_array) img_preproc = img_proc.preprocess(img_array) img = np.expand_dims(img_preproc, axis=0) img_list.append(img) # Split filename for result image name base_name = os.path.basename(input_path) img_name, ext = os.path.splitext(base_name) result_name = img_name + '_gradCAM.png' result_path = os.path.join(result_base, result_name) img_name_list.append(result_path) i = 0 for img in img_list: result_path = img_name_list[i] img_array = img_array_list[i] visualization = img_proc.gradCAM(img, models) plt.rcParams['figure.figsize'] = (18, 6) # Matplotlib preparations fig, axes = plt.subplots(1, 3) axes[0].imshow(img_array[..., 0], cmap='gray') axes[0].set_title('Input') axes[1].imshow(visualization) axes[1].set_title('Grad-CAM') heatmap = np.uint8(cm.jet(visualization)[..., :3] * 255) original = np.uint8(cm.gray(img_array[..., 0])[..., :3] * 255) axes[2].imshow(overlay(heatmap, original)) axes[2].set_title('Overlay') plt.savefig(result_path) i += 1