Beispiel #1
0
def main():
    arg_parser = TestArgsParser()
    args = arg_parser()

    # load content image
    content_image = utils.load_image(args.content, max_size=args.max_size)

    # open session
    soft_config = tf.ConfigProto(allow_soft_placement=True)
    soft_config.gpu_options.allow_growth = True
    sess = tf.Session(config=soft_config)

    # build the graph
    transformer = style_transfer_tester.StyleTransferTester(
        session=sess,
        model_path=args.style_model,
        content_image=content_image,
    )

    # execute the graph
    start_time = time.time()
    output_image = transformer.test()
    end_time = time.time()

    # save result
    utils.save_image(output_image, args.output)

    # report execution time
    shape = content_image.shape
    print('Execution time for a %d x %d image : %.2f ms' %
          (shape[2], shape[1], 1000 * (end_time - start_time)))
Beispiel #2
0
def dataset_check(image_dir, xml_dir, labels, name):
    if not os.path.exists(image_dir):
        raise FileNotFoundError('{} is not exists'.format(image_dir))

    os.makedirs('./dataset_check', exist_ok=True)
    print('Start check {} dataset'.format(name))

    instances, _ = parse_annotation(xml_dir, image_dir, labels, name)
    idx = 0
    for instance in tqdm(instances, desc='Check {} dataset'.format(name)):
        idx += 1

        image_path = instance['filename']
        image = load_image(image_path)

        for object in instance['object']:
            cv2.rectangle(image, (object['xmin'], object['ymin']),
                          (object['xmax'], object['ymax']), (0, 255, 0), 2)
            cv2.putText(image, object['name'],
                        (object['xmin'], object['ymin'] - 5),
                        cv2.FONT_HERSHEY_SIMPLEX, 1e-3 * image.shape[0],
                        (0, 255, 0), 1)

        cv2.imwrite('./dataset_check/{}.jpg'.format(idx), image[..., -1])

    print('End check {} dataset!'.format(name))
Beispiel #3
0
def main(args):
    test_x, test_y = load_image(args.image_path)

    test_inp = to_tensor(test_x.astype(np.float32))
    test_target = to_tensor(test_y.astype(np.float32))

    generator = Generator().to("cuda")

    start_t = time.time()
    pretrain_model = flow.load(args.model_path)
    generator.load_state_dict(pretrain_model)
    end_t = time.time()
    print("load params time : {}".format(end_t - start_t))

    start_t = time.time()
    generator.eval()
    with flow.no_grad():
        gout = to_numpy(generator(test_inp), False)
    end_t = time.time()
    print("infer time : {}".format(end_t - start_t))

    # save images
    save_images(
        gout,
        test_inp.numpy(),
        test_target.numpy(),
        path=os.path.join("./testimage.png"),
        plot_size=1,
    )
Beispiel #4
0
    def __getitem__(self, idx):
        np.random.seed(int(str(time.time() % 1)[2:7]))

        if idx >= len(self.aneurysm_labels):
            neg_sample_flag = True
            idx = np.random.randint(len(self.aneurysm_labels))
        else:
            neg_sample_flag = False

        aneurysm = self.aneurysm_labels[idx]
        patient_idx = int(aneurysm[0])
        patient = self.patient_labels[patient_idx]
        aneurysm = aneurysm[1:]

        image_path = self.filenames[patient_idx]
        offset = 0.0
        scale = 1.0
        image = load_image(image_path, offset, scale)

        # rotate image and labels as well
        image_aug, aneurysm_aug, patient_aug = augment_image_label(
            image, aneurysm, patient, self.aug_factor)
        # image_aug, aneurysm_aug, patient_aug = image, aneurysm, patient

        # crop sample
        crop_dict = crop_patch(image_aug, aneurysm_aug, patient_aug,
                               neg_sample_flag, self.config)
        sample = crop_dict["image_patch"]
        coord = crop_dict["coord"]
        aneurysm_label = crop_dict["aneurysm_label"]
        patient_label = crop_dict["patient_label"]
        # print('  label ->', aneurysm_label[1:4], ', size =', aneurysm_label[4])

        # further flip augmentation
        sample, aneurysm_label, patient_label, coord = flip(
            sample, aneurysm_label, patient_label, coord)

        label = map_label(self.config, aneurysm_label, patient_label)
        sample = sample.astype(np.float32)

        return torch.from_numpy(sample), torch.from_numpy(
            label), coord, image_path
def main():
    args_parser = TrainArgsParser()
    args = args_parser()

    vgg_net = vgg19.VGG19(args.vgg_model_weights_path)

    training_images_paths = utils.get_files(args.dataset_path)
    style_image = utils.load_image(args.style)

    # create a map for content layers info
    CONTENT_LAYERS = {}
    for layer, weight in zip(args.content_layers,args.content_layer_weights):
        CONTENT_LAYERS[layer] = weight

    # create a map for style layers info
    STYLE_LAYERS = {}
    for layer, weight in zip(args.style_layers, args.style_layer_weights):
        STYLE_LAYERS[layer] = weight

    sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True))

    trainer = style_transfer_trainer.StyleTransferTrainer(
        input_shape=(256, 256),
        session=sess,
        content_layer_dict=CONTENT_LAYERS,
        style_layer_dict=STYLE_LAYERS,
        training_images=training_images_paths,
        style_image=style_image,
        discriminator=vgg_net,
        num_training_epochs=args.num_epochs,
        batch_size=args.batch_size,
        content_loss_weight=args.content_weight,
        style_loss_weight=args.style_weight,
        total_variance_loss_weight=args.total_variance_weight,
        learning_rate=args.learn_rate,
        model_save_path=args.output,
        saving_period=args.checkpoint_every
    )

    trainer.train()
    sess.close()
Beispiel #6
0
    def __getitem__(self, idx, split=None):
        # t = time.time()
        # np.random.seed(int(str(t % 1)[2:7]))
        np.random.seed(3)

        offset = 0.  #-535.85
        scale = 1.  #846.87
        imgs = load_image(self.filenames[idx], offset, scale)

        nz, nh, nw = imgs.shape[1:]
        pz = int(np.ceil(float(nz) / self.stride)) * self.stride
        ph = int(np.ceil(float(nh) / self.stride)) * self.stride
        pw = int(np.ceil(float(nw) / self.stride)) * self.stride
        imgs = np.pad(imgs, [[0, 0], [0, pz - nz], [0, ph - nh], [0, pw - nw]],
                      'constant',
                      constant_values=self.pad_value)

        xx, yy, zz = np.meshgrid(np.linspace(-0.5, 0.5,
                                             int(imgs.shape[1] / self.stride)),
                                 np.linspace(-0.5, 0.5,
                                             int(imgs.shape[2] / self.stride)),
                                 np.linspace(-0.5, 0.5,
                                             int(imgs.shape[3] / self.stride)),
                                 indexing='ij')
        coord = np.concatenate(
            [xx[np.newaxis, ...], yy[np.newaxis, ...], zz[np.newaxis, :]],
            0).astype('float32')
        imgs2, nzhw = self.split_comber.split(imgs)
        coord2, nzhw2 = self.split_comber.split(
            coord,
            side_len=int(self.split_comber.side_len / self.stride),
            max_stride=int(self.split_comber.max_stride / self.stride),
            margin=int(self.split_comber.margin / self.stride))
        assert np.all(nzhw == nzhw2)

        print('coord', coord.shape, '-->', coord2.shape)
        print('images', imgs.shape, '-->', imgs2.shape)

        imgs2 = imgs2.astype(np.float32)
        return torch.from_numpy(imgs2), torch.from_numpy(coord2), np.array(
            nzhw)
Beispiel #7
0
    def inference(self, weight_path, image_path, obj_threshold, nms_threshold):
        if not os.path.exists(weight_path):
            raise FileNotFoundError("{} is not exist".format(weight_path))

        if not os.path.exists(image_path):
            raise FileNotFoundError("{} is not exist".format(image_path))

        print("Load weight from {}".format(weight_path))
        self.model.load_weights(weight_path)

        print("Start Inference...")

        image = load_image(image_path)

        self.prediction(image, obj_threshold, nms_threshold)

        plt.figure(figsize=(10, 10))
        plt.imshow(image)
        plt.show()

        print("End Inference!")
Beispiel #8
0
def build_npy(image_dir, xml_dir, labels, name):
    if not os.path.exists(image_dir):
        raise FileNotFoundError('{} is not exists'.format(image_dir))

    if not os.path.exists(xml_dir):
        raise FileNotFoundError('{} is not exists'.format(xml_dir))

    print('Start making {} dataset...'.format(name))

    instances, _ = parse_annotation(xml_dir, image_dir, labels, name)
    data_image = []
    data_annotation = []

    for instance in tqdm(instances,
                         desc='Make npy format {} dataset'.format(name)):
        image_path = instance['filename']
        image = load_image(image_path)
        data_image.append(image)
        data_annotation.append(instance)

    np.save('./{}_image.npy'.format(name), data_image)
    np.save('./{}_annotation.npy'.format(name), data_annotation)

    print('End making {} dataset!'.format(name))
import torch

from utils.data_utils import load_image
from utils.model_pretrain import VGGNet

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
dtype = torch.cuda.FloatTensor
# 载入图片
transform = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])  # 来自ImageNet的mean和variance
unloader = transforms.ToPILImage()  # reconvert into PIL image

style = load_image("../data/data_styletransfer/starry_night.jpg",
                   transform,
                   max_size=400)
content = load_image("../data/data_styletransfer/tubingen.jpg",
                     transform,
                     shape=[style.size(2), style.size(3)])

# 加载模型

target = content.clone().requires_grad_(True)  # 输出图像
optimizer = torch.optim.Adam([target], lr=0.003, betas=(0.5, 0.999))
vgg = VGGNet().to(device).eval()

# 训练
total_step = 2000
content_weight = 1.
tv_weight = 1.0
Beispiel #10
0
idx = np.random.choice(len(aneurysm_labels))
if idx >= len(aneurysm_labels):
    neg_sample_flag = True
    idx = np.random.randint(len(aneurysm_labels))
else:
    neg_sample_flag = False

aneurysm_label = aneurysm_labels[idx]
patient_idx = int(aneurysm_label[0])
size = aneurysm_label[4]

image_path = filenames[patient_idx]
mean = 0.0
std = 100.0
image = load_image(image_path, mean, std)
patient_label = patient_labels[patient_idx]

print('-------- Select Image --------')
print('idx#{} in image'.format(idx), image.shape[1:])
print('aneurysm label', aneurysm_label[1:])
print('patient label', patient_label)

# Do Rotate or Zoom here
angle1 = np.random.uniform(-1, 1) * 45
angle1 = 90
rotmat = np.array(
    [[np.cos(angle1 / 180 * np.pi), -np.sin(angle1 / 180 * np.pi)],
     [np.sin(angle1 / 180 * np.pi),
      np.cos(angle1 / 180 * np.pi)]])