def load_model(self, model_path):
     """
     This is a custom function for loading resnet model.
     ARGS:
     model_path: The relative path to trained model.
     """
     img_width, img_height = IMG_SIZE, IMG_SIZE
     num_channels = 3
     num_classes = NUM_CLASSES
     model = resnet152_model(img_height, img_width, num_channels,
                             num_classes)
     model.load_weights(model_path, by_name=True)
     return model
Esempio n. 2
0
    y_val = keras.utils.to_categorical(y_val, num_classes)
    y_test = keras.utils.to_categorical(y_test, num_classes)

    print("Preprocess data.")

    x_test = x_test[
        ..., ::
        -1]  # What this does? Changes the order of last layer - RGB to BGR
    x_val = x_val[..., ::-1]

    for i in range(3):
        x_test[:, :, :, i] -= MEAN[i]
        x_val[:, :, :, i] -= MEAN[i]

    model = resnet152_model()
    sgd = SGD(lr=1e-2, decay=1e-6, momentum=0.9, nesterov=True)
    model.compile(optimizer=sgd,
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])

    print("Start evaluation!")
    evaluate_model(model,
                   weights_file_resnet,
                   x_test,
                   y_test,
                   bins=15,
                   verbose=True,
                   pickle_file="probs_resnet152_imgnet",
                   x_val=x_val,
                   y_val=y_val)
Esempio n. 3
0
    def get_image_features(self, image_directory):

        from keras.preprocessing import image
        from keras.models import Model

        if self.cnn_extractor == 'vgg16':

            from keras.applications.vgg16 import preprocess_input
            from keras.applications import VGG16

            self.IMG_FEATS = 4096
            base_model = VGG16(weights='imagenet')
            model = Model(inputs=base_model.input,
                          outputs=base_model.get_layer('fc2').output)
            self.extracted_features = []
            self.image_feature_files = list(set(self.image_files))
            number_of_images = len(self.image_feature_files)
            for image_arg, image_file in tqdm(
                    enumerate(self.image_feature_files)):
                image_path = image_directory + image_file
                # if image_arg%100 == 0:
                #     print('%.2f %% completed' %
                #             round(100*image_arg/number_of_images,2))
                img = image.load_img(image_path, target_size=(224, 224))
                img = image.img_to_array(img)
                img = np.expand_dims(img, axis=0)
                img = preprocess_input(img)
                CNN_features = model.predict(img)
                self.extracted_features.append(np.squeeze(CNN_features))
            self.extracted_features = np.asarray(self.extracted_features)

        elif self.cnn_extractor == 'vgg19':

            from keras.applications.vgg19 import preprocess_input
            from keras.applications import VGG19

            self.IMG_FEATS = 4096
            base_model = VGG19(weights='imagenet')
            model = Model(inputs=base_model.input,
                          outputs=base_model.get_layer('fc2').output)
            self.extracted_features = []
            self.image_feature_files = list(set(self.image_files))
            number_of_images = len(self.image_feature_files)
            for image_arg, image_file in enumerate(self.image_feature_files):
                image_path = image_directory + image_file
                if image_arg % 100 == 0:
                    print('%.2f %% completed' %
                          round(100 * image_arg / number_of_images, 2))
                img = image.load_img(image_path, target_size=(224, 224))
                img = image.img_to_array(img)
                img = np.expand_dims(img, axis=0)
                img = preprocess_input(img)
                CNN_features = model.predict(img)
                self.extracted_features.append(np.squeeze(CNN_features))
            self.extracted_features = np.asarray(self.extracted_features)

        elif self.cnn_extractor == 'inception':

            from keras.applications.inception_v3 import preprocess_input
            from keras.applications import InceptionV3

            self.IMG_FEATS = 2048
            base_model = InceptionV3(weights='imagenet')
            model = Model(inputs=base_model.input,
                          outputs=base_model.get_layer('avg_pool').output)
            self.extracted_features = []
            self.image_feature_files = list(set(self.image_files))
            number_of_images = len(self.image_feature_files)
            for image_arg, image_file in tqdm(
                    enumerate(self.image_feature_files)):
                image_path = image_directory + image_file
                # if image_arg%100 == 0:
                #     print('%.2f %% completed' %
                #             round(100*image_arg/number_of_images,2))
                img = image.load_img(image_path, target_size=(299, 299))
                img = image.img_to_array(img)
                img = np.expand_dims(img, axis=0)
                img = preprocess_input(img)
                CNN_features = model.predict(img)
                self.extracted_features.append(np.squeeze(CNN_features))
            self.extracted_features = np.asarray(self.extracted_features)
        elif self.cnn_extractor == 'vgg16_places':
            from VGG_Place205 import VGG16_places
            from keras.applications.vgg16 import preprocess_input
            self.IMG_FEATS = 4096
            base_model = VGG16_places()
            base_model.load_weights(
                "VGG_Place205/vgg_tensorflow_channels_last.h5")
            model = Model(inputs=base_model.input,
                          outputs=base_model.get_layer('fc7').output)

            self.extracted_features = []
            self.image_feature_files = list(set(self.image_files))
            number_of_images = len(self.image_feature_files)
            for image_arg, image_file in enumerate(self.image_feature_files):
                image_path = image_directory + image_file
                if image_arg % 100 == 0:
                    print('%.2f %% completed' %
                          round(100 * image_arg / number_of_images, 2))
                img = image.load_img(image_path, target_size=(224, 224))
                img = image.img_to_array(img)
                img = np.expand_dims(img, axis=0)
                img = preprocess_input(img)
                CNN_features = model.predict(img)
                self.extracted_features.append(np.squeeze(CNN_features))
            self.extracted_features = np.asarray(self.extracted_features)
        elif self.cnn_extractor == 'inceptionresnetv2':
            from keras.applications.inception_resnet_v2 import InceptionResNetV2, preprocess_input
            base_model = InceptionResNetV2(weights="imagenet")
            model = Model(inputs=base_model.input,
                          outputs=base_model.get_layer('avg_pool').output)
            self.extracted_features = []
            self.image_feature_files = list(set(self.image_files))
            number_of_images = len(self.image_feature_files)
            for image_arg, image_file in enumerate(self.image_feature_files):
                image_path = image_directory + image_file
                if image_arg % 100 == 0:
                    print('%.2f %% completed' %
                          round(100 * image_arg / number_of_images, 2))
                img = image.load_img(image_path, target_size=(229, 229))
                img = image.img_to_array(img)
                img = np.expand_dims(img, axis=0)
                img = preprocess_input(img)
                CNN_features = model.predict(img)
                self.extracted_features.append(np.squeeze(CNN_features))
            self.extracted_features = np.asarray(self.extracted_features)
        elif self.cnn_extractor == 'resnet50':
            from keras.applications.resnet50 import ResNet50, preprocess_input
            base_model = ResNet50(weights="imagenet")
            model = Model(inputs=base_model.input,
                          outputs=base_model.get_layer('avg_pool').output)
            self.extracted_features = []
            self.image_feature_files = list(set(self.image_files))
            number_of_images = len(self.image_feature_files)
            for image_arg, image_file in tqdm(
                    enumerate(self.image_feature_files)):
                image_path = image_directory + image_file
                # if image_arg%100 == 0:
                #     print('%.2f %% completed' %
                #             round(100*image_arg/number_of_images,2))
                img = image.load_img(image_path, target_size=(224, 224))
                img = image.img_to_array(img)
                img = np.expand_dims(img, axis=0)
                img = preprocess_input(img)
                CNN_features = model.predict(img)
                self.extracted_features.append(np.squeeze(CNN_features))
            self.extracted_features = np.asarray(self.extracted_features)
        elif self.cnn_extractor == 'xception':
            from keras.applications.xception import Xception, preprocess_input
            base_model = Xception(weights="imagenet")
            model = Model(inputs=base_model.input,
                          outputs=base_model.get_layer('avg_pool').output)
            self.extracted_features = []
            self.image_feature_files = list(set(self.image_files))
            number_of_images = len(self.image_feature_files)
            for image_arg, image_file in enumerate(self.image_feature_files):
                image_path = image_directory + image_file
                if image_arg % 100 == 0:
                    print('%.2f %% completed' %
                          round(100 * image_arg / number_of_images, 2))
                img = image.load_img(image_path, target_size=(224, 224))
                img = image.img_to_array(img)
                img = np.expand_dims(img, axis=0)
                img = preprocess_input(img)
                CNN_features = model.predict(img)
                self.extracted_features.append(np.squeeze(CNN_features))
            self.extracted_features = np.asarray(self.extracted_features)
        elif self.cnn_extractor == 'resnet152':
            from resnet152 import resnet152_model
            base_model = resnet152_model("resnet152_weights_tf.h5")
            model = Model(inputs=base_model.input,
                          outputs=base_model.get_layer('avg_pool').output)
            self.extracted_features = []
            self.image_feature_files = list(set(self.image_files))
            number_of_images = len(self.image_feature_files)
            for image_arg, image_file in enumerate(self.image_feature_files):
                image_path = image_directory + image_file
                if image_arg % 100 == 0:
                    print('%.2f %% completed' %
                          round(100 * image_arg / number_of_images, 2))
                # img = image.load_img(image_path, target_size=(224, 224))
                # img = image.img_to_array(img)
                img = cv2.resize(cv2.imread(image_path),
                                 (224, 224)).astype(np.float32)
                # Remove train image mean
                img[:, :, 0] -= 103.939
                img[:, :, 1] -= 116.779
                img[:, :, 2] -= 123.68
                img = np.expand_dims(img, axis=0)
                # img = preprocess_input(img)
                CNN_features = model.predict(img)
                self.extracted_features.append(np.squeeze(CNN_features))
            self.extracted_features = np.asarray(self.extracted_features)
def build_resnet152_model():
    """
    Builds resnet152 model. Code partially used from another gist.
    """
    return resnet152_model(IMG_SIZE, IMG_SIZE, 3, NUM_CLASSES)