def build_model(self, input_dim): input1 = Input(shape=(input_dim, )) input2 = Input((config.img_x, config.img_y, 1)) input3 = Input((config.img_x, config.img_y, 1)) # 一维特征深度网络 x1 = Dense(32, activation='relu')(input1) x1 = Dropout(0.15)(x1) x1 = Dense(8, activation='relu')(x1) ## s/n 矩阵特征卷积网络 x2 = ResnetBuilder.build_resnet_18(input2, 2) #x2 = Conv2D(2, (3, 3), padding='same')(input2) # 62*62*8 #x2 = MaxPooling2D((2, 2), strides=(1, 1), padding='same')(x2) # 31*31*8 #x2 = Conv2D(4, (3, 3), padding='same')(x2) # 29*29*16 #x2 = MaxPooling2D((2, 2), strides=(1, 1), padding='same')(x2) # 15*15*16 #x2 = Conv2D(4, (3, 3), padding='same')(x2) # 13*13*32 #x2 = MaxPooling2D((2, 2), strides=(1, 1), padding='same')(x2) # 7*7*32 #x2 = Conv2D(16, (3, 3), padding='same')(x2) # 5*5*32 #x2 = MaxPooling2D((2, 2), strides=(1, 1), padding='same')(x2) # 3*3*32 #x2 = Flatten()(x2) #x2 = Dense(32, activation='relu')(x2) #x2 = Dropout(0.15)(x2) #x2 = Dense(8, activation='relu')(x2) ## s/n 矩阵特征卷积网络 x3 = ResnetBuilder.build_resnet_18(input3, 2) #x3 = Conv2D(2, (3, 3), padding='same')(input3) # 62*62*8 #x3 = MaxPooling2D((2, 2), strides=(1, 1), padding='same')(x3) # 31*31*8 #x3 = Conv2D(4, (3, 3), padding='same')(x3) # 29*29*16 #x3 = MaxPooling2D((2, 2), strides=(1, 1), padding='same')(x3) # 15*15*16 #x3 = Conv2D(4, (3, 3), padding='same')(x3) # 13*13*32 #x3 = MaxPooling2D((2, 2), strides=(1, 1), padding='same')(x3) # 7*7*32 #x3 = Conv2D(16, (3, 3), padding='same')(x3) # 5*5*32 #x3 = MaxPooling2D((2, 2), strides=(1, 1), padding='same')(x3) # 3*3*16 #x3 = Flatten()(x3) #x3 = Dense(32, activation='relu')(x3) #x3 = Dropout(0.15)(x3) #x3 = Dense(8, activation='relu')(x3) top_tensor = concatenate([x1, x2], axis=1) top_tensor = Dense(8, activation='relu')(top_tensor) top_tensor = Dropout(0.15)(top_tensor) top_tensor = Dense(2, activation='softmax')(top_tensor) #self.model = ResnetBuilder.build_resnet_18((config.img_x,config.img_y,1),2) self.model = Model(inputs=[input1, input2, input3], outputs=top_tensor) # self.model = Model(inputs=[ input1], outputs=top_tensor) sgd = keras.optimizers.SGD(lr=0.001, momentum=0.9, decay=0.00001, nesterov=False) adam = keras.optimizers.Adam(lr=0.0001, beta_1=0.9, beta_2=0.999, epsilon=1e-8) self.model.compile(loss="categorical_crossentropy", optimizer=adam, metrics=["acc"])
def my_assessor_model(mouth_nn='cnn', mouth_features_dim=512, lstm_units_1=32, dense_fc_1=128, dense_fc_2=64, conv_f_1=32, conv_f_2=64, conv_f_3=128, dropout_p=0.2): mouth_input_shape = (MOUTH_H, MOUTH_W, MOUTH_CHANNELS) my_input_mouth_images = Input(shape=(TIME_STEPS, *mouth_input_shape)) my_input_head_poses = Input(shape=(TIME_STEPS, 3)) my_input_n_of_frames = Input(shape=(1,)) my_input_lipreader_dense = Input(shape=(1024,)) my_input_lipreader_softmax = Input(shape=(500,)) if mouth_nn == 'cnn': mouth_feature_model = my_timedistributed_cnn_model((TIME_STEPS, *mouth_input_shape), conv_f_1, conv_f_2, conv_f_3, mouth_features_dim) elif mouth_nn == 'resnet18': mouth_feature_model = ResnetBuilder.build_resnet_18((TIME_STEPS, *mouth_input_shape), mouth_features_dim, time_distributed=True) elif mouth_nn == 'resnet34': mouth_feature_model = ResnetBuilder.build_resnet_34((TIME_STEPS, *mouth_input_shape), mouth_features_dim, time_distributed=True) elif mouth_nn == 'resnet50': mouth_feature_model = ResnetBuilder.build_resnet_50((TIME_STEPS, *mouth_input_shape), mouth_features_dim, time_distributed=True) elif mouth_nn == 'resnet101': mouth_feature_model = ResnetBuilder.build_resnet_101((TIME_STEPS, *mouth_input_shape), mouth_features_dim, time_distributed=True) elif mouth_nn == 'resnet152': mouth_feature_model = ResnetBuilder.build_resnet_152((TIME_STEPS, *mouth_input_shape), mouth_features_dim, time_distributed=True) elif mouth_nn == 'flatten': mouth_feature_model = Reshape((TIME_STEPS, -1), input_shape=(TIME_STEPS, *mouth_input_shape)) cnn_features = mouth_feature_model(my_input_mouth_images) lstm_input = Concatenate()([cnn_features, my_input_head_poses]) lstm_output = LSTM(lstm_units_1, activation='tanh', kernel_regularizer=l2(1.e-4), return_sequences=False)(lstm_input) concatenated_features = Concatenate()([lstm_output, my_input_n_of_frames, my_input_lipreader_dense, my_input_lipreader_softmax]) fc1 = Dense(dense_fc_1, activation='relu', kernel_regularizer=l2(1.e-4))(concatenated_features) bn1 = BatchNormalization()(fc1) dp1 = Dropout(dropout_p)(bn1) fc2 = Dense(dense_fc_2, activation='relu', kernel_regularizer=l2(1.e-4))(dp1) bn2 = BatchNormalization()(fc2) dp2 = Dropout(dropout_p)(bn2) assessor_output = Dense(1, activation='sigmoid')(dp2) assessor = Model(inputs=[my_input_mouth_images, my_input_head_poses, my_input_n_of_frames, my_input_lipreader_dense, my_input_lipreader_softmax], outputs=assessor_output) assessor.summary() return assessor
def get_model(model, ishape, nb_classes): "Return proper ResNet model" if model.endswith('18'): model = ResnetBuilder.build_resnet_18(ishape, nb_classes) elif model.endswith('34'): model = ResnetBuilder.build_resnet_34(ishape, nb_classes) elif model.endswith('50'): model = ResnetBuilder.build_resnet_50(ishape, nb_classes) elif model.endswith('101'): model = ResnetBuilder.build_resnet_101(ishape, nb_classes) elif model.endswith('152'): model = ResnetBuilder.build_resnet_152(ishape, nb_classes) else: return NotImplemented() return model
def _build_model(self, name, optimizer=None): from resnet import ResnetBuilder """build model from scratch""" # input: (channel, row, col) -> (1, episode_days, ticker) # output: action probability _model = ResnetBuilder.build_resnet_18(input_shape=(1, self._episode_days, self._feature_num), num_outputs=2) logger.debug('built trade model[{name}]'.format(name=name)) # ResnetBuilder.check_model(_model, name=name) if not optimizer: import keras optimizer = keras.optimizers.SGD(lr=0.01, momentum=0.9) _model.compile( optimizer=optimizer, loss={ 'policy_header': 'cosine_proximity', 'value_header': 'mean_squared_error', }, metrics={ 'policy_header': ['mse', 'acc'], 'value_header': ['mse'], }, ) logger.debug('compiled trade model[{name}]'.format(name=name)) return _model
def get_resnet_18(): from resnet import ResnetBuilder model = ResnetBuilder.build_resnet_18(input_shape=(3, 75, 75), num_outputs=2, filters=8, weight_decay=0.) return model
def __init__(self, restore=None, session=None, Dropout=Dropout, num_labels=10): self.num_channels = 3 self.image_size = 32 self.num_labels = num_labels model = ResnetBuilder.build_resnet_32((3, 32, 32), num_labels, activation=False, Dropout=Dropout) if restore != None: model.load_weights(restore) self.model = model
def build_resnet(self, model_name, lr): input_shape = (128, 128, 3) num_outputs = 100 model = None if model_name == "resnet18": model = ResnetBuilder.build_resnet_18(input_shape, num_outputs) elif model_name == "resnet34": model = ResnetBuilder.build_resnet_34(input_shape, num_outputs) elif model_name == "resnet50": model = ResnetBuilder.build_resnet_50(input_shape, num_outputs) elif model_name == "resnet50_keras": inp = Input((224, 224, 3)) model = ResNet50(input_tensor=inp, weights=None, classes=100) else: raise # opt = SGD(lr=lr, momentum=0.9, nesterov=True) opt = Adam() model.compile(optimizer=opt, loss="categorical_crossentropy", metrics=['accuracy']) return model
def model_confirm(self, choosed_model): if choosed_model == 'AlexNet': model = alexnet(self.config) elif choosed_model == 'VGG16': model = vgg16(self.config) elif choosed_model == 'VGG19': model = vgg19(self.config) elif choosed_model == 'InceptionV3': model = inception_v3(self.config) elif choosed_model == 'InceptionV4': model = inception_v4(self.config) elif choosed_model == 'InceptionV4_ResNetV1': model = inception_resnet_v1(self.config) elif choosed_model == 'InceptionV4_ResNetV2': model = inception_resnet_v2(self.config) elif choosed_model == 'ResNet18': model = ResnetBuilder.build_resnet_18(self.config) elif choosed_model == 'ResNet34': model = ResnetBuilder.build_resnet_34(self.config) elif choosed_model == 'ResNet50': model = ResnetBuilder.build_resnet_50(self.config) elif choosed_model == 'ResNet101': model = ResnetBuilder.build_resnet_101(self.config) elif choosed_model == 'ResNet152': model = ResnetBuilder.build_resnet_152(self.config) elif choosed_model == 'DenseNet121': model = densenet121(self.config) elif choosed_model == 'DenseNet169': model = densenet169(self.config) elif choosed_model == 'DenseNet201': model = densenet201(self.config) elif choosed_model == 'DenseNet264': model = densenet264(self.config) else: model = -1 return model
def build(self): if self.model_name == "resnet": original_resnet = self.kwargs.setdefault("original_resnet", False); if original_resnet: print("Warning: you are using original resnet"); self.model = ResnetBuilder.build_resnet_18(self.input_shape, self.classes, original_resnet=original_resnet); if self.model_name == "unet": class_mode = self.kwargs.setdefault("class_mode", "categorical"); self.model = UnetBuilder.build(self.input_shape, [64, 128, 256, 512, 1024], self.classes, class_mode=class_mode); self.model.summary(); if self.load_weights is not None: print("Loading weights: ", self.load_weights); self.model.load_weights(self.load_weights);
def main(): # 4. Load pre-shuffled MNIST data into train and test sets (X_train, y_train), (X_test, y_test) = cifar10.load_data() # 5. Preprocess input data X_train = X_train.reshape(X_train.shape[0], 32, 32, 3) X_test = X_test.reshape(X_test.shape[0], 32, 32, 3) X_train = X_train.astype('float32') X_test = X_test.astype('float32') X_train /= 255 X_test /= 255 # 6. Preprocess class labels Y_train = np_utils.to_categorical(y_train, 10) Y_test = np_utils.to_categorical(y_test, 10) ##### PROGRAM CONSTANTS ##### preLoad = False saveWeights = True trainModel = True ############################# # 7. Define model architecture model = ResnetBuilder.build_resnet_18_deformed((3, 32, 32), 10) if (preLoad): model.load_weights('cifar_weights.h5', by_name=True) # 8. Compile model model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy']) # 9. Fit model on training data model.fit(X_train, Y_train, batch_size=32, epochs=10, verbose=1) if (saveWeights): model.save_weights('cifar_weights.h5') # 10. Evaluate model on test data score = model.evaluate(X_test, Y_test, verbose=0)
def test_custom2(): """ https://github.com/raghakot/keras-resnet/issues/34 """ model = ResnetBuilder.build_resnet_152((3, 512, 512), 2) _test_model_compile(model)
train_data_dir = './train_data/' validation_data_dir = './val_data/' log_filepath = './log/' num_train_samples = 100000 num_val_samples = 20000 num_epoch = 100 result_dir = './result/' if not os.path.exists(result_dir): os.mkdir(result_dir) if __name__ == '__main__': # Resnet50モデルを構築 input_shape = [3, 30, 30] resnet50 = ResnetBuilder.build_resnet_50(input_shape, num_classes) model = Model(input=resnet50.input, output=resnet50.output) # SGD+momentumがいいらしい model.compile(loss='categorical_crossentropy', optimizer=optimizers.SGD(lr=1e-2, decay=1e-6, momentum=0.9, nesterov=True), metrics=['accuracy']) datagen = ImageDataGenerator(data_format="channels_first") train_generator = datagen.flow_from_directory(train_data_dir, target_size=(img_rows, img_cols),
with np.load(os.path.join(arguments["data_path"], "test.npz")) as data: test_imgs = data["imgs"] test_labels = data["labels"].astype(int) X_train, Y_train = np.reshape(train_imgs, (len(train_imgs), 28, 28, 1)), train_labels X_test, Y_test = np.reshape(test_imgs, (len(test_imgs), 28, 28, 1)), test_labels nb_classes = len(np.unique(np.hstack([train_labels, test_labels]))) print("Number of classes: {}".format(nb_classes), flush=True) # Create the model sess = K.get_session() graph = sess.graph # get Tensorflow graph model = ResnetBuilder.build_resnet_18((1, 28, 28), nb_classes) model.compile(loss='sparse_categorical_crossentropy', optimizer=Adam(lr=arguments["learning_rate"]), metrics=['accuracy']) # Train the model # history = model.fit( # X_train, Y_train, # epochs=arguments["epochs"], # batch_size=arguments["batch_size"], # validation_data=(X_test, Y_test)) # accuracy = history.history["val_acc"][-1] print(model.outputs) import sys; sys.exit()
def train_nn_detection(Data, Model, path=None, num=0): data = Data() sess = K.get_session() K.set_learning_phase(False) """#Uncomment this ugly block of code to set up the weights of the detector with tf.variable_scope("model"): model = ResnetBuilder.build_resnet_32((3, 32, 32), 10) model.load_weights("models/cifar-resnet") with tf.variable_scope("model_with_detector"): model_with_detector = ResnetBuilder.build_resnet_32((3, 32, 32), 10, with_detector=2) layers1 = [(i,x) for i,x in enumerate(model.layers) if len(x.weights)] layers2 = model_with_detector.layers layers2 = [(i,x) for i,x in enumerate(layers2) if all('/detector' not in y.name for y in x.weights) and len(x.weights)] set_layers = [] for (i,e),(j,f) in zip(layers1,layers2): ee = [re.sub("_[0-9]+","",x.name) for x in e.weights] ff = [re.sub("_[0-9]+","",x.name).replace("model_with_detector","model") for x in f.weights] set_layers.append(j) assert ee==ff f.set_weights(e.get_weights()) print(set_layers) model_with_detector.save_weights("models/cifar-resnet-detector-base") #""" model_with_detector = ResnetBuilder.build_resnet_32((3, 32, 32), 10, with_detector=2) model_with_detector.load_weights("models/cifar-resnet-detector-base") set_layers = [1, 2, 4, 5, 7, 9, 11, 12, 14, 16, 18, 19, 21, 23, 25, 26, 28, 30, 32, 33, 35, 37, 39, 40, 42, 43, 45, 47, 48, 50, 52, 54, 55, 57, 59, 61, 62, 64, 66, 68, 69, 71, 73, 75, 76, 78, 79, 81, 83, 84, 86, 88, 90, 91, 93, 95, 97, 98, 102, 106, 110, 112, 116, 120, 128] for e in set_layers: model_with_detector.layers[e].trainable = False #print('Test accuracy',np.mean(np.argmax(model_with_detector.predict(data.test_data)[0],axis=1)==np.argmax(data.test_labels,axis=1))) train_data, train_labels = data.train_data, data.train_labels N = len(train_data) """ # uncomment to create the adversarial training data model = ResnetBuilder.build_resnet_32((3, 32, 32), 10, activation=False) model.load_weights("models/cifar-resnet") model = Wrap(model) #attack = FGS(sess, model) attack = CarliniL2(sess, model, batch_size=100, binary_search_steps=3, initial_const=0.1, max_iterations=3000, learning_rate=0.005, confidence=0, targeted=False) for i in range(0,N,1000): now=time.time() train_adv = attack.attack(data.train_data[i:i+1000], data.train_labels[i:i+1000]) print(time.time()-now) print('accuracy',np.mean(np.argmax(model.model.predict(train_adv),axis=1)==np.argmax(data.train_labels[i:i+1000],axis=1))) np.save("tmp/adv"+path.split("/")[1]+str(i), train_adv) print('Accuracy on valid training',np.mean(np.argmax(model.model.predict(data.train_data),axis=1)==np.argmax(data.train_labels,axis=1))) print('Accuracy on adversarial training',np.mean(np.argmax(model.model.predict(train_adv),axis=1)==np.argmax(data.train_labels,axis=1))) #""" train_adv = [] for i in range(0,N,1000): train_adv.extend(np.load("tmp/adv"+path.split("/")[1]+str(i)+".npy")) train_adv = np.array(train_adv) newX = np.zeros((train_data.shape[0]*2,)+train_data.shape[1:]) newX[:train_data.shape[0]] = train_data newX[train_data.shape[0]:] = train_adv newY = np.zeros((train_data.shape[0]*2,1)) newY[:train_data.shape[0]] = 0 newY[train_data.shape[0]:] = 1 for i in range(100): train(sess, model_with_detector, newX, np.concatenate([train_labels]*2,axis=0), newY, path+"-layerdetect-"+str(i)+"-"+str(num), LEARNING_RATE=10**random.randint(-5,-1), MOMENTUM=random.random(), OPTIMIZER=random.choice(['sgd', 'sgd', 'sgd', 'sgd', 'sgd', 'adam', 'rmsprop']), NUM_EPOCHS=20, BATCH_SIZE=2**random.randint(4,9))
def build_resnet_unet(input_shape, classes, class_mode="categorical"): assert classes >= 2 assert class_mode in ["binary", "categorical"] if classes > 2: assert class_mode == "categorical" filters = [512, 256, 128, 64, 64] resnet_blocks, inputs = ResnetBuilder.build_resnet_18_for_other_model( input_shape, original_resnet=False) h = [ resnet_blocks[4], resnet_blocks[3], resnet_blocks[2], resnet_blocks[1], resnet_blocks[0] ] for i in range(5): h[i] = Conv2D(filters=filters[i], kernel_size=(3, 3), padding="same")(h[i]) up6 = Conv2D(filters[1], kernel_size=(3, 3), activation="relu", padding="same", kernel_initializer="he_normal")(UpSampling2D(size=(2, 2))( h[0])) merge6 = add([h[1], up6]) conv6 = Graph_utils._conv_relu_conv_relu(filters[1])(merge6) up7 = Conv2D(filters[2], kernel_size=(3, 3), activation="relu", padding="same", kernel_initializer="he_normal")( UpSampling2D(size=(2, 2))(conv6)) merge7 = add([h[2], up7]) conv7 = Graph_utils._conv_relu_conv_relu(filters[2])(merge7) up8 = Conv2D(filters[3], kernel_size=(3, 3), activation="relu", padding="same", kernel_initializer="he_normal")( UpSampling2D(size=(2, 2))(conv7)) merge8 = add([h[3], up8]) conv8 = Graph_utils._conv_relu_conv_relu(filters[3])(merge8) up9 = Conv2D(filters[4], kernel_size=(3, 3), activation="relu", padding="same", kernel_initializer="he_normal")( UpSampling2D(size=(2, 2))(conv8)) merge9 = add([h[4], up9]) conv9 = Graph_utils._conv_relu_conv_relu(filters[4])(merge9) if class_mode == "binary": conv9 = Conv2D(classes, kernel_size=(3, 3), activation="relu", padding="same", kernel_initializer="he_normal")(conv9) conv9 = Conv2D(1, kernel_size=(1, 1), padding="same", kernel_initializer="he_normal")(conv9) conv10 = Activation("sigmoid")(conv9) else: conv9 = Conv2D(classes, kernel_size=(3, 3), activation="relu", padding="same", kernel_initializer="he_normal")(conv9) conv9 = Conv2D(classes, kernel_size=(1, 1), padding="same", kernel_initializer="he_normal")(conv9) conv10 = Activation("softmax")(conv9) model = Model(inputs=inputs, outputs=conv10) return model
def ResNet(x, **kwarg): return ResnetBuilder.build(x, **kwarg)
def create_model(self, model_type='xception', load_weights=None): if (model_type == 'inceptionv3' or model_type == 1): base = InceptionV3(include_top=False, weights='imagenet', input_tensor=self.input_tensor, classes=self.output_size, pooling='avg') model_name = 'inceptionv3' pred = base.output elif (model_type == 'resnet50' or model_type == 2): base = ResNet50(include_top=False, weights='imagenet', input_tensor=self.input_tensor, classes=self.output_size, pooling='avg') model_name = 'resnet50' pred = base.output elif (model_type == 'vgg19' or model_type == 3): base = VGG19(include_top=False, weights='imagenet', input_tensor=self.input_tensor, classes=self.output_size, pooling='avg') model_name = 'vgg19' pred = base.output elif (model_type == 'vgg16' or model_type == 4): base = VGG16(include_top=False, weights='imagenet', input_tensor=self.input_tensor, classes=self.output_size, pooling='avg') model_name = 'vgg16' pred = base.output elif (model_type == 'resnet152' or model_type == 5): resbuild = ResnetBuilder() base = resbuild.build_resnet_152(self.input_shape, self.output_size) model_name = 'resnet152' pred = base.output elif (model_type == 'resnet50MOD' or model_type == 6): resbuild = ResnetBuilder() base = resbuild.build_resnet_50(self.input_shape, self.output_size) model_name = 'resnet50MOD' pred = base.output elif (model_type == 'inceptionv3MOD' or model_type == 7): base = InceptionV3MOD(include_top=False, weights='imagenet', input_tensor=self.input_tensor, classes=self.output_size, pooling='avg') model_name = 'inceptionv3MOD' pred = base.output else: base = Xception(include_top=False, weights='imagenet', input_tensor=self.input_tensor, classes=self.output_size, pooling='avg') model_name = 'xception' pred = base.output pred = Dense(self.output_size, activation='sigmoid', name='predictions')(pred) self.model = Model(base.input, pred, name=model_name) if load_weights != None: self.model.load_weights(load_weights) for layer in base.layers: layer.trainable = True self.model.compile(loss=losses.binary_crossentropy, optimizer='adam', metrics=[FScore2])
"Trouser", "Pullover", "Dress", "Coat", "Sandal", "Shirt", "Sneaker", "Bag", "Ankle boot", ] input_shape = (28, 28, 1) # モデルの入力サイズ num_classes = 10 # クラス数 # ResNet18 モデルを作成する。 model = ResnetBuilder.build_resnet_18(input_shape, num_classes) # モデルをコンパイルする。 model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"]) # 学習する。 model.fit(train_X, train_labels, epochs=5) # テストデータに対する性能を確認する。 test_loss, test_acc = model.evaluate(test_X, test_labels) print(f"test loss: {test_loss:.2f}, test accuracy: {test_acc:.2%}") # test loss: 0.36, test accuracy: 86.93%
def ResCapsNet(input_shape, n_class, routings): # K.set_image_dim_ordering('th') x = Input(input_shape, name='input') # Part1 conv1 = Conv2D(64, (3, 3), padding='same', name='conv1')(x) bn1 = BatchNormalization(name='bn1')(conv1) relu1 = ReLU()(bn1) pool1 = MaxPool2D((2, 2), strides=(2, 2), padding='same', name='pool1')(relu1) drop1 = Dropout(0.3, name='dropout1')(pool1) conv2 = Conv2D(128, (3, 3), padding='same', name='conv2')(drop1) bn2 = BatchNormalization(name='bn2')(conv2) relu2 = ReLU()(bn2) pool2 = MaxPool2D((2, 2), strides=(2, 2), padding='same', name='pool2')(relu2) drop2 = Dropout(0.3, name='dropout2')(pool2) conv3 = Conv2D(128, (3, 3), padding='same', name='conv3')(drop2) bn3 = BatchNormalization(name='bn3')(conv3) relu3 = ReLU()(bn3) pool3 = MaxPool2D((2, 2), strides=(2, 2), padding='same', name='pool3')(relu3) drop3 = Dropout(0.3, name='dropout3')(pool3) conv4 = Conv2D(128, (3, 3), padding='same', name='conv4')(drop3) bn4 = BatchNormalization(name='bn4')(conv4) relu4 = ReLU()(bn4) pool4 = MaxPool2D((2, 2), strides=(2, 2), padding='same', name='pool4')(relu4) drop4 = Dropout(0.3, name='dropout4')(pool4) conv5 = Conv2D(128, (3, 3), padding='same', name='conv5')(drop4) bn5 = BatchNormalization(name='bn5')(conv5) relu5 = ReLU()(bn5) pool5 = MaxPool2D((2, 2), strides=(2, 2), padding='same', name='pool5')(relu5) drop5 = Dropout(0.3, name='dropout5')(pool5) conv6 = Conv2D(128, (3, 3), padding='same', name='conv6')(drop5) bn6 = BatchNormalization(name='bn6')(conv6) relu6 = ReLU()(bn6) pool6 = MaxPool2D((2, 2), strides=(2, 2), padding='same', name='pool6')(relu6) drop6 = Dropout(0.3, name='dropout6')(pool6) conv7 = Conv2D(128, (3, 3), padding='same', name='conv7')(drop6) bn7 = BatchNormalization(name='bn7')(conv7) relu7 = ReLU()(bn7) pool7 = MaxPool2D((2, 2), strides=(2, 2), padding='same', name='pool7')(relu7) drop7 = Dropout(0.3, name='dropout7')(pool7) # Part2-branch-a primarycaps = PrimaryCap(drop7, dim_capsule=8, n_channels=32, kernel_size=3, strides=2, padding='same', name='primarycaps') digitcaps = CapsuleLayer(num_capsule=n_class, dim_capsule=16, routings=routings, name='digitcaps')(primarycaps) out_caps = Length(name='capsnet')(digitcaps) # fc1 = Dense(n_class, activation='sigmoid', name='fc1')(out_caps) # Part2-branch-b res = ResnetBuilder.build_resnet_18(drop7, n_class) # Part3 add = Add(name='add')([out_caps, res]) train_model = models.Model(x, add, name='ResCapsNet') return train_model
samples = np.load(save_dir+"training_data_2.npz") # Splitting the data train_data = samples['images'][:40000] train_targets = samples['mags'][:40000] val_data = samples['images'][40000:80000] val_targets = samples['mags'][40000:80000] test_data = samples['images'][80000:] test_targets = samples['mags'][80000:] # Making the model and training model = ResnetBuilder.build(input_shape=(1, 32, 32), num_outputs=1, block_fn='basic_block', repetitions=[15, 20, 30, 15]) model.compile(optimizer=optimizers.Adam(lr=0.00005), loss='mse', metrics=['mse']) history = model.fit(train_data, train_targets, epochs=40, batch_size=100, validation_data=(val_data, val_targets)) model.save('magnitude_regression.h5') # Plotting a Graph mse = history.history['mean_squared_error'] val_mse = history.history['val_mean_squared_error'] loss = history.history['loss'] val_loss = history.history['val_loss'] epochs = range(1, len(mse) + 1)
N_CLASS = 28 DATA_DIR = 'dataset224/' n_valid = 1000 Ids, labels = loadData('all.csv', N_CLASS, DATA_DIR) index = random.sample(range(len(Ids)), 1000) train, _ = loadData('train.csv', N_CLASS, DATA_DIR) validation, _ = loadData('validation.csv', N_CLASS, DATA_DIR) params = { 'dim': (224, 224), 'batch_size': 32, 'n_classes': 28, 'n_channels': 3, 'shuffle': True } training_generator = DataGenerator(train, labels, **params) validation_generator = DataGenerator(validation, labels, **params) histories = Histories() model = ResnetBuilder.build_resnet_50((3, 224, 224), 28) model.compile(loss="categorical_crossentropy", optimizer="sgd", metrics=[metrics.categorical_accuracy]) print(len(Ids) - len(train) - len(validation)) model.fit_generator(generator=training_generator, validation_data=validation_generator, callbacks=[histories], epochs=1)
X_train = X_train[:batch_size*(X_train.shape[0]/batch_size)] Y_train = Y_train[:batch_size*(X_train.shape[0]/batch_size)] X_test = X_test[:batch_size*(X_test.shape[0]/batch_size)] Y_test = Y_test[:batch_size*(X_test.shape[0]/batch_size)] # subtract mean and normalize mean = [125.3, 123.0, 113.9] std = [63.0, 62.1, 66.7] mean_image = np.mean(X_train, axis=0) X_train -= np.reshape(mean, [1, 1, 1, 3]) X_test -= np.reshape(mean, [1, 1, 1, 3]) X_train /= np.reshape(std, [1, 1, 1, 3]) X_test /= np.reshape(std, [1, 1, 1, 3]) # build res-18 network res_18_model = ResnetBuilder.build_resnet_18(input_shape, nb_classes, par) if par.weights: res_18_model.load_weights(par.weights, by_name=True) res_18_extractor = Model(inputs=res_18_model.input, outputs=res_18_model.get_layer('flatten_1').output) # build res-50-imagenet network vgg_model = VGG16(weights='imagenet', include_top=False, input_shape=(32, 32, 3), pooling='avg') # res_model = ResNet50(weights='imagenet', include_top=False, input_shape=(32, 32, 3)) for l in vgg_model.layers: print l.name vgg_extractor = Model(inputs=vgg_model.input, outputs=vgg_model.get_layer('global_average_pooling2d_1').output) # res_extractor = Model(inputs=res_model.input, outputs=res_model.get_layer('avg_pool').output) model = vgg_extractor # model = res_18_extractor features = model.predict(X_test, batch_size=batch_size) print features.shape np.save('features.npy', features)
import os import keras.backend.tensorflow_backend as KTF from resnet import ResnetBuilder from keras.models import Sequential from keras.layers import Dense, Dropout, Activation, Flatten from keras.layers import Convolution2D, MaxPooling2D from keras.utils import np_utils from keras import backend as K from keras.models import Sequential from keras.layers import Dense, Flatten from keras.layers.convolutional import Conv2D, MaxPooling2D from keras.utils.np_utils import to_categorical from keras.models import Model from keras.layers import Conv2D, MaxPooling2D, AveragePooling2D from keras.layers import Input, Activation, Dense, Flatten from keras.layers.merge import add from keras.layers.normalization import BatchNormalization from keras.regularizers import l2 from keras import backend as K from keras.utils import plot_model from keras import regularizers import tensorflow as tf from keras import layers from keras.layers import Input,Add,Dense,Activation,ZeroPadding2D, \ BatchNormalization,Flatten,Conv2D,AveragePooling2D,MaxPooling2D,GlobalMaxPooling2D from resnet import ResnetBuilder import keras.backend as K model = ResnetBuilder.build_resnet_18((1, 224, 224), 20) from keras.utils import plot_model plot_model(model, to_file='model.png', show_shapes=1)
def run_nn_detection(Data, path): data = Data() sess = K.get_session() K.set_learning_phase(False) model_with_detector = ResnetBuilder.build_resnet_32((3, 32, 32), 10, with_detector=2, activation=False) model_with_detector.save_weights("/tmp/q") model_with_detector.load_weights("models/cifar-layerdetect-37-0") N = 10#len(data.test_data)//100 """ # uncomment to generate adversarial testing data model = ResnetBuilder.build_resnet_32((3, 32, 32), 10, activation=False) model.load_weights("models/cifar-resnet") model = Wrap(model) #attack = FGS(sess, model) attack = CarliniL2(sess, model, batch_size=100, binary_search_steps=3, initial_const=0.1, max_iterations=3000, learning_rate=0.005, confidence=0, targeted=False) for i in range(0,N,1000): test_adv = attack.attack(data.test_data[i:i+100], data.test_labels[i:i+100]) np.save("tmp/testadv"+path.split("/")[1]+str(i), test_adv) #""" test_adv = [] for i in range(0,N,1000): test_adv.extend(np.load("tmp/testadv"+path.split("/")[1]+str(i)+".npy")) test_adv = np.array(test_adv) print('Accuracy of model on test set',np.mean(np.argmax(model_with_detector.predict(data.test_data)[0],axis=1)==np.argmax(data.test_labels,axis=1))) print('Accuracy of model on adversarial data',np.mean(np.argmax(model_with_detector.predict(test_adv)[0],axis=1)==np.argmax(data.test_labels,axis=1))) print('Probaility detects valid data as valid',np.mean(model_with_detector.predict(data.test_data)[1]<=0)) print('Probability detects adversarail data as adversarial',np.mean(model_with_detector.predict(test_adv)[1]>0)) xs = tf.placeholder(tf.float32, [None, 32, 32, 3]) rmodel = RobustModel(model_with_detector) preds = rmodel.predict(xs) y1 = np.argmax(sess.run(preds, {xs: data.test_data[:N]}),axis=1) print('Robust model accuracy on test dat',np.mean(y1==np.argmax(data.test_labels[:N],axis=1))) print('Probability robust model detects valid data as adversarial', np.mean(y1==10)) y2 = np.argmax(sess.run(preds, {xs: test_adv}),axis=1) print('Probability robust model detects adversarial data as adversarial', np.mean(y2==10)) attack = CarliniL2(sess, rmodel, batch_size=10, binary_search_steps=3, initial_const=0.1, max_iterations=300, learning_rate=0.01, confidence=0, targeted=True) targets = np.argmax(model_with_detector.predict(test_adv[:N])[0],axis=1) realtargets = np.zeros((N, 11)) realtargets[np.arange(N),targets] = 1 np.save("tmp/adaptiveattack",attack.attack(data.test_data[:N], realtargets)) adv = np.load("tmp/adaptiveattack.npy") print('Accuracy on adversarial data',np.mean(np.argmax(model_with_detector.predict(adv)[0],axis=1)==np.argmax(data.test_labels,axis=1))) print('Probability detector detects adversarial data as adversarial',np.mean(model_with_detector.predict(adv)[1]>0)) d=np.sum((adv-data.test_data[:N])**2,axis=(1,2,3))**.5 print("mean distortion attacking robust model", np.mean(d)) d=np.sum((test_adv[:N]-data.test_data[:N])**2,axis=(1,2,3))**.5 print("mean distortion attacking unsecurred model", np.mean(d)) model_with_detector_2 = ResnetBuilder.build_resnet_32((3, 32, 32), 10, with_detector=2, activation=False) model_with_detector_2.load_weights("models/cifar-layerdetect-42-0") print('Accuracy on adversarial data',np.mean(np.argmax(model_with_detector_2.predict(adv)[0],axis=1)==np.argmax(data.test_labels,axis=1))) print('Probability detector detects adversarial data as adversarial',np.mean(model_with_detector_2.predict(adv)[1]>0))
from skimage.color import rgb2gray from skimage.transform import resize from skimage.io import imshow, imsave from PIL import Image import os import numpy as np import matplotlib.pyplot as plt names = ["France", "Greece", "Germany","DE - Sachsen", "Spain", "Italy 2Euro", "Luxembourg", "Italy 1Euro"] model = ResnetBuilder.build_resnet_34((200, 200, 1), 8) opt = keras.optimizers.rmsprop(lr=0.0001, decay=1e-6) model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy']) path = "B:/Desktop/Projekts/Euro_Pred/pred_img/" pred_im = Image.open(path + filename) pred_im = np.array(pred_im) if not is_gray: pred_im = rgb2gray(pred_im)
def test_resnet18(): for ordering in DIM_ORDERING: K.set_image_dim_ordering(ordering) model = ResnetBuilder.build_resnet_18((3, 224, 224), 100) model.compile(loss="categorical_crossentropy", optimizer="sgd") assert True, "Failed to build with '{}' dim ordering".format(ordering)
print('Accuracy:', (TP + TN) / float(TP + TN + FP + FN)) print('Sensitivity:', TP / float(TP + FN)) print('Specificity:', TN / float(TN + FP)) return (TP + TN) / float(TP + TN + FP + FN), TP / float(TP + FN), TN / float(TN + FP) if __name__ == '__main__': K.set_image_data_format('channels_first') k_total = 10 # 交叉验证次数 f = xlwt.Workbook() sheet = f.add_sheet('yourname', cell_overwrite_ok=True) for k_count in range(k_total): accuracy_max = 0 # 搭建模型 model = ResnetBuilder.build_resnet_34((3, 224, 224), 2) model_compile(model) # X,Y 为数据集,根据需要修改路径和预处理方式 X, Y = process_test("./Data/test/AD", "./Data/test/NC") #k折交叉验证 skf = StratifiedKFold(n_splits=5) # 折数可改 for cnt, (train, test) in enumerate(skf.split(X, Y)): # for (batch,batchlabel) in process_image("./Data/test/AD", # "./Data/test/NC",4): # model.fit(batch, batchlabel, epochs=1, validation_split=0.1) model.fit(X[train], Y[test], epochs=100, validation_split=0.2) pred = model.predict(np.array(X[test])) accuracy, sensitivity, specificity = calculate_metric( Y[test], pred) # 在准确率较高的时候保存模型参数,也可以每次都保存 if (accuracy_max < accuracy):
def test_resnet152(): model = ResnetBuilder.build_resnet_152((3, 224, 224), 100) _test_model_compile(model)
# create test input_ds = tf.data.Dataset.from_tensor_slices(X_test) target_ds = tf.data.Dataset.from_tensor_slices(Y_test) test_ds = tf.data.Dataset.zip((input_ds, target_ds)).batch(batch_size).repeat() test_ds = test_ds.prefetch(autotune) ''' train and test ''' results_dict = {} num_try = 3 for try_i in range(num_try): builder = ResnetBuilder(name='ResNet18', act='FReLU', classes=num_label, include_top=True, input_shape=(img_size, img_size, 3)) frelu_model = builder.builder() frelu_model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy']) hist = frelu_model.fit(train_ds, steps_per_epoch=int(np.ceil(X_train.shape[0]//32)), validation_data=test_ds, validation_steps=int(np.ceil(X_test.shape[0]/32)), epochs=200, verbose=1, ) results_dict["FReLU-try" + str(try_i)] = hist.history['accuracy'], hist.history['loss'], hist.history['val_accuracy'], hist.history['val_loss']
def build_resnet_152(self, shape, num_classes): self.model = ResnetBuilder.build_resnet_152(shape, num_classes) print('[+] ResNet-152 model built') return self.model