Esempio n. 1
0
def make_predict(img_obj, train_obj, test_img):
    # just makes the predicted image
    print "Loading trained model..."
    test_model = os.path.join(train_obj.model_path,
                              train_obj.model_name + ".hdf5")
    mean_t, std_t = np.load(
        os.path.join(img_obj.out_path, img_obj.out_name + "_meanstd.npy"))

    if train_obj.model_arch == "vanilla_unet":
        model = vanilla_unet()
        model.load_weights(test_model)
    elif train_obj.model_arch == "rob_unet":
        model = rob_unet()
        model.load_weights(test_model)
    elif train_obj.model_arch == "dil_unet":
        model = dil_unet()
        model.load_weights(test_model)
    else:
        print "you didn't chose one of the possible model architecutres: "
        print "- vanilla_unet \n -rob_unet \n dil_unet"

    print "Loading test image..."

    img = np.load(test_img)
    print "Trying to segment..."
    predict = seg_img(model, img, mean_t, std_t)
    #predicted_imgs.append(predict)
    print "Saving Segmentation Result..."

    np.save(os.path.join(train_obj.model_path, train_obj.model_name + ".npy"),
            predict)

    return
Esempio n. 2
0
def train_model(img_obj, train_obj, log=1):
    ''' Train a model for the configuration provided'''

    # get training imgs, labels and val imgs, labels
    train_img, train_label, val_img, val_label = load_patch(
        patch_path=img_obj.out_path, patch_name=img_obj.out_name)

    # get arguments for the data augumentation generator
    gen_arg = train_obj.get_data_aug()

    # create the data augumentation generator .. bs is batch size
    combined = aug_gen(train_img, train_label, bs=32, **gen_arg)

    psize = img_obj.patch_size * img_obj.patch_scale
    if train_obj.model_arch == "vanilla_unet":
        model = vanilla_unet(patch_size=(psize, psize),
                             dropout=train_obj.model_dropout)
    elif train_obj.model_arch == "rob_unet":
        model = rob_unet(patch_size=(psize, psize),
                         dropout=train_obj.model_dropout)
    elif train_obj.model_arch == "dil_unet":
        model = dil_unet(patch_size=(psize, psize),
                         dropout=train_obj.model_dropout)
    else:
        print "you didn't chose one of the possible model architecutres: "
        print "- vanilla_unet \n -rob_unet \n dil_unet"

    # this looks more scary than it really is , I made the property names same as
    # the arugument names for a reason!
    run_dict = train_seg(train_patch=train_img,
                         train_label=train_label,
                         val_patch=val_img,
                         val_label=val_label,
                         img_gen=combined,
                         model=model,
                         model_path=train_obj.model_path,
                         model_name=train_obj.model_name,
                         epochs=train_obj.model_epoch,
                         monitor=train_obj.model_monitor,
                         early_stop=train_obj.early_stopping,
                         patience=train_obj.model_patience)

    print run_dict

    if (log):
        make_log(img_obj, train_obj, **run_dict)

    return
Esempio n. 3
0
def make_predict(img_obj,train_obj,test_img):
    print "Loading trained model..."
    test_model=os.path.join(train_obj.model_path,train_obj.model_name+".hdf5")
    mean_t, std_t = np.load(os.path.join(img_obj.out_path,img_obj.out_name+"_meanstd.npy"))
    
    if train_obj.model_arch=="vanilla_unet":
        model = vanilla_unet()
        model.load_weights(test_model)
    elif train_obj.model_arch=="rob_unet":
        model = rob_unet()
        model.load_weights(test_model)
    elif train_obj.model_arch=="dil_unet":
        model=dil_unet()
        model.load_weights(test_model)
    else:
        print "you didn't chose one of the possible model architecutres: "
        print "- vanilla_unet \n -rob_unet \n dil_unet"

    print "Loading test image..."

        
    img = np.load(test_img)
    img_rgb = rgb_images(img,period)
    img_rgb = 100.0*(img_rgb-img_rgb.min(axis=(1,2),keepdims = True))/(img_rgb.max(axis=(1,2),keepdims = True)
                                                                       -img_rgb.min(axis=(1,2),keepdims = True))
    img_rgb-=mean_t
    img_rgb/=std_t
    img_rgb,nw,nz  = pad_images(img_rgb)

    print "Segmenting test image..."
    predict = model.predict(img_rgb)
    predict = predict.transpose(1,2,0,3)[:-nw,:-nz,:,0]
    predict = predict[:,:-nw,:-nz,0]
    
    #predicted_imgs.append(predict)
    print "Saving Segmentation Result..."
    
    np.save(os.path.join(train_obj.model_path,train_obj.model_name+".npy"),predict)

    return
Esempio n. 4
0
def make_predict(img_obj,train_obj,img_test):
    test_model=os.path.join(train_obj.model_path,train_obj.model_name+".hdf5")
    mean_t, std_t = np.load(os.path.join(img_obj.out_path,img_obj.out_name+"_meanstd.npy"))
    
    if train_obj.model_arch=="vanilla_unet":
        model = vanilla_unet()
        model.load_weights(test_model)
    elif train_obj.model_arch=="rob_unet":
        model = rob_unet()
        model.load_weights(test_model)
    elif train_obj.model_arch=="dil_unet":
        model=dil_unet()
        model.load_weights(test_model)
    else:
        print "you didn't chose one of the possible model architecutres: "
        print "- vanilla_unet \n -rob_unet \n dil_unet"


    #unsure how i want to load in the imgs...
    #predict = unet.predict(img_rgb)
    #predict = predict.transpose(1,2,0,3)[:-nw,:-nz,:,0]   
    return