def keras_to_uff(model_choice, weights): """ This is important to READ. This must be done in a Tensorflow Version <2. In otherwords a Tensorflow version where graph computation was the norm. Due to the changes in Tensorflow 2, converting to uff is basically impossible to do since all the uff conversions now use DEPRECATED and REMOVED functions and classes. If I'm honest, this is a PITA. Reference for this python file: https://devtalk.nvidia.com/default/topic/1028464/jetson-tx2/converting-tf-model-to-tensorrt-uff-format/ """ K.set_learning_phase(0) if model_choice == 'fastscnn': model = fast_scnn.model(num_classes=20, input_size=(1024, 2048, 3)) input_size = '1024x2048' elif model_choice == 'deeplabv3+': # Its important here to set the output stride to 8 for inferencing model = deeplabv3plus.model(num_classes=20, input_size=(1024, 2048, 3), depthwise=True, output_stride=8) input_size = '1024x2048' elif model_choice == 'separable_unet': # Was trained on a lower resolution model = separable_unet.model(num_classes=20, input_size=(512, 1024, 3)) input_size = '512x1024' # Whatever the model is, load the weights chosen model.load_weights(weights) # Plot the model for visual purposes in case anyone asks what you used tf.keras.utils.plot_model(model, to_file=os.path.join('./results', model_choice, model_choice + '.png'), show_shapes=True) # Get the outputs outputs = [] for output in model.outputs: outputs.append(output.name.split(":")[0]) # Set the filename for the frozen graph frozen_graph = os.path.join('./results', model_choice, model_choice + '_' + input_size + '.pb') # Let's begin session = K.get_session() # Get the graph definition and remove training nodes, ignore deprecation warnings here... graph_def = tf.graph_util.convert_variables_to_constants( session, session.graph_def, outputs) graph_def = tf.graph_util.remove_training_nodes(graph_def) # Write frozen graph to file with open(frozen_graph, 'wb') as f: f.write(graph_def.SerializeToString()) f.close() # Get the uff filename uff_filename = frozen_graph.replace('.pb', '.uff') # Convert and save as uff and we're done uff_model = uff.from_tensorflow_frozen_model(frozen_graph, outputs, output_filename=uff_filename)
def quantize_in_training_phase(model_file, ckpt, inputs): graph = tf.Graph() with tf.Session(graph=graph) as sess: K.set_session(sess) with graph.as_default(): K.set_learning_phase(1) model = keras.models.load_model(model_file, compile=False) original_preds = model.predict(inputs) tf.contrib.quantize.create_training_graph(input_graph=graph, quant_delay=0) sess.run(tf.global_variables_initializer()) quant_vars = select_quantization_variables(tf.global_variables()) for v in quant_vars: logger.debug(f'quatization variable added: {v.name}') model.compile(optimizer=keras.optimizers.SGD(0), loss='mean_squared_error') logger.info('start to optimize quantization stats.') model.fit(inputs, original_preds, epochs=1, verbose=0) for v in quant_vars: logger.debug(f'quantization stats: {v.name} = {sess.run(v)}') saver = tf.train.Saver() logger.info('save checkpoints to "{ckpt}"') saver.save(sess, ckpt)
def loadGraph(pbfpath, prefix=""): """ (1) About prefix. If we set name(prefix) = None,then it will add "import/" prefix to each node. And we should use name(prefix) = "" to keep none prefix. (2) It may be faild when load pb graph if it contains BN layers. """ tf.keras.backend.set_learning_phase(0) K.set_learning_phase(0) K.clear_session() # (1) Load the protobuf file from the disk and parse it to retrieve the unserialized graph_def. #with tf.gfile.GFile(pbfpath, "rb") as fin: with open(pbfpath, "rb") as fin: graph_def = tf.GraphDef() graph_def.ParseFromString(fin.read()) # (2) Import a graph_def into the current default Graph. with tf.Graph().as_default() as graph: tf.import_graph_def( graph_def, input_map=None, return_elements=None, name=prefix, op_dict=None, producer_op_list=None ) # We can verify that we can access the list of operations in the graph for op in graph.get_operations(): pass #print(op.name) return graph """
def load_eval_model(model_path): # support of tflite model if model_path.endswith('.tflite'): from tensorflow.lite.python import interpreter as interpreter_wrapper model = interpreter_wrapper.Interpreter(model_path=model_path) model.allocate_tensors() model_format = 'TFLITE' # support of MNN model elif model_path.endswith('.mnn'): model = MNN.Interpreter(model_path) model_format = 'MNN' # support of TF 1.x frozen pb model elif model_path.endswith('.pb'): model = load_graph(model_path) model_format = 'PB' # support of ONNX model elif model_path.endswith('.onnx'): model = onnxruntime.InferenceSession(model_path) model_format = 'ONNX' # normal keras h5 model elif model_path.endswith('.h5'): custom_object_dict = get_custom_objects() model = load_model(model_path, compile=False, custom_objects=custom_object_dict) model_format = 'H5' K.set_learning_phase(0) else: raise ValueError('invalid model file') return model, model_format
def UNet_evaluation(eval_x): #Path for saving CNN model p1 = "./models/model.json" p2 = "./models/weights.h5" #If the path exist if os.path.isfile(p1) and os.path.isfile(p2): #Change the learning phase into test mode backend.set_learning_phase(0) #Read the pre-learned model and its weight with open(p1, "r") as f: cnn_model = model_from_json(f.read()) cnn_model.load_weights(p2) #Add a color dimension to input (for Tensorflow format) x = eval_x[:, :, :, np.newaxis] #Get the separated source for evaluation data y = cnn_model.predict(x) #y = cnn.model.predict_on_batch(x) #Remove a color dimension from input (for Tensorflow format) eval_y = y[:, :, :, 0] #Restart the session to relieve the GPU memory (to prevent Resource Exhausted Error) backend.clear_session() #backend.get_session() #Less than tensorflow ver.1.14 del cnn_model gc.collect() return eval_y
def test_stochastic_binary(): np.random.seed(42) K.set_learning_phase(1) x = np.random.uniform(-0.01, 0.01, size=10) x = np.sort(x) s = stochastic_binary(alpha="auto_po2") ty = np.zeros_like(s) ts = 0.0 n = 1000 for _ in range(n): y = K.eval(s(K.constant(x))) scale = K.eval(s.scale)[0] ts = ts + scale ty = ty + (y / scale) result = (ty / n).astype(np.float32) scale = np.array([ts / n]) expected = np.array( [-1., -1., -1., -0.852, 0.782, 0.768, 0.97, 0.978, 1.0, 1.0]).astype(np.float32) expected_scale = np.array([0.003906]) assert_allclose(result, expected, atol=0.1) assert_allclose(scale, expected_scale, rtol=0.1)
def __init__(self, h5fpath, input_shape): assert len(input_shape) == 2 or input_shape[2] == 1 self.input_shape = input_shape self.input_size = (int(input_shape[1]), int(input_shape[0])) tf.keras.backend.set_learning_phase(0) K.set_learning_phase(0) self.model = models.load_model(h5fpath, custom_objects={})
def __init__(self, **kwargs): super(DeepLab, self).__init__() self.__dict__.update(self._defaults) # set up default values self.__dict__.update(kwargs) # and update with user overrides self.class_names = get_classes(self.classes_path) K.set_learning_phase(0) self.deeplab_model = self._generate_model()
def convert_model(self, model_path): k.clear_session() k.set_learning_phase(0) model = tf.keras.models.load_model(model_path, compile=False) model_layers = model.layers self._img_size = model.input_shape[1:3] self.model_path = os.path.abspath(model_path) if 'k210' in self._converter_type: self.convert_tflite(model, model_layers, 'k210') self.convert_k210(self.model_path.split(".")[0] + '.tflite') if 'edgetpu' in self._converter_type: self.convert_tflite(model, model_layers, 'edgetpu') self.convert_edgetpu(model_path.split(".")[0] + '.tflite') if 'onnx' in self._converter_type: import tf2onnx self.convert_onnx(model, model_layers) if 'openvino' in self._converter_type: model.save(model_path.split(".")[0]) self.convert_ir(model_path, model_layers) self.convert_oak(model_path) if 'tflite' in self._converter_type: self.convert_tflite(model, model_layers, self._converter_type)
def __init__(self): K.clear_session() K.set_learning_phase(0) self.classes = utils.read_class_names(cfg.RDT_Reader.CLASSES) self.num_classes = len(self.classes) self.time = time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime(time.time())) self.moving_ave_decay = cfg.TRAIN.MOVING_AVE_DECAY self.train_logdir = "./dataset/log/train" # self.testset = data_loader.loadDataObjSSD('test') self.checkpoint_name = cfg.TEST.EVAL_MODEL_PATH + "/eval.ckpt" self.model_path = cfg.TEST.EVAL_MODEL_PATH + "/model/" self.eval_tflite = cfg.TEST.EVAL_MODEL_PATH + "/OD_180x320_newarch_resnet_data_qnt.lite" self.initial_weight = cfg.TEST.WEIGHT_FILE self.output_node_names = ["define_loss/reshapedOutput"] self.learn_rate_init = cfg.TRAIN.LEARN_RATE_INIT self.learn_rate_end = cfg.TRAIN.LEARN_RATE_END self.first_stage_epochs = cfg.TRAIN.FISRT_STAGE_EPOCHS self.second_stage_epochs = cfg.TRAIN.SECOND_STAGE_EPOCHS self.warmup_periods = cfg.TRAIN.WARMUP_EPOCHS # self.trainset = data_loader.loadData('train') self.testset = data_loader.loadDataObjSSDFromYoloFormat('test') # self.trainset = data_loader.loadDataObjSSDFromYoloFormat('train') # self.steps_per_period = len(self.trainset) self.quant_delay = cfg.TRAIN.QUANT_DELAY self.quantizedPb = cfg.TEST.QUANTIZED_WEIGHT_FILE self.resize_dim = tuple(cfg.TEST.INPUT_SIZE) self.number_blocks = cfg.TRAIN.NUMBER_BLOCKS self.model = ObjectDetection(True, self.initial_weight).model self.anch = cfg.TRAIN.ANCHOR_ASPECTRATIO
def load_eval_model(model_path): # support of tflite model if model_path.endswith('.tflite'): from tensorflow.lite.python import interpreter as interpreter_wrapper model = interpreter_wrapper.Interpreter(model_path=model_path) model.allocate_tensors() model_format = 'TFLITE' # support of MNN model elif model_path.endswith('.mnn'): model = MNN.Interpreter(model_path) model_format = 'MNN' # support of TF 1.x frozen pb model elif model_path.endswith('.pb'): model = load_graph(model_path) model_format = 'PB' # normal keras h5 model elif model_path.endswith('.h5'): model = load_model(model_path, compile=False) model_format = 'H5' K.set_learning_phase(0) else: raise ValueError('invalid model file') return model, model_format
def __init__(self): self.graph = tf.Graph() config = tf.ConfigProto() config.gpu_options.allow_growth = True session = tf.Session(config=config, graph=self.graph) #KTF.set_session(session) K.set_session(session) #K._LEARNING_PHASE = tf.constant(0) K.set_learning_phase(0) self.model_path = CONFIG.yolo3_weigths_path self.anchors_path = CONFIG.yolo3_anchors_path self.classes_path = CONFIG.yolo3_classes_path self.score = CONFIG.yolo3_score_threshold self.iou = CONFIG.yolo3_iou_threshold self.class_names = self._get_class() self.anchors = self._get_anchors() self.sess = K.get_session() self.model_image_size = (416, 416) # fixed size or (None, None), hw self.font = ImageFont.truetype(font=CONFIG.font_path, size=np.floor(25.0).astype('int32')) with self.graph.as_default(): self.boxes, self.scores, self.classes = self.generate()
def export(**options): # Model class catalogue models = { 'quartznet': QuartzNet, 'jasper': Jasper, } # Construct model K.set_learning_phase(0) input_ph = tf.placeholder(dtype=tf.float32, shape=[None, 64], name='input') expanded_ph = tf.expand_dims(input_ph, axis=0) model = QuartzNet(b=1, r=5) logits = model(expanded_ph, training=False) prep_conv_size = model.get_layer_configuration( )['prep_config']['kernel_size'] outputs = beam_search_decoder(logits, tf.expand_dims(tf.shape(expanded_ph)[1], axis=0), prep_conv_kernel_size=prep_conv_size, to_dense=True) _ = tf.identity(outputs, name='output') # Restore model weights saver = tf.train.Saver() with tf.Session() as sess: saver.restore(sess, tf.train.latest_checkpoint(options['model_dir'])) # Convert variables to constants constant_graph = tf.graph_util.convert_variables_to_constants( sess, sess.graph_def, ['output'], ['input', 'output']) # Serialize optimized graph output_dir = options['output_dir'] or options['model_dir'] with tf.gfile.FastGFile(os.path.join(output_dir, 'frozen_graph.pb'), 'w') as f: f.write(constant_graph.SerializeToString())
def main(): parser = argparse.ArgumentParser(description='tf.keras model FLOPs & PARAMs checking tool') parser.add_argument('--model_path', type=str, required=True, help='model file to evaluate') parser.add_argument('--model_input_shape', type=str, required=False, default=None, help='model image input shape as <height>x<width>, optional') args = parser.parse_args() custom_object_dict = get_custom_objects() model = load_model(args.model_path, compile=False, custom_objects=custom_object_dict) batch, height, width, channel = model.input.shape.as_list() if args.model_input_shape: height, width = args.model_input_shape.split('x') height, width = int(height), int(width) # to calculate FLOPs we need to use fixed input shape & batch size assert height and width and channel, 'input shape should be specified' if not batch: # if dynamic batch, rebuild model with batch_size=1 input_tensor = Input(shape=(height, width, channel), batch_size=1) output_tensor = model(input_tensor) model = Model(input_tensor, output_tensor) K.set_learning_phase(0) get_flops(model)
def test_stochastic_binary_inference_mode(alpha, test_values, expected_values): K.set_learning_phase(0) x = K.placeholder(ndim=2) q = stochastic_binary(alpha) f = K.function([x], [q(x)]) result = f([test_values])[0] assert_allclose(result, expected_values, rtol=1e-05)
def save_to_pb(keras_model, export_path): """ Save keras model to protobuf for Tensorflow Serving. Source: https://medium.com/@johnsondsouza23/export-keras-model-to-protobuf-for-tensorflow-serving-101ad6c65142 Parameters ---------- keras_model: Keras model instance export_path: str """ # Set the learning phase to Test since the model is already trained. K.set_learning_phase(0) # Build the Protocol Buffer SavedModel at 'export_path' builder = saved_model_builder.SavedModelBuilder(export_path) # Create prediction signature to be used by TensorFlow Serving Predict API signature = predict_signature_def(inputs={"images": keras_model.input}, outputs={"scores": keras_model.output}) with K.get_session() as sess: # Save the meta graph and the variables builder.add_meta_graph_and_variables( sess=sess, tags=[tag_constants.SERVING], signature_def_map={"predict": signature}) builder.save()
def main(base_model_name, weights_file, export_path): # Load model and weights nima = Nima(base_model_name, weights=None) nima.build() nima.nima_model.load_weights(weights_file) # Tell keras that this will be used for making predictions K.set_learning_phase(0) # CustomObject required by MobileNet with CustomObjectScope({ 'relu6': relu6, 'DepthwiseConv2D': DepthwiseConv2D }): builder = saved_model_builder.SavedModelBuilder(export_path) signature = predict_signature_def( inputs={'input_image': nima.nima_model.input}, outputs={'quality_prediction': nima.nima_model.output}) builder.add_meta_graph_and_variables( sess=K.get_session(), tags=[tag_constants.SERVING], signature_def_map={'image_quality': signature}) builder.save() print(f'TF model exported to: {export_path}')
def freeze_model(model_h5_path='model.h5'): K.set_learning_phase(0) model = tf.keras.models.load_model(model_h5_path) frozen_graph = freeze_session( K.get_session(), output_names=[out.op.name for out in model.outputs]) tf.train.write_graph(frozen_graph, "model", "model.pb", as_text=False)
def test_save_load_all(self): for backend in self.list_backends(): try: set_keras_backend(backend) except ModuleNotFoundError: continue K.set_learning_phase(0) # test for use_attn_mask in [True, False]: model = self.create_small_model(use_attn_mask) path = '/tmp/{}.model'.format(uuid.uuid4()) try: model.save(path) new_model = keras.models.load_model( path, custom_objects={ 'MultiHeadAttention': MultiHeadAttention, 'LayerNormalization': LayerNormalization, 'Gelu': Gelu }) TestTransformer.compare_two_models(model, new_model) except Exception as e: raise e finally: if os.path.exists(path): os.remove(path)
def save_model_pb(model, pb_filename): from tensorflow.keras import backend as K K.set_learning_phase(0) frozen_graph = _freeze_session(K.get_session(), output_names=[out.op.name for out in model.outputs]) tf.train.write_graph(frozen_graph, 'model/', pb_filename, as_text=False)
def _store_keras(self) -> None: K.set_learning_phase(0) # prevent model from modifying weights model_json = self.model.to_json() with open(self.keras_json, 'w') as json_file: json_file.write(model_json) self.model.save_weights(self.keras_h5) p.print_success(f'Successfully stored Keras model: {self.model_name}')
def finalize_for_ocv(model_path): K.set_learning_phase(0) model = load_model(model_path) cv_model = augment_for_ocv(model) cv_model_path = model_path.rpslit('.')[0] + '_cv.hdf5' cv_model.save(cv_model_path) keras_to_opencv(cv_model_path)
def get_model(framework, model_variant): """ Load the desired EfficientPose model variant using the requested deep learning framework. Args: framework: string Deep learning framework to use (Keras, TensorFlow, TensorFlow Lite or PyTorch) model_variant: string EfficientPose model to utilize (RT, I, II, III, IV, RT_Lite, I_Lite or II_Lite) Returns: Initialized EfficientPose model and corresponding resolution. """ # Keras if framework in ['keras', 'k']: from tensorflow.keras.backend import set_learning_phase from tensorflow.keras.models import load_model set_learning_phase(0) model = load_model(join('models', 'keras', 'EfficientPose{0}.h5'.format(model_variant.upper())), custom_objects={'BilinearWeights': helpers.keras_BilinearWeights, 'Swish': helpers.Swish(helpers.eswish), 'eswish': helpers.eswish, 'swish1': helpers.swish1}) # TensorFlow elif framework in ['tensorflow', 'tf']: from tensorflow.python.platform.gfile import FastGFile from tensorflow.compat.v1 import GraphDef from tensorflow.compat.v1.keras.backend import get_session from tensorflow import import_graph_def f = FastGFile(join('models', 'tensorflow', 'EfficientPose{0}.pb'.format(model_variant.upper())), 'rb') graph_def = GraphDef() graph_def.ParseFromString(f.read()) f.close() model = get_session() model.graph.as_default() import_graph_def(graph_def) # TensorFlow Lite elif framework in ['tensorflowlite', 'tflite']: from tensorflow import lite model = lite.Interpreter(model_path=join('models', 'tflite', 'EfficientPose{0}.tflite'.format(model_variant.upper()))) model.allocate_tensors() # PyTorch elif framework in ['pytorch', 'torch']: from imp import load_source from torch import load, quantization, backends try: MainModel = load_source('MainModel', join('models', 'pytorch', 'EfficientPose{0}.py'.format(model_variant.upper()))) except: print('\n##########################################################################################################') print('Desired model "EfficientPose{0}Lite" not available in PyTorch. Please select among "RT", "I", "II", "III" or "IV".'.format(model_variant.split('lite')[0].upper())) print('##########################################################################################################\n') return False, False model = load(join('models', 'pytorch', 'EfficientPose{0}'.format(model_variant.upper()))) model.eval() qconfig = quantization.get_default_qconfig('qnnpack') backends.quantized.engine = 'qnnpack' return model, {'rt': 224, 'i': 256, 'ii': 368, 'iii': 480, 'iv': 600, 'rt_lite': 224, 'i_lite': 256, 'ii_lite': 368}[model_variant]
def test_qbidirectional(rnn, all_weights_signature, expected_output): K.set_learning_phase(0) np.random.seed(22) tf.random.set_seed(22) x = x_in = Input((2, 4), name='input') x = QBidirectional( rnn(16, activation="quantized_po2(8)", kernel_quantizer="quantized_po2(8)", recurrent_quantizer="quantized_po2(8)", bias_quantizer="quantized_po2(8)", name='qbirnn_0'))(x) x = QDense(4, kernel_quantizer=quantized_bits(8, 2, 1, alpha=1.0), bias_quantizer=quantized_bits(8, 0, 1), name='dense')(x) x = Activation('softmax', name='softmax')(x) model = Model(inputs=[x_in], outputs=[x]) # reload the model to ensure saving/loading works json_string = model.to_json() clear_session() model = quantized_model_from_json(json_string) # Save the model as an h5 file using Keras's model.save() fd, fname = tempfile.mkstemp('.h5') model.save(fname) del model # Delete the existing model # Return a compiled model identical to the previous one model = load_qmodel(fname) # Clean the created h5 file after loading the model os.close(fd) os.remove(fname) # apply quantizer to weights model_save_quantized_weights(model) all_weights = [] for layer in model.layers: for i, weights in enumerate(layer.get_weights()): w = np.sum(weights) all_weights.append(w) all_weights = np.array(all_weights) assert all_weights.size == all_weights_signature.size assert np.all(all_weights == all_weights_signature) # test forward: inputs = 2 * np.random.rand(10, 2, 4) actual_output = model.predict(inputs).astype(np.float16) assert_allclose(actual_output, expected_output, rtol=1e-4)
def __init__(self, image_size, is_learning_phase=False): K.set_learning_phase(int(is_learning_phase)) K.reset_uids() self.image_size = image_size self.n_cells = self.image_size // 32 self.m = self.buildModel() self.model_compile()
def __init__(self, **kwargs): super(YOLO, self).__init__() self.__dict__.update(self._defaults) # 还能这样? self.__dict__.update(kwargs) # update with user overrides self.class_names = get_classes(self.classes_path) self.anchors = get_anchors(self.anchors_path) self.colors = get_colors(self.class_names) K.set_learning_phase(0) self.inference_model = self._generate_model()
def __init__(self, **kwargs): super(YOLO_np, self).__init__() self.__dict__.update(self._defaults) # set up default values self.__dict__.update(kwargs) # and update with user overrides self.class_names = get_classes(self.classes_path) self.anchors = get_anchors(self.anchors_path) self.colors = get_colors(self.class_names) K.set_learning_phase(0) self.yolo_model = self._generate_model()
def train(data_file, src_type, epochs, init_epochs=0, model_struc="densenet_gru", weights_path=""): tf_config() K.set_learning_phase(True) # 加载模型 train_model = work_net(stage="train", img_size=CHAR_IMG_SIZE, model_struc=model_struc) compile(train_model, loss_names=["char_struc_loss", "sc_char_loss", "lr_compo_loss"]) # 增加度量汇总 metrics_summary = train_model.get_layer('summary_fn').output add_metrics(train_model, metric_name_list=[ "char_struc_acc", "sc_acc", "sc_top3", "sc_top5", "lr_acc", "lr_top3", "lr_top5", "correct_lr_acc", "correct_lr_top3", "correct_lr_top5", "total_acc", "total_top3", "total_top5" ], metric_val_list=metrics_summary) train_model.summary() # for layer in train_model.layers: # print(layer.name, " trainable: ", layer.trainable) # load model load_path = os.path.join( CHAR_RECOG_CKPT_DIR, "char_recog_with_compo_" + model_struc + "_{:04d}.h5".format(init_epochs)) weights_path = weights_path if os.path.exists(weights_path) else load_path if os.path.exists(weights_path): train_model.load_weights(weights_path, by_name=True) print("\nLoad model weights from %s\n" % weights_path) training_generator, validation_generator = data_generator( data_file=data_file, src_type=src_type) # 开始训练 train_model.fit_generator(generator=training_generator, steps_per_epoch=200, epochs=epochs + init_epochs, initial_epoch=init_epochs, verbose=1, validation_data=validation_generator, validation_steps=10, callbacks=get_callbacks(model_struc), max_queue_size=100) # 保存模型 train_model.save_weights( os.path.join(CHAR_RECOG_CKPT_DIR, "char_recog_with_compo_" + model_struc + "_finished.h5"))
def test_stochastic_round_quantized_relu_po2(test_values, expected_values): K.set_learning_phase(1) np.random.seed(666) x = K.placeholder(ndim=2) q = quantized_relu_po2(use_stochastic_rounding=True) f = K.function([x], [q(x)]) res = f([test_values])[0] res = np.average(res) assert_allclose(res, expected_values, rtol=1e-01, atol=1e-6)
def setUpClass(cls): (x_train, y_train), (x_test, y_test), min_, max_ = load_mnist() x_train, y_train = x_train[:NB_TRAIN], y_train[:NB_TRAIN] cls.mnist = (x_train, y_train), (x_test, y_test), (min_, max_) # Create simple keras model import tensorflow as tf tf_version = [int(v) for v in tf.__version__.split(".")] if tf_version[0] == 2 and tf_version[1] >= 3: tf.compat.v1.disable_eager_execution() from tensorflow.keras import backend as k from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense, Flatten, Conv2D, MaxPooling2D else: import keras.backend as k from keras.models import Sequential from keras.layers import Dense, Flatten, Conv2D, MaxPooling2D k.set_learning_phase(1) model = Sequential() model.add( Conv2D(32, kernel_size=(3, 3), activation="relu", input_shape=x_train.shape[1:])) model.add(MaxPooling2D(pool_size=(3, 3))) model.add(Flatten()) model.add(Dense(10, activation="softmax")) model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"]) from art.estimators.classification.keras import KerasClassifier cls.classifier = KerasClassifier(model=model, clip_values=(min_, max_)) cls.classifier.fit(x_train, y_train, nb_epochs=1, batch_size=128) cls.defence = ActivationDefence(cls.classifier, x_train, y_train) datagen = ImageDataGenerator() datagen.fit(x_train) data_gen = KerasDataGenerator(datagen.flow(x_train, y_train, batch_size=NB_TRAIN), size=NB_TRAIN, batch_size=NB_TRAIN) cls.defence_gen = ActivationDefence(cls.classifier, None, None, generator=data_gen)