コード例 #1
0
def main():
    if os.path.isdir(_DST_DIR) is False:
        os.makedirs(_DST_DIR)

    test_files = fetch_filelist(_DATA_DIR)
    category_map = parse_category_map('labels.txt')

    g = tf.Graph()
    with g.as_default():
        model = create_model()
        model.summary()
        latest = tf.train.latest_checkpoint(_CHECKPOINT_DIR)
        model.load_weights(latest)

        for f in test_files:
            dst_dir = os.path.join(_DST_DIR,
                                   os.path.basename(os.path.dirname(f)))
            if os.path.isdir(dst_dir) is False:
                os.makedirs(dst_dir)
            dst_filepath = os.path.join(dst_dir, os.path.basename(f))
            x = img_to_array(load_img(f, target_size=(224, 224)))
            array_to_img(x)
            image, score, class_idx = grad_cam(model, x, 'conv_pw_13_relu')
            cam = array_to_img(image)
            save_img(dst_filepath, cam)
            score_filepath = dst_filepath.replace('.jpg', '.txt')
            category_name = category_map[class_idx]
            with open(score_filepath, 'w', encoding='utf-8') as w:
                w.write(category_name + ':' + str(score))
コード例 #2
0
def create_fake_dataset_and_convert_to_tfrecords(image_array,
                                                 n_images_per_class=1):
    dataset_dir = tempfile.mkdtemp()
    os.makedirs(os.path.join(dataset_dir, 'cat'), exist_ok=True)

    for i in range(n_images_per_class):
        keras_preprocessing.save_img(os.path.join(dataset_dir, 'cat',
                                                  '{}.jpeg'.format(i)),
                                     image_array[i],
                                     scale=False)

    args = {
        'name': 'tfrecords',
        'description': 'test',
        'tfrecord_dir_path': dataset_dir,
        'tfrecord_size': 1,
        'preprocessing': 'NO',
        'image_size': DIMENSIONS[1:3],
        "n_tfrecords": 1,
        'data': {
            'images_dir_path': dataset_dir,
            'annotation_file_path': None,
            'delimiter': ',',
            'header_exists': False,
            'split_names': ['train'],
            'split_percentages': [1.0],
        }
    }
    # generate tfrecords
    build_tfrecord_dataset(args)
    return dataset_dir
コード例 #3
0
ファイル: utils.py プロジェクト: milanseries/cnn-raccoon
def save_input_images(images):
    """
    Saves input images and expose them to the Flask server.

    :param images: Input images taken from Inspector
    """
    input_images = images_top_dir + "/input_images"

    if not os.path.exists(input_images):
        os.mkdir(input_images)

    for i in range(images.shape[0]):
        ts = datetime.datetime.now().timestamp()

        img_path = input_images + "/img_{}_{}.jpg".format(
            str(ts).replace(".", ""), i)
        img_relative = img_relative_path + "/input_images" + "/img_{}_{}.jpg".format(
            str(ts).replace(".", ""), i)

        image = images[i]
        image = smart_resize(image, size=(128, 128))

        save_img(img_path, image)

        if i in input_images_dict.keys():
            input_images_dict[i].append(img_relative)
        else:
            input_images_dict[i] = img_relative
コード例 #4
0
ファイル: image_ops.py プロジェクト: benygood/deconv
def to_small_pitch(path, pitch_size=(50, 50), input_shape=(224, 224, 3)):
    img = image.load_img(path, target_size=pitch_size)
    x = image.img_to_array(img)
    out = np.zeros(input_shape)
    out[50:50 + pitch_size[0], 50:50 + pitch_size[1], :] = x
    fname = os.path.basename(path)
    image.save_img("small_pitch_{}".format(fname), out)
コード例 #5
0
    def train(self,epoch):

        # Adversarial ground truths
        real = np.ones((self.batch_size, 1))
        fake = - np.ones((self.batch_size, 1))
        #fake = np.zeros((self.batch_size, 1))
        dummy = np.zeros((self.batch_size, 1))

        for epoch_num in range(0,epoch+1):
            train_img = next(self.train_data)[0]
            if train_img.shape[0] != self.batch_size:
                train_img = next(self.train_data)[0]
            

            # ---------------------
            #  Train Discriminator
            # ---------------------
            # Select a random batch of images

            masked_imgs, mask, coord, pad_size = self.block_patch(train_img, mode='central_box', patch_size=self.patch_size, margin=self.margin)
            y1, y2, x1, x2 = coord[0].numpy(), coord[0].numpy()+pad_size[0].numpy(), coord[1].numpy(), coord[1].numpy()+pad_size[1].numpy()
        
            # Generate a batch of new images
            inpainted_imgs = self.G.predict(masked_imgs)
            inpainted_fields = self.inpaint_crop(inpainted_imgs, coord, pad_size)

            real_fields = self.inpaint_crop(train_img, coord, pad_size)
            
            filled_in = tf.identity(masked_imgs).numpy() # tensor copy
            filled_in[:, y1:y2, x1:x2, :] = inpainted_fields.numpy()
            filled_in = tf.constant(filled_in)

            # Train the discriminator
            #D.trainable = True

            interpolated_img = RandomWeightedAverage(self.batch_size)(inputs = [train_img, inpainted_imgs])
            interpolated_field = self.inpaint_crop(interpolated_img, coord, pad_size)


            for _ in range(1):
                d_loss = self.C.train_on_batch([train_img, real_fields, inpainted_imgs,inpainted_fields,interpolated_img,interpolated_field],[real,fake,dummy])

            #print(self.C.predict([train_img, real_fields, inpainted_imgs,inpainted_fields,interpolated_img,interpolated_field])[2])
            print("D:",d_loss)
            # ---------------------
            #  Train Generator
            # ---------------------
            #D.trainable = False
            
            #g_loss = self.GL.train_on_batch([inpainted_imgs,inpainted_fields], [real_fields, real])
            g_loss = self.GL.train_on_batch(masked_imgs, [train_img, real])
            print("G:",g_loss)

            print("\n{} epoch".format(epoch_num))
            # If at save interval => save generated image samples
            if epoch_num % self.sample_interval == 0:

                save_img(self.img_save_dir+"filled_in"+str(epoch_num) +".jpg",filled_in[0])
                
                self.save_weight()
コード例 #6
0
def get_landmarks(images, labels):

    detector = dlib.get_frontal_face_detector()
    predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat')
    win = dlib.image_window()

    landmarks = []
    l = labels.astype(np.int64)
    one_hot_labels = np.zeros((l.size, int(l.max() + 1)))
    one_hot_labels[np.arange(l.size), l] = 1

    count = 0

    for image, label in zip(images, labels):

        save_img("temp.png", image)
        img = dlib.load_grayscale_image("temp.png")

        dets = detector(img, 1)
        face_rect = dets[0]

        landmarks.append(
            np.matrix([[p.x, p.y] for p in predictor(img, face_rect).parts()]))

    print(len(one_hot_labels))
    print(len(landmarks))

    np.save("landmarks.npy", landmarks)
    np.save("one_hot_labels.npy", one_hot_labels)
コード例 #7
0
def extract_image(mask_path):
    mask_name = os.path.split(mask_path)[1]
    mask_name = mask_name.split(".")[0] + ".png"
    img_name = mask_name.split(".")[0] + ".jpg"
    img_path = os.path.join(images_folder, img_name)
    mask = image.load_img(mask_path)
    img = image.load_img(img_path)
    mask_np = image.img_to_array(mask)
    image_np = image.img_to_array(img)
    if mask_np.shape[2] == 4:
        mask_np = mask_np[:, :, :3]
    if image_np.shape[2] == 4:
        image_np = image_np[:, :, :3]
    w, h, d = image_np.shape

    new_mk = np.empty([w, h, d])
    mask_np[mask_np < 128] = 0
    mask_np[mask_np >= 128] = 255

    for i in range(0, w):
        for j in range(0, h):
            if (np.all((mask_np[i][j] == 0))):
                if (np.mean(image_np[i][j]) > 230):  #background
                    new_mk[i][j] = [0, 0, 0]
                else:
                    new_mk[i][j] = [255, 0, 0]  #stroma
            else:
                new_mk[i][j] = [255, 255, 255]  #tumor
    print(mask_name)
    image.save_img(os.path.join(outfolder, mask_name), new_mk)
コード例 #8
0
def decode(decoder, codebook, input_file, code_sampler=None, origRows=1008, origCols=3456, save='', verbose=False):
    codes = np.load(input_file)['arr_0']
    num_codes, blocks = len(codes), None
    code_dimensions = codebook[0].shape[0]

    for i, code in enumerate(codes):
        if verbose: print(f"Decoding: {i+1}/{num_codes}", end='\r')
        code = tf.convert_to_tensor(code)
        if code_sampler is not None:
            code = code_sampler(code)
        else:
            code = np.reshape(codebook[np.reshape(code, (code.shape[0]*code.shape[1]))], (1, code.shape[0], code.shape[1], code_dimensions)) #extract indices from codebook
        block = decoder.predict(code)
        block = np.array(block)
        if blocks is None:
            blocks = block
        else:
            blocks = np.append(blocks, block, axis=0)
    print(blocks.shape)

    images = stitch_nblocks_1d(blocks, origRows, origCols)
    if save:
        img_num = 1
        for image in images:
            if verbose: print(f"Saving decoded image {img_num}", end='\r')
            img_name = save + 'decoded_{}.jpeg'.format(img_num)
            image = np.reshape(image, (image.shape[0], image.shape[1], 1))
            save_img(img_name, image, data_format='channels_last')
            img_num += 1

    print('\nDecoded.')
    return images
コード例 #9
0
def image_resize(image_list, original_data_path, resize_data_path):
    for image_name in image_list:
        image_data = image.load_img(os.path.join(original_data_path, image_name))
        image_data = image.img_to_array(image_data)
        image_data = tf.image.resize(image_data, [128, 128])  

        image.save_img(os.path.join(resize_data_path, image_name), image_data, data_format = 'channels_last')
コード例 #10
0
ファイル: mylibrary.py プロジェクト: nvtu/dualpath
def generate_augmentation_images(image_path,
                                 number_of_images=5,
                                 resize=None,
                                 save='./',
                                 prefix_name='image'):
    # input is a path to the image
    # resize is an integer value
    '''
  datagen = ImageDataGenerator(rotation_range=15, 
                               horizontal_flip=True, 
                               width_shift_range=0.05, 
                               height_shift_range=0.05,
                               brightness_range=[0.95,1.02])
  '''
    datagen = ImageDataGenerator(horizontal_flip=True)
    img = load_img(image_path)
    data = img_to_array(img)
    if resize != None:
        data = cv2.resize(data,
                          dsize=(resize, resize),
                          interpolation=cv2.INTER_CUBIC)
    save_img(save + prefix_name + '_original.jpg', data)
    samples = np.expand_dims(data, 0)
    it = datagen.flow(samples, batch_size=1)
    for i in range(number_of_images):
        batch = it.next()
        #image = batch[0].astype('uint8')
        aug_img = array_to_img(batch[0])
        save_img(save + prefix_name + '_' + str(i) + '.jpg', aug_img)
コード例 #11
0
def main(user_img_path):
    os.chdir('./flask_deep/darkflow_yolo')
    print('-------------------------------------')
    print(user_img_path)
    user_img_name = user_img_path.split('/')[-1].split('.')[0]
    print('-------------------------------------')

    # define the model options and run
    options = {
        'model': './cfg/yolo.cfg',
        'load': './bin/yolo.weights',
        'threshold': 0.3,
        'gpu': 0.6
    }

    tfnet = TFNet(options)

    # read the color image and covert to RGB
    img = cv2.imread(user_img_path, cv2.IMREAD_COLOR)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

    # use YOLO to predict the image
    result = tfnet.return_predict(img)
    detected_img = boxing(img, result)

    # save
    detected_img_path = '../static/images/' + str(
        user_img_name) + '_detected.png'
    save_img(detected_img_path, detected_img)
    # plt.imshow(detected_img)
    # plt.show()

    return detected_img_path
コード例 #12
0
    def _draw_filters(filters, n=None):
        """Draw the best filters in a nxn grid.
		# Arguments
			filters: A List of generated images and their corresponding lossesfor each processed filter.
			n: dimension of the grid.
			   If none, the largest possible square will be used
		"""
        if n is None:
            n = int(np.floor(np.sqrt(len(filters))))

        # The filters that have the highest loss are assumed to be better-looking.
        # We will only keep the top n*n filters.
        filters.sort(key=lambda x: x[1], reverse=True)
        filters = filters[:n * n]

        # Build a black picture with enough space for
        # e.g. our 8 x 8 filters of size 412 x 412, with a 5px margin in between.
        MARGIN = 5
        width = n * output_dim[0] + (n - 1) * MARGIN
        height = n * output_dim[1] + (n - 1) * MARGIN
        stitched_filters = np.zeros((width, height, 3), dtype='uint8')

        # Fill the picture with our saved filters.
        for i in range(n):
            for j in range(n):
                img, _ = filters[i * n + j]
                width_margin = (output_dim[0] + MARGIN) * i
                height_margin = (output_dim[1] + MARGIN) * j
                stitched_filters[width_margin:width_margin + output_dim[0],
                                 height_margin:height_margin +
                                 output_dim[1], :] = img

        # Save the result to disk.
        save_img('vgg_{0:}_{1:}x{1:}.png'.format(layer_name, n),
                 stitched_filters)
コード例 #13
0
 def load_and_process_image(file_path):
     img = image.load_img(file_path)
     img = image.img_to_array(img)
     if not processed:
         img = crop_center(img, target_size)
         save_path = os.path.join(processed_dir, os.path.basename(file_path))
         image.save_img(save_path, img)
     return img
コード例 #14
0
def resize_images_from(input_path, image_size):
    for class_dir in tqdm(os.listdir(input_path)):
        path = os.path.join(input_path, class_dir)
        files = os.listdir(path)
        for file in files:
            img = load_img(pathlib.Path(path, file))
            img = np.array(img)
            img = resize(img, image_size).numpy()
            save_img(pathlib.Path(path, file), img)
コード例 #15
0
def saveResult(save_path, npyfile, flag_multi_class=False, num_class=2):
    for i, item in enumerate(npyfile):
        # img = labelVisualize(num_class, COLOR_DICT, item) if flag_multi_class else item[:, :, 0]
        img = labelVisualize(num_class, COLOR_DICT, item) if flag_multi_class else item
        # print("img shape:")
        # print(img.shape)
        # print("img: ")
        # print(img)
        save_img(os.path.join(save_path, "%d_predict.jpg" % i), img)
コード例 #16
0
 def generator_image(gmodel, input_data, generator_data_path, image_label, epoch):
     predict = gmodel.predict(input_data)
     #predict = Binarization(predict, gate_value)
     #匯出資料
     for output_count in range(predict.shape[0]):
         image_name = "epoch_" + str(epoch) + ' image_label_' + image_label + str(output_count) + ".png"
         image.save_img(os.path.join(generator_data_path, image_name),
                        np.concatenate((predict[output_count, :, :, :], input_data.numpy()[output_count, :, :, :]), axis = 1),
                        data_format = 'channels_last')
コード例 #17
0
def process_image(img_path):
    img = image.load_img(img_path, target_size=(224,224))
    image.save_img('load.jpg', img)
    img_array = image.img_to_array(img)
    extended_img_array = np.expand_dims(img_array, axis=0)
    pimg = tf.keras.applications.mobilenet.preprocess_input(
        extended_img_array
    )

    return pimg
コード例 #18
0
def check_post_process(img_path, out_dir):
    img = cv.imread(img_path)
    gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)

    post_img = postprocess_prediction(gray)


    out_img = np.expand_dims(post_img, axis=2)
    img_base_name = os.path.basename(img_path)
    save_img(out_dir + 'check_post_' + img_base_name, out_img)
コード例 #19
0
def extract_image(mask_path):
    mask_name = os.path.split(mask_path)[1]
    mask_name = mask_name.split(".")[0] + ".png"
    img_name = mask_name.split(".")[0] + ".jpg"
    img_path = os.path.join(images_folder, img_name)
    mask = image.load_img(mask_path)
    img = image.load_img(img_path)
    mask_np = image.img_to_array(mask)
    image_np = image.img_to_array(img)
    if mask_np.shape[2] == 4:
        mask_np = mask_np[:, :, :3]
    if image_np.shape[2] == 4:
        image_np = image_np[:, :, :3]

    mask_axis_max = np.max(mask_np, axis=2)
    mask_np[:, :, 0] = mask_axis_max
    mask_np[:, :, 1] = mask_axis_max
    mask_np[:, :, 2] = mask_axis_max
    mask_np[mask_np < 100] = 0  #tissue + white portion
    mask_np[mask_np >= 100] = 2  #glands

    image_axis_min = np.mean(image_np, axis=2)
    image_np[:, :, 0] = image_axis_min
    image_np[:, :, 1] = image_axis_min
    image_np[:, :, 2] = image_axis_min
    image_np[
        image_np >= 230] = 255  #white portion -> 1 tissue (after inversion)
    image_np[image_np < 230] = 0  #tissue -> 0 white portion (after inversion)
    image_np = image_np / 255
    image_np = 1 - image_np

    new_mk = mask_np + image_np

    new_mk[:, :, 0][new_mk[:, :, 0] == 0] = 0  #white background : Blue color
    new_mk[:, :, 1][new_mk[:, :, 1] == 0] = 0  #white background : Blue color
    new_mk[:, :, 2][new_mk[:, :, 2] == 0] = 255  #white background : Blue color

    new_mk[:, :, 0][new_mk[:, :, 0] == 1] = 255  # tissue region : Red color
    new_mk[:, :, 1][new_mk[:, :, 1] == 1] = 0  # tissue region : Red color
    new_mk[:, :, 2][new_mk[:, :, 2] == 1] = 0  # tissue region : Red color

    new_mk[:, :, 0][new_mk[:, :, 0] == 2] = 0  # Glands : Green color
    new_mk[:, :, 1][new_mk[:, :, 1] == 2] = 255  # Glands : Green color
    new_mk[:, :, 2][new_mk[:, :, 2] == 2] = 0  # Glands : Green color

    new_mk[:, :, 0][new_mk[:, :, 0] == 3] = 0  # Glands : Green color
    new_mk[:, :, 1][new_mk[:, :, 1] == 3] = 255  # Glands : Green color
    new_mk[:, :, 2][new_mk[:, :, 2] == 3] = 0  # Glands : Green color

    #new_mk = new_mk/255.0
    #plt.imshow(new_mk)
    #plt.show()
    print(mask_name)

    image.save_img(os.path.join(outfolder, mask_name), new_mk)
コード例 #20
0
 def save_img(self, epoch):
     noise = np.random.normal(0, 1, (
         1,
         self.z_dim,
     ))
     fake_img = self.generator.predict(noise)
     save_img(self.img_save_dir + "face_img" + str(epoch) + ".jpg",
              fake_img[0])
     fake_img = np.clip((0.5 * (fake_img + 1)), 0, 1)
     save_img(self.img_save_dir + "clipped_face_img" + str(epoch) + ".jpg",
              fake_img[0])
コード例 #21
0
def test_single_img(model, img_path, out_path, height, width):
    img = load_image(img_path, height, width)
    pred = model.predict(img)
    pred_out = pred[0, :, :, :]
    # pred_out[pred_out >= 0.05] = 1
    # pred_out[pred_out < 0.05] = 0
    img_name = os.path.basename(img_path)

    # save_img(out_path + 'out_' + img_name, pred_out)
    out_img_path = os.path.join(out_path, img_name)
    save_img(out_img_path, pred_out)
コード例 #22
0
def predict(weight_path, pred_path, threshold=0.2, out_path="./Output"):
    pred_dataset = dataset.CatDataset(pred_path, im_width, im_height)
    X_pred, y_pred = pred_dataset.X, pred_dataset.Y

    model.load_weights(weight_path)
    preds = model.predict(X_pred, verbose=1)
    for i in range(preds.shape[0]):
        pred = preds[i]
        print(np.where(pred > threshold))
        pred[pred > threshold] = 255.0
        pred[pred < threshold] = 0
        save_img(f'{out_path}/{i}.jpg', pred)
コード例 #23
0
def image_smooth(image_list, original_data_path, smooth_data_path):
    for image_name in image_list:
        image_data = image.load_img(os.path.join(original_data_path, image_name))
        image_data = image.img_to_array(image_data)
        image_data = tf.image.resize(image_data, [300, 300]).numpy()  
        smooth_image= []
        for channel in range(np.shape(image_data)[-1]):
            smooth_image.append(ndimage.gaussian_filter(image_data[:,:, channel], 2))
        smooth_image = np.moveaxis(np.asarray(smooth_image), 0, -1)
        output_image = np.concatenate((image_data, smooth_image), axis = 1)

        image.save_img(os.path.join(smooth_data_path, image_name), output_image, data_format = 'channels_last')
コード例 #24
0
ファイル: util.py プロジェクト: hkhajanchi/psig-gan
def save_image_batch(image_tensor, write_dir):
    '''
    Saves generated TensorFlow images to .png files using OpenCV

    @param image_tensor: <<tf Tensor>> must be 4D for [num_images, img_wid, img_hgt, num_color_channels]
    @param write_dir : <<string>> directory to save images to 

    '''

    for i in range(image_tensor.shape[0]):
        write_path = write_dir + "/generated_{}.png".format(i)
        save_img(write_path, image_tensor[i, :, :, :])
コード例 #25
0
def test_batch_imgs(model, batch_imgs_names, imgs_generator, out_path, height, width, gpus_num=1):
    # pred = model.predict(imgs_generator, batch_size=2 * gpus_num)
    pred = model.predict(imgs_generator, steps=4)
    for i in range(2*gpus_num):
        pred_out = pred[i, :, :, :]
        # pred_out[pred_out >= 0.05] = 1
        # pred_out[pred_out < 0.05] = 0
        img_name = os.path.basename(batch_imgs_names[i])

        # save_img(out_path + 'out_' + img_name, pred_out)
        out_img_path = os.path.join(out_path, img_name)
        save_img(out_img_path, pred_out)
コード例 #26
0
 def load_and_process_image(file_path, index, target_size=(64, 64)):
     img = image.load_img(file_path)
     img = image.img_to_array(img)
     if resize:
         img = crop_center(img, target_size)
         save_path = os.path.join(os.path.dirname(file_path),
                                  'preprocessed',
                                  os.path.basename(file_path))
         image.save_img(save_path, img)
     if index % 100 == 0:
         print("Preprocessing %3.2f percent completed" %
               (index / NUM_DATA * 100))
     return img
コード例 #27
0
    def save_transition_predictions(self, over_only=False):
        """

        saves images located at the predicted ends of games

        :param over_only: whether or not to only save over images
        :return: None
        """

        if self.transition_predictions is None:
            self.get_transition_predictions()

        t0 = t.time()

        game_transitions = self.get_game_transitions()

        # number of frames that each transition yields
        frames_per_transition = int(2 * self.step * constants.frames_per_vod /
                                    self.end_transition_step)

        # go through the game transitions
        for game_transition_index, (
                vod_kind, vod_start_index) in enumerate(game_transitions):

            # go through all the vods within the next step
            for i in range(2 * self.step):

                # go through all the frames in each vod
                for frame in range(0, constants.frames_per_vod,
                                   self.end_transition_step):

                    vod_index = vod_start_index + i

                    frame_offset = int((i * constants.frames_per_vod + frame) /
                                       self.end_transition_step)

                    index = game_transition_index * frames_per_transition + frame_offset

                    if not over_only or self.transition_predictions[index] == 4:

                        image = self.transition_tensor[index]
                        name = constants.label_ids[self.transition_predictions[index]] + "-" + \
                               self.video_id + "-" + \
                               str(vod_index) + "-" + str(frame) + ".jpg"

                        save_img(os.path.join(temp_images, name), image)

        t1 = t.time()

        if self.verbose:
            print("saving", index, "images took", t1 - t0, "seconds")
コード例 #28
0
def ejer4(filename, n_epochs):
    img_init = init_image()
    img_init = output_image(img_init[0].numpy())
    save_img("{}_init_img_epochs_{}.pdf".format(filename, n_epochs), img_init)

    all_imgs = []
    n, m = 1, 2  # array para el plot

    for layer_filter in range(n * m):
        layer_filter = layer_filter + 2
        loss, img = fit_grad_asc(layer_filter, n_epochs)
        all_imgs.append(img)

    plot_filters(all_imgs, n, m, filename, n_epochs)
コード例 #29
0
def styleTransfer(sourcepath, stylepath):
    path = 'static/uploads'
    base_image_path = os.path.join(path, "source.png")
    style_reference_image_path = os.path.join(path, "style.png")
    result_prefix = "static/results/result"
    global content_weight
    global total_variation_weight
    global style_weight
    total_variation_weight = 1e-6
    style_weight = 3e-6
    content_weight = 5e-7
    width, height = image.load_img(base_image_path).size
    global img_nrows
    global img_ncols
    img_nrows = 400
    img_ncols = int(width * img_nrows / height)
    content_img = preprocess_image(base_image_path)
    shape = content_img.shape[1:]
    model = VGG19(weights="imagenet", include_top=False, input_shape=shape)
    outputs_dict = dict([(layer.name, layer.output) for layer in model.layers])
    global feature_extractor
    feature_extractor = Model(inputs=model.inputs, outputs=outputs_dict)
    global style_layer_names
    style_layer_names = [
        "block1_conv1",
        "block2_conv1",
        "block3_conv1",
        "block4_conv1",
        "block5_conv1",
    ]
    global content_layer_name
    content_layer_name = "block5_conv2"
    optimizer = SGD(
        schedules.ExponentialDecay(initial_learning_rate=100.0,
                                   decay_steps=100,
                                   decay_rate=0.96))
    base_image = preprocess_image(base_image_path)
    style_reference_image = preprocess_image(style_reference_image_path)
    combination_image = tf.Variable(preprocess_image(base_image_path))
    iterations = 100
    for i in range(1, iterations + 1):
        loss, grads = compute_loss_and_grads(combination_image, base_image,
                                             style_reference_image)
        optimizer.apply_gradients([(grads, combination_image)])
        print("Iteration %d: loss=%.2f" % (i, (loss)))
    print(combination_image)
    img = deprocess_image(combination_image.numpy())
    fname = result_prefix + ".png"
    image.save_img(fname, img)
def aug_data_sess1(
    orig_path, k, SAVE_PATH
):  # use k images from testing dataset as gallery and train the model into a classfication model for this ten IDs
    subfolders = [f.path for f in os.scandir(orig_path) if f.is_dir()]
    Filelist = []
    for dirs in subfolders:
        filename = random.choices(os.listdir(dirs),
                                  k=1)  # change dir name to whatever
        print(filename)
        for file in filename:
            Filelist.append(os.path.join(dirs, file))
    selected_Filelist = Filelist
    num_imgs = len(selected_Filelist)
    print('total number of images:', num_imgs)

    num_aug_per_img = 20
    train_datagen = ImageDataGenerator(brightness_range=[0.5, 1.5],
                                       rotation_range=5,
                                       width_shift_range=0.01,
                                       height_shift_range=0.00,
                                       shear_range=0.2,
                                       zoom_range=0.3,
                                       channel_shift_range=10,
                                       horizontal_flip=True,
                                       fill_mode='nearest')

    for file in tqdm.tqdm(selected_Filelist):
        img = load_img(file)

        x = img_to_array(img)
        x = x.reshape((1, ) + x.shape)
        i = 0
        path = os.path.normpath(file)
        parts = path.split(os.sep)
        # print('processing:' + parts[-1])
        check_folder(SAVE_PATH + '/' + parts[-2])
        save_img(SAVE_PATH + '/' + parts[-2] + '/' + parts[-1], img)
        for batch in train_datagen.flow(x,
                                        batch_size=1,
                                        save_to_dir=SAVE_PATH + '/' +
                                        parts[-2],
                                        save_prefix=parts[-2],
                                        save_format='png'):
            i += 1
            if i > num_aug_per_img:
                break