def plot_evaluation(res, run_id, epoch): maybe_create_folder(join(dir_root, 'images', run_id)) for k in range(len(res['imgs_l'])): img_gray = l_to_rgb(res['imgs_l'][k][:, :, 0]) img_output = lab_to_rgb(res['imgs_l'][k][:, :, 0], res['imgs_ab'][k]) img_true = lab_to_rgb(res['imgs_l'][k][:, :, 0], res['imgs_true_ab'][k]) top_5 = np.argsort(res['imgs_emb'][k])[-5:] try: top_5 = ' / '.join(labels_to_categories[i] for i in top_5) except: ptop_5 = str(top_5) plt.subplot(1, 3, 1) plt.imshow(img_gray) plt.title('Input (grayscale)') plt.axis('off') plt.subplot(1, 3, 2) plt.imshow(img_output) plt.title('Network output') plt.axis('off') plt.subplot(1, 3, 3) plt.imshow(img_true) plt.title('Target (original)') plt.axis('off') plt.suptitle(top_5, fontsize=7) plt.savefig( join(dir_root, 'images', run_id, '{}_{}.png'.format(epoch, k))) plt.clf() plt.close()
def __init__(self, links_source: str, dest_dir: str): # Destination folder maybe_create_folder(dest_dir) self.dest_dir = dest_dir # If the source is a link download it if links_source.startswith('http://'): print('Using urllib.request for the link archive is extremely', 'slow, it\'s better to download the tgz archive manualy', 'and pass its path to this constructor', file=sys.stderr) links_source, _ = urllib.request.urlretrieve( links_source, join(dir_root, 'imagenet_fall11_urls.tgz')) # If the source is an archive extract it if links_source.endswith('.tgz'): with tarfile.open(links_source, 'r:gz') as tar: tar.extractall(path=dir_root) links_source = join(dir_root, 'fall11_urls.txt') if not isfile(links_source): pass #raise Exception('Links source not valid: {}'.format(links_source)) self.links_source = links_source
def __init__(self, source_dir: str, dest_dir: str): if not isdir(source_dir): raise Exception( 'Input folder does not exists: {}'.format(source_dir)) self.source_dir = source_dir # Destination folder maybe_create_folder(dest_dir) self.dest_dir = dest_dir
def __init__(self, links_source: str, dest_dir: str): # Destination folder maybe_create_folder(dest_dir) self.dest_dir = dest_dir if not isfile(links_source): raise Exception('Links source not valid: {}'.format(links_source)) if links_source.endswith('.tgz'): with tarfile.open(links_source, 'r:gz') as tar: tar.extractall(path=dir_root) links_source = join(dir_root, 'fall11_urls.txt') self.links_source = links_source
def __init__(self, inputs_dir: str, records_dir: str, checkpoint_source: str): if not isdir(inputs_dir): raise Exception( 'Input folder does not exists: {}'.format(inputs_dir)) self.inputs_dir = inputs_dir # Destination folder maybe_create_folder(records_dir) self.records_dir = records_dir # Inception checkpoint self.checkpoint_file = maybe_download_inception(checkpoint_source) # Utils self._examples_count = 0 self.records_names_gen = progressive_filename_generator( join(records_dir, 'lab_images_{}.tfrecord'))
def __init__(self, source_dir: str, dest_dir: str, verbose: int, skip: int): # 判断文件夹是否存在 if not isdir(source_dir): raise Exception( 'Input folder does not exists: {}'.format(source_dir)) self.source_dir = source_dir # 若输出文件夹不存在则创建 # Destination folder maybe_create_folder(dest_dir) self.dest_dir = dest_dir # 是否启用啰嗦模式 self.verbose = verbose #跳过文件 self.skip = skip
def plot_evaluation(res, run_id, epoch, is_eval=False): maybe_create_folder(join(dir_root, 'images', run_id)) for k in range(len(res['imgs_l'])): imgs_l = res['imgs_l'][k][:, :, 0] img_gray = l_to_rgb(imgs_l) zeros = np.zeros(res['imgs_ref_ab'][k][:, :, 0].shape) img_ab = lab_to_rgb(zeros, res['imgs_ab'][k]) img_fwd_ab = lab_to_rgb(zeros, res['imgs_fwd_ab'][k]) img_ref_ab = lab_to_rgb(zeros, res['imgs_ref_ab'][k]) img_output = lab_to_rgb(imgs_l, res['imgs_ab'][k]) img_fwd_output = lab_to_rgb(imgs_l, res['imgs_fwd_ab'][k]) img_ref_output = lab_to_rgb(imgs_l, res['imgs_ref_ab'][k]) img_true = lab_to_rgb(imgs_l, res['imgs_true_ab'][k]) # save simple single image output if is_eval: im = trim(img_ref_output) im.save(join(dir_root, 'images', run_id, '{}.png'.format(k)), "PNG") # display the colorfulness score on the image C_output = image_colorfulness(img_output) C_fwd_output = image_colorfulness(img_fwd_output) C_ref_output = image_colorfulness(img_ref_output) C_true = image_colorfulness(img_true) # display the cost function(MSE) output of the image cost = res['cost'] fig, axes = plt.subplots(2, 4) # Colorization ab axes[0,0].imshow(img_ab) axes[0,0].set_title('Deep Koalarization ab') axes[0,0].axis('off') # Low res ab axes[0,1].imshow(img_fwd_ab) axes[0,1].set_title('Feedforward Colorization ab') axes[0,1].axis('off') # Refined ab axes[0,2].imshow(img_ref_ab) axes[0,2].set_title('Refined ab') axes[0,2].axis('off') # Input (grayscale) axes[0,3].imshow(img_gray) axes[0,3].set_title('Input (grayscale)') axes[0,3].axis('off') # Colorization output axes[1,0].imshow(img_output) axes[1,0].set_title('Deep Koalarization output\n' + ("{:.4f}".format(C_output))) axes[1,0].axis('off') # Low Resolution output axes[1,1].imshow(img_fwd_output) axes[1,1].set_title('Feedforward Colorization output\n' + ("{:.4f}".format(C_fwd_output))) axes[1,1].axis('off') # Refinement output axes[1,2].imshow(img_ref_output) axes[1,2].set_title('Refinement output\n' + ("{:.4f}".format(C_ref_output))) axes[1,2].axis('off') # Target (original) axes[1,3].imshow(img_true) axes[1,3].set_title('Target (original)\n' + ("{:.4f}".format(C_true))) axes[1,3].axis('off') plt.suptitle('Cost(MSE): ' + str(cost), fontsize=7) plt.savefig(join( dir_root, 'images', run_id, '{}_{}.png'.format(epoch, k))) plt.clf() plt.close() # write on the output_colorfulness_*.txt file the colorfulness of the output image and the groundtruth image with open('output_colorfulness_{}.txt'.format(run_id), mode='a') as f: f.write('{},{}\n'.format(C_output, C_true))