Пример #1
0
    def _get_features(self, model):
        """

		:param model: a Keras model instance
		:return: A list of features
		"""
        features = None
        if self._model_weights:

            image_path = self._images_path + 'cats4da3c9757cb337c692b9517b9f82abf7f21c681d8b1ce111f7ce546f30bdcf95.jpeg'
            PrintHelper.info_print("in if   :  ", image_path)
            img = image.load_img(image_path, target_size=self._target_size)
            x = image.img_to_array(img)
            x = x.transpose(1, 0, 2)
            x = np.expand_dims(x, axis=0)
            x = preprocess_input(x)
            features = model.predict(x)

        else:
            vaidation_datagen = ImageDataGenerator()

            validation_generator = vaidation_datagen.flow_from_directory(
                self._images_path,
                target_size=self._target_size,
                batch_size=20,
                class_mode='binary',
                shuffle=False)
            try:
                features = model.predict_generator(validation_generator,
                                                   steps=self._image_count)
            except ValueError as e:
                PrintHelper.failure_print("_get features.....", e)

        return features
Пример #2
0
    def extract_features(self):
        """

		:return: A list of features
		"""
        try:
            model = self._load_model()
            features = self._get_features(model)
            return features
        except Exception as e:
            PrintHelper.failure_print("extract features.....", e)
Пример #3
0
    def get_predictions(self):
        """

		:return: a list of predictions
		"""
        features = self.extract_features()
        predictions = None
        try:
            predictions = decode_predictions(features, top=3)
        except ValueError as e:
            PrintHelper.failure_print("get predictions.....", e)

        shutil.rmtree(self._images_path)
        return predictions
Пример #4
0
def image_dataset_upload():
    try:
        form = request.form

        class_name = ""

        for key, value in form.items():
            if key == 'class_name':
                class_name = value

        image_file_list = request.files.getlist("file")

        ImageDownloader().recieve_image_dataset_from_user(
            image_file_list, class_name)

        return Response.create_success_response(
            "Images Downloaded successfully", "success")

    except Exception as e:
        PrintHelper.failure_print("An error occurered:  " + str(e))
        return Response.create_failure_response("An error occurred: " + str(e))
Пример #5
0
    def _load_model(self):
        model = None
        try:
            if self._model_name == 'VGG16' or self._model_name == 'vgg16':
                model = VGG16(weights='imagenet', include_top=True)
            elif self._model_name == 'VGG19' or self._model_name == 'vgg19':
                model = VGG19(weights='imagenet', include_top=True)
            elif self._model_name == 'ResNet50' or self._model_name == 'resNet50' \
              or self._model_name == 'Resnet50' or self._model_name == 'resnet50':
                model = ResNet50(weights='imagenet', include_top=True)
            elif self._model_name == 'Xception' or self._model_name == 'xception':
                model = Xception(weights='imagenet', include_top=True)
            elif self._model_name == 'InceptionV3' or self._model_name == 'inceptionV3' \
              or self._model_name == 'inceptionv3' or self._model_name == 'Inceptionv3':
                model = InceptionV3(weights='imagenet', include_top=True)
            else:
                model = load_model(self._model_path)
                model.load_weights(self._model_weights)

        except Exception as e:
            PrintHelper.failure_print("loading models.....", e)

        return model
Пример #6
0
    def __init__(self,
                 model_name,
                 images,
                 image_count,
                 model_hash=None,
                 base_model=None):
        """

		:param model_name: name/hash of the model to predict/extract-features from
		:param images: path for the images downlaoded
		:param model_hash: hash saved in the database: required for loading weights and files
		:param base_model: required only when a custom generated model is being evaluated, check what is the base model of the model being used
		"""
        self._images_path = images
        self._model_name = model_name
        size = Constants.get_image_dimensions(model_name)
        self._target_size = (size, size)
        self._image_count = image_count

        self._model_path = None
        self._model_weights = None

        # only required for custom models, in which case base_model should be supplied
        if base_model is not None and model_hash is not None:
            try:
                self._model_path = os.path.dirname(os.path.realpath(
                    __file__)) + Constants.saving_model_specific_file(
                        base_model
                    ) + base_model + '----model_file' + model_hash + '.h5'
                self._model_weights = os.path.dirname(
                    os.path.realpath(
                        __file__)) + Constants.saving_model_weights(
                            base_model
                        ) + base_model + '----weights' + model_hash + '.h5'
            except Exception as e:
                PrintHelper.failure_print("model file creation", e)
Пример #7
0
def start_predictions():
    try:
        form = request.form

        model_name = ""

        # Needed only with custom model training
        base_model = None
        model_hash = None
        ######

        for key, value in form.items():
            if key == 'model_hash':
                model_hash = value
            if key == 'model_name':
                model_name = value
            if key == 'model_base_name':
                base_model = value

        image_count = 0
        image_paths = ''
        save_files = request.files.getlist("file")
        for upload in request.files.getlist("file"):
            filename = upload.filename.rsplit("/")[0]
            PrintHelper.info_print("Uploaded: ", filename)
            destination_main = os.path.dirname(
                os.path.realpath(__file__)) + prediction_images_dir + '/'
            destination = destination_main + model_name + '/'
            Constants.directory_validation(destination)
            upload.save(destination + filename)
            image_paths = destination_main
            image_count = +1

        feature_extraction = FeatureExtraction(model_name, image_paths,
                                               image_count)
        predictions = feature_extraction.get_predictions()

        # converting the complex tuple structre into javascript friendly json object
        count = 0
        prediction_array = []
        for item in predictions:
            result_outer = []
            for items in item:
                result_inner = {
                    "class": items[0],
                    "description": items[1],
                    "probability": str(items[2])
                }
                result_outer.append(result_inner)

            image_file = save_files[count]
            count = count + 1

            image_file_name = image_file.filename.rsplit("/")[0]

            final_prediction_result = {
                "filename": image_file_name,
                "predictions": result_outer
            }
            prediction_array.append(final_prediction_result)

        return Response.create_success_response(prediction_array, "success")

    except Exception as e:
        PrintHelper.failure_print("An error occurered:  " + str(e))
        return Response.create_failure_response("An error occurred: " + str(e))