def _get_backbone_model(self):
     backbone_model = None
     if self.backbone_name == 'resnet18':
         backbone_model = ResNet18(input_shape=self.input_shape, include_top=False, weights='imagenet')
     if self.backbone_name == 'resnet34':
         backbone_model = ResNet34(input_shape=self.input_shape, include_top=False, weights='imagenet')
     elif self.backbone_name == 'mobilenetv2':
         backbone_model = MobileNetV2(input_shape=self.input_shape, include_top=False, weights='imagenet')
     assert backbone_model is not None, f'backbone should be one of {list(_FEATURE_LAYERS.keys())[:3]}'
     return backbone_model
 def _get_backbone_model(self):
     backbone_model = None
     if self.backbone_name == 'resnet18':
         backbone_model = ResNet18(input_shape=self.input_shape, include_top=False, weights='imagenet')
     if self.backbone_name == 'resnet34':
         backbone_model = ResNet34(input_shape=self.input_shape, include_top=False, weights='imagenet')
     elif self.backbone_name == 'mobilenetv2':
         backbone_model = MobileNetV2(input_shape=self.input_shape, include_top=False, weights='imagenet')
     elif self.backbone_name == 'efficientnetb0':
         backbone_model = EfficientNetB0(input_shape=self.input_shape, include_top=False, weights='imagenet')
     elif self.backbone_name == 'efficientnetb1':
         backbone_model = EfficientNetB1(input_shape=self.input_shape, include_top=False, weights='imagenet')
     elif self.backbone_name == 'efficientnetb3':
         backbone_model = EfficientNetB3(input_shape=self.input_shape, include_top=False, weights='imagenet')
     elif self.backbone_name == 'efficientnetb5':
         backbone_model = EfficientNetB5(input_shape=self.input_shape, include_top=False, weights='imagenet')
     elif self.backbone_name == 'densenet':
         backbone_model = DenseNet121(input_shape=self.input_shape, include_top=False, weights='imagenet')
     return backbone_model
Ejemplo n.º 3
0
def BravoNet(input_shape, version="", weights=None, quiet=False):
    model = Sequential()
    model.name = append_version("BravoNet", version)

    base_model = ResNet18(weights=weights,
                          include_top=False,
                          classes=200,
                          input_shape=input_shape)

    if not quiet:
        base_model.summary()

    model.add(base_model)
    model.add(GlobalAveragePooling2D())

    model.add(Dense(200, activation="softmax"))

    if not quiet:
        model.summary()

    return model
Ejemplo n.º 4
0
n_classes = 36
y = np.eye(n_classes)[np.asarray(y)]

X = np.asarray(X)

X = preprocess_input(X)

X, y = shuffle(X, y, random_state=0)
X_train, X_test, y_train, y_test = train_test_split(X,
                                                    y,
                                                    test_size=0.20,
                                                    random_state=5)

# build model
base_model = ResNet18(input_shape=(224, 224, 3),
                      weights='imagenet',
                      include_top=False)
x = keras.layers.GlobalAveragePooling2D()(base_model.output)
output = keras.layers.Dense(n_classes, activation='softmax')(x)
model = keras.models.Model(inputs=[base_model.input], outputs=[output])
model.summary()

# train
model.compile(optimizer='SGD',
              loss='categorical_crossentropy',
              metrics=['accuracy'])

BATCH_SIZE_TRAINING = 100
BATCH_SIZE_VALIDATION = 100

NUM_EPOCHS = 5
Ejemplo n.º 5
0
X_train -= mean_image

X_test -= mean_image

X_train /= 128.

X_test /= 128.

lr_reducer = ReduceLROnPlateau(factor=np.sqrt(0.1), cooldown=0, patience=5, min_lr=0.5e-6)

early_stopper = EarlyStopping(min_delta=0.001, patience=10)

csv_logger = CSVLogger('resnet18_cifar10.csv')

base_model = ResNet18((32, 32, 3), weights='imagenet', include_top=True, classes=1000)

base_model.load_weights('resnet18_imagenet_1000.h5')
#base_model.layers.pop()

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


conv2 = Conv2D(10, 1, activation='relu')(base_model.layers[-4].output)
gap = GlobalAveragePooling2D()(conv2)
activation = Activation('softmax')(gap)

model = keras.models.Model(inputs=[base_model.input], outputs=[activation])
print(model.summary())
Ejemplo n.º 6
0
    batch_size=BATCH_SIZE_TRAINING,
    class_mode='categorical',
    subset='training')

validation_generator = data_generator.flow_from_directory(
    '../dataset/train/full_data',
    target_size=(image_height, image_width),
    batch_size=BATCH_SIZE_VALIDATION,
    class_mode='categorical',
    subset='validation')

n_classes = 5

# build model
base_model = ResNet18(input_shape=(image_height, image_width, 3),
                      weights='imagenet',
                      include_top=False)
x = keras.layers.GlobalAveragePooling2D()(base_model.output)
output = keras.layers.Dense(n_classes, activation='softmax')(x)
model = keras.models.Model(inputs=[base_model.input], outputs=[output])

# print(model.layers[86])
for i in range(0, 86):
    model.layers[i].trainable = False

model.summary()
# print(model.layers[1].trainable)
# train
model.compile(optimizer='SGD',
              loss='categorical_crossentropy',
              metrics=['accuracy'])
Ejemplo n.º 7
0
def outer_product(x):
    return keras.backend.batch_dot(x[0], x[1],
                                   axes=[1, 1]) / x[0].get_shape().as_list()[1]


def signed_sqrt(x):
    return keras.backend.sign(x) * keras.backend.sqrt(
        keras.backend.abs(x) + 1e-9)


def L2_norm(x, axis=-1):
    return keras.backend.l2_normalize(x, axis=axis)


model = ResNet18(input_shape=(224, 224, 3),
                 weights='imagenet',
                 classes=11,
                 include_top=False)

x_detector = model.layers[-1].output
shape_detector = model.layers[-1].output_shape
shape_extractor = shape_detector

#print(shape_extractor)

x_extractor = x_detector
#print(x_extractor)

x_detector = keras.layers.Reshape(
    [shape_detector[1] * shape_detector[2], shape_detector[-1]])(x_detector)

#print("After reshape",x_detector.shape)
Ejemplo n.º 8
0
	def CreateResnet(self):
		self.resnet_M = ResNet18((512, 512, 3), weights='imagenet', classes = 1000)
Ejemplo n.º 9
0
test_datagen = image.ImageDataGenerator(rotation_range=40,
                                        width_shift_range=0.2,
                                        height_shift_range=0.2,
                                        shear_range=0.2,
                                        zoom_range=0.2,
                                        horizontal_flip=True,
                                        fill_mode='nearest')

it_train = train_datagen.flow(X_train, y_train)
it_test = test_datagen.flow(X_test, y_test)

print("Training Input Shape: ", X_train.shape, y_train.shape,
      " Test Input Shape: ", X_test.shape, y_test.shape)

base_model = ResNet18((img_size, img_size, 3),
                      weights='imagenet',
                      classes=num_classes,
                      include_top=False)
avg_pool = keras.layers.GlobalAveragePooling2D()(base_model.output)
output = keras.layers.Dense(num_classes, activation='softmax')(avg_pool)
model = Model([base_model.input], output)

if args.load:
    model.load_weights(args.load)

model.compile(loss='categorical_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])
# model.summary()

t = time.time()
Ejemplo n.º 10
0
#!/usr/bin/env python
# coding: utf-8

# In[95]:

from classification_models.resnet import ResNet18, preprocess_input
import numpy as np
import pandas as pd
import string as str
from skimage.transform import resize
from keras.applications.imagenet_utils import decode_predictions
from skimage.io import imread, imshow
import os
model = ResNet18((224, 224, 3), weights='imagenet')

# In[96]:


def pre_process_pic(x):
    x = resize(x, (224, 224)) * 255
    x = preprocess_input(x)
    x = np.expand_dims(x, 0)
    return x


def judge_label(predicts, target_list):
    predicts = predicts[0]
    for predict in predicts:
        if (predict[0] in target_list):
            return True
    return False
import numpy as np
import cv2
from keras import models
from keras import layers
from keras import optimizers
from PIL import Image
from keras import Model
import pickle
import os
from keras.applications.imagenet_utils import decode_predictions
from keras.preprocessing.image import ImageDataGenerator
from classification_models.resnet import ResNet18, preprocess_input

# load model
model = ResNet18(input_shape=(224, 224, 3), weights='imagenet', classes=1000)
layer_name = 'relu1'
intermidiate_layer_model = Model(inputs=model.input,
                                 outputs=model.get_layer(layer_name).output)

#print(model.summary())

folders = '/home/aman9875/Documents/assignment2/fine_data'
train_features = []
train_labels = []
validation_features = []
validation_labels = []

t = 0
for folder in os.listdir(folders):
    print(folder)
    for sub_folder in os.listdir(os.path.join(folders, folder)):
Ejemplo n.º 12
0
def resnet_retinanet(num_classes,
                     backbone='resnet50',
                     inputs=None,
                     modifier=None,
                     **kwargs):
    """ Constructs a retinanet model using a resnet backbone.

    Args
        num_classes: Number of classes to predict.
        backbone: Which backbone to use (one of ('resnet50', 'resnet101', 'resnet152')).
        inputs: The inputs to the network (defaults to a Tensor of shape (None, None, 3)).
        modifier: A function handler which can modify the backbone before using it in retinanet (this can be used to freeze backbone layers for example).

    Returns
        RetinaNet model with a ResNet backbone.
    """
    # choose default input
    if inputs is None:
        if keras.backend.image_data_format() == 'channels_first':
            inputs = keras.layers.Input(shape=(3, None, None))
        else:
            inputs = keras.layers.Input(shape=(None, None, 3))

    # create the resnet backbone
    if backbone == 'resnet50':
        resnet = keras_resnet.models.ResNet50(inputs,
                                              include_top=False,
                                              freeze_bn=True)
    elif backbone == 'resnet101':
        resnet = keras_resnet.models.ResNet101(inputs,
                                               include_top=False,
                                               freeze_bn=True)
    elif backbone == 'resnet152':
        resnet = keras_resnet.models.ResNet152(inputs,
                                               include_top=False,
                                               freeze_bn=True)
    elif backbone == 'resnet36':
        resnet = ResNet34(None,
                          input_tensor=inputs,
                          weights=None,
                          include_top=False)
        layer_names = ['stage3_unit1_relu1', 'stage4_unit1_relu1', 'relu1']
        layer_outputs = [resnet.get_layer(name).output for name in layer_names]
        resnet = keras.models.Model(inputs=inputs,
                                    outputs=layer_outputs,
                                    name=resnet.name)
        return retinanet.retinanet(inputs=inputs,
                                   num_classes=num_classes,
                                   backbone_layers=resnet.outputs,
                                   **kwargs)

    elif backbone == 'resnet18':
        resnet = ResNet18(None,
                          input_tensor=inputs,
                          weights=None,
                          include_top=False)
        layer_names = ['stage3_unit1_relu1', 'stage4_unit1_relu1', 'relu1']
        layer_outputs = [resnet.get_layer(name).output for name in layer_names]
        resnet = keras.models.Model(inputs=inputs,
                                    outputs=layer_outputs,
                                    name=resnet.name)
        return retinanet.retinanet(inputs=inputs,
                                   num_classes=num_classes,
                                   backbone_layers=resnet.outputs,
                                   **kwargs)

    else:
        raise ValueError('Backbone (\'{}\') is invalid.'.format(backbone))

    # invoke modifier if given
    if modifier:
        resnet = modifier(resnet)

    # create the full model
    return retinanet.retinanet(inputs=inputs,
                               num_classes=num_classes,
                               backbone_layers=resnet.outputs[1:],
                               **kwargs)