示例#1
0
def create_batch_tensors(batch):
    """ Creates batch tensors from a batch of filepaths.

        Args:
            batch: A list of filepaths

        Returns:
            success: True if successful.
            x_train: The x_train tensor.
            y_train: The y_train tensor.
    """

    x_train = []
    y_train = []

    # Adds image filepaths to batch
    for filepath in batch:
        x_success, x_image = load_img(FLAGS.x_dir + "/" + filepath,
                                      size=(426, 240))
        y_success, y_image = load_img(FLAGS.y_dir + "/" + filepath,
                                      size=(854, 480))

        if not x_success or not y_success:
            return False, None, None

        x_train.append(x_image)
        y_train.append(y_image)

    # Removes dimensions of 1 from training set
    x_train = np.squeeze(x_train)
    y_train = np.squeeze(y_train)

    return True, x_train, y_train
示例#2
0
def main():
    #Get random dog image
    endpoint = "https://dog.ceo/api/breeds/image/random"
    response = requests.get(endpoint)
    img_url = response.json()['message']
    # Get style image and process it
    style_img_url = "https://upload.wikimedia.org/wikipedia/en/1/14/Picasso_The_Weeping_Woman_Tate_identifier_T05010_10.jpg"
    style_image = preprocess_image(load_img(style_img_url), 256)
    #Calculate style bottleneck for the preprocessed style image
    style_bottleneck = run_style_predict(style_image)

    #Process the content image
    content_image = preprocess_image(load_img(img_url), 384)
    # Stylize the content image using the style bottleneck
    stylized_image = run_style_transform(style_bottleneck, content_image)

    # Create a plot of the images
    fig = plot(content_image, style_image, stylized_image)
    tmpfile = BytesIO()
    fig.savefig(tmpfile, format='png')
    encoded = base64.b64encode(tmpfile.getvalue()).decode('utf8')

    # Display the output in html
    image_html = '<h1>A randoml dog in the style of Picasso!</h1>' + '<img src=\'data:image/png;base64,{}\'>'.format(
        encoded)
    return (image_html)
示例#3
0
    def __getitem__(self, index):
        tar_index   = index % self.tar_size
        clean = torch.from_numpy(np.float32(load_img(self.clean_filenames[tar_index])))
        noisy = torch.from_numpy(np.float32(load_img(self.noisy_filenames[tar_index])))
        
        clean = clean.permute(2,0,1)
        noisy = noisy.permute(2,0,1)

        clean_filename = os.path.split(self.clean_filenames[tar_index])[-1]
        noisy_filename = os.path.split(self.noisy_filenames[tar_index])[-1]

        #Crop Input and Target
        ps = self.img_options['patch_size']
        H = clean.shape[1]
        W = clean.shape[2]
        r = np.random.randint(0, H - ps)
        c = np.random.randint(0, W - ps)
        clean = clean[:, r:r + ps, c:c + ps]
        noisy = noisy[:, r:r + ps, c:c + ps]

        apply_trans = transforms_aug[random.getrandbits(3)]

        clean = getattr(augment, apply_trans)(clean)
        noisy = getattr(augment, apply_trans)(noisy)        

        return clean, noisy, clean_filename, noisy_filename
示例#4
0
    def __init__(self):
        print('initializing......')
        self.img_height = 600
        self.img_width = 600
        self.content_pic_name = 'dogs.jpg'
        self.style_pic_name = 'vege.jpg'
        self.content_path = './content/' + self.content_pic_name
        self.style_path = './style/' + self.style_pic_name
        self.output_path = './output'

        self.content_layers = ['block5_conv2']
        self.style_layers = [
            'block1_conv1', 'block2_conv1', 'block3_conv1', 'block4_conv1',
            'block5_conv1'
        ]
        self.style_layer_weights = [0.5, 1.0, 1.5, 3.0, 4.0]
        self.num_iterations = 500
        self.miss_percentage_threshold = 0.4
        self.lr_decay_rate = 0.8
        self.content_weight = 1
        self.style_weight = 1
        self.variation_weight = 0.01
        self.best_loss = float('inf')

        # params for adam optimizer
        lr = 6
        self.learning_rate = \
            tfe.Variable(lr, name='lr', dtype=tf.float32, trainable=False)  # default: 0.001
        self.beta1 = 0.9  # default: 0.9,
        self.beta2 = 0.999  # default: 0.999
        self.epsilon = 1e-8  # default: 1e-08

        # create functor model for eager execution
        # keep the layers of the model off training,
        # since it is the input image itself getting 'trained' in back propagation
        self.model = self._get_model()
        for layer in self.model.layers:
            layer.trainable = False
        # pass the content and style images through the model
        # to get the content and style features in batch
        content_image = \
            utils.load_img(self.content_path, self.img_height, self.img_width)
        style_image = \
            utils.load_img(self.style_path, self.img_height, self.img_width)
        stack_images = np.concatenate([style_image, content_image], axis=0)
        model_outputs = self.model(stack_images)
        num_style_layers = len(self.style_layers)
        self.content_features = \
            [content_layer[1] for content_layer in model_outputs[num_style_layers:]]
        self.style_features = \
            [style_layer[0] for style_layer in model_outputs[:num_style_layers]]
        self.gram_style_features = \
            [self._gram_matrix(style_feature) for style_feature in self.style_features]

        # set initial image
        self.init_image = \
            utils.load_img(self.content_path, self.img_height, self.img_width)
        self.init_image = \
            tfe.Variable(self.init_image, dtype=tf.float32)
示例#5
0
def main(argv):
    del argv

    content_img = load_img(FLAGS.content_path)
    style_img = load_img(FLAGS.style_path)

    mdl = models.StyleContent(content_img, style_img)
    mdl.train()
示例#6
0
def run():
    train_source_model()
    train_loader = load_img(r"mini_train_07")
    val_loader = load_img(r"test")

    clf = Net(CNN(), r"cnn_mnist_89sample45epoch24acc84.pth")
    clf.fit(train_loader)
    acc = clf.evaluate(val_loader)

    return clf, acc
示例#7
0
def run():
    tr = Trainer()
    ts = Tester()

    train_loader = load_img(r"D:\..MNIST_data\train")
    test_loader = load_img(r"D:\..MNIST_data\test")

    clf = tr.net(train_loader)
    acc = ts.get_acc(clf, test_loader)  #acc=99.39%

    return clf, acc
示例#8
0
    def __getitem__(self, index):
        tar_index   = index % self.tar_size
        clean = torch.from_numpy(np.float32(load_img(self.clean_filenames[tar_index])))
        noisy = torch.from_numpy(np.float32(load_img(self.noisy_filenames[tar_index])))
        
        clean = clean.permute(2,0,1)
        noisy = noisy.permute(2,0,1)

        clean_filename = os.path.split(self.clean_filenames[tar_index])[-1]
        noisy_filename = os.path.split(self.noisy_filenames[tar_index])[-1]
        
        return clean, noisy, clean_filename, noisy_filename
    def __getitem__(self, index):
        tar_index = index % self.tar_size

        clean = torch.from_numpy(np.float32(load_img(self.shape_images[tar_index])))
        noisy = torch.from_numpy(np.float32(load_img(self.blur_images[tar_index])))

        clean_filename = self.shape_images[tar_index]
        noisy_filename = self.blur_images[tar_index]

        clean = clean.permute(2, 0, 1)
        noisy = noisy.permute(2, 0, 1)

        return clean, noisy, clean_filename, noisy_filename
示例#10
0
    def read_trainset(self, data_dir, resize):
        '''
        Load and preprocess images from directory.
        Args: 
            data_dir (str): Directory of dataset. The directory must contain folders named 'A/' and 'B/'
            resize (tuple of ints): The images are resized by this parameter. e.g., resize=(512, 512)
        '''
        def preprocess_img(img, resize):
            img = utils.resize_img(img, size=resize)
            img = np.array(img)
            img = img[..., np.newaxis] if img.ndim == 2 else img
            assert img.ndim == 3
            return img

        A_path = os.path.join(data_dir, 'A', '*.png')
        B_path = os.path.join(data_dir, 'B', '*.png')

        A_files = sorted(glob(A_path))
        B_files = sorted(glob(B_path))

        assert len(A_files) > 0, 'directory cannot be empty: {}'.format(A_path)
        assert len(B_files) > 0, 'directory cannot be empty: {}'.format(B_path)

        A_names = [
            os.path.splitext(os.path.basename(file))[0] for file in A_files
        ]
        B_names = [
            os.path.splitext(os.path.basename(file))[0] for file in B_files
        ]

        assert A_names == B_names, 'filenames must match'

        A_imgs = [
            preprocess_img(utils.load_img(file), resize=resize)
            for file in A_files
        ]
        B_imgs = [
            preprocess_img(utils.load_img(file), resize=resize)
            for file in B_files
        ]

        assert [np.array(x).shape
                for x in A_imgs] == [np.array(x).shape for x in B_imgs
                                     ], 'image shapes must match'

        dataset = {'name': A_names, 'feature': A_imgs, 'label': B_imgs}
        print(" [*] Loaded %d images from %s." %
              (len(dataset['name']), data_dir))

        self._dataset.update(dataset)
        return self
    def __getitem__(self, idx: int):
        img = load_img(self.ids[idx])
        img_id = self.img_ids[idx]

        if self.transform:
            img = self.transform(img)
        return img, img_id
示例#12
0
def load_dataset_with_facial_features(csv_filename, label_map, new_size=None, include_images=False, include_augmented=True):
    df = pd.read_csv(csv_filename)
    x_data, y_data = [], []
    images = []
    for index, row in df.iterrows():
        landmarks = np.fromstring(row['features'], 'float32', sep=' ')
        if landmarks.shape[0] == 68*2:
            landmarks = np.reshape(landmarks, (68, 2))
            x_data.append(landmarks)
            y_data.append(label_map[row['label']])

            if include_augmented and type(row['random_augmentation']) is str:
                landmarks_augm = np.fromstring(row['random_augmentation'], 'float32', sep=' ')
                x_data.append(np.reshape(landmarks_augm, (68, 2)))
                y_data.append(label_map[row['label']])

            if include_images:
                im = load_img(row['file'], True, None)
                im = im[row['y0']:row['y1'], row['x0']:row['x1']]
                if new_size is not None:
                    im = cv2.resize(im, new_size, interpolation = cv2.INTER_AREA)
                images.append(im)
                if include_augmented and type(row['random_augmentation']) is str:
                    images.append(im)
    #x_data, y_data = shuffle(x_data, y_data, random_state=42)
    return  np.array(x_data),  np.array(y_data), np.array(images)
示例#13
0
    def write_tfrecord(self):
        """writes data to tfrecord file
        """
        # build batch of image data
        # self.filepaths is dynamic, is better to call it once outside the loop
        filepaths = self.filepaths
        labels = self.labels
        tfrecord = self.tfrecord
        with tf.io.TFRecordWriter(tfrecord) as writer:
            for fpath, label in tqdm(zip(filepaths, labels),
                                     desc='writing images to tfrecords'):
                img = load_img(fpath,
                               color_mode=self.color_mode,
                               target_size=self.target_size,
                               interpolation=self.interpolation)
                x = img_to_array(img, data_format=self.data_format)
                # Pillow images should be closed after `load_img`,
                # but not PIL images.
                if hasattr(img, 'close'):
                    img.close()
                if self.image_data_generator:
                    for _ in range(self.num_copies):
                        x_copy = x.copy()
                        params = self.image_data_generator.get_random_transform(
                            x_copy.shape)
                        x_copy = self.image_data_generator.apply_transform(
                            x_copy, params)
                        x_copy = self.image_data_generator.standardize(x_copy)
                        # convert augmented image
                        self._write_image(x_copy, label, writer)

                # write th original
                self._write_image(x, label, writer)
def stylize_image(image_path, style, style_weight):
    image = load_img(image_path)
    style_weights = {1: '10',
                     2: '100',
                     3: '1000'}
    print('stylise_image')
    transformer = TransformerNet()
    optimizer = tf.optimizers.Adam(learning_rate = 0.001)
    ckpt = tf.train.Checkpoint(transformer=transformer, optimizer=optimizer, step=tf.Variable(1))
    # ckpt = tf.train.Checkpoint(transformer=transformer)
    # ckpt.restore(tf.train.latest_checkpoint(args.log_dir)).expect_partial()
    model_path = f'models/style/{style}_sw{style_weights[style_weight]}'
    ckpt.restore(tf.train.latest_checkpoint(model_path)).expect_partial()
    # ckpt.restore(tf.train.latest_checkpoint('models/style/la_muse_contentlayer33_sw100')).expect_partial()
    print('ckpt')

    transformed_image = transformer(image)
    print('transformed_image')
    transformed_image = tf.cast(
        tf.squeeze(transformed_image), tf.uint8
    ).numpy()
    print('transformed_image cast')

    img = Image.fromarray(transformed_image, mode="RGB")
    output_path = f'images/output/output_{style}_sw{style_weight}.png'
    img.save(output_path)
    return output_path
示例#15
0
 def next(self):
     if self.isfile:
         ok, frame = self.cap.read()
         if self.inds != None:
             if self.meta_idx < len(self.inds):
                 while self.count < self.inds[self.meta_idx]:
                     self.count += 1
                     ok, frame = self.cap.read()
             else:
                 ok = False
         if ok and self.gray:
             frame = utils.bgr2gray(frame)
     else:
         if self.count >= self.nframes or (self.inds != None and
                                           self.meta_idx >= len(self.inds)):
             ok = False
         else:
             ok = True
             idx = self.inds[
                 self.meta_idx] if self.inds != None else self.count
             frame = utils.load_img(self.files[idx],
                                    gray=self.gray,
                                    use_skimage=self.use_skimage)
     if not ok:
         raise StopIteration
     else:
         self.meta_idx += 1
         self.count += 1
         return frame
示例#16
0
def train_source_model():
    from model_nn_torch_DataLoader import Trainer
    tr = Trainer()

    train_loader = load_img(r"mini_train_89")
    clf = tr.net(train_loader)
    torch.save(clf.state_dict(), r"cnn_mnist_89sample45epoch24acc84.pth")
示例#17
0
def predict_img(img_path):
    # switching to GPU if possible
    use_gpu = torch.cuda.is_available()
    print("\nusing GPU:", use_gpu)

    # loading model
    print("\nLoading model...")
    model = net.load_model(use_gpu=use_gpu)
    if use_gpu:
        model = model.cuda()

    # setting model to evaluation mode
    model.eval()

    # reading image
    print("\nLoading and running image...")
    img = load_img(img_path)

    # running model on the image
    if use_gpu:
        img = img.cuda()

    output = model(img)

    # transforming and plotting the results
    output = output.cpu()[0].data.numpy()
    img = img.cpu()[0].data.numpy()
    show_img_pred(img, output)
示例#18
0
def running(file, file_recreate, file_comp, file_ext):
    img = util.load_img(file)                       # Loads the image selected
    #img = po2(img)                                 # Converts it to nearest power of 2 image
    #print("Image dimensions: ", img.size)
    coef, comp_rep = comp.extract_rgb_coeff(img)    # Extracts the RBG Coefficients from the image and also the compressed form of the image
    image = comp.img_from_dwt_coeff(coef)           # Forms the new image using the dwt coeeficients
    comp_file = "compress"+file_ext
    image.save(comp_file)                           # Saves the image       
    comp_rep.save(file_comp)                        # Saves the compressed representation of the image
    '''
    Below lines of the code are to resize and enhance the images
    '''
    enhancer = ImageEnhance.Brightness(image)
    image = enhancer.enhance(2)
   
    file_enh = "enhanced"+file_ext
    image.save(file_enh)
    im = Image.open(file_enh)
    size = img.size
    im_resized = im.resize(size, Image.BICUBIC)
    im_resized.save(file_recreate)
    
    os.remove(comp_file)
    os.remove(file_enh)
    return os.path.getsize(file)/os.path.getsize(file_comp)
示例#19
0
    def read_testset(self, data_dir, resize):
        def preprocess_img(img, resize):
            img = utils.resize_img(img, size=resize)
            img = np.array(img)
            img = img[..., np.newaxis] if img.ndim == 2 else img
            assert img.ndim == 3
            return img

        data_dir = os.path.join(data_dir, '*.png')

        files = sorted(glob(data_dir))

        assert len(files) > 0, 'directory cannot be empty: {}'.format(files)

        names = [os.path.splitext(os.path.basename(file))[0] for file in files]
        imgs = [
            preprocess_img(utils.load_img(file), resize=resize)
            for file in files
        ]

        dataset = {'name': names, 'feature': imgs, 'label': imgs}
        print(" [*] Loaded %d images from %s." %
              (len(dataset['name']), data_dir))

        self._dataset.update(dataset)
        return self
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("img_path", type=str,  help="path to the RGB image input")
    args = parser.parse_args()
    
    # switching to GPU if possible
    use_gpu = torch.cuda.is_available()
    print("\nusing GPU:", use_gpu)    
    
    # loading model
    print("\nLoading model...")
    model = net.load_model(use_gpu=use_gpu)
    if use_gpu:
        model = model.cuda()
            
    # setting model to evaluation mode
    model.eval()
        
    # reading image
    print("\nLoading and running image...")
    img = load_img(args.img_path)
    
    # running model on the image
    if use_gpu:
        img = img.cuda()

    output = model(img)
    
    # transforming and plotting the results
    output = output.cpu()[0].data.numpy()
    img = img.cpu()[0].data.numpy()
    show_img_pred(img, output)
示例#21
0
def load_facial_dataset_for_autoencoder(img_dir, csv_filename, greyscale=True, new_size=None):
    # bbox_x0,bbox_y0,bbox_x1,bbox_y1
    df = pd.read_csv(csv_filename)
    x_data, y_data = [], []
    for index, row in df.iterrows():
        im = load_img(img_dir + row['name'] + '.jpg', greyscale, None)
        if greyscale:
            im = im[row['bbox_y0']:row['bbox_y1'], row['bbox_x0']:row['bbox_x1']]
        else:
            im = im[row['bbox_y0']:row['bbox_y1'], row['bbox_x0']:row['bbox_x1'], :]
        if new_size is not None:
            im = cv2.resize(im, new_size, interpolation = cv2.INTER_AREA)
        x_data.append(im)
        coords = row.iloc[3:-4].values
        w_org = new_size[1] / (row['bbox_x1'] -  row['bbox_x0'])
        h_org = new_size[0] / (row['bbox_y1'] -  row['bbox_y0'])
        coords = np.reshape(np.array(coords), (76, 2))
        coords = (coords - [row['bbox_x0'], row['bbox_y0']]) * [w_org, h_org]
        coords = coords.astype('int')
        coords = np.clip(coords, 0, [new_size[1] - 1, new_size[0] - 1])
        mask = np.zeros((new_size), dtype='float32')
        mask[[point for point in coords]] = 1
        y_data.append(mask)
    x_data, y_data = shuffle(x_data, y_data, random_state=42)
    return x_data, y_data
示例#22
0
文件: predict.py 项目: abdkiwan/unet
def main():

    FLAGS = parser.parse_args()

    # Calculate the predictions for all the images in the input_img_dir.
    for img_name in os.listdir(FLAGS.input_img_dir):

        if img_name.endswith('.png'):
            original_img = load_img(
                FLAGS.input_img_dir + '/' +
                img_name)  # load_img function exists in file utils.py

            # Resizing image because of the small memory size
            input_img = tl.prepro.imresize(original_img,
                                           [FLAGS.img_size, FLAGS.img_size])
            input_img = np.reshape(input_img,
                                   [1, FLAGS.img_size, FLAGS.img_size, 3])

            unet = UNet(FLAGS.img_size)

            # The output is an array of the size(img_size * img_size, 1)
            prediction = unet.predict(input_img, FLAGS.model_save_dir)

            # Saving the image given the probabilities
            # save_img fnuction exists in file utils.py
            save_img(
                prediction, original_img, FLAGS.img_size, FLAGS.input_img_dir +
                '/' + img_name.split('.')[0] + '_pred.png')
示例#23
0
    def _get_batches_of_transformed_samples(self, index_array):
        """Gets a batch of transformed samples.

        # Arguments
            index_array: Array of sample indices to include in batch.

        # Returns
            A batch of transformed samples.
        """
        batch_x = np.zeros((len(index_array),) + self.image_shape, dtype=self.dtype)
        # build batch of image data
        # self.filepaths is dynamic, is better to call it once outside the loop
        filepaths = self.filepaths
        for i, j in enumerate(index_array):
            img = load_img(filepaths[j],
                           color_mode=self.color_mode,
                           target_size=self.target_size,
                           interpolation=self.interpolation)
            x = img_to_array(img, data_format=self.data_format)
            # Pillow images should be closed after `load_img`,
            # but not PIL images.
            if hasattr(img, 'close'):
                img.close()
            if self.image_data_generator:
                params = self.image_data_generator.get_random_transform(x.shape)
                x = self.image_data_generator.apply_transform(x, params)
                x = self.image_data_generator.standardize(x)
            batch_x[i] = x
        # optionally save augmented images to disk for debugging purposes
        if self.save_to_dir:
            for i, j in enumerate(index_array):
                img = array_to_img(batch_x[i], self.data_format, scale=True)
                fname = '{prefix}_{index}_{hash}.{format}'.format(
                    prefix=self.save_prefix,
                    index=j,
                    hash=np.random.randint(1e7),
                    format=self.save_format)
                img.save(os.path.join(self.save_to_dir, fname))
        # build batch of labels
        if self.class_mode == 'input':
            batch_y = batch_x.copy()
        elif self.class_mode in {'binary', 'sparse'}:
            batch_y = np.empty(len(batch_x), dtype=self.dtype)
            for i, n_observation in enumerate(index_array):
                batch_y[i] = self.classes[n_observation]
        elif self.class_mode == 'categorical':
            batch_y = np.zeros((len(batch_x), len(self.class_indices)),
                               dtype=self.dtype)
            for i, n_observation in enumerate(index_array):
                batch_y[i, self.classes[n_observation]] = 1.
        elif self.class_mode == 'multi_output':
            batch_y = [output[index_array] for output in self.labels]
        elif self.class_mode == 'raw':
            batch_y = self.labels[index_array]
        else:
            return batch_x
        if self.sample_weight is None:
            return batch_x, batch_y
        else:
            return batch_x, batch_y, self.sample_weight[index_array]
示例#24
0
def deepdream(model,
              imgp,
              n_octaves=10,
              octave_scale=1.4,
              lr=0.01,
              iters=20,
              verbose=True,
              interval=5,
              layer_n=10):
    model.eval()
    img = utils.load_img(imgp)

    octaves = [img]
    for _ in range(n_octaves - 1):
        octaves.append(utils.zoom(octaves[-1], octave_scale))

    detail = torch.zeros(*octaves[-1].size()).cuda()
    for i, octave in enumerate(octaves[::-1]):
        if i > 0:
            detail = utils.unzoom(detail, octave.size()[2:])

        currn_img = octave + detail
        dream_img = dreamchapter(model,
                                 currn_img,
                                 lr=lr,
                                 iters=iters,
                                 verbose=verbose,
                                 interval=interval,
                                 layer_n=layer_n)
        detail = dream_img - octave

    return dream_img
示例#25
0
 def next(self):
     if self.isfile:
         ok, frame = self.cap.read()
         if self.inds != None:
             if self.meta_idx < len(self.inds):
                 while self.count < self.inds[self.meta_idx]:
                     self.count += 1
                     ok, frame = self.cap.read()
             else:
                 ok = False
         if ok and self.gray:
             frame = utils.bgr2gray(frame)
     else:
         if self.count >= self.nframes or (self.inds != None and self.meta_idx >= len(self.inds)):
             ok = False
         else:
             ok = True
             idx = self.inds[self.meta_idx] if self.inds != None else self.count
             frame = utils.load_img(self.files[idx], gray=self.gray, use_skimage=self.use_skimage)
     if not ok:
         raise StopIteration
     else:
         self.meta_idx += 1
         self.count += 1
         return frame
示例#26
0
文件: bsdset.py 项目: dbisk/bmcn
 def __getitem__(self, idx):
     # TODO: allow slices instead of forcing just 1 idx at a time
     # TODO: suppress warning following line outputs
     truth = load_img(self.im_paths[idx], to_grayscale=True)
     if (self.transform):
         truth = self.transform(truth)
     noisy = add_noise(truth)
     return {'data': noisy, 'truth': truth}
    def __getitem__(self, index):
        row = self.df.iloc[index]
        img = load_img(os.path.join(self.path, row["Image"]))

        if self.transforms is not None:
            img = self.transforms(img)

        return img, row["Id"]
示例#28
0
def apply_style_on_painting(painting, style):
    painting = ski.img_as_ubyte(painting)
    ski_io.imsave('test_painting.png', painting)
    painting, painting_orig_shape = utils.load_img('test_painting.png', transform, device, True)

    style = ski.img_as_ubyte(style)
    ski_io.imsave('test_style.png', style)
    style = utils.load_img('test_style.png', transform, device)

    gen_painting = painting.clone().requires_grad_(True)
    
    optimizer = optim.Adam([gen_painting], lr=learning_rate)

    for step in range(total_steps):
        orig_features = model(painting)
        style_features = model(style)
        gen_features = model(gen_painting)

        orig_loss = style_loss = 0
        for orig_feature, style_feature, gen_feature in zip(
            orig_features
            , style_features
            , gen_features
        ):

            batch_size, c, h, w = gen_feature.shape
            orig_loss += torch.mean((gen_feature - orig_feature) ** 2)

            gen_gram = gen_feature.view(c, h * w).mm(gen_feature.view(c, h * w).t())
            style_gram = style_feature.view(c, h * w).mm(style_feature.view(c, h * w).t())
            style_loss += torch.mean((gen_gram - style_gram) ** 2)

        total_loss = alpha * orig_loss + beta * style_loss

        optimizer.zero_grad()
        total_loss.backward()
        optimizer.step()

        if step % log_freq == 0:
            gen_painting_numpy = gen_painting.clone().cpu().detach().numpy()[0]
            gen_painting_numpy = np.moveaxis(gen_painting_numpy, 0, -1)
            gen_painting_numpy = cv.resize(gen_painting_numpy, painting_orig_shape)
            gen_painting_numpy = ski_exposure.rescale_intensity(gen_painting_numpy, out_range=(0., 1.))

            yield step, total_loss.item(), gen_painting_numpy
示例#29
0
def predict(pic_path, model_path):
    model.load_weights(model_path)
    data = np.empty((1, 60, 120, 3), dtype="uint8")
    raw_img = utils.load_img(pic_path)
    data[0] = raw_img / 255

    out = model.predict(data)
    result = np.array([np.argmax(i) for i in out])
    return ''.join([utils.CAT2CHR[i] for i in result])
    def __getitem__(self, index):
        tar_index   = index % self.tar_size
        

        clean = torch.from_numpy(np.float32(load_img(self.shape_images[tar_index])))
        noisy = torch.from_numpy(np.float32(load_img(self.blur_images[tar_index])))
        dark = torch.from_numpy(np.float32(load_img(self.dark_images[tar_index])))


        clean_filename = os.path.split(self.shape_images[tar_index])[-1]
        noisy_filename = os.path.split(self.blur_images[tar_index])[-1]
        dark_filename = os.path.split(self.dark_images[tar_index])[-1]

        clean = clean.permute(2,0,1)
        noisy = noisy.permute(2,0,1)
        dark = dark.permute(2,0,1)

        return clean, noisy,dark,  clean_filename, noisy_filename, dark_filename
    def __getitem__(self, index):
        anchor_img_path, anchor_label = self.df.iloc[index][
            "Image"], self.df.iloc[index]["Id"]
        positive_index, negative_index = self._get_triplet_indexes(
            anchor_label, index)

        positive_img_path = self.df.iloc[positive_index]["Image"]
        negative_img_path = self.df.iloc[negative_index]["Image"]

        anchor_img = load_img(os.path.join(self.path, anchor_img_path))
        positive_img = load_img(os.path.join(self.path, positive_img_path))
        negative_img = load_img(os.path.join(self.path, negative_img_path))

        if self.transforms:
            anchor_img = self.transforms(anchor_img)
            positive_img = self.transforms(positive_img)
            negative_img = self.transforms(negative_img)
        return (anchor_img, positive_img, negative_img), []
示例#32
0
 def __init__(self, path):
     self._surf = load_img(path)