Ejemplo n.º 1
0
 def __init__(self, batch_shape, sess, x_input, predicted_labels, gradients, output_dir):
     FileSystemEventHandler.__init__(self)
     self._batch_shape = batch_shape
     self._sess = sess
     self._x_input = x_input
     self._predicted_labels = predicted_labels
     self._gradients = gradients
     self._output_dir = output_dir
     self._category_helper = CategoryHelper("category/categories.csv")
Ejemplo n.º 2
0
 def __init__(self, batch_size, input_dir, net1, net4, output_dir, no_gpu):
     FileSystemEventHandler.__init__(self)
     self._batch_size = batch_size
     self._input_dir = input_dir
     self._net1 = net1
     self._net4 = net4
     self._output_dir = output_dir
     self._no_gpu = no_gpu
     self._category_helper = CategoryHelper("category/categories.csv")
Ejemplo n.º 3
0
 def __init__(self, batch_shape, sess, end_points, x_input,
              img_resize_tensor, shape_tensor, output_dir, itr, img_resize):
     FileSystemEventHandler.__init__(self)
     self._batch_shape = batch_shape
     self._sess = sess
     self._end_points = end_points
     self._x_input = x_input
     self._img_resize_tensor = img_resize_tensor
     self._shape_tensor = shape_tensor
     self._output_dir = output_dir
     self._itr = itr
     self._img_resize = img_resize
     self._category_helper = CategoryHelper("category/categories.csv")
Ejemplo n.º 4
0
 def __init__(self, batch_size, input_dir, net1, net4, model, itr,
              output_dir, no_gpu, kmean):
     FileSystemEventHandler.__init__(self)
     self._batch_size = batch_size
     self._input_dir = input_dir
     self._net1 = net1
     self._net4 = net4
     self._model = model
     self._itr = itr
     self._output_dir = output_dir
     self._no_gpu = no_gpu
     self._kmean = kmean
     self._category_helper = CategoryHelper("category/categories.csv")
Ejemplo n.º 5
0
class FileEventHandler(FileSystemEventHandler):
    def __init__(self, batch_size, input_dir, net1, net4, output_dir, no_gpu):
        FileSystemEventHandler.__init__(self)
        self._batch_size = batch_size
        self._input_dir = input_dir
        self._net1 = net1
        self._net4 = net4
        self._output_dir = output_dir
        self._no_gpu = no_gpu
        self._category_helper = CategoryHelper("category/categories.csv")

    def on_moved(self, event):
        if event.is_directory:
            print("directory moved from {0} to {1}".format(
                event.src_path, event.dest_path))
        else:
            print("file moved from {0} to {1}".format(event.src_path,
                                                      event.dest_path))

    def _defense_for_img_created(self, imgfile):
        """ defense one image: xxx.png,
            write res to xxx.txt with two line(lable human_string),
            copy the src image file to output dir then delete it
        :param img_file:
        :return None:
        """
        start_time = time.time()

        tf = transforms.Compose(
            [transforms.Resize([299, 299]),
             transforms.ToTensor()])
        with torch.no_grad():
            mean_torch = autograd.Variable(
                torch.from_numpy(
                    np.array([0.485, 0.456,
                              0.406]).reshape([1, 3, 1,
                                               1]).astype('float32')).cuda())
            std_torch = autograd.Variable(
                torch.from_numpy(
                    np.array([0.229, 0.224,
                              0.225]).reshape([1, 3, 1,
                                               1]).astype('float32')).cuda())
            mean_tf = autograd.Variable(
                torch.from_numpy(
                    np.array([0.5, 0.5,
                              0.5]).reshape([1, 3, 1,
                                             1]).astype('float32')).cuda())
            std_tf = autograd.Variable(
                torch.from_numpy(
                    np.array([0.5, 0.5,
                              0.5]).reshape([1, 3, 1,
                                             1]).astype('float32')).cuda())
            dataset = Dataset2(imgfile, transform=tf)
            loader = data.DataLoader(dataset,
                                     batch_size=self._batch_size,
                                     shuffle=False)
            net1 = self._net1
            net4 = self._net4

        outputs = []
        for batch_idx, input in enumerate(loader):
            if not self._no_gpu:
                input = input.cuda()
            with torch.no_grad():
                input_var = autograd.Variable(input)
                input_tf = (input_var - mean_tf) / std_tf
                input_torch = (input_var - mean_torch) / std_torch

                labels1 = net1(input_torch, True)[-1]
                labels4 = net4(input_torch, True)[-1]
                labels = (labels1 + labels4).max(
                    1
                )[1] + 1  # argmax + offset to match Google's Tensorflow + Inception 1001 class ids
            outputs.append(labels.data.cpu().numpy())
        outputs = np.concatenate(outputs, axis=0)

        filenames = [imgfile]
        output_file_name = ""
        for filename, label in zip(filenames, outputs):
            res_file_name = os.path.basename(filename)[:-4] + '.txt'
            output_file_name = os.path.join(self._output_dir,
                                            os.path.basename(filename))
            print(output_file_name)
            print("res_file_name: " + res_file_name)
            with open(os.path.join(self._output_dir, res_file_name),
                      'w+') as res_file:
                res_file.write('{0}\n{1}\n'.format(
                    label, self._category_helper.get_category_name(label)))
                res_file.flush()
            if os.path.exists(output_file_name):
                os.remove(output_file_name)
            shutil.copy(filename, output_file_name)
            os.remove(filename)
        elapsed_time = time.time() - start_time
        print('elapsed time: {0:.0f} [s]'.format(elapsed_time))

    def on_created(self, event):
        if event.is_directory:
            print("directory created:{0}".format(event.src_path))
        else:
            print("file created:{0}".format(event.src_path))
            self._defense_for_img_created(event.src_path)

    def on_deleted(self, event):
        if event.is_directory:
            print("directory deleted:{0}".format(event.src_path))
        else:
            print("file deleted:{0}".format(event.src_path))

    def on_modified(self, event):
        if event.is_directory:
            print("directory modified:{0}".format(event.src_path))
        else:
            print("file modified:{0}".format(event.src_path))
Ejemplo n.º 6
0
class FileEventHandler(FileSystemEventHandler):
    def __init__(self, batch_size, input_dir, net1, net4, model, itr, output_dir, no_gpu):
        FileSystemEventHandler.__init__(self)
        self._batch_size = batch_size
        self._input_dir = input_dir
        self._net1 = net1
        self._net4 = net4
        self._model = model
        self._itr = itr
        self._output_dir = output_dir
        self._no_gpu = no_gpu
        self._category_helper = CategoryHelper("category/categories.csv")

    def on_moved(self, event):
        if event.is_directory:
            print("directory moved from {0} to {1}".format(event.src_path, event.dest_path))
        else:
            print("file moved from {0} to {1}".format(event.src_path, event.dest_path))

    def _defense_for_img_created(self, imgfile):
        """ defense one image: xxx.png,
            write res to xxx.txt with two line(lable human_string),
            copy the src image file to output dir then delete it
        :param img_file:
        :return None:
        """
        start_time = time.time()
        
        tf = transforms.Compose([
            transforms.Resize([299,299]),
            transforms.ToTensor()
        ])

        tf_shrink = transforms.Compose([
            transforms.ToPILImage(),
            transforms.Resize([299,299]),
            transforms.ToTensor()
        ]) 
        tf_flip = transforms.Compose([
            transforms.ToPILImage(),
            transforms.RandomHorizontalFlip(p=0.5),
            transforms.ToTensor()
        ]) 

        with torch.no_grad():
            mean_torch = autograd.Variable(torch.from_numpy(np.array([0.485, 0.456, 0.406]).reshape([1,3,1,1]).astype('float32')).cuda())
            std_torch = autograd.Variable(torch.from_numpy(np.array([0.229, 0.224, 0.225]).reshape([1,3,1,1]).astype('float32')).cuda())
            mean_tf = autograd.Variable(torch.from_numpy(np.array([0.5, 0.5, 0.5]).reshape([1,3,1,1]).astype('float32')).cuda())
            std_tf = autograd.Variable(torch.from_numpy(np.array([0.5, 0.5, 0.5]).reshape([1,3,1,1]).astype('float32')).cuda())
            dataset = Dataset2(imgfile, transform=tf)
            loader = data.DataLoader(dataset, batch_size=self._batch_size, shuffle=False)
            net1 = self._net1
            net4 = self._net4

        labels_denoise = {}
        labels_random = {}
        random_outputs = []
        denoise_outputs = []
        for batch_idx, input in enumerate(loader):
	        #cv2
            temp_numpy = input.data.numpy()
            temp_numpy = np.reshape(temp_numpy, (3, 299, 299))
            temp_numpy = np.moveaxis(temp_numpy, -1, 0)
            temp_numpy = np.moveaxis(temp_numpy, -1, 0)
            temp_numpy = cv2.bilateralFilter(temp_numpy, 5, 50, 50)
            temp_numpy = np.moveaxis(temp_numpy, -1, 0) 
            temp_numpy = np.reshape(temp_numpy, (1, 3, 299, 299))
            input2 = torch.from_numpy(temp_numpy)

	        # Random padding
            length_input, _, _, _ = input.size()
            iter_labels = np.zeros([length_input, 1001, self._itr])
            for j in range(self._itr):
                # random fliping
                input0 = batch_transform(input2, tf_flip, 299)
                # random resizing
                resize_shape_ = random.randint(310, 331)
                image_resize = 331
                tf_rand_resize = transforms.Compose([
                    transforms.ToPILImage(),
                    transforms.Resize([resize_shape_, resize_shape_]),
                    transforms.ToTensor()
                ]) 
                input1 = batch_transform(input0, tf_rand_resize, resize_shape_)

                # ramdom padding
                shape = [random.randint(0, image_resize - resize_shape_), random.randint(0, image_resize - resize_shape_), image_resize]
                # print(shape)
       
                new_input = padding_layer_iyswim(input1, shape, tf_shrink)
                #print(type(new_input))
                if not self._no_gpu:
                    new_input = new_input.cuda()
                with torch.no_grad():
                    input_var = autograd.Variable(new_input)
                    logits = self._model(input_var)
                    labels = logits.max(1)[1]
                    labels_index = labels.data.tolist() 
                    #print(len(labels_index))
                    iter_labels[range(len(iter_labels)), labels_index, j] = 1
            final_labels = np.sum(iter_labels, axis=-1)
            labels = np.argmax(final_labels, 1)
            print(labels)
            random_outputs.append(labels)   

            # Denoise
            if not self._no_gpu:
                input = input.cuda()
            with torch.no_grad():
                input_var = autograd.Variable(input)
                input_tf = (input_var-mean_tf)/std_tf
                input_torch = (input_var - mean_torch)/std_torch
        
                labels1 = net1(input_torch,True)[-1]
                # labels2 = net2(input_tf,True)[-1]
                # labels3 = net3(input_tf,True)[-1]
                labels4 = net4(input_torch,True)[-1]

                labels = (labels1+labels4).max(1)[1] + 1  # argmax + offset to match Google's Tensorflow + Inception 1001 class ids
            denoise_outputs.append(labels.data.cpu().numpy())
        
        denoise_outputs = np.concatenate(denoise_outputs, axis=0)
        random_outputs = np.concatenate(random_outputs, axis=0)

        filenames = [imgfile]
        # filenames = [ os.path.basename(ii) for ii in filenames ]
        labels_denoise.update(dict(zip(filenames, denoise_outputs)))
        labels_random.update(dict(zip(filenames, random_outputs)))
        
        # diff filtering
        print('diff filtering...')
        if (len(labels_denoise) == len(labels_random)):
            # initializing 
            final_labels = labels_denoise
            # Compare
            diff_index = [ii for ii in labels_denoise if labels_random[ii] != labels_denoise[ii]]
            if (len(diff_index) != 0):
                # print(diff_index)
                for index in diff_index:
                    final_labels[index] = 0
        else:
            print("Error: Number of labels returned by two defenses doesn't match")
            exit(-1)   

        output_file_name = ""
        for filename, label in final_labels.items():
            res_file_name = os.path.basename(filename)[:-4] + '.txt'
            output_file_name = os.path.join(self._output_dir, os.path.basename(filename))
            #print(output_file_name)
            #print("res_file_name: " + res_file_name)
            with open(os.path.join(self._output_dir, res_file_name), 'w+') as res_file:
                res_file.write('{0}\n{1}\n'.format(label,
                                                   self._category_helper.get_category_name(label)))
                res_file.flush()
            if os.path.exists(output_file_name):
                os.remove(output_file_name)
            shutil.copy(filename, output_file_name)
            os.remove(filename)     
        
        elapsed_time = time.time() - start_time
        print('elapsed time: {0:.1f} [s]'.format(elapsed_time))

    def on_created(self, event):
        if event.is_directory:
            print("directory created:{0}".format(event.src_path))
        else:
            print("file created:{0}".format(event.src_path))
            self._defense_for_img_created(event.src_path)

    def on_deleted(self, event):
        if event.is_directory:
            print("directory deleted:{0}".format(event.src_path))
        else:
            print("file deleted:{0}".format(event.src_path))

    def on_modified(self, event):
        if event.is_directory:
            print("directory modified:{0}".format(event.src_path))
        else:
            print("file modified:{0}".format(event.src_path))
Ejemplo n.º 7
0
class FileEventHandler(FileSystemEventHandler):
    def __init__(self, batch_shape, sess, end_points, x_input, img_resize_tensor, shape_tensor, output_dir, itr, img_resize, net1, net4):
        FileSystemEventHandler.__init__(self)
        self._batch_shape = batch_shape
        self._sess = sess
        self._end_points = end_points
        self._x_input = x_input
        self._img_resize_tensor = img_resize_tensor
        self._shape_tensor = shape_tensor
        self._output_dir = output_dir
        self._itr = itr
        self._img_resize = img_resize        
        self._net1 = net1   
        self._net4 = net4
        self._category_helper = CategoryHelper("category/categories.csv")

    def on_moved(self, event):
        if event.is_directory:
            print("directory moved from {0} to {1}".format(event.src_path, event.dest_path))
        else:
            print("file moved from {0} to {1}".format(event.src_path, event.dest_path))

    def _defense_for_img_created(self, img_file):
        """ defense one image: xxx.png,
            write res to xxx.txt with two line(lable human_string),
            copy the src image file to output dir then delete it
        :param img_file:
        :return None:
        """
        if img_file.endswith('.png'):
            start_time = time.time() 
            output_file_name = ""

            # denoise
            print('Classifying with denoise...')
            transf = transforms.Compose([
                transforms.Resize([299,299]),
                transforms.ToTensor()
            ])
            with torch.no_grad():
                mean_torch = autograd.Variable(torch.from_numpy(np.array([0.485, 0.456, 0.406]).reshape([1,3,1,1]).astype('float32')).cuda())
                std_torch = autograd.Variable(torch.from_numpy(np.array([0.229, 0.224, 0.225]).reshape([1,3,1,1]).astype('float32')).cuda())
                mean_tf = autograd.Variable(torch.from_numpy(np.array([0.5, 0.5, 0.5]).reshape([1,3,1,1]).astype('float32')).cuda())
                std_tf = autograd.Variable(torch.from_numpy(np.array([0.5, 0.5, 0.5]).reshape([1,3,1,1]).astype('float32')).cuda())
                dataset = Dataset2(img_file, transform=transf)
                loader = data.DataLoader(dataset, batch_size=self._batch_shape[0], shuffle=False)
                net1 = self._net1
                net4 = self._net4
            labels_denoise = {}
            outputs = []
            for batch_idx, input in enumerate(loader):
                input = input.cuda()
                with torch.no_grad():
                    input_var = autograd.Variable(input)
                    input_tf = (input_var-mean_tf)/std_tf
                    input_torch = (input_var - mean_torch)/std_torch
            
                    labels1 = net1(input_torch,True)[-1]
                    labels4 = net4(input_torch,True)[-1]
                    labels = (labels1+labels4).max(1)[1] + 1  # argmax + offset to match Google's Tensorflow + Inception 1001 class ids
                outputs.append(labels.data.cpu().numpy())
            outputs = np.concatenate(outputs, axis=0) 
            filenames = [os.path.basename(img_file)]
            labels_denoise.update(dict(zip(filenames, outputs))) 
            print(labels_denoise)
            
            #random padding
            print('Classifying with random padding...')
            labels_random = {}
            for filenames, images in load_one_image(img_file, self._batch_shape):
                final_preds = np.zeros([self._batch_shape[0], 1001, self._itr])
                for j in range(self._itr):
                    if np.random.randint(0, 2, size=1) == 1:
                        images = images[:, :, ::-1, :]
                    resize_shape_ = np.random.randint(310, 331)
                    pred, aux_pred = self._sess.run([self._end_points['Predictions'], self._end_points['AuxPredictions']],
                                                    feed_dict={self._x_input: images, self._img_resize_tensor: [resize_shape_]*2,
                                                               self._shape_tensor: np.array([random.randint(0, self._img_resize - resize_shape_), random.randint(0, self._img_resize - resize_shape_), self._img_resize])})
                    final_preds[..., j] = pred + 0.4 * aux_pred
                final_probs = np.sum(final_preds, axis=-1)
                random_labels = np.argmax(final_probs, 1)
                labels_random.update(dict(zip(filenames, random_labels)))
                print(labels_random)
                for filename, label in zip(filenames, random_labels):
                    res_file_name = os.path.basename(filename)[:-4] + '.txt'
                    output_file_name = os.path.join(self._output_dir, filename)
                    print("res_file_name: " + res_file_name)
                    with open(os.path.join(self._output_dir, res_file_name), 'w+') as res_file:
                        res_file.write('{0}\n{1}\n'.format(label,
                                                           self._category_helper.get_category_name(label)))
                        res_file.flush()

   


            elapsed_time = time.time() - start_time
            print('elapsed time: {0:.00f} [s]'.format(elapsed_time))
            if os.path.exists(output_file_name):
                os.remove(output_file_name)
            shutil.copy(img_file, output_file_name)
            os.remove(img_file)


    def on_created(self, event):
        if event.is_directory:
            print("directory created:{0}".format(event.src_path))
        else:
            print("file created:{0}".format(event.src_path))
            self._defense_for_img_created(event.src_path)

    def on_deleted(self, event):
        if event.is_directory:
            print("directory deleted:{0}".format(event.src_path))
        else:
            print("file deleted:{0}".format(event.src_path))

    def on_modified(self, event):
        if event.is_directory:
            print("directory modified:{0}".format(event.src_path))
        else:
            print("file modified:{0}".format(event.src_path))
Ejemplo n.º 8
0
class FileEventHandler(FileSystemEventHandler):
    def __init__(self, batch_shape, sess, x_input, predicted_labels, gradients,
                 output_dir):
        FileSystemEventHandler.__init__(self)
        self._batch_shape = batch_shape
        self._sess = sess
        self._x_input = x_input
        self._predicted_labels = predicted_labels
        self._gradients = gradients
        self._output_dir = output_dir
        self._category_helper = CategoryHelper("category/categories.csv")

    def on_moved(self, event):
        if event.is_directory:
            print("directory moved from {0} to {1}".format(
                event.src_path, event.dest_path))
        else:
            print("file moved from {0} to {1}".format(event.src_path,
                                                      event.dest_path))

    def _defense_for_img_created(self, img_file):
        """ defense one image: xxx.png,
            write res to xxx.txt with two line(lable human_string),
            copy the src image file to output dir then delete it
        :param img_file:
        :return None:
        """
        if img_file.endswith('.png'):
            output_file_name = ""
            for filenames, images in load_one_image(img_file,
                                                    self._batch_shape):
                #labels = self._sess.run(self._predicted_labels, feed_dict={self._x_input: images})
                a = self._sess.run(self._gradients,
                                   feed_dict={self._x_input: images})
                labels = self._sess.run(self._predicted_labels,
                                        {self._x_input: images - a[0]})

                for filename, label in zip(filenames, labels):
                    res_file_name = os.path.basename(filename)[:-4] + '.txt'
                    output_file_name = os.path.join(self._output_dir, filename)
                    print("res_file_name: " + res_file_name)
                    with open(os.path.join(self._output_dir, res_file_name),
                              'w+') as res_file:
                        res_file.write('{0}\n{1}\n'.format(
                            label,
                            self._category_helper.get_category_name(label)))
                        res_file.flush()
            if os.path.exists(output_file_name):
                os.remove(output_file_name)
            shutil.copy(img_file, output_file_name)
            os.remove(img_file)

    def on_created(self, event):
        if event.is_directory:
            print("directory created:{0}".format(event.src_path))
        else:
            print("file created:{0}".format(event.src_path))
            self._defense_for_img_created(event.src_path)

    def on_deleted(self, event):
        if event.is_directory:
            print("directory deleted:{0}".format(event.src_path))
        else:
            print("file deleted:{0}".format(event.src_path))

    def on_modified(self, event):
        if event.is_directory:
            print("directory modified:{0}".format(event.src_path))
        else:
            print("file modified:{0}".format(event.src_path))
Ejemplo n.º 9
0
class FileEventHandler(FileSystemEventHandler):
    def __init__(self, batch_shape, sess, end_points, x_input,
                 img_resize_tensor, shape_tensor, output_dir, itr, img_resize):
        FileSystemEventHandler.__init__(self)
        self._batch_shape = batch_shape
        self._sess = sess
        self._end_points = end_points
        self._x_input = x_input
        self._img_resize_tensor = img_resize_tensor
        self._shape_tensor = shape_tensor
        self._output_dir = output_dir
        self._itr = itr
        self._img_resize = img_resize
        self._category_helper = CategoryHelper("category/categories.csv")

    def on_moved(self, event):
        if event.is_directory:
            print("directory moved from {0} to {1}".format(
                event.src_path, event.dest_path))
        else:
            print("file moved from {0} to {1}".format(event.src_path,
                                                      event.dest_path))

    def _defense_for_img_created(self, img_file):
        """ defense one image: xxx.png,
            write res to xxx.txt with two line(lable human_string),
            copy the src image file to output dir then delete it
        :param img_file:
        :return None:
        """
        if img_file.endswith('.png'):
            output_file_name = ""
            for filenames, images in load_one_image(img_file,
                                                    self._batch_shape):
                start_time = time.time()
                final_preds = np.zeros([self._batch_shape[0], 1001, self._itr])
                for j in range(self._itr):
                    if np.random.randint(0, 2, size=1) == 1:
                        images = images[:, :, ::-1, :]
                    resize_shape_ = np.random.randint(310, 331)
                    pred, aux_pred = self._sess.run(
                        [
                            self._end_points['Predictions'],
                            self._end_points['AuxPredictions']
                        ],
                        feed_dict={
                            self._x_input:
                            images,
                            self._img_resize_tensor: [resize_shape_] * 2,
                            self._shape_tensor:
                            np.array([
                                random.randint(
                                    0, self._img_resize - resize_shape_),
                                random.randint(
                                    0, self._img_resize - resize_shape_),
                                self._img_resize
                            ])
                        })
                    final_preds[..., j] = pred + 0.4 * aux_pred
                final_probs = np.sum(final_preds, axis=-1)
                labels = np.argmax(final_probs, 1)
                for filename, label in zip(filenames, labels):
                    res_file_name = os.path.basename(filename)[:-4] + '.txt'
                    output_file_name = os.path.join(self._output_dir, filename)
                    print("res_file_name: " + res_file_name)
                    with open(os.path.join(self._output_dir, res_file_name),
                              'w+') as res_file:
                        res_file.write('{0}\n{1}\n'.format(
                            label,
                            self._category_helper.get_category_name(label)))
                        res_file.flush()
                elapsed_time = time.time() - start_time
                print('elapsed time: {0:.00f} [s]'.format(elapsed_time))
            if os.path.exists(output_file_name):
                os.remove(output_file_name)
            shutil.copy(img_file, output_file_name)
            os.remove(img_file)

    def on_created(self, event):
        if event.is_directory:
            print("directory created:{0}".format(event.src_path))
        else:
            print("file created:{0}".format(event.src_path))
            self._defense_for_img_created(event.src_path)

    def on_deleted(self, event):
        if event.is_directory:
            print("directory deleted:{0}".format(event.src_path))
        else:
            print("file deleted:{0}".format(event.src_path))

    def on_modified(self, event):
        if event.is_directory:
            print("directory modified:{0}".format(event.src_path))
        else:
            print("file modified:{0}".format(event.src_path))