def test_nasnet_variable_input_channels(): input_shape = (1, None, None) if K.image_data_format() == 'channels_first' else ( None, None, 1) model = applications.NASNetMobile(weights=None, include_top=False, input_shape=input_shape) assert model.output_shape == (None, None, None, 1056) model = applications.NASNetLarge(weights=None, include_top=False, input_shape=input_shape) assert model.output_shape == (None, None, None, 4032) input_shape = (4, None, None) if K.image_data_format() == 'channels_first' else ( None, None, 4) model = applications.NASNetMobile(weights=None, include_top=False, input_shape=input_shape) assert model.output_shape == (None, None, None, 1056) model = applications.NASNetLarge(weights=None, include_top=False, input_shape=input_shape) assert model.output_shape == (None, None, None, 4032)
def build_fit_save_cnn(input_shape, n_classes, epochs, batch_size, X_train, X_val, y_train, y_val): base_model = applications.NASNetMobile(weights='imagenet', include_top=False, input_shape=input_shape) add_model = Sequential() add_model.add(Flatten(input_shape=base_model.output_shape[1:])) add_model.add(Dense(512, activation='relu')) add_model.add(Dense(512, activation='relu')) add_model.add(Dropout(0.25)) add_model.add(Dense(n_classes, activation='softmax')) # combine base model and fully connected layers final_model = Model(inputs=base_model.input, outputs=add_model(base_model.output)) # specify SDG optimizer parameters sgd = optimizers.SGD(lr=0.001, decay=1e-6, momentum=0.9, nesterov=True) # compile model final_model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy']) history = final_model.fit(X_train, y_train, batch_size=batch_size, epochs=epochs, verbose=1, validation_data=(X_val, y_val)) score = final_model.evaluate(X_val, y_val, verbose=0) print('Val. score:', score[0]) print('Val. accuracy:', score[1]) plot_model(final_model, to_file='drive/ML-Pandora/CNN-NASNetLarge-Pandora-Model.svg') show_history(history) save_model(final_model,history) return final_model
def test_nasnet_pooling(): model = applications.NASNetMobile(weights=None, include_top=False, pooling='avg') assert model.output_shape == (None, 1056) model = applications.NASNetLarge(weights=None, include_top=False, pooling='avg') assert model.output_shape == (None, 4032)
def get_imagenet_architecture(architecture, variant, size, alpha, output_layer, include_top=False, weights='imagenet'): from keras import applications, Model if include_top: assert output_layer == 'last' if size == 'auto': size = get_image_size(architecture, variant, size) shape = (size, size, 3) if architecture == 'densenet': if variant == 'auto': variant = 'densenet-121' if variant == 'densenet-121': model = applications.DenseNet121(weights=weights, include_top=include_top, input_shape=shape) elif variant == 'densenet-169': model = applications.DenseNet169(weights=weights, include_top=include_top, input_shape=shape) elif variant == 'densenet-201': model = applications.DenseNet201(weights=weights, include_top=include_top, input_shape=shape) elif architecture == 'inception-resnet-v2': model = applications.InceptionResNetV2(weights=weights, include_top=include_top, input_shape=shape) elif architecture == 'mobilenet': model = applications.MobileNet(weights=weights, include_top=include_top, input_shape=shape, alpha=alpha) elif architecture == 'mobilenet-v2': model = applications.MobileNetV2(weights=weights, include_top=include_top, input_shape=shape, alpha=alpha) elif architecture == 'nasnet': if variant == 'auto': variant = 'large' if variant == 'large': model = applications.NASNetLarge(weights=weights, include_top=include_top, input_shape=shape) else: model = applications.NASNetMobile(weights=weights, include_top=include_top, input_shape=shape) elif architecture == 'resnet-50': model = applications.ResNet50(weights=weights, include_top=include_top, input_shape=shape) elif architecture == 'vgg-16': model = applications.VGG16(weights=weights, include_top=include_top, input_shape=shape) elif architecture == 'vgg-19': model = applications.VGG19(weights=weights, include_top=include_top, input_shape=shape) elif architecture == 'xception': model = applications.Xception(weights=weights, include_top=include_top, input_shape=shape) elif architecture == 'inception-v3': model = applications.InceptionV3(weights=weights, include_top=include_top, input_shape=shape) if output_layer != 'last': try: if isinstance(output_layer, int): layer = model.layers[output_layer] else: layer = model.get_layer(output_layer) except Exception: raise VergeMLError('layer not found: {}'.format(output_layer)) model = Model(inputs=model.input, outputs=layer.output) return model
def test_nasnet(): model = applications.NASNetMobile(weights=None) assert model.output_shape == (None, 1000) model = applications.NASNetLarge(weights=None) assert model.output_shape == (None, 1000)
input_shape=(256, 256, 3)) elif args.base_model == 'densenet121': base_model = applications.DenseNet121(weights='imagenet', include_top=False, input_shape=(256, 256, 3)) elif args.base_model == 'densenet169': base_model = applications.DenseNet169(weights='imagenet', include_top=False, input_shape=(256, 256, 3)) elif args.base_model == 'densenet201': base_model = applications.DenseNet201(weights='imagenet', include_top=False, input_shape=(256, 256, 3)) elif args.base_model == 'nasnetmobile': base_model = applications.NASNetMobile(weights='imagenet', include_top=False, input_shape=(256, 256, 3)) elif args.base_model == 'nasnetlarge': base_model = applications.NASNetLarge(weights='imagenet', include_top=False, input_shape=(256, 256, 3)) else: raise ValueError( "Model you entered is not present in the model zoo thats offered") if args.bottleneck_tensorname is None: # Taking the last tensor (Just before the softmax layer) BOTTLENECK_TENSOR_NAME = base_model.layers[-1].name else: BOTTLENECK_TENSOR_NAME = args.bottleneck_tensorname
def _nasnetmobile(image_shape): return applications.NASNetMobile(include_top=False, input_shape=image_shape)
def bulid_keras_pretrained_net(net_name, width, height, RGB, ouput_classes, load_epoch = 0): ''' Build NASNET which is able to save weights in one file ''' MODEL_W_PATH = SAVE_FOLDER + '/mobilenetv2_weights_' channel = 3 if RGB == True else 1 dev = "/cpu:0" if N_GPU != 1 else "/gpu:0" with tf.device(dev): if load_epoch != 0: model = load_model(MODEL_W_PATH + str(load_epoch) + '.h5') else: # this could also be the output a different Keras model or layer input_tensor = Input(shape=(width, height, channel)) # this assumes K.image_data_format() == 'channels_last' if net_name == 'Xception': MODEL_W_PATH = SAVE_FOLDER + '/xception_weights_' base_model = applications.xception.Xception(input_tensor=input_tensor,weights='imagenet', include_top=False) elif net_name == 'MobileNet': MODEL_W_PATH = SAVE_FOLDER + '/mobilenetv2_weights_' base_model = applications.mobilenet_v2.MobileNetV2(input_tensor=input_tensor, weights='imagenet', include_top=False) elif net_name == 'NasnetMoblie': MODEL_W_PATH = SAVE_FOLDER + '/nasnetmobile_weights_' base_model = applications.NASNetMobile(input_tensor=input_tensor,weights='imagenet', include_top=False) else: raise ValueError(net_name, ' is not supported!') x = base_model.output ## add a global spatial average pooling layer x = GlobalAveragePooling2D()(x) x = Dropout(0.5)(x) #x = Flatten()(x) x = Dense(TOP_HIDDEN_L ,name='hidden_l')(x) x = BatchNormalization()(x) x = Activation('relu')(x) x = Dropout(0.5)(x) # and a logistic layer -- let's say we have 200 classes predictions = Dense(ouput_classes , kernel_regularizer=regularizers.l2(1e-4) , activation='softmax' , name = 'predictions')(x) # this is the model we will train model = Model(inputs=base_model.input, outputs=predictions) # set if some weights are trainable or not for layer in model.layers[:len(base_model.layers)]: layer.trainable = TRANSFERED_MODEL_UPDATE #model = multi_gpu_model(model, gpus=N_GPU) if N_GPU > 1: model = ModelMGPU(model, N_GPU) # compile the model with a Adam optimizer and dynamic learning rate. model.compile(loss='categorical_crossentropy', \ optimizer=Adam(lr=lr_schedule(load_epoch)), \ #optimizer=SGD(lr=1e-4, momentum=0.9), \ metrics=['accuracy']) model.summary() return model, MODEL_W_PATH