Exemplo n.º 1
0
 def network(inputs):
     previous = resnet50.ResNet50(
         include_top=False,
         input_tensor=inputs,
         pooling=pooling,
         weights=weights,
     ).output
     return tf.keras.layers.Flatten(name="flatten")(previous)
Exemplo n.º 2
0
    def __init__(self, settings: Settings, view_id, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.settings = settings
        self.view_id = view_id

        # region Use VGG16
        if self.settings.svcnn_model == 'vgg16':
            vgg16_model = vgg16.VGG16(include_top=False,
                                      input_shape=settings.input_shape,
                                      pooling=None,
                                      weights=None)
            x = vgg16_model.layers[-1].output
            x = Flatten(name='flatten')(x)
            x = Dense(4096, activation='relu', name='fc1')(x)
            x = Dense(4096, activation='relu', name='fc2')(x)
            x = Dense(settings.num_classes,
                      activation='softmax',
                      name='predictions')(x)
            self.model_input = vgg16_model.input
            self.model_output = x
            self.model = Model(inputs=self.model_input,
                               outputs=self.model_output)
        # endregion

        # region Use VGG19
        elif self.settings.svcnn_model == 'vgg19':
            vgg19_model = vgg19.VGG19(include_top=False,
                                      input_shape=settings.input_shape,
                                      pooling=None,
                                      weights=None)
            x = vgg19_model.layers[-1].output
            x = Flatten(name='flatten')(x)
            x = Dense(4096, activation='relu', name='fc1')(x)
            x = Dense(4096, activation='relu', name='fc2')(x)
            x = Dense(settings.num_classes,
                      activation='softmax',
                      name='predictions')(x)
            self.model_input = vgg19_model.input
            self.model_output = x
            self.model = Model(inputs=self.model_input,
                               outputs=self.model_output)
        # endregion

        # region Use ResNet50
        elif self.settings.svcnn_model == 'resnet50':
            resnet50_model = resnet50.ResNet50(
                include_top=False,
                input_shape=settings.input_shape,
                pooling=None,
                weights=None)
            x = resnet50_model.layers[-1].output
            x = GlobalAveragePooling2D(name='avg_pool')(x)
            x = Dense(settings.num_classes, activation='softmax',
                      name='probs')(x)
            self.model_input = resnet50_model.input
            self.model_output = x
            self.model = Model(inputs=self.model_input,
                               outputs=self.model_output)
Exemplo n.º 3
0
def build_model(input_shape, output_shape):
    pretrained_model = resnet50.ResNet50(weights='imagenet', include_top=False)
    x = pretrained_model.output
    x = GlobalAveragePooling2D()(x)
    x = Dense(2048, activation='relu')(x)
    x = Dropout(0.5)(x)
    outputs = Dense(output_shape, activation='softmax')(x)
    model = Model(inputs=pretrained_model.input, outputs=outputs)
    return model
Exemplo n.º 4
0
    def __init__(self,
                 num_classes,
                 scales=[0.05, 0.1, 0.2, 0.4, 0.6, 0.8],
                 base_layers=[
                     'conv4_block4_out', 'conv4_block5_out',
                     'conv4_block6_out', 'conv5_block1_out',
                     'conv5_block2_out', 'conv5_block3_out'
                 ],
                 aspect_ratios=[0.5, 1.0, 2.0],
                 variances=[0.1, 0.1, 0.2, 0.2],
                 weight_decay=5e-4,
                 **kwargs):
        super().__init__(**kwargs)

        self.num_classes = num_classes

        if len(scales) != len(base_layers):
            raise Exception(
                'You need to provide one scale for each base layer.')
        self.scales = scales
        self.base_layers = base_layers

        self.aspect_ratios = aspect_ratios
        self.boxes_per_cell = len(aspect_ratios)

        if len(variances) != 4:
            raise Exception('You need to provide exactly 4 variance values \
            (one for each bounding box parameter).')
        self.variances = variances

        backbone = resnet50.ResNet50(include_top=False, weights='imagenet')

        self.get_base_features = tf.keras.Model(
            inputs=backbone.layers[0].input,
            outputs=[
                backbone.get_layer(layer_name).output
                for layer_name in self.base_layers
            ])

        self.conv_cls = []
        self.conv_loc = []

        for idx in range(len(self.scales)):
            self.conv_cls.append(
                layers.Conv2D(
                    filters=self.boxes_per_cell * self.num_classes,
                    kernel_size=3,
                    padding='same',
                    kernel_regularizer=tf.keras.regularizers.l2(weight_decay),
                    name='conv_cls_{}'.format(idx + 1)))
            self.conv_loc.append(
                layers.Conv2D(
                    filters=self.boxes_per_cell * 4,
                    kernel_size=3,
                    padding='same',
                    kernel_regularizer=tf.keras.regularizers.l2(weight_decay),
                    name='conv_loc_{}'.format(idx + 1)))
Exemplo n.º 5
0
def ResNetSegmentation(n_classes, input_height=224, input_width=224):

    img_input = Input(shape=(input_height, input_width, 3))

    resnet_model = resnet50.ResNet50(input_tensor=img_input,
                                     weights="imagenet")

    o = resnet_model.get_layer('activation_39').output

    o = (ZeroPadding2D((1, 1), data_format='channels_last'))(o)
    o = (Conv2D(512, (3, 3),
                padding='valid',
                data_format='channels_last',
                kernel_initializer='he_uniform'))(o)
    o = (BatchNormalization())(o)

    o = (UpSampling2D((2, 2), data_format='channels_last'))(o)
    o = (ZeroPadding2D((1, 1), data_format='channels_last'))(o)
    o = (Conv2D(512, (3, 3),
                padding='valid',
                data_format='channels_last',
                kernel_initializer='he_uniform'))(o)
    o = (BatchNormalization())(o)

    o = (UpSampling2D((2, 2), data_format='channels_last'))(o)
    o = (ZeroPadding2D((1, 1), data_format='channels_last'))(o)
    o = (Conv2D(256, (3, 3),
                padding='valid',
                data_format='channels_last',
                kernel_initializer='he_uniform'))(o)
    o = (BatchNormalization())(o)

    o = (UpSampling2D((2, 2), data_format='channels_last'))(o)
    o = (ZeroPadding2D((1, 1), data_format='channels_last'))(o)
    o = (Conv2D(128, (3, 3),
                padding='valid',
                data_format='channels_last',
                kernel_initializer='he_uniform'))(o)
    o = (BatchNormalization())(o)

    o = (UpSampling2D((2, 2), data_format='channels_last'))(o)
    o = (ZeroPadding2D((1, 1), data_format='channels_last'))(o)
    o = (Conv2D(64, (3, 3),
                padding='valid',
                data_format='channels_last',
                kernel_initializer='he_uniform'))(o)
    o = (BatchNormalization())(o)

    o = Conv2D(n_classes, (3, 3),
               padding='same',
               data_format='channels_last',
               kernel_initializer='he_uniform')(o)
    o = (Activation('softmax'))(o)

    model = Model(img_input, o)

    return model
Exemplo n.º 6
0
def simple_model(config):
    model = resnet50.ResNet50(weights=None)
    # model = Sequential([Dense(10, input_shape=(1, )), Dense(1)])

    model.compile(optimizer="Adam",
                  loss="categorical_crossentropy",
                  metrics=["categorical_crossentropy"])

    return model
Exemplo n.º 7
0
def get_resnet50(img_shape=(80,80,3)):
    '''
    Arguments: input image shape
    default = (80,80)
    '''
    image_input = keras.Input(shape=img_shape) #Define the input layer in our embedding_extraction_model
    temp_model = resnet50.ResNet50(weights='imagenet',include_top=False)(image_input) #Set resnet50 to temp_model
    image_embedding_output_layer = keras.layers.GlobalAveragePooling2D()(temp_model) 
    img_embedding_extractor_model = Model(inputs=image_input, outputs=image_embedding_output_layer) #Create a new model and add GlobalAveragePooling2D layer to it.
    return img_embedding_extractor_model
Exemplo n.º 8
0
    def __init__(self, classes=6, units=512, dropout_prob=0.5, l1=0.01, l2=0.01):
        self.base_model = resnet50.ResNet50(
            weights = 'imagenet', 
            input_shape=(224,224,3), 
            include_top = False,
            pooling = 'avg')

        x = Dense(units, activation='relu')(self.base_model.output)
        x = Dropout(dropout_prob)(x)
        y = Dense(classes, activation='softmax', kernel_regularizer=l1_l2(l1=l1, l2=l2))(x)
        self.model = Model(inputs=self.base_model.input, outputs=y)
    def __init__(self, base_path='.'):
        """
        Initializes main variables

        @param base_path: string pointing to the path where the images are located. If no string is indicated, the
            current directory will be considered
        """
        super(ResNet50FeatureExtractor, self).__init__(base_path)
        self.model = resnet50.ResNet50(weights='imagenet',
                                       include_top=False,
                                       pooling='avg')
Exemplo n.º 10
0
    def __init__(self, latent_dim, input_shape, rotation_ranges):
        super(RealEncoder, self).__init__()

        self.resnet = resnet50.ResNet50(weights="imagenet", include_top=False, input_shape=input_shape, pooling="avg")

        self.resnet_feature_dim = np.prod(self.resnet.output.shape[1:])
        self.rotation_regressor = keras.layers.Dense(3, input_shape=(self.resnet_feature_dim,), activation=tf.nn.tanh)

        self.feature_to_latent_mlp = keras.layers.Dense(latent_dim, input_shape=(self.resnet_feature_dim,))

        self.rotation_range_multiplier = np.array([rotation_ranges[0][1], rotation_ranges[1][1], rotation_ranges[2][1]])
        self.rotation_range_multiplier = np.pi * self.rotation_range_multiplier / 180.0
Exemplo n.º 11
0
def build_image_model() -> tf.keras.Model:
    preprocess_input = resnet50.preprocess_input
    resnet_model = resnet50.ResNet50(weights="imagenet", include_top=False)
    global_max_pooling = tf.keras.layers.GlobalAveragePooling2D()

    image_inputs = tf.keras.Input(shape=IMAGE_NET_IMAGE_SIZE +
                                  (IMAGE_NET_CHANNELS, ),
                                  name="image")
    x = preprocess_input(image_inputs)
    x = resnet_model(x, training=False)
    image_embedding = global_max_pooling(x)
    return tf.keras.Model(image_inputs, image_embedding, name="ResNet50")
Exemplo n.º 12
0
def createResNetwork():
    model = resnet50.ResNet50(weights='imagenet')
    for layer in model.layers:
      layer._name = 'res_'+layer._name

    x = Flatten()(model.get_layer('res_avg_pool').output)
    x = Dropout(0.3)(x)
    x = Dense(256, name='res_weights', activation=LeakyReLU(alpha=0.3))(x)
    x = Dense(4, activation='softmax')(x)

    model = Model(model.input, x)
    return model
Exemplo n.º 13
0
def init():
    # Instantiate Resnet50 featurizer
    global featurizer
    featurizer = resnet50.ResNet50(weights='imagenet',
                                   input_shape=(224, 224, 3),
                                   include_top=False,
                                   pooling='avg')

    # Load the model
    global model
    # retreive the path to the model file using the model name
    model_path = Model.get_model_path('aerial_classifier')
    model = tf.keras.models.load_model(model_path)
Exemplo n.º 14
0
def init():
    # Instantiate ResNet50 featurizer
    global featurizer
    featurizer = resnet50.ResNet50(weights='imagenet',
                                   input_shape=(224, 224, 3),
                                   include_top=False,
                                   pooling='avg')

    # Load the model
    global model
    # retreive the path to the model file using the model name
    model_path = Model.get_model_path(model_name='AutoMLac5fd8f37best')
    model = joblib.load(model_path)
Exemplo n.º 15
0
 def __init__(self):
     
     model_path = '../model/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5'
     if path.exists(model_path) == False:
         print('Downloading the model weights...')
         url = 'https://github.com/keras-team/keras-applications/releases/download/resnet/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5'
         with requests.get(url, stream=True) as r:
             with open(model_path, 'wb') as f:
                 shutil.copyfileobj(r.raw, f)
         
     print('Loading the model weights from \n', model_path)
     
     model = resnet50.ResNet50(include_top=False, weights=model_path, pooling='avg')
     self.embedding_model = Model(inputs=model.input, 
                                  outputs=model.output)
Exemplo n.º 16
0
    def _get_model(self, model_name):
        if model_name == 'resnet':
            self.preprocess_input = resnet50.preprocess_input

            base_model = resnet50.ResNet50(include_top=False,
                                           input_shape=self.target_shape)

            for layer in base_model.layers:
                layer.trainable = False

            model = Sequential([base_model, GlobalAveragePooling2D()])

            return model

        return None
Exemplo n.º 17
0
 def __init__(self, model_name='resnet'):
     if (model_name == 'mobilenet'):
         print("Using MobileNetV2 to vectorize images")
         model = mobilenet_v2.MobileNetV2(weights='imagenet')
         layer_name = 'global_average_pooling2d'
         self.preprocess_fn = mobilenet_v2.preprocess_input
     else:
         print("Using ResNet50 to vectorize images")
         model = resnet50.ResNet50(weights='imagenet')
         layer_name = 'avg_pool'
         self.preprocess_fn = resnet50.preprocess_input
     o = model.get_layer(layer_name).output
     self.vec_len = o.shape[1]
     self.intermediate_layer_model = Model(inputs=model.input, 
                                           outputs=o)
Exemplo n.º 18
0
    def compute_repr_resnet(self, x_train: np.ndarray, startTime: float):
        from tensorflow.keras.applications import resnet50
        from tensorflow import stack, concat, squeeze
        from tensorflow.compat.v1.image import resize_images
        from tensorflow.keras.models import Model

        # resize function
        target_size = 224

        def encode_samples(x_sample):
            batch_size = 1000
            no_samples = x_sample.shape[0]
            index = 0
            x_features = []
            while index < no_samples:
                print("encoding sample %d at time %f" %
                      (index, (time.time() - startTime)))
                end = min(index + batch_size, no_samples)
                x = x_sample[index:end]
                if x.shape[
                        -1] == 1:  # add 2 dimensions to make rgb sample out of black-white-sample
                    x = concat((x, ) * 3, axis=-1)
                x = squeeze(x)
                x = resize_images(x, size=(target_size, target_size))
                x = resnet50.preprocess_input(x)
                x_feat = intermediate_layer_model.predict(x, workers=16)
                x_features += [x_feat]
                index += batch_size
            x_feat = concat(x_features, axis=0)
            return x_feat

        # encode function
        model = resnet50.ResNet50(weights='imagenet',
                                  input_shape=(target_size, target_size, 3))
        layer_name = 'avg_pool'
        intermediate_layer_model = Model(
            inputs=model.inputs, outputs=model.get_layer(layer_name).output)

        # intermediate_layer_model.compile(loss="mse",optimizer="sgd")

        def encode(x_samples):
            encoded_samples = encode_samples(x_samples)
            return encoded_samples

        # encode training data
        x_train_repr_1d = encode(x_train)

        return x_train_repr_1d
Exemplo n.º 19
0
def create_bottleneck_features():
    # Configure input directories
    train_images_dir = os.path.join(FLAGS.input_data_dir, 'train')
    valid_images_dir = os.path.join(FLAGS.input_data_dir, 'valid')

    # Create generators for training and validation data
    train_generator = ImageGenerator(train_images_dir,
                                     resnet50.preprocess_input)
    valid_generator = ImageGenerator(valid_images_dir,
                                     resnet50.preprocess_input)

    # Create a featurizer
    featurizer = resnet50.ResNet50(weights='imagenet',
                                   input_shape=(224, 224, 3),
                                   include_top=False,
                                   pooling='avg')

    # Generate training bottleneck features
    print("Generating training bottleneck features")
    features = featurizer.predict_generator(train_generator, verbose=1)
    labels = train_generator.get_labels()

    # Save training dataset to HDF5 file
    filename = 'aerial_bottleneck_train.h5'
    output_file = os.path.join(FLAGS.output_data_dir, filename)
    print("Saving training features to {}".format(output_file))
    print("   Training features: ", features.shape)
    print("   Training labels: ", labels.shape)
    with h5py.File(output_file, "w") as hfile:
        features_dset = hfile.create_dataset('features', data=features)
        labels_dset = hfile.create_dataset('labels', data=labels)

    # Generate validation bottleneck features
    print("Generating validation bottleneck features")
    features = featurizer.predict_generator(valid_generator, verbose=1)
    labels = valid_generator.get_labels()

    # Save validation dataset to HDF5 file
    filename = 'aerial_bottleneck_valid.h5'
    output_file = os.path.join(FLAGS.output_data_dir, filename)
    print("Saving validation features to {}".format(output_file))
    print("   Validation features: ", features.shape)
    print("   Validation labels: ", labels.shape)
    with h5py.File(output_file, "w") as hfile:
        features_dset = hfile.create_dataset('features', data=features)
        labels_dset = hfile.create_dataset('labels', data=labels)

    print("Done")
def build_model():
    input_tensor_shape = (None, 224, 224, 3)

    input_img = tf.compat.v1.placeholder(tf.float32,
                                         input_tensor_shape,
                                         name='input_img')

    x = resnet50.preprocess_input(input_img)

    resnet = resnet50.ResNet50(weights='imagenet',
                               include_top=False,
                               pooling='avg')

    output_tensor = resnet(x)

    return input_img, output_tensor
Exemplo n.º 21
0
def predict(fname):
    input_shape = (224, 224, 3)

    # load and resize image
    img = image.load_img(fname, target_size=input_shape[:2])
    x = image.img_to_array(img)

    # preprocess image

    # make a batch
    import numpy as np
    x = np.expand_dims(x, axis=0)
    print(x.shape)

    # apply the preprocessing function of resnet50
    img_array = resnet50.preprocess_input(x)

    model = resnet50.ResNet50(weights='imagenet', input_shape=input_shape)
    preds = model.predict(x)
    return resnet50.decode_predictions(preds)
Exemplo n.º 22
0
def init():
    try:

        # Create ResNet50 featurizer
        global featurizer

        featurizer = resnet50.ResNet50(weights='imagenet',
                                       input_shape=(224, 224, 3),
                                       include_top=False,
                                       pooling='avg')

        # Load top model
        global model

        model_name = 'aerial_classifier'
        model_path = Model.get_model_path(model_name)
        model = tf.keras.models.load_model(model_path)

    except Exception as e:
        print('Exception during init: ', str(e))
Exemplo n.º 23
0
def unet_resnet(input_shape):
    resnet_base = resnet50.ResNet50(input_shape=input_shape, include_top=False)

    for layer in resnet_base.layers:
        layer.trainable = True

    conv1 = resnet_base.get_layer("activation").output
    conv2 = resnet_base.get_layer("activation_9").output
    conv3 = resnet_base.get_layer("activation_21").output
    conv4 = resnet_base.get_layer("activation_39").output
    conv5 = resnet_base.get_layer("activation_48").output

    conv6 = _merge_block(conv5, conv4, 256)
    conv7 = _merge_block(conv6, conv3, 192)
    conv8 = _merge_block(conv7, conv2, 128)
    conv9 = _merge_block(conv8, conv1, 64)

    conv10 = _merge_block(conv9, resnet_base.input, 32)
    x = _end_block(conv10, spatial_dropout=0.25)
    model = Model(resnet_base.input, x)
    return model, "resnet"
Exemplo n.º 24
0
def train_evaluate(run):
   
    # Create bottleneck featurs
    train_images_dir = os.path.join(FLAGS.data_folder, 'train')
    valid_images_dir = os.path.join(FLAGS.data_folder, 'valid')

    train_generator = ImageGenerator(train_images_dir, resnet50.preprocess_input)
    valid_generator = ImageGenerator(valid_images_dir, resnet50.preprocess_input)

    featurizer = resnet50.ResNet50(
                weights = 'imagenet', 
                input_shape=(224,224,3), 
                include_top = False,
                pooling = 'avg')

    print("Generating bottleneck features")
    train_features = featurizer.predict_generator(train_generator, verbose=1)
    train_labels = train_generator.get_labels()

    valid_features = featurizer.predict_generator(valid_generator, verbose=1)
    valid_labels = valid_generator.get_labels()
    
    # Create a classifier
    model = fcn_classifier(input_shape=(2048,), units=FLAGS.units, l1=FLAGS.l1, l2=FLAGS.l2)
    
    # Start training
    print("Starting training")
    model.fit(train_features, train_labels,
          batch_size=64,
          epochs=20,
          shuffle=True,
          validation_data=(valid_features, valid_labels))
          
    # Save the trained model to outp'uts which is a standard folder expected by AML
    print("Training completed.")
    os.makedirs('outputs', exist_ok=True)
    model_file = os.path.join(FLAGS.save_model_dir, 'model.hd5')
    print("Saving model to: {0}".format(model_file))
    model.save(model_file)
Exemplo n.º 25
0
def get_model(model_name):

    if model_name == "vgg16":
        return vgg16.VGG16(weights='imagenet'
                           ), vgg16.decode_predictions, vgg16.preprocess_input
    elif model_name == "vgg19":
        return vgg19.VGG19(weights='imagenet'
                           ), vgg19.decode_predictions, vgg19.preprocess_input
    elif model_name == "resnet50":
        return resnet50.ResNet50(
            weights='imagenet'
        ), resnet50.decode_predictions, resnet50.preprocess_input
    elif model_name == "resnet101":
        return ResNet101(weights='imagenet'
                         ), resnet.decode_predictions, resnet.preprocess_input
    elif model_name == "mobilenet":
        return mobilenet.MobileNet(
            weights='imagenet'
        ), mobilenet.decode_predictions, mobilenet.preprocess_input
    elif model_name == "densenet":
        return densenet.DenseNet121(
            weights='imagenet'
        ), densenet.decode_predictions, densenet.preprocess_input
def custom_model():
    base_model = resnet50.ResNet50(input_shape=(224, 224, 3),
                                   include_top=False,
                                   weights='imagenet',
                                   pooling='avg')

    x = base_model.output

    x = keras.layers.Dense(1024, activation='relu')(x)
    x = keras.layers.Dropout(0.5)(x)

    out = keras.layers.Dense(2, activation='softmax', name='output_layer')(x)
    custom_resnet_model = keras.models.Model(inputs=base_model.input,
                                             outputs=out)

    for layer in base_model.layers:
        layer.trainable = False

    custom_resnet_model.compile(
        optimizer=tf.keras.optimizers.Adam(),
        loss=tf.keras.losses.sparse_categorical_crossentropy,
        metrics=['accuracy'])

    return custom_resnet_model
Exemplo n.º 27
0
import numpy as np
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications import resnet50

# Load Keras' ResNet50 model that was pre-trained against the ImageNet database
model = resnet50.ResNet50()

# Load the image file, resizing it to 224x224 pixels (required by this model)
# img = image.load_img("bay.jpg", target_size=(224,224))
# img = image.load_img("bonk.jpg", target_size=(224,224))
img = image.load_img("froggy.jpg", target_size=(224, 224))

# Convert the image to a numpy array
x = image.img_to_array(img)

# Add a forth dimension since Keras expects a list of images
x = np.expand_dims(x, axis=0)

# Scale the input image to the range used in the trained network
x = resnet50.preprocess_input(x)

# Run the image through the deep neural network to make a prediction
predictions = model.predict(x)

# Look up the names of the predicted classes. Index zero is the results for the first image.
predicted_classes = resnet50.decode_predictions(predictions, top=10)

print("This is an image of:")

for imagenet_id, name, likelihood in predicted_classes[0]:
    print(" - {}: {:2f} likelihood".format(name, likelihood))
Exemplo n.º 28
0
    """ trains the model and save it """
    batch_size = 32
    num_classes = 10
    epochs = 20

    (x_train, y_train), (x_test, y_test) = K.datasets.cifar10.load_data()
    x_train, y_train = preprocess_data(x_train, y_train)
    x_test, y_test = preprocess_data(x_test, y_test)

    """Setting up the model"""

    original_dim = (32, 32, 3)
    target_size = (224, 224)

    resN = resnet50.ResNet50(include_top=False, weights='imagenet',
                             input_shape=original_dim,
                             pooling='max')
    # Defrost last layer of resnet50
    for layer in resN.layers[:-32]:
        layer.trainable = False

    # Concatenate the resnet with a modified top layer
    res_model = K.Sequential()
    res_model.add(K.layers.Lambda(lambda image:
                                  tf.image.resize(image, target_size)))
    res_model.add(resN)
    res_model.add(K.layers.Dense(512, activation='relu'))
    res_model.add(K.layers.Dropout(0.3))
    res_model.add(K.layers.Dense(512, activation='relu'))
    res_model.add(K.layers.Dropout(0.5))
    res_model.add(K.layers.Dense(10, activation='softmax'))
Exemplo n.º 29
0
    image_size = tuple((224, 224))
    image_bytes = cv2.resize(image_bytes, image_size)
    feature_table = {'resnet': None}
    # feature_table['vgg'] = vgg(image_bytes)
    feature_table['resnet'] = Resnet50(image_bytes)
    return feature_table


# def cluster():
#     kmeans = KMeans(n_clusters=2, random_state=0).fit(array)

if __name__ == "__main__":
    # vgg_model = tf.keras.applications.VGG16(weights='imagenet')
    # vgg_extractor = tf.keras.models.Model(inputs=vgg_model.input, outputs=vgg_model.get_layer("fc2").output)
    resnet50_extractor = resnet50.ResNet50(weights='imagenet',
                                           include_top=False,
                                           input_shape=(224, 224, 3))
    img_dir_path = input('[INPUT] image dir path : ')
    features = {'img': [], 'resnet': [], 'cluster': []}
    pics_num = os.listdir(img_dir_path)
    bar = progressbar.ProgressBar(maxval=len(pics_num), \
    widgets=[progressbar.Bar('=', '[', ']'), ' ', progressbar.Percentage()])
    bar.start()
    for i, img_path in enumerate(pics_num):
        img_path = img_dir_path + img_path
        with open(img_path, 'rb') as f:
            img_bytes = f.read()
        Image = cv2.imdecode(np.fromstring(img_bytes, np.uint8),
                             cv2.IMREAD_UNCHANGED)
        Image = Image[:, :, :3]
        single_feature_table = feature_table_creator(Image)
Exemplo n.º 30
0
from tensorflow.keras import models
from tensorflow.keras import optimizers
from tensorflow.keras.applications import vgg16, inception_v3, resnet50, mobilenet
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.preprocessing.image import img_to_array, load_img
from tensorflow.keras.callbacks import TensorBoard

### ImageNet Large Scale Visual Recognition Challenge (ILSVRC) (1.2 M images, 1000 classes)
# Load the VGG model
vgg_model = vgg16.VGG16(weights='imagenet')

# Load the inception_V3 model
inception_model = inception_v3.InceptionV3(weights='imagenet')

# Load the ResNet50 model
resnet_model = resnet50.ResNet50(weights='imagenet')

# Load the MobileNet model
mobilenet_model = mobilenet.MobileNet(weights='imagenet')

### Load the image and convert its format to a 4-dimensional Tensor as an input of the form
# (batchsize, height, width, channels) requested by the Network.

filename = 'C:/Users/Theo/PycharmProjects/BMDATA/TP1/data/train/cat.1.jpg'
# Load an image in a PIL format
original = load_img(filename, target_size=(224, 224))
numpy_image = img_to_array(original)
# We add the extra dimension to the axis 0
image_batch = np.expand_dims(numpy_image, axis=0)
print('image batch size', image_batch.shape)
plt.imshow(np.uint8(image_batch[0]))