Beispiel #1
0
def traindata(device):
	net=nn.netfunction()
	criterion=nn.criterionfunction()
	optimizer=nn.optimizerfunction()
	trainloader=encode.trainloader
	for epoch in range(3):  # loop over the dataset multiple times

		running_loss = 0.0
		for i, data in enumerate(trainloader, 0):
			# get the inputs
			inputs, labels = data
			#inputs, labels = inputs.to(device), labels.to(device)

			# zero the parameter gradients
			optimizer.zero_grad()

			# forward + backward + optimize
			outputs = net.forward(inputs)
			loss = criterion(outputs, labels)
			loss.backward()
			optimizer.step()

			# print statistics
			running_loss += loss.item()
			if i % 200 == 199:    # print every 200 mini-batches
				print('[%d, %5d] loss: %.3f' %
					(epoch + 1, i + 1, running_loss / 200))
				running_loss = 0.0
				
	print('Finished Training')
	return net
Beispiel #2
0
def getforwardconvlayer():
    dataiter = iter(encode.testloader)
    images, labels = dataiter.next()
    net = neuralnet.netfunction()
    new_net = nn.Sequential(*list(net.features.children())[:-2])
    outputs = new_net(images)
    #print(outputs[0])
    features = outputs[3][5].data.numpy()
    #print(img.size())
    features = features / 2 + 0.5  # convert back to image and unnormalize
    plotfeatures(images[3], outputs[3])
Beispiel #3
0
def decode():
    # get some random training images
    dataiter = iter(encode.trainloader)
    images, labels = dataiter.next()

    # print labels
    print('Actual: ',
          ' '.join('%5s' % encode.classes[labels[j]] for j in range(4)))

    #print classifications
    net = neuralnet.netfunction()
    outputs = net(images)
    _, predicted = torch.max(outputs, 1)
    print('Predicted: ',
          ' '.join('%5s' % encode.classes[predicted[j]] for j in range(4)))

    # show images
    imshow(torchvision.utils.make_grid(images))
Beispiel #4
0
def dd_helper(image, layer, iterations, lr):
    net = neuralnet.netfunction()
    modulelist = list(net.features.modules())
    input = Variable(encode.transform(image).unsqueeze(0), requires_grad=True)
    net.zero_grad()
    for i in range(iterations):
        #         print('Iteration: ', i)
        out = input
        for j in range(layer):
            out = modulelist[j + 1](out)
            loss = out.norm()
            loss.backward()
            input.data = input.data + lr * input.grad.data
    input = input.data.squeeze()
    input.transpose_(0, 1)
    input.transpose_(1, 2)
    input = np.clip(deprocess(input), 0, 1)
    im = Image.fromarray(np.uint8(input * 255))
    return im
Beispiel #5
0
            #decode.getforwardconvlayer()
            dataiter = iter(encode.testloader)
            images, labels = dataiter.next()
            image = torchvision.transforms.ToPILImage()(images[3])
            print(image)
            layer = 1
            iterations = 5
            lr = 0.4
            octave_scale = 1
            num_octaves = 1
            decode.deep_dream_image(image, layer, iterations, lr, octave_scale,
                                    num_octaves)

        elif (cmdlist[0] == "save"):
            torch.save(model.state_dict(), cmdlist[1])

        elif (cmdlist[0] == "load"):
            if (ismodel(cmdlist[1])):
                model = neuralnet.netfunction()
                model.load_state_dict(torch.load(cmdlist[1]))
                model.eval()

        elif (cmdlist[0] == "classify"):
            print("classify")

        elif (cmdlist[0] == "quit"):
            print('end program')
            exit()
        else:
            print("invalid command")