예제 #1
0
def extract_features(path, model_type):
    if model_type == 'inceptionv3':
        from keras.applications.inception_v3 import preprocess_input
        target_size = (299, 299)
    elif model_type == 'vgg16':
        from keras.applications.vgg16 import preprocess_input
        target_size = (224, 224)
    # Get CNN Model from model.py
    model = CNNModel(model_type)
    features = dict()
    # Extract features from each photo
    for name in tqdm(os.listdir(path)):
        # Loading and resizing image
        filename = path + name
        image = load_img(filename, target_size=target_size)
        # Convert the image pixels to a numpy array
        image = img_to_array(image)
        # Reshape data for the model
        image = image.reshape(
            (1, image.shape[0], image.shape[1], image.shape[2]))
        # Prepare the image for the CNN Model model
        image = preprocess_input(image)
        # Pass image into model to get encoded features
        feature = model.predict(image, verbose=0)
        # Store encoded features for the image
        image_id = name.split('.')[0]
        features[image_id] = feature
    return features
예제 #2
0
def predict_test_set(model_path,
                     batch_size,
                     pkl_data_dir,
                     target_dir,
                     num_gpus=1):
    dataset = DataSet(data_dir=config['data']['data_dir'],
                      test=True,
                      pkl_file_dir=pkl_data_dir)
    cnn_model = CNNModel(model_path, False, gpu_nums=num_gpus)
    test_data_gen = dataset.get_test_data_gen(batch_size)
    pred_list = []
    label_list = []
    fp_dict = {'file_path': [], 'pkl_idx': [], 'p': []}
    fn_dict = {'file_path': [], 'pkl_idx': [], 'p': []}
    for batch_data, batch_label, batch_info in test_data_gen:
        preds = cnn_model.predict(batch_data['inputs'], batch_size)
        idx = 0
        for pred in preds:
            l = batch_label['out_class'][idx][1]
            p = pred[1]
            label_list.append(l)
            pred_list.append(p)
            if p >= 0.5 and l != 1:
                fp_dict['file_path'].append(batch_info['file_path'][idx])
                fp_dict['pkl_idx'].append(batch_info['pkl_idx'][idx])
                fp_dict['p'].append(p)
            if p < 0.5 and l != 0:
                fn_dict['file_path'].append(batch_info['file_path'][idx])
                fn_dict['pkl_idx'].append(batch_info['pkl_idx'][idx])
                fn_dict['p'].append(p)
            idx += 1
    auc = cal_roc_and_auc(np.array(pred_list), np.array(label_list))
    fn_df = pd.DataFrame(fn_dict)
    fp_df = pd.DataFrame(fp_dict)
    fn_df.to_csv(os.path.join(target_dir, 'fn_result.csv'), index=False)
    fp_df.to_csv(os.path.join(target_dir, 'fp_result.csv'), index=False)
    draw_roc(np.array(pred_list), np.array(label_list), target_dir)
    print('auc:%f' % auc)
예제 #3
0
class DarkOCR:
    def __init__(self):
        print('DarkOCR initialization...')
        self.data = ImageData()
        # reading data
        self.data.read_origin_data(pickle_path)
        self.model = CNNModel(image_dim, image_dim, classes_count)
        self.models_fold = [
            CNNModel(image_dim, image_dim, classes_count)
            for i in range(fold_count)
        ]
        print('Complete')

    def show_origin_data(self, char='p'):
        # visualization
        self.data.show_origin_all_chars()
        self.data.show_origin_chars_data()
        self.data.show_origin_chars_data(char)

    def show_origin_data_statistics(self):
        self.data.show_origin_data_histogram()
        self.data.print_origin_labels_count()

    def save_data_set_to_png(self, path):
        self.data.save_data_set_to_png(path)

    def augment_folder(self, path, char_i=None, generated_count=50):
        pixels_mean = None
        if char_i is not None:
            path += '/' + str(char_i)
            pixels_mean_per_class = self.data.calc_pixels_mean()
            pixels_mean = pixels_mean_per_class[char_i]

        augment_folder(path,
                       generated_count=generated_count,
                       pixels_mean=pixels_mean)

    def fit_from_aug_folder(self, path=png_path):
        data_set = self.data.read_augmented_data_and_process(
            in_path=path, classes_count_int=4)
        self.fit(data_set)

    def fit_from_aug_pickle(self,
                            aug_pickle_path=augmented_pickle_path,
                            test_fold=4):
        data_set = self.data.read_pickle(aug_pickle_path)
        self.fit(data_set, test_fold=test_fold)

    def fit(self, data_set, test_fold=4):
        (train_x,
         train_y), (test_x,
                    test_y) = self.data.from_processed_data_to_training_set(
                        data_set=data_set,
                        test_fold=test_fold,
                        ignore_class=30)

        self.model.fit(train_x, train_y, test_x, test_y)

    def load_trained_models_group(self):
        print('Loading models group...')
        for fold in range(fold_count):
            self.models_fold[fold].load_model(fold=fold)
        print('Done')

    def predict(self, input_data):
        prediction = self.model.predict(input_data)
        return np.argmax(prediction, axis=1)

    def predict_from_fold(self, input_data, fold=4):
        prediction = self.models_fold[fold].predict(input_data)
        return np.argmax(prediction, axis=1)

    def predict_from_group(self, input_data):
        # print('Making prediction...')
        prediction_votes = np.zeros((len(input_data), classes_count))
        for fold in range(fold_count):
            prediction_votes += self.models_fold[fold].predict(input_data)

        return np.argmax(prediction_votes, axis=1)

    def predict_image(self, im, decode=False):
        im = im.convert("L")
        ia = np.array(im, dtype='d')

        ia = ia / 255
        ia = ia.reshape(-1, image_dim, image_dim, 1)
        prediction = self.predict_from_group(ia)
        if decode:
            prediction = ImageData.decode(prediction[0])

        return prediction

    def evaluate_by_image_folder(self, path):
        correct_count = 0
        examples_count = 0

        answers_counter = [0] * classes_count
        correct_counter = [0] * classes_count

        for im_path in glob.glob(path + '/*.png'):
            hash_i = im_path.rfind("#")
            label = im_path[hash_i + 1]

            im = Image.open(im_path)
            prediction = self.predict_image(im, decode=True)

            if label == prediction:
                correct_count += 1
                correct_counter[ImageData.encode(label)] += 1
            else:
                print("WRONG!: {}, LABEL: {}, PREDICTION: {}".format(
                    im_path, label, prediction))

            answers_counter[ImageData.encode(prediction)] += 1
            examples_count += 1

        accuracy = 100 * correct_count / examples_count
        print('Results: {:.2f}%'.format(accuracy))
        for i in range(len(answers_counter)):
            print('{} ({}). correct: {}, answers count: {}'.format(
                i, ImageData.decode(i), correct_counter[i],
                answers_counter[i]))
        return accuracy