Python Forum
How to invert pixel numbers of MNIST data set
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to invert pixel numbers of MNIST data set
#1
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) 
Reply
#2
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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Voxel to pixel graphics voicemail 3 580 Nov-19-2023, 09:45 AM
Last Post: paul18fr
  Pixel color and action Sartre 4 2,083 Apr-13-2023, 03:26 AM
Last Post: Sartre
  Dynamic pixel display jerryf 2 726 Mar-17-2023, 07:26 PM
Last Post: jerryf
  How to invert scatter plot axis Mark17 3 2,520 Sep-22-2021, 04:45 PM
Last Post: jefsummers
  Plotting Pixel Intensity Doev 0 1,724 Oct-21-2020, 10:56 PM
Last Post: Doev
  5 variants to invert dictionaries with non-unique values Drakax1 2 2,611 Aug-31-2020, 11:40 AM
Last Post: snippsat
  Invert Pillow image colours chesschaser 5 3,786 Jul-11-2020, 02:59 PM
Last Post: chesschaser
  Return draw.rectangle pixel coordinate data to .csv file CephloRhod 0 2,387 May-20-2020, 10:37 AM
Last Post: CephloRhod
  What is the best way to search for a pixel in a screenshot? TheZadok42 1 2,599 May-15-2020, 12:37 PM
Last Post: scidam
  Issue with creating an array of pixel data for PNG files in Google Colab The_Sarco 1 1,931 Apr-29-2020, 12:03 AM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020