예제 #1
0
파일: train.py 프로젝트: tdardinier/CIL
def load_data_with_padding():
    print("Loading data...")
    image_path = project_paths.TRAIN_IMAGE_PATH
    base_filenames = [filename[:-4] for filename in os.listdir(image_path) if filename.endswith(".png")]
    image_filenames = [str(image_path/(filename+".png")) for filename in base_filenames]
    mask_path = project_paths.TRAIN_GROUNDTRUTH_PATH
    mask_filenames = [str(mask_path/(filename+".png")) for filename in base_filenames]
    
    sample_image = mpimg.imread(image_filenames[0])
    num = len(image_filenames)
    h, w, c = sample_image.shape
    padding = (CONTEXT_SIZE-PATCH_SIZE)//2
    images = np.zeros((num,h+2*padding,w+2*padding,c))
    masks = np.zeros((num,h,w))
    print("Loading images...")
    for i,filename in enumerate(image_filenames):
        print(".",end="")
        image = mpimg.imread(filename)[:,:,:3]
        images[i] = tools.pad_with_reflection(image, padding)
    print("\nLoading masks...")
    for i,filename in enumerate(mask_filenames):
        print(".",end="")
        mask = mpimg.imread(filename)
        if mask.ndim > 2:
            mask = mask[:,:,0]
        masks[i] = mask
        
    print("\nDone loading!")
    return images, masks
예제 #2
0
def main():
    print("b Classifying with {}...".format(MODEL_NAME))
    model_path = str(project_paths.MODELS_PATH / "patch" / (MODEL_NAME+".h5"))
    
    model = load_model(model_path, custom_objects={})

    image_path = project_paths.TEST_DIR_PATH
    filenames = [filename for filename in os.listdir(image_path) if filename.endswith(".png")]
    full_filenames = [str(image_path/filename) for filename in filenames]
    
    generate.create_folder(project_paths.MASKS_TEST_PATH / "patch")
    generate.create_folder(project_paths.RESULTS_TEST_PATH / "patch")
    generate.create_folder(project_paths.RESULTS_TEST_PATH / "patchgray")
    generate.create_folder(project_paths.SUBMISSIONS_PATH / "patch")

    datagen = ImageDataGenerator(
        shear_range=0.1,
        zoom_range=0.1,
        horizontal_flip=True,
        rotation_range=20.,
        fill_mode='reflect',
        width_shift_range = 0.05, 
        height_shift_range = 0.05)
    
    for file_idx,filename in enumerate(full_filenames):
        base_name, _ = filenames[file_idx].split(".")
        print("Generating mask for {}...".format(base_name))
        image = mpimg.imread(filename)
        imgheight = image.shape[0]
        imgwidth = image.shape[1]
        image = tools.pad_with_reflection(image, (CONTEXT_SIZE-PATCH_SIZE)//2)
        list_patches = tools.img_crop(image, CONTEXT_SIZE, CONTEXT_SIZE, PATCH_SIZE, PATCH_SIZE)
        
        predictions = []
        for _ in range(7):
            list_labels = model.predict(datagen.flow(list_patches, batch_size=len(list_patches), shuffle=False))
            predictions.append(list_labels)
        list_labels = np.mean(predictions, axis=0)
    
        mask = np.zeros((imgheight, imgwidth))
        gray_mask = np.zeros((imgheight, imgwidth))
        
        idx = 0
        for i in range(0,imgheight,PATCH_SIZE):
            for j in range(0,imgwidth,PATCH_SIZE):
                label = list_labels[idx]
                idx += 1
                gray_mask[i:i+PATCH_SIZE, j:j+PATCH_SIZE] = label[1]
                if (label[1] > label[0]):
                    mask[i:i+PATCH_SIZE, j:j+PATCH_SIZE] = 1
        
        mask_img = tools.solution_to_img(mask)
        mpimg.imsave(project_paths.MASKS_TEST_PATH / "patch" / (base_name+".png"), mask_img)
        mpimg.imsave(project_paths.RESULTS_TEST_PATH / "patchgray" / (base_name+".png"), gray_mask, cmap="gray")
        
        img = mpimg.imread(str(project_paths.TEST_DIR_PATH / (base_name+".png")))
        overlay = tools.make_img_overlay(img, mask)
        mpimg.imsave(project_paths.RESULTS_TEST_PATH / "patch" / (base_name+".png"), overlay)
    
    
    mask_path = project_paths.MASKS_TEST_PATH / "patch"
    image_filenames =  [str(mask_path / filename) for filename in os.listdir(mask_path) if filename.endswith(".png")]
    mask_to_submission.masks_to_submission(project_paths.SUBMISSIONS_PATH / "patch" / "submission.csv", *image_filenames)
예제 #3
0
def CrossVal():
    print("Training model pre_patch...")
    
    images, masks = load_data_with_padding()
    
    num_images = images.shape[0]
    num_val = int(0.2*num_images)
    
    np.random.seed(3)
    indices = np.random.permutation(num_images)

    generate.create_folder(project_paths.MODELS_PATH / "patch")
    
    tta_gen = ImageDataGenerator(
        shear_range=0.1,
        zoom_range=0.1,
        horizontal_flip=True,
        rotation_range=20.,
        fill_mode='reflect',
        width_shift_range = 0.05, 
        height_shift_range = 0.05)
    
    reduce_lr = ReduceLROnPlateau(monitor="loss", factor=0.5, patience=REDUCE_LR_PATIENCE, verbose=1)
    class_weight = {0: 1., 1: 3.}
        
    for k in range(5):
        print("Making fold {}...".format(k))
        model = BuildModel()
        val_idx = indices[k*num_val:(k+1)*num_val]
        train_idx = np.concatenate([indices[:k*num_val], indices[(k+1)*num_val:]])
        train_batch_generator = make_data_generator(images, masks, train_idx, BATCH_SIZE)
        
        model.fit_generator(train_batch_generator,
                            steps_per_epoch = 50,
                            epochs = NUM_EPOCHS,
                            verbose = 2,
                            callbacks = [reduce_lr],
                            class_weight = class_weight)
    
    
        val_imgs, val_masks = images[val_idx], masks[val_idx]
        n,h,w, = val_masks.shape
        
        predicted = np.zeros(n*h*w)
        predicted_post = np.zeros(n*h*w)
        predicted_tta = np.zeros(n*h*w)
        predicted_tta_post = np.zeros(n*h*w)
        groundtruth = val_masks.reshape(n*h*w)
        
        for idx, image in enumerate(val_imgs):
            print("Generating mask {}...".format(idx))
            image = tools.pad_with_reflection(image, (CONTEXT_SIZE-PATCH_SIZE)//2)
            list_patches = np.array(tools.img_crop(image, CONTEXT_SIZE, CONTEXT_SIZE, PATCH_SIZE, PATCH_SIZE))

            predictions = []
            for _ in range(5):
                list_labels = model.predict(tta_gen.flow(list_patches, batch_size=len(list_patches), shuffle=False))
                predictions.append(list_labels)
                gc.collect()
            list_labels_tta = np.mean(predictions, axis=0)
            list_labels = model.predict(list_patches)

            mask = np.zeros((h, w))
            gray_mask = np.zeros((h, w))
            mask_tta = np.zeros((h, w))
            gray_mask_tta = np.zeros((h, w))

            patch_idx = 0
            for i in range(0,h,PATCH_SIZE):
                for j in range(0,w,PATCH_SIZE):
                    label = list_labels[patch_idx]
                    label_tta = list_labels_tta[patch_idx]
                    patch_idx += 1
                    gray_mask[i:i+PATCH_SIZE, j:j+PATCH_SIZE] = label[1]
                    gray_mask_tta[i:i+PATCH_SIZE, j:j+PATCH_SIZE] = label_tta[1]
                    if (label[1] > label[0]):
                        mask[i:i+PATCH_SIZE, j:j+PATCH_SIZE] = 1
                    if (label_tta[1] > label_tta[0]):
                        mask_tta[i:i+PATCH_SIZE, j:j+PATCH_SIZE] = 1
            
            mask_post =  post_processing(gray_mask)
            mask_tta_post =  post_processing(gray_mask_tta)
            
            predicted[idx*h*w:(idx+1)*h*w] = mask.reshape(h*w)
            predicted_post[idx*h*w:(idx+1)*h*w] = mask_post.reshape(h*w)
            predicted_tta[idx*h*w:(idx+1)*h*w] = mask_tta.reshape(h*w)
            predicted_tta_post[idx*h*w:(idx+1)*h*w] = mask_tta_post.reshape(h*w)
            groundtruth[idx*h*w:(idx+1)*h*w] = val_masks[idx].reshape(h*w)
            gc.collect()
            
            
        np.save(str(project_paths.MODELS_PATH / "patch" / "base_{}.npy".format(k)), predicted)
        np.save(str(project_paths.MODELS_PATH / "patch" / "base_post_{}.npy".format(k)), predicted_post)
        np.save(str(project_paths.MODELS_PATH / "patch" / "base_tta_{}.npy".format(k)), predicted)
        np.save(str(project_paths.MODELS_PATH / "patch" / "base_tta_post_{}.npy".format(k)), predicted_post)
        np.save(str(project_paths.MODELS_PATH / "patch" / "truth_{}.npy".format(k)), groundtruth)