def main(): parser = argparse.ArgumentParser() parser.add_argument( '--model', help='full path to the .h5 model (download from https://goo.gl/ciEYZi)', required=True) parser.add_argument('--image', help='full path to the image', required=True) parser.add_argument('--output', help='full path to the output label image', default=None) args = parser.parse_args() saved_model_path = args.model input_file = args.image output_file = args.output or input_file + '_labels.png' model = get_crfrnn_model_def() model.load_weights(saved_model_path) img_data, img_h, img_w, original_size = util.get_preprocessed_image( input_file) probs = model.predict(img_data, verbose=False)[0] segmentation = util.get_label_image(probs, img_h, img_w, original_size) segmentation.save(output_file)
def main(save=True): folder = os.path.dirname(__file__) input_file = os.path.join(folder, 'image.jpg') # Download the model from https://goo.gl/ciEYZi saved_model_path = os.path.join(folder, 'crfrnn_keras_model.h5') if not os.path.isfile(saved_model_path): raise IOError('No file at %s. Download from https://goo.gl/ciEYZi' % saved_model_path) img_data, img_h, img_w = get_preprocessed_image(input_file) img_data = np.concatenate((img_data, img_data[:, :, -1::-1, :]), axis=0) model = get_crfrnn_model_def() model.load_weights(saved_model_path) probs = model.predict(img_data, verbose=False) if save: for i, prob in enumerate(probs): get_label_image(prob, img_h, img_w).save( os.path.join(folder, 'out%d.png' % i)) else: import matplotlib.pyplot as plt fig, axes = plt.subplots(1, len(probs)) if len(probs) == 1: axes = [axes] for ax, prob in zip(axes, probs): ax.imshow(get_label_image(prob, img_h, img_w)) plt.show()
def main(): input_file = 'image.jpg' output_file = 'labels.png' segment_file = 'segment.jpg' # Download the model from https://goo.gl/ciEYZi saved_model_path = 'crfrnn_keras_model.h5' model = get_crfrnn_model_def(num_segs=1) model.load_weights(saved_model_path) img_data, img_h, img_w = util.get_preprocessed_image(input_file) seg_data, seg_h, seg_w = util.get_preprocessed_image(segment_file) # img_data = img_data.reshape([1, 500, 500, 3]) # seg_data = seg_data.reshape([1, 500, 500, 3]) probs = model.predict([img_data, seg_data], verbose=False)[0, :, :, :] segmentation = util.get_label_image(probs, img_h, img_w) segmentation.save(output_file)
def main(): input_file = 'image.jpg' output_file = 'labels.png' # Download the model from https://goo.gl/ciEYZi saved_model_path = 'crfrnn_keras_model.h5' model = get_crfrnn_model_def() model.load_weights(saved_model_path) img_data, img_h, img_w = util.get_preprocessed_image(input_file) probs = model.predict(img_data, verbose=False)[0, :, :, :] segmentation = util.get_label_image(probs, img_h, img_w) segmentation.save(output_file)
def main(input_file="data/image.jpg", output_file="result/crf_result.png"): # 构造模型 crf_as_rnn_model = CRFasRNN(500, 500, num_class=8) input_image = crf_as_rnn_model.img_input output_op = crf_as_rnn_model.build_net(input_image) # 读数据、减均值、pad大小到500, 返回处理后的数据和原始大小 img_data, img_h, img_w = util.get_preprocessed_image(input_file) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) probs = sess.run(output_op, feed_dict={input_image: img_data}) # 从概率到着色图片 segmentation = util.get_label_image(probs[0], img_h, img_w) segmentation.save(output_file) pass
def main(saved_model_path, input_file_path): input_file_dir, input_file_basename = os.path.split(input_file_path) input_file_name, input_file_ext = os.path.splitext(input_file_basename) output_file_path = input_file_dir + input_file_name + '_labels.png' print('runnnig model %s on image %s, write to %s' % (saved_model_path, input_file_path, output_file_path)) # Download the model from https://goo.gl/ciEYZi #saved_model_path = 'crfrnn_keras_model.h5' #model = get_crfrnn_model_def() model = get_fcn8_model_def() model.load_weights(saved_model_path) img_data, img_h, img_w = util.get_preprocessed_image(input_file_path) probs = model.predict(img_data, verbose=False)[0, :, :, :] pdb.set_trace() segmentation = util.get_label_image(probs, img_h, img_w) segmentation.save(output_file_path)
def main(): input_file = 'hiking.jpg' mask_file = 'labels.png' # Download the model from https://goo.gl/ciEYZi saved_model_path = 'crfrnn_keras_model.h5' model = get_crfrnn_model_def() model.load_weights(saved_model_path) img_data, img_h, img_w, size = util.get_preprocessed_image(input_file) probs = model.predict(img_data, verbose=False)[0] segmentation = util.get_label_image(probs, img_h, img_w, size) segmentation.save(mask_file) input_content = cv2.imread(input_file) mask_content = cv2.imread(mask_file) input_content[mask_content == 0] = 255 print(input_content.shape) cv2.imwrite('output.jpg', input_content)
def test(): # Set input and output dirs input_dir = "/storage/cfmata/deeplab/crf_rnn/crfasrnn_keras/data/horse_fine/images_orig/" output_dir = "/storage/cfmata/deeplab/crf_rnn/crfasrnn_keras/image_results/horse_fine/fcn/" input_size = 224 num_crf_iter = 10 saved_model_path = 'results/horse_fine/horse_fine_weights.500-0.53' #model = get_crfrnn_model_def() model = load_model_gby('fcn_RESNET50_8s', input_size, 22, num_crf_iter) model.load_weights(saved_model_path) im_list = open("lst/horsecoarse_test.txt").readlines() im_list = [f[:-1] for f in im_list] for img in im_list: img_data, img_h, img_w = util.get_preprocessed_image(input_dir + img+ ".jpg") probs = model.predict(img_data, verbose=False, batch_size=1)[0, :, :, :] segmentation = util.get_label_image(probs, img_h, img_w) print(output_dir + img) segmentation.save(output_dir + img[:-4] + ".png")
def main(): input_file = inp_f tmp = path_leaf(inp_f) print(tmp) output_file = '../saliency_maps/' + tmp # Download the model from https://goo.gl/ciEYZi saved_model_path = 'crfrnn_keras_model.h5' model = get_crfrnn_model_def() model.load_weights(saved_model_path) img_data, img_h, img_w = util.get_preprocessed_image(input_file) probs = model.predict(img_data, verbose=False)[0, :, :, :] segmentation = util.get_label_image(probs, img_h, img_w) a = np.array(segmentation) print(np.shape(a)) for i in range(a.shape[0]): for j in range(a.shape[1]): if (a[i][j] > 0): a[i][j] = 255 segmentation = PIL.Image.fromarray(a) segmentation.save(output_file)
IMAGE_PATH = './images/' MASKS_PATH = './masks/' image_ids=[] mask_ids=[] channels, height, width = 3, 100, 100 for root,dirs,image_ids in os.walk(IMAGE_PATH): print(image_ids) for rroot,rdirs,mask_ids in os.walk(MASKS_PATH): print(mask_ids) image_ids.sort() mask_ids.sort() X = np.zeros((len(mask_ids), height, width,3), dtype=np.float32) for i in range(len(image_ids)): X[i],imgh,imgw=util.get_preprocessed_image(IMAGE_PATH+image_ids[i]) Y = np.zeros((len(mask_ids), height, width,5), dtype=np.int32) labelpattern=[[255,255,255,255], #white, background [0,128,0,255], #irrigate 01 [0,0,255,255], #non-irrigate [0,0,0,255], #irrigate 02 [255,192,203,255]] #irrigate 03 for i in range(len(mask_ids)): im = np.array(Image.open(MASKS_PATH+mask_ids[i])).astype(np.int32) for j in range(len(labelpattern)): temp=im-labelpattern[j] temp=np.sum(temp,axis=2) b=np.where(temp==0) temparray=np.zeros(5) temparray[j]=1
def save_mask(model, input_file, output_file): img_data, img_h, img_w = util.get_preprocessed_image(input_file) probs = model.predict(img_data, verbose=False)[0, :, :, :] segmentation = util.get_label_image(probs, img_h, img_w) segmentation.save(output_file)
def main(): input_file = 'image.jpg' output_file = 'labels.png' # Download the model from https://goo.gl/ciEYZi saved_model_path = 'crfrnn_keras_model.h5' model = get_crfrnn_model_def() from keras.optimizers import Adam #model = build_model() model.compile(optimizer=Adam(), loss='categorical_crossentropy', metrics=['categorical_accuracy']) # we create two instances with the same arguments data_gen_args = dict(featurewise_center=True, featurewise_std_normalization=True, rotation_range=90, width_shift_range=0.1, height_shift_range=0.1, zoom_range=0.2) image_datagen = keras.preprocessing.image.ImageDataGenerator( **data_gen_args) mask_datagen = keras.preprocessing.image.ImageDataGenerator( **data_gen_args) # Provide the same seed and keyword arguments to the fit and flow methods seed = 1 #xtrain=/home/qwe/Downloads/crfrnn2/xtrain #ytrain='/home/qwe/Downloads/crfrnn2/ytrain' #grayx=cv2.cvtColor(xtrain,cv2.COLOR_BGR2GRAY) #pathx='/home/qwe/Downloads/crfrnn2/xtrain/*.*' #pathy='/home/qwe/Downloads/crfrnn2/ytrain' #c2='/home/qwe/Downloads/crfrnn2/ytrain' #c1='/home/qwe/Downloads/crfrnn2/xtrain' #xtrain= get_image_files(c2) #ytrain=get_image_files(c1) #image_datagen.fit(xtrain) #mask_datagen.fit(ytain, augment=False, seed=seed) # Provide the same seed and keyword arguments to the fit and flow methods image_generator = image_datagen.flow_from_directory('xtrain', target_size=(500, 500), class_mode=None, seed=seed) mask_generator = mask_datagen.flow_from_directory('ytrain', target_size=(500, 500), class_mode=None, seed=seed) # combine generators into one which yields image and masks train_generator = zip(image_generator, mask_generator) #print(type(train_generator),'akash') model.fit_generator(train_generator, steps_per_epoch=2000, epochs=50) img_data, img_h, img_w = util.get_preprocessed_image(input_file) probs = model.predict(img_data, verbose=False)[0, :, :, :] segmentation = util.get_label_image(probs, img_h, img_w) segmentation.save(output_file)
def main(): input_file = 'image.jpg' output_file = 'labels.png' # Download the model from https://goo.gl/ciEYZi saved_model_path = 'crfrnn_keras_model.h5' model = get_crfrnn_model_def() parser = argparse.ArgumentParser() n_classes = args.n_classes #model_name = args.model_name images_path = args.test_images input_width = args.input_width input_height = args.input_height epoch_number = args.epoch_number parser.add_argument("--epoch_number", type=int, default=1) parser.add_argument( "--test_images", type=str, default= "/home/qwe/Downloads/leaf-image-segmentation-segnet-master/predict/0.png" ) parser.add_argument( "--output_path", type=str, default= "/home/qwe/Downloads/leaf-image-segmentation-segnet-master/predictans") parser.add_argument("--input_height", type=int, default=500) parser.add_argument("--input_width", type=int, default=500) #parser.add_argument("--model_name", type = str , default = "vgg_segnet") parser.add_argument("--n_classes", type=int, default=3) #print(sys.argv) parser.add_argument('--validate', action='store_true') #parser.add_argument("--model_name", type=str, default="vgg_segnet") parser.add_argument("--optimizer_name", type=str, default="adadelta") args = parser.parse_known_args()[0] #m = modelFN(n_classes, input_height=input_height, input_width=input_width) m.compile(loss='categorical_crossentropy', optimizer='adadelta', metrics=['accuracy']) #if len(load_weights) > 0: # m.load_weights(load_weights) print("Model output shape", m.output_shape) output_height = m.outputHeight output_width = m.outputWidth G = imageSegmentationGenerator('xtrain/', 'ytrain/', 8, n_classes, input_height, input_width, output_height, output_width) #if validate: # G2 = imageSegmentationGenerator(val_images_path, val_segs_path, val_batch_size, n_classes, input_height, # input_width, output_height, output_width) #if not validate: for ep in range(1): m.fit_generator(G, 100, epochs=1) #m.save_weights(save_weights_path + "." + str(ep)) #m.save(save_weights_path + ".model." + str(ep)) #else: # for ep in range(epochs): # m.fit_generator(G, 512, validation_data=G2, validation_steps=200, epochs=1) # m.save_weights(save_weights_path + "." + str(ep)) # m.save(save_weights_path + ".model." + str(ep)) img_data, img_h, img_w = util.get_preprocessed_image(input_file) probs = model.predict(img_data, verbose=False)[0, :, :, :] segmentation = util.get_label_image(probs, img_h, img_w) segmentation.save(output_file)