def process_images(input_path, batch_size=64, crop=True):
    model = load_model(
        "I:\\pythonProject\\RotNet\\rotnet_models\\rotnet_street_view_resnet50_keras2.hdf5",
        custom_objects={'angle_error': angle_error},
        compile=False)

    extensions = ['.jpg', '.jpeg', '.bmp', '.png']

    if os.path.isfile(input_path) or input_path[:4].lower() == "http":
        image_paths = [input_path]

    else:
        image_paths = [
            os.path.join(input_path, f) for f in os.listdir(input_path)
            if os.path.splitext(f)[1].lower() in extensions
        ]

    predictions = model.predict_generator(RotNetDataGenerator(
        image_paths,
        input_shape=(224, 224, 3),
        batch_size=batch_size,
        one_hot=True,
        preprocess_func=preprocess_input,
        rotate=False,
        crop_largest_rect=True,
        crop_center=True),
                                          val_samples=len(image_paths))

    predicted_angles = np.argmax(predictions, axis=1)
    print(predicted_angles)
    return predicted_angles
    def train(self):
        train_generator = RotNetDataGenerator(
            self.train_data,
            input_shape=self.input_shape,
            batch_size=self.batch_size,
            preprocess_func=preprocess_input,
            crop_center=False,
            crop_largest_rect=True,
            shuffle=True,
            is_hw_ratio=self.is_hw_ratio,
            random_angle=self.random_angle,
            is_random_crop=self.is_random_crop)

        test_generator = RotNetDataGenerator(
            self.test_data,
            input_shape=self.input_shape,
            batch_size=self.batch_size,
            preprocess_func=preprocess_input,
            crop_center=False,
            crop_largest_rect=True,
            is_train=False,  # 关闭训练参数
            is_hw_ratio=self.is_hw_ratio,
            random_angle=self.random_angle,
            is_random_crop=self.is_random_crop)

        steps_per_epoch = min(len(self.train_data) // self.batch_size, 50000)
        validation_steps = min(len(self.test_data) // self.batch_size, 5000)

        monitor = 'val_acc'
        checkpointer = ModelCheckpoint(filepath=os.path.join(
            self.output_dir, self.model_name),
                                       monitor=monitor,
                                       save_best_only=True)
        n_workers = 3
        print('[Info] n_workers: {}'.format(n_workers))

        # training loop
        self.model.fit(
            train_generator,
            steps_per_epoch=steps_per_epoch,
            epochs=self.nb_epoch,
            validation_data=test_generator,
            validation_steps=validation_steps,
            callbacks=[checkpointer],
            workers=n_workers,
        )
def process_images(model, input_path, output_path,
                   batch_size=64, crop=True):
    extensions = ['.jpg', '.jpeg', '.bmp', '.png']

    output_is_image = False
    if os.path.isfile(input_path):
        image_paths = [input_path]
        if os.path.splitext(output_path)[1].lower() in extensions:
            output_is_image = True
            output_filename = output_path
            output_path = os.path.dirname(output_filename)
    else:
        image_paths = [os.path.join(input_path, f)
                       for f in os.listdir(input_path)
                       if os.path.splitext(f)[1].lower() in extensions]
        if os.path.splitext(output_path)[1].lower() in extensions:
            print('Output must be a directory!')

    predictions = model.predict_generator(
        RotNetDataGenerator(
            image_paths,
            input_shape=(224, 224, 3),
            batch_size=64,
            one_hot=True,
            preprocess_func=preprocess_input,
            rotate=False,
            crop_largest_rect=True,
            crop_center=True
        ),
        val_samples=len(image_paths)
    )

    predicted_angles = np.argmax(predictions, axis=1)

    if output_path == '':
        output_path = '.'

    if not os.path.exists(output_path):
        os.makedirs(output_path)

    for path, predicted_angle in zip(image_paths, predicted_angles):
        image = cv2.imread(path)
        rotated_image = rotate(image, -predicted_angle)
        if crop:
            size = (image.shape[0], image.shape[1])
            rotated_image = crop_largest_rectangle(rotated_image, -predicted_angle, *size)
        if not output_is_image:
            output_filename = os.path.join(output_path, os.path.basename(path))
        cv2.imwrite(output_filename, rotated_image)
Пример #4
0
def predict(model, input_path, batch_size=64, crop=True):
    predictions = model.predict_generator(RotNetDataGenerator(
        [input_path],
        input_shape=(224, 224, 3),
        batch_size=64,
        one_hot=True,
        preprocess_func=preprocess_input,
        rotate=False,
        crop_largest_rect=True,
        crop_center=True),
                                          val_samples=1)

    predicted_angle = np.argmax(predictions, axis=1)[0]
    image = cv2.imread(input_path)
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    rotated_image = rotate(image, -predicted_angle)
    if crop:
        size = (image.shape[0], image.shape[1])
        rotated_image = crop_largest_rectangle(rotated_image, -predicted_angle,
                                               *size)
    return rotated_image, predicted_angle
# callbacks
monitor = 'val_angle_error'
checkpointer = ModelCheckpoint(filepath=os.path.join(output_folder,
                                                     model_name + '.hdf5'),
                               monitor=monitor,
                               save_best_only=True)
reduce_lr = ReduceLROnPlateau(monitor=monitor, patience=3)
early_stopping = EarlyStopping(monitor=monitor, patience=5)
tensorboard = TensorBoard()

# training loop
model.fit_generator(
    RotNetDataGenerator(train_filenames,
                        input_shape=input_shape,
                        batch_size=batch_size,
                        preprocess_func=preprocess_input,
                        crop_center=True,
                        crop_largest_rect=True,
                        shuffle=True),
    steps_per_epoch=len(train_filenames) / batch_size,
    epochs=nb_epoch,
    validation_data=RotNetDataGenerator(test_filenames,
                                        input_shape=input_shape,
                                        batch_size=batch_size,
                                        preprocess_func=preprocess_input,
                                        crop_center=True,
                                        crop_largest_rect=True),
    validation_steps=len(test_filenames) / batch_size,
    callbacks=[checkpointer, reduce_lr, early_stopping, tensorboard],
    workers=10)
Пример #6
0
    copyfile(os.path.basename(__file__),
             output_folder + os.path.basename(__file__))

    tensorboard = TensorBoard(log_dir=output_log)

    checkpointer = CustomModelCheckpoint(
        model_for_saving=model,
        filepath=output_weight + "weights_{epoch:02d}_{val_loss:.2f}.h5",
        save_best_only=True,
        monitor='val_loss',
        save_weights_only=True)

    generator_training = RotNetDataGenerator(input_shape=input_shape,
                                             batch_size=batch_size,
                                             one_hot=True,
                                             preprocess_func=preprocess_input,
                                             shuffle=True).generate(
                                                 paths_train, labels_train,
                                                 len(classes_focal))
    generator_valid = RotNetDataGenerator(input_shape=input_shape,
                                          batch_size=batch_size,
                                          one_hot=True,
                                          preprocess_func=preprocess_input,
                                          shuffle=True).generate(
                                              paths_valid, labels_valid,
                                              len(classes_focal))

    # training loop
    model.fit_generator(
        generator=generator_training,
        steps_per_epoch=(len(paths_train) // batch_size),  # 29977
batch_size = 128
nb_epoch = 50

output_folder = 'models'
if not os.path.exists(output_folder):
    os.makedirs(output_folder)

# callbacks
checkpointer = ModelCheckpoint(filepath=os.path.join(output_folder,
                                                     model_name + '.hdf5'),
                               save_best_only=True)
early_stopping = EarlyStopping(patience=2)
tensorboard = TensorBoard()

# training loop
model.fit_generator(RotNetDataGenerator(X_train,
                                        batch_size=batch_size,
                                        one_hot=False,
                                        preprocess_func=binarize_images,
                                        shuffle=True),
                    steps_per_epoch=nb_train_samples / batch_size,
                    epochs=nb_epoch,
                    validation_data=RotNetDataGenerator(
                        X_test,
                        one_hot=False,
                        preprocess_func=binarize_images,
                        batch_size=batch_size),
                    validation_steps=nb_test_samples / batch_size,
                    verbose=1,
                    callbacks=[checkpointer, early_stopping, tensorboard])
Пример #8
0
# training parameters
batch_size = 128
nb_epoch = 50

output_folder = 'models'
if not os.path.exists(output_folder):
    os.makedirs(output_folder)

# callbacks
checkpointer = ModelCheckpoint(filepath=os.path.join(output_folder,
                                                     model_name + '.hdf5'),
                               save_best_only=True)
early_stopping = EarlyStopping(patience=2)
tensorboard = TensorBoard()

# training loop
model.fit_generator(
    RotNetDataGenerator(X_train,
                        batch_size=batch_size,
                        preprocess_func=binarize_images,
                        shuffle=True),
    steps_per_epoch=nb_train_samples / batch_size,
    epochs=nb_epoch,
    validation_data=RotNetDataGenerator(X_test,
                                        batch_size=batch_size,
                                        preprocess_func=binarize_images),
    validation_steps=nb_test_samples / batch_size,
    verbose=1  #,
    # callbacks=[checkpointer, early_stopping, tensorboard]
)
Пример #9
0
def process_images(model, input_path, output_path, batch_size=64, log=False):
    extensions = ['.jpg', '.jpeg', '.bmp', '.png']

    output_is_image = False
    if os.path.isfile(input_path):
        image_paths = [input_path]
        if os.path.splitext(output_path)[1].lower() in extensions:
            output_is_image = True
            output_filename = output_path
            output_path = os.path.dirname(output_filename)
    else:
        image_paths = [
            os.path.join(input_path, f) for f in os.listdir(input_path)
            if os.path.splitext(f)[1].lower() in extensions
        ]
        if os.path.splitext(output_path)[1].lower() in extensions:
            print('Output must be a directory!')

    predictions = model.predict_generator(
        RotNetDataGenerator(image_paths,
                            input_shape=(224, 224, 3),
                            batch_size=64,
                            one_hot=True,
                            preprocess_func=preprocess_input,
                            rotate=False,
                            crop_largest_rect=True,
                            crop_center=True))

    predicted_angles = np.argmax(predictions, axis=1)
    rotated_angles = [
    ]  # We will be using this to compile what angles we actually rotate by for logging

    if output_path == '':
        output_path = '.'

    if not os.path.exists(output_path):
        os.makedirs(output_path)

    for path, predicted_angle in zip(image_paths, predicted_angles):
        image = cv2.imread(path)
        isLandscape = image.shape[1] >= image.shape[
            0]  # image.shape = [height, width, channels]
        if 45 <= predicted_angle < 315:
            """
            Angle rotation logic:
            if 45 < pred_angle < 315:
                if isLandscape: rotate by +-90
                else: rotate by 0/180
            """
            if 45 <= predicted_angle and predicted_angle < 135 and not isLandscape:
                angle_cv2 = cv2.ROTATE_90_CLOCKWISE
                rotated_angle = 90
            elif 135 <= predicted_angle and predicted_angle < 225 and isLandscape:
                angle_cv2 = cv2.ROTATE_180
                rotated_angle = 180
            elif 225 <= predicted_angle and predicted_angle < 315 and not isLandscape:
                angle_cv2 = cv2.ROTATE_90_COUNTERCLOCKWISE
                rotated_angle = 270
            else:
                rotated_angles.append(0)
                continue
            rotated_image = cv2.rotate(image, angle_cv2)
            rotated_angles.append(rotated_angle)
            print("Rotated {} {}°".format(path, rotated_angle))
            if not output_is_image:
                output_filename = os.path.join(output_path,
                                               os.path.basename(path))
            cv2.imwrite(output_filename, rotated_image)
        else:
            rotated_angles.append(0)

    if log:
        """
        Create a CSV file and dump the logs in the format
        "image_filename, rotation_angle"
        """
        with open(
                "log-{}.csv".format(
                    datetime.datetime.now().strftime('%Y%m%dT%H%M%S')),
                'a') as f:
            f.write("Filename, Angle\n")
            for path, rotated_angle in zip(image_paths, rotated_angles):
                f.write("{}, {}\n".format(os.path.basename(path),
                                          rotated_angle))