Exemplo n.º 1
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)
Exemplo n.º 2
0
def calculate_loss_of_contrast(images_hdr_path, images_ldr_path,
                               norm_value_hdr, norm_value_ldr):

    if len(images_hdr_path) != len(images_ldr_path):
        raise ValueError('Sizes of HDR and LDR image lists are different.')

    if len(images_hdr_path) == 0:
        raise ValueError('List of HDR or LDR image paths must not be empty.')

    loss_global_contrast = 0
    loss_local_contrast = 0
    for hdr_image_path, ldr_image_path in zip(images_hdr_path,
                                              images_ldr_path):
        print(".", end='', flush=True)  # show progress
        image_hdr = preprocess_image(
            cv2.imread(str(hdr_image_path),
                       cv2.IMREAD_GRAYSCALE | cv2.IMREAD_ANYDEPTH),
            norm_value_hdr)
        image_ldr = preprocess_image(
            cv2.imread(str(ldr_image_path), cv2.IMREAD_GRAYSCALE),
            norm_value_ldr, gamma_correction)
        loss_global_contrast += measure_global_contrast(image_hdr, image_ldr)
        loss_local_contrast += measure_local_contrast(image_hdr, image_ldr)

    loss_global_contrast /= len(images_hdr_path)
    loss_local_contrast /= len(images_hdr_path)

    print()  # newline after progress dots

    return loss_global_contrast, loss_local_contrast
Exemplo n.º 3
0
def play(save_path):
    ''' Loads network from the location of save_path and plays a game of Pong. '''

    # Initialize the Pong gym environment, set seeds
    env = gym.make('Pong-v0')
    replay_memory = u.ReplayMemory()
    G = tf.Graph()
    with G.as_default():
        # Import TF graph
        saver = tf.train.import_meta_graph(save_path + '.meta',
                                           clear_devices=True)
        G.device(
            '/cpu:0'
        )  # Run graph on CPU so play can be done without taking GPU resources
        # Get input/output tensors
        X = G.get_tensor_by_name('X:0')
        Y = G.get_tensor_by_name('Y:0')
        # Initialize TF session
        sess_config = tf.ConfigProto(device_count={'CPU': 1, 'GPU': 0})
        with tf.Session(config=sess_config) as sess:
            print('Reloading parameters...')
            saver.restore(sess, save_path)
            # Iterate over episodes
            while True:
                obs = u.preprocess_image(env.reset())
                for i in range(3):
                    replay_memory.add_frame(
                        np.zeros((160 // DOWNSAMPLE, 160 // DOWNSAMPLE)))
                replay_memory.add_frame(obs)

                # Iterate over frames
                done = False
                while not done:
                    # Feed state into DQN
                    s = np.stack(
                        [replay_memory.frames[i] for i in range(-4, 0)],
                        axis=-1).reshape(1, 160 // DOWNSAMPLE,
                                         160 // DOWNSAMPLE, 4)
                    y = sess.run(Y, feed_dict={X: s})

                    # Decide on action greedily
                    a = np.argmax(y) + 1

                    # Take action, observe environment, reward
                    obs, r, done, _ = env.step(a)
                    for i in range(STEPS_TO_SKIP):
                        obs, r, done_temp, _ = env.step(1)
                        if done_temp == True:
                            done = True
                    env.render()

                    # Add new frame to replay memory
                    replay_memory.add_frame(u.preprocess_image(obs))

                q = input('Play again? ')
                if q in ['', 'y', 'Y']:
                    pass
                else:
                    env.render(close=True)
                    break
Exemplo n.º 4
0
 def get_trajectory(self, env, episode_max_length, render=False):
     """
     Run agent-environment loop for one whole episode (trajectory)
     Return dictionary of results
     Note that this function returns more than the get_trajectory in the Learner class.
     """
     state = preprocess_image(env.reset())
     prev_state = state
     states = []
     actions = []
     rewards = []
     episode_probabilities = []
     for _ in range(episode_max_length):
         delta = state - prev_state
         action, probabilities = self.act(delta)
         states.append(delta)
         prev_state = state
         state, rew, done, _ = env.step(action)
         state = preprocess_image(state)
         actions.append(action)
         rewards.append(rew)
         episode_probabilities.append(probabilities)
         if done:
             break
         if render:
             env.render()
     return {
         "reward": np.array(rewards),
         "state": np.array(states),
         "action": np.array(actions),
         "prob": np.array(episode_probabilities)
     }
Exemplo n.º 5
0
def load_data(labels,
              img_path,
              size=299,
              data=None,
              oversample=False,
              multi_label=True):

    if oversample:
        new_df = pd.DataFrame()
        major = labels[labels.iloc[:, 1] == np.argmax(
            np.bincount(labels.iloc[:, 1]))]
        minors = np.unique(labels.iloc[:, 1])[np.unique(
            labels.iloc[:, 1]) != np.argmax(np.bincount(labels.iloc[:, 1]))]
        for i in minors:
            minor = labels[labels.iloc[:, 1] == i]
            n = len(major) - len(minor)
            minor_df = resample(minor,
                                replace=True,
                                n_samples=n + len(minor),
                                random_state=42)
            new_df = new_df.append(minor_df)
        new_df = new_df.reset_index(drop=True)
        labels = pd.concat([major, new_df], ignore_index=True)
        labels = labels.reset_index(drop=True)
    else:
        pass
    labels = labels.reset_index(drop=True)
    N = labels.shape[0]
    # add a size to argument
    x_train = np.empty((N, size, size, 3), dtype=np.uint8)
    y_train = pd.get_dummies(labels.iloc[:, 1]).values

    if multi_label:
        y_train_multi = np.empty(y_train.shape, dtype=y_train.dtype)
        y_train_multi[:, 4] = y_train[:, 4]

        for i in range(3, -1, -1):
            y_train_multi[:, i] = np.logical_or(y_train[:, i],
                                                y_train_multi[:, i + 1])

        y_train = y_train_multi

    for i, image_id in enumerate(tqdm(labels.iloc[:, 0])):
        if data == 'aptos':
            x_train[i, :, :, :] = preprocess_image(
                img_path + 'Aptos/train_images/{}.png'.format(image_id), size)
        else:
            try:
                x_train[i, :, :, :] = preprocess_image(
                    img_path + 'train_resized/{}.jpeg'.format(image_id), size)
            except FileNotFoundError:
                continue

    return x_train, y_train
Exemplo n.º 6
0
    def get_tensor_pair(self, patch):
        patch_size = self.hr_patch_size
        lr_patch_size = patch_size / 2

        lr_patch = resize(patch, [lr_patch_size, lr_patch_size, 3],
                          mode='reflect')

        hr_patch = utils.preprocess_image(patch)
        lr_patch = utils.preprocess_image(lr_patch)

        return hr_patch, lr_patch
def generator(samples, batch_size=32):
    num_samples = len(samples)
    while 1:  # Loop forever so the generator never terminates
        sklearn.utils.shuffle(samples)
        for offset in range(0, num_samples, batch_size):
            batch_samples = samples[offset:offset + batch_size]

            images = []
            angles = []
            for batch_sample in batch_samples:
                center_name = FLAGS.data_dir + '/IMG/' + batch_sample[0].split(
                    '/')[-1]
                center_image = preprocess_image(cv2.imread(center_name))
                center_angle = float(batch_sample[3])
                images.append(center_image)
                angles.append(center_angle)
                # data augmentation
                augmented_c_image, augmented_c_angle = augment_data(
                    center_image, center_angle)
                images.append(augmented_c_image)
                angles.append(augmented_c_angle)

                # add in left and right cameras' info
                left_name = FLAGS.data_dir + '/IMG/' + batch_sample[1].split(
                    '/')[-1]
                left_image = preprocess_image(cv2.imread(left_name))
                right_name = FLAGS.data_dir + '/IMG/' + batch_sample[2].split(
                    '/')[-1]
                right_image = preprocess_image(cv2.imread(right_name))
                # create adjusted steering measurements for the side camera images
                correction = 0.3  # this is a parameter to tune
                left_angle = center_angle + correction
                right_angle = center_angle - correction
                # add images and angles to data set
                images.extend([left_image, right_image])
                angles.extend([left_angle, right_angle])

                # data augmentation
                augmented_l_image, augmented_l_angle = augment_data(
                    left_image, left_angle)
                augmented_r_image, augmented_r_angle = augment_data(
                    right_image, right_angle)
                images.extend([augmented_l_image, augmented_r_image])
                angles.extend([augmented_l_angle, augmented_r_angle])

            # trim image to only see section with road
            X = np.array(images)
            y = np.array(angles)

            X, y = sklearn.utils.shuffle(X, y)

            yield X, y
Exemplo n.º 8
0
def play(G, save_path):

    # Initialize the Pong gym environment, set seeds
    env = gym.make('Pong-v0')
    replay_memory = u.ReplayMemory()
    with G.as_default():
        # Get input/output tensors
        X = G.get_tensor_by_name('X:0')
        Y = G.get_tensor_by_name('Y:0')
        saver = tf.train.Saver(var_list=None, max_to_keep=5)
        # Initialize TF session
        with tf.Session() as sess:
            print('Reloading parameters...')
            saver.restore(sess, save_path)
            # Iterate over episodes
            while True:
                obs = u.preprocess_image(env.reset())
                for i in range(3):
                    replay_memory.add_frame(
                        np.zeros((160 // DOWNSAMPLE, 160 // DOWNSAMPLE)))
                replay_memory.add_frame(obs)

                # Iterate over frames
                done = False
                while not done:
                    # Feed state into DQN
                    s = np.stack(
                        [replay_memory.frames[i] for i in range(-4, 0)],
                        axis=-1).reshape(1, 160 // DOWNSAMPLE,
                                         160 // DOWNSAMPLE, 4)
                    y = sess.run(Y, feed_dict={X: s})

                    # Decide on action
                    a = np.argmax(y) + 1

                    # Take action, observe environment, reward
                    obs, r, done, _ = env.step(a)
                    for i in range(STEPS_TO_SKIP):
                        obs, r, done_temp, _ = env.step(1)
                        if done_temp == True:
                            done = True
                    env.render()

                    # Add new state/reward to replay memory
                    replay_memory.add_frame(u.preprocess_image(obs))

                q = input('play again?')
                if q in ['', 'y', 'Y']:
                    pass
                else:
                    env.render(close=True)
                    break
Exemplo n.º 9
0
    def generate(self, original_image, im_label, target_class):
        im_label_as_var = torch.from_numpy(np.asarray([target_class]))
        criterion = nn.CrossEntropyLoss()
        # process image as ImageNet dataset format
        processed_image = preprocess_image(original_image)

        for i in range(10):
            print('Iteration: {}'.format(str(i)))
            # zero previous gradient
            processed_image.grad = None

            out = self.model(processed_image)
            pred_loss = criterion(out, im_label_as_var)
            # calculate gradient
            pred_loss.backward()

            # create noise
            # processed_image.grad.data represents gradient of first layer
            adv_noise = self.alpha * torch.sign(processed_image.grad.data)
            # add noise to image
            processed_image.data = processed_image.data - adv_noise

            # generate confirmation image
            recreated_image = recreate_image(processed_image)
            # process confirmation image
            prep_confirmation_image = preprocess_image(recreated_image)

            pred = self.model(prep_confirmation_image)
            # get prediction index
            _, pred_index = pred.data.max(1)
            confidence = F.softmax(pred, dim=1)[0][pred_index].data.numpy()[0]
            # convert tensor to int
            pred_index = pred_index.numpy()[0]

            if pred_index == target_class:
                print('\nOriginal image class: ', im_label,
                      '\nTarget image class: ', target_class, '\nConfidence: ',
                      confidence)

                # create the image for noise as: Original image - generated image
                noise_image = original_image - recreated_image
                cv2.imwrite(
                    './generated/targeted_adv_noise_from_' + str(im_label) +
                    '_to_' + str(pred_index) + '.jpg', noise_image)
                # write image
                cv2.imwrite(
                    './generated/targeted_adv_img_from_' + str(im_label) +
                    '_to_' + str(pred_index) + '.jpg', recreated_image)
                break

        return 1
Exemplo n.º 10
0
def compute_features(keyframes, path_out, layer='conv5_1', max_dim=340):
    """
    store all local features from conv5_1 of vgg16 at 340 mac res and
    store then in path_data/[mode]/layer/max_dim
    """

    # create folder if it does not exist
    if not os.path.exists(path_out):
        os.makedirs(path_out)

        # init model
        model = init_model(layer)
        # message
        desc_text = "Feature extraction --> Layer: {}, Max_dim: {}, total_images={}".format(
            layer, max_dim, keyframes.shape[0])
        # process keyframes
        for k, keyframe in tqdm(enumerate(keyframes),
                                ascii=True,
                                desc=desc_text):
            feats = model.predict(preprocess_image(
                keyframe, max_dim=max_dim)).squeeze(axis=0)
            np.save(os.path.join(path_out, "{}".format(k)), feats)

    # resume computation
    else:
        computed = np.array(
            [int(k.split('.')[0]) for k in os.listdir(path_out)])
        # if features has been computed...
        if computed.shape[0] == keyframes.shape[0]:
            return path_out
        # start from the last computed...
        elif computed.shape[0] == 0:
            last = 0
        else:
            last = np.sort(computed)[::-1][0]

        # init model
        model = init_model(layer)
        desc_text = "Feature extraction --> Layer: {}, Max_dim: {}, total_images={}".format(
            layer, max_dim, keyframes.shape[0] - last)
        for k, keyframe in tqdm(enumerate(keyframes[last:]),
                                ascii=True,
                                desc=desc_text):
            feats = model.predict(preprocess_image(
                keyframe, max_dim=max_dim)).squeeze(axis=0)
            k += last
            np.save(os.path.join(path_out, "{}".format(k)), feats)

    return path_out
Exemplo n.º 11
0
def predict():
    data = {'success': False}
    if request.files.getlist("images[]"):
        images = request.files.getlist("images[]")
        images_preprocess = []
        items = []
        for image in images:
            item = {}
            image = image.read()
            image = Image.open(io.BytesIO(image))
            image = utils.preprocess_image(image)

            images_preprocess.append(image / 255.)
            item['image'] = utils.encode_image(image)
            items.append(item)

        images_preprocess = np.array(images_preprocess)
        predicts = model.predict(images_preprocess, steps=len(images))
        predicts = np.argmax(predicts, axis=1)
        for idx, predict in enumerate(predicts):
            items[idx]['info'] = class_info[str(predict)]

        data['success'] = True
        data['data'] = items
    print(data)
    return json.dumps(data, ensure_ascii=False, cls=utils.NumpyEncoder)
Exemplo n.º 12
0
    def predict_image(self, image, voc, img_size=IMAGE_SIZE):

        if isinstance(image, str):
            img = preprocess_image(image, img_size)
        elif isinstance(image, np.ndarray):
            img = image
        else:
            raise TypeError(
                "Unknown handling of image input of type {}".format(
                    type(image)))

        img_inp = np.expand_dims(img, 0)

        current_context = np.array([voc.word2one_hot_dict[PLACEHOLDER]] *
                                   (self.max_code_length))
        current_context = np.expand_dims(current_context, 0)
        current_context[0, 0, :] = voc.word2one_hot_dict[START_WORD]

        word_predictions = []
        for i in range(self.max_code_length):
            probas = self.predict({
                'img_data': img_inp,
                'context': current_context
            })
            probas = probas['code'][0][i]
            prediction = np.argmax(probas)
            word_prediction = voc.token2word_dict[prediction]
            if word_prediction == END_WORD or word_prediction == PLACEHOLDER:
                break
            word_predictions.append(word_prediction)
            current_context[0,
                            i + 1, :] = voc.word2one_hot_dict[word_prediction]

        return " ".join(word_predictions)
Exemplo n.º 13
0
def telemetry(sid, data):
    if data:
        # The current steering angle of the car
        steering_angle = data["steering_angle"]
        # The current throttle of the car
        throttle = data["throttle"]
        # The current speed of the car
        speed = data["speed"]
        # The current image from the center camera of the car
        imgString = data["image"]
        image = Image.open(BytesIO(base64.b64decode(imgString)))
        image = utils.preprocess_image(image)
        image_array = np.asarray(image)
        steering_angle = float(
            model.predict(image_array[None, :, :, :], batch_size=1))
        controller.set_desired(set_speed)

        throttle = controller.update(float(speed))

        print(steering_angle, throttle)
        send_control(steering_angle, throttle)

        # save frame
        if args.image_folder != '':
            timestamp = datetime.utcnow().strftime('%Y_%m_%d_%H_%M_%S_%f')[:-3]
            image_filename = os.path.join(args.image_folder, timestamp)
            image.save('{}.jpg'.format(image_filename))
    else:
        # NOTE: DON'T EDIT THIS.
        sio.emit('manual', data={}, skip_sid=True)
Exemplo n.º 14
0
 def slide(self, image):
     """
     Extracts sliding windows over the preprocessed image
     @param image: A 2d ndarray representing an image
     @return: A length two tuple of a list of dictionaries, each corresponding to a window, and a list of images,
     each corresponding to a down sampled image. The down sampled images have been preprocessed by
     utils.preprocess_image
     """
     # Each 'patch' is a list of contributions to bins, like the output from utils.preprocess_image
     patches_dicts = []
     images = []
     for image_index, (scale, patch_extractor) in enumerate(izip(self.scales, self.patch_extractors)):
         if scale != 1:
             downsampled_image = ndimage.interpolation.zoom(image, scale)
         else:
             downsampled_image = image
         preprocessed_image = utils.preprocess_image(downsampled_image)
         images.append(preprocessed_image)
         positions = patch_extractor.patch_positions(preprocessed_image)
         rf_size = [patch_extractor.rf_size[0] / scale, patch_extractor.rf_size[1] / scale]
         #patches = patch_extractor.extract_all(preprocessed_image)
         #for i, patch in enumerate(patches):
         for i in xrange(positions.shape[1]):
             patch_position = positions[:, i].tolist()
             orig_position = (positions[:, i] / scale).tolist()
             patches_dicts.append({
                 'image_index': image_index,
                 'patch_position': patch_position,
                 'patch_size': patch_extractor.rf_size,
                 'orig_position': orig_position,
                 'orig_size': rf_size,
             })
     return patches_dicts, images
Exemplo n.º 15
0
def run_images():
    parser = argparse.ArgumentParser()
    parser.add_argument('--dir_path', default="./test_images", type=str)
    parser.add_argument('--out_path', default='./out_images', type=str)
    parser.add_argument('--model_file',
                        default="./model/yolov2-tiny-voc.h5",
                        type=str)
    args = parser.parse_args()

    paths = utils.get_image_path(args.dir_path)
    images = []
    print('reading image from %s' % args.dir_path)
    for path in paths:
        image = cv2.imread(path)
        resized = cv2.resize(image, (416, 416))
        images.append(resized)

    image_processed = []
    for image in images:
        image_processed.append(utils.preprocess_image(image))

    print('loading model from %s' % args.model_file)
    model = load_model(args.model_file)
    predictions = model.predict(np.array(image_processed))

    if not os.path.exists(args.out_path):
        os.mkdir(args.out_path)
    print('writing image to %s' % args.out_path)
    for i in range(predictions.shape[0]):
        boxes = utils.process_predictions(predictions[i],
                                          probs_threshold=0.3,
                                          iou_threshold=0.2)
        out_image = utils.draw_boxes(images[i], boxes)
        cv2.imwrite('%s/out%s.jpg' % (args.out_path, i), out_image)
Exemplo n.º 16
0
def main():
    parser = argparse.ArgumentParser(description="Deep Dream tutorial")
    parser.add_argument("--src_img", default="sky.jpg", required=True, type=str, help="Source image to perform deep dram on")
    parser.add_argument("--directory", default="../dream_dir", type=str, help="Result directory to save intermediate images")
    parser.add_argument("--iterations", default="1000", type=int, help="How long to dream.")
    parser.add_argument("--save_every", default="1", type=int, help="Saving image after every _ iterations")
    parser.add_argument("--downscale_factor", default="3", type=int, help="Downscale factor for reducing image scale")
    parser.add_argument("--overwrite_save_dir", default=False, type=bool, help="Delete all files in selected directory")

    args = parser.parse_args()

    proc = preprocess_image(args.src_img)
    print(proc.shape)

    model = get_feature_extractor(layer_settings)
    print("model loaded\nDreaming")

    if not os.path.isdir(args.directory):
        try:
            os.mkdir(args.directory)
            print(f"created directory \"{args.directory}\"")
        except:
            print("couldn't create directory")

    if  len(os.listdir(args.directory))>0 and args.overwrite_save_dir==True:
        for f in os.listdir(args.directory):
            os.remove(os.path.join(args.directory, f))
        print("Directory cleaned")

    dream_on(proc, model, args.directory, iterations=args.iterations, save_every=args.save_every, downscale_factor=args.downscale_factor)
Exemplo n.º 17
0
def test_all_images():
    parser = argparse.ArgumentParser()
    arg = parser.add_argument

    # model-related variables
    arg('--model-path', type=str, help='Model path')
    arg('--dataset', type=str, help='roof: roof segmentation / income: income determination')

    # image-related variables
    arg('--masks-dir', type=str, default='./data/dataset/labels', help='numPy masks directory')
    arg('--npy-dir', type=str, default='./data/dataset/split_npy', help='numPy preprocessed patches directory')

    args = parser.parse_args()

    roof_path = "./data/dataset/split_npy"

    if args.dataset == "roof":
        model = load_model(args.model_path, UNet11)
    else:
        model = load_model(args.model_path, UNet11, input_channels=5, num_classes=4)
        # roof_model = load_model("./trained_models/model_10_percent_roof_Unet11_200epochs.pth", UNet11)

    if not os.path.exists("./data/test_all"):
        os.mkdir("./data/test_all")

    # Select sample pictures
    images_filenames = np.array(sorted(glob.glob(args.npy_dir + "/*.npy")))

    for filename in tqdm(images_filenames):

        fig = plt.figure(figsize=(10, 10))

        image = pickle.load(open(filename, "rb"))
        image = preprocess_image(image, args.dataset)

        pred = run_model(image, model, args.dataset)
        # if args.dataset == "income":
            # roof_image = pickle.load(open(os.path.join(roof_path, filename[filename.rfind("/") + 1:]), "rb"))
            # roof_image = preprocess_image(roof_image, "roof")
            # pred_roof = run_model(roof_image, roof_model, "roof")
            # pred[0][0] = pred[0][0] * pred_roof[0][0]
            # pred[0][1] = pred[0][1] * pred_roof[0][0]

        mask_path = os.path.join(args.masks_dir, args.dataset, filename[filename.rfind("/") + 1:])
        y = pickle.load(open(mask_path, "rb"))

        fig.add_subplot(1, 3, 1)
        plt.imshow(reverse_transform(image.cpu().numpy()[0], args.dataset))

        fig.add_subplot(1, 3, 2)
        plt.imshow(masks_to_colorimg(y, args.dataset))

        fig.add_subplot(1, 3, 3)
        plt.imshow(pred_to_colorimg(pred.cpu().numpy(), args.dataset))

        plt.savefig(os.path.join("./data/test_all",
                                 filename[filename.rfind("/") + 1:filename.rfind(".")] + ".png"))

        plt.clf()
        plt.close(fig)
Exemplo n.º 18
0
def predict(sess, image_file):
    """
    Runs the graph stored in "sess" to predict boxes for "image_file". Prints and plots the preditions.

    Arguments:
    sess -- your tensorflow/Keras session containing the YOLO graph
    image_file -- name of an image stored in the "images" folder.

    Returns:
    out_scores -- tensor of shape (None, ), scores of the predicted boxes
    out_boxes -- tensor of shape (None, 4), coordinates of the predicted boxes
    out_classes -- tensor of shape (None, ), class index of the predicted boxes

    Note: "None" actually represents the number of predicted boxes, it varies between 0 and max_boxes.
    """
    image, image_data = preprocess_image("images/" + image_file, model_image_size = (608, 608))

    out_scores, out_boxes, out_classes = sess.run(yolo_eval(yolo_outputs, image_shape),
                                                  feed_dict={yolo_model.input:image_data,
                                                             K.learning_phase():0})

    print('Found {} boxes for {}'.format(len(out_boxes), image_file))
    colors = generate_colors(class_names)
    draw_boxes(image, out_scores, out_boxes, out_classes, class_names, colors)
    image.save(os.path.join("out", image_file), quality=90)
    output_image = scipy.misc.imread(os.path.join("out", image_file))
    imshow(output_image)

    return out_scores, out_boxes, out_classes
Exemplo n.º 19
0
def deblur(image_path):
    data = {
        'A_paths': [image_path],
        'A': np.array([preprocess_image(load_image(image_path))])
    }
    x_test = data['A']
    g = generator_model()
    g.load_weights('generator.h5')
    generated_images = g.predict(x=x_test)
    generated = np.array([deprocess_image(img) for img in generated_images])
    #kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]], np.float32) #锐化
    print(generated.shape)
    #ima = Image.fromarray(generated)
    #dst = cv.filter2D(ima, -1, kernel=kernel)
    #dst.save("/content/drive/My Drive/5405_digitalMedia/result/e.png")
    #image_arr = np.array(dst)

    x_test = deprocess_image(x_test)
    '''
    img = generated[0, :, :, :]
    im = Image.fromarray(img.astype(np.uint8))
    im.save("/content/drive/My Drive/5405_digitalMedia/result/f.png")
    src = cv.imread("/content/drive/My Drive/5405_digitalMedia/result/f.png")
    kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]], dtype=np.float32)
    sharpen_image = cv.filter2D(src, cv.CV_32F, kernel=kernel)
    sharpen_image = cv.convertScaleAbs(sharpen_image)
    cv.imwrite("/content/drive/My Drive/5405_digitalMedia/result/g.png",sharpen_image)
    '''

    for i in range(generated_images.shape[0]):
        x = x_test[i, :, :, :]
        img = generated[i, :, :, :]
        output = np.concatenate((x, img), axis=1)
        im = Image.fromarray(output.astype(np.uint8))
        im.save("result.jpg")  #('deblur'+image_path)
Exemplo n.º 20
0
def predict_and_draw(sess, image, scores, boxes, classes, yolo_model):
    """
    Runs the graph stored in "sess" to predict boxes and draw bounding boxes

    Arguments:
    sess -- your tensorflow/Keras session containing the YOLO graph
    image - Original Image
    scores -- tensor of shape (None, ), predicted score for each box
    boxes -- tensor of shape (None, 4), predicted box coordinates
    classes -- tensor of shape (None,), predicted class for each box
    yolo_model - pre-trained YOLO Model


    Returns:
    image -- Image with bounding box drawn

    """

    # Preprocess your image
    image_data = preprocess_image(image, model_image_size=(608, 608))

    # Run the session with the correct tensors and choose the correct placeholders in the feed_dict.
    # You'll need to use feed_dict={yolo_model.input: ... , K.learning_phase(): 0})
    out_scores, out_boxes, out_classes = sess.run([scores, boxes, classes],
                                                  feed_dict={yolo_model.input: image_data, K.learning_phase(): 0})

    # Draw bounding boxes on the image file
    box = Box(image, out_scores, out_boxes, out_classes, class_names)
    box.draw_boxes()

    # return out_scores, out_boxes, out_classes
    return image
Exemplo n.º 21
0
def predict():

    input_size = (416, 416)
    tf.app.flags.DEFINE_string('image_file', './images/person.jpg', 'image_path')
    FLAGS = tf.app.flags.FLAGS
    image_file = FLAGS.image_file
    image = cv2.imread(image_file)
    image_shape = image.shape[:2]
    image_cp = preprocess_image(image, input_size)

    images = tf.placeholder(tf.float32, [1, input_size[0], input_size[1], 3])
    detection_feat = darknet(images)
    feat_sizes = input_size[0] // 32, input_size[1] // 32
    detection_results = decode(detection_feat, feat_sizes, len(class_names), anchors)

    checkpoint_path = "./checkpoint_dir/yolo2_coco.ckpt"
    #checkpoint_path = "/Users/xiang/Downloads/DeepLearning_tutorials-master/ObjectDetections/yolo2/checkpoint_dir/yolo2_coco.ckpt"
    saver = tf.train.Saver()
    with tf.Session() as sess:
        saver.restore(sess, checkpoint_path)
        bboxes, obj_probs, class_probs = sess.run(detection_results, feed_dict={images: image_cp})

    bboxes, scores, class_inds = postprocess(bboxes, obj_probs, class_probs,
                                             image_shape=image_shape)
    img_detection = draw_detection(image, bboxes, scores, class_inds, class_names)
    cv2.imwrite("./res/detection.jpg", img_detection)
    cv2.imshow("detection results", img_detection)
    print('*****Click the window,and press any key to close!*****')

    cv2.waitKey(0)
    cv2.destroyAllWindows()
Exemplo n.º 22
0
def main():
    input_size = (416,416)
    image_file = './yolo2_data/car.jpg'
    image = cv2.imread(image_file)
    image_shape = image.shape[:2] #只取wh,channel=3不取

    # copy、resize416*416、归一化、在第0维增加存放batchsize维度
    image_cp = preprocess_image(image,input_size)

    # 【1】输入图片进入darknet19网络得到特征图,并进行解码得到:xmin xmax表示的边界框、置信度、类别概率
    tf_image = tf.placeholder(tf.float32,[1,input_size[0],input_size[1],3])
    model_output = darknet_slim(tf_image) # darknet19网络输出的特征图
    output_sizes = input_size[0]//32, input_size[1]//32 # 特征图尺寸是图片下采样32倍
    output_decoded = decode(model_output=model_output,output_sizes=output_sizes,
                               num_class=len(class_names),anchors=anchors)  # 解码

    model_path = "models/model.ckpt"
    init_fn = slim.assign_from_checkpoint_fn(model_path,slim.get_variables())
    with tf.Session() as sess:
        init_fn(sess)
        bboxes,obj_probs,class_probs = sess.run(output_decoded,feed_dict={tf_image:image_cp})

    # 【2】筛选解码后的回归边界框——NMS(post process后期处理)
    bboxes,scores,class_max_index = postprocess(bboxes,obj_probs,class_probs,image_shape=image_shape)

    # 【3】绘制筛选后的边界框
    img_detection = draw_detection(image, bboxes, scores, class_max_index, class_names)
    cv2.imwrite("./yolo2_data/detection.jpg", img_detection)
    print('YOLO_v2 detection has done!')
    cv2.imshow("detection_results", img_detection)
    cv2.waitKey(0)
Exemplo n.º 23
0
def infer(image: str,
          phi: int = 0,
          saved_model: str = './savedmodel',
          classes: dict = None,
          score_threshold: float = 0.3,
          nms_threshold: float = 0.5,
          device: str = 'gpu'):
    if device != 'gpu':
        os.environ['CUDA_VISIBLE_DEVICES'] = '-1'  #Using CPU
    else:
        os.environ['CUDA_VISIBLE_DEVICES'] = '0'  #Using GPU

    #For COCO dataset
    if classes == None:
        classes = {
            value['id'] - 1: value['name']
            for value in json.load(open('coco_90.json', 'r')).values()
        }

    #select resolution according to architecture
    image_sizes = (512, 640, 768, 896, 1024, 1280, 1408)
    image_size = image_sizes[phi]

    #To get different color for each class
    num_classes = len(classes.values())
    colors = [
        np.random.randint(0, 256, 3).tolist() for _ in range(num_classes)
    ]

    #load the model
    model = load_model(saved_model)

    #load and preprocess image
    img = cv2.imread(image)
    src_image = img.copy()
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    h, w = img.shape[:2]
    img, scale = preprocess_image(img, image_size=image_size)

    #detect and post process
    start = time.time()
    boxes, scores, labels = model.predict_on_batch(
        [np.expand_dims(img, axis=0)])
    boxes, scores, labels = np.squeeze(boxes), np.squeeze(scores), np.squeeze(
        labels)
    end = time.time()
    boxes = postprocess_boxes(boxes=boxes, scale=scale, height=h, width=w)

    # print(f'infer time: {end-start}, fps: {1/(end-start)}')

    # select indices which have a score above the threshold
    indices = np.where(scores[:] > score_threshold)[0]
    # indices = tf.image.non_max_suppression(boxes,scores,max_output_size=[100],iou_threshold = nms_threshold,score_threshold = score_threshold)
    boxes = boxes[indices]
    labels = labels[indices]

    #draw boxes on the original image
    draw_boxes(src_image, boxes, scores, labels, colors, classes)

    return src_image
Exemplo n.º 24
0
    def plot_image_heatmap(self, img, top=1):
        img_tensor = preprocess_image(img, self.model.mean,
                                      self.model.std)  # (1, 3, 299, 299)

        self._forward(img_tensor)

        cols = top + 1
        plt.figure(figsize=(4 * cols, 4))

        for i in range(cols):
            if i == 0:
                plt.subplot(1, cols, i + 1)
                plt.imshow(img, alpha=1.0)
                plt.title('Original image')
                plt.axis('off')
            else:
                class_idx = self._get_class_idx(i)
                label = self.idx2label[class_idx]
                proba = self.probas[class_idx]
                heatmap = self._generate_heatmap(class_idx)

                plt.subplot(1, cols, i + 1)
                plt.imshow(img, alpha=1.0)
                plt.imshow(heatmap, cmap='jet', alpha=0.5)
                plt.title('{} ({:.3f})'.format(label, proba))
                plt.axis('off')
def main():
    input_size = (416,416)
    image_file = './005767.jpg'
    image = cv2.imread(image_file)
    image_shape = image.shape[:2] 
    tf.reset_default_graph()
    # copy、resize416*416、
    image_cp = preprocess_image(image,input_size)
    #image_cp=np.ones([1,416,416,3]).astype(np.float32)
    image_cp=np.load("005767.npy")#("/home/huawei/chems/bioavailability_model/atlas_data/005767.npy")
    image_cp=np.transpose(image_cp,[0,2,3,1])
    np.save("atoms.npy",image_cp)
    #
    with tf.name_scope('input'):
        tf_image = tf.placeholder(tf.float32,[1,input_size[0],input_size[1],3],name='input_data')
        #meta_variable=tf.placeholder(tf.float32,[1,1,len(class_names)*1024,1],name='meta_weigiht')
    model_output = Darknet(tf_image)
    #meta=np.ones([1,1,len(class_names)*1024,1], dtype=np.float32)
    model_path = "./yolov2_model/yolov2_coco.ckpt"
    saver = tf.train.Saver()
    with tf.Session() as sess:
        #sess.run(model_output,feed_dict={tf_image:image_cp,meta_variable:meta})
        sess.run(tf.global_variables_initializer())
        a=sess.run(model_output.rtn(),feed_dict={tf_image:image_cp})
        a=np.transpose(a,[0,3,1,2])
        #a=np.transpose(a,[0,3,2,1])
        a=np.reshape(a,[-1])[:90]
        #print(a)
        for i in range(90):
        #    print("=============================")
            print(a[i],i)
        saver.save(sess,model_path)
 def get_data(self, image):
     with tf.name_scope('data'):
         jpeg = tf.read_file(image)
         image = tf.image.decode_jpeg(jpeg, channels=3)
         image = utils.preprocess_image(image, is_training=False)
         image = tf.expand_dims(image, 0)
         self.img = image
Exemplo n.º 27
0
    def r_mac_descriptor(self, file):
        """
        Get the descriptor from the path of the img
        :param file: the path of the image
        :return: the descriptor
        """
        # Load sample image
        # file = utils.DATA_DIR + 'sample.jpg'
        K.clear_session()
        img = image.load_img(file)
        # Resize
        scale = utils.IMG_SIZE / max(img.size)
        new_size = (int(np.ceil(scale * img.size[0])),
                    int(np.ceil(scale * img.size[1]))
                    )  # (utils.IMG_SIZE, utils.IMG_SIZE)
        #print('Original size: %s, Resized image: %s' % (str(img.size), str(new_size)))
        img = img.resize(new_size)
        # Mean substraction
        x = image.img_to_array(img)
        x = np.expand_dims(x, axis=0)
        x = utils.preprocess_image(x)

        # Load RMAC model
        Wmap, Hmap = self.get_size_vgg_feat_map(x.shape[3], x.shape[2])
        regions = self.rmac_regions(Wmap, Hmap, 7)
        #print('Loading RMAC model...')
        model = self.rmac((x.shape[1], x.shape[2], x.shape[3]), len(regions))

        # Compute RMAC vector
        #print('Extracting RMAC from image...')
        return model.predict([x, np.expand_dims(regions, axis=0)])
Exemplo n.º 28
0
    def read_and_decode(self, filename):
        filename_queue = tf.train.string_input_producer([filename])

        reader = tf.TFRecordReader()
        _, serialized_example = reader.read(filename_queue)
        features = tf.parse_single_example(serialized_example,
                                           features={
                                               'image_raw':
                                               tf.FixedLenFeature([],
                                                                  tf.string),
                                               'label_raw':
                                               tf.FixedLenFeature([],
                                                                  tf.string),
                                               'height':
                                               tf.FixedLenFeature([],
                                                                  tf.int64),
                                               'width':
                                               tf.FixedLenFeature([], tf.int64)
                                           })
        height = tf.cast(features['height'], tf.int32)
        width = tf.cast(features['width'], tf.int32)
        image = tf.decode_raw(features['image_raw'], tf.uint8)
        image = tf.reshape(image, [height, width, 3])
        label = tf.decode_raw(features['label_raw'], tf.uint8)
        label = tf.reshape(label, [height, width, 1])

        image, label = preprocess_image(image,
                                        label,
                                        is_training=self.config.is_training)

        return image, label
Exemplo n.º 29
0
def execute(file, curr, rank):
    # Load sample image
    # file = utils.DATA_DIR + 'sample.jpg'
    img = image.load_img(file)

    # Resize
    scale = utils.IMG_SIZE / max(img.size)
    new_size = (int(np.ceil(scale * img.size[0])),
                int(np.ceil(scale * img.size[1])))
    print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())),
          str(curr), "is processing...")
    img = img.resize(new_size)

    # Mean substraction
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    x = utils.preprocess_image(x)

    # Load RMAC model
    Wmap, Hmap = get_size_vgg_feat_map(x.shape[3], x.shape[2])
    regions = rmac_regions(Wmap, Hmap, 3)
    # print('Loading RMAC model...')
    model = rmac((x.shape[1], x.shape[2], x.shape[3]), len(regions), rank)

    # Compute RMAC vector
    # print('Extracting RMAC from image...')
    RMAC = model.predict([x, np.expand_dims(regions, axis=0)])
    return RMAC
Exemplo n.º 30
0
def compute_features(images, args):
    """
    Compute feature values for images

    :param images: List of images to compute features for
    :param args: Run-time arguments
    :return: Feature layer values for images
    """

    # Verify that pre-trained feature model exist
    if not os.path.exists(args.feature_model):
        logging.critical('Checkpoint %s does not exist' % args.feature_model)

        sys.exit(1)

    with tf.Graph().as_default():
        # Pre-process images
        input_images = [
            preprocess_image('%s/%s.jpg' % (path, image_id), args)
            for path, _, image_id in images
        ]

        # Run pre-processed images through network
        return run_network(feature_network(args),
                           args.feature_model,
                           args,
                           train=False,
                           value=input_images)
Exemplo n.º 31
0
def main():
    os.environ['CUDA_VISIBLE_DEVICES'] = '0'

    phi = 0
    weighted_bifpn = False
    model_path = 'checkpoints/flir_59_0.1409_0.6001.h5'
    image_sizes = (512, 640, 768, 896, 1024, 1280, 1408)
    image_size = image_sizes[phi]
    # coco classes
    classes = {
        value['id'] - 1: value['name']
        for value in json.load(open('coco_90.json', 'r')).values()
    }
    num_classes = 3
    score_threshold = 0.01
    colors = [
        np.random.randint(0, 256, 3).tolist() for _ in range(num_classes)
    ]
    _, model = efficientdet(phi=phi,
                            weighted_bifpn=weighted_bifpn,
                            num_classes=num_classes,
                            score_threshold=score_threshold)
    model.load_weights(model_path, by_name=True)

    video_path = r'G:\datasets\iray\cap\20200515_100016.avi'
    video_path = r'G:\datasets\iray\cap\20200515_100016.avi'
    video_path = r'G:\projects\20200513_102922.avi'
    cap = cv2.VideoCapture(video_path)

    while True:
        ret, image = cap.read()
        if not ret:
            break
        src_image = image.copy()
        # BGR -> RGB
        image = image[:, :, ::-1]
        h, w = image.shape[:2]

        image, scale = preprocess_image(image, image_size=image_size)
        # run network
        start = time.time()
        boxes, scores, labels = model.predict_on_batch(
            [np.expand_dims(image, axis=0)])
        boxes, scores, labels = np.squeeze(boxes), np.squeeze(
            scores), np.squeeze(labels)
        print(time.time() - start)
        boxes = postprocess_boxes(boxes=boxes, scale=scale, height=h, width=w)

        # select indices which have a score above the threshold
        indices = np.where(scores[:] > score_threshold)[0]

        # select those detections
        boxes = boxes[indices]
        labels = labels[indices]

        draw_boxes(src_image, boxes, scores, labels, colors, classes)

        cv2.namedWindow('image', cv2.WINDOW_NORMAL)
        cv2.imshow('image', src_image)
        cv2.waitKey(0)
Exemplo n.º 32
0
import numpy as np
import tensorflow as tf
import cv2
from PIL import Image

from model import darknet
from detect_ops import decode
from utils import preprocess_image, postprocess, draw_detection
from config import anchors, class_names


input_size = (416, 416)
image_file = "./images/car.jpg"
image = cv2.imread(image_file)
image_shape = image.shape[:2]
image_cp = preprocess_image(image, input_size)
"""
image = Image.open(image_file)
image_cp = image.resize(input_size, Image.BICUBIC)
image_cp = np.array(image_cp, dtype=np.float32)/255.0
image_cp = np.expand_dims(image_cp, 0)
#print(image_cp)
"""


images = tf.placeholder(tf.float32, [1, input_size[0], input_size[1], 3])
detection_feat = darknet(images)
feat_sizes = input_size[0] // 32, input_size[1] // 32
detection_results = decode(detection_feat, feat_sizes, len(class_names), anchors)

checkpoint_path = "./checkpoint_dir/yolo2_coco.ckpt"
Exemplo n.º 33
0
POOLING_FUNCTION = 'MAX'

# Load images
content_image = utils.read_image(CONTENT_PATH)
style_image = utils.read_image(STYLE_PATH)

g = tf.Graph()
with g.device(DEVICE), g.as_default(), tf.Session() as sess:
    # 1. Compute content representation
    print("1. Computing content representation...")
    content_shape = (1,) + content_image.shape  # add batch size dimension
    x = tf.placeholder(tf.float32, content_shape)
    net, activations, img_mean = vgg.net(VGG_19_PATH, x, pooling_function=POOLING_FUNCTION)

    # Pre-process image
    content_image_pp = utils.preprocess_image(content_image, img_mean)

    content_representation = activations[CONTENT_LAYER].eval(feed_dict={x: np.array([content_image_pp])})

    # 2. Compute style Gram matrices
    print("2. Computing style Gram matrices...")
    style_shape = (1,) + style_image.shape  # add batch size dimension
    x = tf.placeholder(tf.float32, style_shape)
    net, activations, _ = vgg.net(VGG_19_PATH, x, pooling_function=POOLING_FUNCTION)

    # Pre-process image
    style_image_pp = utils.preprocess_image(style_image, img_mean)

    style_layer_shapes = {}
    gram_matrices = {}
    for style_layer, _ in STYLE_LAYERS.items():
Exemplo n.º 34
0
import cv2
import numpy as np
import tensorflow as tf
import matplotlib.image as mpimg

from ssd_300_vgg import SSD
from utils import preprocess_image, process_bboxes
from visualization import plt_bboxes


ssd_net = SSD()
classes, scores, bboxes = ssd_net.detections()
images = ssd_net.images()

sess = tf.Session()
# Restore SSD model.
ckpt_filename = './ssd_checkpoints/ssd_vgg_300_weights.ckpt'
sess.run(tf.global_variables_initializer())
saver = tf.train.Saver()
saver.restore(sess, ckpt_filename)

img = cv2.imread('./demo/dog.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img_prepocessed = preprocess_image(img)
rclasses, rscores, rbboxes = sess.run([classes, scores, bboxes],
                                      feed_dict={images: img_prepocessed})
rclasses, rscores, rbboxes = process_bboxes(rclasses, rscores, rbboxes)

plt_bboxes(img, rclasses, rscores, rbboxes)