Example #1
0
        hp = (ymax - ymin) * 1000 // height / 10
        xminp = xmin * 1000 // width / 10
        yminp = ymin * 1000 // height / 10
        iiifurl = "https://www.dl.ndl.go.jp/api/iiif/" + pid + "/R" + komanum.zfill(
            7) + "/pct:%3.1f,%3.1f,%3.1f,%3.1f/400,/0/default.jpg" % (
                xminp, yminp, wp, hp)
        uri = pid + "/R" + komanum.zfill(
            7) + "/pct:%3.1f,%3.1f,%3.1f,%3.1f/" % (xminp, yminp, wp, hp)
        #print(uri)
        return uri


if __name__ == '__main__':
    np.random.seed(777)
    base_model = DenseNet121(include_top=False,
                             weights='imagenet',
                             pooling='avg',
                             classes=1000)
    #top_model = Sequential()
    #top_model.add(Dense(1000, activation='softmax'))
    #pseudomodel = Model(inputs=base_model.input,outputs=top_model(base_model.output))
    for datasetname in glob.glob(os.path.join(DATASETDIR, "*")):
        #for datasetname in ["dataset11"]:
        print(datasetname)
        path_test_prefix = datasetname
        outputfilename = "features/features_" + datasetname + ".pkl"
        inputs = []
        filenames = []
        urilist = []
        dic = {}
        counter = 0
        for img_path in tqdm(glob.glob(os.path.join(path_test_prefix,
Example #2
0
 def test_DenseNet121(self):
     from keras.applications.densenet import DenseNet121
     model = DenseNet121(include_top=True, weights='imagenet')
     res = run_image(model, self.model_files, img_path)
     self.assertTrue(*res)
Example #3
0
def unet_densenet121_imagenet(input_shape, weights='imagenet'):
    blocks = [6, 12, 24, 16]
    n_channel = 4
    n_class = 4
    img_input = Input(input_shape + (n_channel,))
    
    x = ZeroPadding2D(padding=((11, 11), (11, 11)))(img_input)
    x = Conv2D(64, 7, strides=2, use_bias=False, name='conv1/conv')(x)
    x = BatchNormalization(axis=bn_axis, epsilon=1.001e-5,
                           name='conv1/bn')(x)
    x = Dropout(rate = dropout)(x, training=True)
    x = Activation('relu', name='conv1/relu')(x)
    conv1 = x
    # print(conv1)
    x = ZeroPadding2D(padding=((1, 1), (1, 1)))(x)
    x = MaxPooling2D(3, strides=2, name='pool1')(x)
    x = dense_block(x, blocks[0], name='conv2')
    conv2 = x
    # print(conv2)
    x = transition_block(x, 0.5, name='pool2')
    x = dense_block(x, blocks[1], name='conv3')
    conv3 = x
    # print(conv3)
    # x = ZeroPadding2D(padding=((1, 1), (1, 1)))(x)
    x = transition_block(x, 0.5, name='pool3')
    x = dense_block(x, blocks[2], name='conv4')
    conv4 = x
    # print(conv4)
    x = transition_block(x, 0.5, name='pool4')
    x = dense_block(x, blocks[3], name='conv5')
    x = BatchNormalization(axis=bn_axis, epsilon=1.001e-5,
                           name='bn')(x)
    conv5 = x 
    # print(conv5)
       
    conv6 = conv_block(UpSampling2D()(conv5), 320)
    # print(conv6)
    conv6 = concatenate([conv6,conv4], axis=-1)
    conv6 = conv_block(conv6, 320)

    conv7 = conv_block(UpSampling2D()(conv6), 256)
    # print(conv7)
    conv7 = concatenate([conv7, conv3], axis=-1)
    conv7 = conv_block(conv7, 256)

    conv8 = conv_block(UpSampling2D()(conv7), 128) 
    # print(conv8)  
    conv8 = concatenate([conv8, conv2], axis=-1)
    conv8 = conv_block(conv8, 128)

    conv9 = conv_block(UpSampling2D()(conv8), 96)
    # print(conv9)    
    conv9 = concatenate([conv9, conv1], axis=-1)
    conv9 = conv_block(conv9, 96)

    conv10 = conv_block(UpSampling2D()(conv9), 64)
    # print(conv10)
    conv10 = conv_block(conv10, 64)
    conv10 = Cropping2D(cropping = ((8,8),(8,8)))(conv10)
    res = Conv2D(n_class, (1, 1), activation='softmax', name= 'res')(conv10)

    # print(res)
    model = Model(img_input, res)
    
    if weights == 'imagenet':
        densenet = DenseNet121(input_shape=input_shape + (3,), weights=weights, include_top=False)
        w0 = densenet.layers[2].get_weights()
        w = model.layers[2].get_weights()
        w[0][:, :, [0, 1, 2], :] = 0.9 * w0[0][:, :, :3, :]
        w[0][:, :, 3, :] = 0.1 * w0[0][:, :, 0, :]
    #     # w[0][:, :, 4, :] = 0.1 * w0[0][:, :, 2, :]
        model.layers[2].set_weights(w)
        counter = 0
        for i in range(3, len(densenet.layers)):
            if model.layers[i].get_config()['name'].__contains__('dropout'): 
                counter +=1
                continue

            model.layers[i].set_weights(densenet.layers[i - counter].get_weights())
            model.layers[i].trainable = False
    return model

# # Test model
# os.environ['CUDA_VISIBLE_DEVICES']='0'
# model = unet_densenet121_imagenet(input_shape=(256,256))
# model.summary()
Example #4
0
 def test_DenseNet121(self):
     from keras.applications.densenet import DenseNet121
     model = DenseNet121(include_top=True, weights='imagenet')
     self._test_keras_model(model)
(trainX, valX, trainY, valY) = train_test_split(x_train, y_train, stratify = y_train, test_size=0.2, random_state = 42)


#######################################################################
########################### Build Model ###############################

######## Hyperparameters ###############
batch_size = 40
epochs = 100
steps = trainX.shape[0] // batch_size
########################################

# Inputs
inputs = Input(shape=img_dim)
# DenseNet
densenet121 = DenseNet121(weights='imagenet', include_top=False)(inputs)

def fc_layer(x, units = 256, reg = False):
    if reg == True:
      kernel_reg = regularizers.l2(0.01)
    else:
      kernel_reg = None
    x = Dense(units,  use_bias=True, kernel_regularizer=kernel_reg)(x)
    x = BatchNormalization()(x)
    x = Activation(activation='relu')(x)
    return x

# # Our FC layer
flat1 = Flatten()(densenet121)
fc1 = fc_layer(flat1)
drop1 = Dropout(rate=0.5)(fc1)
Example #6
0
    monitor='val_loss',
    verbose=1,
    period=1,
    mode='auto',
    save_best_only=True,
    save_weights_only=False)

earlystopping = EarlyStopping(monitor='val_loss',
                              min_delta=0.0001,
                              patience=3,
                              verbose=1,
                              mode='auto',
                              restore_best_weights=True)

base_model = DenseNet121(weights="imagenet",
                         include_top=False,
                         input_shape=input_shape,
                         pooling=max)

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

x = base_model.output
x = GlobalAveragePooling2D()(x)

x = Dense(2048,
          kernel_initializer='glorot_uniform',
          bias_initializer=Constant(value=0.01),
          kernel_constraint=max_norm(3),
          bias_constraint=max_norm(3),
          activity_regularizer=l2(0.1))(x)
x = LeakyReLU()(x)
Example #7
0
        if weights_path is not None:
            print("load model weights_path: {}".format(weights_path))
            model.load_weights(weights_path)
        return model


if __name__ == "__main__":
    # model = ModelFactory().get_model(["a", "b"])
    # model.summary()

    
    from keras.applications.densenet import DenseNet121
    from keras.layers import Input
    from keras.layers.core import Dense
    from keras.models import Model

    img_shape = (244, 244, 3)
    img_input = Input(shape=img_shape)
    nb_classes = 2

    base_model = DenseNet121(include_top=False,
                         input_tensor=img_input, 
                         input_shape=img_shape, 
                         weights='imagenet', 
                         pooling="avg")
    x = base_model.output
    predictions = Dense(nb_classes, activation="sigmoid", name="predictions")(x)
    model = Model(inputs=img_input, outputs=predictions)
    model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
    model.summary()
Example #8
0
#!/usr/bin/env python
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.applications.densenet import preprocess_input
from keras.applications.densenet import decode_predictions
from keras.applications.densenet import DenseNet121

# iteration count
_iter = 1
"""
    Main
"""
if __name__ == '__main__':
    # load the model
    model = DenseNet121()
    # load an image from file
    image = load_img('mug.jpg', target_size=(224, 224))
    # convert the image pixels to a numpy array
    image = img_to_array(image)
    # reshape data for the model
    image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
    # prepare the image for the VGG model
    image = preprocess_input(image)

    # predict the probability across all output classes
    for i in range(_iter):
        raw_input('{} iteration, press any key to perform...'.format(str(i)))
        yhat = model.predict(image)

    # return if no iteration
    if not _iter: exit()
#sample_weights = weights[weight_y[1]]

# Fit the model
#base_model = DenseNet121(include_top=False, weights='imagenet')
base_model = None
if base == "VGG16":
    base_model = VGG16(weights='imagenet',
                       include_top=False,
                       input_shape=train_X.shape[1:])
elif base == "VGG19":
    base_model = VGG19(weights='imagenet',
                       include_top=False,
                       input_shape=train_X.shape[1:])
elif base == "Dense":
    base_model = DenseNet121(include_top=False,
                             weights='imagenet',
                             input_shape=train_X.shape[1:])

x = base_model.output

if architecture == "ap1":
    x = GlobalAveragePooling2D()(x)
elif architecture == "flat1":
    x = Flatten()(x)
    x = Dense(512, activation='relu')(x)
    x = Dropout(0.5)(x)

x = Dense(128, activation='relu')(x)

predictions = Dense(1, activation='sigmoid')(x)
Example #10
0
                                    shuffle=False)

print("Loading Data Done\n")
###################################################################################################

# 2. Build Model
###################################################################################################
input_tensor = Input(shape=(224, 224, 3))

if model_name == "VGG16":
    base_model = VGG16(
        include_top=False, weights="imagenet", pooling="avg"
    )  # Compare 'max' and 'avg' pooling, 'avg' works much better.
if model_name == "DenseNet121":
    base_model = DenseNet121(include_top=False,
                             weights="imagenet",
                             pooling="avg")

x = base_model(input_tensor)
predictions = Dense(len(disease_name), activation="sigmoid")(x)
model = Model(inputs=input_tensor, outputs=predictions)

print(model.summary())
print("Building Model Done\n")
###################################################################################################

# 3. Callbacks
###################################################################################################
# Reference: https://stackoverflow.com/questions/41032551/how-to-compute-receiving-operating-characteristic-roc-and-auc-in-keras

Example #11
0
def get_tst_neural_net(type):
    model = None
    custom_objects = dict()
    if type == 'mobilenet_small':
        try:
            from keras.applications.mobilenet import MobileNet
        except:
            from tensorflow.keras.applications.mobilenet import MobileNet
        model = MobileNet((128, 128, 3), depth_multiplier=1, alpha=0.25, include_top=True, weights='imagenet')
    elif type == 'mobilenet':
        try:
            from keras.applications.mobilenet import MobileNet
        except:
            from tensorflow.keras.applications.mobilenet import MobileNet
        model = MobileNet((224, 224, 3), depth_multiplier=1, alpha=1.0, include_top=True, weights='imagenet')
    elif type == 'mobilenet_v2':
        try:
            from keras.applications.mobilenet_v2 import MobileNetV2
        except:
            from tensorflow.keras.applications.mobilenet_v2 import MobileNetV2
        model = MobileNetV2((224, 224, 3), alpha=1.4, include_top=True, weights='imagenet')
    elif type == 'resnet50':
        try:
            from keras.applications.resnet50 import ResNet50
        except:
            from tensorflow.keras.applications.resnet50 import ResNet50
        model = ResNet50(input_shape=(224, 224, 3), include_top=True, weights='imagenet')
    elif type == 'inception_v3':
        try:
            from keras.applications.inception_v3 import InceptionV3
        except:
            from tensorflow.keras.applications.inception_v3 import InceptionV3
        model = InceptionV3(input_shape=(299, 299, 3), include_top=True, weights='imagenet')
    elif type == 'inception_resnet_v2':
        try:
            from keras.applications.inception_resnet_v2 import InceptionResNetV2
        except:
            from tensorflow.keras.applications.inception_resnet_v2 import InceptionResNetV2
        model = InceptionResNetV2(input_shape=(299, 299, 3), include_top=True, weights='imagenet')
    elif type == 'xception':
        try:
            from keras.applications.xception import Xception
        except:
            from tensorflow.keras.applications.xception import Xception
        model = Xception(input_shape=(299, 299, 3), include_top=True, weights='imagenet')
    elif type == 'densenet121':
        try:
            from keras.applications.densenet import DenseNet121
        except:
            from tensorflow.keras.applications.densenet import DenseNet121
        model = DenseNet121(input_shape=(224, 224, 3), include_top=True, weights='imagenet')
    elif type == 'densenet169':
        try:
            from keras.applications.densenet import DenseNet169
        except:
            from tensorflow.keras.applications.densenet import DenseNet169
        model = DenseNet169(input_shape=(224, 224, 3), include_top=True, weights='imagenet')
    elif type == 'densenet201':
        try:
            from keras.applications.densenet import DenseNet201
        except:
            from tensorflow.keras.applications.densenet import DenseNet201
        model = DenseNet201(input_shape=(224, 224, 3), include_top=True, weights='imagenet')
    elif type == 'nasnetmobile':
        try:
            from keras.applications.nasnet import NASNetMobile
        except:
            from tensorflow.keras.applications.nasnet import NASNetMobile
        model = NASNetMobile(input_shape=(224, 224, 3), include_top=True, weights='imagenet')
    elif type == 'nasnetlarge':
        try:
            from keras.applications.nasnet import NASNetLarge
        except:
            from tensorflow.keras.applications.nasnet import NASNetLarge
        model = NASNetLarge(input_shape=(331, 331, 3), include_top=True, weights='imagenet')
    elif type == 'vgg16':
        try:
            from keras.applications.vgg16 import VGG16
        except:
            from tensorflow.keras.applications.vgg16 import VGG16
        model = VGG16(input_shape=(224, 224, 3), include_top=False, pooling='avg', weights='imagenet')
    elif type == 'vgg19':
        try:
            from keras.applications.vgg19 import VGG19
        except:
            from tensorflow.keras.applications.vgg19 import VGG19
        model = VGG19(input_shape=(224, 224, 3), include_top=False, pooling='avg', weights='imagenet')
    elif type == 'multi_io':
        model = get_custom_multi_io_model()
    elif type == 'multi_model_layer_1':
        model = get_custom_model_with_other_model_as_layer()
    elif type == 'multi_model_layer_2':
        model = get_small_model_with_other_model_as_layer()
    elif type == 'Conv2DTranspose':
        model = get_Conv2DTranspose_model()
    elif type == 'RetinaNet':
        model, custom_objects = get_RetinaNet_model()
    elif type == 'conv3d_model':
        model = get_simple_3d_model()
    elif type == 'conv1d_model':
        model = get_simple_1d_model()
    return model, custom_objects
Example #12
0
X = np.array(image_batch)
y = to_categorical(np.array(label_batch))

#%%
X_train, X_val, y_train, y_val = train_test_split(X,
                                                  y,
                                                  test_size=0.2,
                                                  random_state=13)
X_train, X_test, y_train, y_test = train_test_split(X_train,
                                                    y_train,
                                                    test_size=0.25,
                                                    random_state=11)

#%%
dnsnet = DenseNet121(weights='imagenet',
                     include_top=False,
                     input_shape=(64, 64, 3),
                     classes=2)

X_dense_train = pre_dense(X_train)
X_dense_val = pre_dense(X_val)
X_dense_test = pre_dense(X_test)

features_dense_train = dnsnet.predict(np.array(X_dense_train),
                                      batch_size=256,
                                      verbose=1)
features_dense_val = dnsnet.predict(np.array(X_dense_val),
                                    batch_size=256,
                                    verbose=1)
features_dense_test = dnsnet.predict(np.array(X_dense_test),
                                     batch_size=256,
                                     verbose=1)
Example #13
0
### Split data ###
##################

trainPath = os.path.join("dir_" + pathName, 'train_file.csv')
validationPath = os.path.join("dir_" + pathName, 'validation_file.csv')
SplitDataToTrainAndValidation(trainRawPath, trainPath, validationPath, seed,
                              validationNum)

###################
### Build Model ###
###################
model = None

if model_structure == '121':
    densenet_121_base_model = DenseNet121(include_top=False,
                                          weights='imagenet',
                                          input_shape=(imageDim, imageDim, 3))
    # add a global spatial average pooling layer
    x = densenet_121_base_model.output
    x = GlobalAveragePooling2D()(x)
    # x = MaxPooling2D(pool_size=(2, 2))(x)
    # x = Conv2D(filters=1024, kernel_size=3, padding='same', activation='relu')(x)
    # x = MaxPooling2D(pool_size=(2, 2))(x)
    # x = Dropout(0.6)(x)
    # x = Flatten()(x)
    # x = Dropout(0.6)(x)
    # and a logistic layer
    predictions = Dense(14, activation="sigmoid")(x)
    # this is the model we will train
    model = Model(inputs=densenet_121_base_model.input, outputs=predictions)
elif model_structure == '169':
Example #14
0
                             weights=weights,
                             input_tensor=Input(shape=(299, 299, 3)),
                             pooling='avg')
    model = Model(input=base_model.input, output=base_model.layers[-1].output)
    image_size = (299, 299)
elif model_name == "inceptionresnetv2":
    base_model = InceptionResNetV2(include_top=include_top,
                                   weights=weights,
                                   input_tensor=Input(shape=(299, 299, 3)))
    base_model.summary()
    model = Model(input=base_model.input,
                  output=base_model.get_layer('custom').output)
    image_size = (299, 299)
elif model_name == 'densenet':
    base_model = DenseNet121(include_top=include_top,
                             weights=weights,
                             input_tensor=Input(shape=(224, 224, 3)),
                             input_shape=(224, 224, 3))
    # x = Dense(num_classes, activation='softmax')(base_model.get_layer('avg_pool').output)
    x = Flatten()(base_model.layers[-1].output)
    x = Dense(4096, name="output_layer")(x)
    model = Model(input=base_model.input, output=x)
    # model.summary()
    image_size = (224, 224)
else:
    base_model = None

print("[INFO] successfully loaded base model and model...")

# path to training dataset
train_labels = os.listdir(train_path)
Example #15
0
        target_size=(64, 64))

    #print(train_generator.classes)
    history = model.fit_generator(train_generator,
                                  steps_per_epoch=100,
                                  epochs=10,
                                  validation_data=validation_generator,
                                  validation_steps=10)

    for layer in model.layers[:400]:
        layer.trainable = False
    for layer in model.layers[400:]:
        layer.trainable = True

    history = model.fit_generator(train_generator,
                                  steps_per_epoch=100,
                                  epochs=10,
                                  validation_data=validation_generator,
                                  validation_steps=10)
    model.save('fine-tuned-%s.h5' % modality)


if __name__ == "__main__":

    modalities = ["DRAN", "DRCT", "DRMR", "DRXR", "DRPE", "DRCO", "DRUS"]

    base_model = DenseNet121(include_top=False, weights='imagenet')

    for modality in modalities:
        create_model(base_model, modality)
Example #16
0

train_array = image_array(train_images, len(train_images))
validation_array = image_array(validation_images, len(validation_images))
test_array = image_array(test_images, len(test_images))

np.save('train_array_size256.npy', train_array)
np.save('validation_array_size256.npy', validation_array)
np.save('test_array_size256.npy', test_array)

train_preprocessed = preprocess_input(train_array)
validation_preprocessed = preprocess_input(validation_array)
test_preprocessed = preprocess_input(test_array)

base_model = DenseNet121(input_shape=(256, 256, 3),
                         include_top=False,
                         weights='imagenet')

### Train Bottlenecks

bottleneck_features_train = base_model.predict(train_preprocessed, verbose=1)
#save as numpy array,
np.save('bottleneck_features_train_256size_densenet.npy',
        bottleneck_features_train)

bottleneck_features_validation = base_model.predict(validation_preprocessed,
                                                    verbose=1)
#save as numpy array,
np.save('bottleneck_features_val_256size_densenet.npy',
        bottleneck_features_validation)
Example #17
0
# -*- coding: utf-8 -*-

from keras.applications.xception import Xception
from keras.applications.vgg16 import VGG16
from keras.applications.vgg19 import VGG19
from keras.applications.resnet50 import ResNet50
from keras.applications.inception_v3 import InceptionV3
from keras.applications.inception_resnet_v2 import InceptionResNetV2
from keras.applications.mobilenet import MobileNet
from keras.applications.densenet import DenseNet121, DenseNet169, DenseNet201
from keras.applications.nasnet import NASNetLarge, NASNetMobile

xception = Xception()
vgg16 = VGG16()
vgg19 = VGG19()
res50 = ResNet50()
inception3 = InceptionV3()
inception_res2 = InceptionResNetV2()
mobile = MobileNet()
dense121 = DenseNet121()
dense169 = DenseNet169()
dense201 = DenseNet201()
nasnet_l = NASNetLarge()
nasnet_m = NASNetMobile()
Example #18
0
with open(args.pd, 'r') as f:
    f.read('w_minus')
    f.read('n_by_p')
    f.read('x')
    f.read('y')
    f.read('training_generator')
    f.read('validation_generator')"""
    
##############################Building Model############################################################
from sklearn.metrics import accuracy_score,roc_auc_score
from keras.optimizers import Adam

print('Building Model...')
#with tf.device('/cpu:0'):
model=Sequential()
model.add(DenseNet121(weights='imagenet',include_top=False,input_shape=(224,224,3)))#,dropout_rate=0.5))
model.add(GlobalAveragePooling2D())
model.add(Dropout(0.5))
model.add(Dense(14,activation='sigmoid'))# adds last layer to have 14 neurons [[14 ,kernel_regularizer=regularizers.l2(0.01))]]
model.summary()
parallel_model = model
#parallel_model = multi_gpu_model(model, gpus=2) # model has been built i.e the structure of CNN model has been built



def custom_loss(targets, output):
    _epsilon = tf.convert_to_tensor(K.epsilon(),output.dtype.base_dtype)#, output.dtype.base_dtype
    output = tf.clip_by_value(tf.cast(output,tf.float32), _epsilon, 1 - _epsilon)
    output = tf.math.log(output / (1 - output))
    weight=tf.convert_to_tensor(w_minus,dtype='float32')
    return K.mean(weight*tf.nn.weighted_cross_entropy_with_logits(logits=output, labels=targets, pos_weight=np.array(n_by_p), name=None),axis=-1)
def first_phase(trained=True,
                printGap=True,
                first_phase_train_reps=first_phase_train_reps):
    global denseNet121_model
    tensorboard = TensorBoard(log_dir=first_phase_folder + 'tb_logs',
                              batch_size=batch_size)

    if not trained:
        # create the base pre-trained model
        input_tensor = Input(shape=input_shape)
        base_model = DenseNet121(input_tensor=input_tensor,
                                 weights='imagenet',
                                 include_top=False)

        # add a global spatial average pooling layer
        x = base_model.output
        x = GlobalAveragePooling2D()(x)
        # add a fully-connected layer
        x = Dense(1024, activation='relu')(x)
        # add a logistic layer
        predictions = Dense(classes_num, activation='softmax')(x)

        # this is the model we will train
        denseNet121_model = Model(inputs=base_model.input, outputs=predictions)
        denseNet121_model.compile(optimizer=Adam(lr=0.0001),
                                  loss='categorical_crossentropy',
                                  metrics=['acc'])
    else:
        denseNet121_model = load_model(data_folder +
                                       '1st_phase_denseNet121_model.h5')

    if not os.path.exists(first_phase_folder):
        os.makedirs(first_phase_folder)

    for i in range(first_phase_train_reps):
        history = denseNet121_model.fit_generator(
            train_img_class_gen,
            steps_per_epoch=steps_per_small_epoch,
            epochs=small_epochs,
            verbose=2,
            validation_data=val_img_class_gen,
            validation_steps=val_steps_per_small_epoch,
            workers=4,
            callbacks=[tensorboard])
        print('itr', i)
        if i % saves_per_epoch == 0:
            print('{} epoch completed'.format(int(i / saves_per_epoch)))

        if i >= 5:
            ts = calendar.timegm(time.gmtime())
            denseNet121_model.save(first_phase_folder + str(ts) +
                                   '_denseNet121_model.h5')
            save_obj(history.history,
                     str(ts) + '_denseNet121_history',
                     folder=first_phase_folder)

        if printGap:
            steps = len(val_names_list) / batch_size
            predicts = denseNet121_model.predict_generator(
                val_img_class_gen, steps=steps / 10, verbose=2)  ##########
            predProb = np.max(predicts, axis=-1)
            predId = np.argmax(predicts, axis=-1)
            trueId = list(
                map(
                    lambda x: val_name_id_dict[str(x).split('.')[0].split('/')[
                        1]], [name for name in val_img_class_gen.filenames]))
            gap = GAP_vector(predId, predProb, trueId)
            print('gap: ', gap)

        denseNet121_model.save(data_folder + '1st_phase_denseNet121_model.h5')
Example #20
0
def load_DenseNet121(input_shape,
                     classes,
                     optimizer,
                     function,
                     include_top=True,
                     verbose=0,
                     compile=False,
                     trainable=False):
    #loading DenseNet121 pretrained model
    base_model = DenseNet121(input_shape=input_shape,
                             include_top=False,
                             weights='imagenet')

    #freeze layers if trainable = false
    for layer in base_model.layers:
        layer.trainable = trainable

    if include_top:
        if classes == 2:
            #binary case
            x = base_model.output
            x = Dense(128)(x)
            x = GlobalAveragePooling2D(name='gavp')(x)
            predictions = Dense(classes, activation='softmax', name='soft')(x)

            model = Model(inputs=base_model.input, output=predictions)

            model.compile(optimizer=optimizer,
                          loss='categorical_crossentropy',
                          metrics=['accuracy'])
        else:
            #adding a global spatial average pooling layer
            x = base_model.output
            x = Flatten(input_shape=input_shape, name='f')(x)
            x = Dense(1024,
                      activation='relu',
                      name='d1',
                      kernel_regularizer=l2(.01))(x)
            x = Dropout(0.5)(x)
            x = Dense(1024, activation="relu")(x)
            #logistic layer for prediction
            predictions = Dense(classes, activation='softmax', name='d2')(x)

            model = Model(inputs=base_model.input, output=predictions)

            model.compile(optimizer=optimizer,
                          loss='categorical_crossentropy',
                          metrics=['accuracy'])
    else:
        x = base_model.output
        out = AveragePooling2D((7, 7), name='avg_pool_app')(x)
        model = Model(inputs=base_model.input, output=out)

        if classes == 2:
            model.compile(optimizer=optimizer,
                          loss='binary_crossentropy',
                          metrics=['accuracy'])
        else:
            model.compile(optimizer=optimizer,
                          loss='categorical_crossentropy',
                          metrics=['accuracy'])

    if verbose == 1:
        model.summary()
    elif verbose == 2:
        print_layers(model)

    return model
Example #21
0
def train(model_dir, results_subdir, random_seed, resolution):
    np.random.seed(random_seed)
    tf.set_random_seed(np.random.randint(1 << 31))
    session_conf = tf.ConfigProto(intra_op_parallelism_threads=1,
                                  inter_op_parallelism_threads=1)
    session_conf.gpu_options.allow_growth = True
    sess = tf.Session(graph=tf.get_default_graph(), config=session_conf)
    set_session(sess)

    # Configure number of GPUs:
    num_gpu = len(K.tensorflow_backend._get_available_gpus())
    print("Number of available GPUs:", num_gpu)

    # parser config
    config_file = model_dir + "/config.ini"
    print("Config File Path:", config_file, flush=True)
    assert os.path.isfile(config_file)
    cp = ConfigParser()
    cp.read(config_file)

    # default config
    base_model_name = cp["DEFAULT"].get("base_model_name")
    output_dir = os.path.join(results_subdir, "classification_results/train")
    # check output_dir, create it if not exists
    print("Output Directory:", output_dir, flush=True)
    if not os.path.isdir(output_dir):
        os.makedirs(output_dir)

    # train config
    path_model_base_weights = cp["TRAIN"].get("path_model_base_weights")
    use_trained_model_weights = cp["TRAIN"].getboolean(
        "use_trained_model_weights")
    use_best_weights = cp["TRAIN"].getboolean("use_best_weights")
    output_weights_name = cp["TRAIN"].get("output_weights_name")
    epochs = cp["TRAIN"].getint("epochs")
    batch_size = cp["TRAIN"].getint("batch_size")
    initial_learning_rate = cp["TRAIN"].getfloat("initial_learning_rate")
    image_dimension = cp["TRAIN"].getint("image_dimension")
    patience_reduce_lr = cp["TRAIN"].getint("patience_reduce_lr")
    reduce_lr = cp["TRAIN"].getfloat("reduce_lr")
    min_lr = cp["TRAIN"].getfloat("min_lr")
    positive_weights_multiply = cp["TRAIN"].getfloat(
        "positive_weights_multiply")
    patience = cp["TRAIN"].getint("patience")
    samples_per_epoch = cp["TRAIN"].getint("samples_per_epoch")
    gan_resolution = resolution

    print("** DenseNet input resolution:", image_dimension, flush=True)
    print("** GAN image resolution:", gan_resolution, flush=True)
    print("** Patience epochs", patience, flush=True)
    print("** Samples per epoch:", samples_per_epoch, flush=True)

    log2_record = int(np.log2(gan_resolution))
    record_file_ending = "*" + np.str(log2_record) + ".tfrecords"
    print("** Resolution ",
          gan_resolution,
          " corresponds to ",
          record_file_ending,
          " TFRecord file.",
          flush=True)

    # if previously trained weights is used, never re-split
    if use_trained_model_weights:
        print("** use trained model weights **", flush=True)
        training_stats_file = os.path.join(output_dir, ".training_stats.json")
        if os.path.isfile(training_stats_file):
            # TODO: add loading previous learning rate?
            training_stats = json.load(open(training_stats_file))
        else:
            training_stats = {}
    else:
        # start over
        training_stats = {}

    show_model_summary = cp["TRAIN"].getboolean("show_model_summary")
    running_flag_file = os.path.join(output_dir, ".training.lock")
    if os.path.isfile(running_flag_file):
        raise RuntimeError("A process is running in this directory!!!")
    else:
        open(running_flag_file, "a").close()

    try:
        print("backup config file to", output_dir, flush=True)
        shutil.copy(config_file,
                    os.path.join(output_dir,
                                 os.path.split(config_file)[1]))

        tfrecord_dir_tr = os.path.join(results_subdir, "inference/train")
        tfrecord_dir_vl = os.path.join(results_subdir, "inference/valid")

        shutil.copy(tfrecord_dir_tr + "/train.csv", output_dir)
        shutil.copy(tfrecord_dir_vl + "/valid.csv", output_dir)

        # Get class names
        class_names = get_class_names(output_dir, "train")

        # get train sample counts
        train_counts, train_pos_counts = get_sample_counts(
            output_dir, "train", class_names)
        valid_counts, _ = get_sample_counts(output_dir, "valid", class_names)

        print("Total Training Data:", train_counts, flush=True)
        print("Total Validation Data:", valid_counts, flush=True)
        train_steps = int(min(train_counts, samples_per_epoch) / batch_size)
        print("** train_steps:", train_steps, flush=True)
        validation_steps = int(np.floor(valid_counts / batch_size))
        print("** validation_steps:", validation_steps, flush=True)

        # compute class weights
        print("** compute class weights from training data **", flush=True)
        class_weights = get_class_weights(
            train_counts,
            train_pos_counts,
            multiply=positive_weights_multiply,
        )
        print("** class_weights **", flush=True)
        print(class_weights)

        print("** load model **", flush=True)
        if use_trained_model_weights:
            if use_best_weights:
                model_weights_file = os.path.join(
                    output_dir, "best_" + output_weights_name)
            else:
                model_weights_file = os.path.join(output_dir,
                                                  output_weights_name)
        else:
            model_weights_file = None

        # Use downloaded weights
        if os.path.isfile(path_model_base_weights):
            base_weights = path_model_base_weights
            print("** Base weights will be loaded.", flush=True)
        else:
            base_weights = None
            print("** No Base weights.", flush=True)

        # Get Model
        # ------------------------------------
        input_shape = (image_dimension, image_dimension, 3)
        img_input = Input(shape=input_shape)

        base_model = DenseNet121(include_top=False,
                                 weights=base_weights,
                                 input_tensor=img_input,
                                 input_shape=input_shape,
                                 pooling="avg")

        x = base_model.output
        predictions = Dense(len(class_names),
                            activation="sigmoid",
                            name="predictions")(x)
        model = Model(inputs=img_input, outputs=predictions)

        if use_trained_model_weights and model_weights_file != None:
            print("** load model weights_path:",
                  model_weights_file,
                  flush=True)
            model.load_weights(model_weights_file)
        # ------------------------------------

        if show_model_summary:
            print(model.summary())

        print("** create image generators", flush=True)
        train_seq = TFWrapper(tfrecord_dir=tfrecord_dir_tr,
                              record_file_endings=record_file_ending,
                              batch_size=batch_size,
                              model_target_size=(image_dimension,
                                                 image_dimension),
                              steps=train_steps,
                              augment=True,
                              shuffle=True,
                              prefetch=True,
                              repeat=True)

        valid_seq = TFWrapper(tfrecord_dir=tfrecord_dir_vl,
                              record_file_endings=record_file_ending,
                              batch_size=batch_size,
                              model_target_size=(image_dimension,
                                                 image_dimension),
                              steps=None,
                              augment=False,
                              shuffle=False,
                              prefetch=True,
                              repeat=True)

        # Initialise train and valid iterats
        print("** Initialise train and valid iterators", flush=True)
        train_seq.initialise()
        valid_seq.initialise()

        output_weights_path = os.path.join(output_dir, output_weights_name)
        print("** set output weights path to:",
              output_weights_path,
              flush=True)
        """
        if num_gpu > 1:
            print("** MULTI_gpu_model is used! gpus=", num_gpu)
            AltModelCheckpoint = _define_alt_model_checkpoint(ModelCheckpoint)
            model_train = multi_gpu_model(model, num_gpu)
            checkpoint = AltModelCheckpoint(
                output_weights_path, model,
                save_weights_only=True,
                save_best_only=False,
                verbose=1)
        """
        print("** SINGLE_gpu_model is used!", flush=True)
        model_train = model
        checkpoint = ModelCheckpoint(
            output_weights_path,
            save_weights_only=True,
            save_best_only=False,
            verbose=1,
        )

        print("** compile model with class weights **", flush=True)
        optimizer = Adam(lr=initial_learning_rate)
        model_train.compile(optimizer=optimizer, loss="binary_crossentropy")

        auroc = MultipleClassAUROC(sequence=valid_seq,
                                   class_names=class_names,
                                   weights_path=output_weights_path,
                                   stats=training_stats,
                                   early_stop_p=patience,
                                   learn_rate_p=patience_reduce_lr,
                                   learn_rate_f=reduce_lr,
                                   min_lr=min_lr,
                                   workers=0)

        callbacks = [
            checkpoint,
            TensorBoard(log_dir=os.path.join(output_dir, "logs"),
                        batch_size=batch_size), auroc
        ]

        print("** start training **", flush=True)
        history = model_train.fit_generator(
            generator=train_seq,
            steps_per_epoch=train_steps,
            epochs=epochs,
            validation_data=valid_seq,
            validation_steps=validation_steps,
            callbacks=callbacks,
            class_weight=class_weights,
            workers=0,
            shuffle=False,
        )

        # dump history
        print("** dump history **", flush=True)
        with open(os.path.join(output_dir, "history.pkl"), "wb") as f:
            pickle.dump({
                "history": history.history,
                "auroc": auroc.aurocs,
            }, f)
        print("** done! **", flush=True)

    finally:
        os.remove(running_flag_file)
Example #22
0
elif args.model == "Xception":
    from keras.applications.xception import preprocess_input
    preprocessing_function = preprocess_input
    base_model = Xception(weights='imagenet', include_top=False, input_shape=(HEIGHT, WIDTH, 3))
elif args.model == "InceptionResNetV2":
    from keras.applications.inceptionresnetv2 import preprocess_input
    preprocessing_function = preprocess_input
    base_model = InceptionResNetV2(weights='imagenet', include_top=False, input_shape=(HEIGHT, WIDTH, 3))
elif args.model == "MobileNet":
    from keras.applications.mobilenet import preprocess_input
    preprocessing_function = preprocess_input
    base_model = MobileNet(weights='imagenet', include_top=False, input_shape=(HEIGHT, WIDTH, 3))
elif args.model == "DenseNet121":
    from keras.applications.densenet import preprocess_input
    preprocessing_function = preprocess_input
    base_model = DenseNet121(weights='imagenet', include_top=False, input_shape=(HEIGHT, WIDTH, 3))
elif args.model == "DenseNet169":
    from keras.applications.densenet import preprocess_input
    preprocessing_function = preprocess_input
    base_model = DenseNet169(weights='imagenet', include_top=False, input_shape=(HEIGHT, WIDTH, 3))
elif args.model == "DenseNet201":
    from keras.applications.densenet import preprocess_input
    preprocessing_function = preprocess_input
    base_model = DenseNet201(weights='imagenet', include_top=False, input_shape=(HEIGHT, WIDTH, 3))
elif args.model == "NASNetLarge":
    from keras.applications.nasnet import preprocess_input
    preprocessing_function = preprocess_input
    base_model = NASNetLarge(weights='imagenet', include_top=True, input_shape=(HEIGHT, WIDTH, 3))
elif args.model == "NASNetMobile":
    from keras.applications.nasnet import preprocess_input
    preprocessing_function = preprocess_input
Example #23
0
The following demonstrates how to compute the predictions
of a pretrained deep learning model obtained from 
`keras <https://keras.io/>`_
with *onnxruntime*. The conversion requires
`keras <https://keras.io/>`_,
`tensorflow <https://www.tensorflow.org/>`_,
`keras-onnx <https://github.com/onnx/keras-onnx/>`_,
`onnxmltools <https://pypi.org/project/onnxmltools/>`_
but then only *onnxruntime* is required
to compute the predictions.
"""
import os
if not os.path.exists('dense121.onnx'):
    from keras.applications.densenet import DenseNet121
    model = DenseNet121(include_top=True, weights='imagenet')

    from keras2onnx import convert_keras
    onx = convert_keras(model, 'dense121.onnx')
    with open("dense121.onnx", "wb") as f:
        f.write(onx.SerializeToString())

##################################
# Let's load an image (source: wikipedia).

from keras.preprocessing.image import array_to_img, img_to_array, load_img
img = load_img('Sannosawa1.jpg')
ximg = img_to_array(img)

import matplotlib.pyplot as plt
plt.imshow(ximg / 255)
Example #24
0
def main():
    tf.set_random_seed(1234)  # for producing the same images

    if not hasattr(keras.backend, "tf"):
        raise RuntimeError("This tutorial requires keras to be configured"
                           " to use the TensorFlow backend.")

    if keras.backend.image_dim_ordering() != 'tf':
        keras.backend.set_image_dim_ordering('tf')
        print("INFO: '~/.keras/keras.json' sets 'image_dim_ordering' to "
              "'th', temporarily setting to 'tf'")

    sess = tf.Session()
    keras.backend.set_session(sess)

    # load and preprocess dataset
    data_spec = DataSpec(batch_size=TOT_IMAGES,
                         scale_size=256,
                         crop_size=224,
                         isotropic=False)
    image_producer = ImageNetProducer(data_path=INPUT_DIR,
                                      num_images=TOT_IMAGES,
                                      data_spec=data_spec,
                                      batch_size=TOT_IMAGES)

    # Define input TF placeholder
    x = tf.placeholder(tf.float32, shape=(None, 224, 224, 3))
    y = tf.placeholder(tf.float32, shape=(None, 1000))
    class_num = 1000

    # load target model and produce data
    # model = preprocess layer + pretrained model
    from keras.applications.densenet import DenseNet121
    from keras.applications.densenet import preprocess_input
    pretrained_model = DenseNet121(weights='imagenet')
    image_producer.startover()
    target_model = keras_model_wrapper(pretrained_model,
                                       preprocess_input,
                                       x=x,
                                       y=y)
    for (indices, label, names, images) in image_producer.batches(sess):
        images = np.array(images)
        label = np_utils.to_categorical(np.array(label), class_num)
    accuracy = model_eval(sess,
                          x,
                          y,
                          target_model.predictions,
                          images,
                          label,
                          args={'batch_size': 32})
    print('Test accuracy of wrapped target model:{:.4f}'.format(accuracy))

    # data information
    x_test, y_test = images, label  # x_test [0, 255]
    print('loading %s images in total ', images.shape)
    print(np.min(x_test), np.max(x_test))

    # local attack specific parameters
    clip_min = args["lower"]
    clip_max = args["upper"]
    nb_imgs = args["nb_imgs"]
    li_eps = args["epsilon"]
    targeted_true = True if args["attack_type"] == 'targeted' else False
    k = args["K"]  # iteration
    a = args["learning_rate"]  # step size

    # Test the accuracy of targeted attacks, need to redefine the attack graph
    target_ys_one_hot, orig_images, target_ys, orig_labels = generate_attack_inputs(
        target_model, x_test, y_test, class_num, nb_imgs)

    # Set random seed to improve reproducibility
    tf.set_random_seed(args["seed"])
    np.random.seed(args["seed"])

    # test whether adversarial examples exsit, if no, generate it, otherwise, load it.
    prefix = "Results"
    prefix = os.path.join(prefix, str(args["seed"]))

    if not os.path.exists(prefix):  # no history info
        # load local models or define the architecture
        local_model_types = ['VGG16', 'VGG19', 'resnet50']
        local_model_ls = []
        pred_ls = []
        for model_type in local_model_types:
            pretrained_model, preprocess_input_func = load_local_model(
                model_type)
            local_model = keras_model_wrapper(pretrained_model,
                                              preprocess_input_func,
                                              x=x,
                                              y=y)
            accuracy = model_eval(sess,
                                  x,
                                  y,
                                  local_model.predictions,
                                  images,
                                  label,
                                  args={'batch_size': 32})
            print('Test accuracy of model {}: {:.4f}'.format(
                model_type, accuracy))
            local_model_ls.append(local_model)
            pred_ls.append(local_model.predictions)

        # load local model attack graph
        if targeted_true:
            orig_img_loss = compute_cw_loss(target_model,
                                            orig_images,
                                            target_ys_one_hot,
                                            targeted=targeted_true)
        else:
            orig_img_loss = compute_cw_loss(target_model,
                                            orig_images,
                                            orig_labels,
                                            targeted=targeted_true)

        local_attack_graph = LinfPGDAttack(local_model_ls,
                                           epsilon=li_eps,
                                           k=k,
                                           a=a,
                                           random_start=False,
                                           loss_func='xent',
                                           targeted=targeted_true,
                                           x=x,
                                           y=y)
        # pgd attack to local models and generate adversarial example seed
        if targeted_true:
            _, pred_labs, local_aes, pgd_cnt_mat, max_loss, \
            min_loss, ave_loss, max_gap, min_gap, ave_gap = local_attack_in_batches(sess,
                              orig_images,
                              target_ys_one_hot,
                              eval_batch_size = 1,
                              attack_graph=local_attack_graph,
                              model=target_model,
                              clip_min=clip_min,
                              clip_max=clip_max)
        else:
            _, pred_labs, local_aes, pgd_cnt_mat, max_loss, \
            min_loss, ave_loss, max_gap, min_gap, ave_gap = local_attack_in_batches(sess,
                              orig_images,
                              orig_labels,
                              eval_batch_size = 1,
                              attack_graph=local_attack_graph,
                              model=target_model,
                              clip_min=clip_min,
                              clip_max=clip_max)

        # calculate the loss for all adversarial seeds
        if targeted_true:
            adv_img_loss = compute_cw_loss(target_model,
                                           local_aes,
                                           target_ys_one_hot,
                                           targeted=targeted_true)
        else:
            adv_img_loss = compute_cw_loss(target_model,
                                           local_aes,
                                           orig_labels,
                                           targeted=targeted_true)

        success_rate = accuracy_score(target_ys, pred_labs)
        print(
            '** Success rate of targeted adversarial examples generated from local models: **'
            + str(success_rate))
        accuracy = accuracy_score(np.argmax(orig_labels, axis=1), pred_labs)
        print(
            '** Success rate of targeted adversarial examples generated by local models (untargeted): **'
            + str(1 - accuracy))

        # l-inf distance of orig_images and local_aes
        dist = local_aes - orig_images
        l_fin_dist = np.linalg.norm(dist.reshape(nb_imgs, -1), np.inf, axis=1)

        # save the generated local adversarial example ...
        os.makedirs(prefix)
        # save statistics
        fname = prefix + '/adv_img_loss.txt'
        np.savetxt(fname, adv_img_loss)
        fname = prefix + '/orig_img_loss.txt'
        np.savetxt(fname, orig_img_loss)
        fname = prefix + '/pgd_cnt_mat.txt'
        np.savetxt(fname, pgd_cnt_mat)
        fname = prefix + '/max_loss.txt'
        np.savetxt(fname, max_loss)
        fname = prefix + '/min_loss.txt'
        np.savetxt(fname, min_loss)
        fname = prefix + '/ave_loss.txt'
        np.savetxt(fname, ave_loss)
        fname = prefix + '/max_gap.txt'
        np.savetxt(fname, max_gap)
        fname = prefix + '/min_gap.txt'
        np.savetxt(fname, min_gap)
        fname = prefix + '/ave_gap.txt'
        np.savetxt(fname, ave_gap)

        # save output for local attacks
        fname = os.path.join(prefix, 'local_aes.npy')
        np.save(fname, local_aes)
        fname = os.path.join(prefix, 'orig_images.npy')
        np.save(fname, orig_images)
        fname = os.path.join(prefix, 'target_ys.npy')
        np.save(fname, target_ys)
        fname = os.path.join(prefix, 'target_ys_one_hot.npy')
        np.save(fname, target_ys_one_hot)
    else:
        print('loading data from files')
        local_aes = np.load(os.path.join(prefix, 'local_aes.npy'))
        orig_images = np.load(os.path.join(prefix, 'orig_images.npy'))
        target_ys = np.load(os.path.join(prefix, 'target_ys.npy'))
        target_ys_one_hot = np.load(
            os.path.join(prefix, 'target_ys_one_hot.npy'))

    assert local_aes.shape == (nb_imgs, 224, 224, 3)
    assert orig_images.shape == (nb_imgs, 224, 224, 3)
    assert target_ys.shape == (nb_imgs, )
    assert target_ys_one_hot.shape == (nb_imgs, class_num)

    print('begin blackbox attack')

    # load attack parameters
    if args["blackbox_attack"] == "nes":
        from NES import nes_attack

        with open("nes.json") as infile:
            json_dict = json.load(infile)
            args.update(json_dict)

    elif args["blackbox_attack"] == "autozoom":
        from AUTOZOOM import AutoZOOM, autozoom_attack

        with open("autozoom.json") as infile:
            json_dict = json.load(infile)
            args.update(json_dict)

        # load autoencoder
        encoder = load_model(
            os.path.join(args["codec_dir"], 'imagenet_2_whole_encoder.h5'))
        decoder = load_model(
            os.path.join(args["codec_dir"], 'imagenet_2_whole_decoder.h5'))
        args["img_resize"] = decoder.input_shape[1]

        # define black-box model graph of autozoom
        autozoom_attack_graph = AutoZOOM(sess,
                                         target_model,
                                         args,
                                         decoder,
                                         num_channels=3,
                                         image_size=224,
                                         num_labels=class_num)
    else:
        raise NotImplementedError

    num_queries_list = []
    success_flags = []
    # fetch batch
    orig_images = orig_images[args["bstart"]:args["bend"]]
    target_ys = target_ys[args["bstart"]:args["bend"]]
    local_aes = local_aes[args["bstart"]:args["bend"]]
    target_ys_one_hot = target_ys_one_hot[args['bstart']:args['bend']]

    # begin loop
    for idx in range(len(orig_images)):

        initial_img = orig_images[idx:idx + 1]
        target_class = target_ys[idx]
        target_y_one_hot = target_ys_one_hot[idx]

        if args["attack_seed_type"] == 'adv':
            print('attack seed is %s' % args["attack_seed_type"])
            attack_seed = local_aes[idx:idx + 1]
        else:
            print('attack seed is %s' % args["attack_seed_type"])
            attack_seed = orig_images[idx:idx + 1]

        if args["blackbox_attack"] == "nes":
            _, num_queries, ae = nes_attack(sess, args, target_model,
                                            attack_seed, initial_img,
                                            target_class, class_num,
                                            IMAGE_SIZE)
        elif args["blackbox_attack"] == "autozoom":
            ae, num_queries = autozoom_attack(autozoom_attack_graph,
                                              attack_seed, initial_img,
                                              target_y_one_hot)
        else:
            raise NotImplementedError

        if num_queries == args["max_queries"]:
            success_flags.append(0)
        else:
            success_flags.append(1)
        num_queries_list.append(num_queries)

    # save query number and success
    fname = os.path.join(prefix,
                         '{}_num_queries.txt'.format(args["attack_seed_type"]))
    np.savetxt(fname, num_queries_list)
    fname = os.path.join(
        prefix, '{}_success_flags.txt'.format(args["attack_seed_type"]))
    np.savetxt(fname, success_flags)

    print('finish blackbox attack')
Example #25
0

# Import Densenet from Keras
from keras.applications.densenet import DenseNet121
from keras.layers import Dense, GlobalAveragePooling2D
from keras.models import Model
from keras import backend as K


# For your work in the assignment, you'll be loading a set of pre-trained weights to reduce training time.

# In[2]:


# Create the base pre-trained model
base_model = DenseNet121(weights='./nih/densenet.hdf5', include_top=False);


# View a summary of the model

# In[3]:


# Print the model summary
base_model.summary()


# In[4]:


# Print out the first five layers
Example #26
0
def DenseNet121(**kwargs):
    from keras.applications.densenet import DenseNet121
    return DenseNet121(**kwargs)
Example #27
0
preds_mobile = model.predict(x)
print('Xception Predicted:', decode_predictions(preds_mobile, top=5)[0])

# VGG19
from keras.applications.vgg19 import VGG19, preprocess_input, decode_predictions
x = preprocess_input(x)
model = VGG19(weights='imagenet')
preds_mobile = model.predict(x)
print('VGG16 Predicted:', decode_predictions(preds_mobile, top=5)[0])

# ResNet50
from keras.applications.resnet50 import ResNet50, preprocess_input, decode_predictions
x = preprocess_input(x)
model = ResNet50(weights='imagenet')
preds_resnet50 = model.predict(x)
print('ResNet50 Predicted:', decode_predictions(preds_resnet50, top=5)[0])

# DenseNet121
from keras.applications.densenet import DenseNet121, preprocess_input, decode_predictions
x = preprocess_input(x)
model = DenseNet121(weights='imagenet')
preds_mobile = model.predict(x)
print('DenseNet121 Predicted:', decode_predictions(preds_mobile, top=5)[0])

# InceptionResNetV2
from keras.applications.inception_resnet_v2 import InceptionResNetV2, preprocess_input, decode_predictions
x = preprocess_input(x)
model = InceptionResNetV2(weights='imagenet')
preds_mobile = model.predict(x)
print('InceptionResNetV2 Predicted:',
      decode_predictions(preds_mobile, top=5)[0])
                                    height_shift_range=0.2,
                                    zoom_range=0.2,
                                    shear_range=0.2)
train_generator = train_data_gen.flow_from_directory(directory='assets/train/',
                                                     target_size=TARGET_SIZE,
                                                     batch_size=BATCH_SIZE,
                                                     class_mode='categorical')

valid_data_gen = ImageDataGenerator(rescale=1. / 255)
valid_generator = valid_data_gen.flow_from_directory(directory='assets/valid/',
                                                     target_size=TARGET_SIZE,
                                                     batch_size=BATCH_SIZE,
                                                     class_mode='categorical')

base_model = DenseNet121(weights='imagenet',
                         include_top=True,
                         input_shape=(224, 224, 3))
main = Dropout(0.1)(base_model.layers[-2].output)
predictions = Dense(128,
                    activation='softmax',
                    kernel_regularizer=regularizers.l2(0.0001))(main)

model = Model(inputs=base_model.input, outputs=predictions)

model.summary()
model.compile(optimizer=Adam(lr=0.00003),
              loss='categorical_crossentropy',
              metrics=[top1_loss])

file_path = MODEL_PATH + 'model.hdf5'
# train the model on the new data for a few epochs
Example #29
0
print(x_test.shape[0], 'test samples')
print('y_train shape:', y_train.shape)

# Convert class vectors to binary class matrices.
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)

if args.resume:
    print('resume from checkpoint')
    model = keras.models.load_model(save_file)
else:
    print('train from start')
    model = models.Sequential()
    
    if '121' in args_model:
        base_model = DenseNet121(weights=None, include_top=False, input_shape=(32, 32, 3), pooling='avg')
    elif '169' in args_model:
        base_model = DenseNet169(weights=None, include_top=False, input_shape=(32, 32, 3), pooling='avg')
    elif '201' in args_model:
        base_model = DenseNet201(weights=None, include_top=False, input_shape=(32, 32, 3), pooling='avg')
        
       
    model.add(base_model)
    #model.add(layers.Flatten())
    #model.add(layers.BatchNormalization())
    #model.add(layers.Dense(128, activation='relu'))
    #model.add(layers.Dropout(0.5))
    #model.add(layers.BatchNormalization())
    #model.add(layers.Dense(64, activation='relu'))
    #model.add(layers.Dropout(0.5))
    #model.add(layers.BatchNormalization())
Example #30
0
def test(model_dir, data_dir, results_subdir, random_seed, resolution):
    np.random.seed(random_seed)
    tf.set_random_seed(np.random.randint(1 << 31))
    session_conf = tf.ConfigProto(intra_op_parallelism_threads=1,
                                  inter_op_parallelism_threads=1)
    sess = tf.Session(graph=tf.get_default_graph(), config=session_conf)
    set_session(sess)

    # parser config
    config_file = model_dir + "/config.ini"
    print("Config File Path:", config_file, flush=True)
    assert os.path.isfile(config_file)
    cp = ConfigParser()
    cp.read(config_file)

    output_dir = os.path.join(results_subdir, "classification_results/test")
    print("Output Directory:", output_dir, flush=True)
    if not os.path.isdir(output_dir):
        os.makedirs(output_dir)

    # default config
    image_dimension = cp["TRAIN"].getint("image_dimension")
    gan_resolution = resolution
    batch_size = cp["TEST"].getint("batch_size")
    use_best_weights = cp["TEST"].getboolean("use_best_weights")

    if use_best_weights:
        print("** Using BEST weights", flush=True)
        model_weights_path = os.path.join(
            results_subdir, "classification_results/train/best_weights.h5")
    else:
        print("** Using LAST weights", flush=True)
        model_weights_path = os.path.join(
            results_subdir, "classification_results/train/weights.h5")

    print("** DenseNet Input Resolution:", image_dimension, flush=True)
    print("** GAN Image Resolution:", gan_resolution, flush=True)

    # get test sample count
    test_dir = os.path.join(results_subdir, "inference/test")
    shutil.copy(test_dir + "/test.csv", output_dir)

    # Get class names
    class_names = get_class_names(output_dir, "test")

    tfrecord_dir_te = os.path.join(data_dir, "test")
    test_counts, _ = get_sample_counts(output_dir, "test", class_names)

    # get indicies (all of csv file for validation)
    print("** test counts:", test_counts, flush=True)

    # compute steps
    test_steps = int(np.floor(test_counts / batch_size))
    print("** test_steps:", test_steps, flush=True)

    log2_record = int(np.log2(gan_resolution))
    record_file_ending = "*" + np.str(log2_record) + ".tfrecords"
    print("** resolution ",
          gan_resolution,
          " corresponds to ",
          record_file_ending,
          " TFRecord file.",
          flush=True)

    # Get Model
    # ------------------------------------
    input_shape = (image_dimension, image_dimension, 3)
    img_input = Input(shape=input_shape)

    base_model = DenseNet121(include_top=False,
                             weights=None,
                             input_tensor=img_input,
                             input_shape=input_shape,
                             pooling="avg")

    x = base_model.output
    predictions = Dense(len(class_names),
                        activation="sigmoid",
                        name="predictions")(x)
    model = Model(inputs=img_input, outputs=predictions)

    print(" ** load model from:", model_weights_path, flush=True)
    model.load_weights(model_weights_path)
    # ------------------------------------

    print("** load test generator **", flush=True)
    test_seq = TFWrapper(tfrecord_dir=tfrecord_dir_te,
                         record_file_endings=record_file_ending,
                         batch_size=batch_size,
                         model_target_size=(image_dimension, image_dimension),
                         steps=None,
                         augment=False,
                         shuffle=False,
                         prefetch=True,
                         repeat=False)

    print("** make prediction **", flush=True)
    test_seq.initialise()  #MAKE SURE REINIT
    y_hat = model.predict_generator(test_seq, workers=0)
    test_seq.initialise()  #MAKE SURE REINIT
    y = test_seq.get_y_true()
    test_log_path = os.path.join(output_dir, "test.log")
    print("** write log to", test_log_path, flush=True)
    aurocs = []
    tpr_fpr_thr = []
    with open(test_log_path, "w") as f:
        for i in range(len(class_names)):
            tpr, fpr, thr = roc_curve(y[:, i], y_hat[:, i])
            roc_rates = np.concatenate(
                (fpr.reshape(-1, 1), tpr.reshape(-1, 1), thr.reshape(-1, 1)),
                axis=1)
            tpr_fpr_thr.append(roc_rates)
            try:
                score = roc_auc_score(y[:, i], y_hat[:, i])
                if score < 0.5:
                    score = 1. - score
                aurocs.append(score)
            except ValueError:
                score = 0
            f.write(np.str(class_names[i]) + " : " + np.str(score) + "\n")
        mean_auroc = np.mean(aurocs)
        f.write("-------------------------\n")
        f.write("mean auroc: " + np.str(mean_auroc) + "\n")
        print("mean auroc:", mean_auroc, flush=True)

    roc_char = np.asarray(tpr_fpr_thr)
    np.save(output_dir + "/roc_char.npy", roc_char)
    print("Saved ROC data (TPR, FPR, THR) to:",
          output_dir + "/roc_char.npy",
          flush=True)