def test_parser(self): proj_id = 3 depth = 15 noise_level = 16 case_id = 27 output = rsn.filename_parser("sino003151627.npy") expect = (proj_id, depth, noise_level, case_id) self.assertTrue(output==expect, msg=utg.errmsg(output, expect))
def test_basic(self): """A basic use case of offset_1d""" length, patch_size, strides = 16, 4, 4 output = [ offset for offset in utt.offset_1d(length, patch_size, strides) ] expected = [0, 4, 8, 12] self.assertTrue(output == expected, msg=utg.errmsg(output, expected))
def test_1d(self): a = np.ones([2]) l = [a, a] shape = [-1] output = utt.combine_tensor_list(l, shape) expected = np.ones([4]) self.assertTrue(np.array_equal(output, expected), msg=utg.errmsg(output, expected))
def test_merge(self): pipe_const0 = utp.Inputer(item='test') pipe_const1 = utp.Inputer(item='test') pipe_const2 = utp.Inputer(item='test') pipe_merge = utp.Pipe([pipe_const0, pipe_const1, pipe_const2]) output = next(pipe_merge.out) expected = ['test', 'test', 'test'] self.assertTrue(output == expected, msg=utg.errmsg(output, expected))
def test_item(self): pipe_input = utp.Inputer() pipe_input.insert('test1') pipe_input.insert('test2') pipe_input.insert('test3') output = [item for item in pipe_input.out] expected = ['test1', 'test2', 'test3'] self.assertTrue(output == expected, msg=utg.errmsg(output, expected))
def test_non_multiple(self): """A use case when length is not a multiple of strides""" length, patch_size, strides = 17, 4, 4 output = [ offset for offset in utt.offset_1d( length, patch_size, strides, check_all=True) ] expected = [0, 4, 8, 12, 13] self.assertTrue(output == expected, msg=utg.errmsg(output, expected))
def test_fixed(self): input_fn = 'img000000000.npy' inputs = np.load(os.path.join(TEST_DATA_PATH, input_fn)) expec_fn = 'test_down_sample_nd_test_fixed.dat' with open(os.path.join(TEST_DATA_PATH, expec_fn), 'r') as file_in: expect = pickle.load(file_in) output = utt.down_sample_nd(inputs, [4, 5, 1], method='fixed') self.assertTrue(np.array_equal(output, expect), msg=utg.errmsg(output, expect))
def test_gray(self): pipe_a = utp.Inputer(np.ones([1, 10, 10, 1])) pipe_b = utp.Inputer(np.zeros([1, 10, 10, 1])) pipe_merge = utp.Pipe([pipe_a, pipe_b]) pipe_stacker = utp.TensorStacker(pipe_merge) stacked = next(pipe_stacker.out) output = stacked.shape expect = (2, 10, 10, 1) self.assertTrue(output == expect, msg=utg.errmsg(output, expect))
def test_stack_tensor(self): input1 = np.ones([3, 3]) input2 = np.ones([4, 4]) * 2 pipe_input1 = utp.Inputer(input1) pipe_input2 = utp.Inputer(input2) pipe_stack = utp.Pipe([pipe_input1, pipe_input2]) output = next(pipe_stack.out) expect = [input1, input2] self.assertTrue(output == expect, msg=utg.errmsg(output, expect))
def test_stack(self): tensor_list = [] for i in xrange(3): t = np.ones([1, 5, 5, 1]) * i tensor_list.append(t) tensor = uti.image2tensor(tensor_list) output = tensor.shape expect = tuple([3, 5, 5, 1]) self.assertTrue(output == expect, msg=utg.errmsg(output, expect))
def _gather_f(self): input_ = self._gather() if len(input_) != 1: raise ValueError( utg.errmsg(len(input_), 1, "Wrong input list length, ")) output = input_[0] if output is None: raise TypeError("None input is not allowed.") return output
def test_split_image(self): tensor_shape = [5, 16, 16, 3] patch_shape = [1, 16, 16, 3] output = [ offsets for offsets in utt.offset_nd(tensor_shape, patch_shape) ] expected = [] for ii in xrange(5): expected.append([ii] + [0, 0, 0]) self.assertTrue(output == expected, msg=utg.errmsg(output, expected))
def test_1d(self): tensor_shape = [4] patch_shape = [2] strides = [2] output = [ offset for offset in utt.offset_nd(tensor_shape, patch_shape, strides) ] expect = [0, 2] self.assertTrue(output == expect, msg=utg.errmsg(output, expect))
def test_basic(self): img = np.load(os.path.join(TEST_DATA_PATH, 'img000000001.npy')) img = np.array(img) tensor = uti.image2tensor(img) output = uti.rgb2gray(tensor) with open(os.path.join(TEST_DATA_PATH, 'test_image2tensor_basic.dat')) as fileout: expect = pickle.load(fileout) self.assertTrue(np.array_equal(output, expect), msg=utg.errmsg(output, expect))
def test_offset1(self): """A use case when length is not a multiple of strides""" length, patch_size, strides = 16, 8, 4 offset1 = 4 output = [ offset for offset in utt.offset_1d( length, patch_size, strides, offset1=offset1) ] expected = [0, 4] self.assertTrue(output == expected, msg=utg.errmsg(output, expected))
def test_auto_imgrgb(self): data_file = os.path.join(TEST_DATA_PATH, 'img000000001.npy') imgrgb = np.array(np.load(data_file)) shapergb = imgrgb.shape pipe_input = utp.Inputer(imgrgb) pipe_formater = utp.TensorFormater(pipe_input) imgtensor = next(pipe_formater.out) output = imgtensor.shape expect = [1] + list(shapergb) expect = tuple(expect) self.assertTrue(output == expect, msg=utg.errmsg(output, expect))
def test_split_channel(self): tensor_shape = [16, 16, 8] patch_shape = [16, 16, 1] strides = [1, 1, 1] output = [ offsets for offsets in utt.offset_nd(tensor_shape, patch_shape, strides) ] expected = [] for ic in xrange(8): expected.append([0, 0] + [ic]) self.assertTrue(output == expected, msg=utg.errmsg(output, expected))
def test_image(self): patch_shape = (64, 64, 3) strides = (16, 16, 1) input_fn = "test_crop_tensor.test_image.input.dat" output_fn = "test_crop_tensor.test_image.output.dat" with open(os.path.join(TEST_DATA_PATH, input_fn)) as file_in: input_ = pickle.load(file_in) with open(os.path.join(TEST_DATA_PATH, output_fn)) as file_ex: expected = pickle.load(file_ex) output = utt.crop_tensor(input_, patch_shape, strides) self.assertTrue(np.array_equal(output, expected), msg=utg.errmsg(output, expected))
def down_sample_nd(input_, ratio, offset=None, method='mean'): DeprecationWarning() """Down sample of tensor on N axises. """ dim = len(input_.shape) if offset is None: offset = [0] * dim if dim != len(ratio): raise ValueError(utg.errmsg(len(ratio), dim), "ratio dimension mismatch, ") if dim != len(offset): raise ValueError(utg.errmsg(len(offset), dim), "offset dimension mismatch, ") output = np.zeros(input_.shape) output[:] = input_ for axis in xrange(dim): output = down_sample_1d(output, axis=axis, ratio=ratio[axis], offset=offset[axis], method=method) return output
def test_basic(self): tensor_shape = [16, 8] patch_shape = [8, 4] strides = [4, 2] output = [ offsets for offsets in utt.offset_nd(tensor_shape, patch_shape, strides) ] expected = [] for iy in xrange(3): for ix in xrange(3): expected.append([iy * 4, ix * 2]) self.assertTrue(output == expected, msg=utg.errmsg(output, expected))
def test_image(self): large_shape = (800, 800, 3) offset = (3, 3, 0) strides = map(lambda a, b: a + b, (64, 64, 3), offset) datapath = "/home/hongxwing/Workspace/xlearn/tests/data" input_fn = "test_combine_tensor_list.test_image.input.dat" output_fn = "test_combine_tensor_list.test_image.output.dat" with open(os.path.join(datapath, input_fn)) as file_in: input_ = pickle.load(file_in) with open(os.path.join(datapath, output_fn)) as file_ex: expected = pickle.load(file_ex) output = utt.combine_tensor_list(input_, large_shape, strides) self.assertTrue(np.array_equal(output, expected), msg=utg.errmsg(output, expected))
def test_gray2(self): data_file = os.path.join(TEST_DATA_PATH, 'img000000001.npy') imgrgb = np.array(np.load(data_file)) imggray = imgrgb[:, :, 0] imgin = np.reshape(imggray, [1, 288, 352, 1]) pipe_a = utp.Inputer(imgin) pipe_b = utp.Inputer(imgin) pipe_merge = utp.Pipe([pipe_a, pipe_b]) pipe_stacker = utp.TensorStacker(pipe_merge) stacked = next(pipe_stacker.out) output = stacked.shape expect = (2, 288, 352, 1) self.assertTrue(output == expect, msg=utg.errmsg(output, expect))
def test_auto_stacked_image(self): data_file = os.path.join(TEST_DATA_PATH, 'img000000001.npy') imgrgb = np.array(np.load(data_file)) input_ = np.zeros( [2, imgrgb.shape[0], imgrgb.shape[1], imgrgb.shape[2]]) input_[0, :, :, :] = imgrgb input_[1, :, :, :] = imgrgb pipe_input = utp.Inputer(input_) pipe_tensor = utp.TensorFormater(pipe_input) tensor = next(pipe_tensor.out) output = tensor.shape expect = (2, 288, 352, 3) self.assertTrue(output == expect, msg=utg.errmsg(output, expect))
def test_auto_after_stacker(self): data_file = os.path.join(TEST_DATA_PATH, 'img000000001.npy') imgrgb = np.array(np.load(data_file)) imgin = np.reshape(imgrgb, [1, 288, 352, 3]) pipe_a = utp.Inputer(imgin) pipe_b = utp.Inputer(imgin) pipe_merge = utp.Pipe([pipe_a, pipe_b]) pipe_stacker = utp.TensorStacker(pipe_merge) pipe_tensor = utp.TensorFormater(pipe_stacker) tensor = next(pipe_tensor.out) output = tensor.shape expect = (2, 288, 352, 3) self.assertTrue(output == expect, msg=utg.errmsg(output, expect))
def _process(self): input_ = self._gather_f() patch_shape = list(input_.shape) dim = len(patch_shape) for i in xrange(dim): patch_shape[i] -= self._margin0[i] patch_shape[i] -= self._margin1[1] if patch_shape[i] < 1: raise ValueError( utg.errmsg(patch_shape[i], ">0", "crop smaller than 0, ")) ind = [] for i in xrange(dim): ind.append( xrange(self._margin0[i] + 1, input_.shape[i] - self._margin1[i])) output = input_[ind] return output
def test_basic(self): pipe_input = utp.Inputer(item='test') pipe_copy = utp.Copyer(pipe_input, copy_number=3) output = [item for item in pipe_copy.out] expected = ['test', 'test', 'test'] self.assertTrue(output == expected, msg=utg.errmsg(output, expected))
def test_max(self): pipe_count = utp.Counter() pipe_count.max_state = 5 output = [cnt for cnt in pipe_count.out] expected = [0, 1, 2, 3, 4] self.assertTrue(output == expected, msg=utg.errmsg(output, expected))
def test_basic_mean(self): inputs = np.array([1, 2, 3, 4, 5]) output = utt.down_sample_1d(inputs, axis=0, ratio=2, method='mean') expect = np.array([1.5, 3.5]) self.assertTrue(np.array_equal(output, expect), msg=utg.errmsg(output, expect))
def test_basic_0(self): output = utg.seperate_file_name("sino000000018.raw") expect = ('sino', 18, 'raw') self.assertTrue(output == expect, msg=utg.errmsg(output, expect))