def train(dataset, epochs = 50, drop_rate=0.4,**kwargs): # input image dimensions epochs = epochs if not 'data' in kwargs: (x_train, y_train), (x_test, y_test), (img_rows, img_cols, nb_classes) = datama.getData(dataset) else: ((x_train, y_train), (x_test, y_test), nb_classes) = kwargs['data'] img_rows, img_cols = x_train.shape[1], x_train.shape[2] input_shape = (img_rows, img_cols, 1) print("X train shape {}".format(x_train.shape)) print("y train shape {}".format(y_train.shape)) print("X test shape {}".format(x_test.shape)) print("y test shape {}".format(y_test.shape)) model = Model3_deepXplore(input_shape=input_shape, drop_rate=drop_rate, nb_classes=nb_classes) model.summary() if not 'bestModelfile' in kwargs: bestModel = "./model/deepxplore_"+dataset+".hdf5" else: bestModel = kwargs['bestModelfile'] if not 'logfile' in kwargs: log = "./log/deepxplore_"+dataset+".log" else: log = kwargs['logfile'] checkpointer = ModelCheckpoint(filepath=bestModel, verbose=1, save_best_only=True, monitor="val_acc") logger = CSVLogger(log, separator=",", append=False) # compile model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) model.fit(x_train, y_train, batch_size=64, epochs=epochs, validation_data=(x_test, y_test), callbacks=[checkpointer, logger])
def compute_shuffle_idx(size): (x_train, y_train), (_, _), (_, _, num_class) = datama.getData('mnist') idx_mnist = np.arange(x_train.shape[0]) np.random.shuffle(idx_mnist) (x_train, y_train), (_, _), (_, _, num_class) = datama.getData('cifar10') idx_cifar10 = np.arange(x_train.shape[0]) np.random.shuffle(idx_cifar10) data = { 'mnist': idx_mnist[:size], 'fashion_mnist': idx_mnist[:size], 'cifar10': idx_cifar10[:size] } if not os.path.isdir('./adv_data/'): os.mkdir('./adv_data/') np.save('./adv_data/data_index.npy', data) return idx_mnist[:size], idx_cifar10[:size]
def train(dataset='cifar10', name="bn",**kwargs): batch_size = kwargs['batch_size'] if 'batch_size' in kwargs else 128 epochs = kwargs['epochs'] if 'epochs' in kwargs else 300 dropout = kwargs['dropout'] if 'dropout' in kwargs else 0.5 weight_decay = kwargs['weight_decay'] if 'weight_decay' in kwargs else 0.0001 log_filepath = './'+name models = {"bn":build_model_bn, "nonBn":build_model_nonBn} schedulers = {"bn":scheduler_bn, "nonBn":scheduler_nonBn} # load data if not 'data' in kwargs: (x_train, y_train), (x_test, y_test), (img_rows, img_cols, num_classes) = datama.getData(dataset) else: ((x_train, y_train), (x_test, y_test), num_classes) = kwargs['data'] img_rows, img_cols = x_train.shape[1], x_train.shape[2] # build network model = models[name](x_train.shape[1:],dropout,weight_decay) print(model.summary()) change_lr = LearningRateScheduler(schedulers[name]) if not 'logfile' in kwargs: csvlog = callbacks.CSVLogger("./log/netinnet_" + dataset + ".log", separator=',', append=False) else: csvlog = callbacks.CSVLogger(kwargs['logfile'], separator=',', append=False) if not 'bestModelfile' in kwargs: checkPoint = callbacks.ModelCheckpoint('./model/netinnet_' + dataset + ".h5", save_best_only=True, monitor="val_acc", verbose=1) else: checkPoint = callbacks.ModelCheckpoint(kwargs['bestModelfile'], monitor="val_acc", save_best_only=True, verbose=1) cbks = [change_lr,csvlog, checkPoint] # set data augmentation # if you do not want to use data augmentation, comment below codes. print('Using real-time data augmentation.') datagen = ImageDataGenerator(horizontal_flip=True, width_shift_range=0.125, height_shift_range=0.125, fill_mode='constant', cval=0.) datagen.fit(x_train) iterations = x_train.shape[0]//batch_size #start training model.fit_generator(datagen.flow(x_train, y_train, batch_size=batch_size), steps_per_epoch=iterations, epochs=epochs, callbacks=cbks, validation_data=(x_test, y_test))
def selectData(model, dataname, name, idx_true, test=False): print("{} ,{}".format(dataname, name)) (x_train, y_train), (x_test, y_test), (_, _, num_class) = datama.getData(dataname) if name == 'mlp': x_train = x_train.reshape(x_train.shape[0], -1) x_test = x_test.reshape(x_test.shape[0], -1) selectedIndex = idx_true[dataname] (x_train, y_train) = (x_train[selectedIndex], y_train[selectedIndex]) assert name in weightspaths, "Error dict weightspath does not contain {}".format( name) #Test whether model weights is loaded correctly. scores = model.evaluate(x_train, y_train, verbose=0) assert scores[ 1] > 0.85, "Do not load the trained-well weights. The model is not trained well." logger.info("Model {}, score {}".format(name, scores)) del x_train, y_train y_test = np.squeeze(np.argmax(y_test, axis=1)[..., np.newaxis]) return x_test, y_test, np.arange(x_test.shape[0])
def train(dataset,epochs=50,**kwargs): epochs = epochs # load data if not 'data' in kwargs: (x_train, y_train), (x_test, y_test), (img_rows, img_cols, nb_classes) = datama.getData(dataset) else: ((x_train, y_train), (x_test, y_test), nb_classes) = kwargs['data'] img_rows, img_cols = x_train.shape[1], x_train.shape[2] # build network model = build_model(x_train.shape[1:]) print(model.summary()) # set callback change_lr = LearningRateScheduler(scheduler) if not 'logfile' in kwargs: csvlog = callbacks.CSVLogger("./log/lenet_" + dataset + ".log", separator=',', append=False) else: csvlog = callbacks.CSVLogger(kwargs['logfile'], separator=',', append=False) if not 'bestModelfile' in kwargs: checkPoint = callbacks.ModelCheckpoint("./model/lenet_" + dataset + ".h5", monitor="val_acc", save_best_only=True, verbose=1) else: checkPoint = callbacks.ModelCheckpoint(kwargs['bestModelfile'], monitor="val_acc", save_best_only=True, verbose=1) cbks = [change_lr,csvlog,checkPoint] # start train model.fit(x_train, y_train, batch_size=128, epochs=epochs, callbacks=cbks, validation_data=(x_test, y_test), shuffle=True)
def train(dataset, epochs=50,**kwargs): batch_size = 128 epochs = epochs if not 'data' in kwargs: (x_train, y_train), (x_test, y_test), (img_rows, img_cols, num_classes) = datama.getData(dataset) else: ((x_train, y_train), (x_test, y_test), num_classes) = kwargs['data'] #img_rows, img_cols = x_train.shape[1], x_train.shape[2] x_train = x_train.reshape(x_train.shape[0], -1) x_test = x_test.reshape(x_test.shape[0], -1) model = mlp(num_classes, inputshape=x_train.shape[1:]) model.compile(loss='categorical_crossentropy', optimizer=RMSprop(), metrics=['accuracy']) if not 'logfile' in kwargs: csvlog = callbacks.CSVLogger("./log/mlp_" + dataset + ".log", separator=',', append=False) else: csvlog = callbacks.CSVLogger(kwargs['logfile'], separator=',', append=False) if not 'bestModelfile' in kwargs: checkPoint = callbacks.ModelCheckpoint("./model/mlp_" + dataset + ".h5", monitor="val_acc", save_best_only=True, verbose=1) else: checkPoint = callbacks.ModelCheckpoint(kwargs['bestModelfile'], monitor="val_acc", save_best_only=True, verbose=1) history = model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=1, validation_data=(x_test, y_test), callbacks=[csvlog,checkPoint]) score = model.evaluate(x_test, y_test, verbose=0) print('Test loss:', score[0]) print('Test accuracy:', score[1])
def train_VGG16(dataset, num_epoch=300, batchSize=128, **kwargs): if not 'data' in kwargs: (X_train, Y_train), (X_test, Y_test), (img_rows, img_cols, nb_class) = datama.getData(dataset) else: ((X_train, Y_train), (X_test, Y_test), nb_class) = kwargs['data'] img_rows, img_cols = X_train.shape[1], X_train.shape[2] if not 'bestModelfile' in kwargs: bestmodelname = "./model/vgg_" + dataset + ".h5" else: bestmodelname = kwargs['bestModelfile'] if not 'logfile' in kwargs: logpath = "./log/vgg_" + dataset + ".log" else: logpath = kwargs['logfile'] # VGG16 Original Weights weights_path = './model/vgg16_weights_tf_dim_ordering_tf_kernels.h5' # if not regularization: print("VGG16_clipped") model = VGG16_clipped( input_shape=X_train.shape[1:], rate=0.4, nb_classes=nb_class ) # VGG16_clipped(input_shape=(32,32,3), rate=0.2, nb_classes=10, drop=False) vgg16 = VGG16(weights='imagenet', include_top=False) layer_dict = dict([(layer.name, layer) for layer in vgg16.layers]) for l in model.layers: if l.name in layer_dict: model.get_layer(name=l.name).set_weights( layer_dict[l.name].get_weights()) #model.load_weights(weights_path, by_name=True) checkPoint = ModelCheckpoint(bestmodelname, monitor="val_acc", save_best_only=True, verbose=1) model.summary() num_layers = len(model.layers) a = np.arange(num_layers) layers = a[(num_layers - 6):-2] print(layers) lr = 1e-2 def lr_scheduler(epoch): initial_lrate = lr drop = 0.9 epochs_drop = 50.0 lrate = initial_lrate * np.power(drop, np.floor((1 + epoch) / epochs_drop)) return lrate reduce_lr = callbacks.LearningRateScheduler(lr_scheduler, verbose=1) # compile the model with SGD/momentum optimizer model.compile(loss='categorical_crossentropy', optimizer=optimizers.SGD(lr=lr, momentum=0.9), metrics=['accuracy']) csvlog = callbacks.CSVLogger(logpath, separator=',', append=False) # data augmentation # if you do not want to use data augmentation, comment below codes. train_datagen = ImageDataGenerator( featurewise_center=False, # set input mean to 0 over the dataset samplewise_center=False, # set each sample mean to 0 featurewise_std_normalization= False, # divide inputs by std of the dataset samplewise_std_normalization=False, # divide each input by its std zca_whitening=False, # apply ZCA whitening rotation_range= 15, # randomly rotate images in the range (degrees, 0 to 180) width_shift_range= 0.1, # randomly shift images horizontally (fraction of total width) height_shift_range= 0.1, # randomly shift images vertically (fraction of total height) horizontal_flip=True, # randomly flip images vertical_flip=False) # randomly flip images train_datagen.fit(X_train) train_generator = train_datagen.flow(X_train, Y_train, batch_size=batchSize) # fine-tune the model nb_train_samples = X_train.shape[0] // batchSize nb_epoch = num_epoch model.fit_generator(train_generator, steps_per_epoch=nb_train_samples, epochs=nb_epoch, validation_data=(X_test, Y_test), callbacks=[checkPoint, reduce_lr, csvlog]) # model.fit(X_train, Y_train, # batch_size=128, # epochs=num_epoch, # callbacks=[checkPoint, reduce_lr, csvlog], # validation_data=(X_test, Y_test), # shuffle=True) del model
if 'cifar' in dataname: # if you normalize data to 0 and 1 #bounds = (0, 1.0) # if the image value range is still between 0 and 255. bounds = (0, 255.0) weightspaths = cifarmodelweights else: # if you normalize data to 0 and 1 bounds = (0, 1.0) # if the image value range is still between 0 and 255. #bounds = (0, 255.0) weightspaths = mnistmodelweights[dataname] name = args['model'] (x_train, _), (_, _), (_, _, num_class) = datama.getData(dataname) bestModelName = path + weightspaths[name] if name == 'mlp': x_train = x_train.reshape(x_train.shape[0], -1) # x_test = x_test.reshape(x_test.shape[0], -1) model = helper.load_model(name, bestModelName, x_train.shape[1:], num_class, isDrop=False) del x_train if not os.path.isfile('./adv_data/slectedTest10000ImgsIdx'): cx_train, cy_train, cidx = selectData(model, dataname,