def main():
    # Define the parser
    parser = argparse.ArgumentParser()
    parser.add_argument('--batch_size',
                        type=int,
                        default=32,
                        help='minibatch size')

    # Read the arguments
    eval_args = parser.parse_args()

    with open(os.path.join('saved_models', 'config.pkl'), 'rb') as f:
        saved_args = pickle.load(f)

    # Initialize with the saved args
    model = Model(saved_args, 'eval')
    # Initialize TensorFlow session
    sess = tf.InteractiveSession()
    # Initialize TensorFlow saver
    saver = tf.train.Saver()

    # restore the session
    saver.restore(sess, 'saved_models/model.ckpt')

    data_loader = DataLoader(eval_args.batch_size, mode='eval')

    data_loader.load_data('/home/kevin/ncfm_data', '/home/kevin/test.txt')
    data_loader.convert2images(maxn=saved_args.input_dim[0])

    # Maintain the total_error until now
    accuracy = 0
    total_loss = 0.
    counter = 0.

    for e in range(1):
        # Reset the data pointers of the data loader object
        data_loader.reset_batch_pointer()

        for b in range(data_loader.num_batches):
            start = time.time()
            # Get the source, target data for the next batch
            x, y = data_loader.next_batch()

            feed = {model.input_data: x, model.target_data: y}
            loss, pred = sess.run([model.loss, model.pred], feed)

            accuracy += np.sum(np.array(pred) == np.array(y))
            total_loss += loss
            end = time.time()
            print "Processed batch number : ", b, "out of ", data_loader.num_batches, " batches, time consuming: ", end - start

            counter += data_loader.batch_size

    # Print the mean error across all the batches
    print "Total mean accuracy of the evaluation is ", accuracy / counter
    print "Total mean testing loss of the model is ", total_loss / counter
	def load_model(self):

		# Load the saved arguments to the model from the config file
		with open(os.path.join(self.tf_model_path, 'config.pkl'), 'rb') as f:
			self.saved_args = pickle.load(f)

		# Initialize with the saved args
		model = Model(self.saved_args, is_training=False)
		# Initialize TensorFlow session
		sess = tf.InteractiveSession()
		# Initialize TensorFlow saver
		saver = tf.train.Saver()

		saver.restore(sess, os.path.join(self.tf_model_path, 'model.ckpt'))

		self.sess = sess
		self.model = model
예제 #3
0
    num_rand_samples = 1328
    random_samples = np.random.randint(0, num_samples, num_rand_samples)
    new_X_test = np.zeros((num_rand_samples, 1, 28, 28))
    for i, sample_no in enumerate(random_samples):
        new_X_test[i, 0, :, :] = (X_test[sample_no, :, :, 0])
    new_Y_test = Y_test[random_samples, :]

    f = open(os.path.join(args.log_dir, 'Random_Test_%s_.p' % (dataset)), 'w')
    pickle.dump({"adv_input": new_X_test, "adv_labels": new_Y_test}, f)
    f.close()
    if (args.attack == 'cw-l2' or args.attack == 'all'):
        #No softmax for Carlini attack
        pytorch_network = Net()
        pytorch_network.load_state_dict(torch.load(args_ckpt))
        pytorch_network.eval()
        model = Model(torch_model=pytorch_network, softmax=False)
        keras_network = model.model
        transfer.pytorch_to_keras(pytorch_network, model.model)
        pytorch_network.eval()
        model = model.model
        batch_size = 16
        craft_one_type(sess,
                       model,
                       new_X_test,
                       new_Y_test,
                       dataset,
                       'cw-l2',
                       batch_size,
                       log_path=args.log_dir)

    if (args.attack == 'xent' or args.attack == 'cw_pgd'):
def train(args):

	valid = True

	# Create the data loader object. This object would preprocess the data in terms of
	# batches each of size args.batch_size, of length args.seq_length
	train_data_loader = DataLoader(args.batch_size, mode='train')
	valid_data_loader = DataLoader(args.batch_size, mode='valid')

	# data_loader.load_data('/home/kevin/ncfm_data', '/home/kevin/train.txt')
	train_data_loader.load_data('/home/kevin/data/ncfm/bbox_train', '/home/kevin/data/ncfm/train.txt')
	train_data_loader.convert2images(maxn=args.input_dim[0])

	if valid:
		valid_data_loader.load_data('/home/kevin/data/ncfm/bbox_valid', '/home/kevin/data/ncfm/valid.txt')
		valid_data_loader.convert2images(maxn=args.input_dim[0])

	# Create a MLP model with the arguments
	model = Model(args)

	# Initialize a TensorFlow session
	with tf.Session() as sess:
		# Add all the variables to the list of variables to be saved
		saver = tf.train.Saver(tf.global_variables())

		# Initialize all the variables in the graph
		sess.run(tf.global_variables_initializer())

		tf.summary.scalar('Realtime loss', model.loss)
		tf.summary.scalar('Realtime learning rate', model.lr)
		all_summaries = tf.summary.merge_all()
		train_writer = tf.summary.FileWriter(os.path.join(args.model_dir, 'log'), sess.graph)

		# save the configuration and model
		if os.path.isfile(os.path.join(args.model_dir, 'config.pkl')):
			# Get the checkpoint state to load the model from
			ckpt = tf.train.get_checkpoint_state(args.model_dir)
			print('loading model: ', ckpt.model_checkpoint_path)
			# Restore the model at the checpoint
			saver.restore(sess, ckpt.model_checkpoint_path)
		else:
			# Save the arguments int the config file
			with open(os.path.join(args.model_dir, 'config.pkl'), 'wb') as f:
				pickle.dump(args, f)

		# For each epoch
		for e in range(args.num_epochs):
			# Assign the learning rate (decayed acc. to the epoch number)
			sess.run(tf.assign(model.lr, args.learning_rate * (args.decay_rate ** e)))
			# Assign is_training
			# sess.run(tf.assign(model.is_training, True))
			# Reset the pointers in the data loader object
			train_data_loader.reset_batch_pointer()

			# For each batch in this epoch
			train_loss = 0.
			train_counter = 0.
			train_accuracy = 0.
			labels = []
			preds = []


			if valid and e % 1 == 0:

				valid_loss = 0.
				valid_accuracy = 0.
				valid_counter = 0.

				# Assign is_training
				# sess.run(tf.assign(model.is_training, False))

				valid_data_loader.reset_batch_pointer()

				for b in range(valid_data_loader.num_batches):
					x, y = valid_data_loader.next_batch()

					start = time.time()

					feed = {model.input_data: x, model.target_data: y}
					loss, pred = sess.run([model.loss, model.pred], feed)

					end = time.time()

					valid_loss += loss
					valid_accuracy += np.sum(np.array(pred) == np.array(y))
					valid_counter += args.batch_size
					labels += y
					preds += pred.tolist()

				print "----------------------------------------------------------------------------------------------------------------------"
				print(
					"validation at epoch {}, valid_loss = {:.6f}, accuracy = {:.3f}, time/batch = {:.6f}"
						.format(
						e,
						valid_loss / valid_counter,
						valid_accuracy / valid_counter,
						end - start))

				print utility.get_per_class_accuracy(preds, labels, args.output_dim)

			# training phase
			#sess.run(tf.assign(model.is_training, True))

			for b in range(train_data_loader.num_batches):
				# Tic
				start = time.time()
				# Get the source and target data of the current batch
				# x has the source data, y has the target data
				x, y = train_data_loader.next_batch()

				feed = {model.input_data: x, model.target_data: y}
				loss, pred, summaries, _ = sess.run([model.loss, model.pred, all_summaries, model.train_op], feed)

				train_writer.add_summary(summaries, e*train_data_loader.num_batches+b)

				# Toc
				end = time.time()
				# Print epoch, batch, loss and time taken
				#train_writer.add_summary(summary, train_step)
				train_loss += loss
				train_accuracy += np.sum(np.array(pred) == np.array(y))
				train_counter += args.batch_size

				if b % args.display == 0:
					print(
						"training {}/{} (epoch {}), train_loss = {:.6f}, accuracy = {:.3f}, time/batch = {:.6f}, learning rate = {:.6f}"
							.format(
							e * train_data_loader.num_batches + b,
							args.num_epochs * train_data_loader.num_batches,
							e,
							train_loss / train_counter,
							train_accuracy / train_counter,
							end - start,
							sess.run(model.lr)))

					train_loss = 0.
					train_counter = 0.
					train_accuracy = 0.

			# Save the model if the current epoch and batch number match the frequency
			if (e) % args.save_every == 0 and (
				(e * train_data_loader.num_batches + b) > 0): #* train_data_loader.num_batches + b
				checkpoint_path = os.path.join(args.model_dir, 'model.ckpt')
				saver.save(sess, checkpoint_path, global_step=e * train_data_loader.num_batches + b+1)
				print("model saved to {}".format(checkpoint_path))

				# Save the arguments int the config file
				with open(os.path.join(args.model_dir, 'config.pkl'), 'wb') as f:
					pickle.dump(args, f)
					saver.save(sess, os.path.join(args.model_dir, 'model.ckpt'))
예제 #5
0
    def train(self):
        # zapobieganie wyświetlanie notacji naukowej (to z e) przy print
        np.set_printoptions(suppress=True)
        # pobranie ustawień
        train_folder = self.__configuration.paths().train_cache()
        validation_folder = self.__configuration.paths().validation_cache()
        train_files = os.listdir(train_folder)
        validation_files = os.listdir(validation_folder)
        batch_size = self.__configuration.data().batch_size()
        input_size = self.__configuration.data().input_size()
        device = self.__configuration.settings().device()
        iterations = self.__configuration.settings().iterations()
        # do folderu z modelem dodajemy datę rozpoczęcia uczenia
        start_date = "model_1"
        output_model_folder = self.__configuration.paths().model(
        ) + "_" + start_date
        learning_rate = self.__configuration.settings().learning_rate()
        num_classes = self.__configuration.settings().num_classes()

        # utworzenie elementów tensorflow, do których wstawiane będa dane treningowe
        train_x_input, train_y_input = self.__create_placeholders(
            batch_size, input_size, num_classes)
        learning_rate_placeholder = tf.placeholder(tf.float32, None)
        tf_session = self.__create_tf_session()

        with tf.variable_scope("scope", reuse=tf.AUTO_REUSE):
            model = Model.create(self.__configuration, train_x_input)
            # utworzenie funkcji potrzebnych do oceny modelu
            cost = tf.reduce_mean(
                tf.nn.softmax_cross_entropy_with_logits_v2(
                    logits=model, labels=train_y_input))
            optimizer = tf.train.AdamOptimizer(
                learning_rate=learning_rate_placeholder).minimize(cost)
            correct_predictions = tf.equal(tf.argmax(model, 1),
                                           tf.argmax(train_y_input, 1))
            accuracy_op = tf.reduce_mean(
                tf.cast(correct_predictions, tf.float32))
            tf.summary.scalar('accuracy', accuracy_op)
            merged = tf.summary.merge_all()
            test_writer = tf.summary.FileWriter(
                self.__configuration.paths().output(), tf_session.graph)
            # obiekt zapisujący model
            saver = Saver()
            # inicjalizacja sesji tensorflow
            tf_session.run(tf.global_variables_initializer())
            # tablica przechowująca wyniki poszczególnych iteracji
            accuracies = []
            with tf.device('/device:' + device):
                counter = 0
                for iteration in range(iterations):
                    train_accuracy = 0.0
                    batch_counter = 0
                    print("iteration " + str(iteration))
                    # mieszanie kolejności odczytu plików z dysku
                    random.shuffle(train_files)
                    for batch_file in train_files:
                        # odczyt partii z dysku
                        data = self.__get_data_from_batch_file(
                            batch_file, train_folder)
                        # mieszanie tablic z danymi i etykietami
                        shuffled_X, shuffled_y = shuffle(
                            data.get_X(), data.get_y())
                        # tworzenie i iteracja po partiach danych
                        for batch_X, batch_y in self.__get_batchs_list(
                                shuffled_X, shuffled_y, batch_size):
                            # karmienie sieci
                            _, acc = tf_session.run(
                                [optimizer, accuracy_op],
                                feed_dict={
                                    train_x_input: batch_X,
                                    train_y_input: batch_y,
                                    learning_rate_placeholder: learning_rate
                                })
                            train_accuracy += acc
                            batch_counter += 1
                    # walidacja
                    accuracy, confusion_matrix = self.__validate(
                        accuracy_op, tf_session, train_x_input, train_y_input,
                        validation_files, validation_folder, batch_size, model,
                        merged, test_writer, counter)
                    # save train accuracy
                    train_accuracy = train_accuracy / batch_counter
                    train_accuracy_summary = tf.Summary(value=[
                        tf.Summary.Value(tag="train_accuracy",
                                         simple_value=train_accuracy),
                    ])
                    test_writer.add_summary(train_accuracy_summary, iteration)
                    if accuracy > 0.8:  # TODO można ustawić inny warunek
                        print("Zmiana współczuynnika uczenia")
                        learning_rate = self.__configuration.settings(
                        ).second_learning_rate()
                    accuracies.append(accuracy)
                    counter = counter + 1
                # self.test_confustion_matrix(tf_session, validation_files, validation_folder, correct_predictions, model) # TODO zastanowić się, czy to correct_predictions tutaj powinno zostac przekazane
                self.__save_results(accuracies, output_model_folder, saver,
                                    tf_session)
                test_writer.close()
    # Sample random test data
    _, _, X_test, Y_test = get_data(dataset)
    num_samples = np.shape(X_test)[0]
    num_rand_samples = 1328
    random_samples = np.random.randint(0, num_samples, num_rand_samples)
    new_X_test = X_test[random_samples, :, :, :]
    new_Y_test = Y_test[random_samples, :]

    f = open(os.path.join(args.log_dir, 'Random_Test_%s_.p' % (dataset)), 'w')
    pickle.dump({"adv_input": new_X_test, "adv_labels": new_Y_test}, f)
    f.close()
    if (args.attack == 'spsa' or args.attack == 'all'):
        pytorch_network = Net()
        pytorch_network.load_state_dict(torch.load(args_ckpt))
        pytorch_network.eval()
        model = Model(torch_model=pytorch_network)
        keras_network = model.model
        transfer.pytorch_to_keras(pytorch_network, model.model)
        pytorch_network.eval()
        model = model.model
        model_logits = model
        batch_size = 16
        craft_one_type(sess,
                       model,
                       new_X_test,
                       new_Y_test,
                       dataset,
                       'spsa',
                       batch_size,
                       log_path=args.log_dir,
                       fp_path=args.fingerprint_dir)