Beispiel #1
0
 def set_base_model(self):
     stringWeights = None
     if (self.imageNetWeights):
         print("Using imagenet weights...")
         stringWeights = "imagenet"
     else:
         print("Using random initialization of the weights...")
         stringWeights = None
     if (self.base_model_type == "vgg16"):
         self.base_model = keras.applications.vgg16.VGG16(
             include_top=False,
             weights=stringWeights,
             input_shape=self.input_shape,
             pooling='max')
     elif (self.base_model_type == "resnet50"):
         self.base_model = ResNet50(include_top=False,
                                    weights=stringWeights,
                                    input_shape=self.input_shape,
                                    pooling='max')
     elif (self.base_model_type == "resnet101"):
         self.base_model = ResNet101(include_top=False,
                                     weights=stringWeights,
                                     input_shape=self.input_shape,
                                     pooling='max')
     elif (self.base_model_type == "resnet152"):
         self.base_model = ResNet152(include_top=False,
                                     weights=stringWeights,
                                     input_shape=self.input_shape,
                                     pooling='max')
     elif (self.base_model_type == "inceptionv3"):
         self.base_model = keras.applications.inception_v3.InceptionV3(
             include_top=False, weights=stringWeights, pooling='max')
 def create(self, res: Resolution, classes: Optional[int]) -> Model:
     """
     Creates a keras.models.Model object.
     """
     if type(classes) is not int:
         raise ValueError(classes)
     return ResNet101(
         include_top=True,
         weights=None,
         input_tensor=Input(res.hwc()),
         classes=classes,
     )
def get_model(model_name,
              input_tensor=Input(shape=(96, 96, 3)),
              num_class=2):  ##Modified by Dooman
    inputs = Input(input_shape)

    if model_name == "Xception":
        base_model = Xception(include_top=False, input_shape=input_shape)
    elif model_name == "ResNet50":
        base_model = ResNet50(include_top=False, input_shape=input_shape)
    elif model_name == "ResNet101":
        base_model = ResNet101(include_top=False, input_shape=input_shape)
    elif model_name == "InceptionV3":
        base_model = InceptionV3(include_top=False, input_shape=input_shape)
    elif model_name == "InceptionResNetV2":
        base_model = InceptionResNetV2(include_top=False,
                                       input_shape=input_shape)
    elif model_name == "DenseNet201":
        base_model = DenseNet201(include_top=False, input_shape=input_shape)
    elif model_name == "NASNetMobile":
        base_model = NASNetMobile(
            include_top=False, input_tensor=input_tensor)  ##Modified by Dooman
    elif model_name == "NASNetLarge":
        base_model = NASNetLarge(include_top=False, input_tensor=input_tensor)
    if model_name == "VGG16":
        base_model = VGG16(input_shape=IMAGE_SIZE + [3],
                           weights='imagenet',
                           include_top=False)

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

    x = base_model(inputs)

    output1 = GlobalMaxPooling2D()(x)
    output2 = GlobalAveragePooling2D()(x)
    output3 = Flatten()(x)

    outputs = Concatenate(axis=-1)([output1, output2, output3])

    outputs = Dropout(0.5)(outputs)
    outputs = BatchNormalization()(outputs)

    if num_class > 1:
        outputs = Dense(num_class, activation="softmax")(outputs)
    else:
        outputs = Dense(1, activation="sigmoid")(outputs)

    model = Model(inputs, outputs)

    model.summary()

    return model
 def create(self, res: Resolution, classes: Optional[int]) -> Model:
     """
     Creates a keras.models.Model object.
     """
     img_input = Input(res.hwc())
     x = ResNet101(
         include_top=True,
         weights=None,
         input_tensor=img_input,
     )
     x = Dense(1, activation='relu', name='predictions')(x.output)
     model = Model(img_input, x, name='resnet101a')
     return model
Beispiel #5
0
def DeepRN():
    base_model = ResNet101(include_top=False, weights='imagenet')
    # for layer in base_model.layers:
    #     layer.trainable = False
    x = SpatialPyramidPooling([3])(base_model.output)
    # x = Dropout(0.75)(x)
    x = Dense(1024, activation='relu')(x)
    x = Dense(1024, activation='relu')(x)
    x = Dense(1024, activation='relu')(x)
    x = Dense(1024, activation='relu')(x)
    x = Dense(5, activation='softmax')(x)
    model = Model(base_model.input, x)
    model.summary()

    return model
Beispiel #6
0
def make_encoder(input, name='resnet50', pretrained=True):
	if name == 'resnet18':
		from classification_models.keras import Classifiers
		ResNet18, _ = Classifiers.get('resnet18')
		model = ResNet18(
			weights='imagenet' if pretrained else None,
			input_tensor=input,
			include_top=False
		)
	elif name == 'resnet50':
		from keras.applications.resnet import ResNet50
		model = ResNet50(
			weights='imagenet' if pretrained else None,
			input_tensor=input,
			include_top=False
		)
	elif name == 'resnet101':
		from keras.applications.resnet import ResNet101
		model = ResNet101(
			weights='imagenet' if pretrained else None,
			input_tensor=input,
			include_top=False
		)
	elif name == 'resnet152':
		from keras.applications.resnet import ResNet152
		model = ResNet152(
			weights='imagenet' if pretrained else None,
			input_tensor=input,
			include_top=False
		)
	elif name == 'vgg16':
		from keras.applications.vgg16 import VGG16
		model = VGG16(
			weights='imagenet' if pretrained else None,
			input_tensor=input,
			include_top=False
		)
	elif name == 'vgg19':
		from keras.applications.vgg19 import VGG19
		model = VGG19(
			weights='imagenet' if pretrained else None,
			input_tensor=input,
			include_top=False
		)
	else:
		raise Exception(f'unknown encoder {name}')

	return model
Beispiel #7
0
 def train(self):
     # re-size all the images to this
     IMAGE_SIZE = [224, 224]
     # add preprocessing layer to the front of VGG
     resnet = ResNet101(input_shape=IMAGE_SIZE + [3],
                        weights='imagenet',
                        include_top=False)
     # don't train existing weights
     for layer in resnet.layers:
         layer.trainable = False
     # useful for getting number of classes
     folders = glob(self.train_path + '*')
     # our layers - you can add more if you want
     x = Flatten()(resnet.output)
     prediction = Dense(len(folders), activation='sigmoid')(x)
     # create a model object
     model = Model(inputs=resnet.input, outputs=prediction)
     # view the structure of the model
     model.summary()
     # tell the model what cost and optimization method to use
     model.compile(loss='binary_crossentropy',
                   optimizer='adam',
                   metrics=['accuracy'])
     # Use the Image Data Generator to import the images from the dataset
     train_datagen = ImageDataGenerator(rescale=1. / 255,
                                        shear_range=0.2,
                                        zoom_range=0.2,
                                        horizontal_flip=True)
     test_datagen = ImageDataGenerator(rescale=1. / 255)
     training_set = train_datagen.flow_from_directory(
         self.train_path,
         target_size=(224, 224),
         batch_size=32,
         class_mode='categorical')
     test_set = test_datagen.flow_from_directory(self.train_path,
                                                 target_size=(224, 224),
                                                 batch_size=32,
                                                 class_mode='categorical')
     # fit the model
     r = model.fit_generator(training_set,
                             validation_data=test_set,
                             epochs=10,
                             steps_per_epoch=2,
                             validation_steps=len(test_set))
     model.save(self.model_save_path)
class_examples = [1702, 941, 732, 665, 961]
max_sample = 1702
for i in range(1, 5):
    samples_generate = max_sample - class_examples[i]
    random_generate = np.random.choice(class_examples[i], samples_generate)
    for j in random_generate:
        X_train = np.append(X_train, np.array([X_train[j]]), axis=0)
        y_train = np.append(y_train, np.array([y_train[j]]), axis=0)

# ## Defining Model

# In[ ]:

base_model = ResNet101(input_shape=(224, 224, 3),
                       weights='imagenet',
                       include_top=False)

x = base_model.output
x = MaxPool2D()(x)
x = Flatten()(x)
x = Dense(128, activation='relu', kernel_regularizer=regularizers.l2(0.1))(x)
x = Dropout(0.5)(x)
x = Dense(64, activation='relu', kernel_regularizer=regularizers.l2(0.1))(x)
x = Dropout(0.5)(x)
pre = Dense(5, activation='softmax')(x)

model = Model(inputs=[base_model.input], outputs=[pre])
model.summary()

# ## Data Generator
Beispiel #9
0
def ResNet(input_shape = (const.X_Height, const.X_Width, const.X_Channels), classes = const.Y_Classes, Layers = 50, source = 'keras', weights = 'imagenet'):
    """
    create RestNet model

    Arguments:
        input_shape = Height, Width and channels for each input image
        classes = how many classes model will be trainned on
        Layers: how many layers; should be one of [18, 34, 50, 101, 152]
        source: 'keras' (use built-in model) or 'manual' (use my custom model above)
        weights: 'imagenet' (load weights from keras lib) or None (no weights loading) 
            'imagenet' only available if layers in [50,101,152]
    """

    # validate parameters
    if (Layers not in [18, 34, 50, 101, 152]):
        raise ValueError('Invalid layer number: ' + str(Layers) + ' (must be one of [18, 34, 50, 101, 152]).')

    if (source not in ['keras', 'manual']):
        raise ValueError('Invalid model source: ' + str(source) + " (must be 'keras' or 'manual'.")

    if (weights not in [None, 'imagenet']):
        raise ValueError('Invalid weights definition: ' + str(weights) + " (must be None or 'imagenet'.")

    if (Layers in [18, 34]):
        if (source == 'keras'):
            raise ValueError("No keras model available for small ResNets. 'source' parameter must be 'manual' when layers are 18 or 34.")

        if (weights != None):
            raise ValueError("No weights available for small ResNets. 'weights' Parameter must be None when layers are 18 or 34.")

    # build model
    if (source == 'keras'):
        # load base model from keras
        if (Layers == 50):
            from keras.applications.resnet import ResNet50
            baseModel = ResNet50(include_top = False, weights = weights, input_shape = input_shape)
        elif (Layers == 101):
            from keras.applications.resnet import ResNet101
            baseModel = ResNet101(include_top = False, weights = weights, input_shape = input_shape)
        elif (Layers == 152):
            from keras.applications.resnet import ResNet152
            baseModel = ResNet152(include_top = False, weights = weights, input_shape = input_shape)
    elif (source == 'manual'):
        # load model from my implementation
        if (Layers in [18,34]):
            baseModel = ResNetSmall(input_shape=input_shape, classes=classes, Layers=Layers)
        else:
            baseModel = ResNetLarge(input_shape=input_shape, classes=classes, Layers=Layers, weights=weights)

    # add final layers to built-in keras model
    from keras.models import Model
    from keras.layers import Dense, Flatten, AveragePooling2D

    X = baseModel.output
    X = AveragePooling2D(pool_size=(2, 2), strides=None, padding='valid', data_format=None)(X)
    X = Flatten()(X)
    Preds = Dense(const.Y_Classes, activation='softmax', name='fc' + str(const.Y_Classes))(X)

    model = Model(inputs=baseModel.input, outputs=Preds)

    # return the model
    return model
                         width_shift_range=0.2,
                         height_shift_range=0.2,
                         shear_range=0.15,
                         horizontal_flip=True,
                         fill_mode="nearest")

test_gen = ImageDataGenerator()
train = gen.flow(x_train)
test = test_gen.flow(x_test)

#################### Feature Extraction ############################

# Feature extract from resnet101
model1 = Sequential()
model1.add(
    ResNet101(weights='imagenet', include_top=False, input_shape=(75, 75, 3)))
model1.add(Conv2D(2048, (3, 3), activation='relu'))
model1.summary()
x_train1 = model1.predict(train)
x_test1 = model1.predict(test)
print(x_train1.shape)

# Feature extract from Inceptionv3
model2 = Sequential()
model2.add(
    InceptionV3(weights='imagenet', include_top=False,
                input_shape=(75, 75, 3)))
model2.summary()
x_train2 = model2.predict(train)
x_test2 = model2.predict(test)
print(x_train2.shape)
Beispiel #11
0
def build_model(xsize=224,
                ysize=224,
                channels=3,
                nlabels=2,
                lr=0.000001,
                bn=False,
                dropout=0.5,
                mtype='vgg'):

    from keras.applications.resnet50 import ResNet50
    from keras.applications.resnet import ResNet101
    from keras.applications.vgg16 import VGG16
    from keras.applications.vgg19 import VGG19
    from keras.applications.inception_v3 import InceptionV3
    from keras.applications.xception import Xception

    # Create the model
    model = models.Sequential()

    mod_conv = None
    #Load the VGG model

    if not mtype in [
            'vgg', 'vgg19', 'resnet', 'resnet101', 'inception', 'xception',
            'simple'
    ]:
        return False

    if 'vgg' in mtype:

        if mtype == 'vgg':
            # Transfer Learning using VGG16
            mod_conv = VGG16(weights='imagenet',
                             include_top=False,
                             input_shape=(xsize, ysize, channels))
        if mtype == 'vgg19':
            # Transfer Learning using VGG19
            mod_conv = VGG19(weights='imagenet',
                             include_top=False,
                             input_shape=(xsize, ysize, channels))

        # Freeze the layers except the last 4 layers
        for layer in mod_conv.layers[:-4]:
            layer.trainable = False

        # Add the vgg convolutional base model
        model.add(mod_conv)

    if 'resnet' in mtype:

        if mtype == 'resnet':
            # Transfer Learning using ResNet50
            mod_conv = ResNet50(weights='imagenet',
                                include_top=False,
                                input_shape=(xsize, ysize, channels))

        if mtype == 'resnet101':
            # Transfer Learning using ResNet100
            mod_conv = ResNet101(weights='imagenet',
                                 include_top=False,
                                 input_shape=(xsize, ysize, channels))

        output = mod_conv.layers[-1].output
        mod_conv = models.Model(mod_conv.input, output=output)

        # BatchNormalization Layers need to be trainable
        for layer in mod_conv.layers:
            if isinstance(layer, BatchNormalization):
                layer.trainable = True
            else:
                layer.trainable = False

        for layer in mod_conv.layers:
            if layer.name in [
                    'res5c_branch2b', 'res5c_branch2c', 'activation_97'
            ]:
                layer.trainable = True

        # Add the resnet convolutional base model
        model.add(mod_conv)

    if mtype == 'inception':

        # Transfer Learning using Inception V3
        mod_conv = InceptionV3(weights='imagenet',
                               include_top=False,
                               input_shape=(xsize, ysize, channels),
                               classes=nlabels)

        # Freeze the layers except the last 4 layers
        for layer in mod_conv.layers:
            layer.trainable = False

        # Add the inception convolutional base model
        model.add(mod_conv)

    if mtype == 'xception':
        # Transfer Learning using Xception
        mod_conv = Xception(weights='imagenet',
                            include_top=False,
                            input_shape=(xsize, ysize, channels))

        # Freeze the layers except the last 4 layers
        for layer in mod_conv.layers:
            layer.trainable = False

        # Add the inception convolutional base model
        model.add(mod_conv)

    if mtype == 'simple':
        # Create Custom made CNN
        build_cnn(model=model, bn=bn, dropout=dropout)

    # fully connected layers after 3D to 1D flat
    model.add(layers.Flatten())
    model.add(
        layers.Dense(64, activation='relu', kernel_initializer='he_uniform'))
    model.add(layers.Dropout(dropout))  # <- comprobar si es necesario

    model.add(layers.Dense(nlabels, activation='softmax'))

    model.summary()

    model.compile(optimizer=optimizers.Adam(lr=lr),
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])

    return model
Beispiel #12
0
def Build_Model_ResNet(px_train ,  pSessionParameters , pTrainingParameters ):
    
    from keras.applications.resnet import ResNet50
    from keras.applications.resnet import ResNet101
    from keras.applications.resnet import ResNet152
    
    from keras.applications.resnet_v2 import ResNet50V2
    from keras.applications.resnet_v2 import ResNet101V2
    from keras.applications.resnet_v2 import ResNet152V2
    

    
    from keras.layers import Input
       
    # Builds a new ResNet model
    ''' 
    Input parameters:
      px_train: training data to be used to set the input shape of the model
      pModelBuildParameters: Dict of Parameters to define how the model is built.

    Return parameters: model

    '''    
    BatchNormFlag = pSessionParameters['BatchNorm']     
 
    NoClasses = pSessionParameters['ModelBuildParameters']['NoClasses']
    Activation = pSessionParameters['ModelBuildParameters']['Activation']
    IncludeTopFlag = pSessionParameters['ModelBuildParameters']['IncludeTop']
    Model = pSessionParameters['ModelBuildParameters']['Model']
    Version = pSessionParameters['ModelBuildParameters']['Version'] 
    
    if IncludeTopFlag:
        if Version==1:
            if 'ResNet50' in Model:                
                conv_base = ResNet50(input_shape=(px_train.shape[1:]), weights=None, include_top=True , classes = NoClasses, pooling='avg')           
            elif 'ResNet101' in Model:
                conv_base = ResNet101(input_shape=(px_train.shape[1:]), weights=None, include_top=True , classes = NoClasses, pooling='avg')           
            elif 'ResNet152' in Model:
                conv_base = ResNet152(input_shape=(px_train.shape[1:]), weights=None, include_top=True , classes = NoClasses, pooling='avg')           
        else: # Version 2
            if 'ResNet50' in Model:                
                conv_base = ResNet50V2(input_shape=(px_train.shape[1:]), weights=None, include_top=True , classes = NoClasses, pooling='avg')           
            elif 'ResNet101' in Model:
                conv_base = ResNet101V2(input_shape=(px_train.shape[1:]), weights=None, include_top=True , classes = NoClasses, pooling='avg')           
            elif 'ResNet152' in Model:
                conv_base = ResNet152V2(input_shape=(px_train.shape[1:]), weights=None, include_top=True , classes = NoClasses, pooling='avg')           
                        
        model = models.Sequential()
        model.add(conv_base)    
    else:
        if Version==1:
            if 'ResNet50' in Model:                
                conv_base = ResNet50(input_shape=(px_train.shape[1:]), weights=None, include_top=False , classes = NoClasses, pooling='avg')           
            elif 'ResNet101' in Model:
                conv_base = ResNet101(input_shape=(px_train.shape[1:]), weights=None, include_top=False , classes = NoClasses, pooling='avg')           
            elif 'ResNet152' in Model:
                conv_base = ResNet152(input_shape=(px_train.shape[1:]), weights=None, include_top=False , classes = NoClasses, pooling='avg')           
        else: # Version 2
            if 'ResNet50' in Model:                
                conv_base = ResNet50V2(input_shape=(px_train.shape[1:]), weights=None, include_top=False , classes = NoClasses, pooling='avg')           
            elif 'ResNet101' in Model:
                conv_base = ResNet101V2(input_shape=(px_train.shape[1:]), weights=None, include_top=False , classes = NoClasses, pooling='avg')           
            elif 'ResNet152' in Model:
                conv_base = ResNet152V2(input_shape=(px_train.shape[1:]), weights=None, include_top=False , classes = NoClasses, pooling='avg')           
                
        model = models.Sequential()
        model.add(conv_base)    

        # Add Dense Layers and Dropout and BatchNorm layers based on ModelBuildParameters
        model = Build_Model_AddLayers(model , pSessionParameters )
        model.add(layers.Dense(NoClasses, activation=Activation, name='dense_class'))        
    
    
    return model
Beispiel #13
0
print(X.shape)
X = X.reshape(-1, 64, 64, 3)
print()
y_original_generated_label = y_generated_label
y_generated_label = to_categorical(y_generated_label, 2)
#Train-Test split
X_train, X_val, Y_train, Y_val = train_test_split(X, y_generated_label, test_size = 0.2,shuffle=True, random_state=5)

print(X_train.shape)
print(X_val.shape)
print(Y_train.shape)
print(Y_val.shape)

image_size = 64
from keras.applications.resnet import ResNet101
ResNet101_conv = ResNet101(include_top=False, weights='imagenet', input_shape=(image_size, image_size, 3))
for layer in ResNet101_conv.layers[:-4]:
    layer.trainable = False
for layer in ResNet101_conv.layers:
    print(layer, layer.trainable)
from keras import models
from keras import layers
from keras import optimizers
model = models.Sequential()
# Add the vgg convolutional base model
model.add(ResNet101_conv)
# Add new layers
model.add(layers.Flatten())
model.add(layers.Dense(1024, activation='relu'))
model.add(layers.Dropout(0.5))
model.add(layers.Dense(2, activation='softmax'))
Beispiel #14
0
##########데이터 로드

x_train, x_test, y_train, y_test = np.load("../data/npy/P_project.npy",allow_pickle=True)


categories = ["Beaggle", "Bichon Frise", "Border Collie","Bulldog", "Corgi","Poodle","Retriever","Samoyed",
                "Schnauzer","Shih Tzu",]
nb_classes = len(categories)


#일반화
x_train = preprocess_input(x_train)
x_test = preprocess_input(x_test)


resnet101 = ResNet101(include_top=False,weights='imagenet',input_shape=x_train.shape[1:])
resnet101.trainable = False
x = resnet101.output
x = MaxPooling2D(pool_size=(2,2)) (x)
x = Flatten() (x)

x = Dense(128, activation= 'relu') (x)
x = BatchNormalization() (x)
x = Dense(64, activation= 'relu') (x)
x = BatchNormalization() (x)
x = Dense(10, activation= 'softmax') (x)

model = Model(inputs = resnet101.input, outputs = x)
model.summary()
model.compile(loss='categorical_crossentropy', optimizer=Adam(1e-5), metrics=['acc'])
def extract_features(nn_model, fine_tune=False):
    if fine_tune is True:
        if nn_model == 'resnet50':
            model = ResNet50(include_top=False, weights=None, pooling='avg')
        elif nn_model == 'resnet101':
            model = ResNet101(include_top=False, weights=None, pooling='avg')
        elif nn_model == 'resnet152':
            model = ResNet152(include_top=False, weights=None, pooling='avg')
        elif nn_model == 'densenet121':
            model = DenseNet121(include_top=False, weights=None, pooling='avg')
        elif nn_model == 'densenet169':
            model = DenseNet169(include_top=False, weights=None, pooling='avg')
        elif nn_model == 'densenet201':
            model = DenseNet201(include_top=False, weights=None, pooling='avg')
        else:
            raise NotImplementedError("The NN model is not implemented!")
        model.load_weights('./finetune/' + nn_model +
                           '/finetune_weights_50_epoch.h5',
                           by_name=True)

    else:
        if nn_model == 'resnet50':
            model = ResNet50(include_top=False,
                             weights='imagenet',
                             pooling='avg')
        elif nn_model == 'resnet101':
            model = ResNet101(include_top=False,
                              weights='imagenet',
                              pooling='avg')
        elif nn_model == 'resnet152':
            model = ResNet152(include_top=False,
                              weights='imagenet',
                              pooling='avg')
        elif nn_model == 'densenet121':
            model = DenseNet121(include_top=False,
                                weights='imagenet',
                                pooling='avg')
        elif nn_model == 'densenet169':
            model = DenseNet169(include_top=False,
                                weights='imagenet',
                                pooling='avg')
        elif nn_model == 'densenet201':
            model = DenseNet201(include_top=False,
                                weights='imagenet',
                                pooling='avg')
        else:
            raise NotImplementedError("The NN model is not implemented!")

    ImgPath = './dataset/img'
    ProcessedPath = './dataset/feature_' + nn_model
    Lastlist = os.listdir(ImgPath)
    sum = 0
    for lastfolder in Lastlist:
        LastPath = os.path.join(ImgPath, lastfolder)
        savepath = os.path.join(ProcessedPath, lastfolder)
        imagelist = os.listdir(LastPath)
        for image1 in imagelist:
            sum += 1
            print(image1)
            print('sum is ', sum)
            image_pre, ext = os.path.splitext(image1)
            imgfile = LastPath + '/' + image1
            img = image.load_img(imgfile, target_size=(64, 64))
            x = image.img_to_array(img)  # shape:(64,64,3)
            x = np.expand_dims(x, axis=0)
            x = preprocess_input(x)  # shape:(1,64,64,3)
            print(x.shape)
            want = model.predict(x)
            print(np.shape(want))  # shape:(1,2048)

            # normalize
            a = np.min(want)
            b = np.max(want)
            want = (want - a) / (b - a)
            if (not os.path.exists(savepath)):
                os.makedirs(savepath)
            np.save(os.path.join(savepath, image_pre + '.npz'), want)
Beispiel #16
0
            그래서 이를 1/255로 스케일링하여 0-1 범위로 변환시켜줍니다. 
            이는 다른 전처리 과정에 앞서 가장 먼저 적용됩니다.
- shear_range: 임의 전단 변환 (shearing transformation) 범위
- zoom_range: 임의 확대/축소 범위
- horizontal_flip`: True로 설정할 경우, 50% 확률로 이미지를 수평으로 뒤집습니다. 
    원본 이미지에 수평 비대칭성이 없을 때 효과적입니다. 즉, 뒤집어도 자연스러울 때 사용하면 좋습니다.
- fill_mode 이미지를 회전, 이동하거나 축소할 때 생기는 공간을 채우는 방식
'''

train_generator = idg.flow(x_train, y_train, batch_size=8)
# seed => random_state
valid_generator = idg2.flow(x_test, y_test)
test_generator = idg2.flow(x_pred, shuffle=False)

resnet = ResNet101(include_top=False,
                   weights='imagenet',
                   input_shape=(128, 128, 3))
x = resnet.output
resnet.trainable = False
# x = Conv2D(filters =1024,kernel_size=(3,3), strides=1, padding='valid') (x)
# x = Conv2D(filters =1024,kernel_size=(3,3), strides=1, padding='valid',) (x)
x = GlobalAveragePooling2D()(x)
# x = Dropout(0.5) (x)
x = Flatten()(x)
x = Dense(128)(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Dense(64)(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Dense(26, activation='sigmoid')(x)
def chosen_model(choice):
    global base_model
    if choice == 19:
        model_exist()
    else:
        while (1):
            print()
            print(
                'Transfer Learning? - Will use pre-trained model with imagenet weights'
            )
            print('y')
            print('n')
            weights_wanted = input()
            if weights_wanted.upper() != 'Y' and weights_wanted.upper() != 'N':
                print('ERROR: Please enter a valid choice')
            else:
                break
        if choice == 1:
            print('Selected Model = Xception')
            if weights_wanted.upper() == 'Y':
                base_model = Xception(weights='imagenet', include_top=False)
            else:
                base_model = Xception(weights=None, include_top=False)
        if choice == 2:
            print('Selected Model = VGG16')
            if weights_wanted.upper() == 'Y':
                base_model = VGG16(weights='imagenet', include_top=False)
            else:
                base_model = VGG16(weights=None, include_top=False)
        if choice == 3:
            print('Selected Model = VGG19')
            if weights_wanted.upper() == 'Y':
                base_model = VGG19(weights='imagenet', include_top=False)
            else:
                base_model = VGG19(weights=None, include_top=False)
        if choice == 4:
            print('Selected Model = ResNet50')
            if weights_wanted.upper() == 'Y':
                base_model = ResNet50(weights='imagenet', include_top=False)
            else:
                base_model = ResNet50(weights=None, include_top=False)
        if choice == 5:
            print('Selected Model = ResNet101')
            if weights_wanted.upper() == 'Y':
                base_model = ResNet101(weights='imagenet', include_top=False)
            else:
                base_model = ResNet101(weights=None, include_top=False)
        if choice == 6:
            print('Selected Model = ResNet152')
            if weights_wanted.upper() == 'Y':
                base_model = ResNet152(weights='imagenet', include_top=False)
            else:
                base_model = ResNet152(weights=None, include_top=False)
        if choice == 7:
            print('Selected Model = ResNet50V2')
            if weights_wanted.upper() == 'Y':
                base_model = ResNet50V2(weights='imagenet', include_top=False)
            else:
                base_model = ResNet50V2(weights=None, include_top=False)
        if choice == 8:
            print('Selected Model = ResNet101V2')
            if weights_wanted.upper() == 'Y':
                base_model = ResNet101V2(weights='imagenet', include_top=False)
            else:
                base_model = ResNet101V2(weights=None, include_top=False)
        if choice == 9:
            print('Selected Model = ResNet152V2')
            if weights_wanted.upper() == 'Y':
                base_model = ResNet152V2(weights='imagenet', include_top=False)
            else:
                base_model = ResNet152V2(weights=None, include_top=False)
        if choice == 10:
            print('Selected Model = InceptionV3')
            if weights_wanted.upper() == 'Y':
                base_model = InceptionV3(weights='imagenet', include_top=False)
            else:
                base_model = InceptionV3(weights=None, include_top=False)
        if choice == 11:
            print('Selected Model = InceptionResNetV2')
            if weights_wanted.upper() == 'Y':
                base_model = InceptionResNetV2(weights='imagenet',
                                               include_top=False)
            else:
                base_model = InceptionResNetV2(weights=None, include_top=False)
        if choice == 12:
            print('Selected Model = MobileNet')
            if weights_wanted.upper() == 'Y':
                base_model = MobileNet(weights='imagenet', include_top=False)
            else:
                base_model = MobileNet(weights=None, include_top=False)
        if choice == 13:
            print('Selected Model = MobileNetV2')
            if weights_wanted.upper() == 'Y':
                base_model = MobileNetV2(weights='imagenet', include_top=False)
            else:
                base_model = MobileNetV2(weights=None, include_top=False)
        if choice == 14:
            print('Selected Model = DenseNet121')
            if weights_wanted.upper() == 'Y':
                base_model = DenseNet121(weights='imagenet', include_top=False)
            else:
                base_model = DenseNet121(weights=None, include_top=False)
        if choice == 15:
            print('Selected Model = DenseNet169')
            if weights_wanted.upper() == 'Y':
                base_model = DenseNet169(weights='imagenet', include_top=False)
            else:
                base_model = DenseNet169(weights=None, include_top=False)
        if choice == 16:
            print('Selected Model = DenseNet201')
            if weights_wanted.upper() == 'Y':
                base_model = DenseNet201(weights='imagenet', include_top=False)
            else:
                base_model = DenseNet201(weights=None, include_top=False)
        if choice == 17:
            print('Selected Model = NASNetLarge')
            if weights_wanted.upper() == 'Y':
                base_model = NASNetLarge(weights='imagenet', include_top=False)
            else:
                base_model = NASNetLarge(weights=None, include_top=False)
        if choice == 18:
            print('Selected Model = NASNetMobile')
            if weights_wanted.upper() == 'Y':
                base_model = NASNetMobile(weights='imagenet',
                                          include_top=False)
            else:
                base_model = NASNetMobile(weights=None, include_top=False)

        CLASSES = len(os.listdir('data/train'))
        print('Number of Classes = {}'.format(CLASSES))

        x = base_model.output
        x = GlobalAveragePooling2D(name='avg_pool')(x)
        x = Dropout(0.4)(x)
        predictions = Dense(CLASSES, activation='softmax')(x)
        model = Model(inputs=base_model.input, outputs=predictions)

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

        model.compile(optimizer='rmsprop',
                      loss='categorical_crossentropy',
                      metrics=['accuracy'])
        training(model)
Beispiel #18
0
if args.resume:
    print('resume from checkpoint')
    model = keras.models.load_model(save_file)
else:
    print('train from start')
    model = models.Sequential()

    if '50' in args_model:
        base_model = ResNet50(weights=None,
                              include_top=False,
                              input_shape=(32, 32, 3),
                              pooling=None)
    elif '101' in args_model:
        base_model = ResNet101(weights=None,
                               include_top=False,
                               input_shape=(32, 32, 3),
                               pooling=None)
    elif '152' in args_model:
        base_model = ResNet152(weights=None,
                               include_top=False,
                               input_shape=(32, 32, 3),
                               pooling=None)

    #base_model.summary()

    #pdb.set_trace()

    #model.add(layers.UpSampling2D((2,2)))
    #model.add(layers.UpSampling2D((2,2)))
    #model.add(layers.UpSampling2D((2,2)))
    model.add(base_model)
Beispiel #19
0
#Train-Test split
X_train, X_val, Y_train, Y_val = train_test_split(X,
                                                  y_generated_label,
                                                  test_size=0.2,
                                                  shuffle=True,
                                                  random_state=5)

print(X_train.shape)
print(X_val.shape)
print(Y_train.shape)
print(Y_val.shape)

image_size = 64
from keras.applications.resnet import ResNet101
resnet_conv = ResNet101(weights=None,
                        include_top=False,
                        input_shape=(image_size, image_size, 3))
for layer in resnet_conv.layers[:-4]:
    layer.trainable = False
for layer in resnet_conv.layers:
    print(layer, layer.trainable)
from keras import models
from keras import layers
from keras import optimizers

from tensorflow.keras.callbacks import ModelCheckpoint
filepath = "current_best_weights_resnet101_machine_label_balance.hdf5"
model = models.Sequential()
# Add the vgg convolutional base model
model.add(resnet_conv)
# Add new layers