def main(patch_size, n_classes, model_path, output_filename, output_mapname, test_id, bands, class_weights): # model = get_model(n_channels=4) # test_id = 'test' # test_id = '24' img = normalize(tiff.imread('../data/mband/{}.tif'.format(test_id)).transpose([1, 2, 0])) # make channels last n_channels = len(bands) if bands[0] == -1: n_channels = 8 print("\t num_channels", n_channels) img = img[:, :, bands] model = get_model(n_classes, patch_size, n_channels, class_weights) model.load_weights(model_path) """ # Apply PCA to the image pca_path = "./pca.pkl" # open a file, where you stored the pickled data file = open(pca_path, 'rb') pca = pickle.load(file) file.close() reshaped_img_m = img.reshape((img.shape[0]*img.shape[1], img.shape[2])) reshaped_img_pca = pca.transform(reshaped_img_m) img_pca = reshaped_img_pca.reshape((img.shape[0], img.shape[1], reshaped_img_pca.shape[1])) img = img_pca """ mask = predict(img, model, patch_sz=patch_size, n_classes=n_classes).transpose([2,0,1]) # make channels first map = picture_from_mask(mask, 0.5) tiff.imsave(output_filename, (255*mask).astype('uint8')) tiff.imsave(output_mapname, map)
4: 7, 5: 1, 6: 0, 7: 4, 8: 3 } pict = 255*np.ones(shape=(3, mask.shape[1], mask.shape[2]), dtype=np.uint8) for i in range(1, 9): cl = z_order[i] for ch in range(3): pict[ch,:,:][mask[cl,:,:] > threshold] = colors[cl][ch] return pict if __name__ == '__main__': model = get_model() weights_path = "weights/unet_weights_30_50ep.hdf5" model.load_weights(weights_path) print(weights_path) for i in range(1,15): test_id = str(i) print('Predicting for data/sat/{}.tif'.format(test_id)) img = normalize(tiff.imread('data/sat/{}.tif'.format(test_id))) print(img.shape) for i in range(15): if i == 0: # reverse first dimension mymat = predict(img[::-1,:,:], model, patch_sz=PATCH_SZ, n_classes=N_CLASSES).transpose([2,0,1]) #print(mymat[0][0][0], mymat[3][12][13]) # print("Case 1",img.shape, mymat.shape) elif i == 1: # reverse second dimension
def segment_result(path): li = [os.path.join(path, i) for i in os.listdir(path)] model = get_model() model.load_weights(weights_path) count = 0 for i in li: img = normalize(tiff.imread(i).transpose([1, 2, 0])) # make channels last img1 = i.split("/")[-1] # test_id = img1.split(".")[0] print(img1) # img = normalize(tiff.imread('data/new_test/{}.tif'.format(test_id)).transpose([1,2,0])) # make channels last for i in range(7): if i == 0: # reverse first dimension mymat = predict(img[::-1, :, :], model, patch_sz=PATCH_SZ, n_classes=N_CLASSES).transpose([2, 0, 1]) print("================ mat ===================") print(mymat[0][0][0], mymat[3][12][13]) print("Case 1", img.shape, mymat.shape) elif i == 1: # reverse second dimension temp = predict(img[:, ::-1, :], model, patch_sz=PATCH_SZ, n_classes=N_CLASSES).transpose([2, 0, 1]) print("================ mat ===================") print(temp[0][0][0], temp[3][12][13]) print("Case 2", temp.shape, mymat.shape) mymat = np.mean(np.array([temp[:, ::-1, :], mymat]), axis=0) elif i == 2: # transpose(interchange) first and second dimensions temp = predict(img.transpose([1, 0, 2]), model, patch_sz=PATCH_SZ, n_classes=N_CLASSES).transpose([2, 0, 1]) print("================ mat ===================") print(temp[0][0][0], temp[3][12][13]) print("Case 3", temp.shape, mymat.shape) mymat = np.mean(np.array([temp.transpose(0, 2, 1), mymat]), axis=0) elif i == 3: temp = predict(np.rot90(img, 1), model, patch_sz=PATCH_SZ, n_classes=N_CLASSES) print("================ mat ===================") print( temp.transpose([2, 0, 1])[0][0][0], temp.transpose([2, 0, 1])[3][12][13]) print("Case 4", temp.shape, mymat.shape) mymat = np.mean(np.array( [np.rot90(temp, -1).transpose([2, 0, 1]), mymat]), axis=0) elif i == 4: temp = predict(np.rot90(img, 2), model, patch_sz=PATCH_SZ, n_classes=N_CLASSES) print("================ mat ===================") print( temp.transpose([2, 0, 1])[0][0][0], temp.transpose([2, 0, 1])[3][12][13]) print("Case 5", temp.shape, mymat.shape) mymat = np.mean(np.array( [np.rot90(temp, -2).transpose([2, 0, 1]), mymat]), axis=0) elif i == 5: temp = predict(np.rot90(img, 3), model, patch_sz=PATCH_SZ, n_classes=N_CLASSES) print("================ mat ===================") print( temp.transpose([2, 0, 1])[0][0][0], temp.transpose([2, 0, 1])[3][12][13]) print("Case 6", temp.shape, mymat.shape) mymat = np.mean(np.array( [np.rot90(temp, -3).transpose(2, 0, 1), mymat]), axis=0) else: temp = predict(img, model, patch_sz=PATCH_SZ, n_classes=N_CLASSES).transpose([2, 0, 1]) print("================ mat ===================") print(temp[0][0][0], temp[3][12][13]) print("Case 7", temp.shape, mymat.shape) mymat = np.mean(np.array([temp, mymat]), axis=0) print(mymat[0][0][0], mymat[3][12][13]) map = picture_from_mask(mymat, 0.5) mask = predict(img, model, patch_sz=PATCH_SZ, n_classes=N_CLASSES).transpose( [2, 0, 1]) # make channels first # print("====================Mask=======================") # print(mask) # print("=================map============================") map = picture_from_mask(mask, 0.5) # print(map) # tiff.imsave('data/test1/mask.tif', (255*mask).astype('uint8')) # tiff.imsave('data/test1/result.tif', (255*mymat).astype('uint8')) tiff.imsave('data/test_set/test_map/map.{}.tif'.format(img1), map) count += 1