def check_predict(id='6100_3_2'):
    model = get_unet()
    model.load_weights('weights/unet_10_jk0.7914')
    # 这个阈值是calc_jacc时算出来的最佳阈值
    msk = predict_id(id, model, [0.4, 0.1, 0.4, 0.3, 0.3, 0.5, 0.3, 0.6, 0.1, 0.1])
    img = M(id)
    display_predict_result(img, msk)
def get_model(shape):
    n_ch = shape[1]
    patch_height = shape[2]
    patch_width = shape[3]

    if get_net == "unet":
        model = network.get_unet(n_ch, patch_height, patch_width)
    elif get_net == "fcn":
        model = network.get_fcn(n_ch, patch_height, patch_width)
    elif get_net == "hed":
        model = network.get_hed(n_ch, patch_height, patch_width)
    elif get_net == "unet3":
        model = network.get_unet3(n_ch, patch_height, patch_width)
    elif get_net == "unet4":
        model = network.get_unet4(n_ch, patch_height, patch_width)
    elif get_net == "unet5":
        model = network.get_unet5(n_ch, patch_height, patch_width)
    elif get_net == "unet_all":
        model = network.get_unet_all(n_ch, patch_height, patch_width)
    elif get_net == "unet_dm":
        model = network.get_unet_dm(n_ch, patch_height, patch_width)
    elif get_net == "unet_dm2":
        model = network.get_unet_dm2(n_ch, patch_height, patch_width)
    elif get_net == "unet_dsm":
        model = network.get_unet_dsm(n_ch, patch_height, patch_width)
    elif get_net == "unet_br":
        model = network.get_unet_br(n_ch, patch_height, patch_width)
    elif get_net == "unet_br2":
        model = network.get_unet_br2(n_ch, patch_height, patch_width)
    elif get_net == "fcnet":
        model = network.get_fcnet(n_ch, patch_height, patch_width)
    elif get_net == "unet_5l":
        model = network.get_unet5l(n_ch, patch_height, patch_width)
    elif get_net == "unet_6l":
        model = network.get_unet6l(n_ch, patch_height, patch_width)
    elif get_net == "unet_brnew":
        model = network.get_unet_brnew(n_ch, patch_height, patch_width)
    elif get_net == "hed_crf":
        model = network.get_hedcrf(n_ch, patch_height, patch_width)
    elif get_net == "unet3_com":
        model = network.get_unet3_combine(n_ch, patch_height, patch_width)
    elif get_net == "demo":
        model = network.get_demo(n_ch, patch_height, patch_width)
    else:
        print("Please input a correct network!")
        exit(0)

    print("Check: final output of the network:", model.output_shape)
    json_string = model.to_json()
    open('./' + name_experiment + '/' + name_experiment + '_architecture.json',
         'w').write(json_string)

    return model
def train_net():
    print("start train net")
    x_val, y_val = np.load('data/x_tmp_%d.npy' % class_number), np.load('data/y_tmp_%d.npy' % class_number)
    img, msk = np.load('data/x_trn_%d.npy' % class_number), np.load('data/y_trn_%d.npy' % class_number)
    x_trn, y_trn = get_patches(img, msk)

    model = get_unet()
    model.load_weights('weights/unet_10_jk0.7878')
    # 回调函数,在epoch结束后保存在验证集上性能最好的模型
    model_checkpoint = ModelCheckpoint('weights/unet_tmp.hdf5', monitor='loss', save_best_only=True)
    model.fit(x_trn, y_trn, batch_size=64, epochs=1, verbose=1, shuffle=True,
              callbacks=[model_checkpoint], validation_data=(x_val, y_val))
    score, trs = calc_jacc(model)
    print('val jk', score)
    model.save_weights('weights/unet_10_jk%.4f' % score)

    return model
def check_predict(id='6120_2_3'):
    model = get_unet()
    model.load_weights('weights/unet_10_jk0.7878')

    msk = predict_id(id, model, [0.4, 0.1, 0.4, 0.3, 0.3, 0.5, 0.3, 0.6, 0.1, 0.1])
    img = M(id)

    plt.figure()
    ax1 = plt.subplot(131)
    ax1.set_title('image ID:6120_2_3')
    ax1.imshow(img[:, :, 5], cmap=plt.get_cmap('gist_ncar'))
    ax2 = plt.subplot(132)
    ax2.set_title('predict bldg pixels')
    ax2.imshow(msk[0], cmap=plt.get_cmap('gray'))
    ax3 = plt.subplot(133)
    ax3.set_title('predict bldg polygones')
    ax3.imshow(mask_for_polygons(mask_to_polygons(msk[0], epsilon=1), img.shape[:2]), cmap=plt.get_cmap('gray'))

    plt.show()
def train_net():
    print("start train net")
    x_val, y_val = np.load('data/x_tmp_%d.npy' % N_Cls), np.load('data/y_tmp_%d.npy' % N_Cls)
    img = np.load('data/x_trn_%d.npy' % N_Cls)
    msk = np.load('data/y_trn_%d.npy' % N_Cls)

    x_trn, y_trn = get_patches(img, msk)

    model = get_unet()
    model.load_weights('weights/unet_10_jk0.7878')
    model_checkpoint = ModelCheckpoint('weights/unet_tmp.hdf5', monitor='loss', save_best_only=True)
    for i in range(1):
        model.fit(x_trn, y_trn, batch_size=64, nb_epoch=1, verbose=1, shuffle=True,
                  callbacks=[model_checkpoint], validation_data=(x_val, y_val))
        del x_trn
        del y_trn
        x_trn, y_trn = get_patches(img, msk)
        score, trs = calc_jacc(model)
        print('val jk', score)
        model.save_weights('weights/unet_10_jk%.4f' % score)

    return model