Python Forum

Full Version: How to invert pixel numbers of MNIST data set
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
So I'm trying to:
a) invert the pixel values of the mnist training data set and
b) run it in 5 epochs

I have zero experience or idea of how to do this. Have been googling for hours but now just want a straight up answer. :(


import numpy as np
from PIL import Image

epochs = 5

for e in range(epochs):
    for record in training_data_list:
        all_values = record.split(',')
        inputs = (numpy.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01

training_data_file = open("mnist_train.csv", 'r')
training_data_list = training_data_file.readlines()
training_data_file.close()

test_data_file = open("mnist_test.csv", 'r')
test_data_list = test_data_file.readlines()
test_data_file.close()

#def inverseImageBW_array(originalImage):
#    temp = 1 - originalImage
#    temp = -1.* originalImage
#    return temp
    
targets = numpy.zeros(output_nodes) + 0.01
targets[int(all_values[0])] = 0.99
n.train(inputs, targets)
pass
pass 
This is my next cell


 test_data_file = open("mnist_test.csv", 'r')
test_data_list = test_data_file.readlines()
test_data_file.close()

scorecard = []

for record in test_data_list:
    all_values = record.split(',')
    correct_label = int(all_values[0])
    inputs = (numpy.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01
    outputs = n.query(inputs)
    label = numpy.argmax(outputs)
    if (label == correct_label):
        scorecard.append(1)
    else:
        scorecard.append(0)
        pass    
    pass

scorecard_array = numpy.asarray(scorecard)
print ("performance = ", scorecard_array.sum() * 100 / scorecard_array.size) 
I suspect you've used a dataset which is similar to this one.

As far as I understand, pixel inversion is transformation when black colors become white and vice verse?! If so, the following code can help you:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
data=pd.read_csv("https://raw.githubusercontent.com/sjwhitworth/golearn/master/examples/datasets/mnist_train.csv")
data.values[:, 1:] = np.abs(data.values[:, 1:] - 255)  # color inversion
x=data.values[-1, 1:]  # plot latest row in the dataset
plt.imshow(x.reshape(28, 28), cmap='gray')
plt.show()