def run_crfasrnn(inputfile, outputfile, gpudevice): MODEL_FILE = 'TVG_CRFRNN_new_deploy.prototxt' PRETRAINED = 'TVG_CRFRNN_COCO_VOC.caffemodel' IMAGE_FILE = inputfile if gpudevice > 0: #Do you have GPU device? has_gpu = 1 #which gpu device is available? gpu_device = gpudevice #assume the first gpu device is available, e.g. Titan X else: has_gpu = 0 if has_gpu == 1: caffe.set_device(gpu_device) caffe.set_mode_gpu() tic() net = caffe.Segmenter(MODEL_FILE, PRETRAINED, True) toc() else: caffe.set_mode_cpu() tic() net = caffe.Segmenter(MODEL_FILE, PRETRAINED, False) toc() input_image = 255 * caffe.io.load_image(IMAGE_FILE) width = input_image.shape[0] height = input_image.shape[1] maxDim = max(width, height) image = PILImage.fromarray(np.uint8(input_image)) image = np.array(image) pallete = getpallete(256) mean_vec = np.array([103.939, 116.779, 123.68], dtype=np.float32) reshaped_mean_vec = mean_vec.reshape(1, 1, 3) # Rearrange channels to form BGR im = image[:, :, ::-1] # Subtract mean im = im - reshaped_mean_vec # Pad as necessary cur_h, cur_w, cur_c = im.shape pad_h = 500 - cur_h pad_w = 500 - cur_w im = np.pad(im, pad_width=((0, pad_h), (0, pad_w), (0, 0)), mode='constant', constant_values=0) # Get predictions segmentation = net.predict([im]) segmentation2 = segmentation[0:cur_h, 0:cur_w] output_im = PILImage.fromarray(segmentation2) output_im.putpalette(pallete) plt.imshow(output_im) plt.savefig(outputfile)
def test_net(prototxt, caffemodel, images, labels, lut): net = caffe.Segmenter(prototxt, caffemodel, True) mean_vec = np.array([103.939, 116.779, 123.68], dtype=np.float32) reshaped_mean_vec = mean_vec.reshape(1, 1, 3) pa_list = [] ma_list = [] m_IU_list = [] fw_IU_list = [] for img_path, label_path in zip(images, labels): im, cur_h, cur_w = preprocess_image(img_path, reshaped_mean_vec) label = imread(label_path) label = lut[label] segmentation = net.predict([im]) pred = segmentation[0:cur_h, 0:cur_w] pa = pixel_accuracy(pred, label) ma = mean_accuracy(pred, label) m_IU = mean_IU(pred, label) fw_IU = frequency_weighted_IU(pred, label) pa_list.append(pa) ma_list.append(ma) m_IU_list.append(m_IU) fw_IU_list.append(fw_IU) 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 find_subjects(image_file, outpath="output.jpg"): #caffe.set_mode_gpu() net = caffe.Segmenter(MODEL_FILE, PRETRAINED) input_image = 255 * caffe.io.load_image(image_file) width = input_image.shape[0] height = input_image.shape[1] maxDim = max(width, height) image = PILImage.fromarray(np.uint8(input_image)) image = np.array(image) plt.imshow(image) mean_vec = np.array([103.939, 116.779, 123.68], dtype=np.float32) reshaped_mean_vec = mean_vec.reshape(1, 1, 3) # Rearrange channels to form BGR im = image[:, :, ::-1] # Subtract mean im = im - reshaped_mean_vec # Pad as necessary cur_h, cur_w, cur_c = im.shape pad_h = 500 - cur_h pad_w = 500 - cur_w im = np.pad(im, pad_width=((0, pad_h), (0, pad_w), (0, 0)), mode='constant', constant_values=0) # Get predictions segmentation = net.predict([im]) segmentation2 = segmentation[0:cur_h, 0:cur_w] output_im = PILImage.fromarray(segmentation2) output_im.putpalette(PALLETE) plt.imshow(output_im) plt.savefig('output.png') subjects = subject_detection(decode_segmentation(output_im.getcolors())) output = create_filter(PILImage.open(image_file), output_im, subjects, outpath) return output
def run_crfasrnn_in_batches(input_batch, output_batch, gpu): caffe.set_device(gpu) caffe.set_mode_gpu() net = caffe.Segmenter(MODEL_FILE, PRETRAINED, True) input_images = [] input_shapes = [] for filename in input_batch: image = 255 * caffe.io.load_image(filename) image = PILImage.fromarray(np.uint8(image)) image = np.array(image) # Rearrange channels to form BGR image = image[:,:,::-1] # Subtract mean image = image - RESHAPED_MEAN_VEC # Pad as necessary cur_h, cur_w, cur_c = image.shape input_shapes.append(image.shape) pad_h = 500 - cur_h pad_w = 500 - cur_w image = np.pad(image, pad_width=((0, pad_h), (0, pad_w), (0, 0)), mode='constant', constant_values=0) input_images.append(image) # Get predictions segmentations = net.predict(input_images) for segmentation, input_shape, output_filename in izip(segmentations, input_shapes, output_batch): cur_h, cur_w, cur_c = input_shape segmentation2 = segmentation[0:cur_h, 0:cur_w] output_im = PILImage.fromarray(segmentation2) output_im.putpalette(PALETTE) plt.imshow(output_im) plt.savefig(output_filename)
def main(): iteration, image_paths = process_arguments(sys.argv) if iteration: prototxt = 'TVG_CRFRNN_COCO_VOC_TEST_3_CLASSES.prototxt' model = 'models/train_iter_{}.caffemodel'.format(iteration) else: prototxt = 'TVG_CRFRNN_COCO_VOC.prototxt' model = 'TVG_CRFRNN_COCO_VOC.caffemodel' if not exist_model(model, prototxt): help() # default images (part of http://www.cs.berkeley.edu/~bharath2/codes/SBD/download.html) if not image_paths: image_paths.append('images/2007_005844.png') # chair image_paths.append('images/2008_007811.png') # bottle image_paths.append('images/2007_002094.png') # bird palette = palette_demo() net = caffe.Segmenter(prototxt, model, True) for path in image_paths: image, cur_h, cur_w = preprocess_image(path) if image == None: print(path + ' does not exist! Skipping.' , file=sys.stderr) continue print('Processing ' + path + '...', end='') segmentation = net.predict([image]) segm_post = postprocess_label(segmentation, cur_h, cur_w, palette) plt.imshow(segm_post) plt.savefig(create_label_name(path)) print('finished.')
def __init__(self, root, deviceID, netObject): self.root = root self.deviceID = deviceID with open(root + 'labels.txt') as f: labels_df = pd.DataFrame([{ 'synset_id': l.strip().split(' ')[0], 'name': ' '.join(l.strip().split(' ')[1:]).split(',')[0] } for l in f.readlines()]) labels_df.sort('synset_id') MODEL_FILE = root + 'deploy.prototxt' PRETRAINED = root + 'model.caffemodel' caffe.set_mode_gpu() caffe.set_device(self.deviceID) if netObject == 'CNN' or netObject == 'RCNN': net = caffe.Classifier( MODEL_FILE, PRETRAINED, mean=np.load(root + 'mean.npy').mean(1).mean(1), channel_swap=(2, 1, 0), raw_scale=1, image_dims=(224, 224)) #TBD Read from Prototxt if netObject == 'NET': net = caffe.Net(MODEL_FILE, PRETRAINED, caffe.TEST) if netObject == 'SEGMENTER': net = caffe.Segmenter(MODEL_FILE, PRETRAINED) #, True) self.net = net self.labels_df = labels_df
import cPickle import logging import numpy as np import pandas as pd from PIL import Image as PILImage #import Image import cStringIO as StringIO import caffe import matplotlib.pyplot as plt MODEL_FILE = 'TVG_CRFRNN_COCO_VOC.prototxt' PRETRAINED = 'TVG_CRFRNN_COCO_VOC.caffemodel' IMAGE_FILE = 'input.jpg' #caffe.set_mode_gpu() net = caffe.Segmenter(MODEL_FILE, PRETRAINED) input_image = 255 * caffe.io.load_image(IMAGE_FILE) width = input_image.shape[0] height = input_image.shape[1] maxDim = max(width, height) image = PILImage.fromarray(np.uint8(input_image)) image = np.array(image) pallete = [ 0, 0, 0, 128, 0, 0, 0, 128, 0, 128, 128, 0, 0, 0, 128, 128, 0, 128, 0, 128, 128, 128, 128, 128, 64, 0, 0, 192, 0, 0, 64, 128, 0, 192, 128, 0, 64, 0, 128, 192, 0, 128, 64, 128, 128, 192, 128, 128, 0, 64, 0, 128, 64, 0, 0, 192, 0, 128, 192, 0, 0, 64, 128, 128, 64, 128, 0, 192, 128, 128, 192, 128, 64, 64, 0, 192, 64, 0, 64, 192, 0, 192, 192, 0
def __init__(self, model_file, trained_model): self.model_file = model_file self.trained_model = trained_model self.net = caffe.Segmenter(model_file, trained_model, gpu=True)
def run_persondetectiontest(inputdir, outputdir, gpudevice): if not os.path.exists(results_root + outputdir + inputdir): os.makedirs(results_root + outputdir + inputdir) else: warnings.warn('The result directory already exists!', RuntimeWarning) filelist = [ os.path.basename(os.path.normpath(x)) for x in glob.glob(data_root + inputdir + '*' + image_typ) ] done_list = [ os.path.basename(os.path.normpath(x)) for x in glob.glob(results_root + inputdir + '*') ] filelist = list(set(filelist) - set(done_list)) filelist = np.sort(filelist) if len(filelist) == 0: print "filelist was empty" return if gpudevice >= 0: # Do you have GPU device? has_gpu = 1 # which gpu device is available? gpu_device = gpudevice # assume the first gpu device is available, e.g. Titan X else: has_gpu = 0 if has_gpu == 1: caffe.set_device(gpu_device) caffe.set_mode_gpu() net = caffe.Segmenter(MODEL_FILE, PRETRAINED, True) else: print 'We wanna use a GPU, is not given, ya know.' sys.exit(2) net.blobs['data'].reshape(1, 3, maxsize, maxsize) net.reshape() for filename in filelist: print filename input_image = 255 * \ caffe.io.load_image(data_root + inputdir + filename) width = input_image.shape[0] height = input_image.shape[1] im, cur_h, cur_w = preprocess_image(input_image) # Get predictions net.blobs['data'].data[...] = im segmentation = net.forward() output_im = segmentation[net.outputs[0]] output_im = segmentation['pred'][0, 15, 0:cur_h, 0:cur_w] maxDim = max(width, height) if maxDim > maxsize: output_im = imresize(output_im, (width, height)) imsave(results_root + outputdir + inputdir + filename[:-3] + 'png', output_im)
import caffe MODEL_FILE = '/home/sadeep/Desktop/crf-rnn-web-demo/caffe-fcn-sadeep/models/crf_rnn/fcn-8s-pascal-deploy.prototxt' PRETRAINED = '/home/sadeep/Desktop/crf-rnn-web-demo/caffe-fcn-sadeep/models/crf_rnn/fcn-8s-pascal.caffemodel' IMAGE_FILE = '/home/sadeep/Desktop/crf-rnn-web-demo/caffe-fcn-sadeep/models/crf_rnn/2007_000033.jpg' pallete = [ 0, 0, 0, 128, 0, 0, 0, 128, 0, 128, 128, 0, 0, 0, 128, 128, 0, 128, 0, 128, 128, 128, 128, 128, 64, 0, 0, 192, 0, 0, 64, 128, 0, 192, 128, 0, 64, 0, 128, 192, 0, 128, 64, 128, 128, 192, 128, 128, 0, 64, 0, 128, 64, 0, 0, 192, 0, 128, 192, 0, 0, 64, 128, 128, 64, 128, 0, 192, 128, 128, 192, 128, 64, 64, 0, 192, 64, 0, 64, 192, 0, 192, 192, 0 ] net = caffe.Segmenter(MODEL_FILE, PRETRAINED, gpu=False) input_image = 255 * exifutil.open_oriented_im(IMAGE_FILE) # Mean values in BGR format mean_vec = np.array([103.939, 116.779, 123.68], dtype=np.float32) reshaped_mean_vec = mean_vec.reshape(1, 1, 3) # Rearrange channels to form BGR im = input_image[:, :, ::-1] # Subtract mean im = im - reshaped_mean_vec # Pad as necessary cur_h, cur_w, cur_c = im.shape