def blackwhite_colorized_comparison(dir_color): images = os.listdir(dir_color) # dfine plot num_col = 6 num_rows = math.ceil(len(images) * 2 / num_col) plt.figure(figsize=(num_col * 2.5 + 1, (num_rows + 2) * 2.5 + 1)) gs1 = gridspec.GridSpec(num_rows + 2, num_col, width_ratios=[1] * num_col, wspace=0.03, hspace=0.03, top=1, bottom=0, left=0, right=1) # make plots for i, image in enumerate(images): image1 = load_images(dir_color, image) # plot image ax1 = plt.subplot(gs1[i * 2]) ax1.imshow(image1[:, :, 0], cmap='gray') ax2 = plt.subplot(gs1[i * 2 + 1]) ax2.imshow(color.lab2rgb(image1)) ax1.axis('off') ax2.axis('off') plt.subplot_tool() plt.savefig("../../../black-colored-comparison.jpg", bbox_inches='tight') plt.close()
def image_check(model, num_of_images, name, b_size=32, dim=3): script_dir = os.path.dirname( os.path.abspath(inspect.getfile( inspect.currentframe()))) # script directory rel_path = "../../test_set" abs_file_path = os.path.join(script_dir, rel_path) image_list = os.listdir(abs_file_path) all_images = np.zeros((num_of_images, 224, 224, dim)) for i in range(num_of_images): # get image image_lab = load_images(abs_file_path, image_list[i], size=(224, 224)) # image is of size 256x256 image_l = images_to_l(image_lab) all_images[i, :, :, :] = np.tile(image_l[:, :, np.newaxis], (1, 1, 1, dim)) color_im = model.predict(all_images, batch_size=b_size) for i in range(num_of_images): # to rgb lab_im = np.concatenate( (all_images[i, :, :, 0][:, :, np.newaxis], color_im[i]), axis=2) im_rgb = color.lab2rgb(lab_im) # save abs_svave_path = os.path.join(script_dir, '../../result_images/') scipy.misc.toimage(im_rgb, cmin=0.0, cmax=1.0).save(abs_svave_path + name + image_list[i])
def whole_image_check(model, num_of_images, name): # find directory script_dir = os.path.dirname( os.path.abspath(inspect.getfile( inspect.currentframe()))) # script directory rel_path = "../../test_set" abs_file_path = os.path.join(script_dir, rel_path) image_list = os.listdir(abs_file_path) # repeat for each image # lets take first n images for i in range(num_of_images): # get image image_lab = load_images(abs_file_path, image_list[i]) # image is of size 256x256 image_l = images_to_l(image_lab) h, w = image_l.shape # split images to list of images slices_dim = 256 // 32 slices = np.zeros((slices_dim * slices_dim, 32, 32, 1)) for a in range(slices_dim): for b in range(slices_dim): slices[a * slices_dim + b] = image_l[a * 32:(a + 1) * 32, b * 32:(b + 1) * 32, np.newaxis] # lover originals dimension to 224x224 to feed vgg and increase dim image_l_224_b = resize_image(image_l, (224, 224)) image_l_224 = np.repeat(image_l_224_b[:, :, np.newaxis], 3, axis=2).astype(float) # append together booth lists input_data = [slices, np.array([ image_l_224, ] * slices_dim**2)] # predict predictions_ab = model.predict(input_data) # reshape back original_size_im = np.zeros((h, w, 2)) for n in range(predictions_ab.shape[0]): a, b = n // slices_dim * 32, n % slices_dim * 32 original_size_im[a:a + 32, b:b + 32, :] = predictions_ab[n, :, :] # to rgb color_im = np.concatenate( (image_l[:, :, np.newaxis], original_size_im), axis=2) # color_im = np.concatenate(((np.ones(image_l.shape) * 50)[:, :, np.newaxis], original_size_im), axis=2) im_rgb = color.lab2rgb(color_im) # save abs_svave_path = os.path.join(script_dir, '../../result_images/') scipy.misc.toimage(im_rgb, cmin=0.0, cmax=1.0).save(abs_svave_path + name + image_list[i])
def generate_h5_small_vgg(self, size, name): import h5py # generate examples x1 = np.zeros((size, 32, 32, 1)) x2 = np.zeros((size, 224, 224, 1)) y = np.zeros((size, 32, 32, 2)) z1 = np.zeros((size, 224, 224, 3)) i = 0 while i < size: # print(i) # download image file_dir, file_name = self.select_file(i) lab_im = load_images(os.path.join(self.dir_from, file_dir), file_name, size=self.im_size) if type(lab_im) is not np.ndarray or lab_im == "error": continue h, w, _ = lab_im.shape random_i, random_j = random.randint(0, h - 32), random.randint(0, w - 32) im_part = lab_im[random_i: random_i + 32, random_j: random_j + 32, :] x1[i, :, :, :] = images_to_l(im_part)[:, :, np.newaxis] x2[i, :, :, :] = images_to_l(lab_im)[:, :, np.newaxis] y[i, :, :] = images_to_ab(im_part) z1[i, :, :, :] = lab_im i += 1 f = h5py.File(os.path.join(self.dir_to1, name), 'w') # Creating dataset to store features f.create_dataset('small', (size, 32, 32, 1), dtype='float', data=x1) f.create_dataset('vgg224', (size, self.im_size[0], self.im_size[0], 1), dtype='float', data=x2) # Creating dataset to store labels f.create_dataset('ab_hist', (size, 32, 32, 2), dtype='float', data=y) f.close() f = h5py.File(os.path.join(self.dir_to2, name), 'w') # Creating dataset to store features X1_dset = f.create_dataset('im', (i, 224, 224, 3), dtype='float') X1_dset[:] = z1[:i] f.close()
def alg_comparison_vertical(im_dir): # dfine plot files = os.listdir(im_dir) just_im_names = list(set([x.split("-")[-1] for x in files])) # just_im_names = ['n02215770_4433.JPEG', 'Screen Shot 2017-07-05 at 12.42.28.png', 'n02867401_971.JPEG', 'n02783994_7940.JPEG', 'n03219483_4276.JPEG', 'n03891538_27243.JPEG', 'n02213543_4098.JPEG', 'n01322221_4873.JPEG', 'n02903126_318.JPEG', 'n02124623_8147.JPEG', 'maruti-suzuki-swift-default-image.png-version201707131518.png', 'n02940385_203.JPEG'] num_col = 8 num_rows = len(just_im_names) plt.figure(figsize=(num_col * 2.5 + 1, (num_rows + 2) * 2.5 + 1)) gs1 = gridspec.GridSpec(num_rows + 2, num_col, width_ratios=[1] * num_col, wspace=0.03, hspace=0.03, top=1, bottom=0, left=0, right=1) orders_tup = sorted(methods_order_v.items(), key=itemgetter(1)) ordered_methods = [x[0] for x in orders_tup] # make plots for j in range(num_rows): for i in range(num_col): image1 = load_images(im_dir, ordered_methods[i] + just_im_names[j]) # plot image ax1 = plt.subplot(gs1[j * num_col + i]) ax1.imshow(color.lab2rgb(image1)) ax1.axis('off') # if ax1.is_first_row(): # ax1.set_title(images[i].split(".")[0], fontsize=9) if ax1.is_first_row(): ax1.set_title(rename_methods_v[ordered_methods[i]], fontsize=22, y=1.08) # ax1.text(-0.12, 0.5, rename_methods_v[ordered_methods[i]], # horizontalalignment='left', # verticalalignment='center', # transform=ax1.transAxes, # color="black", # rotation=90) plt.savefig("../../../images-methods-comparison-full.pdf", bbox_inches='tight', format='pdf') plt.close()
def alg_comparison(im_dir, methods, images): # dfine plot num_rows = len(methods) num_col = len(images) plt.figure(figsize=(num_col * 2.5 + 1, (num_rows + 2) * 2.5 + 1)) gs1 = gridspec.GridSpec(num_rows + 2, num_col, width_ratios=[1] * num_col, wspace=0.03, hspace=0.03, top=1, bottom=0, left=0, right=1) orders_tup = sorted(methods_order.items(), key=itemgetter(1)) ordered_methods = [x[0] for x in orders_tup] # make plots for j in range(num_rows): for i in range(num_col): image1 = load_images(im_dir, ordered_methods[j] + images[i]) # plot image ax1 = plt.subplot(gs1[j * num_col + i]) ax1.imshow(color.lab2rgb(image1)) ax1.axis('off') # if ax1.is_first_row(): # ax1.set_title(images[i].split(".")[0], fontsize=9) if ax1.is_first_col(): ax1.text(-0.10, 0.5, rename_methods[ordered_methods[j]], horizontalalignment='right', verticalalignment='center', transform=ax1.transAxes, color="black", rotation=90, multialignment='center') plt.savefig("../../../images-methods-comparison-100.jpg", bbox_inches='tight') plt.close()
plt.figure(figsize=(num_col * 2.5 + 1, (num_rows + 2) * 2.5 + 1)) gs1 = gridspec.GridSpec(num_rows + 2, num_col, width_ratios=[1] * num_col, wspace=0.03, hspace=0.03, top=1, bottom=0, left=0, right=1) original = [] # add grayscale image at beginning for j, alg in enumerate(file_prefix): image = load_images("../../test_set", image_name) # original.append(image) # plot image ax1 = plt.subplot(gs1[j]) ax1.imshow(image[:, :, 0], cmap='gray') ax1.text(0.03, 0.97, "črno-bela", horizontalalignment='left', verticalalignment='top', transform=ax1.transAxes, size=22, color="white", bbox=dict(boxstyle="round,pad=0.05", fc="black", lw=2))
# for directory_ in os.listdir(dir_to): # for file_ in os.listdir(os.path.join(dir_to, directory_)): # name = file_.split("-")[-1] # os.rename(os.path.join(dir_to, directory_, file_), os.path.join(dir_to, directory_, name)) # get originals # for file_ in os.listdir(os.path.join(dir_to, "imp9-full-test-full-")): # copyfile("../../../validation/" + file_, # "../../../EvalueationApp/static/images/originals/" + file_) import matplotlib.pyplot as plt for file_ in os.listdir(os.path.join(dir_to, "hist05-test-full-")): image1 = load_images(os.path.join(dir_to, "hist05-test-full-"), file_, size=(224, 224)) scipy.misc.toimage(color.lab2rgb(image1), cmin=0.0, cmax=1.0)\ .save(os.path.join(dir_to, "hist05-test-full-", file_)) # add other methods # methods_order_v = { # # "hist02-test-full-": 10, # "hist05-test-full-": 12, # "hyper03-test-full-": 3, # # "imp09-test-full-": 4, # # "imp9-full-test-full-": 7, # # "let-there-color-test-full-": 2, # "vgg-test-full-": 9} #
def whole_image_check_hist(model, num_of_images, name): # find directory script_dir = os.path.dirname( os.path.abspath(inspect.getfile( inspect.currentframe()))) # script directory rel_path = "../../test_set" abs_file_path = os.path.join(script_dir, rel_path) image_list = os.listdir(abs_file_path) # repeat for each image # lets take first n images for i in range(num_of_images): # get image image_lab = load_images(abs_file_path, image_list[i]) # image is of size 256x256 image_l = images_to_l(image_lab) h, w = image_l.shape # split images to list of images slices_dim = 256 // 32 slices = np.zeros((slices_dim * slices_dim * 4, 32, 32, 1)) for a in range(slices_dim * 2 - 1): for b in range(slices_dim * 2 - 1): slices[a * slices_dim * 2 + b] = image_l[a * 32 // 2:a * 32 // 2 + 32, b * 32 // 2:b * 32 // 2 + 32, np.newaxis] # lover originals dimension to 224x224 to feed vgg and increase dim image_l_224_b = resize_image(image_l, (224, 224)) image_l_224 = np.repeat(image_l_224_b[:, :, np.newaxis], 3, axis=2).astype(float) # append together booth lists input_data = [slices, np.array([ image_l_224, ] * slices_dim**2 * 4)] # predict predictions_hist = model.predict(input_data) # reshape back indices = np.argmax(predictions_hist[:, :, :, :], axis=3) predictions_a = indices // 20 * 10 - 100 + 5 predictions_b = indices % 20 * 10 - 100 + 5 # +5 to set in the middle box predictions_ab = np.stack((predictions_a, predictions_b), axis=3) original_size_im = np.zeros((h, w, 2)) for n in range(predictions_ab.shape[0]): a, b = n // (slices_dim * 2) * 16, n % (slices_dim * 2) * 16 if a + 32 > 256 or b + 32 > 256: continue # it is empty edge # weight decision if a == 0 and b == 0: weight = weight_top_left elif a == 0 and b == 224: weight = weight_top_right elif a == 0: weight = weight_top elif a == 224 and b == 0: weight = weight_bottom_left elif b == 0: weight = weight_left elif a == 224 and b == 224: weight = weight_bottom_right elif a == 224: weight = weight_bottom elif b == 224: weight = weight_right else: weight = weight_m im_a = predictions_ab[n, :, :, 0] * weight im_b = predictions_ab[n, :, :, 1] * weight original_size_im[a:a + 32, b:b + 32, :] += np.stack((im_a, im_b), axis=2) # to rgb color_im = np.concatenate( (image_l[:, :, np.newaxis], original_size_im), axis=2) # color_im = np.concatenate(((np.ones(image_l.shape) * 50)[:, :, np.newaxis], original_size_im), axis=2) im_rgb = color.lab2rgb(color_im) # save abs_svave_path = os.path.join(script_dir, '../../result_images/') scipy.misc.toimage(im_rgb, cmin=0.0, cmax=1.0).save(abs_svave_path + name + image_list[i])
def whole_image_check_overlapping_no_vgg(model, num_of_images, name): # find directory script_dir = os.path.dirname( os.path.abspath(inspect.getfile( inspect.currentframe()))) # script directory rel_path = "../../test_set" abs_file_path = os.path.join(script_dir, rel_path) image_list = os.listdir(abs_file_path) # repeat for each image # lets take first n images for i in range(num_of_images): # get image image_lab = load_images(abs_file_path, image_list[i]) # image is of size 256x256 image_l = images_to_l(image_lab) h, w = image_l.shape # split images to list of images slices_dim = 256 // 32 slices = np.zeros((slices_dim * slices_dim * 4, 32, 32, 1)) for a in range(slices_dim * 2 - 1): for b in range(slices_dim * 2 - 1): slices[a * slices_dim * 2 + b] = image_l[a * 32 // 2:a * 32 // 2 + 32, b * 32 // 2:b * 32 // 2 + 32, np.newaxis] # append together booth lists input_data = slices # predict predictions_ab = model.predict(input_data, batch_size=32) # reshape back original_size_im = np.zeros((h, w, 2)) for n in range(predictions_ab.shape[0]): a, b = n // (slices_dim * 2) * 16, n % (slices_dim * 2) * 16 if a + 32 > 256 or b + 32 > 256: continue # it is empty edge # weight decision if a == 0 and b == 0: weight = weight_top_left elif a == 0 and b == 224: weight = weight_top_right elif a == 0: weight = weight_top elif a == 224 and b == 0: weight = weight_bottom_left elif b == 0: weight = weight_left elif a == 224 and b == 224: weight = weight_bottom_right elif a == 224: weight = weight_bottom elif b == 224: weight = weight_right else: weight = weight_m im_a = predictions_ab[n, :, :, 0] * weight im_b = predictions_ab[n, :, :, 1] * weight original_size_im[a:a + 32, b:b + 32, :] += np.stack((im_a, im_b), axis=2) # to rgb color_im = np.concatenate( (image_l[:, :, np.newaxis], original_size_im), axis=2) # color_im = np.concatenate(((np.ones(image_l.shape) * 50)[:, :, np.newaxis], original_size_im), axis=2) im_rgb = color.lab2rgb(color_im) # save abs_svave_path = os.path.join(script_dir, '../../result_images/') scipy.misc.toimage(im_rgb, cmin=0.0, cmax=1.0).save(abs_svave_path + name + image_list[i])