def execute(outputsize): faces_dir_path = "data/train_set/48_48_faces_web_augmented" bkgs_dir_path = "data/train_set/48_48_nonfaces_aflw" target_path = "data/train_set/13" faces_dir=join(target_path,"faces") nonfaces_dir = join(target_path,"nonfaces") os.makedirs(nonfaces_dir) os.makedirs(faces_dir) img_faces = [ f for f in listdir(faces_dir_path) if isfile(join(faces_dir_path,f)) and f.endswith("png") ] img_bkgs = [ f for f in listdir(bkgs_dir_path) if isfile(join(bkgs_dir_path,f)) and f.endswith("jpg") ] for i, img_name in enumerate(img_faces): img_path = join(faces_dir_path,img_name) img = imread(img_path) resized_img = resize(img,outputsize) ubyte_img = img_as_ubyte(resized_img) imsave(join(faces_dir,img_name), ubyte_img) print "processed "+ img_path for i, img_name in enumerate(img_bkgs): img_path = join(bkgs_dir_path,img_name) img = imread(img_path) gray_img = rgb2gray(img) resized_img = resize(gray_img,outputsize) ubyte_img = img_as_ubyte(resized_img) imsave(join(nonfaces_dir,img_name), ubyte_img) print "processed "+ img_path
def test_bool_and_anti_aliasing_errors(): img = np.zeros((10, 10), dtype=bool) with pytest.raises(ValueError): rescale(img, 0.5, anti_aliasing=True) with pytest.raises(ValueError): resize(img, (5, 5), anti_aliasing=True)
def test_bool_nonzero_order_errors(order): img = np.zeros((10, 10), dtype=bool) with pytest.raises(ValueError): rescale(img, 0.5, order=order) with pytest.raises(ValueError): resize(img, (5, 5), order=order) with pytest.raises(ValueError): warp(img, np.eye(3), order=order)
def test_resize3d_keep(): # keep 3rd dimension x = np.zeros((5, 5, 3), dtype=np.float64) x[1, 1, :] = 1 resized = resize(x, (10, 10), order=0, anti_aliasing=False, mode='constant') with pytest.raises(ValueError): # output_shape too short resize(x, (10, ), order=0, anti_aliasing=False, mode='constant') ref = np.zeros((10, 10, 3)) ref[2:4, 2:4, :] = 1 assert_array_almost_equal(resized, ref) resized = resize(x, (10, 10, 3), order=0, anti_aliasing=False, mode='constant') assert_array_almost_equal(resized, ref)
def test_resize2d(): x = np.zeros((5, 5), dtype=np.float64) x[1, 1] = 1 resized = resize(x, (10, 10), order=0, anti_aliasing=False, mode='constant') ref = np.zeros((10, 10)) ref[2:4, 2:4] = 1 assert_array_almost_equal(resized, ref)
def test_downsize(): x = np.zeros((10, 10), dtype=np.double) x[2:4, 2:4] = 1 scaled = resize(x, (5, 5), order=0, anti_aliasing=False, mode='constant') assert_equal(scaled.shape, (5, 5)) assert_equal(scaled[1, 1], 1) assert_equal(scaled[2:, :].sum(), 0) assert_equal(scaled[:, 2:].sum(), 0)
def test_bool_array_warnings(): img = np.zeros((10, 10), dtype=bool) with expected_warnings(['Input image dtype is bool']): rescale(img, 0.5, anti_aliasing=True) with expected_warnings(['Input image dtype is bool']): resize(img, (5, 5), anti_aliasing=True) with expected_warnings(['Input image dtype is bool']): rescale(img, 0.5, order=1) with expected_warnings(['Input image dtype is bool']): resize(img, (5, 5), order=1) with expected_warnings(['Input image dtype is bool']): warp(img, np.eye(3), order=1)
def test_bool_img_resize(): img = np.ones((12, 18), dtype=bool) img[2:-2, 4:-4] = False res = resize(img, (6, 9)) expected = np.ones((6, 9)) expected[1:-1, 2:-2] = False assert_array_equal(res, expected)
def test_resize3d_resize(): # resize 3rd dimension x = np.zeros((5, 5, 3), dtype=np.float64) x[1, 1, :] = 1 resized = resize(x, (10, 10, 1), order=0, anti_aliasing=False, mode='constant') ref = np.zeros((10, 10, 1)) ref[2:4, 2:4] = 1 assert_array_almost_equal(resized, ref)
def test_order_0_warp_dtype(dtype): img = _convert(astronaut()[:10, :10, 0], dtype) assert resize(img, (12, 12), order=0).dtype == dtype assert rescale(img, 0.5, order=0).dtype == dtype assert rotate(img, 45, order=0).dtype == dtype assert warp_polar(img, order=0).dtype == dtype assert swirl(img, order=0).dtype == dtype
def test_resize3d_2din_3dout(): # 3D output with 2D input x = np.zeros((5, 5), dtype=np.float64) x[1, 1] = 1 resized = resize(x, (10, 10, 1), order=0, anti_aliasing=False, mode='constant') ref = np.zeros((10, 10, 1)) ref[2:4, 2:4] = 1 assert_array_almost_equal(resized, ref)
def test_resize_nd(): for dim in range(1, 6): shape = 2 + np.arange(dim) * 2 x = np.ones(shape) out_shape = np.asarray(shape) * 1.5 resized = resize(x, out_shape, order=0, mode='reflect', anti_aliasing=False) expected_shape = 1.5 * shape assert_array_equal(resized.shape, expected_shape) assert np.all(resized == 1)
def test_resize2d_4d(): # resize with extra output dimensions x = np.zeros((5, 5), dtype=np.float64) x[1, 1] = 1 out_shape = (10, 10, 1, 1) resized = resize(x, out_shape, order=0, anti_aliasing=False, mode='constant') ref = np.zeros(out_shape) ref[2:4, 2:4, ...] = 1 assert_array_almost_equal(resized, ref)
def test_downsize(dtype): x = np.zeros((10, 10), dtype=dtype) x[2:4, 2:4] = 1 scaled = resize(x, (5, 5), order=0, anti_aliasing=False, mode='constant') expected_dtype = np.float32 if dtype == np.float16 else dtype assert scaled.dtype == expected_dtype assert scaled.shape == (5, 5) assert scaled[1, 1] == 1 assert scaled[2:, :].sum() == 0 assert scaled[:, 2:].sum() == 0
def test_nonzero_order_warp_dtype(dtype, order): img = _convert(astronaut()[:10, :10, 0], dtype) float_dtype = _supported_float_type(dtype) assert resize(img, (12, 12), order=order).dtype == float_dtype assert rescale(img, 0.5, order=order).dtype == float_dtype assert rotate(img, 45, order=order).dtype == float_dtype assert warp_polar(img, order=order).dtype == float_dtype assert swirl(img, order=order).dtype == float_dtype
def test_resize_dtype(): x = np.zeros((5, 5)) x_f32 = x.astype(np.float32) x_u8 = x.astype(np.uint8) x_b = x.astype(bool) assert resize(x, (10, 10), preserve_range=False).dtype == x.dtype assert resize(x, (10, 10), preserve_range=True).dtype == x.dtype assert resize(x_u8, (10, 10), preserve_range=False).dtype == np.double assert resize(x_u8, (10, 10), preserve_range=True).dtype == np.double assert resize(x_b, (10, 10), preserve_range=False).dtype == bool assert resize(x_b, (10, 10), preserve_range=True).dtype == bool assert resize(x_f32, (10, 10), preserve_range=False).dtype == x_f32.dtype assert resize(x_f32, (10, 10), preserve_range=True).dtype == x_f32.dtype
def test_resize3d_bilinear(): # bilinear 3rd dimension x = np.zeros((5, 5, 2), dtype=np.float64) x[1, 1, 0] = 0 x[1, 1, 1] = 1 resized = resize(x, (10, 10, 1), order=1, mode='constant', anti_aliasing=False) ref = np.zeros((10, 10, 1)) ref[1:5, 1:5, :] = 0.03125 ref[1:5, 2:4, :] = 0.09375 ref[2:4, 1:5, :] = 0.09375 ref[2:4, 2:4, :] = 0.28125 assert_array_almost_equal(resized, ref)
def test_resize_clip(order, preserve_range, anti_aliasing, dtype): # test if clip as expected if dtype == np.uint8 and (preserve_range or order == 0): expected_max = 255 else: expected_max = 1.0 x = np.ones((5, 5), dtype=dtype) if dtype == np.uint8: x *= 255 resized = resize(x, (3, 3), order=order, preserve_range=preserve_range, anti_aliasing=anti_aliasing) assert resized.max() == expected_max
def execute(outputsize): faces_dir_path = "data/train_set/48_48_faces_web_augmented" bkgs_dir_path = "data/train_set/48_48_nonfaces_aflw" target_path = "data/train_set/13" faces_dir = join(target_path, "faces") nonfaces_dir = join(target_path, "nonfaces") os.makedirs(nonfaces_dir) os.makedirs(faces_dir) img_faces = [ f for f in listdir(faces_dir_path) if isfile(join(faces_dir_path, f)) and f.endswith("png") ] img_bkgs = [ f for f in listdir(bkgs_dir_path) if isfile(join(bkgs_dir_path, f)) and f.endswith("jpg") ] for i, img_name in enumerate(img_faces): img_path = join(faces_dir_path, img_name) img = imread(img_path) resized_img = resize(img, outputsize) ubyte_img = img_as_ubyte(resized_img) imsave(join(faces_dir, img_name), ubyte_img) print "processed " + img_path for i, img_name in enumerate(img_bkgs): img_path = join(bkgs_dir_path, img_name) img = imread(img_path) gray_img = rgb2gray(img) resized_img = resize(gray_img, outputsize) ubyte_img = img_as_ubyte(resized_img) imsave(join(nonfaces_dir, img_name), ubyte_img) print "processed " + img_path
def test_downsize_anti_aliasing_invalid_stddev(): x = np.zeros((10, 10), dtype=np.float64) with pytest.raises(ValueError): resize(x, (5, 5), order=0, anti_aliasing=True, anti_aliasing_sigma=-1, mode='constant') with expected_warnings(["Anti-aliasing standard deviation greater"]): resize(x, (5, 15), order=0, anti_aliasing=True, anti_aliasing_sigma=(1, 1), mode="reflect") resize(x, (5, 15), order=0, anti_aliasing=True, anti_aliasing_sigma=(0, 1), mode="reflect")
def resize_imgs_in_dir(outputsize): img_dir_path = "data/newnonfaces/48_48_non_faces_aflw" target_path = "data/newnonfaces/13/nonfaces" os.makedirs(target_path) img_faces = [ f for f in listdir(img_dir_path) if isfile(join(img_dir_path,f)) and f.endswith("jpg") ] for i, img_name in enumerate(img_faces): img_path = join(img_dir_path,img_name) img = imread(img_path) resized_img = resize(img,outputsize) ubyte_img = img_as_ubyte(resized_img) imsave(join(target_path,img_name), ubyte_img) print "processed "+ img_path
def resize_imgs_in_dir(outputsize): img_dir_path = "data/newnonfaces/48_48_non_faces_aflw" target_path = "data/newnonfaces/13/nonfaces" os.makedirs(target_path) img_faces = [ f for f in listdir(img_dir_path) if isfile(join(img_dir_path, f)) and f.endswith("jpg") ] for i, img_name in enumerate(img_faces): img_path = join(img_dir_path, img_name) img = imread(img_path) resized_img = resize(img, outputsize) ubyte_img = img_as_ubyte(resized_img) imsave(join(target_path, img_name), ubyte_img) print "processed " + img_path
def check_for_image(): import skimage import skimage.data import skimage.util from skimage.color import rgb2gray img = rgb2gray(imread("data/originalPics/2002/07/19/big/img_372.jpg")) #img = rgb2gray(skimage.data.lena()) #img = imread("data/processed_images/13_train_set_aflw/train/faces/001111.jpg") # Load mean value from file. f = file(LOAD_MEAN_TO_FILE, 'rb') mean_val = pickle.load(f) f.close() # Scale search for mul in xrange(3, 20): img_orig = resize(img, (mul * 10, mul * 10)) im = img_orig[..., np.newaxis] arr = skimage.util.view_as_windows(im, (28, 28, 1), step=1) arr = arr - mean_val f = file(LOAD_STATE_FROM_FILE, 'rb') obj = pickle.load(f) f.close() arr = np.rollaxis(arr, 5, 3) borrow = True shared_x = theano.shared(np.asarray(arr, dtype=theano.config.floatX), borrow=borrow) iT = T.lscalar() jT = T.lscalar() x = T.tensor3("x") layer0_input = x.reshape((1, 1, 28, 28)) net = twelve_net(layer0_input, None, T.tanh, obj) prediction = net.log_regression_layer.y_pred py_x = net.log_regression_layer.p_y_given_x test_model = theano.function([iT, jT], [prediction, py_x, layer0_input], givens={x: shared_x[iT, jT, 0, :, :, :]}) rows = arr.shape[0] cols = arr.shape[1] count = 0 faces = [] for i in xrange(rows): for j in xrange(cols): [y, p_y_given_x, f] = test_model(i, j) f = f.reshape(1, 28, 28) f = np.rollaxis(f, 0, 3) #plt.imshow(f) #plt.show() if y == 1: count += 1 faces.append([i, j]) print i, j print("Check") plt.imshow(img_orig) plt.set_cmap('gray') img_desc = plt.gca() for point in faces: topleftx = point[1] toplefty = point[0] rect = patches.Rectangle((topleftx, toplefty), 28, 28, fill=False, color='c') img_desc.add_patch(rect) print count plt.show()
def check_for_image(): import skimage import skimage.data import skimage.util from skimage.color import rgb2gray img = rgb2gray(imread("data/originalPics/2002/07/19/big/img_372.jpg")) #img = rgb2gray(skimage.data.lena()) #img = imread("data/processed_images/13_train_set_aflw/train/faces/001111.jpg") # Load mean value from file. f = file(LOAD_MEAN_TO_FILE, 'rb') mean_val = pickle.load(f) f.close() # Scale search for mul in xrange(3, 20): img_orig = resize(img, (mul*10, mul*10)) im = img_orig[..., np.newaxis] arr = skimage.util.view_as_windows(im, (28, 28, 1), step=1) arr = arr - mean_val f = file(LOAD_STATE_FROM_FILE, 'rb') obj = pickle.load(f) f.close() arr = np.rollaxis(arr, 5, 3) borrow = True shared_x = theano.shared(np.asarray(arr, dtype=theano.config.floatX), borrow=borrow) iT = T.lscalar() jT = T.lscalar() x = T.tensor3("x") layer0_input = x.reshape((1, 1, 28, 28)) net = twelve_net(layer0_input, None, T.tanh, obj) prediction = net.log_regression_layer.y_pred py_x = net.log_regression_layer.p_y_given_x test_model = theano.function( [iT, jT], [prediction, py_x, layer0_input], givens={ x: shared_x[iT, jT, 0, :, :, :] } ) rows = arr.shape[0] cols = arr.shape[1] count = 0 faces = [] for i in xrange(rows): for j in xrange(cols): [y,p_y_given_x,f] = test_model(i, j) f=f.reshape(1, 28, 28) f = np.rollaxis(f, 0, 3) #plt.imshow(f) #plt.show() if y == 1: count += 1 faces.append([i, j]) print i, j print ("Check") plt.imshow(img_orig) plt.set_cmap('gray') img_desc = plt.gca() for point in faces: topleftx = point[1] toplefty = point[0] rect = patches.Rectangle( (topleftx, toplefty), 28, 28, fill=False, color='c' ) img_desc.add_patch(rect) print count plt.show()
def check_for_image(): import skimage import skimage.data import skimage.util img = skimage.data.lena() #img = io.imread("data/originalPics/2002/07/19/big/img_130.jpg") #img = io.imread("data/train_set/dataset/13/train/faces/00027998.png") img = rgb2gray(img) img = img_as_ubyte(img) img = img[:, :, np.newaxis] #img = imread("data/processed_images/13_train_set_aflw/train/faces/001111.jpg") for mul in xrange(3, 20): im = resize(img, (mul * 10, mul * 10)) im = img_as_ubyte(im) arr = skimage.util.view_as_windows(im, (13, 13, NUM_CHANNELS), step=STEP_SIZE) f = file(LOAD_STATE_FROM_FILE, 'rb') obj = pickle.load(f) f.close() arr = np.rollaxis(arr, 5, 3) borrow = True shared_x = theano.shared( np.asarray(arr, dtype=theano.config.floatX), # @UndefinedVariable borrow=borrow) iT = T.lscalar() jT = T.lscalar() x = T.tensor3("x") layer0_input = x.reshape((1, NUM_CHANNELS, 13, 13)) net = twelve_net(layer0_input, None, relu, obj) prediction = net.log_regression_layer.y_pred py_x = net.log_regression_layer.p_y_given_x test_model = theano.function([iT, jT], [prediction, py_x, layer0_input], givens={x: shared_x[iT, jT, 0, :, :, :]}) rows = arr.shape[0] cols = arr.shape[1] count = 0 faces = [] fig, axarr = plt.subplots(1, 2) confidence_map = np.zeros((rows, cols)) for i in xrange(rows): for j in xrange(cols): [y, p_y_given_x, f] = test_model(i, j) f = f.reshape(NUM_CHANNELS, 13, 13) f = np.rollaxis(f, 0, 3) f = f[:, :, 0] confidence_map[i, j] = p_y_given_x[0, 1] # plt.imshow(f,cmap = "Greys_r") # plt.show() if y == 1: count += 1 faces.append([i * STEP_SIZE, j * STEP_SIZE]) print i, j print("Check") # hack for gray im = im[:, :, 0] confidence_map = np.pad(confidence_map, 6, 'constant') axarr[0].get_xaxis().set_visible(False) axarr[0].get_yaxis().set_visible(False) axarr[1].get_xaxis().set_visible(False) axarr[1].get_yaxis().set_visible(False) axarr[1].imshow(confidence_map, cmap="Greys_r") axarr[0].imshow(im, cmap="Greys_r") img_desc = plt.gca() #plt.imshow(im,cmap = "Greys_r") #img_desc = plt.gca() for point in faces: topleftx = point[1] toplefty = point[0] rect = patches.Rectangle((topleftx, toplefty), 13, 13, fill=False, color='c') axarr[0].add_patch(rect) print count fig_name = "lena_" + str(mul) + ".png" plt.savefig(fig_name, bbox_inches='tight')
def check_for_image(): import skimage import skimage.data import skimage.util img = skimage.data.lena() #img = io.imread("data/originalPics/2002/07/19/big/img_130.jpg") #img = io.imread("data/train_set/dataset/13/train/faces/00027998.png") img = rgb2gray(img) img = img_as_ubyte(img) img=img[:,:,np.newaxis] #img = imread("data/processed_images/13_train_set_aflw/train/faces/001111.jpg") for mul in xrange(3, 20): im = resize(img, (mul*10, mul*10)) im = img_as_ubyte(im) arr = skimage.util.view_as_windows(im, (13, 13, NUM_CHANNELS), step=STEP_SIZE) f = file(LOAD_STATE_FROM_FILE, 'rb') obj = pickle.load(f) f.close() arr = np.rollaxis(arr, 5, 3) borrow = True shared_x = theano.shared(np.asarray(arr, dtype=theano.config.floatX), # @UndefinedVariable borrow=borrow) iT = T.lscalar() jT = T.lscalar() x = T.tensor3("x") layer0_input = x.reshape((1, NUM_CHANNELS, 13, 13)) net = twelve_net(layer0_input, None, relu, obj) prediction = net.log_regression_layer.y_pred py_x = net.log_regression_layer.p_y_given_x test_model = theano.function( [iT, jT], [prediction, py_x, layer0_input], givens={ x: shared_x[iT, jT, 0, :, :, :] } ) rows = arr.shape[0] cols = arr.shape[1] count = 0 faces = [] fig,axarr = plt.subplots(1,2) confidence_map = np.zeros((rows,cols)) for i in xrange(rows): for j in xrange(cols): [y,p_y_given_x,f] = test_model(i,j) f=f.reshape(NUM_CHANNELS,13,13) f = np.rollaxis(f,0,3) f = f[:,:,0] confidence_map[i,j] = p_y_given_x[0,1] # plt.imshow(f,cmap = "Greys_r") # plt.show() if y == 1: count += 1 faces.append([i*STEP_SIZE,j*STEP_SIZE]) print i,j print ("Check") # hack for gray im = im[:,:,0] confidence_map=np.pad(confidence_map, 6,'constant') axarr[0].get_xaxis().set_visible(False) axarr[0].get_yaxis().set_visible(False) axarr[1].get_xaxis().set_visible(False) axarr[1].get_yaxis().set_visible(False) axarr[1].imshow(confidence_map,cmap = "Greys_r") axarr[0].imshow(im,cmap = "Greys_r") img_desc = plt.gca() #plt.imshow(im,cmap = "Greys_r") #img_desc = plt.gca() for point in faces: topleftx = point[1] toplefty = point[0] rect = patches.Rectangle( (topleftx, toplefty), 13, 13, fill=False, color='c' ) axarr[0].add_patch(rect) print count fig_name = "lena_" + str(mul)+".png" plt.savefig(fig_name, bbox_inches='tight')
def test_output_shape_arg_type(_type): img = np.random.rand(3, 3) output_shape = _type([5, 5]) assert resize(img, output_shape).shape == tuple(output_shape)
def test_downsize_anti_aliasing(): x = np.zeros((10, 10), dtype=np.double) x[2, 2] = 1 scaled = resize(x, (5, 5), order=1, anti_aliasing=True, mode='constant') assert scaled.shape == (5, 5) assert np.all(scaled[:3, :3] > 0) assert scaled[3:, :].sum() == 0 assert scaled[:, 3:].sum() == 0 sigma = 0.125 out_size = (5, 5) resize(x, out_size, order=1, mode='constant', anti_aliasing=True, anti_aliasing_sigma=sigma) resize(x, out_size, order=1, mode='edge', anti_aliasing=True, anti_aliasing_sigma=sigma) resize(x, out_size, order=1, mode='symmetric', anti_aliasing=True, anti_aliasing_sigma=sigma) resize(x, out_size, order=1, mode='reflect', anti_aliasing=True, anti_aliasing_sigma=sigma) resize(x, out_size, order=1, mode='wrap', anti_aliasing=True, anti_aliasing_sigma=sigma) with pytest.raises(ValueError): # Unknown mode, or cannot translate mode resize(x, out_size, order=1, mode='non-existent', anti_aliasing=True, anti_aliasing_sigma=sigma)