Ejemplo n.º 1
0
def predict(model_path, im_path):
    '''
    Test procedure
    ---------------
    :param model_path: path of the saved model
    :param im_path: path of an image
    '''

    # TODO 3: load configurations from saved model, initialize the model.
    # Note: you can complete this section by referring to Part 4: test.

    # step 1: load configurations from saved model using torch.load(model_path)
    # and get the configs dictionary, configs = checkpoint['configs'],
    # then get each config from configs, eg., norm_size = configs['norm_size']
    checkpoint = torch.load(model_path)
    configs = checkpoint['configs']
    norm_size = configs['norm_size']
    output_size = configs['output_size']
    hidden_size = configs['hidden_size']
    n_layers = configs['n_layers']
    act_type = configs['act_type']

    # step 2: initialize the model by MLP()
    model = MLP(norm_size[0] * norm_size[1], output_size, hidden_size,
                n_layers, act_type)
    # step 3: load model parameters we saved in model_path
    # hint: similar to what we do in Part 4: test.
    model.load_state_dict(checkpoint['state_dict'])
    # End TODO 3
    # enter the evaluation mode
    model.eval()

    # image pre-processing, similar to what we do in ListDataset()
    transform = transforms.Compose([
        transforms.ToPILImage(),
        transforms.Resize(norm_size),
        transforms.ToTensor()
    ])

    # image pre-processing, similar to what we do in ListDataset()
    im = cv2.imread(im_path)
    im = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
    im = transform(im)
    im.sub_(0.5).div_(0.5)

    # input im into the model
    with torch.no_grad():
        input = im.view(1, -1)
        out = model(input)
        prediction = out.argmax(1)[0].item()

    # convert index of prediction to the corresponding character
    letters = string.ascii_letters[-26:]  # ABCD...XYZ
    prediction = letters[prediction]

    print('Prediction: {}'.format(prediction))
Ejemplo n.º 2
0
def main():
	parser = argparse.ArgumentParser(description='Pytorch example: CIFAR-10')
	parser.add_argument('--gpu', '-g', type=int, default=-1,
						help='GPU ID (negative value indicates CPU)')
	parser.add_argument('--model', '-m', default='result/model_final',
						help='Path to the model for test')
	parser.add_argument('--image', '-i', default='image.png',
						help='Path to the model for test')
	parser.add_argument('--unit', '-u', type=int, default=1000,
						help='Number of units')
	args = parser.parse_args()

	print('GPU: {}'.format(args.gpu))
	print('')

	# Set up a neural network to test
	net = MLP(args.unit, 28*28, 10)
	# Load designated network weight 
	net.load_state_dict(torch.load(args.model))
	# Set model to GPU
	device = 'cpu'
	if args.gpu >= 0:
		# Make a specified GPU current
		device = 'cuda:' + str(args.gpu)
		net = net.to(device)

	# Load image
	transform = transforms.Compose(	[
		transforms.Grayscale(),
		transforms.Resize((28, 28)),
		transforms.ToTensor()] )
	image = transform(Image.open(args.image, 'r')).unsqueeze(0).to(device)

	with torch.no_grad():
		# Reshape the input
		image = image.view(-1, 28*28)
		if args.gpu >= 0:
			image = image.to(device)
		# Forward
		outputs = net(image)
		# Predict the label
# 		_, predicted = torch.max(outputs, 1)
		_, predicted = torch.topk(outputs, 3)
		# Print the result
		print('Predicted label : {0}, {1}, {2}'.format(predicted[0][0].tolist(),predicted[0][1].tolist(),predicted[0][2].tolist()))
Ejemplo n.º 3
0
print("Loaded Network to GPU ... ")

optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)

if not ft.dir_exist(
        os.path.join(proj_path, "checkpoint", "MLPTwoStep{}".format(option))):
    os.makedirs(
        os.path.join(proj_path, "checkpoint", "MLPTwoStep{}".format(option)))

# if having epoch saved, then load already trained model
if not start_epoch == 0:
    # load the existing model and resume training
    checkpoint = torch.load(
        os.path.join(proj_path, "checkpoint", "MLPTwoStep{}".format(option),
                     "cp_{:03d}.pth".format(start_epoch)))
    net.load_state_dict(checkpoint['model_state_dict'])
    optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
    # load the existing training result
    acc_val_ex, acc_train_ex, loss_val_ex, loss_train_ex = load_train_result(
        os.path.join(proj_path, "checkpoint", "MLPTwoStep{}".format(option),
                     "training_result_{:03d}.npz".format(start_epoch)))
    acc_val[:start_epoch] = acc_val_ex[:start_epoch]
    acc_train[:start_epoch] = acc_train_ex[:start_epoch]
    loss_val[:start_epoch] = loss_val_ex[:start_epoch]
    loss_train[:start_epoch] = loss_train_ex[:start_epoch]
    print("Loaded existing model check point ...")

# train the network
start_t = time.time()

print_freq = 20
Ejemplo n.º 4
0
def test(model_path,
         im_dir='data/character_classification/images',
         test_file_path='data/character_classification/test.json',
         batch_size=8,
         device='cpu'):
    '''
    Test procedure
    ---------------
    :param model_path: path of the saved model
    :param im_dir: path to directory with images
    :param test_file_path: file with test image paths and labels
    :param batch_size: test batch size
    :param device: 'cpu' or 'cuda'
    '''

    # load configurations from saved model, initialize and test the model
    checkpoint = torch.load(model_path)
    configs = checkpoint['configs']
    norm_size = configs['norm_size']
    output_size = configs['output_size']
    hidden_size = configs['hidden_size']
    n_layers = configs['n_layers']
    act_type = configs['act_type']

    # initialize the model by MLP()
    model = MLP(norm_size[0] * norm_size[1], output_size, hidden_size,
                n_layers, act_type)

    # load model parameters we saved in model_path
    model.load_state_dict(checkpoint['state_dict'])
    model = model.to(device)
    print('[Info] Load model from {}'.format(model_path))

    # enter the evaluation mode
    model.eval()

    # test loader
    testloader = dataLoader(im_dir, test_file_path, norm_size, batch_size)

    # run the test process
    n_correct = 0.
    n_ims = 0.

    logits = []
    all_labels = []

    with torch.no_grad(
    ):  # we do not need to compute gradients during test stage

        for ims, labels in testloader:
            ims, labels = ims.to(device), labels.type(torch.float).to(device)
            input = ims.view(ims.size(0), -1)
            out = model(input)
            predictions = out.argmax(1)
            n_correct += torch.sum(predictions == labels)
            n_ims += ims.size(0)

            logits.append(out)
            all_labels.append(labels)

        logits = torch.cat(logits, dim=0).detach().cpu().numpy()
        all_labels = torch.cat(all_labels, dim=0).cpu().numpy()

        tsne = TSNE(n_components=2, init='pca')
        Y = tsne.fit_transform(logits)

        letters = list(string.ascii_letters[-26:])
        Y = (Y - Y.min(0)) / (Y.max(0) - Y.min(0))
        for i in range(len(all_labels)):
            if (all_labels[i] < 26):
                c = plt.cm.rainbow(float(all_labels[i]) / 26)
                plt.text(Y[i, 0],
                         Y[i, 1],
                         s=letters[int(all_labels[i])],
                         color=c)
        plt.show()

    print('[Info] Test accuracy = {:.1f}%'.format(100 * n_correct / n_ims))
Ejemplo n.º 5
0
def main():
	parser = argparse.ArgumentParser(description='Pytorch example: MNIST')
	parser.add_argument('--batchsize', '-b', type=int, default=100,
						help='Number of images in each mini-batch')
	parser.add_argument('--epoch', '-e', type=int, default=20,
						help='Number of sweeps over the training data')
	parser.add_argument('--frequency', '-f', type=int, default=-1,
						help='Frequency of taking a snapshot')
	parser.add_argument('--gpu', '-g', type=int, default=-1,
						help='GPU ID (negative value indicates CPU)')
	parser.add_argument('--out', '-o', default='result',
						help='Directory to output the result')
	parser.add_argument('--resume', '-r', default='',
						help='Resume the training from snapshot')
	parser.add_argument('--unit', '-u', type=int, default=1000,
						help='Number of units')
	args = parser.parse_args()

	print('GPU: {}'.format(args.gpu))
	print('# unit: {}'.format(args.unit))
	print('# Minibatch-size: {}'.format(args.batchsize))
	print('# epoch: {}'.format(args.epoch))
	print('')

	# Set up a neural network to train
	net = MLP(args.unit, 28*28, 10)
	# Load designated network weight 
	if args.resume:
		net.load_state_dict(torch.load(args.resume))
	# Set model to GPU
	if args.gpu >= 0:
		# Make a specified GPU current
		device = 'cuda:' + str(args.gpu)
		net = net.to(device)

	# Setup a loss and an optimizer
	criterion = nn.CrossEntropyLoss()
	optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)

	# Load the MNIST

	transform = transforms.Compose(	[transforms.ToTensor()] )

	trainvalset = datasets.MNIST(root='./data', train=True,
										download=True, transform=transform)
	# Split train/val
	n_samples = len(trainvalset)
	trainsize = int(n_samples * 0.9)
	valsize = n_samples - trainsize
	trainset, valset = torch.utils.data.random_split(trainvalset, [trainsize, valsize])

	trainloader = torch.utils.data.DataLoader(trainset, batch_size=args.batchsize,
										shuffle=True, num_workers=2)
	valloader = torch.utils.data.DataLoader(valset, batch_size=args.batchsize,
										shuffle=True, num_workers=2)
	# Setup result holder
	x = []
	ac_train = []
	ac_val = []
	# Train
	for ep in range(args.epoch):  # Loop over the dataset multiple times

		running_loss = 0.0
		correct_train = 0
		total_train = 0
		correct_val = 0
		total_val = 0

		for i, data in enumerate(trainloader, 0):
			# Get the inputs; data is a list of [inputs, labels]
			inputs, labels = data
			if args.gpu >= 0:
				inputs = inputs.to(device)
				labels = labels.to(device)
			# Reshape the input 
			inputs = inputs.view(-1, 28*28)
			# Reset the parameter gradients
			optimizer.zero_grad()

			# Forward
			outputs = net(inputs)
			# Predict the label
			_, predicted = torch.max(outputs, 1)
			# Check whether estimation is right
			c = (predicted == labels).squeeze()
			for i in range(len(predicted)):
				correct_train += c[i].item()
				total_train += 1
			# Backward + Optimize
			loss = criterion(outputs, labels)
			loss.backward()
			optimizer.step()
			# Add loss
			running_loss += loss.item()

		# Report loss of the epoch
		print('[epoch %d] loss: %.3f' % (ep + 1, running_loss))

		# Save the model
		if (ep + 1) % args.frequency == 0:
			path = args.out + "/model_" + str(ep + 1)
			torch.save(net.state_dict(), path)

		# Validation
		with torch.no_grad():
			for data in valloader:
				images, labels = data
				if args.gpu >= 0:
					images = images.to(device)
					labels = labels.to(device)
				# Reshape the input
				images = images.view(-1, 28*28)
				# Forward
				outputs = net(images)
				# Predict the label
				_, predicted = torch.max(outputs, 1)
				# Check whether estimation is right
				c = (predicted == labels).squeeze()
				for i in range(len(predicted)):
					correct_val += c[i].item()
					total_val += 1

		# Record result
		x.append(ep+1)
		ac_train.append(100 * correct_train / total_train)
		ac_val.append(100 * correct_val / total_val)

	print('Finished Training')
	path = args.out + "/model_final"
	torch.save(net.state_dict(), path)

	# Draw graph
	fig = plt.figure()
	ax = fig.add_subplot(1, 1, 1)
	ax.plot(x, ac_train, label='Training')
	ax.plot(x, ac_val, label='Validation')
	ax.legend()
	ax.set_xlabel("Epoch")
	ax.set_ylabel("Accuracy [%]")
	ax.set_ylim(80, 100)

	plt.savefig(args.out + '/accuracy_mnist_mlp.png')