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 predict(self): print("predict test data") imgs_test = self.load_test_data() model = self.get_multi() # model=self.get_multi_crf("test") model.load_weights('myFCN.h5') start = time.clock() imgs_mask_test, edge = model.predict(imgs_test, batch_size=1, verbose=1) end = time.clock() save_dir = os.path.join(os.getcwd(), "img_result") if not os.path.isdir(save_dir): os.makedirs(save_dir) with open(os.getcwd() + "/test_image_name.txt") as f: txt = f.readlines() txt = [line.split(' ') for line in txt] for i in range(len(txt)): segmentation = util.get_label_image(imgs_mask_test[i, :, :, :], int(txt[i][1]), int(txt[i][2])) segmentation.save(os.path.join(save_dir, txt[i][0][:-4] + ".png")) print('.', end='') print("Test end") print('predict time: {}s'.format(end - start))
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(): for i in range(len(mask_ids)): X_test=np.asarray([X[i]]) Y_test=np.asarray([Y[i]]) print (X_test.shape) print (Y_test.shape) tempX=X.tolist() tempY=Y.tolist() X_train=tempX[0:i]+tempX[i+1:] Y_train=tempY[0:i]+tempY[i+1:] X_train=np.asarray(X_train) Y_train=np.asarray(Y_train) print(X_train.shape) print(Y_train.shape) model=getmodel() model.fit(X_train,Y_train,epochs=400,batch_size=16) #preds=model.predict(X_test,Y_test) #preds=model.evaluate(X_test,Y_test) #print ("Loss = " + str(preds[0])) #print ("Test Accuracy = " + str(preds[1])) probs = model.predict(X_test, verbose=False)[0, :, :, :] print("Test IU score:"+str(iou_loss_core(Y_test,probs))) output_file = 'labels'+str(i)+'.png' segmentation = util.get_label_image(probs, 100, 100) segmentation.save(output_file) '''
def evaluate(self): print("evaluate test data") imgs_test, imgs_mask = self.load_labeltest_data() print("loading data done") model = self.get_multi() # model=self.get_multi_crf("test") model.load_weights('myFCN.h5') start = time.clock() imgs_mask_test, edge = model.predict(imgs_test, batch_size=1, verbose=1) end = time.clock() print('predict time: {}s'.format(end - start)) save_dir = os.path.join(os.getcwd(), "img_result") if not os.path.isdir(save_dir): os.makedirs(save_dir) edge_save_dir = os.path.join(os.getcwd(), "edge_result") if not os.path.isdir(edge_save_dir): os.makedirs(edge_save_dir) with open(os.getcwd() + "/test_image_name.txt") as f: txt = f.readlines() txt = [line.split(' ') for line in txt] pa_list = [] ma_list = [] m_IU_list = [] fw_IU_list = [] for i in range(len(txt)): segmentation = util.get_label_image(imgs_mask_test[i, :, :, :], int(txt[i][1]), int(txt[i][2])) segmentation.save(os.path.join(save_dir, txt[i][0][:-4] + ".png")) # # edge = util.get_sig_image(edge[i,:,:], int(txt[i][1]),int(txt[i][2])) # cv2.imwrite(os.path.join(edge_save_dir,txt[i][0][:-4]+".png"), edge) print('.', end='') seg = imgs_mask_test[i, :, :, :].argmax( axis=2).astype("uint8")[:int(txt[i][1]), :int(txt[i][2])] mask = imgs_mask[i, :, :][:int(txt[i][1]), :int(txt[i][2])] pa = pixel_accuracy(seg, mask) ma = mean_accuracy(seg, mask) m_IU = mean_IU(seg, mask) fw_IU = frequency_weighted_IU(seg, mask) pa_list.append(pa) ma_list.append(ma) m_IU_list.append(m_IU) fw_IU_list.append(fw_IU) print("Test evaultate end") print("pixel_accuracy: " + str(np.mean(pa_list))) print("mean_accuracy: " + str(np.mean(ma_list))) print("mean_IU: " + str(np.mean(m_IU_list))) print("frequency_weighted: " + str(np.mean(fw_IU_list)))
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 = '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 model_predict_gby(model, input_path, output_path, INPUT_SIZE): img_org = Image.open(input_path) ww, hh = img_org.size if INPUT_SIZE == None: img = img_org.resize(((ww // 32) * 32, (hh // 32) * 32)) else: # if the input size is fixed: img = img_org.resize((INPUT_SIZE, INPUT_SIZE)) img = np.array(img, dtype=np.float32) x = np.expand_dims(img, axis=0) x = preprocess_input(x) probs = model.predict(x) #pdb.set_trace() print(probs) segmentation = util.get_label_image(probs[0, :, :, :], hh, ww) segmentation.save(output_path)
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(): 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(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)
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)