def predict_image(id): # load image, switch to BGR, subtract mean, and make dims C x H x W for Caffe VOCopts_seg_imgsetpath = os.path.join(VOCopts['datadir'], VOCopts['dataset'], 'ImageSets/Segmentation/{}.txt'.format(VOCopts['testset'])) with open(VOCopts_seg_imgsetpath, 'r') as rf: gtids = [i.replace('\n', '') for i in rf.readlines()] num = 1 total_time = 0 for imname in gtids: start_time = datetime.datetime.now() if not imname: continue imgpath = os.path.join(VOCopts['datadir'], VOCopts['dataset'], 'JPEGImages/{}.jpg'.format(imname)) print '读取图片: {}'.format(imgpath) im = Image.open(imgpath).resize((1008,1100)) in_ = np.array(im, dtype=np.float32) in_ = in_[:, :, ::-1] in_ -= np.array((104.00698793, 116.66876762, 122.67891434)) in_ = in_.transpose((2, 0, 1)) # shape for input (data blob is N x C x H x W), set data net.blobs['data'].reshape(1, *in_.shape) net.blobs['data'].data[...] = in_ # np.save("infer.npy", np.array(net.blobs['data'].data)) # run net and take argmax foim = im.resize((500, 500))r prediction net.forward() out = net.blobs['score'].data[0].argmax(axis=0) print out.max() out_im = Image.fromarray(out.astype('uint8')) print out_im.mode end_time = datetime.datetime.now() process_time = end_time - start_time total_time += process_time.total_seconds() print '处理图片第{}张图片{} 耗时{}'.format(num, imname, process_time) print '总耗时{}, 平均耗时{}'.format(total_time, total_time / num) num += 1 save_path = os.path.join(VOCopts['resdir'], 'Segmentation/{}_{}_cls/'.format(id, VOCopts['testset'])) check_dir(save_path) resfile = os.path.join(VOCopts['resdir'], 'Segmentation/{}_{}_cls/{}.png'.format(id, VOCopts['testset'], imname)) out_im.putpalette(np.array(colormap).reshape(-1)) out_im.save(resfile) # visualize segmentation in PASCAL VOC colors voc_palette = vis.make_palette(21) out_palette_im = Image.fromarray(vis.color_seg(out, voc_palette)) check_dir('./valresult/') out_palette_im.save('./valresult/out_palette_{}.png'.format(imname)) masked_im = Image.fromarray(vis.vis_seg(im, out, voc_palette)) masked_im.save('./valresult/out_palette_{}_visualization.png'.format(imname)) print '存储结果图片: {}'.format(resfile)
def segment(img): time_start = time.time() caffe.set_device(0) caffe.set_mode_gpu() im = modifySize(Image.open(img)) if (im.mode == "RGBA"): im = im.convert("RGB") in_ = np.array(im, dtype=np.float32) in_ = in_[:, :, ::-1] in_ -= np.array((104.00698793, 116.66876762, 122.67891434)) in_ = in_.transpose((2, 0, 1)) # load net #net = caffe.Net('Server/FCN/voc-fcn8s/deploy.prototxt', 'Server/FCN/voc-fcn8s/fcn8s-heavy-pascal.caffemodel', caffe.TEST) # shape for input (data blob is N x C x H x W), set data net.blobs['data'].reshape(1, *in_.shape) net.blobs['data'].data[...] = in_ # run net and take argmax for prediction net.forward() out = net.blobs['score'].data[0].argmax(axis=0) # visualize segmentation in PASCAL VOC colors voc_palette = vis.make_palette(21) out_im = Image.fromarray(vis.color_seg(out, voc_palette)) imgNames = img.split('.') output = os.path.join("./static/image", imgNames[0] + ".output.png") out_im.save(output) masked_im = Image.fromarray(vis.vis_seg(im, out, voc_palette)) visualization = os.path.join("./static/image", imgNames[0] + ".vis.jpg") masked_im.save(visualization) time_end = time.time() print('totally cost', time_end - time_start) return time_end - time_start
def segment(img): # load image, switch to BGR, subtract mean, and make dims C x H x W for Caffe print "start" im = modifySize(Image.open(img)) in_ = np.array(im, dtype=np.float32) in_ = in_[:, :, ::-1] in_ -= np.array((104.00698793, 116.66876762, 122.67891434)) in_ = in_.transpose((2, 0, 1)) net = caffe.Net('Server/FCN/voc-fcn8s/deploy.prototxt', 'Server/FCN/voc-fcn8s/fcn8s-heavy-pascal.caffemodel', caffe.TEST) time_start = time.time() # shape for input (data blob is N x C x H x W), set data net.blobs['data'].reshape(1, *in_.shape) net.blobs['data'].data[...] = in_ # run net and take argmax for prediction print "forward" net.forward() out = net.blobs['score'].data[0].argmax(axis=0) print "end forward" # visualize segmentation in PASCAL VOC colors voc_palette = vis.make_palette(21) out_im = Image.fromarray(vis.color_seg(out, voc_palette)) time_end = time.time() print('totally cost', time_end - time_start) imgNames = img.split('.') output = os.path.join("./static/image", imgNames[0] + "output.png") out_im.save(output) masked_im = Image.fromarray(vis.vis_seg(im, out, voc_palette)) visualization = output = os.path.join("./static/image", imgNames[0] + "visualization.jpg") masked_im.save(visualization)
def infer(input_image,input_mask,input_mask_thr,output_mask,output_overlay): # the demo image is "2007_000129" from PASCAL VOC # load image, switch to BGR, subtract mean, and make dims C x H x W for Caffe image = Image.open(input_image) image = image.resize([128, 128], Image.ANTIALIAS) in_ = np.array(image, dtype=np.float32) in_ = in_[:,:,::-1] # in_ -= np.array((104.00698793,116.66876762,122.67891434)) in_ = in_.transpose((2,0,1)) label = Image.open(input_mask) label = label.resize([128, 128],Image.NEAREST) label = np.array(label,np.uint8) # label -= 1 # rotate labels so classes start at 0, void is 255 # label = label[np.newaxis, ...] label = (label > 200) * 1 from scipy.misc import imsave if input_mask_thr: imsave(input_mask_thr,label) # shape for input (data blob is N x C x H x W), set data net.blobs['data'].reshape(1, *in_.shape) net.blobs['data'].data[...] = in_ # run net and take argmax for prediction net.forward() out = net.blobs['output_sep'].data[0] Dtype = out.dtype im = Image.fromarray(out[0,:,:]) im = im.resize([6, 6], Image.NEAREST) im = np.array(im,Dtype) print im out = out.argmax(axis=0) im = Image.fromarray(out.astype(Dtype)) im = im.resize([6, 6], Image.NEAREST) im = np.array(im,Dtype) print im # visualize segmentation in PASCAL VOC colors voc_palette = vis.make_palette(2) out_im = Image.fromarray(vis.color_seg(out, voc_palette)) out_im.save(output_mask) masked_im = Image.fromarray(vis.vis_seg(image, out, voc_palette)) masked_im.save(output_overlay)
def callback(imgmsg): # print(imgmsg) bridge = CvBridge() img_ori = bridge.imgmsg_to_cv2(imgmsg, "bgr8") # Resize image [Image size is defined during training] img_resize = skimage.transform.resize(img_ori, IMAGE_DIM) * 255 # Convert RGB to BGR [skimage reads image in RGB, some networks may need BGR] image_t = img_resize[:, :, ::-1] # Mean subtraction & scaling [A common technique used to center the data] image_t = image_t.astype(numpy.float16) image_t = (image_t - numpy.float16(IMAGE_MEAN)) # -----------step4: get result------------------------------------------------- graph.queue_inference_with_fifo_elem(fifo_in, fifo_out, image_t, 'user object') # Get the results from NCS out, userobj = fifo_out.read_elem() # flatten ---> image out = out.reshape(-1, 2).T.reshape(2, 331, -1) out = out.argmax(axis=0) out = out[6:-5, 6:-5] # save result voc_palette = vis.make_palette(2) out_im = Image.fromarray(vis.color_seg(out, voc_palette)) iamge_name = IMAGE_PATH.split('/')[-1].rstrip('.jpg') # out_im.save('demo_test/' + iamge_name + '_ncs_' + '.png') # get masked image img_masked = Image.fromarray(vis.vis_seg(img_ori, out, voc_palette)) # # visualization img_ori = img_ori[:, :, ::-1] img_masked = img_masked[:, :, ::-1] cv2.imshow("in", img_ori) cv2.imshow("out", img_masked) cv2.waitKey(1)
def segmentation(path, current_painting): # the demo image is "2007_000129" from PASCAL VOC # load image, switch to BGR, subtract mean, and make dims C x H x W for Caffe # im = Image.open('demo/image.jpg') # path = "demo/Trials/twice.jpg" im = Image.open(path) # reshape input layer from dimensions of image H x W reshapeInputLayer(im) delayPrint("Starting to segment the image: {}".format(current_painting), PRINT_SECONDS) in_ = np.array(im, dtype=np.float32) in_ = in_[:, :, ::-1] in_ -= np.array((104.00698793, 116.66876762, 122.67891434)) in_ = in_.transpose((2, 0, 1)) # Own code: # Set mode to GPU caffe.set_mode_cpu() # load net net = caffe.Net('voc-fcn8s/deploy.prototxt', 'voc-fcn8s/fcn8s-heavy-pascal.caffemodel', caffe.TEST) # shape for input (data blob is N x C x H x W), set data net.blobs['data'].reshape(1, *in_.shape) net.blobs['data'].data[...] = in_ # run net and take argmax for prediction net.forward() out = net.blobs['score'].data[0].argmax(axis=0) # visualize segmentation in PASCAL VOC colors voc_palette = vis.make_palette(21) out_im = Image.fromarray(vis.color_seg(out, voc_palette)) # out_im.save('demo/output.png') out_im.save('demo/output/output_%s.png' % (current_painting.split(".")[0])) masked_im = Image.fromarray(vis.vis_seg(im, out, voc_palette)) # print extracted colors of original image vis.extractColors(path) # masked_im.save('demo/visualization.jpg') masked_im.save('demo/output/output_%s.jpg' % (current_painting))
label = cv2.resize(label, (self.width, self.height), interpolation=cv2.INTER_NEAREST) # label = np.expand_dims(label, 0) return label def data_generation(self, inds): inputs, targets = [], [] for idx in inds: inputs.append(self.load_image(idx)) targets.append(self.load_label(idx)) inputs = np.array(inputs) targets = np.array(targets) return inputs, targets if __name__ == '__main__': import vis palette = vis.make_palette(21) g = DataGenerator(320, 320, split='train', batch_size=1, shuffle=False) for i in range(20): image, label = g[i] image = np.array(image[0] + cfg.mean, 'uint8') label = label[0] label[np.where(label==255)] = 0 out_im = Image.fromarray(vis.color_seg(label, palette)) out_im.save('check_data/{}.png'.format(i)) masked_im = vis.vis_seg(image, label, palette) cv2.imwrite('check_data/{}.jpg'.format(i), masked_im)
caffe.set_mode_gpu() origin = [] for files in os.listdir('segmentation/Origin'): origin.append(files) net = caffe.Net('voc-fcn32s/deploy.prototxt', 'voc-fcn32s/fcn32s-heavy-pascal.caffemodel', caffe.TEST) for file in origin: im = Image.open('segmentation/Origin/' + file) in_ = np.array(im, dtype=np.float32) in_ = in_[:, :, ::-1] in_ -= np.array((104.00698793, 116.66876762, 122.67891434)) in_ = in_.transpose((2, 0, 1)) # shape for input (data blob is N x C x H x W), set data net.blobs['data'].reshape(1, *in_.shape) net.blobs['data'].data[...] = in_ # run net and take argmax for prediction net.forward() out = net.blobs['score'].data[0].argmax(axis=0) # visualize segmentation in PASCAL VOC colors voc_palette = vis.make_palette(21) out_im = Image.fromarray(vis.color_seg(out, voc_palette)) out_im.save('segmentation/fcn32s/' + file) end = time.clock() print("time used: ", end - start)
def predict(): # initialize the data dictionary that will be returned from the # view data = {"success": False} # ensure an image was properly uploaded to our endpoint if flask.request.method == "POST": if flask.request.files.get("image"): # read the image in PIL format image = flask.request.files["image"].read() image = Image.open(io.BytesIO(image)) # if necesary, we can perpare images ahead. # preprocess the image and prepare it for classification # image = prepare_image(image, target=(224, 224)) # image = np.matrix(image, dtype=np.int32) # make file "time.txt" and write the path of the image get from clinet file_time = open("time.txt", "w+") fileName = datetime.datetime.now().strftime('%Y-%m-%d-%H:%M:%S') image.save('./images/' + fileName + '.jpg') file_time.write(absPath + '/images/' + fileName + '.jpg') file_time.close() pred_hooks = None if FLAGS.debug: debug_hook = tf_debug.LocalCLIDebugHook() pred_hooks = [debug_hook] examples = dataset_util.read_examples_list(FLAGS.infer_data_list) image_files = [os.path.join(FLAGS.data_dir, filename) for filename in examples] with graph.as_default(): try: global model predictions = model.predict(input_fn=lambda: preprocessing.eval_input_fn(image_files), hooks=pred_hooks) except Exception as e: raise TypeError("bad input") from e output_dir = FLAGS.output_dir if not os.path.exists(output_dir): os.makedirs(output_dir) # print(image_files) for pred_dict, image_path in zip(predictions, image_files): image_basename = os.path.splitext(os.path.basename(image_path))[0] output_filename = image_basename + '.png' path_to_output = os.path.join(output_dir, output_filename) orginalImage = np.array(Image.open(image_path)) img = Image.fromarray(orginalImage) mask = pred_dict['decoded_labels'] mask = Image.fromarray(mask) mask = mask.convert('L') threshold = 10 table = [] for i in range(256): if i < threshold: table.append(0) else: table.append(1) mask = mask.point(table, '1') mask = np.matrix(mask, dtype=np.int32) voc_palette = vis.make_palette(2) out_im = Image.fromarray(vis.color_seg(mask, voc_palette)) (shotname, extension) = os.path.splitext(image_path) # mask images # out_im.save(shotname+'out.png') masked_im = Image.fromarray(vis.vis_seg(img, mask, voc_palette)) # get vis in images/ # masked_im.save(shotname+'vis.png') print("generating:", path_to_output) masked_im.save(path_to_output) # results = imagenet_utils.decode_predictions(preds) data["predictions"] = [] # loop over the results and add them to the list of # returned predictions # for (imagenetID, label, prob) in 1: r = {"label": "apple", "probability": float(0.999)} data["predictions"].append(r) # indicate that the request was a success data["success"] = True # return the data dictionary as a JSON response return flask.jsonify(data)
import numpy as np from PIL import Image import caffe import vis # the demo image is "2007_000129" from PASCAL VOC # load image, switch to BGR, subtract mean, and make dims C x H x W for Caffe im = Image.open('demo/image.jpg') in_ = np.array(im, dtype=np.float32) in_ = in_[:,:,::-1] in_ -= np.array((104.00698793,116.66876762,122.67891434)) in_ = in_.transpose((2,0,1)) # load net net = caffe.Net('voc-fcn8s/deploy.prototxt', 'voc-fcn8s/fcn8s-heavy-pascal.caffemodel', caffe.TEST) # shape for input (data blob is N x C x H x W), set data net.blobs['data'].reshape(1, *in_.shape) net.blobs['data'].data[...] = in_ # run net and take argmax for prediction net.forward() out = net.blobs['score'].data[0].argmax(axis=0) # visualize segmentation in PASCAL VOC colors voc_palette = vis.make_palette(21) out_im = Image.fromarray(vis.color_seg(out, voc_palette)) out_im.save('demo/output.png') masked_im = Image.fromarray(vis.vis_seg(im, out, voc_palette)) masked_im.save('demo/visualization.jpg')
def main(unused_argv): # Using the Winograd non-fused algorithms provides a small performance boost. os.environ['TF_ENABLE_WINOGRAD_NONFUSED'] = '1' pred_hooks = None if FLAGS.debug: debug_hook = tf_debug.LocalCLIDebugHook() pred_hooks = [debug_hook] model = tf.estimator.Estimator( model_fn=deeplab_model.deeplabv3_plus_model_fn, model_dir=FLAGS.model_dir, params={ 'output_stride': FLAGS.output_stride, 'batch_size': 1, # Batch size must be 1 because the images' size may differ 'base_architecture': FLAGS.base_architecture, 'pre_trained_model': None, 'batch_norm_decay': None, 'num_classes': _NUM_CLASSES, }) examples = dataset_util.read_examples_list(FLAGS.infer_data_list) image_files = [ os.path.join(FLAGS.data_dir, filename) for filename in examples ] predictions = model.predict( input_fn=lambda: preprocessing.eval_input_fn(image_files), hooks=pred_hooks) output_dir = FLAGS.output_dir if not os.path.exists(output_dir): os.makedirs(output_dir) for pred_dict, image_path in zip(predictions, image_files): image_basename = os.path.splitext(os.path.basename(image_path))[0] output_filename = image_basename + '.png' path_to_output = os.path.join(output_dir, output_filename) orginalImage = np.array(Image.open(image_path)) img = Image.fromarray(orginalImage) print("generating:", path_to_output) start = time.clock() mask = pred_dict['decoded_labels'] end = time.clock() print('running time %s' % (end - start)) print(mask.size) mask = Image.fromarray(mask) mask = mask.convert('L') threshold = 10 table = [] for i in range(256): if i < threshold: table.append(0) else: table.append(1) mask = mask.point(table, '1') mask = np.matrix(mask, dtype=np.int32) print(mask) voc_palette = vis.make_palette(2) out_im = Image.fromarray(vis.color_seg(mask, voc_palette)) (shotname, extension) = os.path.splitext(image_path) out_im.save(path_to_output) masked_im = Image.fromarray(vis.vis_seg(img, mask, voc_palette)) masked_im.save(shotname + 'vis.jpg') plt.axis('off') plt.imshow(masked_im) plt.savefig(path_to_output, bbox_inches='tight') plt.show()
out = sess.run(output_y, feed_dict={input_x: np.expand_dims(new_im, axis=0)}) im_output = np.array( tf.argmax(out[0], 2).eval()) result = im_output[:im_shape[0], :im_shape[1]] out_im = Image.fromarray(result.astype('uint8')) end_time = datetime.datetime.now() process_time = end_time - start_time total_time += process_time.total_seconds() print '处理图片第{}张图片{} 耗时{}'.format(num, imname, process_time) print '总耗时{}, 平均耗时{}'.format(total_time, total_time / num) num += 1 save_path = os.path.join(VOCopts['resdir'], 'Segmentation/{}_{}_cls/'.format(id, VOCopts['testset'])) check_dir(save_path) resfile = os.path.join(VOCopts['resdir'], 'Segmentation/{}_{}_cls/{}.png'.format(id, VOCopts['testset'], imname)) out_im.putpalette(np.array(colormap).reshape(-1)) out_im.save(resfile) # visualize segmentation in PASCAL VOC colors voc_palette = vis.make_palette(21) out_palette_im = Image.fromarray(vis.color_seg(result, voc_palette)) check_dir('./valresult/') out_palette_im.save('./valresult/out_{}.png'.format(imname)) masked_im = Image.fromarray(vis.vis_seg(im, result, voc_palette)) masked_im.save('./valresult/out_{}_visualization.png'.format(imname)) print '存储结果图片: {}'.format(resfile)
K.clear_session() # Clear previous models from memory. model = fcn8s_resnet(height=cfg.height, width=cfg.width) weights_path = os.path.abspath(args.weights) model.load_weights(weights_path, by_name=True) with open('../data/VOC2007/ImageSets/Segmentation/minival.txt') as f: ims = [x.strip() + '.jpg' for x in f.readlines()][:10] t = timer.now() segs = [] for i in range(len(ims)): img_path = '../data/VOC2007/JPEGImages/' + ims[i] segs.append(pred(model, img_path)) delta = timer.now() - t print(len(ims), 'images cost: ', delta) print('average cost: ', delta / len(ims)) with open('segs.pkl', 'wb') as f: pickle.dump(segs, f) # visualize segmentation in PASCAL VOC colors voc_palette = make_palette(21) for i in range(len(ims)): img_path = '../data/VOC2007/JPEGImages/' + ims[i] im = Image.open(img_path) im = np.array(im, np.uint8) im = cv2.resize(im, (cfg.width, cfg.height)) out_im = Image.fromarray(color_seg(segs[i], voc_palette)) out_im.save('results/{}_output.png'.format(ims[i][:-4])) masked_im = Image.fromarray(vis_seg(im, segs[i], voc_palette)) masked_im.save('results/' + ims[i])