Exemple #1
0
def load_imagenet_test_data(normalize=False, count=-1, outType='YUV'):
    d = unpickle(IMAGENET_PATH + '/val_data')
    x = d['data']

    if count != -1:
        x = x[:count]

    return preproc(x, normalize=normalize, outType=outType)
Exemple #2
0
def load_cifar10_test_data(normalize=False, count=-1, outType='YUV'):
    filename = '{}/test_batch'.format(CIFAR10_PATH)
    batch_data = unpickle(filename)
    data_test = batch_data[b'data']
    labels_test = batch_data[b'labels']

    if count != -1:
        data_test = data_test[:count]

    return preproc(data_test, normalize=normalize, outType=outType)
Exemple #3
0
def load_imagenet_data(idx, normalize=False, flip=False, count=-1, outType='YUV'):
    data_file = IMAGENET_PATH + '/train_data_batch_'
    d = unpickle(data_file + str(idx))
    x = d['data']
    mean_image = d['mean']

    if count != -1:
        x = x[:count]

    return preproc(x, normalize=normalize, flip=flip, mean_image=mean_image, outType=outType)
Exemple #4
0
def imagenet_data_generator(batch_size, normalize=False, flip=False, scale=1, outType='YUV'):
    while True:
        for idx in range(1, 11):
            data_file = IMAGENET_PATH + '/train_data_batch_'
            d = unpickle(data_file + str(idx))
            x = d['data']
            mean_image = d['mean']
            count = 0
            while count <= x.shape[0] - batch_size:
                data = x[count:count + batch_size]
                count = count + batch_size

                if outType == 'YUV':
                    data_yuv, data_rgb = preproc(data, normalize=normalize, flip=flip, mean_image=mean_image)
                    yield data_yuv[:, :, :, :1], data_yuv[:, :, :, 1:] * scale

                elif outType == 'LAB':
                    lab, grey = preproc(data, normalize=normalize, flip=flip, mean_image=mean_image, outType=outType)
                    yield lab, grey
Exemple #5
0
def imagenet_test_data_generator(batch_size, normalize=False, scale=1, count=-1, outType='YUV'):
    d = unpickle(IMAGENET_PATH + '/val_data')
    x = d['data']

    if count != -1:
        x = x[:count]

    while True:
        count = 0
        while count <= x.shape[0] - batch_size:
            data = x[count:count + batch_size]
            count = count + batch_size

            if outType == 'YUV':
                data_yuv, data_rgb = preproc(data, normalize=normalize, outType=outType)
                yield data_yuv[:, :, :, :1], data_yuv[:, :, :, 1:] * scale

            elif outType == 'LAB':
                lab, grey = preproc(data, normalize=normalize, outType=outType)
                yield lab, grey
    def fit(self, iteration=1000):
        """ generate a style-transfered image.

        The overall process is as follows:
            1. initiating the initial canvas
            2. setting Evaluator
            3. optimizing using L-BFGS method

        Args:
            iteration (int): the total iteration number of optimization
        """

        if self.initial_canvas == 'random':
            generated_img = utils.preproc(np.random.uniform(0, 255, size=self.img_shape)\
                                .astype(K.floatx()))
        elif self.initial_canvas == 'content':
            generated_img = self.content_img.copy()
        else:  # style
            generated_img = self.style_img.copy()

        evaluator = Evaluator(self.eval_fn, self.img_shape)
        self.step = 0

        print('Starting optimization with L-BFGS-B')

        for i in range(1, iteration + 1):

            if self.verbose:
                print('Starting iteration %d' % (i))

            start_time = time.time()
            generated_img, min_loss, _ = fmin_l_bfgs_b(evaluator.loss,
                                                       generated_img.flatten(),
                                                       fprime=evaluator.grads,
                                                       callback=self._callback,
                                                       maxfun=20)
            generated_img = np.clip(generated_img, -127, 127)
            end_time = time.time()

            if self.verbose:
                print('Total_Loss: %g, Content Loss: %g, Style Loss: %g' % \
                                                                    (min_loss,
                                                                     evaluator.content_loss,
                                                                     evaluator.style_loss))
                print('Iteration %d completed in %d s' %
                      (i, end_time - start_time))

            if i == 1 or (self.save_every_n_iters != 0
                          and i % self.save_every_n_iters == 0):
                utils.save_image(utils.deproc(generated_img, self.img_shape),
                                 self.output_path,
                                 'generated_%d' % (i) + '.jpg')
Exemple #7
0
def load_cifar10_data(normalize=False, shuffle=False, flip=False, count=-1, outType='YUV'):
    names = unpickle('{}/batches.meta'.format(CIFAR10_PATH))[b'label_names']
    data, labels = [], []
    for i in range(1, 6):
        filename = '{}/data_batch_{}'.format(CIFAR10_PATH, i)
        batch_data = unpickle(filename)
        if len(data) > 0:
            data = np.vstack((data, batch_data[b'data']))
            labels = np.hstack((labels, batch_data[b'labels']))
        else:
            data = batch_data[b'data']
            labels = batch_data[b'labels']

    if shuffle:
        np.random.shuffle(data)

    if count != -1:
        data = data[:count]

    return preproc(data, normalize=normalize, flip=flip, outType=outType)
Exemple #8
0
if __name__ == '__main__':
    args = parse_args()

    #load img and mask
    noisy_img = utils.imread(args.noisy_img)
    if not (args.clean_img is None):
        clean_img = utils.imread(args.clean_img)
    output_dir = args.output_dir

    if len(noisy_img.shape) == 2:
        num_ch = 1
        noisy_img = noisy_img[:, :, np.newaxis]
    else:
        num_ch = noisy_img.shape[-1]

    noisy_img = Variable(utils.preproc(noisy_img))

    # Generate 2 DIP trajectories
    traj_set = []
    for _ in range(2):
        T = dip(noisy_img,
                lr=args.lr,
                niter=args.niter,
                traj_iter=args.traj_iter,
                num_ch=num_ch,
                reg_noise_std=args.reg_noise_std,
                n_ch_down=args.n_ch_down,
                n_ch_up=args.n_ch_up,
                skip_conn=args.skip_conn,
                depth=args.depth,
                act_fun=args.act_fun,
    def __init__(self,
                 content_path,
                 style_path,
                 output_path,
                 loss_ratio=1e-3,
                 verbose=True,
                 save_every_n_iters=10,
                 initial_canvas='content'):
        """ intialize all things for style transfer

        the overall process is as follows:
            1. image preprocessing
            2. define loss functions for content representations and style representations
            3. define evaluation function for training loss and gradients

        Args:
            content_image_path (str): The path for the content image
            style_image_path (str): The path for the style image
            loss_ratio (float): alpha divided by beta (beta is defined as 1)
            verbose (bool): True for print states, False otherwise
            save_every_n_steps: save images every n steps
            initial_canvas (str): the initial canvas for generated images.
                                  Choice in {'random', 'content', 'style'}
        """
        self.output_path = output_path
        self.save_every_n_iters = save_every_n_iters
        self.initial_canvas = initial_canvas
        self.verbose = verbose
        self.step = 0

        content_layer = DEFAULT_CONTENT_LAYER
        style_layers = DEFAULT_STYLE_LAYERS

        # load the style and content images
        content_img = utils.open_image(content_path)
        self.img_shape = (content_img.shape[0], content_img.shape[1], 3)
        self.content_img = utils.preproc(content_img)
        self.style_img = utils.preproc(
            utils.open_image(style_path, self.img_shape))
        content_img = K.variable(self.content_img)
        style_img = K.variable(self.style_img)

        # define a placeholder for a generated image
        if K.image_data_format() == 'channels_first':
            generated_img = K.placeholder(
                (1, 3, self.img_shape[0], self.img_shape[1]))
        else:
            generated_img = K.placeholder(
                (1, self.img_shape[0], self.img_shape[1], 3))

        # create a keras tensor for the input
        input_tensor = K.concatenate([content_img, style_img, generated_img],
                                     axis=0)

        # load VGG16 with the weights pretrained on imagenet.
        # ! the original paper uses vgg19 and replaces its all max_pooling to avg_pooling.
        vgg16 = VGG16(input_tensor=input_tensor,
                      include_top=False,
                      input_shape=self.img_shape)

        # outputs of each layer
        outputs_dict = {layer.name: layer.output for layer in vgg16.layers}

        # loss for the content image
        content_feat = outputs_dict[content_layer][0]
        generat_feat = outputs_dict[content_layer][2]
        feat_size, ch_size = utils.get_feat_channel_size(generat_feat)

        # ! the original paper suggests 'divided by 2'
        # the following denominator is from:
        # from https://github.com/cysmith/neural-style-tf/blob/master/neural_style.py
        content_loss = K.sum(K.square(content_feat - generat_feat)) \
                        / (2. *  feat_size * ch_size)

        # loss for the style image.
        style_loss = K.variable(0.)
        style_loss_weight = 1. / len(style_layers)

        for style_layer in style_layers:
            style_feat = outputs_dict[style_layer][1]
            generat_feat = outputs_dict[style_layer][2]
            feat_size, ch_size = utils.get_feat_channel_size(generat_feat)

            style_loss += style_loss_weight * \
                          K.sum(K.square(utils.gram_matrix(style_feat) - \
                                         utils.gram_matrix(generat_feat))) / \
                          (4. * feat_size ** 2 * ch_size ** 2)

        # composite loss
        beta = 1
        alpha = loss_ratio * beta
        content_loss = alpha * content_loss
        style_loss = beta * style_loss
        total_loss = content_loss + style_loss

        # gradients
        grads = K.gradients(total_loss, generated_img)

        # evaluation function
        self.eval_fn = K.function(
            [generated_img], [total_loss, content_loss, style_loss] + grads)
Exemple #10
0
def build_trainset_and_voc_char_level(ifilename,ofilename,vocabulary_file,shuffle=True):
    with open(ifilename,'r') as infile,open(ofilename,'w') as outfile,open(vocabulary_file,'w') as vocfile:
        vocabulary=defaultdict(lambda :0)
        trainingset=[]
        for eachline in infile:
            item=[]
            splits=eachline.split('=>')
            rule=splits[0].strip()
            node_info=splits[1].strip()
            semantics=splits[2].strip()

            item.append(node_info)
            item.append(semantics)

            char_list=[]
            bio_list=[]
            chunk_list=re.split(r'(\(.*?\)|\[.*?\])',rule)
            for each_chunk in chunk_list:
                if each_chunk.startswith('[') or each_chunk.startswith('('):
                    each_chunk=each_chunk.strip('[]()')
                    name,tag=each_chunk.split(':')
                    tmp_char_list=[i for i in preproc(name).split(' ') if i!='']
                    tmp_bio_list=[]
                    tag=tag.split('=')[1]
                    for each_char in tmp_char_list:
                        vocabulary[each_char]+=1
                        tmp_bio_list.append('I-'+tag)
                    char_list.extend(tmp_char_list)
                    tmp_bio_list[0]='B-'+tag
                    bio_list.extend(tmp_bio_list)
                else:
                    tmp_char_list=[i for i in preproc(each_chunk).split(' ') if i!='']
                    if tmp_char_list==[]:
                        continue
                    tmp_bio_list=[]
                    for each_char in tmp_char_list:
                        vocabulary[each_char]+=1
                        tmp_bio_list.append('O')
                    char_list.extend(tmp_char_list)
                    bio_list.extend(tmp_bio_list)
            item.append((char_list,bio_list))
            trainingset.append(item)

        if shuffle:
            random.shuffle(trainingset)

        for each in trainingset:
            outfile.write('\n') # 用来区分每一个sample
            outfile.write(each[0]+'\n') # 第一个位置是sentence classification结果
            outfile.write(each[1]+'\n') # 第二个位置是解析的语义槽
            char_list,bio_list=each[2]
            assert len(char_list)==len(bio_list)
            for idx,_ in enumerate(bio_list):
                outfile.write(char_list[idx]+'\t'+bio_list[idx]+'\n')
        total_num=len(trainingset)
        vocfile.write('<PAD>\t0\t0\n')
        vocfile.write('<UNK>\t1\t0\n')
        index=2
        tuples=list(sorted(vocabulary.items(),key=lambda x:x[1],reverse=True))
        for voc,count in tuples:
            vocfile.write(voc+'\t'+str(index)+'\t'+str(count)+'\n')
            index+=1
Exemple #11
0
if __name__ == '__main__':
    args = parse_args()
    print(args)
    img_file = args.img_file
    output_file = args.output_file
    mask_file = args.mask_file

    #load img and mask
    noisy_img = utils.imread(img_file)
    if len(noisy_img.shape) == 2:
        num_ch = 1
    else:
        num_ch = noisy_img.shape[-1]

    noisy_img = Variable(utils.preproc(noisy_img))
    if (len(mask_file) > 0):
        mask = utils.imread(mask_file)
        mask = Variable(utils.preproc(mask))
    else:
        mask = None

    dip(noisy_img,
        args.output_file,
        mask=mask,
        lr=args.lr,
        niter=args.niter,
        traj_iter=args.traj_iter,
        net_struct=args.net_struct,
        num_ch=num_ch,
        fixed_start=(args.fixed_start != 0),