text
stringlengths
1
93.6k
for model_file in model_files:
epoch = model_file.split('.')[0] if not test_once else '0'
output_dir = os.path.join(output_path, epoch)
file_results = os.path.join(output_dir,'results.txt')
if not os.path.exists(output_dir):
os.mkdir(output_dir)
output_dir = os.path.join(output_dir, 'density_maps')
if not os.path.exists(output_dir):
os.mkdir(output_dir)
trained_model = os.path.join(args.model_path, epoch + '.h5') if not test_once else args.model_path
while(not os.path.isfile(trained_model)):
time.sleep(3)
network.load_net(trained_model, net)
if args.test_batch_size != 1 or args.test_fixed_size != -1:
test_mae, test_mse, detail = test_model_patches(net, data_loader_test, args.save_output, \
output_dir, test_fixed_size=args.test_fixed_size, test_batch_size=args.test_batch_size, \
gpus=args.gpus)
else:
test_mae, test_mse, detail = test_model_origin(net, data_loader_test, args.save_output, \
output_dir, test_fixed_size=args.test_fixed_size, test_batch_size=args.test_batch_size, \
gpus=args.gpus)
log_text = 'TEST EPOCH: %s, MAE: %.2f, MSE: %0.2f' % (epoch, test_mae, test_mse)
print log_text
with open(file_results, 'w') as f:
f.write(detail + 'MAE: %0.2f, MSE: %0.2f' % (test_mae, test_mse))
# <FILESEP>
import torch
from libs.base_utils import do_resize_content
from imagedream.ldm.util import (
instantiate_from_config,
get_obj_from_str,
)
from omegaconf import OmegaConf
from PIL import Image
import numpy as np
from inference import generate3d
from huggingface_hub import hf_hub_download
import json
import argparse
import shutil
from model import CRM
import PIL
import rembg
import os
from pipelines import TwoStagePipeline
rembg_session = rembg.new_session()
def expand_to_square(image, bg_color=(0, 0, 0, 0)):
# expand image to 1:1
width, height = image.size
if width == height:
return image
new_size = (max(width, height), max(width, height))
new_image = Image.new("RGBA", new_size, bg_color)
paste_position = ((new_size[0] - width) // 2, (new_size[1] - height) // 2)
new_image.paste(image, paste_position)
return new_image
def remove_background(
image: PIL.Image.Image,
rembg_session = None,
force: bool = False,
**rembg_kwargs,
) -> PIL.Image.Image:
do_remove = True
if image.mode == "RGBA" and image.getextrema()[3][0] < 255:
# explain why current do not rm bg
print("alhpa channl not enpty, skip remove background, using alpha channel as mask")
background = Image.new("RGBA", image.size, (0, 0, 0, 0))
image = Image.alpha_composite(background, image)
do_remove = False
do_remove = do_remove or force
if do_remove:
image = rembg.remove(image, session=rembg_session, **rembg_kwargs)
return image
def do_resize_content(original_image: Image, scale_rate):
# resize image content wile retain the original image size
if scale_rate != 1:
# Calculate the new size after rescaling
new_size = tuple(int(dim * scale_rate) for dim in original_image.size)
# Resize the image while maintaining the aspect ratio
resized_image = original_image.resize(new_size)
# Create a new image with the original size and black background
padded_image = Image.new("RGBA", original_image.size, (0, 0, 0, 0))
paste_position = ((original_image.width - resized_image.width) // 2, (original_image.height - resized_image.height) // 2)
padded_image.paste(resized_image, paste_position)