diff --git a/README.md b/README.md index 6c7e13a..ac6d040 100644 --- a/README.md +++ b/README.md @@ -91,7 +91,9 @@ python test.py --dataset university --detection_threshold 0.99 --viz 3. matplotlib 4. h5py 5. tensorflow -6. [Deeplab V3](https://github.com/sthalles/deeplab_v3) +6. PIL +7. scikit-learn +8. [Deeplab V3](https://github.com/sthalles/deeplab_v3) ## Examples diff --git a/annotate.py b/annotate.py index 5dee6eb..51cb099 100644 --- a/annotate.py +++ b/annotate.py @@ -1,8 +1,9 @@ import matplotlib.pyplot as plt import numpy -import scipy.misc +from PIL import Image from sklearn.cluster import KMeans import sys +import os dataset = 'beach' margin = 10 @@ -18,13 +19,14 @@ def next_img(): global image_np, image_display, labels, img_id, segment_id - try: - image_np = scipy.misc.imread('dataset/%s/%d.png'%(dataset,img_id)) - except IOError: + image_filename = 'dataset/%s/%d.png'%(dataset,img_id) + if os.path.exists(image_filename): + image_np = numpy.array(Image.open(image_filename)) + else: sys.exit(0) image_display = image_np.copy() image_np = numpy.mean(image_np, axis=2) - #print(image_np.shape, image_display.shape) +# print(image_np.shape, image_display.shape) labels = numpy.zeros(image_np.shape, dtype=int) segment_id=0 plt.imshow(image_display) @@ -35,7 +37,7 @@ def next_img(): def onkey(event): if event.key==' ': global img_id - scipy.misc.imsave('dataset/%s/label%d.png'%(dataset,img_id), labels) + Image.fromarray(labels, mode='L').save('dataset/%s/label%d.png'%(dataset,img_id)) img_id += 1 next_img() elif event.key=='r': diff --git a/baselines.py b/baselines.py index 123634b..2220307 100644 --- a/baselines.py +++ b/baselines.py @@ -1,12 +1,13 @@ import matplotlib.pyplot as plt import numpy -import scipy.misc +from PIL import Image import sys import cv2 import time from sklearn.cluster import KMeans from sklearn.mixture import GaussianMixture import glob +import os method = 'threshold' #method = 'threshold_adp' @@ -44,13 +45,14 @@ if method!='backSub' and image_id < test_idx: image_id += 1 continue - try: - I = scipy.misc.imread('dataset/%s/%d.png' % (dataset,image_id)) + image_filename = 'dataset/%s/%d.png' % (dataset,image_id) + if os.path.exists(image_filename): + I = numpy.array(Image.open(image_filename)) if len(I.shape)>2: I = numpy.mean(I, axis=2) - except IOError: + else: break - gt = scipy.misc.imread('dataset/%s/label%d.png' % (dataset, image_id)) + gt = numpy.array(Image.open('dataset/%s/label%d.png'%(dataset,image_id))) gt = gt > 0 dt = numpy.zeros(I.shape, dtype=bool) image_np = I[ybound:ybound+imscale, xbound:xbound+imscale] @@ -71,7 +73,7 @@ if dataset=='combined' and image_id in [97, 225, 249]: backSub = cv2.createBackgroundSubtractorMOG2() mask = backSub.apply(image_np) - scipy.misc.imsave('dataset/%s/backSub/%d.png'%(dataset,image_id), mask.astype(numpy.uint8)) + Image.fromarray(mask.astype(numpy.uint8), mode='L').save('dataset/%s/backSub/%d.png'%(dataset,image_id)) elif method=='kmeans': window_size = 15 if dataset=='beach' or dataset=='shore' else 100 margin = 10 if dataset=='beach' or dataset=='shore' else 100 @@ -227,9 +229,9 @@ # print('Image #%d Precision:%.2f/%.2f Recall:%.2f/%.2f (%.2fs)'%(image_id, prc,obj_prc,rcl,obj_rcl, t2-t1)) if image_id == save_frame: - scipy.misc.imsave('results/original_%d.png'%save_frame, image_np.astype(numpy.uint8)) - scipy.misc.imsave('results/detected_%s_%d.png'%(method,save_frame), dt_viz) - scipy.misc.imsave('results/ground_truth_%d.png'%save_frame, gt_viz) + Image.fromarray(image_np.astype(numpy.uint8), mode='L').save('results/original_%d.png'%save_frame) + Image.fromarray(dt_viz, mode='RGB').save('results/detected_%s_%d.png'%(method, save_frame)) + Image.fromarray(gt_viz, mode='RGB').save('results/ground_truth_%d.png'%save_frame) print('save_frame',save_frame) sys.exit(1) if viz: diff --git a/process_record.py b/process_record.py index ac5bd26..82fc5fd 100644 --- a/process_record.py +++ b/process_record.py @@ -1,5 +1,5 @@ import numpy -import scipy.misc +from PIL import Image import h5py import glob import sys @@ -34,7 +34,7 @@ previous_img = None for i in range(1,num_samples+1): - image_np = scipy.misc.imread('dataset/%s/%d.png'%(dataset,i),mode='RGB') + image_np = numpy.array(Image.open('dataset/%s/%d.png'%(dataset,i))) image_np = image_np[ybound:ybound+imscale,xbound:xbound+imscale].mean(axis=2) if previous_img is None: diff_img = numpy.zeros(image_np.shape, dtype=numpy.uint8) @@ -42,7 +42,7 @@ else: diff_img = ((image_np - previous_img)/2 + 128).astype(numpy.uint8) if use_history: - backSub_img = scipy.misc.imread('dataset/%s/backSub/%d.png'%(dataset,i),mode='RGB').mean(axis=2) + backSub_img = numpy.array(Image.open('dataset/%s/backSub/%d.png'%(dataset,i))).mean(axis=2) previous_img = image_np image_h = image_np.shape[0] image_w = image_np.shape[1] @@ -50,13 +50,13 @@ image_np = numpy.dstack((image_np, diff_img, backSub_img)).astype(numpy.uint8) else: image_np = numpy.dstack((image_np, image_np, image_np)).astype(numpy.uint8) - annotation = scipy.misc.imread('dataset/%s/label%d.png'%(dataset,i), mode='L') + annotation = numpy.array(Image.open('dataset/%s/label%d.png'%(dataset,i))) for p in numpy.array(numpy.nonzero(annotation)).T: pos_idx.add(tuple(p)) annotation = annotation[ybound:ybound+imscale,xbound:xbound+imscale] if imscale!=imsize: - image_np = scipy.misc.imresize(image_np, size=(imsize, imsize), interp='bilinear') - annotation = scipy.misc.imresize(annotation, size=(imsize, imsize), interp='bilinear') + image_np = numpy.array(Image.fromarray(image_np).resize((imsize, imsize), resample=Image.BILINEAR)) + annotation = numpy.array(Image.fromarray(annotation).resize((imsize, imsize), resample=Image.BILINEAR)) annotation = numpy.array(annotation > 0, dtype=numpy.uint8) print(i,image_np.shape,image_np.dtype) if i-1 in train_samples: diff --git a/test.py b/test.py index 4888125..40d4d5b 100644 --- a/test.py +++ b/test.py @@ -13,6 +13,7 @@ import sys import sklearn.metrics import glob +from PIL import Image parser = argparse.ArgumentParser() envarg = parser.add_argument_group('Training params') @@ -108,7 +109,7 @@ def __init__(self): previous_img = None for image_idx in range(1,num_samples+1): t1 = time.time() - original_image = scipy.misc.imread('dataset/%s/%d.png'%(args.dataset,image_idx),mode='RGB') + original_image = numpy.array(Image.open('dataset/%s/%d.png'%(args.dataset,image_idx))) image_np = original_image[ybound:ybound+imscale,xbound:xbound+imscale].mean(axis=2) if args.use_history: if previous_img is None: @@ -116,7 +117,7 @@ def __init__(self): backSub_img = numpy.zeros(image_np.shape, dtype=numpy.uint8) else: diff_img = ((image_np - previous_img)/2 + 128).astype(numpy.uint8) - backSub_img = scipy.misc.imread('dataset/%s/backSub/%d.png'%(args.dataset,image_idx),mode='RGB').mean(axis=2) + backSub_img = numpy.array(Image.open('dataset/%s/backSub/%d.png'%(args.dataset,image_idx))).mean(axis=2) previous_img = image_np if image_idx < test_idx: continue @@ -124,12 +125,12 @@ def __init__(self): image_np = numpy.dstack((image_np, diff_img, backSub_img)).astype(numpy.uint8) else: image_np = numpy.dstack((image_np, image_np, image_np)).astype(numpy.uint8) - original_annotation = scipy.misc.imread('dataset/%s/label%d.png'%(args.dataset,image_idx), mode='L') + original_annotation = numpy.array(Image.open('dataset/%s/label%d.png'%(args.dataset,image_idx))) annotation = original_annotation[ybound:ybound+imscale,xbound:xbound+imscale] annotation = numpy.array(annotation > 0, dtype=numpy.uint8) if imsize!=imscale: - input_images[:] = scipy.misc.imresize(image_np, size=(imsize, imsize), interp='bilinear') - input_labels[:] = scipy.misc.imresize(annotation, size=(imsize, imsize), interp='bilinear') + input_images[:] = numpy.array(Image.fromarray(image_np).resize((imsize, imsize), resample=Image.BILINEAR)) + input_labels[:] = numpy.array(Image.fromarray(annotation).resize((imsize, imsize), resample=Image.BILINEAR)) else: input_images[:] = image_np input_labels[:] = annotation @@ -142,7 +143,7 @@ def __init__(self): # print('Loss %.2f(%.2f+%.2f) tp:%d fp:%d fn:%d prc:%.2f rcl:%.2f'%(ls,pl,nl,vtp,vfp,vfn,prc,rcl)) predicted_softmax = scipy.special.softmax(result[0,:,:,:],axis=-1)[:,:,1] if imsize!=imscale: - predicted_softmax = scipy.misc.imresize(predicted_softmax, size=(imscale, imscale), interp='bilinear') / 255.0 + predicted_softmax = numpy.array(Image.fromarray(predicted_softmax, mode='F').resize((imscale, imscale), resample=Image.BILINEAR)) ap_dt.extend(predicted_softmax.flatten()) ap_gt.extend(annotation.flatten()) predicted = predicted_softmax > detection_threshold @@ -222,12 +223,12 @@ def __init__(self): dt_viz[y1:y2, x2, :] = [255,255,0] comp_time.append(t2 - t1) -# print('%d/%d images prc %.2f/%.2f rcl %.2f/%.2f (%.2fs)'%(image_idx,num_samples,prc,obj_prc,rcl,obj_rcl,t2-t1)) + print('%d/%d images prc %.2f/%.2f rcl %.2f/%.2f (%.2fs)'%(image_idx,num_samples,prc,obj_prc,rcl,obj_rcl,t2-t1)) if image_idx == args.save_frame: - scipy.misc.imsave('results/original_%d.png'%args.save_frame, image_np[:,:,0].astype(numpy.uint8)) - scipy.misc.imsave('results/detected_%s_%d.png'%('history' if args.use_history else 'cnn', args.save_frame), dt_viz) - scipy.misc.imsave('results/ground_truth_%d.png'%args.save_frame, gt_viz) + Image.fromarray(image_np[:,:,0].astype(numpy.uint8), mode='L').save('results/original_%d.png'%args.save_frame) + Image.fromarray(dt_viz, mode='RGB').save('results/detected_%s_%d.png'%('history' if args.use_history else 'cnn', args.save_frame)) + Image.fromarray(gt_viz, mode='RGB').save('results/ground_truth_%d.png'%args.save_frame) print('save_frame',args.save_frame) sys.exit(1) if viz: