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)
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 temp = predict(img[:,::-1,:], model, patch_sz=PATCH_SZ, n_classes=N_CLASSES).transpose([2,0,1]) #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(temp[0][0][0], temp[3][12][13])
all_sliced_tifs = [ file for file in all_sliced_tifs if file[-4:] == ".tif" ][0:3] total_files_count = len(all_sliced_tifs) for current_file_count, test_file in enumerate(all_sliced_tifs[0:]): test_filename = test_file print("...running inference for {}".format(test_filename)) test_file = "../../satellite-image/Planet.com/Planet_Data_Sliced/tif/all-sliced-tiff-850px/" + test_file class_id = 4 # img = take_4bands(normalize(tiff.imread('data/mband/{}.tif'.format(test_id)).transpose([1,2,0]))) # make channels last # img = tiff.imread(test_file).transpose([1,2,0]) # print (img) img = tiff.imread(test_file).transpose([1, 2, 0]) img, _ = get_4bands( normalize(tiff.imread(test_file).transpose( [1, 2, 0]))) # make channels last band_list = [0, 1, 2, 3] # to permute all the bands perm_list = list(itertools.permutations(band_list)) print("...the shape of given image is: ", img.shape) res_shape = tuple( [8] + list(img.shape[0:2]) + [N_CLASSES]) # [8] here comes from the 8 augmented images results = np.zeros(shape=res_shape) for i in range(8): print("...running inference for augmentation {}".format(i)) # TODO: The average taken in the following way isn't mathematically correct. Refer to the issue in original repo if i == 0: # reverse first dimension mymat = predict(img[::-1, :, :], model, patch_sz=PATCH_SZ, n_classes=N_CLASSES).transpose([2, 0, 1])
z_order = {1: 3, 2: 4, 3: 0, 4: 1, 5: 2} pict = 255 * np.ones(shape=(3, mask.shape[1], mask.shape[2]), dtype=np.uint8) for i in range(1, 6): 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() model.load_weights(weights_path) test_id = 'test' img = normalize( tiff.imread('data/mband/{}.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(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])
x0, x1 = i * dimpatch, (i + 1) * dimpatch y0, y1 = j * dimpatch, (j + 1) * dimpatch print("I:", i) print("J:", j) print("SZ:", dimpatch) print("X0,X1:", x0, x1) print("Y0,Y1:", y0, y1) prediction[x0:x1, y0:y1, :] = patches_predict[k, :, :, :] return prediction[:h, :w, :] if __name__ == '__main__': model = get_model() model.load_weights(weights_path) img = normalize(tiff.imread('data/mband/test.tif')) print("IMG H", img.shape[0]) print("IMG W", img.shape[1]) print("IMG Channel", img.shape[2]) #img = np.expand_dims(img,) xdimens = img.shape[0] ydimens = img.shape[1] s = (xdimens, ydimens) extrachannel1 = np.zeros(s) extrachannel2 = np.zeros(s) extrachannel3 = np.zeros(s) extrachannel4 = np.zeros(s) extrachannel5 = np.zeros(s) output = np.zeros((xdimens, ydimens, 8)) output[:, :, 0] = img[:, :, 0] output[:, :, 1] = img[:, :, 1]
'output/planet_classtest.tif', 'output/result.tif', 'output/map.tif', 'output/planet_result.tif', 'output/planet_map.tif' ] for file in overwrite_check: if os.path.exists(file): print( 'ERROR: file {0} already exists. Please rename or delete the following files before creating predictions:' .format(file)) print(*overwrite_check, sep='\n') sys.exit() if planet_test == False: image_id = 'test' weights_path = 'weights/150_epoch_unet_weights.hdf5' img = normalize( tiff.imread('data/mband/{}.tif'.format(image_id)).transpose( [1, 2, 0])) # make channels last elif planet_test == True: planet_imagedir = 'data/planet_training/predict/' image_id = '20180412_143154_1003_1B_AnalyticMS' # rearranging order for planet image no longer necessary now that it matches training data img = normalize( tiff.imread(planet_imagedir + '{}.tif'.format(image_id))) # add 4 channels of 0 to array to predict planet image # img_pad = ((0,0), (0,0), (0,4)) # img_fixed2 = np.pad(img, pad_width=img_pad, mode='constant', constant_values=0) #trim the planet image to the same dimensions as training data # img_fixed2 = img_fixed2[:848, :837, :] # tiff.imsave('output/planet_classtest.tif', img) # img = img_fixed2 tiff.imsave('output/img01.tif', img)
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
if item[1].isdigit() else float('inf'), item)) # #sorted_dir_list0 = sorted_dir_list0[918:] #for z in sorted_dir_list0: if __name__ == '__main__': model = get_model() model.load_weights(weights_path) #model = load_model('/home/lpe/Desktop/junk/dish_ilastik_34/output/tha_gewd_u_net_2.h5',custom_objects={'weighted_binary_crossentropy': weighted_binary_crossentropy}) #test_id = "01" #img = normalize(tiff.imread(z)).transpose([1,0,2]) # make channels last for z in sorted_dir_list0: # img = normalize(tiff.imread(z))#.transpose([1,0,2]) #img = image_resize(img,height=1173)#.transpose([1,0,2]) print(z) img = normalize(image_resize(cv2.imread(z), height=1173)) 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([0, 2, 1]) #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([1, 0, 2]) #print(temp[0][0][0], temp[3][12][13]) print("Case 2", temp.shape, mymat.shape)
overwrite_check = [ 'output/planet_classtest.tif', 'output/result.tif', 'output/map.tif', 'output/planet_result.tif', 'output/planet_map.tif' ] for file in overwrite_check: if os.path.exists(file): print( 'ERROR: file {0} already exists. Please rename or delete the following files before creating predictions:' .format(file)) print(*overwrite_check, sep='\n') sys.exit() planet_imagedir = 'data/planet_training/predict/' image_id = '20180412_143154_1003_1B_AnalyticMS' img = normalize(tiff.imread(planet_imagedir + '{}.tif'.format(image_id))) # add 4 channels of 0 to array to predict planet image # img_pad = ((0,0), (0,0), (0,4)) # img_fixed2 = np.pad(img, pad_width=img_pad, mode='constant', constant_values=0) #trim the planet image to the same dimensions as training data # img_fixed2 = img_fixed2[:848, :837, :] # tiff.imsave('output/planet_classtest.tif', img) # img = img_fixed2 for i in range(7): if i == 0: # reverse first dimension mymat = predict(img[::-1, :, :], model, patch_sz=PATCH_SZ, n_classes=N_CLASSES) print("Case 1", img.shape, mymat.shape)