Exemplo n.º 1
0
    def initialize(self, ctx):
        """First try to load torchscript else load eager mode state_dict based model"""

        properties = ctx.system_properties
        self.device = torch.device("cuda:" +
                                   str(properties.get("gpu_id")) if torch.cuda.
                                   is_available() else "cpu")
        model_dir = properties.get("model_dir")

        # Read model serialize/pt file
        model_pt_path = os.path.join(model_dir, "mnist_cnn.pt")
        # Read model definition file
        model_def_path = os.path.join(model_dir, "mnist.py")
        if not os.path.isfile(model_def_path):
            raise RuntimeError("Missing the model definition file")

        from mnist import Net
        state_dict = torch.load(model_pt_path, map_location=self.device)
        self.model = Net()
        self.model.load_state_dict(state_dict)
        self.model.to(self.device)
        self.model.eval()

        logger.debug(
            'Model file {0} loaded successfully'.format(model_pt_path))
        self.initialized = True
Exemplo n.º 2
0
 def __init__(self):
     super().__init__()
     enable_cuda = False
     if torch.cuda.is_available():
         enable_cuda = True
     self.model = Net(enable_cuda)
     if self.model.use_cuda:
         self.model = self.model.cuda()
     if os.path.exists('mnist_params.pkl'):
         self.model.load_state_dict(torch.load('mnist_params.pkl'))
     self.initUI()
Exemplo n.º 3
0
def main():
    model = Net(activation=torch.nn.LeakyReLU())
    use_cuda = True
    device = 'cuda'

    test_batch_size = 20
    kwargs = {'num_workers': 1, 'pin_memory': True} if use_cuda else {}
    test_loader = torch.utils.data.DataLoader(datasets.MNIST(
        '../data',
        train=True,
        transform=transforms.Compose([
            transforms.ToTensor(),
        ])),
                                              batch_size=test_batch_size,
                                              shuffle=True,
                                              **kwargs)
    PATH = 'mnist_cnn200_enc.pt'
    model.load_state_dict(torch.load(PATH, map_location=device))
    model = model.eval().to(device)
    with torch.no_grad():
        for data, target in test_loader:
            data, target = data.to(device), target.to(device)
            shape = data.shape
            x = data.reshape(shape[0], numpy.prod(shape[1:]))
            output = model(x)['output'].reshape(shape)
            cv2.imshow('source', data[3][0].cpu().numpy())
            cv2.imshow('target', output[3][0].cpu().numpy())
            output1 = model.decode(
                torch.normal(torch.zeros((100, 2)), torch.ones(
                    (100, 2))).to(data)).reshape((100, 28, 28))
            fig, axes = plt.subplots(10, 10)
            plt.figure(1)
            for i in range(100):
                img = output1[i].cpu().numpy()
                k = i // 10
                j = i % 10
                axes[k, j].imshow((img * 255).astype(numpy.int32),
                                  cmap='gray',
                                  vmin=0,
                                  vmax=255)
            plt.subplots_adjust(wspace=0, hspace=0)
            plt.show()
            q = cv2.waitKey()
            if q == 113:  # q
                return
Exemplo n.º 4
0
class MAIN_Window(QMainWindow):
    def __init__(self):
        super().__init__()
        enable_cuda = False
        if torch.cuda.is_available():
            enable_cuda = True
        self.model = Net(enable_cuda)
        if self.model.use_cuda:
            self.model = self.model.cuda()
        if os.path.exists('mnist_params.pkl'):
            self.model.load_state_dict(torch.load('mnist_params.pkl'))
        self.initUI()

    def initUI(self):
        self.setGeometry(200, 200, 550, 300)
        self.setWindowTitle('MNIST Recognition')
        self.draw_widget = DrawWidget()
        widget = QWidget()

        self.setCentralWidget(widget)
        layout = QGridLayout()
        layout.setSpacing(40)

        layout.addWidget(self.draw_widget, 0, 0)

        self.text_edit = QTextEdit()
        self.text_edit.setFocusPolicy(Qt.NoFocus)

        lb = QLabel("Predict: ")
        self.lbl = QLabel()

        self.createtoolbar()

        layout.addWidget(self.text_edit, 0, 1, 2, 1)

        splitter = QSplitter()
        layout.addWidget(splitter, 1, 0)
        splitter.addWidget(lb)
        splitter.addWidget(self.lbl)

        widget.setLayout(layout)
        self.setMinimumSize(300, 300)
        self.show()

    def createtoolbar(self):
        self.toolbar = QToolBar('Tool')

        self.style_label = QLabel('Line Style: ')
        self.style_combobox = QComboBox()
        self.style_combobox.addItem('Solid Line', Qt.SolidLine)
        self.style_combobox.addItem('DashLine', Qt.DashLine)
        self.style_combobox.addItem('DotLine', Qt.DotLine)
        self.style_combobox.addItem('DashDotLine', Qt.DashDotLine)
        self.style_combobox.addItem('DashDotDotLine', Qt.DashDotDotLine)
        self.style_combobox.currentIndexChanged.connect(self.draw_widget.set_style)

        self.widthLabel = QLabel('Line Width: ')
        self.width_spin_box = QSpinBox()
        self.width_spin_box.setValue(10)
        self.width_spin_box.valueChanged.connect(self.set_width)

        self.colorbtn = QToolButton()
        self.colorbtn.setText('Color')
        pixmap = QPixmap(20, 20)
        pixmap.fill(Qt.black)
        self.colorbtn.setIcon(QIcon(pixmap))
        self.colorbtn.clicked.connect(self.show_color)

        self.clearbtn = QToolButton()
        self.clearbtn.setText('Clear')
        self.clearbtn.clicked.connect(self.clear)

        self.calcbtn = QToolButton()
        self.calcbtn.setText('Calc')
        self.calcbtn.clicked.connect(self.calc)

        self.toolbar.addWidget(self.style_label)
        self.toolbar.addWidget(self.style_combobox)
        self.toolbar.addSeparator()
        self.toolbar.addWidget(self.widthLabel)
        self.toolbar.addWidget(self.width_spin_box)
        self.toolbar.addSeparator()
        self.toolbar.addWidget(self.colorbtn)
        self.toolbar.addSeparator()
        self.toolbar.addWidget(self.clearbtn)
        self.toolbar.addSeparator()
        self.toolbar.addWidget(self.calcbtn)

        self.addToolBar(self.toolbar)

    def show_color(self):
        color = QColorDialog.getColor(Qt.black, self, 'select color')
        if color.isValid():
            self.draw_widget.set_color(color)
            pixmap = QPixmap(20, 20)
            pixmap.fill(color)
            self.colorbtn.setIcon(QIcon(pixmap))

    def clear(self):
        self.lbl.setText('')
        self.text_edit.setText('')
        self.draw_widget.clear()

    def set_width(self, w):
        self.draw_widget.set_width(w)

    # reference : https://zhuanlan.zhihu.com/p/30120447
    def judge_edge(self, img, length, flag, val=0):
        size = [-1, -1]
        for i in range(length):
            if flag == 'row':
                line1 = img[i, img[i, :] > val]
                line2 = img[length - 1 - i, img[length - 1 - i, :] > val]
            else:
                line1 = img[img[:, i] > val, i]
                line2 = img[img[:, length - i - 1] > val, length - 1 - i]
            if len(line1) > 0 and size[0] == -1:
                size[0] = i
            if len(line2) > 0 and size[1] == -1:
                size[1] = length - i - 1
            if size[0] != -1 and size[1] != -1:
                return size
        return [0, length]

    def crop(self, img):
        height = img.shape[0]
        width = img.shape[1]
        size = []
        size.append(self.judge_edge(img, height, 'row', val=0))
        size.append(self.judge_edge(img, width, 'column', val=0))
        return img[max(size[0][0]-10, 0):min(size[0][1] + 10, height),
               max(size[1][0]-10, 0):min(size[1][1] + 10, width)]

    def calc(self):
        self.draw_widget.pix.save('test.jpg', 'JPG')
        self.text_edit.setText('')

        img = Image.open('test.jpg').convert('L')
        img = -1 * ((np.array(img) / 255.) * 2 - 1)
        img = self.crop(img)
        img = Image.fromarray(img).resize((28, 28))
        img = np.array(img).reshape((1, 1, 28, 28))

        # plt.imshow(img.reshape(28, 28), cmap='gray')
        # plt.show()

        outut, pred = evaluate(self.model, img)
        self.lbl.setText(str(pred))
        outlist = sorted(outut.items(), key=lambda item: item[1], reverse=True)
        for i in range(len(outlist)):
            self.text_edit.append('{}------{:.4f}'.format(outlist[i][0], outlist[i][1]))
    './data',
    train=False,
    download=True,
    transform=transforms.Compose([
        transforms.ToTensor(),
    ])),
                                          batch_size=1,
                                          shuffle=True)

# 定义我们正在使用的设备
print("CUDA Available: ", torch.cuda.is_available())
device = torch.device("cuda" if (
    use_cuda and torch.cuda.is_available()) else "cpu")

# 初始化网络
model = Net().to(device)

# 加载已经预训练的模型
model.load_state_dict(torch.load(pretrained_model, map_location='cpu'))

# 在评估模式下设置模型。在这种情况下,这适用于Dropout图层
model.eval()


# FGSM算法攻击代码
def fgsm_attack(image, epsilon, data_grad):
    # 收集数据梯度的元素符号
    sign_data_grad = data_grad.sign()
    # 通过调整输入图像的每个像素来创建扰动图像
    perturbed_image = image + epsilon * sign_data_grad
    # 添加剪切以维持[0,1]范围
Exemplo n.º 6
0
    )
    stream_iterator = iter(stream)
    while stream_iterator:
        try:
            line = next(stream_iterator)
            out_file.write(line)
        except StopIteration:
            break
    output_file.close()


if __name__ == "__main__":
    # Session initialization
    session = get(count=1, expected="", successes=0)
    with st.spinner("Loading neural network..."):
        mnist = Net()
        mnist.load_state_dict(
            torch.load("models/mnist_cnn.pt", map_location="cpu"))

    # Read text file
    input_file = open("texts.json", "r")
    output_file = open("texts_unicode.json", "w")
    fix_file_encoding(input_file, output_file)
    with open("texts_unicode.json") as json_file:
        texts = json.load(json_file)
    languages = texts["languages"]

    # language choice
    language = st.sidebar.radio(" ", list(languages.keys()))
    text = texts[languages[language]]
Exemplo n.º 7
0
class MNISTDigitClassifier(object):
    """
    MNISTDigitClassifier handler class. This handler takes a greyscale image
    and returns the digit in that image.
    """
    def __init__(self):
        self.model = None
        self.mapping = None
        self.device = None
        self.initialized = False

    def initialize(self, ctx):
        """First try to load torchscript else load eager mode state_dict based model"""

        properties = ctx.system_properties
        self.device = torch.device("cuda:" +
                                   str(properties.get("gpu_id")) if torch.cuda.
                                   is_available() else "cpu")
        model_dir = properties.get("model_dir")

        # Read model serialize/pt file
        model_pt_path = os.path.join(model_dir, "mnist_cnn.pt")
        # Read model definition file
        model_def_path = os.path.join(model_dir, "mnist.py")
        if not os.path.isfile(model_def_path):
            raise RuntimeError("Missing the model definition file")

        from mnist import Net
        state_dict = torch.load(model_pt_path, map_location=self.device)
        self.model = Net()
        self.model.load_state_dict(state_dict)
        self.model.to(self.device)
        self.model.eval()

        logger.debug(
            'Model file {0} loaded successfully'.format(model_pt_path))
        self.initialized = True

    def preprocess(self, data):
        """
         Scales, crops, and normalizes a PIL image for a MNIST model,
         returns an Numpy array
        """
        image = data[0].get("data")
        if image is None:
            image = data[0].get("body")

        mnist_transform = transforms.Compose([
            transforms.ToTensor(),
            transforms.Normalize((0.1307, ), (0.3081, ))
        ])
        image = Image.open(io.BytesIO(image))
        image = mnist_transform(image)
        return image

    def inference(self, img, topk=5):
        ''' Predict the class (or classes) of an image using a trained deep learning model.
        '''
        # Convert 2D image to 1D vector
        img = np.expand_dims(img, 0)
        img = torch.from_numpy(img)

        self.model.eval()
        inputs = Variable(img).to(self.device)
        outputs = self.model.forward(inputs)

        _, y_hat = outputs.max(1)
        predicted_idx = str(y_hat.item())
        return [predicted_idx]

    def postprocess(self, inference_output):
        return inference_output
Exemplo n.º 8
0
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
plt.ioff()
import numpy as np
import torch
from torchvision import datasets, transforms
from mnist import Net
from scipy.optimize import minimize
from test_utils import cplx_imshow
import ipdb

PATH = '/home/jk/matt/mnist_cnn.pt'
img_side = 28
#TODO CUDA
my_net = Net(img_side).double().cuda()
print('Loading model')
my_net.load_state_dict(torch.load(PATH))
my_net.eval()
batch_size = 64
max_iter = 32
mu = 0.1307
sigma = 0.3801
dl = torch.utils.data.DataLoader(datasets.MNIST('../data',
                                                train=False,
                                                download=False,
                                                transform=transforms.Compose([
                                                    transforms.ToTensor(),
                                                    transforms.Normalize(
                                                        (mu, ), (sigma, ))
                                                ])),
def main():
    # Training settings
    parser = argparse.ArgumentParser(description='PyTorch MNIST Example')
    parser.add_argument('--batch-size',
                        type=int,
                        default=64,
                        metavar='N',
                        help='input batch size for training (default: 64)')
    parser.add_argument('--test-batch-size',
                        type=int,
                        default=1000,
                        metavar='N',
                        help='input batch size for testing (default: 1000)')
    parser.add_argument('--epochs',
                        type=int,
                        default=2,
                        metavar='N',
                        help='number of epochs to train (default: 10)')
    parser.add_argument('--lr',
                        type=float,
                        default=0.01,
                        metavar='LR',
                        help='learning rate (default: 0.01)')
    parser.add_argument('--momentum',
                        type=float,
                        default=0.5,
                        metavar='M',
                        help='SGD momentum (default: 0.5)')
    parser.add_argument('--no-cuda',
                        action='store_true',
                        default=False,
                        help='disables CUDA training')
    parser.add_argument('--seed',
                        type=int,
                        default=1,
                        metavar='S',
                        help='random seed (default: 1)')
    parser.add_argument(
        '--log-interval',
        type=int,
        default=10,
        metavar='N',
        help='how many batches to wait before logging training status')

    parser.add_argument('--save-model',
                        action='store_true',
                        default=True,
                        help='For Saving the current Model')
    args = parser.parse_args()

    torch.manual_seed(args.seed)

    use_cuda = not args.no_cuda and torch.cuda.is_available()
    device = torch.device("cuda" if use_cuda else "cpu")
    kwargs = {'num_workers': 1, 'pin_memory': True} if use_cuda else {}

    tf = transforms.Compose([
        transforms.ToTensor(),
        # transforms.Normalize((0.1307,), (0.3081,))
    ])
    test_loader = torch.utils.data.DataLoader(datasets.MNIST('../data',
                                                             train=False,
                                                             transform=tf),
                                              batch_size=args.test_batch_size,
                                              shuffle=True,
                                              **kwargs)

    model = Net().to(device)
    model.load_state_dict(torch.load("mnist_cnn.pt"))

    save_image(test_loader, device, "images")
Exemplo n.º 10
0
def main():
    # Training settings
    parser = argparse.ArgumentParser(description='PyTorch MNIST Example')
    parser.add_argument('--batch-size',
                        type=int,
                        default=64,
                        metavar='N',
                        help='input batch size for training (default: 64)')
    parser.add_argument('--test-batch-size',
                        type=int,
                        default=1000,
                        metavar='N',
                        help='input batch size for testing (default: 1000)')
    parser.add_argument('--epochs',
                        type=int,
                        default=10,
                        metavar='N',
                        help='number of epochs to train (default: 10)')
    parser.add_argument('--lr',
                        type=float,
                        default=0.01,
                        metavar='LR',
                        help='learning rate (default: 0.01)')
    parser.add_argument('--momentum',
                        type=float,
                        default=0.5,
                        metavar='M',
                        help='SGD momentum (default: 0.5)')
    parser.add_argument('--no-cuda',
                        action='store_true',
                        default=False,
                        help='disables CUDA training')
    parser.add_argument('--seed',
                        type=int,
                        default=1,
                        metavar='S',
                        help='random seed (default: 1)')
    parser.add_argument(
        '--log-interval',
        type=int,
        default=10,
        metavar='N',
        help='how many batches to wait before logging training status')
    args = parser.parse_args()
    use_cuda = not args.no_cuda and torch.cuda.is_available()

    torch.manual_seed(args.seed)
    device = torch.device("cuda" if use_cuda else "cpu")
    kwargs = {'num_workers': 1, 'pin_memory': True} if use_cuda else {}

    train_loader = torch.utils.data.DataLoader(datasets.MNIST(
        '../data',
        train=True,
        download=True,
        transform=transforms.Compose([
            transforms.ToTensor(),
            transforms.Normalize((0.1307, ), (0.3081, ))
        ])),
                                               batch_size=args.batch_size,
                                               shuffle=True,
                                               **kwargs)
    test_loader = torch.utils.data.DataLoader(datasets.MNIST(
        '../data',
        train=False,
        transform=transforms.Compose([
            transforms.ToTensor(),
            transforms.Normalize((0.1307, ), (0.3081, ))
        ])),
                                              batch_size=args.test_batch_size,
                                              shuffle=True,
                                              **kwargs)

    model = Net().to(device)
    optimizer = optim.SGD(model.parameters(),
                          lr=args.lr,
                          momentum=args.momentum)
    for i in range(1, args.epochs + 1):
        train(args, model, device, train_loader, optimizer, epoch=i)
        test(args, model, device, test_loader)
Exemplo n.º 11
0
                        help="Number of images to save")
    parser.add_argument("--seed", type=int, default=0, help="Random seed")
    parser.add_argument("--model",
                        type=str,
                        default="data/mnist_cnn.pt",
                        help="Model to attack")

    return parser.parse_args()


if __name__ == "__main__":
    args = get_args()
    print("EPSILON", args.epsilon)

    # load pre-trained model
    model = Net()
    model.load_state_dict(torch.load(args.model, map_location="cpu"))

    # set model to evaluation mode
    model.eval()

    loader = data.mnist(batch_size=1, seed=args.seed)

    n_success = 0
    success_rate = 0.0
    t_count = 0
    t_queries = []
    images_saved = 0

    if args.blackbox:
        attack = ES(n_iter=args.n_iter, epsilon=args.epsilon)
def find_best_input(model, target_class, num_loops=50, lr=0.5):
    img = np.uint8(np.zeros((1, 28, 28)))
    for i in range(num_loops):
        tensor = img_to_tensor(img)
        optimizer = Adam([tensor], lr=lr)
        output = model(tensor)
        # The model output uses log_softmax, so the output tends towards negative numbers where the smallest one is the best prediction.
        # We want to maximize the target class, so we multiply it by -10, so the goal is to make it as small as possible and ideally 0
        # Furthermore, we add the other negative non-targets, so the goal is to make them more negative -> more unlikely
        loss = -10 * output[0, target_class] + torch.sum(output)
        print('Target Class: {0}, Iteration: {1}, Loss: {2:.2f}'.format(
            str(target_class), str(i), loss.data.numpy()))
        model.zero_grad()
        loss.backward()
        optimizer.step()
        img = tensor_to_img(tensor)
        img_path = './generated/img_{}_iteration_{}.jpg'.format(
            str(target_class), str(i))
        save_image(img, img_path)


if __name__ == '__main__':
    model = Net()
    model.load_state_dict(torch.load('./mnist_cnn.pt'))
    model.eval()  # Don't change model weights
    if not os.path.exists('./generated'):
        os.makedirs('./generated')
    for target_class in range(10):
        find_best_input(model, target_class, num_loops=50)