def test_softencoding(): image_path = "Dataset/Test/images/0.JPEG" image = cv2.imread(image_path) image_small = cv2.resize(image, (16, 16)) l, a, b = convert_rgb_to_lab(image) l_small, a_small, b_small = convert_rgb_to_lab(image_small) encoding = softencoding(a_small, b_small) nonzeros_per_pixels = np.count_nonzero(encoding, axis=2) num_diff_five = np.where(nonzeros_per_pixels != 5) assert (num_diff_five[0].size == 0)
def __getitem__(self, idx): i = idx * self.batch_size length = min(self.batch_size, (len(self.names) - i)) batch_x = np.empty((length, 64, 64, 1), dtype=np.float32) batch_y = np.empty((length, 16, 16, 313), dtype=np.float32) for i_batch in range(length): name = self.names[i] image = os.path.join(self.image_folder, name) bgr = cv.imread(image) l, a, b = convert_rgb_to_lab(bgr) x = l / 255. out_a = cv.resize(a, (16, 16), cv.INTER_CUBIC) out_b = cv.resize(b, (16, 16), cv.INTER_CUBIC) out_a = out_a.astype(np.int32) - 128 ## / 256 * 190 - 90 out_b = out_b.astype(np.int32) - 128 ## / 256 * 190 - 90 y = softencoding(out_a, out_b) batch_x[i_batch, :, :, 0] = x batch_y[i_batch] = y i += 1 return batch_x, batch_y
def pixel_analyzing(src): cord = np.load("pts_in_hull.npy") nbrs = NearestNeighbors(n_neighbors=1, algorithm='ball_tree').fit(cord) main = os.fsencode(src) result = np.zeros(313) for folder in os.listdir(main): foldername = src + "/" + os.fsdecode(folder) + "/images/" for file in os.listdir(os.fsencode(foldername)): filename = os.fsdecode(file) if filename.endswith( ('.JPEG', '.png', '.jpg') ) and not filename.startswith("."): # image extension we need image = cv2.imread(foldername + filename) l_channel, a_channel, b_channel = convert_rgb_to_lab(image) a = a_channel.reshape(-1, 1) b = b_channel.reshape(-1, 1) X = np.column_stack((a, b)) distances, indices = nbrs.kneighbors(X) result[indices] += 1 #saved the indices in an array result = result / np.sum(result) sigma = 5 wts = np.exp(-result**2 / (2 * sigma**2)) wts = wts / np.sum([wts], axis=1)[:, np.newaxis] #print(wts) #print(np.sum(wts)) np.save("prior_probs", arr=wts)
def test_decode(): ### TODO: find a better way? image_path = "Dataset/Test/images/0.JPEG" image = cv2.imread(image_path) image_small = cv2.resize(image, (16, 16)) l, a, b = convert_rgb_to_lab(image) l_small, a_small, b_small = convert_rgb_to_lab(image_small) a_small = a_small.astype(np.int32) - 128 b_small = b_small.astype(np.int32) - 128 encoding = softencoding(a_small, b_small) a_dec, b_dec = decode(encoding) new_img_path = "test_res0.JPEG" merge_channels((new_img_path, l.astype(dtype=np.uint8)), (new_img_path, a_dec.astype(dtype=np.uint8), b_dec.astype(dtype=np.uint8)), "Dataset/Test", "unit_testing", True)
import cv2 ## Change with wanted model name model_name = 'new_model.h5' net = Network(model_name) net.load() test_folder = "Dataset/Test/images" folder = os.fsencode(test_folder) for file in os.listdir(folder): filename = os.fsdecode(file) if filename.endswith(('.JPEG', '.png', '.jpg')): image = cv2.imread(os.path.join(test_folder, filename)) image_small = cv2.resize(image, (16, 16)) l, a, b = convert_rgb_to_lab(image) x_test = np.empty((1, 64, 64, 1), dtype=np.float32) x_test[0, :, :, 0] = l / 255. pred = net.predict(x_test) a, b = decode(pred.reshape((16, 16, 313))) new_result_name = model_name + "_" + filename merge_channels((new_result_name, l.astype(dtype=np.uint8)), (new_result_name, a.astype(dtype=np.uint8), b.astype(dtype=np.uint8)), "Dataset", "merged", True)